From 8639f19c82f6e263654db44286256bb8d028d2c2 Mon Sep 17 00:00:00 2001
From: 647556386 <647556386@qq.com>
Date: 星期日, 30 十一月 2025 10:10:40 +0800
Subject: [PATCH] 1

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs |  254 ++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 183 insertions(+), 71 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 6a804a7..141197b 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"
@@ -51,6 +51,7 @@
 using WIDESEA_IStockService;
 using WIDESEA_ITaskInfoService;
 using WIDESEA_Model.Models;
+using WIDESEA_Model.Models.Outbound;
 
 namespace WIDESEA_TaskInfoService
 {
@@ -65,6 +66,7 @@
         private readonly IInboundOrderService _inboundOrderService;
         private readonly IInboundOrderDetailService _inboundOrderDetailService;
 
+        private readonly IRepository<Dt_OutboundBatch> _OutboundBatchRepository;
         private readonly IOutboundOrderService _outboundOrderService;
         private readonly IOutboundOrderDetailService _outboundOrderDetailService;
         private readonly IOutStockLockInfoService _outStockLockInfoService;
@@ -93,7 +95,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, IOutStockLockInfoService outStockLockInfoService, IAllocateService allocateService) : 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, IAllocateService allocateService, IRepository<Dt_OutboundBatch> outboundBatchRepository) : base(BaseDal)
         {
             _mapper = mapper;
             _unitOfWorkManage = unitOfWorkManage;
@@ -111,6 +113,7 @@
             _invokeMESService = invokeMESService;
             _outStockLockInfoService = outStockLockInfoService;
             _allocateService = allocateService;
+            _OutboundBatchRepository = outboundBatchRepository;
         }
 
 
@@ -127,7 +130,7 @@
 
                 if (int.TryParse(taskNum, out var newTaskNum))
                 {
-                    task =await BaseDal.QueryFirstAsync(x => x.TaskNum == newTaskNum);
+                    task = await BaseDal.QueryFirstAsync(x => x.TaskNum == newTaskNum);
                     if (task == null)
                     {
                         return WebResponseContent.Instance.Error("鏈壘鍒颁换鍔′俊鎭�");
@@ -154,12 +157,12 @@
                                 return responseContent;
                             }
                         }
-                        catch (AggregateException ex) 
+                        catch (AggregateException ex)
                         {
                             _logger.LogError($"TaskService TaskCompleted  taskResult:   {ex.Message} ");
                             return WebResponseContent.Instance.Error(ex.Message);
                         }
-                        catch(Exception ex)
+                        catch (Exception ex)
                         {
                             _logger.LogError(ex, $"Unexpected error in {task.TaskType}");
                             return WebResponseContent.Instance.Error(ex.Message);
@@ -270,12 +273,12 @@
             {
                 foreach (var inboundOrder in inboundOrders)
                 {
-                    if (inboundOrder.OrderType == InOrderTypeEnum.Allocat.ObjToInt())//璋冩嫧鍏ュ簱
+                    if (inboundOrder.OrderType == InOrderTypeEnum.AllocatInbound.ObjToInt())//璋冩嫧鍏ュ簱
                     {
                         if (inboundOrder != null && inboundOrder.OrderStatus == InOrderStatusEnum.鍏ュ簱瀹屾垚.ObjToInt())
                         {
                             var allocate = _allocateService.Repository.QueryData(x => x.OrderNo == inboundOrder.InboundOrderNo).First();
-                            var feedmodel = new AllocateDto
+                            var allocatefeedmodel = new AllocateDto
                             {
                                 ReqCode = Guid.NewGuid().ToString(),
                                 ReqTime = DateTime.Now.ToString(),
@@ -308,9 +311,9 @@
                                        Unit = row.Unit
                                    }).ToList()
                                }).ToList();
-                            feedmodel.Details = groupedData;
+                            allocatefeedmodel.Details = groupedData;
 
-                            var result = await _invokeMESService.FeedbackAllocate(feedmodel);
+                            var result = await _invokeMESService.FeedbackAllocate(allocatefeedmodel);
                             if (result != null && result.code == 200)
                             {
                                 _inboundOrderService.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 1 })
@@ -382,6 +385,13 @@
             return WebResponseContent.Instance.OK();
         }
 
