From 5fc251a847ccc0540da8b112ed2bddba846455d8 Mon Sep 17 00:00:00 2001
From: pan <antony1029@163.com>
Date: 星期一, 17 十一月 2025 20:39:49 +0800
Subject: [PATCH] 提交

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_OutboundService/SplitPackageService.cs |  143 +++++++++++++++++++++++++++++++----------------
 1 files changed, 93 insertions(+), 50 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/SplitPackageService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/SplitPackageService.cs"
index 33f0cd7..54ae514 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/SplitPackageService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/SplitPackageService.cs"
@@ -1,4 +1,5 @@
 锘縰sing Microsoft.AspNetCore.Http;
+using Org.BouncyCastle.Asn1.Ocsp;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -9,6 +10,7 @@
 using WIDESEA_Core.BaseRepository;
 using WIDESEA_Core.BaseServices;
 using WIDESEA_DTO.Outbound;
+using WIDESEA_IBasicService;
 using WIDESEA_IOutboundService;
 using WIDESEA_IStockService;
 using WIDESEA_Model.Models;
@@ -23,13 +25,15 @@
         private readonly IStockInfoService _stockInfoService;
         private readonly IStockInfoDetailService _stockInfoDetailService;
         private readonly IOutStockLockInfoService _outStockLockInfoService;
+        private readonly IDailySequenceService _dailySequenceService;
 
-        public SplitPackageService(IRepository<Dt_SplitPackageRecord> BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockInfoService stockInfoService, IOutStockLockInfoService outStockLockInfoService, IStockInfoDetailService stockInfoDetailService) : base(BaseDal)
+        public SplitPackageService(IRepository<Dt_SplitPackageRecord> BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockInfoService stockInfoService, IOutStockLockInfoService outStockLockInfoService, IStockInfoDetailService stockInfoDetailService, IDailySequenceService dailySequenceService) : base(BaseDal)
         {
             _unitOfWorkManage = unitOfWorkManage;
             _stockInfoService = stockInfoService;
             _outStockLockInfoService = outStockLockInfoService;
             _stockInfoDetailService = stockInfoDetailService;
+            _dailySequenceService = dailySequenceService;
         }
 
         /// <summary>
@@ -42,66 +46,104 @@
                 _unitOfWorkManage.BeginTran();
 
                 // 1. 楠岃瘉鍑哄簱閿佸畾淇℃伅
-                var lockInfo = await _stockInfoDetailService.Db.Queryable<Dt_OutStockLockInfo>()
+                var lockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                           .Where(x => x.OrderNo == request.OrderNo &&
                            x.PalletCode == request.PalletCode &&
                            x.CurrentBarcode == request.OriginalBarcode &&
-                           x.Status == 0)
+                           x.Status == 1)
                     .FirstAsync();
 
                 if (lockInfo == null)
                     return WebResponseContent.Instance.Error("鏈壘鍒版湁鏁堢殑鍑哄簱閿佸畾淇℃伅");
- 
+
                 // 2. 妫�鏌ュ墿浣欓攣瀹氭暟閲�
-                decimal remainingLockQuantity = lockInfo.AssignQuantity - lockInfo.PickedQty;
+                decimal remainingLockQuantity = lockInfo.OriginalQuantity - lockInfo.PickedQty;
                 if (request.SplitQuantity > remainingLockQuantity)
                     return WebResponseContent.Instance.Error($"鎷嗗寘鏁伴噺涓嶈兘澶т簬鍓╀綑閿佸畾鏁伴噺锛屽墿浣欙細{remainingLockQuantity}");
 
                 var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
-               .Where(x => x.Barcode == request.OriginalBarcode)
+               .Where(x => x.Barcode == request.OriginalBarcode && x.StockId == lockInfo.StockId)
                .FirstAsync();
+                if (stockDetail == null)
+                    throw new Exception($"鏈壘鍒版潯鐮亄request.OriginalBarcode}瀵瑰簲鐨勫簱瀛樿褰�");
 
+                stockDetail.StockQuantity = request.SplitQuantity;
+                stockDetail.OutboundQuantity = request.SplitQuantity;
+                _stockInfoDetailService.Db.Updateable<Dt_StockInfoDetail>(stockDetail).ExecuteCommand();
+
+
+                var seq = await _dailySequenceService.GetNextSequenceAsync();
                 // 3. 鐢熸垚鏂版潯鐮�
