Bladeren bron

优化 SXService.cs 的排序、时间计算和日志记录

- 添加 `using Newtonsoft.Json;` 和 `using System.Diagnostics;` 的引用
- 修改 `invlist` 的排序规则
- 调整 `startTime` 和 `endTime` 的计算方式
- 修改 `if` 条件判断逻辑
- 在 `ApplyFloor` 方法中添加 `Stopwatch` 计时器
- 优化 `cell` 列表的查询逻辑,减少多次遍历
- 添加日志记录,记录方法执行的总耗时及各步骤耗时
林豪 左 5 maanden geleden
bovenliggende
commit
db3d2696de
1 gewijzigde bestanden met toevoegingen van 35 en 9 verwijderingen
  1. 35 9
      wms.service/Service/SXService.cs

+ 35 - 9
wms.service/Service/SXService.cs

@@ -6,6 +6,7 @@ using SqlSugar.Extensions;
 using System;
 using System.Collections.Generic;
 using System.Data;
+using System.Diagnostics;
 using System.Linq;
 using System.Linq.Expressions;
 using WCS.Entity.sx;
@@ -782,7 +783,7 @@ namespace wms.service.Service
             {
                 var invlist = from loc in _basewarecellrepository.GetList(p => p.IsStop == LocationStop.LocationInvoke.GetHashCode() && p.StateNum == LocationState.LocationState_Full)
                               join stock in _billInvnowrepository.GetList(p => p.InvStateCode == InvState.InvEcecState_In.ToString() && p.ExecStateCode == InvLockState.InvState_Normal.ToString() && p.IsTorsChk == true && string.IsNullOrEmpty(p.PreStock) && p.ContUsageQty <= 0 && p.TorsChkQty < 1
-                              && !p.InDocsNo.StartsWith("CHA") && p.Grade == "A")
+                              && !p.ProductMachCode.Contains("R") && p.Grade == "A")
                               on loc.ContGrpBarCode equals stock.ContGrpBarCode
                               select new
                               {
@@ -843,7 +844,7 @@ namespace wms.service.Service
                     res.ResMsg = ResponseStatusCodeEnum.NotEnoughStock.GetDescription() + "没有需要扭转检测的工字轮";
                     return res;
                 }
-                invlist = invlist.OrderBy(p => p.Depth).ThenBy(p => p.ProductTime);
+                invlist = invlist.OrderBy(p => p.Depth).ThenBy(p => p.ProductTime.Day);
                 var tasklist = _taskrepository.AsQueryable().With(SqlWith.NoLock).Where(p => p.BusType == SxTaskBusType.TaskBusType_SX_TorCheckStockOut.GetDescription() && p.Type == TaskType.OutDepot).ToList();
                 var torTaskCount = _sysconfigrepository.GetFirst(p => p.Code == "TorTaskCount").SContent;
                 if (tasklist.Any() && tasklist.Count > int.Parse(torTaskCount))
@@ -926,9 +927,9 @@ namespace wms.service.Service
                     var time2 = item.HoldTime - decimal.Parse(TorschkDate.Default1); //时效下限
                                                                                      //|| (decimal.Parse((DateTime.Now - item.ProductTime).TotalHours.ToString("f2")) > 72 && decimal.Parse((DateTime.Now - item.ProductTime).TotalHours.ToString("f2")) > item.HoldTime)
                                                                                      //if (decimal.Parse((DateTime.Now - item.ProductTime).TotalHours.ToString("f2")) >= time2) //按小时判断是否扭转的方式应客户要求停用-2024-12-24
-                    var startTime = DateTime.Now.Date.AddDays(-2).AddHours(+11);
+                    var startTime = DateTime.Now.Date.AddDays(-1).AddMilliseconds(-1);
                     var endTime = startTime.AddDays(+1).AddMilliseconds(-1);
