From 97073e9e6d03114221436ee0aa1e143d6e2c4a09 Mon Sep 17 00:00:00 2001
From: wankeda <Administrator@DESKTOP-HAU3ST3>
Date: 星期一, 04 八月 2025 13:07:39 +0800
Subject: [PATCH] 出入库代码逻辑优化

---
 WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs |  549 +++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 356 insertions(+), 193 deletions(-)

diff --git a/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs b/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
index 77ca387..3310331 100644
--- a/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
+++ b/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
@@ -19,6 +19,7 @@
 using MailKit.Search;
 using Microsoft.AspNetCore.SignalR;
 using Newtonsoft.Json;
+using OfficeOpenXml.FormulaParsing.Excel.Functions.Database;
 using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
 using Org.BouncyCastle.Asn1.Tsp;
 using SqlSugar;
@@ -71,6 +72,7 @@
         private readonly IUnitOfWorkManage _unitOfWorkManage;
         private readonly IStockRepository _stockRepository;
         private readonly IBasicService _basicService;
+        private readonly IBasicRepository _basicRepository;
         private readonly IOutboundService _outboundService;
         private readonly IInboundService _inboundService;
         private readonly IInboundOrderDetailService _inboundOrderDetailService;
@@ -86,9 +88,10 @@
         private readonly IOutboundOrderDetail_HtyService _outboundOrderDetail_HtyService;
         private readonly IPalletTypeInfoRepository _palletTypeInfoRepository;
         private readonly IOutboundOrderDetailRepository _outboundOrderDetailRepository;
+        private readonly IStockInfoDetailRepository _stockInfoDetailRepository;
         public ITaskRepository Repository => BaseDal;
 
-        public TaskService(ITaskRepository BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IMaterielInfoService materielInfoService, IInboundOrderDetail_HtyService inboundOrderDetail_HtyService, IOutboundOrder_HtyService outboundOrder_HtyService, IOutboundOrderDetail_HtyService outboundOrderDetail_HtyService, IInboundOrder_HtyService inboundOrder_HtyService, IStockRepository stockRepository, IInboundOrderDetailService inboundOrderDetailService, IBasicService basicService, IOutboundService outboundService, IInboundService inboundService, IRecordService recordService, IStockService stockService, ITask_HtyService taskHtyService, ILocationInfoService locationInfoService, IOutboundOrderDetailRepository outboundOrderDetailRepository) : base(BaseDal)
+        public TaskService(ITaskRepository BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IMaterielInfoService materielInfoService, IInboundOrderDetail_HtyService inboundOrderDetail_HtyService, IOutboundOrder_HtyService outboundOrder_HtyService, IOutboundOrderDetail_HtyService outboundOrderDetail_HtyService, IInboundOrder_HtyService inboundOrder_HtyService, IStockRepository stockRepository, IInboundOrderDetailService inboundOrderDetailService, IBasicService basicService, IOutboundService outboundService, IInboundService inboundService, IRecordService recordService, IStockService stockService, ITask_HtyService taskHtyService, ILocationInfoService locationInfoService, IOutboundOrderDetailRepository outboundOrderDetailRepository, IBasicRepository basicRepository, IStockInfoDetailRepository stockInfoDetailRepository, IPalletTypeInfoRepository palletTypeInfoRepository) : base(BaseDal)
         {
             _mapper = mapper;
             _stockRepository = stockRepository;
@@ -107,6 +110,9 @@
             _outboundOrder_HtyService = outboundOrder_HtyService;
             _outboundOrderDetail_HtyService = outboundOrderDetail_HtyService;
             _outboundOrderDetailRepository = outboundOrderDetailRepository;
+            _basicRepository = basicRepository;
+            _stockInfoDetailRepository = stockInfoDetailRepository;
+            _palletTypeInfoRepository = palletTypeInfoRepository;
         }
 
         public string ReceiveWMSTaskin = WIDESEA_Core.Helper.AppSettings.Configuration["ReceiveWMSTaskin"];
@@ -171,32 +177,67 @@
         /// </summary>
         /// <param name="taskNum">浠诲姟鍙�</param>
         /// <returns>杩斿洖澶勭悊缁撴灉</returns>
