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

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs |  203 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 194 insertions(+), 9 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_TaskInfoService/TaskService.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_TaskInfoService/TaskService.cs"
index 225e7f6..7d96932 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_TaskInfoService/TaskService.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_TaskInfoService/TaskService.cs"
@@ -63,7 +63,7 @@
 
         private readonly IOutboundOrderService _outboundOrderService;
         private readonly IOutboundOrderDetailService _outboundOrderDetailService;
-
+        private readonly IOutStockLockInfoService _outStockLockInfoService;
         private readonly ILocationStatusChangeRecordService _locationStatusChangeRecordService;
         private readonly IESSApiService _eSSApiService;
         private readonly IStockService _stockService;
@@ -89,7 +89,7 @@
 
         public List<int> TaskOutboundTypes => typeof(TaskTypeEnum).GetEnumIndexList();
 
-        public TaskService(IRepository<Dt_Task> BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IRepository<Dt_StockInfo> stockRepository, ILocationInfoService locationInfoService, IInboundOrderService inboundOrderService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IESSApiService eSSApiService, ILogger<TaskService> logger, IStockService stockService, IRecordService recordService, IInboundOrderDetailService inboundOrderDetailService, IOutboundOrderService outboundOrderService, IOutboundOrderDetailService outboundOrderDetailService, IInvokeMESService invokeMESService) : base(BaseDal)
+        public TaskService(IRepository<Dt_Task> BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IRepository<Dt_StockInfo> stockRepository, ILocationInfoService locationInfoService, IInboundOrderService inboundOrderService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IESSApiService eSSApiService, ILogger<TaskService> logger, IStockService stockService, IRecordService recordService, IInboundOrderDetailService inboundOrderDetailService, IOutboundOrderService outboundOrderService, IOutboundOrderDetailService outboundOrderDetailService, IInvokeMESService invokeMESService, IOutStockLockInfoService outStockLockInfoService) : base(BaseDal)
         {
             _mapper = mapper;
             _unitOfWorkManage = unitOfWorkManage;
@@ -105,7 +105,9 @@
             _outboundOrderService = outboundOrderService;
             _outboundOrderDetailService = outboundOrderDetailService;
             _invokeMESService = invokeMESService;
+            _outStockLockInfoService = outStockLockInfoService;
         }
+
 
         /// <summary>
         /// 
@@ -251,21 +253,23 @@
                             business_type = inboundOrder.BusinessType,
                             factoryArea = inboundOrder.FactoryArea,
                             operationType = 1,
+                            Operator = inboundOrder.Operator,
                             orderNo = inboundOrder.UpperOrderNo,
                             status = inboundOrder.OrderStatus,
                             details = new List<FeedbackInboundDetailsModel>()
 
                         };
 
-                        var groupedData = inboundOrder.Details.GroupBy(item => new { item.MaterielCode, item.SupplyCode, item.BatchNo, item.lineNo, item.BarcodeUnit, item.WarehouseCode})
+                        var groupedData = inboundOrder.Details.GroupBy(item => new { item.MaterielCode, item.SupplyCode, item.BatchNo, item.lineNo, item.BarcodeUnit, item.WarehouseCode })
                            .Select(group => new FeedbackInboundDetailsModel
                            {
                                materialCode = group.Key.MaterielCode,
                                supplyCode = group.Key.SupplyCode,
                                batchNo = group.Key.BatchNo,
                                lineNo = group.Key.lineNo,
-                               warehouseCode = group.Key.WarehouseCode=="0"?"1072": group.Key.WarehouseCode,
-                               unit= group.Key.BarcodeUnit,
+                               warehouseCode = group.Key.WarehouseCode,
+                               // warehouseCode= "1072",
+                               unit = group.Key.BarcodeUnit,
                                barcodes = group.Select(row => new FeedbackBarcodesModel
                                {
                                    barcode = row.Barcode,
@@ -273,20 +277,51 @@
                                }).ToList()
                            }).ToList();
                         feedmodel.details = groupedData;
-                        
+
                         _invokeMESService.FeedbackInbound(feedmodel);
                     }
 
                 }
             }
-            catch (Exception ex) {
+            catch (Exception ex)
+            {
                 _logger.LogInformation("InboundTaskCompleted 鍥炲啓MES澶辫触:  " + ex.Message);
             }
 
             return WebResponseContent.Instance.OK();
         }
 
