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 locationSqlFunction : SqlFunctionFactory { private BaseLocation? baseLocation; private ISqlSugarClient _sqlSugarClient; private readonly SerilogHelper _logger; public locationSqlFunction(IServiceProvider serviceProvider) { _sqlSugarClient = serviceProvider.GetRequiredService(); _logger = serviceProvider.GetRequiredService(); } public override void deleteFunction(BaseLocation _entity) { var wcsDb = _sqlSugarClient.AsTenant().GetConnection("wcs"); baseLocation = _entity; try { List baseLocationInfos = wcsDb.Queryable().Where(it => it.LocationCode == baseLocation.LocationCode).ToList(); if (baseLocationInfos.Count == 0) { _logger.Info("不存在Location配置"); return; } foreach (var item in baseLocationInfos) { wcsDb.Ado.BeginTran(); wcsDb.Deleteable(item).ExecuteCommand(); wcsDb.Ado.CommitTran(); } } catch (Exception ex) { wcsDb.AsTenant().RollbackTran(); _logger.Error($"删除Location设置异常{ex.Message}"); } } public override void insertFunction(BaseLocation _entity) { var wcsDb = _sqlSugarClient.AsTenant().GetConnection("wcs"); baseLocation = _entity; try { bool hasTask = wcsDb.Queryable().Any(it => it.LocationCode == baseLocation.LocationCode); if (hasTask) { _logger.Info("已有该Location配置"); return; } wcsDb.Ado.BeginTran(); wcsDb.Insertable(baseLocation).ExecuteCommand(); wcsDb.Ado.CommitTran(); } catch (Exception ex) { wcsDb.AsTenant().RollbackTran(); _logger.Error($"插入Location设置异常{ex.Message}"); } } public override List searchFunction(BaseLocation _entity) { var wcsDb = _sqlSugarClient.AsTenant().GetConnection("wcs"); baseLocation = _entity; try { // 根据 ConfigKey 查询配置信息 List result = wcsDb.Queryable() .Where(it => it.LocationCode == baseLocation.LocationCode) .ToList(); if (result.Count == 0) { _logger.Info($"未找到 ConfigKey 为 {baseLocation?.LocationCode} 的Location配置"); // 可以根据需要返回 null 或者抛出异常 return null; } _logger.Info($"成功查询Location配置: {baseLocation?.LocationCode}"); return result; } catch (Exception ex) { _logger.Error($"查询Location设置异常: {ex.Message}"); return null; } } public override void updateFunction(BaseLocation _entity) { var wcsDb = _sqlSugarClient.AsTenant().GetConnection("wcs"); baseLocation = _entity; try { // 检查是否存在该配置 bool exists = wcsDb.Queryable() .Any(it => it.LocationCode == baseLocation.LocationCode); if (!exists) { _logger.Info($"不存在 LocationCode 为 {baseLocation.LocationCode} 的物料信息,无法更新"); return; } // 开始事务 wcsDb.Ado.BeginTran(); var updateResult = wcsDb.Updateable(baseLocation).ExecuteCommand(); wcsDb.Ado.CommitTran(); if (updateResult > 0) { _logger.Info($"成功更新LocationCode配置: {baseLocation.LocationCode}"); } else { _logger.Info($"更新LocationCode配置影响行数为0: {baseLocation.LocationCode}"); } } catch (Exception ex) { wcsDb.AsTenant().RollbackTran(); _logger.Error($"更新LocationCode设置异常: {ex.Message}"); throw; // 或者根据业务需求决定是否抛出 } } } }