-        public WebResponseContent TaskCompleted(int taskNum)
+        public async Task<WebResponseContent> TaskCompleted(int taskNum)
         {
             try
             {
-                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
+                Dt_Task task = await Repository.QueryFirstAsync(x => x.TaskNum == taskNum);
                 if (task == null)
                 {
-                    return WebResponseContent.Instance.Error("鏈壘鍒颁换鍔′俊鎭�");
+                    return await Task.FromResult(WebResponseContent.Instance.Error($"鏈壘鍒颁换鍔′俊鎭�"));
                 }
-                MethodInfo? methodInfo = GetType().GetMethod(((TaskTypeEnum)task.TaskType) + "TaskCompleted");
-                if (methodInfo != null)
+                if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup)
                 {
-                    WebResponseContent? responseContent = (WebResponseContent?)methodInfo.Invoke(this, new object[] { task });
-                    if (responseContent != null)
-                    {
-                        return responseContent;
-                    }
+                    return await Task.FromResult(InboundTaskCompleted(taskNum));
                 }
-                return WebResponseContent.Instance.Error("鏈壘鍒颁换鍔$被鍨嬪搴斾笟鍔″鐞嗛�昏緫");
+                else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
+                {
+                    return await Task.FromResult(OutboundTaskCompleted(taskNum));
+                }
+                //else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.RelocationGroup)
+                //{
+                //    return await Task.FromResult(RelocationTaskCompleted(task));
+                //}
+                else
+                {
+                    return await Task.FromResult(WebResponseContent.Instance.Error($"鏈壘鍒拌绫诲瀷浠诲姟,浠诲姟绫诲瀷:{task.TaskType}"));
+                }
+            }
+            catch (Exception ex)
+            {
+                return await Task.FromResult(WebResponseContent.Instance.Error(ex.Message));
+            }
+        }
+
+
+
+        /// <summary>
+        /// 淇敼浠诲姟鐘舵��
+        /// </summary>
+        /// <param name="task"></param>
+        /// <returns></returns>
+        public WebResponseContent UpdateTaskInfo(WCSTaskDTO task)
+        {
+            try
+            {
+                Dt_Task wmsTask = BaseDal.QueryFirst(x => x.TaskNum == task.TaskNum);
+                if (wmsTask != null)
+                {
+                    wmsTask.TaskStatus = task.TaskState;
+                    wmsTask.CurrentAddress = task.CurrentAddress;
+                    wmsTask.NextAddress = task.NextAddress;
+                    wmsTask.Dispatchertime = task.Dispatchertime;
+                    BaseDal.UpdateData(wmsTask);
+                }
+                return WebResponseContent.Instance.OK();
             }
             catch (Exception ex)
             {
                 return WebResponseContent.Instance.Error(ex.Message);
             }
-
         }
+
+
         public WebResponseContent UpdateTaskStatus(int taskNum, int tasktype)
         {
             WebResponseContent content = new WebResponseContent();
@@ -235,7 +276,7 @@
                 Dt_StockInfoDetail stockInfoDetail = stockInfo.Details.FirstOrDefault(x => x.StockId == stockInfo.Id);
 
                 List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.Repository.LocationCodesGetStockInfos(new List<string> { stockInfo.LocationCode }).Where(x => x.StockStatus == (int)StockStatusEmun.宸插叆搴�.ObjToInt()).ToList();
-                if (task.TaskType == TaskTypeEnum.PalletInbound.ObjToInt())
+                if (task.TaskType == TaskTypeEnum.Inbound.ObjToInt())
                 {
                     stockInfo.SerialNumber = stockInfos.Count + 1;
                     stockInfo.InDate = DateTime.Now;
@@ -293,7 +334,7 @@
                 _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetail);
                 //_recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Inbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
                 Db.Ado.CommitTran();
-                SendInboundInfoToWMS(task, inboundOrder, stockInfoDetail);
+                SendInboundInfoToWMS(task, inboundOrder, stockInfo);
                 #endregion
             }
             catch (Exception ex)
@@ -404,18 +445,18 @@
             Dt_InboundOrderDetail inboundOrderDetail = inboundOrder.Details
                 .FirstOrDefault(x => x.LinId == stockInfo.Details.FirstOrDefault()?.LinId);
 
-            if (inboundOrder.OrderType == 0 && inboundOrder != null && stockInfo.StockStatus == StockStatusEmun.鍏ュ簱纭.ObjToInt())
-            {
-                //鍏ュ簱鏁伴噺鍥炰紶
-                return ProcessNormalInbound(task, stockInfo, locationInfo, lastStatus, inboundOrder, inboundOrderDetail);
-            }
-            else
-            {
-                //璋冩嫧鍑哄叆搴撲换鍔℃暟閲忓洖浼�
-                return ProcessAllocateInbound(task, stockInfo, locationInfo, lastStatus, inboundOrder, inboundOrderDetail);
-            }
+            //if (inboundOrder != null && stockInfo.StockStatus == StockStatusEmun.鍏ュ簱纭.ObjToInt())
+            //{
+            //鍏ュ簱鏁伴噺鍥炰紶
+            return ProcessNormalInbound(task, stockInfo, locationInfo, lastStatus, inboundOrder, inboundOrderDetail);
+            //}
+            //else
+            //{
+            //    //璋冩嫧鍑哄叆搴撲换鍔℃暟閲忓洖浼�
+            //    return ProcessAllocateInbound(task, stockInfo, locationInfo, lastStatus, inboundOrder, inboundOrderDetail);
+            //}
         }
-
+        //鍏朵粬鍏ュ簱
         private WebResponseContent ProcessNormalInbound(Dt_Task task, Dt_StockInfo stockInfo, Dt_LocationInfo locationInfo,
             int lastStatus, Dt_InboundOrder inboundOrder, Dt_InboundOrderDetail inboundOrderDetail)
         {
@@ -444,11 +485,15 @@
                 // 鏇存柊鍏ュ簱鍗�
                 UpdateInboundOrder(inboundOrder, inboundOrderDetail);
 
-                // 濡傛灉鏄渶鍚庝竴鏉℃槑缁嗭紝澶勭悊WMS鍥炰紶
-                if (inboundOrder.Details.Count == 1)
+                // 鍗曟嵁鏁伴噺鍏ㄩ儴涓婃灦瀹屾垚锛屽鐞哤MS鍥炰紶
+                if (inboundOrder.OrderStatus == InboundStatusEnum.鍏ュ簱瀹屾垚.ObjToInt() && inboundOrder.OrderType == 0)
                 {
                     var stockInfoDetail = stockInfo.Details.FirstOrDefault();
-                    SendInboundInfoToWMS(task, inboundOrder, stockInfoDetail);
+                    SendInboundInfoToWMS(task, inboundOrder, stockInfo);
+                }
+                else if (inboundOrder.OrderStatus == InboundStatusEnum.鍏ュ簱瀹屾垚.ObjToInt() && inboundOrder.OrderType == 2)
+                {
+                    SendAllocateInfoToWMS(task, inboundOrder, stockInfo);
                 }
 
                 _unitOfWorkManage.CommitTran();
@@ -460,41 +505,51 @@
                 throw;
             }
         }