-                string newBarcode = "";
+                string newBarcode = "WSLOT" + DateTime.Now.ToString("yyyyMMdd") + seq.ToString()?.PadLeft(5, '0');
+
+                decimal remainingQty = remainingLockQuantity - request.SplitQuantity;
+
+                // 涓烘媶鍖呬骇鐢熺殑鏂版潯鐮佸垱寤哄簱瀛樿褰�
+                var newStockDetail = new Dt_StockInfoDetail
+                {
+                    SupplyCode = stockDetail.SupplyCode,
+                    WarehouseCode = stockDetail.WarehouseCode,
+                    BarcodeQty = stockDetail.BarcodeQty,
+                    BarcodeUnit = stockDetail.Barcode,
+                    BusinessType = stockDetail.BusinessType,
+                    Unit = stockDetail.Unit,
+                    StockId = lockInfo.StockId,
+                    MaterielCode = stockDetail.MaterielCode,
+                    OrderNo = stockDetail.OrderNo,
+                    BatchNo = stockDetail.BatchNo,
+                    StockQuantity = remainingQty,
+                    OutboundQuantity = remainingQty, // 閿佸畾鍏ㄩ儴鏁伴噺
+                    Barcode = newBarcode,
+                    InboundOrderRowNo = stockDetail.InboundOrderRowNo,
+
+                };
+                await _outStockLockInfoService.Db.Insertable(newStockDetail).ExecuteCommandAsync();
 
                 // 4. 鍒涘缓鏂扮殑鍑哄簱閿佸畾淇℃伅锛堟柊鏉$爜锛�
                 var newLockInfo = new Dt_OutStockLockInfo
                 {
+
                     OrderNo = lockInfo.OrderNo,
                     OrderDetailId = lockInfo.OrderDetailId,
                     BatchNo = lockInfo.BatchNo,
                     MaterielCode = lockInfo.MaterielCode,
                     MaterielName = lockInfo.MaterielName,
                     StockId = lockInfo.StockId,
-                    OrderQuantity = lockInfo.OrderQuantity,
-                    OriginalQuantity = lockInfo.OriginalQuantity,
-                    AssignQuantity = request.SplitQuantity, // 鏂版潯鐮佸垎閰嶆暟閲�
+                    OrderQuantity = remainingQty,
+                    OriginalQuantity = remainingQty,
+                    AssignQuantity = remainingQty, // 鏂版潯鐮佸垎閰嶆暟閲�
                     PickedQty = 0, // 鏂版潯鐮佹湭鎷i��
                     LocationCode = lockInfo.LocationCode,
                     PalletCode = lockInfo.PalletCode,
                     TaskNum = lockInfo.TaskNum,
                     Status = (int)OutLockStockStatusEnum.鍑哄簱涓�,
                     Unit = lockInfo.Unit,
-                
+                    SupplyCode = lockInfo.SupplyCode,
+                    OrderType = lockInfo.OrderType,
                     CurrentBarcode = newBarcode, // 鏂版潯鐮�
                     OriginalLockQuantity = request.SplitQuantity,
                     IsSplitted = 1,
                     ParentLockId = lockInfo.Id // 璁板綍鐖剁骇閿佸畾ID
                 };
-                await Db.Insertable(newLockInfo).ExecuteCommandAsync();
+                await _outStockLockInfoService.Db.Insertable(newLockInfo).ExecuteCommandAsync();
 
                 // 5. 鏇存柊鍘熼攣瀹氫俊鎭殑鍒嗛厤鏁伴噺锛堝噺灏戞媶鍖呮暟閲忥級
-                lockInfo.AssignQuantity -= request.SplitQuantity;
-                await Db.Updateable(lockInfo).ExecuteCommandAsync();
+                lockInfo.AssignQuantity = request.SplitQuantity;
+                await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
 
                 // 6. 璁板綍鎷嗗寘鍘嗗彶锛堢敤浜庤拷韪級
                 var splitHistory = new Dt_SplitPackageRecord
                 {
+                    FactoryArea = lockInfo.FactoryArea,
+                    TaskNum = lockInfo.TaskNum,
                     OutStockLockInfoId = lockInfo.Id,
+                    StockId = stockDetail?.StockId ?? 0,
+                    Operator = App.User.UserName,
+                    IsReverted = false,
                     OriginalBarcode = lockInfo.CurrentBarcode,
                     NewBarcode = newBarcode,
                     SplitQty = request.SplitQuantity,
-                    RemainQuantity = lockInfo.RemainQuantity - request.SplitQuantity,
+                    RemainQuantity = lockInfo.OriginalQuantity - request.SplitQuantity,
                     MaterielCode = lockInfo.MaterielCode,
                     SplitTime = DateTime.Now,
                     OrderNo = request.OrderNo,
@@ -110,10 +152,10 @@
                 };
                 await Db.Insertable(splitHistory).ExecuteCommandAsync();
 
-                Db.Ado.CommitTran();
+                _unitOfWorkManage.CommitTran();
 
                 // 7. 鍥炰紶鏂版潯鐮佺粰MES
-               // await SendBarcodeToMES(newBarcode, request.MaterielCode, request.SplitQuantity);
+                // await SendBarcodeToMES(newBarcode, request.MaterielCode, request.SplitQuantity);
 
                 return WebResponseContent.Instance.OK("鎷嗗寘鎴愬姛", new
                 {
@@ -123,7 +165,7 @@
             }
             catch (Exception ex)
             {
-                Db.Ado.RollbackTran();
+                _unitOfWorkManage.RollbackTran();
                 return WebResponseContent.Instance.Error($"鎷嗗寘澶辫触: {ex.Message}");
             }
         }
@@ -140,66 +182,67 @@
                         .OrderByDescending(x => x.CreateDate)
                         .FirstAsync();
 
