From e6b190354191122069b1a0518f050d6504f7ec5e Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 11 二月 2026 15:29:39 +0800
Subject: [PATCH] 重构WMS服务并简化逻辑

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_BasicService/LocationInfoService.cs |  174 ++++++++++++++++++++++++++++-----------------------------
 1 files changed, 85 insertions(+), 89 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_BasicService/LocationInfoService.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_BasicService/LocationInfoService.cs
index 16c743a..cfa497c 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_BasicService/LocationInfoService.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_BasicService/LocationInfoService.cs
@@ -1,23 +1,9 @@
-锘縰sing HslCommunication.WebSocket;
-using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
-using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
-using SqlSugar;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+锘縰sing SqlSugar;
 using WIDESEA_Common.CommonEnum;
 using WIDESEA_Common.LocationEnum;
-using WIDESEA_Common.StockEnum;
 using WIDESEA_Core;
 using WIDESEA_Core.BaseRepository;
 using WIDESEA_Core.BaseServices;
-using WIDESEA_Core.Const;
-using WIDESEA_Core.DB;
-using WIDESEA_Core.Enums;
-using WIDESEA_Core.Helper;
-using WIDESEA_Core.Seed;
 using WIDESEA_Core.Utilities;
 using WIDESEA_DTO.Basic;
 using WIDESEA_IBasicService;
