From c4d89e32e105c9618e18618d97442b30b68c5f77 Mon Sep 17 00:00:00 2001
From: 647556386 <647556386@qq.com>
Date: 星期日, 30 十一月 2025 09:56:39 +0800
Subject: [PATCH] 虚拟出入库功能

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs |  136 ++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 127 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 f8217d5..8682865 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"
@@ -38,9 +38,11 @@
 using WIDESEA_Core.BaseServices;
 using WIDESEA_Core.Enums;
 using WIDESEA_Core.Helper;
+using WIDESEA_DTO.Allocate;
 using WIDESEA_DTO.Basic;
 using WIDESEA_DTO.Inbound;
 using WIDESEA_DTO.Task;
+using WIDESEA_IAllocateService;
 using WIDESEA_IBasicService;
 using WIDESEA_IInboundService;
 using WIDESEA_IOutboundService;
@@ -69,7 +71,7 @@
         private readonly IESSApiService _eSSApiService;
         private readonly IStockService _stockService;
         private readonly IRecordService _recordService;
-
+        private readonly IAllocateService _allocateService;
         private readonly IInvokeMESService _invokeMESService;
         public IRepository<Dt_Task> Repository => BaseDal;
 
@@ -90,7 +92,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) : 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) : base(BaseDal)
         {
             _mapper = mapper;
             _unitOfWorkManage = unitOfWorkManage;
@@ -107,6 +109,7 @@
             _outboundOrderDetailService = outboundOrderDetailService;
             _invokeMESService = invokeMESService;
             _outStockLockInfoService = outStockLockInfoService;
+            _allocateService = allocateService;
         }
 
 
@@ -250,7 +253,53 @@
                 {
                     if (inboundOrder.OrderType == InOrderTypeEnum.Allocat.ObjToInt())//璋冩嫧鍏ュ簱
                     {
+                        if (inboundOrder != null && inboundOrder.OrderStatus == InOrderStatusEnum.鍏ュ簱瀹屾垚.ObjToInt())
+                        {
+                            var allocate = _allocateService.Repository.QueryData(x => x.OrderNo == inboundOrder.InboundOrderNo).First();
+                            var feedmodel = new AllocateDto
+                            {
+                                ReqCode = Guid.NewGuid().ToString(),
+                                ReqTime = DateTime.Now.ToString(),
+                                BusinessType = "2",
+                                FactoryArea = inboundOrder.FactoryArea,
+                                OperationType = 1,
+                                Operator = inboundOrder.Operator,
+                                OrderNo = inboundOrder.UpperOrderNo,
+                                fromWarehouse = allocate?.FromWarehouse??"",
+                                toWarehouse = allocate?.ToWarehouse??"",
+                                Details = new List<AllocateDtoDetail>()
 
+                            };
+
+                            var groupedData = inboundOrder.Details.GroupBy(item => new { item.MaterielCode, item.SupplyCode, item.BatchNo, item.lineNo, item.BarcodeUnit, item.WarehouseCode })
+                               .Select(group => new AllocateDtoDetail
+                               {
+                                   MaterialCode = group.Key.MaterielCode,
+                                   LineNo = group.Key.lineNo,
+                                   WarehouseCode = group.Key.WarehouseCode,
+                                   Qty = group.Sum(x => x.BarcodeQty),
+                                   // warehouseCode= "1072",
+                                   Unit = group.Key.BarcodeUnit,
+                                   Barcodes = group.Select(row => new BarcodeInfo
+                                   {
+                                       Barcode = row.Barcode,
+                                       Qty = row.BarcodeQty,
+                                       BatchNo = row.BatchNo,
+                                       SupplyCode = row.SupplyCode,
+                                       Unit = row.Unit
+                                   }).ToList()
+                               }).ToList();
+                            feedmodel.Details = groupedData;
+
+                            var result = await _invokeMESService.FeedbackAllocate(feedmodel);
+                            if (result != null && result.code == 200)
+                            {
+                                _inboundOrderService.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 1 })
+                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
+                                _inboundOrderDetailService.Db.Updateable<Dt_InboundOrderDetail>().SetColumns(it => new Dt_InboundOrderDetail { ReturnToMESStatus = 1 })
+                                .Where(it => it.OrderId == inboundOrder.Id).ExecuteCommand();
+                            }
+                        }
                     }
                     else if (inboundOrder.OrderType == InOrderTypeEnum.ReCheck.ObjToInt()) //閲嶆鍏ュ簱
                     {
@@ -293,11 +342,11 @@
                                }).ToList();
                             feedmodel.details = groupedData;
 
