From a87dfb6174dcb3072f41f281d9e860dcf8ffc56d Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期一, 30 三月 2026 12:50:44 +0800
Subject: [PATCH] feat(StockInfoService): 实现 Get3DLayoutAsync 方法

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockInfoService.cs |  104 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 104 insertions(+), 0 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockInfoService.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockInfoService.cs
index 8e4027b..71f1e9b 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockInfoService.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockInfoService.cs
@@ -1,6 +1,7 @@
 using WIDESEA_Common.StockEnum;
 using WIDESEA_Core.BaseRepository;
 using WIDESEA_Core.BaseServices;
+using WIDESEA_DTO.Stock;
 using WIDESEA_IStockService;
 using WIDESEA_Model.Models;
 
@@ -78,5 +79,108 @@
         {
             return await BaseDal.QueryFirstAsync(x => x.PalletCode == palletCode && x.LocationCode == locationCode);
         }
+
+        /// <summary>
+        /// 鑾峰彇浠撳簱3D甯冨眬鏁版嵁
+        /// </summary>
+        /// <param name="warehouseId">浠撳簱ID</param>
+        /// <returns>3D甯冨眬DTO</returns>
+        public async Task<Stock3DLayoutDTO> Get3DLayoutAsync(int warehouseId)
+        {
+            // 1. 鏌ヨ浠撳簱淇℃伅
+            var warehouse = await Repository.Change<Dt_Warehouse>().GetFirstAsync(x => x.Id == warehouseId);
+
+            // 2. 鏌ヨ璇ヤ粨搴撴墍鏈夎揣浣�
+            var locations = await Repository.Change<Dt_LocationInfo>().GetListAsync(x => x.WarehouseId == warehouseId);
+
+            // 3. 鏌ヨ璇ヤ粨搴撴墍鏈夊簱瀛樹俊鎭紙鍖呭惈Details瀵艰埅灞炴�э級
+            var stockInfos = await Repository.Change<Dt_StockInfo>().Includes(x => x.Details).GetListAsync(x => x.WarehouseId == warehouseId);
+
+            // 4. 鎻愬彇鐗╂枡缂栧彿鍜屾壒娆″彿鍒楄〃锛堝幓閲嶏級
+            var materielCodeList = stockInfos
+                .SelectMany(s => s.Details)
+                .Select(d => d.MaterielCode)
+                .Where(c => !string.IsNullOrEmpty(c))
+                .Distinct()
+                .ToList();
+
+            var batchNoList = stockInfos
+                .SelectMany(s => s.Details)
+                .Select(d => d.BatchNo)
+                .Where(b => !string.IsNullOrEmpty(b))
+                .Distinct()
+                .ToList();
+
+            // 5. 鍒涘缓搴撳瓨瀛楀吀鐢ㄤ簬蹇�熸煡鎵撅紙浠ocationId涓洪敭锛�
+            var stockDict = stockInfos.ToDictionary(s => s.LocationId, s => s);
+
+            // 6. 鏄犲皠姣忎釜璐т綅鍒癓ocation3DItemDTO
+            const float defaultMaxCapacity = 100f;
+            var locationItems = locations.Select(loc =>
+            {
+                var item = new Location3DItemDTO
+                {
+                    LocationId = loc.Id,
+                    LocationCode = loc.LocationCode,
+                    Row = loc.Row,
+                    Column = loc.Column,
+                    Layer = loc.Layer,
+                    LocationStatus = loc.LocationStatus,
+                    MaxCapacity = defaultMaxCapacity
+                };
+
+                // 灏濊瘯浠庡簱瀛樺瓧鍏镐腑鑾峰彇搴撳瓨淇℃伅
+                if (stockDict.TryGetValue(loc.Id, out var stockInfo))
+                {
+                    item.PalletCode = stockInfo.PalletCode;
+                    item.StockQuantity = stockInfo.Details.Sum(d => d.StockQuantity);
+
+                    // 鑾峰彇绗竴涓槑缁嗙殑鐗╂枡淇℃伅锛堝鏋滃瓨鍦級
+                    var firstDetail = stockInfo.Details.FirstOrDefault();
+                    if (firstDetail != null)
+                    {
+                        item.MaterielCode = firstDetail.MaterielCode;
+                        item.MaterielName = firstDetail.MaterielName;
+                        item.BatchNo = firstDetail.BatchNo;
+                    }
+
+                    // 璁$畻搴撳瓨鐘舵��
+                    var ratio = item.MaxCapacity > 0 ? item.StockQuantity / item.MaxCapacity : 0;
+                    if (ratio >= 0.9f)
+                        item.StockStatus = 3; // 宸叉弧 (FULL)
+                    else if (ratio >= 0.1f)
+                        item.StockStatus = 1; // 鏈夎揣 (HAS_STOCK)
+                    else if (ratio > 0)
+                        item.StockStatus = 2; // 搴撳瓨绱у紶 (LOW_STOCK)
+                    else
+                        item.StockStatus = 0; // 鏃犺揣 (EMPTY)
+                }
+                else
+                {
+                    item.StockStatus = 0; // 鏃犺揣 (EMPTY)
+                    item.StockQuantity = 0;
+                }
+
+                return item;
+            }).ToList();
+
+            // 7. 璁$畻浠撳簱灏哄
+            var maxRow = locations.Any() ? locations.Max(l => l.Row) : 0;
+            var maxColumn = locations.Any() ? locations.Max(l => l.Column) : 0;
+            var maxLayer = locations.Any() ? locations.Max(l => l.Layer) : 0;
+
+            // 8. 鏋勫缓杩斿洖缁撴灉
+            return new Stock3DLayoutDTO
+            {
+                WarehouseId = warehouseId,
+                WarehouseName = warehouse?.WarehouseName ?? string.Empty,
+                MaxRow = maxRow,
+                MaxColumn = maxColumn,
+                MaxLayer = maxLayer,
+                MaterielCodeList = materielCodeList,
+                BatchNoList = batchNoList,
+                Locations = locationItems
+            };
+        }
     }
 }

--
Gitblit v1.9.3