+
+        public async Task<WebResponseContent> OutAllocateTaskCompleted(Dt_Task task)
+        {
+            _logger.LogInformation($"TaskService  OutAllocateTaskCompleted: {task.TaskNum}");
+
+           return  await OutboundTaskCompleted(task);          
+        }
         public async Task<WebResponseContent> OutboundTaskCompleted(Dt_Task task)
         {
             _logger.LogInformation($"TaskService  OutboundTaskCompleted: {task.TaskNum}");
@@ -394,7 +404,7 @@
             locationInfo.LocationStatus = LocationStatusEnum.Free.ObjToInt();
             _locationInfoService.Repository.UpdateData(locationInfo);
 
-            var outloks =await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>().Where(x => x.TaskNum == task.TaskNum).ToListAsync();
+            var outloks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>().Where(x => x.TaskNum == task.TaskNum).ToListAsync();
 
             var stockids = outloks.Select(x => x.StockId).ToList();
 
@@ -489,7 +499,7 @@
             try
             {
                 //鏌ュ簱瀛�
-                Dt_StockInfo stockInfo =await _stockRepository.Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.PalletCode == task.PalletCode).FirstAsync();
+                Dt_StockInfo stockInfo = await _stockRepository.Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.PalletCode == task.PalletCode).FirstAsync();
                 if (stockInfo == null)
                 {
                     _logger.LogInformation($"TaskService  InPickTaskCompleted: 鏈壘鍒版墭鐩樺搴旂殑缁勭洏淇℃伅.{task.TaskNum}");
@@ -510,7 +520,7 @@
 
                 var beforelocationStatus = locationInfo.LocationStatus;
                 // 鑾峰彇鎵�鏈夊洖搴撲腑鐨勫嚭搴撻攣瀹氳褰�
-                var returnLocks =await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                var returnLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
                     .Where(it => it.OrderNo == task.OrderNo && it.PalletCode == task.PalletCode && it.Status == (int)OutLockStockStatusEnum.鍥炲簱涓�)
                     .ToListAsync();
                 // 鏇存柊鍑哄簱閿佸畾璁板綍鐘舵�佷负鍥炲簱瀹屾垚
@@ -518,7 +528,7 @@
                 {
                     lockInfo.Status = (int)OutLockStockStatusEnum.宸插洖搴�;
                 }
-                _outStockLockInfoService.Db.Updateable(returnLocks).ExecuteCommand();                             
+                _outStockLockInfoService.Db.Updateable(returnLocks).ExecuteCommand();
 
                 stockInfo.LocationCode = task.TargetAddress;
                 stockInfo.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt();
@@ -532,7 +542,7 @@
                 }
 
                 _stockService.StockInfoService.Repository.UpdateData(stockInfo);
-            
+
                 await ProcessStockDetailsForReturn(task, stockInfo.Id);
 
                 await DeleteZeroQuantityStockDetails(stockInfo.Id);
@@ -572,70 +582,172 @@
 
         private async Task HandleOutboundOrderToMESCompletion(Dt_OutboundOrder outboundOrder, string orderNo)
         {
-            // 鑾峰彇璁㈠崟鏄庣粏鏁版嵁
-            var orderDetails = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
-                .LeftJoin<Dt_OutboundOrder>((d, o) => d.OrderId == o.Id)
-                .Where((d, o) => o.OrderNo == orderNo)
-                .Select((d, o) => d)
-                .ToListAsync();
-
-            var feedmodel = new FeedbackOutboundRequestModel
+            try
             {
-                reqCode = Guid.NewGuid().ToString(),
-                reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
-                business_type = outboundOrder.BusinessType,
-                factoryArea = outboundOrder.FactoryArea,
-                operationType = 1,
-                Operator = App.User.UserName,
-                orderNo = outboundOrder.UpperOrderNo,
-                documentsNO = outboundOrder.OrderNo,
-                status = outboundOrder.OrderStatus,
-                details = new List<FeedbackOutboundDetailsModel>()
-            };
-
-            // 浣跨敤璁㈠崟鏄庣粏鐨凮verOutQuantity浣滀负鍥炰紶鏁伴噺
-            foreach (var detail in orderDetails)
-            {
-                // 鑾峰彇璇ユ槑缁嗗搴旂殑鏉$爜淇℃伅锛堜粠閿佸畾璁板綍锛�
-                var detailLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
-                    .Where(x => x.OrderNo == orderNo &&
-                               x.OrderDetailId == detail.Id &&
-                               x.Status == (int)OutLockStockStatusEnum.鎷i�夊畬鎴�)
+                var orderDetails = await _outboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
+                    .LeftJoin<Dt_OutboundOrder>((o, item) => o.OrderId == item.Id)
+                    .Where((o, item) => item.OrderNo == orderNo)
+                    .Select((o, item) => o)
                     .ToListAsync();
 
-                var detailModel = new FeedbackOutboundDetailsModel
+                bool allCompleted = true;
+                foreach (var detail in orderDetails)
                 {
-                    materialCode = detail.MaterielCode,
-                    lineNo = detail.lineNo, // 娉ㄦ剰锛氳繖閲屽彲鑳介渶瑕佽皟鏁村瓧娈靛悕
-                    warehouseCode = detail.WarehouseCode,
-                    qty = detail.OverOutQuantity, // 浣跨敤璁㈠崟鏄庣粏鐨勫凡鍑哄簱鏁伴噺
-                    currentDeliveryQty = detail.OverOutQuantity,
-                    unit = detail.Unit,
-                    barcodes = detailLocks.Select(lockInfo => new WIDESEA_DTO.Outbound.BarcodesModel
+                    if (detail.OverOutQuantity < detail.NeedOutQuantity)
                     {
-                        barcode = lockInfo.CurrentBarcode,
-                        supplyCode = lockInfo.SupplyCode,
-                        batchNo = lockInfo.BatchNo,
-                        unit = lockInfo.Unit,
-                        qty = lockInfo.PickedQty // 鏉$爜绾у埆鐨勬暟閲忎粛鐢ㄩ攣瀹氳褰�
-                    }).ToList()
-                };
+                        allCompleted = false;
+                        break;
+                    }
+                }
+                _logger.LogInformation($"TaskService  HandleOutboundOrderToMESCompletion: {outboundOrder.OrderNo} , {allCompleted}");
+                int newStatus = allCompleted ? (int)OutOrderStatusEnum.鍑哄簱瀹屾垚 : (int)OutOrderStatusEnum.鍑哄簱涓�;
 