+        ////璋冩嫧鍏ュ簱
+        //private WebResponseContent ProcessAllocateInbound(Dt_Task task, Dt_StockInfo stockInfo, Dt_LocationInfo locationInfo,
+        //    int lastStatus, Dt_InboundOrder inboundOrder, Dt_InboundOrderDetail inboundOrderDetail)
+        //{
+        //    // 鏇存柊鍏ュ簱鍗曟槑缁嗙姸鎬�
+        //    UpdateInboundOrderDetails(stockInfo, inboundOrder, ref inboundOrderDetail);
 
-        private WebResponseContent ProcessAllocateInbound(Dt_Task task, Dt_StockInfo stockInfo, Dt_LocationInfo locationInfo,
-            int lastStatus, Dt_InboundOrder inboundOrder, Dt_InboundOrderDetail inboundOrderDetail)
-        {
-            // 濡傛灉鏄渶鍚庝竴鏉℃槑缁�
-            if (inboundOrder.Details.Count == 1)
-            {
-                try
-                {
-                    _unitOfWorkManage.BeginTran();
+        //    // 鏇存柊搴撳瓨鏄庣粏鐘舵��
+        //    stockInfo.Details.ForEach(x => x.Status = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt());
 
-                    // 澶勭悊璋冩嫧鍏ュ簱鍗�
-                    //ProcessAllocateInboundOrder(inboundOrder, inboundOrderDetail);
+        //    try
+        //    {
+        //        _unitOfWorkManage.BeginTran();
 
-                    // 鑾峰彇鐩稿叧搴撳瓨淇℃伅
-                    var relatedStockInfo = _stockService.StockInfoService.Repository
-                        .QueryData(x => x.BatchNo == inboundOrderDetail.BatchNo);
+        //        // 鏇存柊浠诲姟鐘舵��
+        //        UpdateTaskStatus(task);
 
-                    // 鍙戦�佽皟鎷ㄤ俊鎭埌WMS
-                    SendAllocateInfoToWMS(task, inboundOrder, relatedStockInfo);
+        //        // 鏇存柊璐т綅鐘舵��
+        //        UpdateLocationStatus(locationInfo, lastStatus);
 
-                    _unitOfWorkManage.CommitTran();
-                }
-                catch
-                {
-                    _unitOfWorkManage.RollbackTran();
-                    throw;
-                }
-            }
+        //        // 鏇存柊搴撳瓨淇℃伅
+        //        UpdateStockInfo(stockInfo);
 
-            // 娣诲姞鍘嗗彶璁板綍
-            AddInboundOrderDetailHistory(inboundOrderDetail);
+        //        // 娣诲姞璁板綍
+        //        AddRecords(task, stockInfo, locationInfo, lastStatus);
 
-            return WebResponseContent.Instance.OK();
-        }
+        //        // 鏇存柊鍏ュ簱鍗�
+        //        UpdateInboundOrder(inboundOrder, inboundOrderDetail);
+
+        //        // 鍗曟嵁鏁伴噺鍏ㄩ儴涓婃灦瀹屾垚锛屽鐞哤MS鍥炰紶
+        //        if (inboundOrderDetail.OrderDetailStatus == OrderDetailStatusEnum.Over.ObjToInt())
+        //        {
+        //            var stockInfoDetail = stockInfo.Details.FirstOrDefault();
+        //            SendInboundInfoToWMS(task, inboundOrder, stockInfo);
+        //        }
+
+        //        _unitOfWorkManage.CommitTran();
+        //        return WebResponseContent.Instance.OK();
+        //    }
+        //    catch
+        //    {
+        //        _unitOfWorkManage.RollbackTran();
+        //        throw;
+        //    }
+        //}
 
         #region Helper Methods
 
@@ -518,7 +573,12 @@
                 else if (inboundOrderDetail.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt())
                 {
                     inboundOrderDetail.OrderDetailStatus = InboundStatusEnum.鍏ュ簱涓�.ObjToInt();
+                    inboundOrder.OrderStatus = InboundStatusEnum.鍏ュ簱涓�.ObjToInt();
                 }
+            }
+            if (inboundOrder.Details.Count == overCount)
+            {
+                inboundOrder.OrderStatus = InboundStatusEnum.鍏ュ簱瀹屾垚.ObjToInt();
             }
         }
 
@@ -560,53 +620,62 @@
             }
         }
 
