From 5171d3f59b89389bf75293afd210cfa6de4ccff7 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期五, 10 四月 2026 23:03:49 +0800
Subject: [PATCH] feat: 添加堆垛机相关常量类并重构代码使用常量

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs |  400 ++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 296 insertions(+), 104 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
index c8cf69a..0b87683 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -1,160 +1,351 @@
-锘縰sing Autofac.Core;
-using System.Net;
-using System.Threading.Channels;
+锘縰sing SqlSugar;
+using WIDESEA_Common.Constants;
 using WIDESEA_Common.StockEnum;
 using WIDESEA_Core;
+using WIDESEA_DTO.MES;
 using WIDESEA_DTO.Stock;
+using WIDESEA_IBasicService;
 using WIDESEA_IStockService;
 using WIDESEA_Model.Models;
 
 namespace WIDESEA_StockService
 {
     /// <summary>
-    /// 搴撳瓨鏈嶅姟
+    /// 搴撳瓨鏈嶅姟鑱氬悎瀹炵幇绫�
     /// </summary>
-    public class StockSerivce : IStockService
+    public class StockService : IStockService
     {
+        /// <summary>
+        /// 搴撳瓨鏄庣粏鏈嶅姟
+        /// </summary>
         public IStockInfoDetailService StockInfoDetailService { get; }
+
+        /// <summary>
+        /// 搴撳瓨淇℃伅鏈嶅姟
+        /// </summary>
         public IStockInfoService StockInfoService { get; }
+
+        /// <summary>
+        /// 搴撳瓨鏄庣粏鍘嗗彶鏈嶅姟
+        /// </summary>
         public IStockInfoDetail_HtyService StockInfoDetail_HtyService { get; }
+
+        /// <summary>
+        /// 搴撳瓨鍘嗗彶鏈嶅姟
+        /// </summary>
         public IStockInfo_HtyService StockInfo_HtyService { get; }
 
+        /// <summary>
+        /// 浠撳簱鏈嶅姟
+        /// </summary>
+        public IWarehouseService _warehouseService { get; }
 
+        /// <summary>
+        /// Mes鎺ュ彛鏈嶅姟
+        /// </summary>
+        public IMesService _mesService { get; }
 
-        public StockSerivce(
+        /// <summary>
+        /// 鏋勯�犲嚱鏁�
+        /// </summary>
+        /// <param name="stockInfoDetailService">搴撳瓨鏄庣粏鏈嶅姟</param>
+        /// <param name="stockInfoService">搴撳瓨淇℃伅鏈嶅姟</param>
+        /// <param name="stockInfoDetail_HtyService">搴撳瓨鏄庣粏鍘嗗彶鏈嶅姟</param>
+        /// <param name="stockInfo_HtyService">搴撳瓨鍘嗗彶鏈嶅姟</param>
+        public StockService(
             IStockInfoDetailService stockInfoDetailService,
             IStockInfoService stockInfoService,
             IStockInfoDetail_HtyService stockInfoDetail_HtyService,
-            IStockInfo_HtyService stockInfo_HtyService)
+            IStockInfo_HtyService stockInfo_HtyService,
+            IMesService mesService,
+            IWarehouseService warehouseService)
         {
             StockInfoDetailService = stockInfoDetailService;
             StockInfoService = stockInfoService;
             StockInfoDetail_HtyService = stockInfoDetail_HtyService;
             StockInfo_HtyService = stockInfo_HtyService;
+            _mesService = mesService;
+            _warehouseService = warehouseService;
+        }
+
+        /// <summary>
+        /// 鍦ㄤ簨鍔′腑鎵ц鎿嶄綔
+        /// </summary>
+        private async Task<WebResponseContent> ExecuteWithinTransactionAsync(Func<Task<WebResponseContent>> operation)
+        {
+            var db = StockInfoService.Repository.Db as SqlSugarClient;
+            if (db == null)
+            {
+                return WebResponseContent.Instance.Error("Database context does not support transactions");
+            }
+
+            var ownsTransaction = db.Ado.Transaction == null;
+            try
+            {
+                if (ownsTransaction)
+                {
+                    db.BeginTran();
+                }
+
+                var result = await operation();
+                if (result?.Status == true)
+                {
+                    if (ownsTransaction)
+                    {
+                        db.CommitTran();
+                    }
+                    return result;
+                }
+
+                if (ownsTransaction)
+                {
+                    db.RollbackTran();
+                }
+                return result ?? WebResponseContent.Instance.Error("Transaction failed");
+            }
+            catch
+            {
+                if (ownsTransaction)
+                {
+                    db.RollbackTran();
+                }
+                throw;
+            }
         }
 
         /// <summary>
         /// 缁勭洏
         /// </summary>
-        public async Task<bool> GroupPalletAsync(StockDTO stock)
+        public async Task<WebResponseContent> GroupPalletAsync(StockDTO stock)
         {
-            var now = DateTime.Now;
-            var details = stock.Details.Select(item => new Dt_StockInfoDetail
+            WebResponseContent content = new WebResponseContent();
+            try
             {
-                MaterielCode = "鐢佃姱",
-                MaterielName = "鐢佃姱",
-                StockQuantity = item.Quantity,
-                Unit = "PCS",
-                Creater = "system",
-                OrderNo = "111",
-                ProductionDate = now.ToString(),
-                EffectiveDate = now.AddYears(1).ToString(),
-                SerialNumber = item.CellBarcode,
-                InboundOrderRowNo = item.Channel,
-                Status = StockStatusEmun.缁勭洏鏆傚瓨.GetHashCode(),
-            }).ToList();
+                var now = DateTime.Now;
+                var details = stock.Details.Select(item => new Dt_StockInfoDetail
+                {
+                    MaterielCode = StockConstants.MATERIAL_CODE,
+                    MaterielName = StockConstants.MATERIAL_NAME,
+                    StockQuantity = item.Quantity,
+                    Unit = StockConstants.UNIT,
+                    Creater = StockConstants.SYSTEM_USER,
+                    OrderNo = StockConstants.DEFAULT_ORDER_NO,
+                    ProductionDate = now.ToString(),
+                    EffectiveDate = now.AddYears(1).ToString(),
+                    SerialNumber = item.CellBarcode,
+                    InboundOrderRowNo = item.Channel,
+                    Status = StockStatusEmun.缁勭洏鏆傚瓨.GetHashCode(),
+                }).ToList();
 
-            var existingStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
-            if (existingStock != null)
-            {
-                details.ForEach(d => d.StockId = existingStock.Id);
-                return await StockInfoDetailService.Repository.AddDataAsync(details) > 0;
+                var bindRequest = new BindContainerRequest
+                {
+                    ContainerCode = stock?.TargetPalletNo,
+                    EquipmentCode = StockConstants.MES_EQUIPMENT_CODE,
+                    ResourceCode = StockConstants.MES_RESOURCE_CODE,
+                    LocalTime = now,
+                    OperationType = StockConstants.MES_BIND_OPERATION_TYPE,
+                    ContainerSfcList = details.Select(d => new ContainerSfcItem
+                    {
+                        Sfc = d.SerialNumber,
+                        Location = d.InboundOrderRowNo.ToString(),
+                    }).ToList()
+                };
+
+                return await ExecuteWithinTransactionAsync(async () =>
+                {
+                    var existingStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
+                    var result = false;
+                    if (existingStock != null)
+                    {
+                        details.ForEach(d => d.StockId = existingStock.Id);
+                        result = await StockInfoDetailService.Repository.AddDataAsync(details) > 0;
+                        return result ? content.OK("缁勭洏鎴愬姛") : content.Error("缁勭洏澶辫触");
+                    }
+
+                    var entity = new Dt_StockInfo
+                    {
+                        PalletCode = stock.TargetPalletNo,
+                        WarehouseId = stock.Roadway == "娉ㄦ恫缁勭洏鏈烘鎵�" ? (await _warehouseService.Db.Queryable<Dt_Warehouse>().FirstAsync(w => w.WarehouseCode == "GW1")).WarehouseId : 0,
+                        StockStatus = StockStatusEmun.缁勭洏鏆傚瓨.GetHashCode(),
+                        Creater = StockConstants.SYSTEM_USER,
+                        Details = details
+                    };
+                    result = StockInfoService.Repository.AddData(entity, x => x.Details);
+                    if (!result) return content.Error("缁勭洏澶辫触");
+
+                    //var mesResult = _mesService.BindContainer(bindRequest);
+                    //if (mesResult == null || mesResult.Data == null || !mesResult.Data.IsSuccess)
+                    //{
+                    //    return content.Error($"缁勭洏鎴愬姛锛屼絾MES缁戝畾澶辫触: {mesResult?.Data?.Msg ?? mesResult?.ErrorMessage ?? "鏈煡閿欒"}");
+                    //}
+                    return content.OK("缁勭洏鎴愬姛");
+                });
             }
-
-            var entity = new Dt_StockInfo
+            catch (Exception ex)
             {
-                PalletCode = stock.TargetPalletNo,
-                WarehouseId = 1,
-                StockStatus = 1,
-                Creater = "system",
-                Details = details
-            };
-
-            return StockInfoService.Repository.AddData(entity, x => x.Details);
+                return content.Error($"缁勭洏澶辫触: {ex.Message}");
+            }
         }
 
         /// <summary>
         /// 鎹㈢洏
         /// </summary>
-        public async Task<bool> ChangePalletAsync(StockDTO stock)
+        public async Task<WebResponseContent> ChangePalletAsync(StockDTO stock)
         {
-            if (stock == null ||
-                string.IsNullOrWhiteSpace(stock.TargetPalletNo) ||
-                string.IsNullOrWhiteSpace(stock.SourcePalletNo) ||
-                string.Equals(stock.SourcePalletNo, stock.TargetPalletNo, StringComparison.OrdinalIgnoreCase))
+            WebResponseContent content = new WebResponseContent();
+            try
             {
-                return false;
-            }
-
-            var sourceStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
-            if (sourceStock == null) return false;
-
-            var targetStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
-            if (targetStock == null)
-            {
-                var newStock = new Dt_StockInfo
+                if (stock == null ||
+                    string.IsNullOrWhiteSpace(stock.TargetPalletNo) ||
+                    string.IsNullOrWhiteSpace(stock.SourcePalletNo) ||
+                    string.Equals(stock.SourcePalletNo, stock.TargetPalletNo, StringComparison.OrdinalIgnoreCase))
                 {
-                    PalletCode = stock.TargetPalletNo,
-                    WarehouseId = sourceStock.WarehouseId,
-                    StockStatus = sourceStock.StockStatus,
-                    Creater = "system"
-                };
+                    return content.Error("婧愭墭鐩樺彿涓庣洰鏍囨墭鐩樺彿鐩稿悓");
+                }
 
-                var newId = StockInfoService.Repository.AddData(newStock);
-                if (newId <= 0) return false;
+                return await ExecuteWithinTransactionAsync(async () =>
+                {
+                    var sourceStock = await StockInfoService.Repository.QueryDataNavFirstAsync(s => s.PalletCode == stock.SourcePalletNo);
+                    if (sourceStock == null) return content.Error("婧愭墭鐩樹笉瀛樺湪");
 
-                targetStock = newStock;
-                targetStock.Id = newId;
+                    var targetStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.TargetPalletNo);
+                    if (targetStock == null)
+                    {
+                        var newStock = new Dt_StockInfo
+                        {
+                            PalletCode = stock.TargetPalletNo,
+                            WarehouseId = sourceStock.WarehouseId,
+                            StockStatus = StockStatusEmun.缁勭洏鏆傚瓨.GetHashCode(),
+                            Creater = StockConstants.SYSTEM_USER,
+                        };
+
+                        var newId = StockInfoService.Repository.AddData(newStock);
+                        if (newId <= 0) return content.Error("鎹㈢洏澶辫触");
+
+                        targetStock = newStock;
+                        targetStock.Id = newId;
+                    }
+
+                    var serialNumbers = stock.Details.Select(d => d.Channel).Distinct().ToList();
+                    if (!serialNumbers.Any()) return content.Error("鏈壘鍒版湁鏁堢殑搴忓垪鍙�");
+
+                    var detailEntities = StockInfoDetailService.Repository.QueryData(
+                        d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.InboundOrderRowNo));
+                    if (!detailEntities.Any()) return content.Error("鏈壘鍒版湁鏁堢殑搴撳瓨鏄庣粏");
+
+                    if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "鎹㈢洏")) <= 0)
+                        return content.Error("鎹㈢洏鍘嗗彶璁板綍淇濆瓨澶辫触");
+
+                    if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock, targetStock }, "鎹㈢洏")) <= 0)
+                        return content.Error("鎹㈢洏鍘嗗彶璁板綍淇濆瓨澶辫触");
+
+                    // 璋冪敤MES瑙g粦婧愭墭鐩樼數鑺�
+                    var unbindRequest = new UnBindContainerRequest
+                    {
+                        EquipmentCode = StockConstants.MES_EQUIPMENT_CODE,
+                        ResourceCode = StockConstants.MES_RESOURCE_CODE,
+                        LocalTime = DateTime.Now,
+                        ContainCode = stock.SourcePalletNo,
+                        SfcList = detailEntities.Select(d => d.SerialNumber).ToList()
+                    };
+                    var unbindResult = _mesService.UnBindContainer(unbindRequest);
+                    if (unbindResult == null || unbindResult.Data == null || !unbindResult.Data.IsSuccess)
+                    {
+                        return content.Error($"鎹㈢洏鎴愬姛锛屼絾MES瑙g粦澶辫触: {unbindResult?.Data?.Msg ?? unbindResult?.ErrorMessage ?? "鏈煡閿欒"}");
+                    }
+
+                    detailEntities.ForEach(d => d.StockId = targetStock.Id);
+                    var result = await StockInfoDetailService.Repository.UpdateDataAsync(detailEntities);
+                    if (!result) return content.Error("鎹㈢洏澶辫触");
+
+                    // 璋冪敤MES缁戝畾鐩爣鎵樼洏鐢佃姱
+                    var bindRequest = new BindContainerRequest
+                    {
+                        ContainerCode = stock.TargetPalletNo,
+                        EquipmentCode = StockConstants.MES_EQUIPMENT_CODE,
+                        ResourceCode = StockConstants.MES_RESOURCE_CODE,
+                        LocalTime = DateTime.Now,
+                        OperationType = StockConstants.MES_BIND_OPERATION_TYPE,
+                        ContainerSfcList = detailEntities.Select(d => new ContainerSfcItem
+                        {
+                            Sfc = d.SerialNumber,
+                            Location = d.InboundOrderRowNo.ToString()
+                        }).ToList()
+                    };
+                    var bindResult = _mesService.BindContainer(bindRequest);
+                    if (bindResult == null || bindResult.Data == null || !bindResult.Data.IsSuccess)
+                    {
+                        return content.Error($"鎹㈢洏鎴愬姛锛屼絾MES缁戝畾澶辫触: {bindResult?.Data?.Msg ?? bindResult?.ErrorMessage ?? "鏈煡閿欒"}");
+                    }
+
+                    return content.OK("鎹㈢洏鎴愬姛");
+                });
             }