-                feedmodel.details.Add(detailModel);
+                if (outboundOrder.OrderStatus != newStatus)
+                {
+                    await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
+                        .SetColumns(x => x.OrderStatus == newStatus)
+                        .Where(x => x.OrderNo == orderNo)
+                        .ExecuteCommandAsync();
+
+                }
+                //鍙湁姝e父鍒嗘嫞瀹屾垚鏃舵墠鍚慚ES鍙嶉
+                if (allCompleted && newStatus == (int)OutOrderStatusEnum.鍑哄簱瀹屾垚)
+                {
+
+                    if (outboundOrder.OrderType == OutOrderTypeEnum.Allocate.ObjToInt())
+                    {
+                        var allocate = _allocateService.Repository.QueryData(x => x.UpperOrderNo == outboundOrder.UpperOrderNo).First();
+                        var allocatefeedmodel = new AllocateDto
+                        {
+                            ReqCode = Guid.NewGuid().ToString(),
+                            ReqTime = DateTime.Now.ToString(),
+                            BusinessType = "3",
+                            FactoryArea = outboundOrder.FactoryArea,
+                            OperationType = 1,
+                            Operator = outboundOrder.Operator,
+                            OrderNo = outboundOrder.UpperOrderNo,
+                            // documentsNO = outboundOrder.OrderNo,
+                            // status = outboundOrder.OrderStatus,
+                            fromWarehouse = allocate?.FromWarehouse ?? "",
+                            toWarehouse = allocate?.ToWarehouse ?? "",
+                            Details = new List<AllocateDtoDetail>()
+
+                        };
+                        foreach (var detail in orderDetails)
+                        {
+                            // 鑾峰彇璇ユ槑缁嗗搴旂殑鏉$爜淇℃伅锛堜粠閿佸畾璁板綍锛�
+                            var detailLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                                .Where(x => x.OrderNo == orderNo &&
+                                           x.OrderDetailId == detail.Id &&
+                                           x.Status == (int)OutLockStockStatusEnum.鎷i�夊畬鎴�)
+                                .ToListAsync();
+
+                            var detailModel = new AllocateDtoDetail
+                            {
+                                MaterialCode = detail.MaterielCode,
+                                LineNo = detail.lineNo, // 娉ㄦ剰锛氳繖閲屽彲鑳介渶瑕佽皟鏁村瓧娈靛悕
+                                WarehouseCode = detail.WarehouseCode,
+                                Qty = detail.OverOutQuantity, // 浣跨敤璁㈠崟鏄庣粏鐨勫凡鍑哄簱鏁伴噺
+                                //currentDeliveryQty = detail.OverOutQuantity,
+                                Unit = detail.Unit,
+                                Barcodes = detailLocks.Select(lockInfo => new BarcodeInfo
+                                {
+                                    Barcode = lockInfo.CurrentBarcode,
+                                    SupplyCode = lockInfo.SupplyCode,
+                                    BatchNo = lockInfo.BatchNo,
+                                    Unit = lockInfo.Unit,
+                                    Qty = lockInfo.PickedQty // 鏉$爜绾у埆鐨勬暟閲忎粛鐢ㄩ攣瀹氳褰�
+                                }).ToList()
+                            };
+
+                            allocatefeedmodel.Details.Add(detailModel);
+                        }
+
+                        var result = await _invokeMESService.FeedbackAllocate(allocatefeedmodel);
+                        if (result != null && result.code == 200)
+                        {
+                            await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
+                                   .SetColumns(x => x.ReturnToMESStatus == 1)
+                                   .Where(x => x.OrderId == outboundOrder.Id).ExecuteCommandAsync();
+
+                            await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
+                                  .SetColumns(x => new Dt_OutboundOrder
+                                  {
+                                      ReturnToMESStatus = 1,
+                                      Operator = App.User.UserName,
+                                  }).Where(x => x.OrderNo == orderNo).ExecuteCommandAsync();
+                        }
+                    }
+                    else if (outboundOrder.OrderType == OutOrderTypeEnum.ReCheck.ObjToInt())
+                    {
+
+                    }
+                    else
+                    {
+                        var feedmodel = new FeedbackOutboundRequestModel
+                        {
+                            reqCode = Guid.NewGuid().ToString(),
+                            reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
+                            business_type = outboundOrder.BusinessType,
+                            factoryArea = outboundOrder.FactoryArea,
+                            operationType = 1,
+                            Operator = outboundOrder.Operator,
+                            orderNo = outboundOrder.UpperOrderNo,
+                            documentsNO = outboundOrder.OrderNo,
+                            status = outboundOrder.OrderStatus,
+                            details = new List<FeedbackOutboundDetailsModel>()
+                        };
+
+
+                        foreach (var detail in orderDetails)
+                        {
+                            // 鑾峰彇璇ユ槑缁嗗搴旂殑鏉$爜淇℃伅锛堜粠閿佸畾璁板綍锛�
+                            var detailLocks = await _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                                .Where(x => x.OrderNo == orderNo &&
+                                           x.OrderDetailId == detail.Id &&
+                                           x.Status == (int)OutLockStockStatusEnum.鎷i�夊畬鎴�)
+                                .ToListAsync();
+
+                            var detailModel = new FeedbackOutboundDetailsModel
+                            {
+                                materialCode = detail.MaterielCode,
+                                lineNo = detail.lineNo, // 娉ㄦ剰锛氳繖閲屽彲鑳介渶瑕佽皟鏁村瓧娈靛悕
+                                warehouseCode = detail.WarehouseCode,
+                                qty = detail.OverOutQuantity, // 浣跨敤璁㈠崟鏄庣粏鐨勫凡鍑哄簱鏁伴噺
+                                currentDeliveryQty = detail.OverOutQuantity,
+                                unit = detail.Unit,
+                                barcodes = detailLocks.Select(lockInfo => new WIDESEA_DTO.Outbound.BarcodesModel
+                                {
+                                    barcode = lockInfo.CurrentBarcode,
+                                    supplyCode = lockInfo.SupplyCode,
+                                    batchNo = lockInfo.BatchNo,
+                                    unit = lockInfo.Unit,
+                                    qty = lockInfo.PickedQty // 鏉$爜绾у埆鐨勬暟閲忎粛鐢ㄩ攣瀹氳褰�
+                                }).ToList()
+                            };
+
+                            feedmodel.details.Add(detailModel);
+                        }
+
+                        var result = await _invokeMESService.FeedbackOutbound(feedmodel);
+                        if (result != null && result.code == 200)
+                        {
+                            await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
+                                .SetColumns(x => x.ReturnToMESStatus == 1)
+                                .Where(x => x.OrderId == outboundOrder.Id)
+                                .ExecuteCommandAsync();
+
+                            await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
+                                .SetColumns(x => x.ReturnToMESStatus == 1)
+                                .Where(x => x.OrderNo == orderNo)
+                                .ExecuteCommandAsync();
+                        }
+                    }
+                }
             }
-
-            var result = await _invokeMESService.FeedbackOutbound(feedmodel);
-            if (result != null && result.code == 200)
+            catch (Exception ex)
             {
-                await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
-                    .SetColumns(x => x.ReturnToMESStatus == 1)
-                    .Where(x => x.OrderId == outboundOrder.Id)
-                    .ExecuteCommandAsync();
-
-                await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
-                    .SetColumns(x => x.ReturnToMESStatus == 1)
-                    .Where(x => x.OrderNo == orderNo)
-                    .ExecuteCommandAsync();
+                _logger.LogError($"CheckAndUpdateOrderStatus澶辫触 - OrderNo: {orderNo}, Error: {ex.Message}");
             }
 
         }
@@ -722,8 +834,8 @@
                 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();
-
+                //_stockRepository.Db.Deleteable(stockInfo).ExecuteCommand();
+                _stockService.StockInfoService.DeleteData(stockInfo);
                 _locationStatusChangeRecordService.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Outbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
 
                 return await Task.FromResult(WebResponseContent.Instance.OK());

--
Gitblit v1.9.3