-        private void SendInboundInfoToWMS(Dt_Task task, Dt_InboundOrder inboundOrder, Dt_StockInfoDetail stockInfoDetail)
+        private void SendInboundInfoToWMS(Dt_Task task, Dt_InboundOrder inboundOrder, Dt_StockInfo stockInfo)
         {
-            if (stockInfoDetail == null) return;
 
+
+            List<Dt_StockInfo> StockInfos = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.WarehouseId == task.WarehouseId).Includes(x => x.Details).Where(x => x.Details.Any(v => v.OrderNo == inboundOrder.OrderNo)).ToList();
+            //var dt_StockInfo = _stockRepository.StockInfoRepository.QueryData(x => x.Id == stockInfo.Id).ToList();
+            //List<Dt_StockInfo> StockInfos = new List<Dt_StockInfo>();
+            //foreach (var item in dt_StockInfo)
+            //{
+            //    Dt_StockInfo StockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.Id == item.Id).Includes(x => x.Details).First();
+            //    StockInfos.Add(StockInfo);
+            //}
+
+            if (StockInfos.Count == 0) throw new Exception("鏈壘鍒板簱瀛樹俊鎭�");
             var houseInboundPassBack = new HouseInboundPassBack
             {
                 ApiType = "AsnController",
                 Method = "AsrsGroudingAsn",
                 Parameters = new List<HouseInboundPassBack.data>
+    {
+        new HouseInboundPassBack.data
         {
-            new HouseInboundPassBack.data
+            Value = new List<data.data1>
             {
-                Value = new List<data.data1>
+                new data.data1
                 {
-                    new data.data1
+                    AsnNo = inboundOrder.OrderNo,
+                    //InWarehouse = task.Roadway,
+                    TransactionCode = inboundOrder.TransactionCode,
+                    InoutType = inboundOrder.OrderType,
+                    OrderType = inboundOrder.InoutType,
+                    // 閬嶅巻鎵�鏈夊簱瀛樻槑缁嗭紝娣诲姞鍒� DetailList
+                    DetailList = StockInfos.Select(d => new data.data1.Inbound
                     {
-                        AsnNo = task.OrderNo,
-                        InWarehouse = task.Roadway,
-                        TransactionCode = inboundOrder.TransactionCode,
-                        InoutType = inboundOrder.OrderType,
-                        OrderType = inboundOrder.InoutType,
-                        DetailList = new List<data.data1.Inbound>
-                        {
-                            new data.data1.Inbound
-                            {
-                                LinId = stockInfoDetail.LinId,
-                                MaterielCode = stockInfoDetail.MaterielCode,
-                                OrderQuantity = stockInfoDetail.StockQuantity,
-                                BatchNo = stockInfoDetail.BatchNo,
-                                FinishQty = stockInfoDetail.StockQuantity,
-                                LocationName = task.TargetAddress
-                            }
-                        }
-                    }
+                        LinId = d.Details.FirstOrDefault()?.LinId?? "",
+                        MaterielCode = d.Details.FirstOrDefault()?.MaterielCode?? "",
+                        OrderQuantity = d.Details.FirstOrDefault().StockQuantity,
+                        BatchNo = d.BatchNo,
+                        LPNNo = d.PalletCode,
+                        FinishQty = d.Details.FirstOrDefault().StockQuantity,
+                        LocationName = d.LocationCode
+                    }).ToList()
                 }
             }
         }
+    }
             };
 
             var authResult = AuthenticateWithWMS();
             if (authResult.IsSuccess)
             {
                 houseInboundPassBack.Context = new Dictionary<string, string>
-        {
-            { "Ticket", authResult.Ticket },
-            { "InvOrgId", authResult.InvOrgId }
-        };
+            {
+                { "Ticket", authResult.Ticket },
+                { "InvOrgId", authResult.InvOrgId }
+            };
 
                 HttpHelper.Post<WebResponseContent>(ReceiveWMSTaskin, houseInboundPassBack, "绔嬪簱鍏ュ簱鏁伴噺鍥炰紶WMS");
             }
@@ -650,40 +719,48 @@
             _inboundService.InboundOrderDetailService.DeleteData(inboundOrderDetail);
         }
 