-                    if (item.ProductTime >= startTime && item.ProductTime <= endTime) //时间在此范围即可流转,例:3号标记的检测盘(11:00到次日11:00之间),5号全部出库进行检测(7点开始出
+                    if (item.ProductTime <= startTime) //时间在此范围即可流转,例:3号标记的检测盘(00:00到23:59之间),5号全部出库进行检测(7点开始出
                     {
                         var inv = _billInvnowrepository.GetSingle(p =>
                             p.ContGrpBarCode == item.ContGrpBarCode &&
@@ -1324,6 +1325,8 @@ namespace wms.service.Service
         /// <returns></returns>
         public SRes<ApplyFloorResponse> ApplyFloor(ApplyFloorRequest reqDto)
         {
+            var time = new Stopwatch();
+            time.Restart();
             var result = new SRes<ApplyFloorResponse>()
             {
                 ResCode = ResponseStatusCodeEnum.Sucess.GetHashCode(),
@@ -1449,19 +1452,37 @@ namespace wms.service.Service
             var existstock = _billInvnowrepository.AsQueryable().With(SqlWith.NoLock).Where(p => p.InvStateCode == InvState.InvEcecState_In.ToString() && p.Wind == stock.Wind && p.HWTypeCode == stock.HWTypeCode && p.WbGroupCode == stock.WbGroupCode && p.IsControlpanel == stock.IsControlpanel && p.IsTorsChk == stock.IsTorsChk && p.Ovced == stock.Ovced).ToList();
             //去掉禁用巷道
             List<int> distuns = new List<int>();
-
-            var cell = _basewarecellrepository.AsQueryable().With(SqlWith.NoLock).Where(p => p.IsStop == LocationStop.LocationInvoke.GetHashCode() && p.StateNum == LocationState.LocationState_Empty).ToList();
+            var time1 = new Stopwatch();
+            time1.Restart();
+            var time2 = new Stopwatch();
+            time2.Restart();
+            // 提前查询并返回符合条件的 cell 列表
+            var cellQuery = _basewarecellrepository.AsQueryable()
+                .With(SqlWith.NoLock)
+                .Where(p => p.IsStop == LocationStop.LocationInvoke.GetHashCode() && p.StateNum == LocationState.LocationState_Empty);
+
+            // 获取禁用的巷道信息
             var disabletunnels = _sysconfigrepository.GetList(p => p.SType == "InStop" && !string.IsNullOrEmpty(p.SContent));
             if (disabletunnels != null && disabletunnels.Any())
             {
+                // 先提取禁用的巷道数据到集合中
+                var disableTunnelIds = new HashSet<(int Tunnel, int Floor)>();
+
                 foreach (var item in disabletunnels)
                 {
                     var distun = int.Parse(item.Default1);
                     var disfloor = int.Parse(item.Default2);
-                    var disids = cell.Where(p => p.Floor == disfloor && p.Tunnel == distun).Select(p => p.Id);
-                    cell = cell.Where(p => !disids.Contains(p.Id)).ToList();
+                    disableTunnelIds.Add((distun, disfloor));
                 }
+
+                // 在数据库查询时进行过滤,避免多次遍历 cell 列表
+                cellQuery = cellQuery.Where(p => disableTunnelIds.All(x => x.Tunnel != p.Tunnel && x.Floor != p.Floor));
             }
+
+            // 执行查询,获取最终的 cell 列表
+            var cell = cellQuery.ToList();
+
+            time2.Stop();
             bool isTobeTors = false;
             int tun = 2;
             int row = 8;
@@ -1496,7 +1517,8 @@ namespace wms.service.Service
 
             //每个扫码固定楼层,如果固定楼层设备坏了或者库存满了,再换楼层
             var taskcount = _taskrepository.AsQueryable().With(SqlWith.NoLock).Where(p => p.Type == TaskType.EnterDepot && p.Status < TaskStatus.Finish && p.Floor != 0).ToList();
-
+            var time10 = new Stopwatch();
+            time10.Restart();
             if (tempcells[0].Item2 > 30)
             {
                 if (taskcount.Where(p => p.Floor == tempcells[0].Item1).Count() > floorTaskCount || tempcells[0].Item2 == 0)
@@ -1563,6 +1585,10 @@ namespace wms.service.Service
                     }
                 }
             }
+            time10.Stop();
+            time1.Stop();
+            time.Stop();
+            _logger.LogInformation($"ApplyFloor:{reqDto.Code}--总耗时{time.ElapsedMilliseconds}-可用楼层计算耗时{time1.ElapsedMilliseconds}-排除禁用巷道{time2.ElapsedMilliseconds}-计算目标楼层耗时{time2.ElapsedMilliseconds}");
 
             #region 逻辑暂时不用(不换楼层优先)