-
-            var serialNumbers = stock.Details.Select(d => d.CellBarcode).Distinct().ToList();
-            if (!serialNumbers.Any()) return false;
-
-            var detailEntities = StockInfoDetailService.Repository.QueryData(
-                d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.SerialNumber));
-            if (!detailEntities.Any()) return false;
-
-            if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "鎹㈢洏")) <= 0)
-                return false;
-
-            if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock, targetStock }, "鎹㈢洏")) <= 0)
-                return false;
-
-            detailEntities.ForEach(d => d.StockId = targetStock.Id);
-            return await StockInfoDetailService.Repository.UpdateDataAsync(detailEntities);
+            catch (Exception ex)
+            {
+                return content.Error($"鎹㈢洏澶辫触: {ex.Message}");
+            }
         }
 
         /// <summary>
         /// 鎷嗙洏
         /// </summary>
-        public async Task<bool> SplitPalletAsync(StockDTO stock)
+        public async Task<WebResponseContent> SplitPalletAsync(StockDTO stock)
         {
-            if (stock == null || string.IsNullOrWhiteSpace(stock.SourcePalletNo))
-                return false;
-
-            var sourceStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
-            if (sourceStock == null) return false;
-
-            var serialNumbers = stock.Details.Select(d => d.CellBarcode).Distinct().ToList();
-            if (!serialNumbers.Any())
+            WebResponseContent content = new WebResponseContent();
+            try
             {
-                serialNumbers = sourceStock.Details
-                                            .Where(x => stock.Details.Any(d => d.Channel == x.InboundOrderRowNo))
-                                            .Select(x => x.SerialNumber)
-                                            .ToList();
+                if (stock == null || string.IsNullOrWhiteSpace(stock.SourcePalletNo))
+                    return content.Error("婧愭墭鐩樺彿涓嶈兘涓虹┖");
+
+                return await ExecuteWithinTransactionAsync(async () =>
+                {
+                    var sourceStock = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
+                    if (sourceStock == null) return content.Error("婧愭墭鐩樹笉瀛樺湪");
+
+                    var serialNumbers = stock.Details.Select(d => d.CellBarcode).Distinct().ToList();
+                    if (!serialNumbers.Any())
+                    {
+                        serialNumbers = sourceStock.Details
+                                                    .Where(x => stock.Details.Any(d => d.Channel == x.InboundOrderRowNo))
+                                                    .Select(x => x.SerialNumber)
+                                                    .ToList();
+                    }
+
+                    var detailEntities = StockInfoDetailService.Repository.QueryData(
+                        d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.SerialNumber));
+                    if (!detailEntities.Any()) return content.Error("鏈壘鍒版湁鏁堢殑搴撳瓨鏄庣粏");
+
+                    if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "鎷嗙洏")) <= 0)
+                        return content.Error("鎷嗙洏鍘嗗彶璁板綍淇濆瓨澶辫触");
+
+                    if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock }, "鎷嗙洏")) <= 0)
+                        return content.Error("鎷嗙洏鍘嗗彶璁板綍淇濆瓨澶辫触");
+
+                    // 璋冪敤MES瑙g粦鐢佃姱
+                    var unbindRequest = new UnBindContainerRequest
+                    {
+                        EquipmentCode = StockConstants.MES_EQUIPMENT_CODE,
+                        ResourceCode = StockConstants.MES_RESOURCE_CODE,
+                        LocalTime = DateTime.Now,
+                        ContainCode = stock.SourcePalletNo,
+                        SfcList = detailEntities.Select(d => d.SerialNumber).ToList()
+                    };
+                    var unbindResult = _mesService.UnBindContainer(unbindRequest);
+                    if (unbindResult == null || unbindResult.Data == null || !unbindResult.Data.IsSuccess)
+                    {
+                        return content.Error($"鎷嗙洏鎴愬姛锛屼絾MES瑙g粦澶辫触: {unbindResult?.Data?.Msg ?? unbindResult?.ErrorMessage ?? "鏈煡閿欒"}");
+                    }
+
+                    var result = await StockInfoDetailService.Repository.DeleteDataAsync(detailEntities);
+                    if (!result) return content.Error("鎷嗙洏澶辫触");
+                    return content.OK("鎷嗙洏鎴愬姛");
+                });
             }
-
-            var detailEntities = StockInfoDetailService.Repository.QueryData(
-                d => d.StockId == sourceStock.Id && serialNumbers.Contains(d.SerialNumber));
-            if (!detailEntities.Any()) return false;
-
-            if (await StockInfoDetail_HtyService.Repository.AddDataAsync(CreateDetailHistory(detailEntities, "鎷嗙洏")) <= 0)
-                return false;
-
-            if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock }, "鎷嗙洏")) <= 0)
-                return false;
-
-            return await StockInfoDetailService.Repository.DeleteDataAsync(detailEntities);
+            catch (Exception ex)
+            {
+                return content.Error($"鎷嗙洏澶辫触: {ex.Message}");
+            }
         }
 
         /// <summary>
@@ -224,9 +415,10 @@
                 Creater = s.Creater,
                 CreateDate = s.CreateDate,
                 Modifier = s.Modifier,
-                ModifyDate = s.ModifyDate
+                ModifyDate = s.ModifyDate,
+                LocationId = s.LocationId,
+                OutboundDate = s.OutboundDate
             }).ToList();
         }
-
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3