-        private void SendAllocateInfoToWMS(Dt_Task task, Dt_InboundOrder inboundOrder, List<Dt_StockInfo> stockInfos)
+        private void SendAllocateInfoToWMS(Dt_Task task, Dt_InboundOrder inboundOrder, Dt_StockInfo stockInfos)
         {
 
-            var detail = _stockService.StockInfoDetailService.Repository.QueryFirst(x => x.StockId == stockInfos.FirstOrDefault().Id);
+            List<Dt_StockInfo> StockInfos = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.WarehouseId == task.WarehouseId).Includes(x => x.Details).Where(x => x.Details.Any(v => v.OrderNo == inboundOrder.OrderNo)).ToList();
+            //var dt_StockInfo = _stockRepository.StockInfoRepository.QueryData(x => x.Id == stockInfo.Id).ToList();
+            //List<Dt_StockInfo> StockInfos = new List<Dt_StockInfo>();
+            //foreach (var item in dt_StockInfo)
+            //{
+            //    Dt_StockInfo StockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.Id == item.Id).Includes(x => x.Details).First();
+            //    StockInfos.Add(StockInfo);
+            //}
+
+            if (StockInfos.Count == 0) throw new Exception("鏈壘鍒板簱瀛樹俊鎭�");
             var inventoryAllocate = new InventoryAllocate
             {
-                ApiType = "AsnController",
-                Method = "AsrsGroudingAsn",
+                ApiType = "InventoryAllocateController",
+                Method = "AsrsFinishedStockCount",
                 Parameters = new List<InventoryAllocate.Allocate>
         {
              new InventoryAllocate.Allocate
             {
                 Value = new List<Allocate.data1>
                 {
-                    new Allocate.data1
+                new Allocate.data1
+                {
+                    No = inboundOrder.OrderNo,
+                    //InWarehouse = task.Roadway,
+                    TransactionCode = inboundOrder.TransactionCode,
+                    InoutType = inboundOrder.OrderType,
+                    OrderType = inboundOrder.InoutType,
+                    // 閬嶅巻鎵�鏈夊簱瀛樻槑缁嗭紝娣诲姞鍒� DetailList
+                    DetailList = StockInfos.Select(d => new Allocate.data1.Inventory
                     {
-                        No = task.OrderNo,
-                        InWarehouse = task.Roadway,
-                        TransactionCode = inboundOrder.TransactionCode,
-                        InoutType = inboundOrder.OrderType,
-                        OrderType = inboundOrder.InoutType,
-                        DetailList = new List<Allocate.data1.Inventory>
-                        {
-                            new Allocate.data1.Inventory
-                            {
-                                LinId = detail.LinId,
-                                MaterielCode = detail.MaterielCode,
-                                OrderQuantity = detail.StockQuantity,
-                                BatchNo = detail.BatchNo,
-                                FinishQty = detail.StockQuantity,
-                                LocationName = task.TargetAddress
-                            }
-                        }
-                    }
+                        LinId = d.Details.FirstOrDefault()?.LinId?? "",
+                        MaterielCode = d.Details.FirstOrDefault()?.MaterielCode?? "",
+                        OrderQuantity = d.Details.FirstOrDefault().StockQuantity,
+                        BatchNo = d.BatchNo,
+                        LPN_No = d.PalletCode,
+                        FinishQty = d.Details.FirstOrDefault().StockQuantity,
+                        LocationName = d.LocationCode
+                    }).ToList()
+                }
                 }
             }
         }
@@ -728,15 +805,15 @@
                 ApiType = "AuthenticationController",
                 Parameters = new List<Parameter>
         {
-            new Parameter { Value = "LKAdmin" },
-            new Parameter { Value = "LKAdmin" }
+            new Parameter { Value = "LK-Admin" },
+            new Parameter { Value = "LK-Admin" }
         },
                 Method = "Login",
             };
 
             var response = HttpHelper.Post<WebResponseContent>(ReceiveWMSTaskAUT, authentication, "鐧诲綍WMS璐﹀彿");
 
-            if (response.Status && response.Context != null)
+            if (response.Context != null)
             {
                 return (true, response.Context["Ticket"].ToString(), response.Context["InvOrgId"].ToString());
             }
@@ -760,8 +837,26 @@
                     out var locationInfo, out var outStockLockInfos);
                 if (!validationResult.Status) return validationResult;
 
+                int lastStatus = locationInfo.LocationStatus;
+                locationInfo.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
+
+
+
+                Dt_OutboundOrderDetail outboundOrderDetails = _outboundService.OutboundOrderService.Db.Queryable<Dt_OutboundOrderDetail>()
+                    .Where(x => x.BatchNo == stockInfo.Details.FirstOrDefault().BatchNo)
+                    .First();
+                if (outboundOrderDetails == null) return WebResponseContent.Instance.Error("鏈壘鍒板嚭搴撳崟淇℃伅");
+
+                Dt_OutboundOrder inboundOrder = _outboundService.OutboundOrderService.Db.Queryable<Dt_OutboundOrder>()
+    .Where(x => x.Id == outboundOrderDetails.OrderId)
+    .Includes(x => x.Details)
+    .First();
+
+                if (inboundOrder == null) return WebResponseContent.Instance.Error("鏈壘鍒板嚭搴撳崟淇℃伅");
                 // 2. 澶勭悊鍑哄簱璁㈠崟璇︽儏
-                var outboundOrderDetails = ProcessOutboundOrderDetails(outStockLockInfos);
+                //var outboundOrderDetails = ProcessOutboundOrderDetails(outStockLockInfos);
+
+                UpdateOutboundOrderDetails(stockInfo, inboundOrder, ref outboundOrderDetails);
 
                 // 3. 鎵ц鏍稿績鍑哄簱閫昏緫
                 return ExecuteOutboundLogic(task, stockInfo, locationInfo, outboundOrderDetails);
@@ -800,33 +895,34 @@
             return WebResponseContent.Instance.OK();
         }
 