@@ -27,124 +13,71 @@
 {
     public partial class LocationInfoService : ServiceBase<Dt_LocationInfo, IRepository<Dt_LocationInfo>>, ILocationInfoService
     {
-        private readonly IUnitOfWorkManage _unitOfWorkManage;
-        private readonly IRepository<Dt_StockInfo> _stockInfoRepository;
         public IRepository<Dt_LocationInfo> Repository => BaseDal;
 
-        public LocationInfoService(IRepository<Dt_LocationInfo> BaseDal, IUnitOfWorkManage unitOfWorkManage, IRepository<Dt_StockInfo> stockInfoRepository) : base(BaseDal)
+        public LocationInfoService(IRepository<Dt_LocationInfo> BaseDal) : base(BaseDal)
         {
-            _unitOfWorkManage = unitOfWorkManage;
-            _stockInfoRepository = stockInfoRepository;
         }
 
         /// <summary>
         /// 鎵归噺鍚敤璐т綅
         /// </summary>
-        /// <param name="keys">璐т綅涓婚敭鏁扮粍</param>
-        /// <returns></returns>
         public WebResponseContent LocationEnableStatus(int[] keys)
         {
-            List<Dt_LocationInfo> locationInfos = Repository.QueryData(x => keys.Contains(x.Id));
-            locationInfos.ForEach(x =>
-            {
-                x.EnableStatus = EnableStatusEnum.Normal.ObjToInt();
-            });
+            var locationInfos = Repository.QueryData(x => keys.Contains(x.Id));
+            locationInfos.ForEach(x => x.EnableStatus = EnableStatusEnum.Normal.GetHashCode());
             Repository.UpdateData(locationInfos);
-
             return WebResponseContent.Instance.OK();
         }
 
         /// <summary>
         /// 鎵归噺绂佺敤璐т綅
         /// </summary>
-        /// <param name="keys">璐т綅涓婚敭鏁扮粍</param>
-        /// <returns></returns>
         public WebResponseContent LocationDisableStatus(int[] keys)
         {
-            List<Dt_LocationInfo> locationInfos = Repository.QueryData(x => keys.Contains(x.Id));
-            locationInfos.ForEach(x =>
-            {
-                x.EnableStatus = EnableStatusEnum.Disable.ObjToInt();
-            });
+            var locationInfos = Repository.QueryData(x => keys.Contains(x.Id));
+            locationInfos.ForEach(x => x.EnableStatus = EnableStatusEnum.Disable.GetHashCode());
             Repository.UpdateData(locationInfos);
-
             return WebResponseContent.Instance.OK();
         }
 
         /// <summary>
         /// 鍗曚釜鍚敤璐т綅
         /// </summary>
-        /// <param name="key">璐т綅涓婚敭</param>
-        /// <returns></returns>
-        public WebResponseContent LocationEnableStatus(int key)
-        {
-            return LocationEnableStatus(new int[] { key });
-        }
+        public WebResponseContent LocationEnableStatus(int key) => LocationEnableStatus(new[] { key });
 
         /// <summary>
         /// 鍗曚釜绂佺敤璐т綅
         /// </summary>
-        /// <param name="key">璐т綅涓婚敭</param>
-        /// <returns></returns>
-        public WebResponseContent LocationDisableStatus(int key)
-        {
-            return LocationDisableStatus(new int[] { key });
-        }
+        public WebResponseContent LocationDisableStatus(int key) => LocationDisableStatus(new[] { key });
 
         /// <summary>
         /// 鍒濆鍖栬揣浣�
         /// </summary>
-        /// <param name="initializationLocationDTO"></param>
-        /// <returns></returns>
-        public WebResponseContent InitializationLocation(InitializationLocationDTO initializationLocationDTO)
+        public WebResponseContent InitializationLocation(InitializationLocationDTO dto)
         {
             try
             {
-                (bool, string, object?) result = ModelValidate.ValidateModelData(initializationLocationDTO);
-                if (!result.Item1) return WebResponseContent.Instance.Error(result.Item2);
+                var (isValid, errorMsg, _) = ModelValidate.ValidateModelData(dto);
+                if (!isValid) return WebResponseContent.Instance.Error(errorMsg);
 
-                int depth = initializationLocationDTO.Depth;
-                List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
-                for (int i = 0; i < initializationLocationDTO.MaxRow; i++)
+                var locationInfos = new List<Dt_LocationInfo>();
+                int depth = dto.Depth;
+
+                for (int row = 1; row <= dto.MaxRow; row++)
                 {
-                    if ((i + 1) % initializationLocationDTO.MaxRow == 1)
+                    depth = CalculateDepth(row, dto.MaxRow, dto.Depth, depth);
+
+                    for (int col = 1; col <= dto.MaxColumn; col++)
                     {
-                        depth = initializationLocationDTO.Depth;
-                    }
-                    else if ((i + 1) % initializationLocationDTO.MaxRow == initializationLocationDTO.Depth + 1)
-                    {
-                        depth = 1;
-                    }
-                    else if ((i + 1) % initializationLocationDTO.MaxRow > 1 && (i + 1) % initializationLocationDTO.MaxRow <= initializationLocationDTO.Depth)
-                    {
-                        depth -= 1;
-                    }
-                    else
-                    {
-                        depth += 1;
-                    }
-                    for (int j = 0; j < initializationLocationDTO.MaxColumn; j++)
-                    {
-                        for (int k = 0; k < initializationLocationDTO.MaxLayer; k++)
+                        for (int layer = 1; layer <= dto.MaxLayer; layer++)
                         {
-                            Dt_LocationInfo locationInfo = new Dt_LocationInfo()
-                            {
-                                WarehouseId = 0,
-                                Column = j + 1,
-                                EnableStatus = EnableStatusEnum.Normal.ObjToInt(),
-                                Layer = k + 1,
-                                LocationStatus = LocationStatusEnum.Free.ObjToInt(),
-                                LocationType = LocationTypeEnum.Undefined.ObjToInt(),
-                                RoadwayNo = $"{initializationLocationDTO.Roadway.ToString()}",
-                                Row = i + 1,
-                                Depth = depth,
-                            };
-                            locationInfo.LocationCode = $"{locationInfo.RoadwayNo}-{locationInfo.Row.ToString().PadLeft(3, '0')}-{locationInfo.Column.ToString().PadLeft(3, '0')}-{locationInfo.Layer.ToString().PadLeft(3, '0')}-{locationInfo.Depth.ToString().PadLeft(2, '0')}";
-                            locationInfo.LocationName = $"{locationInfo.RoadwayNo}宸烽亾{locationInfo.Row.ToString().PadLeft(3, '0')}琛寋locationInfo.Column.ToString().PadLeft(3, '0')}鍒梴locationInfo.Layer.ToString().PadLeft(3, '0')}灞倇locationInfo.Depth.ToString().PadLeft(2, '0')}娣�";
-                            locationInfos.Add(locationInfo);
+                            var location = CreateLocationInfo(dto.Roadway, row, col, layer, depth);
+                            locationInfos.Add(location);
                         }
                     }
                 }
+
                 BaseDal.AddData(locationInfos);
                 return WebResponseContent.Instance.OK();
             }
@@ -153,5 +86,68 @@
                 return WebResponseContent.Instance.Error(ex.Message);
             }
         }
+
+        private static int CalculateDepth(int row, int maxRow, int maxDepth, int currentDepth)
+        {
+            int mod = row % maxRow;
+            if (mod == 1) return maxDepth;
+            if (mod == maxDepth + 1) return 1;
+            if (mod > 1 && mod <= maxDepth) return currentDepth - 1;
+            return currentDepth + 1;
+        }
+
+        private static Dt_LocationInfo CreateLocationInfo(string roadwayNo, int row, int col, int layer, int depth)
+        {
+            return new Dt_LocationInfo
+            {
+                WarehouseId = 0,
+                Row = row,
+                Column = col,
+                Layer = layer,
+                Depth = depth,
+                RoadwayNo = roadwayNo,
+                EnableStatus = EnableStatusEnum.Normal.GetHashCode(),
+                LocationStatus = LocationStatusEnum.Free.GetHashCode(),
+                LocationType = LocationTypeEnum.Undefined.GetHashCode(),
+                LocationCode = $"{roadwayNo}-{row:D3}-{col:D3}-{layer:D3}-{depth:D2}",
+                LocationName = $"{roadwayNo}宸烽亾{row:D3}琛寋col:D3}鍒梴layer:D3}灞倇depth:D2}娣�"
+            };
+        }
+
+
+
+        /// <summary>
+        /// 鑾峰彇绌洪棽璐т綅淇℃伅(鏍规嵁宸烽亾鏌ヨ)
+        /// </summary>
+        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());
+
+            return locations?
+                .OrderBy(x => x.Layer)
+                .ThenBy(x => x.Depth)
+                .ThenBy(x => x.Column)
+                .ThenBy(x => x.Row)
+                .FirstOrDefault();
+        }
+
+        /// <summary>
+        /// 鑾峰彇璐т綅淇℃伅(鏍规嵁宸烽亾鍜岃揣浣嶇紪鐮佹煡璇�)
+        /// </summary>
+        public async Task<Dt_LocationInfo?> GetLocationInfo(string roadwayNo, string locationCode)
+        {
+            return await BaseDal.QueryFirstAsync(x => x.RoadwayNo == roadwayNo && x.LocationCode == locationCode);
+        }
+
+        /// <summary>
+        /// 鏇存柊璐т綅淇℃伅
+        /// </summary>
+        public async Task<bool> UpdateLocationInfoAsync(Dt_LocationInfo locationInfo)
+        {
+            return await BaseDal.UpdateDataAsync(locationInfo);
+        }
     }
 }

--
Gitblit v1.9.3