From b044183d3f3eae7150371318a017160fcd67c715 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 08 四月 2026 18:09:28 +0800
Subject: [PATCH] Merge branch 'dev' to sync branches

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs |   96 +++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 91 insertions(+), 5 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
index 8e3c669..2b27518 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -1,7 +1,9 @@
 锘縰sing SqlSugar;
 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 +35,16 @@
         public IStockInfo_HtyService StockInfo_HtyService { get; }
 
         /// <summary>
+        /// 浠撳簱鏈嶅姟
+        /// </summary>
+        public IWarehouseService _warehouseService { get; }
+
+        /// <summary>
+        /// Mes鎺ュ彛鏈嶅姟
+        /// </summary>
+        public IMesService _mesService { get; }
+
+        /// <summary>
         /// 鏋勯�犲嚱鏁�
         /// </summary>
         /// <param name="stockInfoDetailService">搴撳瓨鏄庣粏鏈嶅姟</param>
@@ -43,12 +55,16 @@
             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>
@@ -120,6 +136,20 @@
                     Status = StockStatusEmun.缁勭洏鏆傚瓨.GetHashCode(),
                 }).ToList();
 
+                var bindRequest = new BindContainerRequest
+                {
+                    ContainerCode = stock?.TargetPalletNo,
+                    EquipmentCode = "STK-GROUP-001",
+                    ResourceCode = "STK-GROUP-001",
+                    LocalTime = now,
+                    OperationType = 0, // 0浠h〃缁勭洏
+                    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);
@@ -134,15 +164,20 @@
                     var entity = new Dt_StockInfo
                     {
                         PalletCode = stock.TargetPalletNo,
-                        //WarehouseId = stock.WarehouseId > 0 ? stock.WarehouseId : 1,
-                        WarehouseId = 1,
+                        WarehouseId = stock.Roadway == "娉ㄦ恫缁勭洏鏈烘鎵�" ? (await _warehouseService.Db.Queryable<Dt_Warehouse>().FirstAsync(w => w.WarehouseCode == "GW1")).WarehouseId : 0,
                         StockStatus = StockStatusEmun.缁勭洏鏆傚瓨.GetHashCode(),
                         Creater = "system",
                         Details = details
                     };
-
                     result = StockInfoService.Repository.AddData(entity, x => x.Details);
-                    return result ? content.OK("缁勭洏鎴愬姛") : content.Error("缁勭洏澶辫触");
+                    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("缁勭洏鎴愬姛");
                 });
             }
             catch (Exception ex)
@@ -203,9 +238,45 @@
                     if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock, targetStock }, "鎹㈢洏")) <= 0)
                         return content.Error("鎹㈢洏鍘嗗彶璁板綍淇濆瓨澶辫触");
 
+                    // 璋冪敤MES瑙g粦婧愭墭鐩樼數鑺�
+                    var unbindRequest = new UnBindContainerRequest
+                    {
+                        EquipmentCode = "STK-GROUP-001",
+                        ResourceCode = "STK-GROUP-001",
+                        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 = "STK-GROUP-001",
+                        ResourceCode = "STK-GROUP-001",
+                        LocalTime = DateTime.Now,
+                        OperationType = 0,
+                        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("鎹㈢洏鎴愬姛");
                 });
             }
@@ -250,6 +321,21 @@
                     if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock }, "鎷嗙洏")) <= 0)
                         return content.Error("鎷嗙洏鍘嗗彶璁板綍淇濆瓨澶辫触");
 
+                    // 璋冪敤MES瑙g粦鐢佃姱
+                    var unbindRequest = new UnBindContainerRequest
+                    {
+                        EquipmentCode = "STK-GROUP-001",
+                        ResourceCode = "STK-GROUP-001",
+                        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("鎷嗙洏鎴愬姛");

--
Gitblit v1.9.3