-                            var result= await _invokeMESService.FeedbackInbound(feedmodel);
+                            var result = await _invokeMESService.FeedbackInbound(feedmodel);
                             if (result != null && result.code == 200)
                             {
                                 _inboundOrderService.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 1 })
-                                .Where(it => it.Id== inboundOrder.Id).ExecuteCommand();
+                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
                                 _inboundOrderDetailService.Db.Updateable<Dt_InboundOrderDetail>().SetColumns(it => new Dt_InboundOrderDetail { ReturnToMESStatus = 1 })
                                 .Where(it => it.OrderId == inboundOrder.Id).ExecuteCommand();
                             }
@@ -329,7 +378,7 @@
             var outloks = _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>().Where(x => x.TaskNum == task.TaskNum).ToList();
 
             var stockids = outloks.Select(x => x.StockId).ToList();
-            
+
             _stockService.StockInfoService.Db.Updateable<Dt_StockInfo>()
                                   .SetColumns(it => new Dt_StockInfo
                                   {
@@ -343,9 +392,9 @@
                                   {
                                       Status = StockStatusEmun.鍑哄簱閿佸畾.ObjToInt()
                                   })
-                                  .Where(it => stockids.Contains( it.StockId))
+                                  .Where(it => stockids.Contains(it.StockId))
                                   .ExecuteCommand();
- 
+
 
 
             return WebResponseContent.Instance.OK();
@@ -405,7 +454,7 @@
             }
         }
 
-        public WebResponseContent InPickTaskCompleted(Dt_Task task)
+        public async Task<WebResponseContent> InPickTaskCompleted(Dt_Task task)
         {
             _logger.LogInformation($"TaskService  InPickTaskCompleted: {task.TaskNum}");
             //鏌ュ簱瀛�
@@ -418,13 +467,25 @@
             {
                 return WebResponseContent.Instance.Error($"鏈壘鍒拌鎵樼洏搴撳瓨鏄庣粏淇℃伅");
             }
-
             //鏌ヨ揣浣�
             Dt_LocationInfo locationInfo = _locationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
             if (locationInfo == null)
             {
                 return WebResponseContent.Instance.Error($"鏈壘鍒板搴旂殑缁堢偣璐т綅淇℃伅");
             }
+            // 鑾峰彇鎵�鏈夊洖搴撲腑鐨勫嚭搴撻攣瀹氳褰�
+            var returnLocks = _outStockLockInfoService.Db.Queryable<Dt_OutStockLockInfo>()
+                .Where(it => it.OrderNo == task.OrderNo && it.PalletCode == task.PalletCode && it.Status == (int)OutLockStockStatusEnum.鍥炲簱涓�)
+                .ToList();
+            // 鏇存柊鍑哄簱閿佸畾璁板綍鐘舵�佷负鍥炲簱瀹屾垚
+            foreach (var lockInfo in returnLocks)
+            {
+                lockInfo.Status = (int)OutLockStockStatusEnum.宸插洖搴�;
+            }
+            _outStockLockInfoService.Db.Updateable(returnLocks).ExecuteCommand();
+
+            await DeleteZeroQuantityStockDetails(stockInfo.Id);
+
             stockInfo.LocationCode = task.TargetAddress;
             stockInfo.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt();
             stockInfo.Details.ForEach(x =>
@@ -433,6 +494,9 @@
             });
             _stockService.StockInfoService.Repository.UpdateData(stockInfo);
             _stockService.StockInfoDetailService.Repository.UpdateData(stockInfo.Details);