-        private List<Dt_OutboundOrderDetail> ProcessOutboundOrderDetails(List<Dt_OutStockLockInfo> outStockLockInfos)
-        {
-            var outboundOrderDetails = new List<Dt_OutboundOrderDetail>();
+        //private List<Dt_OutboundOrderDetail> ProcessOutboundOrderDetails(List<Dt_OutStockLockInfo> outStockLockInfos)
+        //{
+        //    var outboundOrderDetails = new List<Dt_OutboundOrderDetail>();
 
-            foreach (var lockInfo in outStockLockInfos)
-            {
-                var detail = _outboundService.OutboundOrderDetailService.Repository.QueryFirst(x => x.Id == lockInfo.OrderDetailId);
-                if (detail != null)
-                {
-                    detail.OverOutQuantity = detail.LockQuantity;
-                    if (detail.LockQuantity == detail.OrderQuantity)
-                    {
-                        detail.OrderDetailStatus = OrderDetailStatusEnum.Over.ObjToInt();
-                    }
-                    outboundOrderDetails.Add(detail);
-                }
-            }
+        //    foreach (var lockInfo in outStockLockInfos)
+        //    {
+        //        List<Dt_OutboundOrderDetail> detail = _outboundService.OutboundOrderDetailService.Repository.QueryData(x => x.Id == lockInfo.Id);
+        //        if (detail != null)
+        //        {
+        //            detail.OverOutQuantity = lockInfo.OriginalQuantity;
+        //            if (detail.LockQuantity == detail.OrderQuantity)
+        //            {
+        //                detail.OrderDetailStatus = OrderDetailStatusEnum.Over.ObjToInt();
+        //            }
+        //            outboundOrderDetails.Add(detail);
+        //        }
+        //    }
 
-            return outboundOrderDetails;
-        }
+        //    return outboundOrderDetails;
+        //}
 
         private WebResponseContent ExecuteOutboundLogic(Dt_Task task, Dt_StockInfo stockInfo,
-            Dt_LocationInfo locationInfo, List<Dt_OutboundOrderDetail> outboundOrderDetails)
+            Dt_LocationInfo locationInfo, Dt_OutboundOrderDetail outboundOrderDetails)
         {
             try
             {
                 _unitOfWorkManage.BeginTran();
+
 
                 // 鏇存柊鍑哄簱璁㈠崟璇︽儏
                 _outboundService.OutboundOrderDetailService.Repository.UpdateData(outboundOrderDetails);
@@ -845,22 +941,26 @@
 
                 // 鏍规嵁璁㈠崟绫诲瀷澶勭悊涓嶅悓閫昏緫
                 var outDetail = _outboundService.OutboundOrderDetailService.Db.Queryable<Dt_OutboundOrderDetail>()
-                    .Where(x => x.LPNNo == stockInfo.PalletCode).First();
+                    .Where(x => x.BatchNo == outboundOrderDetails.BatchNo).First();
                 var outboundOrder = _outboundService.OutboundOrderService.Db.Queryable<Dt_OutboundOrder>()
                     .Where(x => x.Id == outDetail.OrderId).Includes(x => x.Details).First();
 
-                if (outboundOrder.OrderType == 1) // 鏅�氬嚭搴�
+
+                if (outboundOrder.OrderStatus == OutboundStatusEnum.鍑哄簱瀹屾垚.ObjToInt())
                 {
-                    ProcessNormalOutbound(task, stockInfo, outboundOrder, outDetail);
+                    var stockInfoDetail = stockInfo.Details.FirstOrDefault();
+                    //ProcessNormalOutbound(task, stockInfo, outboundOrder, outDetail);
+                    SendNormalOutboundToWMS(task, outboundOrder, stockInfoDetail, outDetail);
                 }
-                else if (outboundOrder.OrderType == 240) // 鐩樼偣鍑哄簱
+
+                if (outboundOrder.OrderType == 240) // 鐩樼偣鍑哄簱
                 {
                     // 鐩樼偣鍑哄簱鐗规畩澶勭悊閫昏緫
                 }
-                else // 璋冩嫧鍑哄簱
-                {
-                    ProcessAllocateOutbound(task, stockInfo, outboundOrder, outDetail);
-                }
+                //else // 璋冩嫧鍑哄簱
+                //{
+                //    ProcessAllocateOutbound(task, stockInfo, outboundOrder, outDetail);
+                //}
 
                 _unitOfWorkManage.CommitTran();
                 return WebResponseContent.Instance.OK();
@@ -871,12 +971,45 @@
                 throw;
             }
         }
+        private void UpdateOutboundOrderDetails(Dt_StockInfo stockInfo, Dt_OutboundOrder inboundOrder,
+    ref Dt_OutboundOrderDetail inboundOrderDetail)
+        {
+            int overCount = inboundOrder.Details.Count(x => x.OrderDetailStatus == OrderDetailStatusEnum.Over.ObjToInt());
+            inboundOrderDetail = inboundOrder.Details.FirstOrDefault(x => x.BatchNo == stockInfo.Details.FirstOrDefault()?.BatchNo);
+
+            foreach (var item in stockInfo.Details)
+            {
+                if (inboundOrderDetail == null) continue;
+
+                inboundOrderDetail.OverOutQuantity += item.OutboundQuantity;
+
+                if (inboundOrderDetail.OverOutQuantity == inboundOrderDetail.OrderQuantity)
+                {
+                    inboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.Over.ObjToInt();
+                    overCount++;
+                }
+                else if (inboundOrderDetail.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt())
+                {
+                    inboundOrderDetail.OrderDetailStatus = OutboundStatusEnum.鍑哄簱涓�.ObjToInt();
+                    inboundOrder.OrderStatus = OutboundStatusEnum.鍑哄簱涓�.ObjToInt();
+                }
+            }
+            if (inboundOrder.Details.Count == overCount)
+            {
+                inboundOrder.OrderStatus = OutboundStatusEnum.鍑哄簱瀹屾垚.ObjToInt();
+            }
+        }
 
         private void UpdateStockStatus(Dt_StockInfo stockInfo, Dt_LocationInfo locationInfo)
         {
-            stockInfo.LocationCode = locationInfo.LocationCode;
+            stockInfo.LocationCode = "";
             stockInfo.StockStatus = StockStatusEmun.鍑哄簱瀹屾垚.ObjToInt();
-            _stockService.StockInfoService.Repository.UpdateData(stockInfo);
+            stockInfo.Details.ForEach(x =>
+            {
+                x.Status = StockStatusEmun.鍑哄簱瀹屾垚.ObjToInt();
+            });
+            _stockRepository.StockInfoRepository.UpdateData(stockInfo);
+            _stockRepository.StockInfoDetailRepository.UpdateData(stockInfo.Details);
         }
 
         private void UpdateLocationStatus(Dt_LocationInfo locationInfo)
