pan
2025-11-18 e9feb28be7d4417b24bd53a77332846e2cbab734
ÏîÄ¿´úÂë/WMSÎÞ²Ö´¢°æ/WIDESEA_WMSServer/WIDESEA_OutboundService/SplitPackageService.cs
@@ -84,7 +84,7 @@
                    SupplyCode = stockDetail.SupplyCode,
                    WarehouseCode = stockDetail.WarehouseCode,
                    BarcodeQty = stockDetail.BarcodeQty,
                    BarcodeUnit = stockDetail.Barcode,
                    BarcodeUnit = stockDetail.BarcodeUnit,
                    BusinessType = stockDetail.BusinessType,
                    Unit = stockDetail.Unit,
                    StockId = lockInfo.StockId,
@@ -170,48 +170,180 @@
            }
        }
        // æ’¤é”€æ‹†åŒ…
        //public async Task<WebResponseContent> RevertSplitPackage(string originalBarcode)
        //{
        //    try
        //    {
        //        _unitOfWorkManage.BeginTran();
        //        // 1. æŸ¥æ‰¾æœ€è¿‘的未撤销拆包记录
        //        var splitRecord = await Db.Queryable<Dt_SplitPackageRecord>()
        //                .Where(x => x.OriginalBarcode == originalBarcode && !x.IsReverted)
        //                .OrderByDescending(x => x.SplitTime)
        //                .FirstAsync();
        //        if (splitRecord == null)
        //            return WebResponseContent.Instance.Error("未找到可撤销的拆包记录");
        //        // 2. æ£€æŸ¥æ–°æ¡ç æ˜¯å¦å·²æ‹£é€‰
        //        var newLockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
        //            .Where(x => x.CurrentBarcode == splitRecord.NewBarcode)
        //            .FirstAsync();
        //        if (newLockInfo?.Status ==6) // å‡è®¾çŠ¶æ€2表示已拣选
        //            return WebResponseContent.Instance.Error("新条码已拣选,无法撤销拆包");
        //        // 3. èŽ·å–åŽŸæ¡ç çš„é”å®šä¿¡æ¯
        //        var originalLockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
        //            .Where(x => x.CurrentBarcode == originalBarcode)
        //            .FirstAsync();
        //        if (originalLockInfo == null)
        //            return WebResponseContent.Instance.Error("未找到原条码锁定信息");
        //        originalLockInfo.AssignQuantity  += splitRecord.RemainQuantity;
        //       // originalLockInfo.Status = (int)OutLockStockStatusEnum.出库中;
        //        originalLockInfo.IsSplitted = 0; // æ ‡è®°ä¸ºæœªæ‹†åŒ…
        //        await _outStockLockInfoService.Db.Updateable(originalLockInfo).ExecuteCommandAsync();
        //        // 5. è¿˜åŽŸåŽŸæ¡ç åº“å­˜è®°å½•
        //        var originalStockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
        //            .Where(x => x.Barcode == originalBarcode && x.StockId == splitRecord.StockId)
        //            .FirstAsync();
        //        if (originalStockDetail != null)
        //        {
        //            // å°†æ‹†å‡ºçš„æ•°é‡åŠ å›žåˆ°åŽŸæ¡ç åº“å­˜
        //            originalStockDetail.StockQuantity += splitRecord.RemainQuantity;
        //            originalStockDetail.OutboundQuantity += splitRecord.RemainQuantity;
        //            await _stockInfoDetailService.Db.Updateable(originalStockDetail).ExecuteCommandAsync();
        //        }
        //        // 6. åˆ é™¤æ–°æ¡ç çš„锁定信息
        //        if (newLockInfo != null)
        //        {
        //            await _outStockLockInfoService.Db.Deleteable<Dt_OutStockLockInfo>()
        //                .Where(x => x.CurrentBarcode == splitRecord.NewBarcode)
        //                .ExecuteCommandAsync();
        //        }
        //        // 7. åˆ é™¤æ–°æ¡ç çš„库存记录
        //        var newStockDetail = await _stockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
        //            .Where(x => x.Barcode == splitRecord.NewBarcode)
        //            .FirstAsync();
        //        if (newStockDetail != null)
        //        {
        //            await _stockInfoDetailService.Db.Deleteable<Dt_StockInfoDetail>()
        //                .Where(x => x.Barcode == splitRecord.NewBarcode)
        //                .ExecuteCommandAsync();
        //        }
        //        // 8. æ›´æ–°æ‹†åŒ…记录为已撤销
        //        splitRecord.IsReverted = true;
        //        await Db.Updateable(splitRecord).ExecuteCommandAsync();
        //        _unitOfWorkManage.CommitTran();
        //        return WebResponseContent.Instance.OK($"撤销拆包成功,还原数量:{splitRecord.SplitQty}");
        //    }
        //    catch (Exception ex)
        //    {
        //        _unitOfWorkManage.RollbackTran();
        //        return WebResponseContent.Instance.Error($"撤销拆包失败:{ex.Message}");
        //    }
        //}
        public async Task<WebResponseContent> RevertSplitPackage(string originalBarcode)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
                // æŸ¥æ‰¾æœ€è¿‘的未撤销拆包记录
                var splitPackage = await Db.Queryable<Dt_SplitPackageRecord>()
                // 1. æŸ¥æ‰¾æ‰€æœ‰æœªæ’¤é”€çš„æ‹†åŒ…记录
                var splitRecords = await Db.Queryable<Dt_SplitPackageRecord>()
                        .Where(x => x.OriginalBarcode == originalBarcode && !x.IsReverted)
                        .OrderByDescending(x => x.CreateDate)
                        .FirstAsync();
                        .OrderBy(x => x.SplitTime) // æŒ‰æ—¶é—´æ­£åºï¼Œä»Žæœ€æ—©å¼€å§‹æ’¤é”€
                        .ToListAsync();
                if (splitPackage == null)
                    return WebResponseContent.Instance.Error("未找到拆包记录");
                if (splitRecords == null || !splitRecords.Any())
                    return WebResponseContent.Instance.Error("未找到可撤销的拆包记录");
                // æ£€æŸ¥æ–°æ¡ç æ˜¯å¦å·²æ‹£é€‰
                var newOutStockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                    .Where(x => x.CurrentBarcode == splitPackage.NewBarcode)
                    .FirstAsync();
                // 2. æ£€æŸ¥æ‰€æœ‰æ–°æ¡ç æ˜¯å¦å·²æ‹£é€‰
                var newBarcodes = splitRecords.Select(x => x.NewBarcode).ToList();
                var newLockInfos = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                    .Where(x => newBarcodes.Contains(x.CurrentBarcode))
                    .ToListAsync();
                if (newOutStockInfo.Status == 2)
                    return WebResponseContent.Instance.Error("新条码已拣选,无法撤销拆包");
                var pickedBarcodes = newLockInfos.Where(x => x.Status == 2).Select(x => x.CurrentBarcode).ToList();
                if (pickedBarcodes.Any())
                    return WebResponseContent.Instance.Error($"以下条码已拣选,无法撤销:{string.Join(",", pickedBarcodes)}");
                // è¿˜åŽŸåŽŸå‡ºåº“è¯¦æƒ…æ•°é‡
                var originalOutStockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                // 3. èŽ·å–åŽŸæ¡ç ä¿¡æ¯
                var originalLockInfo = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                    .Where(x => x.CurrentBarcode == originalBarcode)
                    .FirstAsync();
                originalOutStockInfo.AssignQuantity += splitPackage.SplitQty;
                await _outStockLockInfoService.Db.Updateable(originalOutStockInfo).ExecuteCommandAsync();
                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 => x.CurrentBarcode == splitPackage.NewBarcode)
                    .Where(x => newBarcodes.Contains(x.CurrentBarcode))
                    .ExecuteCommandAsync();
                // æ ‡è®°æ‹†åŒ…记录为已撤销
                splitPackage.IsReverted = true;
                await Db.Updateable(splitPackage).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("撤销拆包成功");
                return WebResponseContent.Instance.OK($"撤销拆包成功,共还原{splitRecords.Count}次拆包,总数量:{totalRevertQty}");
            }
            catch (Exception ex)
@@ -220,7 +352,6 @@
                return WebResponseContent.Instance.Error($"撤销拆包失败:{ex.Message}");
            }
        }
        // èŽ·å–æ‹†åŒ…ä¿¡æ¯
        public async Task<WebResponseContent> GetSplitPackageInfo(string orderNo, string palletCode, string barcode)
        {