+        public WebResponseContent OutboundTaskCompleted(Dt_Task task)
+        {
+            _logger.LogInformation($"TaskService  OutboundTaskCompleted: {task.TaskNum}");
+            //鏌ヨ揣浣�
+            Dt_LocationInfo locationInfo = _locationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
+            if (locationInfo == null)
+            {
+                return WebResponseContent.Instance.Error($"鏈壘鍒板搴旂殑缁堢偣璐т綅淇℃伅");
+            }
+            locationInfo.LocationStatus = LocationStatusEnum.Free.ObjToInt();
+            _locationInfoService.Repository.UpdateData(locationInfo);
 
+            var outloks = _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>().Where(x => x.TaskNum == task.TaskNum).ToList();
+            outloks.ForEach(o =>
+            {
+                o.Status = OutLockStockStatusEnum.宸插嚭搴�.ObjToInt();
+            });
+            _outStockLockInfoService.Db.Updateable(outloks).ExecuteCommand();
+
+            var locationCodes = outloks.Select(it => it.LocationCode).Distinct().ToList();
+
+            //_stockRepository.Db.Updateable<Dt_StockInfo>()
+            //  .SetColumns(it => new Dt_StockInfo { StockStatus = StockStatusEmun.})
+            //  .Where(it => locationCodes.Contains(it.LocationCode))
+            //  .ExecuteCommand();
+
+
+            return WebResponseContent.Instance.OK();
+
+
+        }
         public async Task<WebResponseContent> InEmptyTaskCompleted(Dt_Task task)
         {
 
@@ -340,14 +375,20 @@
             }
         }
 
+        public  WebResponseContent InPickTaskCompleted(Dt_Task task)
+        {
+            _logger.LogInformation($"TaskService  InPickTaskCompleted: {task.TaskNum}");
 
+
+            return WebResponseContent.Instance.OK();
+        }
 
         public async Task<WebResponseContent> OutEmptyTaskCompleted(Dt_Task task)
         {
             WebResponseContent content = new WebResponseContent();
             try
             {
-                Dt_StockInfo stockInfo = _stockRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == task.PalletCode && x.WarehouseId == task.WarehouseId).First();
+                Dt_StockInfo stockInfo = _stockRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == task.PalletCode).First();
                 if (stockInfo == null)
                 {
                     return WebResponseContent.Instance.Error($"鏈壘鍒版墭鐩樺搴旂殑搴撳瓨淇℃伅");
@@ -359,7 +400,6 @@
                     return content.Error($"鏈壘鍒板搴旂殑缁堢偣璐т綅淇℃伅");
                 }
 
-               // _stockRepository.Db.Deleteable(stockInfo);
 
                 int beforeStatus = locationInfo.LocationStatus;
 
@@ -371,6 +411,7 @@
                 task.TaskStatus = TaskStatusEnum.Finish.ObjToInt();
                 BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateTypeEnum.鑷姩瀹屾垚 : OperateTypeEnum.浜哄伐瀹屾垚);
                 _stockService.StockInfoService.Repository.DeleteAndMoveIntoHty(stockInfo, App.User.UserId == 0 ? OperateTypeEnum.鑷姩瀹屾垚 : OperateTypeEnum.浜哄伐瀹屾垚);
+                _stockRepository.Db.Deleteable(stockInfo).ExecuteCommand();
 
                 _locationStatusChangeRecordService.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Outbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
 
@@ -383,5 +424,149 @@
             }
         }
 
