From 9e579eda4601ed7b492b9d19a24e8146f6ebdf8d Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期六, 19 四月 2025 19:50:43 +0800
Subject: [PATCH] 优化空托出入库逻辑

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/PartialTaskService_Inbound.cs |  162 +++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 137 insertions(+), 25 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/PartialTaskService_Inbound.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/PartialTaskService_Inbound.cs"
index 48c0576..095fc47 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/PartialTaskService_Inbound.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/PartialTaskService_Inbound.cs"
@@ -10,6 +10,7 @@
 using WIDESEA_Core.Helper;
 using WIDESEAWCS_DTO.WCSInfo;
 using System.Transactions;
+using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
 
 namespace WIDESEA_TaskInfoService
 {
@@ -122,7 +123,7 @@
                     locationInfo.LocationStatus = LocationStatusEnum.PalletLock.ObjToInt();
                     if (isUpdateStock)
                     {
-                        locationInfo.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
+                        locationInfo.LocationStatus = LocationStatusEnum.OutLock.ObjToInt();
                         if (stockInfo != null && stockInfo.Details != null && stockInfo.Details.Count > 0)
                         {
                             orderNo = stockInfo.Details.FirstOrDefault()?.OrderNo ?? "";
@@ -159,9 +160,27 @@
             WebResponseContent content = new WebResponseContent();
             try
             {
+                Dt_RoadwayInfo roadwayInfo = _basicRepository.RoadwayInfoRepository.QueryFirst(x => x.InStationCode == lineDTO.stationCode) ?? throw new Exception("鏈壘鍒拌绔欏彴瀵瑰簲鐨勫贩閬擄紝璇锋鏌ュ熀纭�閰嶇疆淇℃伅");
+                if (roadwayInfo.EnableStatus != EnableStatusEnum.Normal.ObjToInt() && roadwayInfo.EnableStatus != EnableStatusEnum.OnlyIn.ObjToInt())
+                {
+                    throw new Exception("璇ュ贩閬撲笉鍙叆搴擄紝璇锋鏌ュ贩閬撶鐢ㄤ俊鎭�");
+                }
+                if (roadwayInfo.AreaId > 0)
+                {
+                    Dt_AreaInfo areaInfo = _basicRepository.AreaInfoRepository.QueryFirst(x => x.Id == roadwayInfo.AreaId);
+                    if (areaInfo != null && areaInfo.AreaStatus == EnableEnum.Disable.ObjToInt())
+                    {
+                        throw new Exception("鍖哄煙琚鐢紝涓嶅彲鍏ュ簱");
+                    }
+                }
                 var task = BaseDal.QueryFirst(x => x.PalletCode == lineDTO.Barcode);
                 if (task == null) throw new Exception($"鏈壘鍒版墭鐩樺彿[{lineDTO.Barcode}]鐨勫叆搴撲换鍔�");
                 if (task.TaskState >= (int)InTaskStatusEnum.SC_InExecuting) throw new Exception($"鎵樼洏鍙穂{lineDTO.Barcode}]鐨勫叆搴撲换鍔$姸鎬佷笉鍖归厤");
+
+                #region 鍒ゆ柇鏄惁涓虹┖鎵樺叆搴�
+                if (task.TaskType == TaskTypeEnum.PalletInbound.ObjToInt())
+                    return GetEmptyLocation(task);
+                #endregion
 
                 Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(lineDTO.Barcode);
                 if (stockInfo == null) throw new Exception($"鏈壘鍒版墭鐩榌{lineDTO.Barcode}]鐨勭粍鐩樹俊鎭�");
@@ -171,12 +190,13 @@
                     throw new Exception($"鎵樼洏[{lineDTO.Barcode}],璇ョ粍鐩樼姸鎬佷笉鍙叆搴�");
 
                 Dt_InboundOrder inboundOrder = _inboundService.InbounOrderService.GetInboundOrder(stockInfoDetail.OrderNo);
-                if (inboundOrder == null || inboundOrder.Details == null) throw new Exception($"鏈壘鍒版墭鐩榌{lineDTO.Barcode}]鐨勫叆搴撳崟鏄庣粏淇℃伅");
-                Dt_LocationInfo locationInfo = null;
+                if (inboundOrder == null) throw new Exception($"鏈壘鍒版墭鐩榌{lineDTO.Barcode}]鐨勫叆搴撳崟淇℃伅");
 
                 #region 鍒ゆ柇鏄惁鎸囧畾璐т綅
-                Dt_InboundOrderDetail orderDetail = inboundOrder.Details.FirstOrDefault(x => x.BatchNo == stockInfoDetail.BatchNo);
-                if (orderDetail != null && !string.IsNullOrEmpty(orderDetail.LocationCode))
+                Dt_InboundOrderDetail? orderDetail = inboundOrder.Details.FirstOrDefault(x => x.BatchNo == stockInfoDetail.BatchNo);
+                if (orderDetail == null) throw new Exception($"鏈壘鍒版墭鐩榌{lineDTO.Barcode}]鐨勫叆搴撳崟鏄庣粏淇℃伅");
+                Dt_LocationInfo? locationInfo = null;
+                if (!string.IsNullOrEmpty(orderDetail.LocationCode))
                 {
                     locationInfo = _basicService.LocationInfoService.GetLocation(orderDetail.LocationCode);
                 }
@@ -185,13 +205,13 @@
                 #region 鍒ゆ柇褰撳墠鍏ュ簱鎵樼洏鎵规鍙锋槸鍚﹀瓨鍦ㄥ凡鍒嗛厤鍏ュ簱
                 else
                 {
-                    Dt_StockInfo instockInfo = null;
+                    Dt_StockInfo? instockInfo = null;
                     Dt_StockInfoDetail instockInfoDetail = _stockService.StockInfoDetailService.Get_StockInfoDetail(stockInfoDetail.OrderNo, stockInfoDetail.BatchNo);
                     if (instockInfoDetail != null) instockInfo = _stockService.StockInfoService.Repository.GetStockInfo(instockInfoDetail.StockId);
                     if (instockInfo != null)
                     {
                         locationInfo = _basicService.LocationInfoService.GetLocation(instockInfo.LocationCode);
-                        if (locationInfo?.MaxQty <= locationInfo?.CurrentQty) locationInfo = null;
+                        if (locationInfo.MaxQty <= locationInfo.CurrentQty) locationInfo = null;
                     }
                     #endregion
                     else
@@ -200,25 +220,19 @@
                         if (instockInfoDetail != null) instockInfo = _stockService.StockInfoService.Repository.GetStockInfo(instockInfoDetail.StockId);
                         if (instockInfo != null)
                         {
-                            locationInfo = _basicService.LocationInfoService.GetLocation(instockInfo.LocationCode);
-                            //if (locationInfo.MaxQty == 12)
-                            //{
-                            //    locationInfo.EnableStatus = EnableStatusEnum.OnlyOut.ObjToInt();
-                            //    _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
-                            //    locationInfo = null;
-                            //}
-                            if (locationInfo.EnableStatus == EnableStatusEnum.OnlyOut.ObjToInt()) locationInfo = null;
-                            else
+                            Dt_LocationInfo? location = _basicService.LocationInfoService.GetLocation(instockInfo.LocationCode);//鏌ヨ宸插瓨鍦ㄥ簱瀛樿揣浣嶇姸鎬�
+                            if (location?.Row == 1)
                             {
-                                List<Dt_StockInfoDetail> stockInfoDetails = _stockService.StockInfoDetailService.Get_StockInfoDetails(stockInfoDetail.OrderNo);//鑾峰彇宸ュ崟鎵�鏈夊凡鍏ュ簱鎴栧叆搴撲腑搴撳瓨
-                                if (stockInfoDetails.GroupBy(x => x.BatchNo).Count() < 2) locationInfo.EnableStatus = EnableStatusEnum.OnlyOut.ObjToInt();
-                                else locationInfo = null;
+                                var newLocation = Db.Queryable<Dt_LocationInfo>().Where(x => x.Row == 2 && x.Layer == location.Layer && x.Column == location.Column).First();
+                                if (newLocation != null && newLocation.LocationStatus == LocationStatusEnum.Free.ObjToInt() && newLocation.CurrentQty == 0 && (newLocation.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt() || newLocation.EnableStatus == EnableStatusEnum.Normal.ObjToInt()))
+                                {
+                                    locationInfo = newLocation;
+                                }
                             }
-                            if (locationInfo?.MaxQty <= locationInfo?.CurrentQty) locationInfo = null;
                         }
                     }
 
-                    if (locationInfo == null)
+                    if (locationInfo == null)//鍒嗛厤鏂拌揣浣�
                     {
                         int inboundOrderCount = inboundOrder.Details.Count;
                         List<Dt_StockInfoDetail> stockInfoDetails = _stockService.StockInfoDetailService.Get_StockInfoDetails(stockInfoDetail.OrderNo);//鑾峰彇宸ュ崟鎵�鏈夊凡鍏ュ簱鎴栧叆搴撲腑搴撳瓨
@@ -226,7 +240,7 @@
                         locationInfo = _basicService.LocationInfoService.AssignLocation(inboundOrderCount);//閲嶆柊鍒嗛厤璐т綅
                     }
                 }
-                
+
 
                 if (locationInfo == null) throw new Exception($"鏃犲彲鍏ヨ揣浣�");
 
@@ -235,13 +249,17 @@
                 task.CurrentAddress = task.NextAddress;
                 task.NextAddress = locationInfo.LocationCode;
                 task.TargetAddress = task.NextAddress;
-                task.IsPickPlace = locationInfo.MaxQty - locationInfo.CurrentQty == 1;
+                string[] targetCodes = task.NextAddress.Split("-");
+                var Row = Convert.ToInt16(targetCodes[0]);
+                task.TargetIsPickPlace = Row > 1 ? locationInfo.MaxQty - locationInfo.CurrentQty == 1 : false;
+                task.SourceIsPickPlace = false;
                 stockInfo.LocationCode = locationInfo.LocationCode;
                 stockInfo.StockStatus = StockStatusEmun.鍏ュ簱涓�.ObjToInt();
                 stockInfoDetail.Status = StockStatusEmun.鍏ュ簱涓�.ObjToInt();
                 locationInfo.CurrentQty++;
                 if (locationInfo.MaxQty < locationInfo.CurrentQty) throw new Exception();
-                if (locationInfo.MaxQty == 12) locationInfo.EnableStatus = EnableStatusEnum.OnlyOut.ObjToInt();
+                /*if (locationInfo.MaxQty == 12)*/
+                locationInfo.EnableStatus = EnableStatusEnum.OnlyOut.ObjToInt();
                 locationInfo.LocationStatus = LocationStatusEnum.Inbounding.ObjToInt();
 
                 Db.Ado.BeginTran();
@@ -254,7 +272,8 @@
                 content.OK(data: new ReceiveWMSInfo()
                 {
                     TargetAddress = task.TargetAddress,
-                    IsPickPlace = task.IsPickPlace,
+                    SourceIsPickPlace = task.SourceIsPickPlace,
+                    TargetIsPickPlace = task.TargetIsPickPlace,
                 });
             }
             catch (Exception ex)
@@ -265,6 +284,99 @@
             return content;
         }
 
+        /// <summary>
+        /// 鑾峰彇绌烘墭鐩樺叆搴撹揣浣�
+        /// </summary>
+        /// <param name="task"></param>
+        /// <returns></returns>
+        public WebResponseContent GetEmptyLocation(Dt_Task task)
+        {
+            try
+            {
+                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
+                if (stockInfo == null) throw new Exception($"鏈壘鍒版墭鐩榌{task.PalletCode}]鐨勭粍鐩樹俊鎭�");
+                Dt_StockInfoDetail? stockInfoDetail = stockInfo.Details.Where(x => x.StockId == stockInfo.Id).FirstOrDefault();
+                if (stockInfoDetail == null) throw new Exception($"鏈壘鍒版墭鐩榌{task.PalletCode}]鐨勭粍鐩樿鎯�");
+                if (stockInfo.StockStatus != StockStatusEmun.鍏ュ簱纭.ObjToInt() || !string.IsNullOrEmpty(stockInfo.LocationCode))
+                    throw new Exception($"鎵樼洏[{task.PalletCode}],璇ョ粍鐩樼姸鎬佷笉鍙叆搴�");
+                Dt_StockInfo? instockInfo = null;
+                Dt_LocationInfo? locationInfo = null;
+                string CarChargingStation = AppSettings.Configuration[nameof(CarChargingStation)];//鑾峰彇绌挎杞﹀厖鐢典綅缃�
+                var ChargingStations = CarChargingStation.Split(",");
+                Dt_StockInfoDetail? instockInfoDetail = null;
+                var instockInfoDetails = _stockService.StockInfoDetailService.Repository.QueryData(x => x.MaterielCode == stockInfoDetail.MaterielCode && x.Status == StockStatusEmun.鍏ュ簱涓�.ObjToInt());
+                if (instockInfoDetails.Count > 0)
+                    instockInfoDetail = instockInfoDetails.OrderByDescending(x => x.CreateDate).First();
+                if (instockInfoDetails.Count < 1)
+                {
+                    instockInfoDetails = _stockService.StockInfoDetailService.Repository.QueryData(x => x.MaterielCode == stockInfoDetail.MaterielCode && x.Status == StockStatusEmun.宸插叆搴�.ObjToInt());
+                    if (instockInfoDetails != null && instockInfoDetails.Count > 0)
+                        instockInfoDetail = instockInfoDetails.OrderByDescending(x => x.CreateDate).First();
+                }
+                if (instockInfoDetail != null) instockInfo = _stockService.StockInfoService.Repository.GetStockInfo(instockInfoDetail.StockId);
+                if (instockInfo != null)//鏃犲簱瀛�
+                {
+                    locationInfo = _basicRepository.LocationInfoRepository.QueryFirst(x => x.LocationCode == instockInfo.LocationCode);
+                    if (ChargingStations.Contains(locationInfo.LocationCode))
+                    {
+                        if (locationInfo.MaxQty - locationInfo.CurrentQty <= 1) locationInfo = null;
+                    }
+                    else if (locationInfo.MaxQty - locationInfo.CurrentQty < 1)
+                    {
+                        locationInfo = null;
+                    }
+                    if (locationInfo == null)
+                    {
+                        locationInfo = _basicService.LocationInfoService.GetLocationPallet();
+                    }
+                }
+                else
+                {
+                    locationInfo = _basicService.LocationInfoService.AssignLocation(ChargingStations);//鏌ユ壘绌挎杞﹀厖鐢佃揣浣�
+                }
+                if (locationInfo == null) locationInfo = _basicService.LocationInfoService.AssignLocation(2);//閲嶆柊鍒嗛厤璐т綅
+                if (locationInfo == null) throw new Exception($"鏃犲彲鍏ヨ揣浣�");
+                Dt_LocationInfo? locationInfo1 = null;
+                if (locationInfo.Row == 1)
+                {
+                    locationInfo1 = _basicService.LocationInfoService.Repository.QueryFirst(x => x.Row == 2 && x.Layer == locationInfo.Layer && x.Column == locationInfo.Column);
+                    locationInfo1.LocationStatus = LocationStatusEnum.Pallet.ObjToInt();
+                }
 
+                task.TaskState = (int)InTaskStatusEnum.SC_InExecuting;
+                task.CurrentAddress = task.NextAddress;
+                task.NextAddress = locationInfo.LocationCode;
+                task.TargetAddress = task.NextAddress;
+                task.TargetIsPickPlace = locationInfo.Row > 1 ? locationInfo.MaxQty - locationInfo.CurrentQty == 1 : false;
+                task.SourceIsPickPlace = false;
+                stockInfo.LocationCode = locationInfo.LocationCode;
+                stockInfo.StockStatus = StockStatusEmun.鍏ュ簱涓�.ObjToInt();
+                stockInfoDetail.Status = StockStatusEmun.鍏ュ簱涓�.ObjToInt();
+                locationInfo.CurrentQty++;
+                if (locationInfo.MaxQty < locationInfo.CurrentQty) throw new Exception();
+                locationInfo.EnableStatus = EnableStatusEnum.OnlyOut.ObjToInt();
+                locationInfo.LocationStatus = LocationStatusEnum.Inbounding.ObjToInt();
+
+                Db.Ado.BeginTran();
+                UpdateData(task);
+                _stockService.StockInfoService.Repository.UpdateData(stockInfo);
+                _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetail);
+                _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
+                if (locationInfo1 != null) _basicService.LocationInfoService.Repository.UpdateData(locationInfo1);
+                Db.Ado.CommitTran();
+
+                return WebResponseContent.Instance.OK(data: new ReceiveWMSInfo()
+                {
+                    TargetAddress = task.TargetAddress,
+                    SourceIsPickPlace = task.SourceIsPickPlace,
+                    TargetIsPickPlace = task.TargetIsPickPlace,
+                });
+            }
+            catch (Exception ex)
+            {
+                Db.Ado.RollbackTran();
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
     }
 }

--
Gitblit v1.9.3