@@ -907,18 +1040,40 @@
             var stockInfoDetail = _stockRepository.StockInfoDetailRepository.Db.Queryable<Dt_StockInfoDetail>()
                 .Where(x => x.StockId == stockInfo.Id).First();
 
-            if (outboundOrder.Details.Count == 1)
-            {
-                MoveOutboundOrderToHistory(outboundOrder);
-            }
+            //if (outboundOrder.OrderStatus == OutboundStatusEnum.鍑哄簱瀹屾垚.ObjToInt())
+            //{
+            //    var history = new Dt_OutboundOrder_Hty
+            //    {
+            //        OrderStatus = outboundOrder.OrderStatus,
+            //        CreateType = outboundOrder.CreateType,
+            //        UpperOrderNo = outboundOrder.UpperOrderNo,
+            //        OrderNo = outboundOrder.OrderNo,
+            //        OutWareHouse = outboundOrder.OutWareHouse,
+            //        TransactionCode = outboundOrder.TransactionCode,
+            //        InoutType = outboundOrder.InoutType,
+            //        OrderType = outboundOrder.OrderType,
+            //        Creater = "WMS",
+            //        CreateDate = DateTime.Now,
+            //    };
+            //    _outboundOrder_HtyService.AddData(history);
+            //    _outboundService.OutboundOrderService.DeleteData(outboundOrder);
+            //}
 
-            MoveOutboundDetailToHistory(outDetail);
+            //MoveOutboundDetailToHistory(outDetail);
 
             // 鍙戦�佸嚭搴撲俊鎭埌WMS
-            SendNormalOutboundToWMS(task, outboundOrder, stockInfoDetail, outDetail);
+            if (outboundOrder.OrderStatus == OutboundStatusEnum.鍑哄簱瀹屾垚.ObjToInt() && outboundOrder.OrderType == 0)
+            {
+                SendNormalOutboundToWMS(task, outboundOrder, stockInfoDetail, outDetail);
+            }
+            else if (outboundOrder.OrderStatus == OutboundStatusEnum.鍑哄簱瀹屾垚.ObjToInt() && outboundOrder.OrderType == 2)
+            {
+                SendAllocateOutboundToWMS(task, outboundOrder, stockInfoDetail, outDetail);
+            }
+
 
             // 鍒犻櫎搴撳瓨淇℃伅
-            DeleteStockInfo(stockInfo);
+            //DeleteStockInfo(stockInfo);
         }
 
         private void ProcessAllocateOutbound(Dt_Task task, Dt_StockInfo stockInfo,
@@ -928,36 +1083,32 @@
 
             if (outboundOrder.Details.Count == 1)
             {
-                MoveOutboundOrderToHistory(outboundOrder);
+                var history = new Dt_OutboundOrder_Hty
+                {
+                    OrderStatus = outboundOrder.OrderStatus,
+                    CreateType = outboundOrder.CreateType,
+                    UpperOrderNo = outboundOrder.UpperOrderNo,
+                    OrderNo = outboundOrder.OrderNo,
+                    OutWareHouse = outboundOrder.OutWareHouse,
+                    TransactionCode = outboundOrder.TransactionCode,
+                    InoutType = outboundOrder.InoutType,
+                    OrderType = outboundOrder.OrderType,
+                    Creater = "WMS",
+                    CreateDate = DateTime.Now,
+                };
+                _outboundOrder_HtyService.AddData(history);
+                _outboundService.OutboundOrderService.DeleteData(outboundOrder);
             }
 
             MoveOutboundDetailToHistory(outDetail);
 
             // 鍙戦�佽皟鎷ㄥ嚭搴撲俊鎭埌WMS
-            SendAllocateOutboundToWMS(task, outboundOrder, outDetail, relatedStockInfos);
+
 
             // 鍒犻櫎搴撳瓨淇℃伅
             DeleteStockInfo(stockInfo);
         }
 
