From 73bd807e31d05723cb06fee3a4fd144867bcf8cb Mon Sep 17 00:00:00 2001 From: wenjy Date: Wed, 25 Jun 2025 16:15:17 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20SqlSugar=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98=E5=A4=84=E7=90=86=E6=B5=8B?= =?UTF-8?q?=E8=AF=95StartCheckStatus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.WCS/MainCentralControl.cs | 56 ++++++++++++++++++----------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/SlnMesnac.WCS/MainCentralControl.cs b/SlnMesnac.WCS/MainCentralControl.cs index cb5205a..f7d3a8f 100644 --- a/SlnMesnac.WCS/MainCentralControl.cs +++ b/SlnMesnac.WCS/MainCentralControl.cs @@ -111,8 +111,8 @@ namespace SlnMesnac.WCS private void StartCheckStatus() { //数据库里面的读写器集合,仅用来更新状态 - List rfidDbList = SqlSugarClient.Queryable().ToList(); - List agvEquipList = SqlSugarClient.Queryable().Where(it => it.EquipType == 1).ToList(); + List rfidDbList = SqlSugarClient.Ado.Context.CopyNew().Queryable().ToList(); + List agvEquipList = SqlSugarClient.Ado.Context.CopyNew().Queryable().Where(it => it.EquipType == 1).ToList(); Task.Run(async () => { while (true) @@ -161,7 +161,17 @@ namespace SlnMesnac.WCS rfidDb.IsOnline = status ? 1 : 0; } } - SqlSugarClient.Updateable(rfidDbList).UpdateColumns(it => new { it.IsOnline }).ExecuteCommand(); + lock (string.Empty) + { + try + { + SqlSugarClient.Ado.Context.CopyNew().Updateable(rfidDbList).UpdateColumns(it => new { it.IsOnline }).ExecuteCommand(); + } + catch (Exception ex) + { + _logger.Error($"更新RFID状态异常:{ex.Message}",ex); + } + } #endregion RFID状态 //色粉库位有未取走色粉声光报警 @@ -211,7 +221,7 @@ namespace SlnMesnac.WCS { PlcAbsractFactory? workShop3Plc = plcList.FirstOrDefault(x => x.ConfigKey == "workShop3Plc"); // 构建查询并执行 Any 方法 - bool hasRecords = SqlSugarClient.Queryable() + bool hasRecords = SqlSugarClient.Ado.Context.CopyNew().Queryable() .InnerJoin((wbe, wpi) => wbe.ContainerCode == wpi.PalletInfoCode && !string.IsNullOrEmpty(wbe.ContainerCode) && wpi.TonerFlag == 1 && wbe.EquipType == 7).Any(); @@ -240,7 +250,9 @@ namespace SlnMesnac.WCS { try { - List agvList = SqlSugarClient.Queryable().ToList(); + + //List agvList = SqlSugarClient.Queryable().ToList(); + List agvList = SqlSugarClient.Ado.Context.CopyNew().Queryable().ToList(); var data = new { reqCode = StaticData.SnowId.NextId(), @@ -274,7 +286,8 @@ namespace SlnMesnac.WCS } //TaskHandleByAgv(agvList); - SqlSugarClient.Updateable(agvList).ExecuteCommand(); + //SqlSugarClient.Updateable(agvList).ExecuteCommand(); + SqlSugarClient.Ado.Context.CopyNew().Updateable(agvList).ExecuteCommand(); } } catch (Exception ex) @@ -327,12 +340,13 @@ namespace SlnMesnac.WCS { Task.Run(() => { - List agvEquipList = SqlSugarClient.Queryable().Where(it => it.EquipType == 1).ToList(); + var sqlClient = SqlSugarClient.Ado.Context.CopyNew(); + List agvEquipList = SqlSugarClient.Ado.Context.CopyNew().Queryable().Where(it => it.EquipType == 1).ToList(); while (true) { try { - List wcsTasks = SqlSugarClient.Queryable().Where(t => t.IsDelete == 1 || t.IsDelete == 2).ToList(); + List wcsTasks = SqlSugarClient.Ado.Context.CopyNew().Queryable().Where(t => t.IsDelete == 1 || t.IsDelete == 2).ToList(); foreach (var item in wcsTasks) { WcsBaseEquip agvEquip = agvEquipList.First(); @@ -353,30 +367,30 @@ namespace SlnMesnac.WCS List bigType = new List() { 1, 3, 8, 10 }; if (bigType.Contains(item.TaskType)) { - List? wmsBaseLocations = SqlSugarClient.Queryable().Where(x => x.LocationCode == item.CurrPointNo || x.LocationCode == item.EndPointNo).ToList(); + List? wmsBaseLocations = SqlSugarClient.Ado.Context.CopyNew().Queryable().Where(x => x.LocationCode == item.CurrPointNo || x.LocationCode == item.EndPointNo).ToList(); if (wmsBaseLocations != null && wmsBaseLocations.Count > 0) { wmsBaseLocations.ForEach(x => x.LocationStatus = 0); - SqlSugarClient.Updateable(wmsBaseLocations).ExecuteCommand(); + SqlSugarClient.Ado.Context.CopyNew().Updateable(wmsBaseLocations).ExecuteCommand(); } } else //如果是小料箱,解锁wcsbaseEquip { - List equips = SqlSugarClient.Queryable().Where(x => x.AgvPositionCode == item.CurrPointNo || x.AgvPositionCode == item.EndPointNo).ToList(); + List equips = SqlSugarClient.Ado.Context.CopyNew().Queryable().Where(x => x.AgvPositionCode == item.CurrPointNo || x.AgvPositionCode == item.EndPointNo).ToList(); if (equips != null && equips.Count > 0) { equips.ForEach(x => x.EquipStatus = 0); - SqlSugarClient.Updateable(equips).ExecuteCommand(); + SqlSugarClient.Ado.Context.CopyNew().Updateable(equips).ExecuteCommand(); } } #endregion - SqlSugarClient.Deleteable(item).ExecuteCommand(); + SqlSugarClient.Ado.Context.CopyNew().Deleteable(item).ExecuteCommand(); _logger.Agv($"任务id:{item.Id},任务名:{item.TaskName}已删除"); // if (!string.IsNullOrEmpty(item.NextPointNo) && item.TaskStatus >= 2 && item.IsDelete == 2) //有执行任务的agv编号,并且需要去异常库位 { - WmsBaseLocation? endLocation = SqlSugarClient.Queryable().First(x => x.LocationCode == "3066" && x.LocationStatus == 0 && string.IsNullOrEmpty(x.ContainerCode)); + WmsBaseLocation? endLocation = SqlSugarClient.Ado.Context.CopyNew().Queryable().First(x => x.LocationCode == "3066" && x.LocationStatus == 0 && string.IsNullOrEmpty(x.ContainerCode)); if (endLocation != null) { WcsTask task = new WcsTask(); @@ -389,23 +403,23 @@ namespace SlnMesnac.WCS task.CreatedBy = "wcs"; task.TaskName = "前往异常库位移库任务"; task.PalletInfoCode = item.PalletInfoCode; - SqlSugarClient.AsTenant().BeginTran(); + sqlClient.AsTenant().BeginTran(); try { - int id = SqlSugarClient.Insertable(task).ExecuteReturnIdentity(); + int id = SqlSugarClient.Ado.Context.CopyNew().Insertable(task).ExecuteReturnIdentity(); WcsTaskLog wcsTaskLog = CoreMapper.Map(task); wcsTaskLog.Id = id; //锁定库位 endLocation.LocationStatus = 1; - SqlSugarClient.Updateable(endLocation).ExecuteCommand(); - SqlSugarClient.Insertable(wcsTaskLog).ExecuteCommand(); - SqlSugarClient.AsTenant().CommitTran(); + SqlSugarClient.Ado.Context.CopyNew().Updateable(endLocation).ExecuteCommand(); + SqlSugarClient.Ado.Context.CopyNew().Insertable(wcsTaskLog).ExecuteCommand(); + sqlClient.AsTenant().CommitTran(); _logger.Agv($"生成{task.TaskName},起点:{task.CurrPointNo},终点:{task.EndPointNo}"); } catch (Exception ex) { _logger.Error($"生成移库任务提交事务异常:{ex.Message}"); - SqlSugarClient.AsTenant().RollbackTran(); + sqlClient.AsTenant().RollbackTran(); } } else @@ -421,7 +435,7 @@ namespace SlnMesnac.WCS } else { - SqlSugarClient.Deleteable(item).ExecuteCommand(); + sqlClient.Deleteable(item).ExecuteCommand(); _logger.Agv($"任务id:{item.Id},任务名:{item.TaskName}已删除"); } }