From 246622a6e9c2563bd21d627c21c6012017f0f04e Mon Sep 17 00:00:00 2001
From: pan <antony1029@163.com>
Date: 星期四, 20 十一月 2025 15:54:03 +0800
Subject: [PATCH] 提交

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_OutboundService/SplitPackageService.cs |  476 +++++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 316 insertions(+), 160 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 b0b9bab..34b8f2b 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,6 +1,7 @@
 锘縰sing Microsoft.AspNetCore.Http;
 using Microsoft.Extensions.Logging;
 using Org.BouncyCastle.Asn1.Ocsp;
+using SqlSugar.Extensions;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -45,6 +46,168 @@
         /// <summary>
         /// 鎷嗗寘鎷嗙鎿嶄綔
         /// </summary>
+        //public async Task<WebResponseContent> SplitPackage(SplitPackageDto request)
+        //{
+        //    try
+        //    {
+        //        _unitOfWorkManage.BeginTran();
+
+        //        // 1. 楠岃瘉鍑哄簱閿佸畾淇℃伅
+        //        var lockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+        //                  .Where(x => x.OrderNo == request.OrderNo &&
+        //                   x.PalletCode == request.PalletCode &&
+        //                   x.CurrentBarcode == request.OriginalBarcode &&
+        //                   x.Status == 1)
+        //            .FirstAsync();
+
+        //        if (lockInfo == null)
+        //            return WebResponseContent.Instance.Error("鏈壘鍒版湁鏁堢殑鍑哄簱閿佸畾淇℃伅");
+
+        //        // 2. 妫�鏌ュ墿浣欓攣瀹氭暟閲�
+        //        decimal remainingLockQuantity = lockInfo.OriginalQuantity - lockInfo.PickedQty;
+        //        if (request.SplitQuantity > remainingLockQuantity)
+        //            return WebResponseContent.Instance.Error($"鎷嗗寘鏁伴噺涓嶈兘澶т簬鍓╀綑閿佸畾鏁伴噺锛屽墿浣欙細{remainingLockQuantity}");
+
+        //        var baseStockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+        //       .Where(x => x.Barcode == request.OriginalBarcode && x.StockId == lockInfo.StockId)
+        //       .FirstAsync();
+        //        if (baseStockDetail == null)
+        //            throw new Exception($"鏈壘鍒版潯鐮亄request.OriginalBarcode}瀵瑰簲鐨勫簱瀛樿褰�");
+
+
+        //        // 4. 璁$畻鎷嗗垎鍚庣殑鏁伴噺
+        //        decimal remainingQty = baseStockDetail.StockQuantity - request.SplitQuantity;
+
+        //        // 鏇存柊鍩虹鏉$爜鐨勫簱瀛樻暟閲忎负鍓╀綑鏁伴噺
+        //        baseStockDetail.StockQuantity = remainingQty;
+        //        baseStockDetail.OutboundQuantity = remainingQty;
+        //        await _stockInfoDetailService.Db.Updateable(baseStockDetail).ExecuteCommandAsync();
+
+
+        //        var seq = await _dailySequenceService.GetNextSequenceAsync();
+        //        // 3. 鐢熸垚鏂版潯鐮�
+        //        string newBarcode = "WSLOT" + DateTime.Now.ToString("yyyyMMdd") + seq.ToString()?.PadLeft(5, '0');
+
+
+        //        // 涓烘媶鍖呬骇鐢熺殑鏂版潯鐮佸垱寤哄簱瀛樿褰�
+        //        var newStockDetail = new Dt_StockInfoDetail
+        //        {
+        //            SupplyCode = baseStockDetail.SupplyCode,
+        //            WarehouseCode = baseStockDetail.WarehouseCode,
+        //            BarcodeQty = baseStockDetail.BarcodeQty,
+        //            BarcodeUnit = baseStockDetail.BarcodeUnit,
+        //            BusinessType = baseStockDetail.BusinessType,
+        //            Unit = baseStockDetail.Unit,
+        //            StockId = lockInfo.StockId,
+        //            MaterielCode = baseStockDetail.MaterielCode,
+        //            OrderNo = baseStockDetail.OrderNo,
+        //            BatchNo = baseStockDetail.BatchNo,
+        //            StockQuantity = request.SplitQuantity, // 鏂版潯鐮佽幏寰楁媶鍒嗘暟閲�
+        //            OutboundQuantity = request.SplitQuantity,
+        //            Barcode = newBarcode,
+        //            InboundOrderRowNo = baseStockDetail.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 = request.SplitQuantity,
+        //            OriginalQuantity = request.SplitQuantity,
+        //            AssignQuantity = request.SplitQuantity, // 鏂版潯鐮佸垎閰嶆暟閲�
+        //            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 _outStockLockInfoService.Db.Insertable(newLockInfo).ExecuteCommandAsync();
+
+        //        lockInfo.AssignQuantity = remainingQty;
+        //        lockInfo.IsSplitted = 1; // 鏍囪涓哄凡鎷嗗寘
+        //        await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
+
+        //        var previousSplitRecord = await Db.Queryable<Dt_SplitPackageRecord>()
+        //      .Where(x => x.OriginalBarcode == request.OriginalBarcode && !x.IsReverted)
+        //      .OrderByDescending(x => x.SplitTime)
+        //      .FirstAsync();
+
+        //        // 6. 璁板綍鎷嗗寘鍘嗗彶锛堢敤浜庤拷韪級
+        //        var splitHistory = new Dt_SplitPackageRecord
+        //        {
+        //            FactoryArea = lockInfo.FactoryArea,
+        //            TaskNum = lockInfo.TaskNum,
+        //            OutStockLockInfoId = lockInfo.Id,
+        //            StockId = baseStockDetail.StockId,
+        //            Operator = App.User.UserName,
+        //            IsReverted = false,
+        //            OriginalBarcode = request.OriginalBarcode,
+        //            NewBarcode = newBarcode,
+        //            SplitQty = request.SplitQuantity,
+        //            RemainQuantity = remainingQty, // 璁板綍鎷嗗垎鍚庣殑鍓╀綑鏁伴噺
+        //            MaterielCode = lockInfo.MaterielCode,
+        //            SplitTime = DateTime.Now,
+        //            OrderNo = request.OrderNo,
+        //            PalletCode = request.PalletCode,
+        //            Status = (int)SplitPackageStatusEnum.宸叉媶鍖�,
+        //            PreviousSplitRecordId = previousSplitRecord?.Id??0 // 璁板綍鍓嶄竴娆℃媶鍖匢D锛屽缓绔嬫媶鍖呴摼
+        //        };
+        //        await Db.Insertable(splitHistory).ExecuteCommandAsync();
+
+        //        _unitOfWorkManage.CommitTran();
+
+        //        try
+        //        {
+        //            MaterielToMesDTO dto = new MaterielToMesDTO
+        //            {
+        //                batchNo = baseStockDetail.BatchNo,
+        //                factoryArea = baseStockDetail.FactoryArea,
+        //                materialCode = baseStockDetail.MaterielCode,
+        //                newmaterialCode = newBarcode,
+        //                oldmaterialCode = request.OriginalBarcode,
+        //                operationType = 1,
+        //                qty = remainingQty,
+        //                supplyCode = baseStockDetail.SupplyCode,
+        //                unit = baseStockDetail.BarcodeUnit,
+        //                warehouseCode = baseStockDetail.WarehouseCode,
+        //                reqCode = Guid.NewGuid().ToString(),
+        //                reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
+        //            };
+        //            _invokeMESService.NewMaterielToMes(dto);
+        //        }
+        //        catch(Exception ex)
+        //        {
+        //            _logger.LogError("SplitPackage 鍥炰紶MES:  " + ex.Message);
+        //        }
+        //        return WebResponseContent.Instance.OK("鎷嗗寘鎴愬姛", new
+        //        {
+        //            NewBarcode = newBarcode,
+        //            NewLockInfoId = newLockInfo.Id
+        //        });
+        //    }
+        //    catch (Exception ex)
+        //    {
+        //        _unitOfWorkManage.RollbackTran();
+        //        return WebResponseContent.Instance.Error($"鎷嗗寘澶辫触: {ex.Message}");
+        //    }
+        //}
+        // 鑾峰彇鏉$爜鐨勬媶鍖呭巻鍙查摼
+
         public async Task<WebResponseContent> SplitPackage(SplitPackageDto request)
         {
             try
@@ -67,58 +230,63 @@
                 if (request.SplitQuantity > remainingLockQuantity)
                     return WebResponseContent.Instance.Error($"鎷嗗寘鏁伴噺涓嶈兘澶т簬鍓╀綑閿佸畾鏁伴噺锛屽墿浣欙細{remainingLockQuantity}");
 
-                var stockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
-               .Where(x => x.Barcode == request.OriginalBarcode && x.StockId == lockInfo.StockId)
-               .FirstAsync();
-                if (stockDetail == null)
+                // 3. 鏌ユ壘褰撳墠鏉$爜鐨勫簱瀛樹俊鎭�
+                var baseStockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+                    .Where(x => x.Barcode == request.OriginalBarcode && x.StockId == lockInfo.StockId)
+                    .FirstAsync();
+
+                if (baseStockDetail == null)
                     throw new Exception($"鏈壘鍒版潯鐮亄request.OriginalBarcode}瀵瑰簲鐨勫簱瀛樿褰�");
 
-                stockDetail.StockQuantity = request.SplitQuantity;
-                stockDetail.OutboundQuantity = request.SplitQuantity;
-                _stockInfoDetailService.Db.Updateable<Dt_StockInfoDetail>(stockDetail).ExecuteCommand();
+                // 4. 璁板綍鎷嗗寘鍓嶇殑搴撳瓨鏁伴噺鍜岄攣瀹氫俊鎭�
+                decimal stockBeforeSplit = baseStockDetail.StockQuantity;
+                decimal assignBeforeSplit = lockInfo.AssignQuantity;
 
+                // 5. 璁$畻鎷嗗垎鍚庣殑鍓╀綑鏁伴噺
+                decimal remainingQty = baseStockDetail.StockQuantity - request.SplitQuantity;
 
+                // 鏇存柊鍩虹鏉$爜鐨勫簱瀛樻暟閲忎负鍓╀綑鏁伴噺
+                baseStockDetail.StockQuantity = remainingQty;
+                baseStockDetail.OutboundQuantity = remainingQty;
+                await _stockInfoDetailService.Db.Updateable(baseStockDetail).ExecuteCommandAsync();
+
+                // 6. 鐢熸垚鏂版潯鐮�
                 var seq = await _dailySequenceService.GetNextSequenceAsync();
-                // 3. 鐢熸垚鏂版潯鐮�
                 string newBarcode = "WSLOT" + DateTime.Now.ToString("yyyyMMdd") + seq.ToString()?.PadLeft(5, '0');
 
-                decimal remainingQty = remainingLockQuantity - request.SplitQuantity;
-
-                // 涓烘媶鍖呬骇鐢熺殑鏂版潯鐮佸垱寤哄簱瀛樿褰�
+                // 7. 涓烘柊鏉$爜鍒涘缓搴撳瓨璁板綍锛堟媶鍒嗗嚭鐨勬暟閲忥級
                 var newStockDetail = new Dt_StockInfoDetail
                 {
-                    SupplyCode = stockDetail.SupplyCode,
-                    WarehouseCode = stockDetail.WarehouseCode,
-                    BarcodeQty = stockDetail.BarcodeQty,
-                    BarcodeUnit = stockDetail.BarcodeUnit,
-                    BusinessType = stockDetail.BusinessType,
-                    Unit = stockDetail.Unit,
+                    SupplyCode = baseStockDetail.SupplyCode,
+                    WarehouseCode = baseStockDetail.WarehouseCode,
+                    BarcodeQty = baseStockDetail.BarcodeQty,
+                    BarcodeUnit = baseStockDetail.BarcodeUnit,
+                    BusinessType = baseStockDetail.BusinessType,
+                    Unit = baseStockDetail.Unit,
                     StockId = lockInfo.StockId,
-                    MaterielCode = stockDetail.MaterielCode,
-                    OrderNo = stockDetail.OrderNo,
-                    BatchNo = stockDetail.BatchNo,
-                    StockQuantity = remainingQty,
-                    OutboundQuantity = remainingQty, // 閿佸畾鍏ㄩ儴鏁伴噺
+                    MaterielCode = baseStockDetail.MaterielCode,
+                    OrderNo = baseStockDetail.OrderNo,
+                    BatchNo = baseStockDetail.BatchNo,
+                    StockQuantity = request.SplitQuantity,
+                    OutboundQuantity = request.SplitQuantity,
                     Barcode = newBarcode,
-                    InboundOrderRowNo = stockDetail.InboundOrderRowNo,
-
+                    InboundOrderRowNo = baseStockDetail.InboundOrderRowNo,
                 };
-                await _outStockLockInfoService.Db.Insertable(newStockDetail).ExecuteCommandAsync();
+                await _stockInfoDetailService.Db.Insertable(newStockDetail).ExecuteCommandAsync();
 
-                // 4. 鍒涘缓鏂扮殑鍑哄簱閿佸畾淇℃伅锛堟柊鏉$爜锛�
+                // 8. 鍒涘缓鏂扮殑鍑哄簱閿佸畾淇℃伅锛堟柊鏉$爜锛�
                 var newLockInfo = new Dt_OutStockLockInfo
                 {
-
                     OrderNo = lockInfo.OrderNo,
                     OrderDetailId = lockInfo.OrderDetailId,
                     BatchNo = lockInfo.BatchNo,
                     MaterielCode = lockInfo.MaterielCode,
                     MaterielName = lockInfo.MaterielName,
                     StockId = lockInfo.StockId,
-                    OrderQuantity = remainingQty,
-                    OriginalQuantity = remainingQty,
-                    AssignQuantity = remainingQty, // 鏂版潯鐮佸垎閰嶆暟閲�
-                    PickedQty = 0, // 鏂版潯鐮佹湭鎷i��
+                    OrderQuantity = request.SplitQuantity,
+                    OriginalQuantity = request.SplitQuantity,
+                    AssignQuantity = request.SplitQuantity,
+                    PickedQty = 0,
                     LocationCode = lockInfo.LocationCode,
                     PalletCode = lockInfo.PalletCode,
                     TaskNum = lockInfo.TaskNum,
@@ -126,30 +294,33 @@
                     Unit = lockInfo.Unit,
                     SupplyCode = lockInfo.SupplyCode,
                     OrderType = lockInfo.OrderType,
-                    CurrentBarcode = newBarcode, // 鏂版潯鐮�
+                    CurrentBarcode = newBarcode,
                     OriginalLockQuantity = request.SplitQuantity,
                     IsSplitted = 1,
-                    ParentLockId = lockInfo.Id // 璁板綍鐖剁骇閿佸畾ID
+                    ParentLockId = lockInfo.Id
                 };
                 await _outStockLockInfoService.Db.Insertable(newLockInfo).ExecuteCommandAsync();
 
-                // 5. 鏇存柊鍘熼攣瀹氫俊鎭殑鍒嗛厤鏁伴噺锛堝噺灏戞媶鍖呮暟閲忥級
-                lockInfo.AssignQuantity = request.SplitQuantity;
+                // 9. 鏇存柊鍘熼攣瀹氫俊鎭殑鍒嗛厤鏁伴噺锛堝噺灏戞媶鍖呮暟閲忥級
+                lockInfo.AssignQuantity = remainingQty;
+                lockInfo.IsSplitted = 1;
                 await _outStockLockInfoService.Db.Updateable(lockInfo).ExecuteCommandAsync();
 
-                // 6. 璁板綍鎷嗗寘鍘嗗彶锛堢敤浜庤拷韪級
+                // 10. 璁板綍鎷嗗寘鍘嗗彶
                 var splitHistory = new Dt_SplitPackageRecord
                 {
                     FactoryArea = lockInfo.FactoryArea,
                     TaskNum = lockInfo.TaskNum,
                     OutStockLockInfoId = lockInfo.Id,
-                    StockId = stockDetail?.StockId ?? 0,
+                    StockId = baseStockDetail.StockId,
                     Operator = App.User.UserName,
                     IsReverted = false,
-                    OriginalBarcode = lockInfo.CurrentBarcode,
+                    OriginalBarcode = request.OriginalBarcode,
                     NewBarcode = newBarcode,
                     SplitQty = request.SplitQuantity,
-                    RemainQuantity = lockInfo.OriginalQuantity - request.SplitQuantity,
+                    RemainQuantity = remainingQty,
+                    StockBeforeSplit = stockBeforeSplit, // 璁板綍鎷嗗寘鍓嶇殑搴撳瓨鏁伴噺
+                    AssignBeforeSplit = assignBeforeSplit, // 璁板綍鎷嗗寘鍓嶇殑鍒嗛厤鏁伴噺
                     MaterielCode = lockInfo.MaterielCode,
                     SplitTime = DateTime.Now,
                     OrderNo = request.OrderNo,
@@ -159,34 +330,12 @@
                 await Db.Insertable(splitHistory).ExecuteCommandAsync();
 
                 _unitOfWorkManage.CommitTran();
-                              
-                try
-                {
-                    MaterielToMesDTO dto = new MaterielToMesDTO
-                    {
-                        batchNo = stockDetail.BatchNo,
-                        factoryArea = stockDetail.FactoryArea,
-                        materialCode = stockDetail.MaterielCode,
-                        newmaterialCode = newBarcode,
-                        oldmaterialCode = request.OriginalBarcode,
-                        operationType = 1,
-                        qty = remainingQty,
-                        supplyCode = stockDetail.SupplyCode,
-                        unit = stockDetail.BarcodeUnit,
-                        warehouseCode = stockDetail.WarehouseCode,
-                        reqCode = Guid.NewGuid().ToString(),
-                        reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
-                    };
-                    _invokeMESService.NewMaterielToMes(dto);
-                }
-                catch(Exception ex)
-                {
-                    _logger.LogError("SplitPackage 鍥炰紶MES:  " + ex.Message);
-                }
-                return WebResponseContent.Instance.OK("鎷嗗寘鎴愬姛", new
+
+                return WebResponseContent.Instance.OK("鎷嗗寘鎴愬姛", new SplitPackageChainDto
                 {
                     NewBarcode = newBarcode,
-                    NewLockInfoId = newLockInfo.Id
+                    NewLockInfoId = newLockInfo.Id,
+                    RemainQuantity = remainingQty
                 });
             }
             catch (Exception ex)
@@ -196,7 +345,96 @@
             }
         }
 