+            await ProcessStockDetailsForReturn(task, stockInfo.Id);
+
+    
 
             if (stockInfo.PalletType == PalletTypeEnum.Empty.ObjToInt())
             {
@@ -451,6 +515,60 @@
             return WebResponseContent.Instance.OK();
         }
 
+        /// <summary>
+        /// 鍒犻櫎搴撳瓨鏁颁负0鐨勫簱瀛樻槑缁嗚褰�
+        /// </summary>
+        private async Task DeleteZeroQuantityStockDetails(int stockId)
+        {
+            try
+            {
+                // 鍒犻櫎搴撳瓨鏁伴噺涓�0鐨勮褰�
+                var deleteCount = await _stockService.StockInfoDetailService.Db.Deleteable<Dt_StockInfoDetail>()
+                    .Where(x => x.StockId == stockId &&
+                               x.StockQuantity == 0 &&
+                               (x.Status==StockStatusEmun.鍑哄簱瀹屾垚.ObjToInt()|| x.Status==
+                                          StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt())) // 鍙垹闄ゅ凡瀹屾垚鐘舵�佺殑闆跺簱瀛�
+                    .ExecuteCommandAsync();
+
+                if (deleteCount > 0)
+                {
+                    _logger.LogInformation($"鍒犻櫎{deleteCount}鏉¢浂搴撳瓨鏄庣粏璁板綍 - StockId: {stockId}");
+                }
+            }
+            catch (Exception ex)
+            {
+                _logger.LogWarning($"鍒犻櫎闆跺簱瀛樿褰曞け璐� - StockId: {stockId}, Error: {ex.Message}");
+                // 娉ㄦ剰锛氬垹闄ゅけ璐ヤ笉搴旇褰卞搷涓绘祦绋嬶紝璁板綍鏃ュ織鍚庣户缁�
+            }
+        }
+        /// <summary>
+        /// 澶勭悊鍥炲簱鐩稿叧鐨勬墍鏈夊簱瀛樻槑缁嗙姸鎬佸彉鏇�
+        /// </summary>
+        private async Task ProcessStockDetailsForReturn(Dt_Task returnTask, int stockId)
+        {
+            // 鑾峰彇璇ユ墭鐩樹笅鎵�鏈夐渶瑕佸洖搴撶殑搴撳瓨鏄庣粏
+            var stockDetails = await _stockService.StockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
+                .Where(x => x.StockId == stockId &&
+                           x.StockQuantity > 0 &&
+                          ( x.Status == StockStatusEmun.鍑哄簱閿佸畾.ObjToInt()|| x.Status==
+                                      StockStatusEmun.鍏ュ簱纭.ObjToInt()))  // 鍖呮嫭鍑哄簱閿佸畾鍜屽叆搴撶‘璁ょ殑
+                .ToListAsync();
+
+            foreach (var detail in stockDetails)
+            {
+          
+                detail.Status = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt();              
+                detail.OutboundQuantity = 0;  // 娓呯┖鍑哄簱鏁伴噺       
+
+                _logger.LogInformation($"鏇存柊搴撳瓨鏄庣粏鐘舵�� - 鏉$爜: {detail.Barcode}, 鏁伴噺: {detail.StockQuantity}");
+            }
+
+            if (stockDetails.Any())
+            {
+                await _stockService.StockInfoDetailService.Db.Updateable(stockDetails).ExecuteCommandAsync();
+                _logger.LogInformation($"鍏辨洿鏂皗stockDetails.Count}涓簱瀛樻槑缁嗙姸鎬佷负鍏ュ簱瀹屾垚");
+            }
+        }
         public async Task<WebResponseContent> OutEmptyTaskCompleted(Dt_Task task)
         {
             WebResponseContent content = new WebResponseContent();

--
Gitblit v1.9.3