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