- 
+        public async Task<WebResponseContent> RevertSplitPackage(string originalBarcode)
+        {
+            try
+            {
+                _unitOfWorkManage.BeginTran();
+
+                // 1. 鏌ユ壘璇ユ潯鐮佷綔涓哄師濮嬫潯鐮佺殑鎵�鏈夋湭鎾ら攢鎷嗗寘璁板綍锛屾寜鏃堕棿鍊掑簭
+                var splitRecords = await Db.Queryable<Dt_SplitPackageRecord>()
+                        .Where(x => x.OriginalBarcode == originalBarcode && !x.IsReverted)
+                        .OrderByDescending(x => x.SplitTime)
+                        .ToListAsync();
+
+                if (splitRecords == null || !splitRecords.Any())
+                    return WebResponseContent.Instance.Error("鏈壘鍒板彲鎾ら攢鐨勬媶鍖呰褰�");
+
+                // 2. 妫�鏌ユ墍鏈夋柊鏉$爜鏄惁宸叉嫞閫�
+                var newBarcodes = splitRecords.Select(x => x.NewBarcode).ToList();
+                var newLockInfos = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                    .Where(x => newBarcodes.Contains(x.CurrentBarcode))
+                    .ToListAsync();
+
+                var pickedBarcodes = newLockInfos.Where(x => x.Status == 2).Select(x => x.CurrentBarcode).ToList();
+                if (pickedBarcodes.Any())
+                    return WebResponseContent.Instance.Error($"浠ヤ笅鏉$爜宸叉嫞閫夛紝鏃犳硶鎾ら攢鎷嗗寘锛歿string.Join(",", pickedBarcodes)}");
+
+                // 3. 鑾峰彇鍘熸潯鐮佺殑閿佸畾淇℃伅鍜屽簱瀛樹俊鎭�
+                var originalLockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                    .Where(x => x.CurrentBarcode == originalBarcode)
+                    .FirstAsync();
+
+                if (originalLockInfo == null)
+                    return WebResponseContent.Instance.Error("鏈壘鍒板師鏉$爜閿佸畾淇℃伅");
+
+                var originalStockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+                    .Where(x => x.Barcode == originalBarcode && x.StockId == originalLockInfo.StockId)
+                    .FirstAsync();
+
+                if (originalStockDetail == null)
+                    return WebResponseContent.Instance.Error("鏈壘鍒板師鏉$爜搴撳瓨淇℃伅");
+
+                // 4. 鏌ユ壘绗竴娆℃媶鍖呰褰曪紝鑾峰彇鎷嗗寘鍓嶇殑鐘舵��
+                var firstSplitRecord = splitRecords.OrderBy(x => x.SplitTime).FirstOrDefault();
+                if (firstSplitRecord == null)
+                    return WebResponseContent.Instance.Error("鏈壘鍒版湁鏁堢殑鎷嗗寘璁板綍");
+
+                // 5. 鎭㈠鍘熸潯鐮佸埌鎷嗗寘鍓嶇殑鐘舵��
+                originalStockDetail.StockQuantity = firstSplitRecord.StockBeforeSplit;
+                originalStockDetail.OutboundQuantity = firstSplitRecord.StockBeforeSplit;
+                await _stockInfoDetailService.Db.Updateable(originalStockDetail).ExecuteCommandAsync();
+
+                // 6. 鎭㈠鍘熸潯鐮侀攣瀹氫俊鎭埌鎷嗗寘鍓嶇殑鐘舵��
+                originalLockInfo.AssignQuantity = firstSplitRecord.AssignBeforeSplit;
+                originalLockInfo.OriginalQuantity = firstSplitRecord.AssignBeforeSplit;
+                originalLockInfo.OrderQuantity = firstSplitRecord.AssignBeforeSplit;
+                originalLockInfo.Status = (int)OutLockStockStatusEnum.鍑哄簱涓�;
+                originalLockInfo.IsSplitted = 0; // 鎭㈠涓烘湭鎷嗗寘鐘舵��
+                await _outStockLockInfoService.Db.Updateable(originalLockInfo).ExecuteCommandAsync();
+
+                // 7. 鍒犻櫎鎵�鏈夋柊鏉$爜鐨勯攣瀹氫俊鎭�
+                await _outStockLockInfoService.Db.Deleteable<Dt_OutStockLockInfo>()
+                    .Where(x => newBarcodes.Contains(x.CurrentBarcode))
+                    .ExecuteCommandAsync();
+
+                // 8. 鍒犻櫎鎵�鏈夋柊鏉$爜鐨勫簱瀛樿褰�
+                await _stockInfoDetailService.Db.Deleteable<Dt_StockInfoDetail>()
+                    .Where(x => newBarcodes.Contains(x.Barcode))
+                    .ExecuteCommandAsync();
+
+                // 9. 鏍囪鎵�鏈夋媶鍖呰褰曚负宸叉挙閿�
+                foreach (var record in splitRecords)
+                {
+                    record.IsReverted = true;
+                    record.RevertTime = DateTime.Now;
+         
+                    record.Status = (int)SplitPackageStatusEnum.宸叉挙閿�;
+                }
+                await Db.Updateable(splitRecords).ExecuteCommandAsync();
+
+                _unitOfWorkManage.CommitTran();
+
+                return WebResponseContent.Instance.OK($"鎾ら攢鎷嗗寘鎴愬姛锛屽叡鎾ら攢{splitRecords.Count}娆℃媶鍖咃紝{originalBarcode}鎭㈠涓烘媶鍖呭墠鏁伴噺锛歿firstSplitRecord.StockBeforeSplit}");
+
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                return WebResponseContent.Instance.Error($"鎾ら攢鎷嗗寘澶辫触锛歿ex.Message}");
+            }
+        }
+
         //public async Task<WebResponseContent> RevertSplitPackage(string originalBarcode)
         //{
         //    try
