You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.9 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Sln.Wcs.Model;
using Sln.Wcs.Model.Domain;
using Sln.Wcs.Serilog;
using SqlSugar;
namespace Sln.Wcs.Common.Functions
{
public class plcSqlFunction : SqlFunctionFactory<BaseConfigInfo>
{
private BaseConfigInfo? baseConfiginfo;
private ISqlSugarClient _sqlSugarClient;
private readonly SerilogHelper _logger;
public plcSqlFunction(IServiceProvider serviceProvider)
{
_sqlSugarClient = serviceProvider.GetRequiredService<ISqlSugarClient>();
_logger = serviceProvider.GetRequiredService<SerilogHelper>();
}
public override void insertFunction(BaseConfigInfo _entity)
{
var wcsDb = _sqlSugarClient.AsTenant().GetConnection("wcs");
baseConfiginfo = _entity;
try
{
bool hasTask = wcsDb.Queryable<BaseConfigInfo>().Any(it => it.ConfigKey == baseConfiginfo.ConfigKey);
if (hasTask)
{
_logger.Info("已有该Plc配置");
return;
}
wcsDb.Ado.BeginTran();
wcsDb.Insertable(baseConfiginfo).ExecuteCommand();
wcsDb.Ado.CommitTran();
}
catch (Exception ex)
{
wcsDb.AsTenant().RollbackTran();
_logger.Error($"插入Plc设置异常{ex.Message}");
}
}
public override void deleteFunction(BaseConfigInfo _entity)
{
var wcsDb = _sqlSugarClient.AsTenant().GetConnection("wcs");
baseConfiginfo = _entity;
try
{
List<BaseConfigInfo> baseConfigs = wcsDb.Queryable<BaseConfigInfo>().Where(it => it.ConfigKey == baseConfiginfo.ConfigKey).ToList();
if (baseConfigs.Count == 0)
{
_logger.Info("不存在Plc配置");
return;
}
foreach (var item in baseConfigs)
{
wcsDb.Ado.BeginTran();
wcsDb.Deleteable<BaseConfigInfo>(item).ExecuteCommand();
wcsDb.Ado.CommitTran();
}
}
catch (Exception ex)
{
wcsDb.AsTenant().RollbackTran();
_logger.Error($"删除Plc设置异常{ex.Message}");
}
}
public override List<BaseConfigInfo> searchFunction(BaseConfigInfo _entity)
{
var wcsDb = _sqlSugarClient.AsTenant().GetConnection("wcs");
baseConfiginfo = _entity;
try
{
// 根据 ConfigKey 查询配置信息
List<BaseConfigInfo> result = wcsDb.Queryable<BaseConfigInfo>()
.Where(it => it.ConfigKey == baseConfiginfo.ConfigKey)
.ToList();
if (result.Count == 0)
{
_logger.Info($"未找到 ConfigKey 为 {baseConfiginfo?.ConfigKey} 的Plc配置");
// 可以根据需要返回 null 或者抛出异常
return null;
}
_logger.Info($"成功查询Plc配置: {baseConfiginfo?.ConfigKey}");
return result;
}
catch (Exception ex)
{
_logger.Error($"查询Plc设置异常: {ex.Message}");
return null;
}
}
public override void updateFunction(BaseConfigInfo _entity)
{
var wcsDb = _sqlSugarClient.AsTenant().GetConnection("wcs");
baseConfiginfo = _entity;
try
{
// 检查是否存在该配置
bool exists = wcsDb.Queryable<BaseConfigInfo>()
.Any(it => it.ConfigKey == baseConfiginfo.ConfigKey);
if (!exists)
{
_logger.Info($"不存在 ConfigKey 为 {baseConfiginfo.ConfigKey} 的Plc配置无法更新");
return;
}
// 开始事务
wcsDb.Ado.BeginTran();
var updateResult = wcsDb.Updateable(baseConfiginfo).ExecuteCommand();
wcsDb.Ado.CommitTran();
if (updateResult > 0)
{
_logger.Info($"成功更新Plc配置: {baseConfiginfo.ConfigKey}");
}
else
{
_logger.Info($"更新Plc配置影响行数为0: {baseConfiginfo.ConfigKey}");
}
}
catch (Exception ex)
{
wcsDb.AsTenant().RollbackTran();
_logger.Error($"更新Plc设置异常: {ex.Message}");
throw; // 或者根据业务需求决定是否抛出
}
}
}
}