-                    if (splitPackage == null)
-                        return  WebResponseContent.Instance.Error("鏈壘鍒版媶鍖呰褰�");
+                if (splitPackage == null)
+                    return WebResponseContent.Instance.Error("鏈壘鍒版媶鍖呰褰�");
 
-                    // 妫�鏌ユ柊鏉$爜鏄惁宸叉嫞閫�
-                    var newOutStockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
-                        .Where(x => x.CurrentBarcode == splitPackage.NewBarcode)
-                        .FirstAsync();
+                // 妫�鏌ユ柊鏉$爜鏄惁宸叉嫞閫�
+                var newOutStockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                    .Where(x => x.CurrentBarcode == splitPackage.NewBarcode)
+                    .FirstAsync();
 
-                    if (newOutStockInfo.Status == 1)
-                        return  WebResponseContent.Instance.Error("鏂版潯鐮佸凡鎷i�夛紝鏃犳硶鎾ら攢鎷嗗寘");
+                if (newOutStockInfo.Status == 2)
+                    return WebResponseContent.Instance.Error("鏂版潯鐮佸凡鎷i�夛紝鏃犳硶鎾ら攢鎷嗗寘");
 
-                    // 杩樺師鍘熷嚭搴撹鎯呮暟閲�
-                    var originalOutStockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
-                        .Where(x => x.CurrentBarcode == originalBarcode)
-                        .FirstAsync();
+                // 杩樺師鍘熷嚭搴撹鎯呮暟閲�
+                var originalOutStockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                    .Where(x => x.CurrentBarcode == originalBarcode)
+                    .FirstAsync();
 
-                    originalOutStockInfo.AssignQuantity += splitPackage.SplitQty;
-                    await _outStockLockInfoService.Db.Updateable(originalOutStockInfo).ExecuteCommandAsync();
+                originalOutStockInfo.AssignQuantity += splitPackage.SplitQty;
+                await _outStockLockInfoService.Db.Updateable(originalOutStockInfo).ExecuteCommandAsync();
 
-                    // 鍒犻櫎鏂板嚭搴撹鎯呰褰�
-                    await _outStockLockInfoService.Db.Deleteable<Dt_OutStockLockInfo>()
-                        .Where(x => x.CurrentBarcode == splitPackage.NewBarcode)
-                        .ExecuteCommandAsync();
+                // 鍒犻櫎鏂板嚭搴撹鎯呰褰�
+                await _outStockLockInfoService.Db.Deleteable<Dt_OutStockLockInfo>()
+                    .Where(x => x.CurrentBarcode == splitPackage.NewBarcode)
+                    .ExecuteCommandAsync();
 
-                    // 鏍囪鎷嗗寘璁板綍涓哄凡鎾ら攢
-                    splitPackage.IsReverted = true;
-                    await Db.Updateable(splitPackage).ExecuteCommandAsync();
+                // 鏍囪鎷嗗寘璁板綍涓哄凡鎾ら攢
+                splitPackage.IsReverted = true;
+                await Db.Updateable(splitPackage).ExecuteCommandAsync();
 
                 _unitOfWorkManage.CommitTran();
-                    return  WebResponseContent.Instance.OK("鎾ら攢鎷嗗寘鎴愬姛");
-                
+                return WebResponseContent.Instance.OK("鎾ら攢鎷嗗寘鎴愬姛");
+
             }
             catch (Exception ex)
             {
-                return  WebResponseContent.Instance.Error($"鎾ら攢鎷嗗寘澶辫触锛歿ex.Message}");
+                _unitOfWorkManage.RollbackTran();
+                return WebResponseContent.Instance.Error($"鎾ら攢鎷嗗寘澶辫触锛歿ex.Message}");
             }
         }
 
         // 鑾峰彇鎷嗗寘淇℃伅
-        public async Task< WebResponseContent > GetSplitPackageInfo(string orderNo, string palletCode, string barcode)
+        public async Task<WebResponseContent> GetSplitPackageInfo(string orderNo, string palletCode, string barcode)
         {
             var outStockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                 .Where(x => x.OrderNo == orderNo &&
                            x.PalletCode == palletCode &&
                            x.CurrentBarcode == barcode &&
-                           x.Status == 0)
+                           x.Status == 1)
                 .FirstAsync();
 
             if (outStockInfo == null)
-                return  WebResponseContent.Instance .Error("鏈壘鍒板搴旂殑鍑哄簱淇℃伅");
+                return WebResponseContent.Instance.Error("鏈壘鍒板搴旂殑鍑哄簱淇℃伅");
 
-            var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
-                .Where(x => x.Barcode == barcode)
-                .FirstAsync();
+            //var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+            //    .Where(x => x.Barcode == barcode)
+            //    .FirstAsync();
 
-            return  WebResponseContent.Instance .OK("",new
+            return WebResponseContent.Instance.OK("", new
             {
                 MaterielCode = outStockInfo.MaterielCode,
                 RemainQuantity = outStockInfo.RemainQuantity,
-                Unit = "涓�" // 鏍规嵁瀹為檯鎯呭喌鑾峰彇鍗曚綅
+                Unit = outStockInfo.Unit
             });
         }
         /// <summary>

--
Gitblit v1.9.3