@@ -228,7 +466,7 @@
         //        if (originalLockInfo == null)
         //            return WebResponseContent.Instance.Error("鏈壘鍒板師鏉$爜閿佸畾淇℃伅");
 
-          
+
         //        originalLockInfo.AssignQuantity  += splitRecord.RemainQuantity;  
         //       // originalLockInfo.Status = (int)OutLockStockStatusEnum.鍑哄簱涓�;
         //        originalLockInfo.IsSplitted = 0; // 鏍囪涓烘湭鎷嗗寘
@@ -269,7 +507,7 @@
 
         //        // 8. 鏇存柊鎷嗗寘璁板綍涓哄凡鎾ら攢
         //        splitRecord.IsReverted = true;              
-        
+
         //        await Db.Updateable(splitRecord).ExecuteCommandAsync();
 
         //        _unitOfWorkManage.CommitTran();
@@ -283,101 +521,19 @@
         //        return WebResponseContent.Instance.Error($"鎾ら攢鎷嗗寘澶辫触锛歿ex.Message}");
         //    }
         //}
- 
-        public async Task<WebResponseContent> RevertSplitPackage(string originalBarcode)
+
+
+        // 鑾峰彇鍙挙閿�鐨勬媶鍖呰褰曞垪琛�
+        public Dt_SplitPackageRecord GetRevertableSplitRecords(string originalBarcode)
         {
-            try
-            {
-                _unitOfWorkManage.BeginTran();
+            var revertableRecords =   Db.Queryable<Dt_SplitPackageRecord>()
+                    .Where(x => x.OriginalBarcode == originalBarcode && !x.IsReverted)
+                    .OrderBy(x => x.SplitTime)
+                   .First();
 
-                // 1. 鏌ユ壘鎵�鏈夋湭鎾ら攢鐨勬媶鍖呰褰�
-                var splitRecords = await Db.Queryable<Dt_SplitPackageRecord>()
-                        .Where(x => x.OriginalBarcode == originalBarcode && !x.IsReverted)
-                        .OrderBy(x => x.SplitTime) // 鎸夋椂闂存搴忥紝浠庢渶鏃╁紑濮嬫挙閿�
-                        .ToListAsync();
-
-                if (splitRecords == null || !splitRecords.Any())
-                    return WebResponseContent.Instance.Error("鏈壘鍒板彲鎾ら攢鐨勬媶鍖呰褰�");
-
-                // 2. 妫�鏌ユ墍鏈夋柊鏉$爜鏄惁宸叉嫞閫�
-                var newBarcodes = splitRecords.Select(x => x.NewBarcode).ToList();
-                var newLockInfos = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
-                    .Where(x => newBarcodes.Contains(x.CurrentBarcode))
-                    .ToListAsync();
-
-                var pickedBarcodes = newLockInfos.Where(x => x.Status == 2).Select(x => x.CurrentBarcode).ToList();
-                if (pickedBarcodes.Any())
-                    return WebResponseContent.Instance.Error($"浠ヤ笅鏉$爜宸叉嫞閫夛紝鏃犳硶鎾ら攢锛歿string.Join(",", pickedBarcodes)}");
-
-                // 3. 鑾峰彇鍘熸潯鐮佷俊鎭�
-                var originalLockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
-                    .Where(x => x.CurrentBarcode == originalBarcode)
-                    .FirstAsync();
-
-                if (originalLockInfo == null)
-                    return WebResponseContent.Instance.Error("鏈壘鍒板師鏉$爜閿佸畾淇℃伅");
-
-                var originalStockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
-                    .Where(x => x.Barcode == originalBarcode && x.StockId == originalLockInfo.StockId)
-                    .FirstAsync();
-
-                // 4. 鑾峰彇鎵�鏈夋柊鏉$爜鐨勫簱瀛樿褰�
-                var newStockDetails = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
-                    .Where(x => newBarcodes.Contains(x.Barcode))
-                    .ToListAsync();
-
-                // 5. 璁$畻鎬昏繕鍘熸暟閲�
-                decimal totalRevertQty = newStockDetails.Sum(x => x.StockQuantity);
-
-                // 6. 杩樺師鍘熸潯鐮佸簱瀛樿褰�
-                if (originalStockDetail != null)
-                {
-                    // 鍘熸潯鐮佸綋鍓嶆暟閲忓姞涓婃墍鏈夋柊鏉$爜鐨勬暟閲�
-                    originalStockDetail.StockQuantity += totalRevertQty;
-                    originalStockDetail.OutboundQuantity += totalRevertQty;
-                    await _stockInfoDetailService.Db.Updateable(originalStockDetail).ExecuteCommandAsync();
-                }
-
-                // 7. 杩樺師鍘熸潯鐮侀攣瀹氫俊鎭�
-                decimal totalAssignQty = newLockInfos.Sum(x => x.AssignQuantity);
-                originalLockInfo.AssignQuantity += totalAssignQty;
-                if (originalLockInfo.OrderQuantity < originalLockInfo.AssignQuantity)
-                {
-                    originalLockInfo.AssignQuantity=originalLockInfo.OrderQuantity;
-                }
-                originalLockInfo.Status = (int)OutLockStockStatusEnum.鍑哄簱涓�;
-                originalLockInfo.IsSplitted = 0;
-                await _outStockLockInfoService.Db.Updateable(originalLockInfo).ExecuteCommandAsync();
-
-                // 8. 鍒犻櫎鎵�鏈夋柊鏉$爜鐨勯攣瀹氫俊鎭�
-                await _outStockLockInfoService.Db.Deleteable<Dt_OutStockLockInfo>()
-                    .Where(x => newBarcodes.Contains(x.CurrentBarcode))
-                    .ExecuteCommandAsync();
-
-                // 9. 鍒犻櫎鎵�鏈夋柊鏉$爜鐨勫簱瀛樿褰�
-                await _stockInfoDetailService.Db.Deleteable<Dt_StockInfoDetail>()
-                    .Where(x => newBarcodes.Contains(x.Barcode))
-                    .ExecuteCommandAsync();
-
-                // 10. 鏍囪鎵�鏈夋媶鍖呰褰曚负宸叉挙閿�
-                foreach (var record in splitRecords)
-                {
-                    record.IsReverted = true;
-              
-                }
-                await Db.Updateable(splitRecords).ExecuteCommandAsync();
-
-                _unitOfWorkManage.CommitTran();
-
-                return WebResponseContent.Instance.OK($"鎾ら攢鎷嗗寘鎴愬姛锛屽叡杩樺師{splitRecords.Count}娆℃媶鍖咃紝鎬绘暟閲忥細{totalRevertQty}");
-
-            }
-            catch (Exception ex)
-            {
-                _unitOfWorkManage.RollbackTran();
-                return WebResponseContent.Instance.Error($"鎾ら攢鎷嗗寘澶辫触锛歿ex.Message}");
-            }
+            return   revertableRecords ;
         }
+ 
         // 鑾峰彇鎷嗗寘淇℃伅
         public async Task<WebResponseContent> GetSplitPackageInfo(string orderNo, string palletCode, string barcode)
         {

--
Gitblit v1.9.3