From 8c3a3fd584e38e72bfb321ab70bfcfc2ab0a4e2e Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期四, 16 四月 2026 23:18:01 +0800
Subject: [PATCH] refactor(StockService): 移除注释掉的MES绑定解绑相关代码

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs |  184 ++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 169 insertions(+), 15 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
index 8e3c669..d2f1044 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -1,7 +1,11 @@
-锘縰sing SqlSugar;
+锘縰sing Newtonsoft.Json;
+using 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;
 
@@ -33,6 +37,21 @@
         public IStockInfo_HtyService StockInfo_HtyService { get; }
 
         /// <summary>
+        /// 浠撳簱鏈嶅姟
+        /// </summary>
+        public IWarehouseService _warehouseService { get; }
+
+        /// <summary>
+        /// SqlSugar瀹㈡埛绔紙鐢ㄤ簬涓存椂琛ㄦ搷浣滐級
+        /// </summary>
+        public ISqlSugarClient SqlSugarClient { get; }
+
+        /// <summary>
+        /// Mes鎺ュ彛鏈嶅姟
+        /// </summary>
+        public IMesService _mesService { get; }
+
+        /// <summary>
         /// 鏋勯�犲嚱鏁�
         /// </summary>
         /// <param name="stockInfoDetailService">搴撳瓨鏄庣粏鏈嶅姟</param>
@@ -43,12 +62,18 @@
             IStockInfoDetailService stockInfoDetailService,
             IStockInfoService stockInfoService,
             IStockInfoDetail_HtyService stockInfoDetail_HtyService,
-            IStockInfo_HtyService stockInfo_HtyService)
+            IStockInfo_HtyService stockInfo_HtyService,
+            IMesService mesService,
+            IWarehouseService warehouseService,
+            ISqlSugarClient sqlSugarClient)
         {
             StockInfoDetailService = stockInfoDetailService;
             StockInfoService = stockInfoService;
             StockInfoDetail_HtyService = stockInfoDetail_HtyService;
             StockInfo_HtyService = stockInfo_HtyService;
+            _mesService = mesService;
+            _warehouseService = warehouseService;
+            SqlSugarClient = sqlSugarClient;
         }
 
         /// <summary>
@@ -107,12 +132,12 @@
                 var now = DateTime.Now;
                 var details = stock.Details.Select(item => new Dt_StockInfoDetail
                 {
-                    MaterielCode = "鐢佃姱",
-                    MaterielName = "鐢佃姱",
+                    MaterielCode = StockConstants.MATERIAL_CODE,
+                    MaterielName = StockConstants.MATERIAL_NAME,
                     StockQuantity = item.Quantity,
-                    Unit = "PCS",
-                    Creater = "system",
-                    OrderNo = "111",
+                    Unit = StockConstants.UNIT,
+                    Creater = StockConstants.SYSTEM_USER,
+                    OrderNo = StockConstants.DEFAULT_ORDER_NO,
                     ProductionDate = now.ToString(),
                     EffectiveDate = now.AddYears(1).ToString(),
                     SerialNumber = item.CellBarcode,
@@ -131,18 +156,24 @@
                         return result ? content.OK("缁勭洏鎴愬姛") : content.Error("缁勭洏澶辫触");
                     }
 
+                    int WarehouseId = stock.Roadway switch
+                    {
+                        "娉ㄦ恫缁勭洏鏈烘鎵�" => (await _warehouseService.Db.Queryable<Dt_Warehouse>().FirstAsync(w => w.WarehouseCode == "GWSC1")).WarehouseId,
+                        "鎹㈢洏鏈烘鎵�" => (await _warehouseService.Db.Queryable<Dt_Warehouse>().FirstAsync(w => w.WarehouseCode == "HCSC1")).WarehouseId
+                    };
+
                     var entity = new Dt_StockInfo
                     {
                         PalletCode = stock.TargetPalletNo,
-                        //WarehouseId = stock.WarehouseId > 0 ? stock.WarehouseId : 1,
-                        WarehouseId = 1,
+                        WarehouseId = WarehouseId,
                         StockStatus = StockStatusEmun.缁勭洏鏆傚瓨.GetHashCode(),
-                        Creater = "system",
+                        Creater = StockConstants.SYSTEM_USER,
                         Details = details
                     };
-
                     result = StockInfoService.Repository.AddData(entity, x => x.Details);
-                    return result ? content.OK("缁勭洏鎴愬姛") : content.Error("缁勭洏澶辫触");
+                    if (!result) return content.Error("缁勭洏澶辫触");
+
+                    return content.OK("缁勭洏鎴愬姛");
                 });
             }
             catch (Exception ex)