+
+
+
+        /// <summary>
+        /// 鍥炲簱瀹屾垚鍥炶皟 
+        public async Task<WebResponseContent> BackToStockComplete(Dt_Task task)
+        {
+            try
+            {
+                _unitOfWorkManage.BeginTran();
+
+                // 鑾峰彇鐩稿叧鐨勫嚭搴撻攣瀹氫俊鎭�
+                var lockInfos = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                    .Where(x => x.TaskNum == task.TaskNum &&
+                               x.Status == (int)OutLockStockStatusEnum.鍥炲簱涓�)
+                    .ToListAsync();
+
+                if (!lockInfos.Any())
+                    return WebResponseContent.Instance.Error("鏈壘鍒板洖搴撲腑鐨勯攣瀹氫俊鎭�");
+
+                //  鎭㈠搴撳瓨鍑哄簱鏁伴噺锛堝洖搴撶殑閮ㄥ垎锛�
+                await RestoreStockOutboundQuantity(lockInfos);
+
+                //  鏇存柊鍑哄簱鍗曟槑缁嗙殑閿佸畾鏁伴噺
+                var orderDetailGroups = lockInfos.GroupBy(x => x.OrderDetailId);
+
+                foreach (var group in orderDetailGroups)
+                {
+                    var orderDetailId = group.Key;
+                    var totalUnpicked = group.Sum(x => x.AssignQuantity - x.PickedQty);
+
+                    if (totalUnpicked > 0)
+                    {
+                        var orderDetail = await _outboundOrderService.Db.Queryable<Dt_OutboundOrderDetail>()
+                            .Where(x => x.Id == orderDetailId)
+                            .FirstAsync();
+                        orderDetail.LockQuantity -= totalUnpicked;
+
+                        // 鎭㈠鐘舵��
+                        if (orderDetail.LockQuantity <= 0 && orderDetail.OverOutQuantity <= 0)
+                        {
+                            orderDetail.OrderDetailStatus = (int)OrderDetailStatusEnum.New;
+                        }
+                        else if (orderDetail.OverOutQuantity > 0)
+                        {
+                            orderDetail.OrderDetailStatus = (int)OrderDetailStatusEnum.AssignOverPartial;
+                        }
+
+                        await _outboundOrderService.Db.Updateable(orderDetail).ExecuteCommandAsync();
+                    }
+                }
+
+                //  鏇存柊閿佸畾淇℃伅鐘舵�佷负宸插洖搴�
+                foreach (var lockInfo in lockInfos)
+                {
+                    lockInfo.Status = (int)OutLockStockStatusEnum.宸插洖搴�;
+                }
+                await _outStockLockInfoService.Db.Updateable(lockInfos).ExecuteCommandAsync();
+
+                // 6. 鏇存柊搴撳瓨鐘舵��
+                var stockIds = lockInfos.Select(x => x.StockId).Distinct().ToList();
+                var stocks = await _stockService.StockInfoService.Db.Queryable<Dt_StockInfo>()
+                    .Where(x => stockIds.Contains(x.Id))
+                    .ToListAsync();
+
+                foreach (var stock in stocks)
+                {
+                    stock.StockStatus = (int)StockStatusEmun.鍏ュ簱瀹屾垚;
+                    stock.LocationCode = task.TargetAddress; // 鏇存柊璐т綅
+                }
+                await _stockService.StockInfoService.Db.Updateable(stocks).ExecuteCommandAsync();
+
+                // 7. 鏇存柊璐т綅鐘舵��
+                var location = await _locationInfoService.Db.Queryable<Dt_LocationInfo>()
+                    .Where(x => x.LocationCode == task.TargetAddress)
+                    .FirstAsync();
+                if (location != null)
+                {
+                    location.LocationStatus = (int)LocationStatusEnum.InStock;
+                    await _locationInfoService.Db.Updateable(location).ExecuteCommandAsync();
+                }
+
+                //  鏇存柊浠诲姟鐘舵�佷负宸插畬鎴�
+                task.TaskStatus = (int)TaskStatusEnum.Finish;
+
+                await Db.Updateable(task).ExecuteCommandAsync();
+
+                _unitOfWorkManage.CommitTran();
+
+                return WebResponseContent.Instance.OK("鍥炲簱瀹屾垚", new
+                {
+                    TaskNum = task.TaskNum,
+                    PalletCode = task.PalletCode,
+                    RestoredQuantity = lockInfos.Sum(x => x.AssignQuantity - x.PickedQty)
+                });
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                return WebResponseContent.Instance.Error($"鍥炲簱瀹屾垚澶勭悊澶辫触: {ex.Message}");
+            }
+        }
+
+        /// <summary>
+        /// 鎭㈠搴撳瓨鍑哄簱鏁伴噺锛堝洖搴撶殑閮ㄥ垎锛�
+        /// </summary>
+        private async Task RestoreStockOutboundQuantity(List<Dt_OutStockLockInfo> lockInfos)
+        {
+            // 鎸夊簱瀛業D鍜岀墿鏂欏垎缁�
+            var stockGroups = lockInfos.GroupBy(x => new { x.StockId, x.MaterielCode });
+
+            foreach (var group in stockGroups)
+            {
+                var stockId = group.Key.StockId;
+                var materielCode = group.Key.MaterielCode;
+                var totalUnpicked = group.Sum(x => x.AssignQuantity - x.PickedQty);
+
+                if (totalUnpicked <= 0) continue;
+
+                // 鑾峰彇璇ョ墿鏂欏湪搴撳瓨涓殑鎵�鏈夋潯鐮�
+                var stockDetails = await _stockService.StockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+                    .Where(x => x.StockId == stockId && x.MaterielCode == materielCode)
+                    .ToListAsync();
+
+                if (!stockDetails.Any()) continue;
+
+                // 鎸夊嚭搴撴暟閲忕殑姣斾緥鍒嗛厤鎭㈠鏁伴噺
+                var totalOutbound = stockDetails.Sum(x => x.OutboundQuantity);
+
+                if (totalOutbound <= 0) continue;
+
+                foreach (var detail in stockDetails)
+                {
+                    if (detail.OutboundQuantity <= 0) continue;
+
+                    decimal ratio = detail.OutboundQuantity / totalOutbound;
+                    decimal restoreAmount = Math.Min(detail.OutboundQuantity, totalUnpicked * ratio);
+
+                    detail.OutboundQuantity -= restoreAmount;
+                    await _stockService.StockInfoDetailService.Db.Updateable(detail).ExecuteCommandAsync();
+                }
+            }
+        }
+
     }
 }

--
Gitblit v1.9.3