From 4b483d9d06bead231b88ca212fd799196668a057 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 28 四月 2026 22:58:27 +0800
Subject: [PATCH] fix(任务服务): 修复出库任务选择逻辑和货位分配问题

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_BasicService/LocationInfoService.cs |   70 ++++++++++++++++++++++++++++-------
 1 files changed, 56 insertions(+), 14 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_BasicService/LocationInfoService.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_BasicService/LocationInfoService.cs
index aff926c..9594985 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_BasicService/LocationInfoService.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_BasicService/LocationInfoService.cs
@@ -25,6 +25,7 @@
         private readonly IRepository<Dt_Task> _taskRepository;
         private readonly IRepository<Dt_StockInfo> _stockInfoRepository;
         private readonly IRecordService _recordService;
+        private readonly IRepository<Dt_Warehouse> _warehouseRepository;
 
         /// <summary>
         /// 鏋勯�犲嚱鏁�
@@ -36,6 +37,7 @@
             IRepository<Dt_LocationInfo> baseDal,
             IRepository<Dt_Task> taskRepository,
             IRepository<Dt_StockInfo> stockInfoRepository,
+            IRepository<Dt_Warehouse> warehouseRepository,
             IMapper mapper,
             IRecordService recordService) : base(baseDal)
         {
@@ -43,6 +45,7 @@
             _stockInfoRepository = stockInfoRepository;
             _mapper = mapper;
             _recordService = recordService;
+            _warehouseRepository = warehouseRepository;
         }
 
         /// <summary>
@@ -130,22 +133,43 @@
 
         /// <summary>
         /// 鏍规嵁宸烽亾鑾峰彇绌洪棽璐т綅淇℃伅
+        /// 鎺掑簭绛栫暐锛氭繁搴︿紭鍏堬紙浜屾繁浣嶄紭鍏堬級锛屽叾娆℃寜灞傘�佸垪銆佽鍗囧簭
         /// </summary>
         /// <param name="roadwayNo">宸烽亾缂栧彿</param>
-        /// <returns>绌洪棽璐т綅淇℃伅锛屽鏋滄湭鎵惧埌鍒欒繑鍥瀗ull</returns>
+        /// <returns>绌洪棽璐т綅淇℃伅锛屽鏋滅┖闂茶揣浣嶄笉瓒冲垯杩斿洖null</returns>
         public async Task<Dt_LocationInfo?> GetLocationInfo(string roadwayNo)
         {
-            var locations = await BaseDal.QueryDataAsync(x =>
-                x.EnableStatus == EnableStatusEnum.Normal.GetHashCode() &&
-                x.RoadwayNo == roadwayNo &&
-                x.LocationStatus == LocationStatusEnum.Free.GetHashCode());
+            // HC 宸烽亾浣跨敤 Capacity 绫诲瀷锛屽叾浠栧贩閬撲娇鐢� ShelfCapacity 绫诲瀷
+            var locationType = roadwayNo.Contains("HC")
+                ? (int)LocationTypeEnum.Capacity
+                : (int)LocationTypeEnum.ShelfCapacity;
 
-            return locations?
+            var enableStatus = EnableStatusEnum.Normal.GetHashCode();
+            var freeStatus = LocationStatusEnum.Free.GetHashCode();
+
+            // 鏁版嵁搴撶 COUNT 妫�鏌ョ┖闂茶揣浣嶆暟閲忥紙浠呰繑鍥炰竴涓暟瀛楋紝鏃犳暟鎹紶杈撳紑閿�锛�
+            var freeCount = await BaseDal.Db.Queryable<Dt_LocationInfo>()
+                .Where(x => x.EnableStatus == enableStatus
+                    && x.RoadwayNo == roadwayNo
+                    && x.LocationStatus == freeStatus
+                    && x.LocationType == locationType)
+                .CountAsync();
+
+            // 绌洪棽璐т綅涓嶈冻鏈�浣庝繚鐣欐暟閲忔椂杩斿洖null锛岄伩鍏嶅皢宸烽亾鍒嗛厤鑰楀敖
+            const int minFreeLocationThreshold = 5;
+            if (freeCount < minFreeLocationThreshold) return null;
+
+            // 鏁版嵁搴撶鎺掑簭鍙栫涓�鏉★紙鍙紶杈撳崟琛屾暟鎹級
+            return await BaseDal.Db.Queryable<Dt_LocationInfo>()
+                .Where(x => x.EnableStatus == enableStatus
+                    && x.RoadwayNo == roadwayNo
+                    && x.LocationStatus == freeStatus
+                    && x.LocationType == locationType)
+                .OrderByDescending(x => x.Depth)
                 .OrderBy(x => x.Layer)
-                .ThenByDescending(x => x.Depth)
-                .ThenBy(x => x.Column)
-                .ThenBy(x => x.Row)
-                .FirstOrDefault();
+                .OrderBy(x => x.Column)
+                .OrderBy(x => x.Row)
+                .FirstAsync();
         }
 
         /// <summary>