@@ -180,7 +211,7 @@
                             PalletCode = stock.TargetPalletNo,
                             WarehouseId = sourceStock.WarehouseId,
                             StockStatus = StockStatusEmun.缁勭洏鏆傚瓨.GetHashCode(),
-                            Creater = "system",
+                            Creater = StockConstants.SYSTEM_USER,
                         };
 
                         var newId = StockInfoService.Repository.AddData(newStock);
@@ -206,6 +237,7 @@
                     detailEntities.ForEach(d => d.StockId = targetStock.Id);
                     var result = await StockInfoDetailService.Repository.UpdateDataAsync(detailEntities);
                     if (!result) return content.Error("鎹㈢洏澶辫触");
+
                     return content.OK("鎹㈢洏鎴愬姛");
                 });
             }
@@ -225,6 +257,30 @@
             {
                 if (stock == null || string.IsNullOrWhiteSpace(stock.SourcePalletNo))
                     return content.Error("婧愭墭鐩樺彿涓嶈兘涓虹┖");
+
+                // 骞傜瓑鍐欏叆锛氭鏌ヤ复鏃惰〃鏄惁宸叉湁璇ユ墭鐩樿褰曪紝鏃犲垯鍐欏叆
+                var existingTemp = SqlSugarClient.Queryable<Dt_SplitTemp>()
+                    .Where(t => t.PalletCode == stock.SourcePalletNo)
+                    .First();
+                if (existingTemp == null)
+                {
+                    // 鏌ヨ璇ユ墭鐩樺綋鍓嶆墍鏈夌數鑺紝瀛樺叆涓存椂琛�
+                    var sourceStockForTemp = StockInfoService.Repository.QueryFirst(s => s.PalletCode == stock.SourcePalletNo);
+                    if (sourceStockForTemp != null)
+                    {
+                        var allDetails = StockInfoDetailService.Repository.QueryData(d => d.StockId == sourceStockForTemp.Id);
+                        if (allDetails != null && allDetails.Any())
+                        {
+                            var sfcListJson = JsonConvert.SerializeObject(allDetails.Select(d => d.SerialNumber).ToList());
+                            await SqlSugarClient.Insertable(new Dt_SplitTemp
+                            {
+                                PalletCode = stock.SourcePalletNo,
+                                SfcList = sfcListJson,
+                                CreateTime = DateTime.Now
+                            }).ExecuteCommandAsync();
+                        }
+                    }
+                }
 
                 return await ExecuteWithinTransactionAsync(async () =>
                 {
@@ -250,8 +306,6 @@
                     if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock }, "鎷嗙洏")) <= 0)
                         return content.Error("鎷嗙洏鍘嗗彶璁板綍淇濆瓨澶辫触");
 
-                    var result = await StockInfoDetailService.Repository.DeleteDataAsync(detailEntities);
-                    if (!result) return content.Error("鎷嗙洏澶辫触");
                     return content.OK("鎷嗙洏鎴愬姛");
                 });
             }
@@ -333,5 +387,105 @@
                 OutboundDate = s.OutboundDate
             }).ToList();
         }