-        private void MoveOutboundOrderToHistory(Dt_OutboundOrder outboundOrder)
-        {
-            var history = new Dt_OutboundOrder_Hty
-            {
-                OrderStatus = outboundOrder.OrderStatus,
-                CreateType = outboundOrder.CreateType,
-                UpperOrderNo = outboundOrder.UpperOrderNo,
-                OrderNo = outboundOrder.OrderNo,
-                OutWareHouse = outboundOrder.OutWareHouse,
-                TransactionCode = outboundOrder.TransactionCode,
-                InoutType = outboundOrder.InoutType,
-                OrderType = outboundOrder.OrderType,
-                Creater = "WMS",
-                CreateDate = DateTime.Now,
-            };
-            _outboundOrder_HtyService.AddData(history);
-            _outboundService.OutboundOrderService.DeleteData(outboundOrder);
-        }
 
         private void MoveOutboundDetailToHistory(Dt_OutboundOrderDetail outDetail)
         {
@@ -979,40 +1130,48 @@
         private void SendNormalOutboundToWMS(Dt_Task task, Dt_OutboundOrder outboundOrder,
             Dt_StockInfoDetail stockInfoDetail, Dt_OutboundOrderDetail outDetail)
         {
+
+
+            //List<Dt_StockInfo> StockInfos = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.WarehouseId == task.WarehouseId).Includes(x => x.Details).Where(x => x.Details.Any(v => v.BatchNo == outDetail.BatchNo)).ToList();
+            List<Dt_OutStockLockInfo> outStockLockInfos = _outboundService.OutboundStockLockInfoService.Repository.QueryData(x => x.OrderNo == outboundOrder.OrderNo);
+
+
+            if (outStockLockInfos.Count == 0) throw new Exception("鏈壘鍒板簱瀛樹俊鎭�");
+
+            // 鏋勫缓鍥炰紶鏁版嵁锛屽皢鎵�鏈夊尮閰嶇殑搴撳瓨鏄庣粏鍔犲叆 DetailList
             var passBack = new HouseoutboundPassBack
             {
-                ApiType = "ShippingOrderController",
-                Method = "AsrsOutboundSO",
+                ApiType = "InventoryAllocateController",
+                Method = "AsrsFinishedStockCount",
                 Parameters = new List<HouseoutboundPassBack.datas>
+    {
+        new HouseoutboundPassBack.datas
         {
-            new HouseoutboundPassBack.datas
+            Value = new List<HouseoutboundPassBack.datas.data1>
             {
-                Value = new List<HouseoutboundPassBack.datas.data1>
+                new HouseoutboundPassBack.datas.data1
                 {
-                    new HouseoutboundPassBack.datas.data1
-                    {
-                        No = outboundOrder.OrderNo,
-                        OutWareHouse = task.Roadway,
-                        TransactionCode = outboundOrder.TransactionCode,
-                        InoutType = outboundOrder.OrderType,
-                        OrderType = outboundOrder.InoutType,
-                        DetailList = new List<HouseoutboundPassBack.datas.data1.Inbound>
+                    No = outboundOrder.OrderNo,
+                    OutWareHouse = task.Roadway,
+                    TransactionCode = outboundOrder.TransactionCode,
+                    InoutType = outboundOrder.OrderType,
+                    OrderType = outboundOrder.InoutType,
+                    DetailList = outStockLockInfos.Select(d => new HouseoutboundPassBack.datas.data1.Inbound
                         {
-                            new HouseoutboundPassBack.datas.data1.Inbound
-                            {
-                                LinId = outDetail.LinId,
-                                LPN_No = task.PalletCode,
-                                MaterielCode = stockInfoDetail.MaterielCode,
-                                OrderQuantity = stockInfoDetail.OutboundQuantity,
-                                BatchNo = stockInfoDetail.BatchNo,
-                                FinishQty = stockInfoDetail.OutboundQuantity,
-                                LocationName = task.SourceAddress
-                            }
-                        }
-                    }
+                            LinId = d.LinId?? "",
+                            LPN_No = d.PalletCode,
+                            MaterielCode = d.MaterielCode?? "",
+                            OrderQuantity = d.OrderQuantity,
+                            BatchNo = d.BatchNo,
+                            FinishQty = d.OrderQuantity,
+                            LocationName = d.LocationCode
+                        })
+                        .ToList()
+
                 }
             }
         }
+    }
             };
 
             var authResult = AuthenticateWithWMS();
@@ -1029,8 +1188,12 @@
         }
 
         private void SendAllocateOutboundToWMS(Dt_Task task, Dt_OutboundOrder outboundOrder,
-            Dt_OutboundOrderDetail outDetail, List<Dt_StockInfo> stockInfos)
+            Dt_StockInfoDetail stockInfoDetail, Dt_OutboundOrderDetail outDetail)
         {
+            List<Dt_OutStockLockInfo> outStockLockInfos = _outboundService.OutboundStockLockInfoService.Repository.QueryData(x => x.OrderNo == outboundOrder.OrderNo);
+
+
+            if (outStockLockInfos.Count == 0) throw new Exception("鏈壘鍒板簱瀛樹俊鎭�");
             var allocate = new InventoryAllocate
             {
                 ApiType = "InventoryAllocateController",
@@ -1048,7 +1211,7 @@
                         TransactionCode = outboundOrder.TransactionCode,
                         InoutType = outboundOrder.OrderType,
                         OrderType = outboundOrder.InoutType,
-                        DetailList = stockInfos.Select(item =>
+                        DetailList = outStockLockInfos.Select(item =>
                         {
                             var detail = _stockService.StockInfoDetailService.Repository.QueryFirst(x => x.StockId == item.Id);
                             return new Allocate.data1.Inventory

--
Gitblit v1.9.3