@@ -167,6 +191,16 @@
         public async Task<Dt_LocationInfo> GetLocationInfoAsync(string locationCode)
         {
             return await BaseDal.QueryFirstAsync(x => x.LocationCode == locationCode);
+        }
+
+        /// <summary>
+        /// 鏍规嵁璐т綅ID鑾峰彇璐т綅淇℃伅
+        /// </summary>
+        /// <param name="id">璐т綅id</param>
+        /// <returns>璐т綅淇℃伅</returns>
+        public async Task<Dt_LocationInfo> GetLocationInfoAsync(int id)
+        {
+            return await BaseDal.QueryFirstAsync(x => x.Id == id);
         }
 
         /// <summary>
@@ -295,7 +329,7 @@
             // 鍒ゆ柇璇ヤ綅缃槸鍚︽湁搴撳瓨
             var stockInfo = await _stockInfoRepository.QueryDataNavFirstAsync(x =>
                             x.LocationCode == newLocationID &&
-                            x.StockStatus == StockStatusEmun.鍏ュ簱瀹屾垚.GetHashCode() &&
+                            (x.StockStatus == StockStatusEmun.鍏ュ簱瀹屾垚.GetHashCode() || x.StockStatus == StockStatusEmun.绌烘墭鐩樺簱瀛�.GetHashCode()) &&
                             x.LocationDetails.LocationStatus == LocationStatusEnum.InStock.GetHashCode());
             if (stockInfo == null)
             {
@@ -494,11 +528,18 @@
         /// <param name="layer">灞傛暟</param>
         /// <param name="depth">娣卞害</param>
         /// <returns>璐т綅淇℃伅瀵硅薄</returns>
-        private static Dt_LocationInfo CreateLocationInfo(string roadwayNo, int row, int col, int layer, int depth)
+        private Dt_LocationInfo CreateLocationInfo(string roadwayNo, int row, int col, int layer, int depth)
         {
+            var warehouse = _warehouseRepository.QueryData(x => x.WarehouseCode == roadwayNo).FirstOrDefault();
+
+            if (warehouse == null)
+            {
+                throw new InvalidOperationException($"鏈壘鍒板贩閬撶紪鍙蜂负 {roadwayNo} 鐨勪粨搴撲俊鎭�");
+            }
+
             return new Dt_LocationInfo
             {
-                WarehouseId = 0,
+                WarehouseId = warehouse.WarehouseId,
                 Row = row,
                 Column = col,
                 Layer = layer,
@@ -507,6 +548,7 @@
                 EnableStatus = EnableStatusEnum.Normal.GetHashCode(),
                 LocationStatus = LocationStatusEnum.Free.GetHashCode(),
                 LocationType = LocationTypeEnum.Undefined.GetHashCode(),
+                //LocationCode = $"{roadwayNo}-{row:D3}-{col:D3}-{layer:D3}",
                 LocationCode = $"{row:D3}-{col:D3}-{layer:D3}",
                 LocationName = $"{roadwayNo}宸烽亾{row:D3}琛寋col:D3}鍒梴layer:D3}灞倇depth:D2}娣�"
             };
@@ -514,4 +556,4 @@
 
         #endregion 绉佹湁鏂规硶
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3