+
+        /// <summary>
+        /// 鎵归噺鎷嗙洏纭 - 涓�娆℃�ц皟鐢∕ES瑙g粦鏁翠釜鎵樼洏
+        /// </summary>
+        /// <param name="palletCode">婧愭墭鐩樺彿</param>
+        /// <returns>鎿嶄綔缁撴灉</returns>
+        public async Task<WebResponseContent> SplitPalletConfirmAsync(string palletCode)
+        {
+            WebResponseContent content = new WebResponseContent();
+            try
+            {
+                if (string.IsNullOrWhiteSpace(palletCode))
+                    return content.Error("鎵樼洏鍙蜂笉鑳戒负绌�");
+
+                // 1. 浠庝复鏃惰〃璇诲彇鐢佃姱鍒楄〃
+                var tempRecord = SqlSugarClient.Queryable<Dt_SplitTemp>()
+                    .Where(t => t.PalletCode == palletCode)
+                    .First();
+                if (tempRecord == null)
+                    return content.Error("鏈壘鍒版媶鐩樹复鏃惰褰曪紝璇峰厛鎵ц鎷嗙洏鎿嶄綔");
+
+                var sfcList = JsonConvert.DeserializeObject<List<string>>(tempRecord.SfcList);
+                if (sfcList == null || !sfcList.Any())
+                    return content.Error("涓存椂琛ㄤ腑鐢佃姱鍒楄〃涓虹┖");
+
+                // 2. 璋冪敤MES瑙g粦
+                var unbindRequest = new UnBindContainerRequest
+                {
+                    EquipmentCode = StockConstants.MES_EQUIPMENT_CODE,
+                    ResourceCode = StockConstants.MES_RESOURCE_CODE,
+                    LocalTime = DateTime.Now,
+                    ContainCode = palletCode,
+                    SfcList = sfcList
+                };
+                var unbindResult = _mesService.UnBindContainer(unbindRequest);
+                if (unbindResult == null || unbindResult.Data == null || !unbindResult.Data.IsSuccess)
+                {
+                    return content.Error($"MES瑙g粦澶辫触: {unbindResult?.Data?.Msg ?? unbindResult?.ErrorMessage ?? "鏈煡閿欒"}");
+                }
+
+                // 3. 鍒犻櫎涓存椂琛ㄨ褰�
+                await SqlSugarClient.Deleteable<Dt_SplitTemp>().Where(t => t.PalletCode == palletCode).ExecuteCommandAsync();
+
+                return content.OK("鎵归噺鎷嗙洏纭鎴愬姛");
+            }
+            catch (Exception ex)
+            {
+                return content.Error($"鎵归噺鎷嗙洏纭澶辫触: {ex.Message}");
+            }
+        }
+
+        /// <summary>
+        /// 鎵归噺缁勭洏纭 - 涓�娆℃�ц皟鐢∕ES缁戝畾鏁翠釜鎵樼洏
+        /// </summary>
+        /// <param name="palletCode">鐩爣鎵樼洏鍙�</param>
+        /// <returns>鎿嶄綔缁撴灉</returns>
+        public async Task<WebResponseContent> GroupPalletConfirmAsync(string palletCode)
+        {
+            WebResponseContent content = new WebResponseContent();
+            try
+            {
+                if (string.IsNullOrWhiteSpace(palletCode))
+                    return content.Error("鎵樼洏鍙蜂笉鑳戒负绌�");
+
+                // 1. 鏌ヨ璇ユ墭鐩樹笅鐨勬墍鏈夌數鑺槑缁�
+                var stockInfo = StockInfoService.Repository.QueryFirst(s => s.PalletCode == palletCode);
+                if (stockInfo == null)
+                    return content.Error("鎵樼洏涓嶅瓨鍦�");
+
+                var details = StockInfoDetailService.Repository.QueryData(d => d.StockId == stockInfo.Id);
+                if (details == null || !details.Any())
+                    return content.Error("鎵樼洏涓嬫棤鐢佃姱鏁版嵁");
+
+                // 2. 璋冪敤MES缁戝畾
+                var bindRequest = new BindContainerRequest
+                {
+                    ContainerCode = palletCode,
+                    EquipmentCode = StockConstants.MES_EQUIPMENT_CODE,
+                    ResourceCode = StockConstants.MES_RESOURCE_CODE,
+                    LocalTime = DateTime.Now,
+                    OperationType = StockConstants.MES_BIND_OPERATION_TYPE,
+                    ContainerSfcList = details.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("鎵归噺缁勭洏纭鎴愬姛");
+            }
+            catch (Exception ex)
+            {
+                return content.Error($"鎵归噺缁勭洏纭澶辫触: {ex.Message}");
+            }
+        }
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3