From e7cf443b37f8f4d8a1bc4fe4cd6f058f39e5c7f5 Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期六, 14 十二月 2024 01:15:03 +0800
Subject: [PATCH] 更新

---
 代码管理/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs |  343 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 342 insertions(+), 1 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs"
index e7babd1..60c1462 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs"
@@ -5,6 +5,9 @@
 using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
+using WIDESEA_Common.LocationEnum;
+using WIDESEA_Common.OrderEnum;
+using WIDESEA_Common.StockEnum;
 using WIDESEA_Core;
 using WIDESEA_Core.BaseRepository;
 using WIDESEA_Core.BaseServices;
@@ -19,6 +22,7 @@
 using WIDESEA_IStockRepository;
 using WIDESEA_IStockService;
 using WIDESEA_Model.Models;
+using WIDESEA_OutboundRepository;
 using WIDESEA_StockRepository;
 
 namespace WIDESEA_OutboundService
@@ -26,12 +30,349 @@
     public partial class OutboundOrderDetailService : ServiceBase<Dt_OutboundOrderDetail, IOutboundOrderDetailRepository>, IOutboundOrderDetailService
     {
         private readonly IUnitOfWorkManage _unitOfWorkManage;
+        private readonly IStockService _stockService;
+        private readonly IOutboundRepository _outboundRepository;
+        private readonly IOutStockLockInfoService _outStockLockInfoService;
+        private readonly IBasicService _basicService;
+        private readonly IRecordService _recordService;
 
         public IOutboundOrderDetailRepository Repository => BaseDal;
 
-        public OutboundOrderDetailService(IOutboundOrderDetailRepository BaseDal, IUnitOfWorkManage unitOfWorkManage) : base(BaseDal)
+        public OutboundOrderDetailService(IOutboundOrderDetailRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockService stockService, IOutboundRepository outboundRepository, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService) : base(BaseDal)
         {
             _unitOfWorkManage = unitOfWorkManage;
+            _stockService = stockService;
+            _outboundRepository = outboundRepository;
+            _outStockLockInfoService = outStockLockInfoService;
+            _basicService = basicService;
+            _recordService = recordService;
+        }
+
+        public WebResponseContent LockOutboundStock(int orderDetailId)
+        {
+            Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == orderDetailId);
+            (bool, string) result = CheckDeital(outboundOrderDetail);
+            if (!result.Item1) return WebResponseContent.Instance.Error(result.Item2);
+            Dt_OutboundOrder outboundOrder = _outboundRepository.OutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetail.OrderId);
+
+            float needQuantity = outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity;
+
+            List<Dt_StockInfo> outStock = new List<Dt_StockInfo>();
+
+            List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(outboundOrderDetail.MaterielCode, outboundOrder.WarehouseId);
+            float stockTotalQuantity = stockInfos.Select(x => x.Details.Sum(v => v.StockQuantity - v.OutboundQuantity)).Sum(x => x);
+            if (stockTotalQuantity >= needQuantity)//搴撳瓨澶�
+            {
+                int index = 0;
+                while (needQuantity > 0)
+                {
+                    Dt_StockInfo stockInfo = stockInfos[index];
+                    float useableStockQuantity = stockInfo.Details.Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode).Sum(x => x.StockQuantity - x.OutboundQuantity);
+                    if (useableStockQuantity < needQuantity)
+                    {
+                        outboundOrderDetail.LockQuantity += useableStockQuantity;
+                        stockInfo.Details.ForEach(x => x.OutboundQuantity = x.StockQuantity);
+                        needQuantity -= useableStockQuantity;
+                    }
+                    else
+                    {
+                        outboundOrderDetail.LockQuantity += needQuantity;
+                        stockInfo.Details.ForEach(x =>
+                        {
+                            if (x.StockQuantity > x.OutboundQuantity && x.MaterielCode == outboundOrderDetail.MaterielCode)
+                            {
+                                if (x.StockQuantity - x.OutboundQuantity >= needQuantity)
+                                {
+                                    x.OutboundQuantity += needQuantity;
+                                    needQuantity = 0;
+                                }
+                                else
+                                {
+                                    needQuantity -= (x.StockQuantity - x.OutboundQuantity);
+                                    x.OutboundQuantity = x.StockQuantity;
+                                }
+                            }
+                        });
+                    }
+                    outStock.Add(stockInfo);
+                    index++;
+                }
+                return WebResponseContent.Instance.OK(data: new { stockInfos, outboundOrderDetail });
+            }
+            else
+            {
+                for (int i = 0; i < stockInfos.Count; i++)
+                {
+                    Dt_StockInfo stockInfo = stockInfos[i];
+                    float useableStockQuantity = stockInfo.Details.Where(x => x.MaterielCode == outboundOrderDetail.MaterielCode).Sum(x => x.StockQuantity - x.OutboundQuantity);
+                    if (useableStockQuantity < needQuantity)
+                    {
+                        outboundOrderDetail.LockQuantity += useableStockQuantity;
+                        stockInfo.Details.ForEach(x => x.OutboundQuantity = x.StockQuantity);
+                        needQuantity -= useableStockQuantity;
+                    }
+                    else
+                    {
+                        outboundOrderDetail.LockQuantity += needQuantity;
+                        stockInfo.Details.ForEach(x =>
+                        {
+                            if (x.StockQuantity > x.OutboundQuantity && x.MaterielCode == outboundOrderDetail.MaterielCode)
+                            {
+                                if (x.StockQuantity - x.OutboundQuantity >= needQuantity)
+                                {
+                                    x.OutboundQuantity += needQuantity;
+                                    needQuantity = 0;
+                                }
+                                else
+                                {
+                                    needQuantity -= (x.StockQuantity - x.OutboundQuantity);
+                                    x.OutboundQuantity = x.StockQuantity;
+                                }
+                            }
+                        });
+                    }
+                    outStock.Add(stockInfo);
+                }
+                return WebResponseContent.Instance.OK(data: new { stockInfos, outboundOrderDetail });
+            }
+        }
+
+        private (bool, string) CheckDeital(Dt_OutboundOrderDetail outboundOrderDetail)
+        {
+            if (outboundOrderDetail == null)
+            {
+                return (false, "鏈壘鍒板嚭搴撳崟鏄庣粏淇℃伅");
+            }
+            if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt())
+            {
+                return (false, "璇ユ槑缁嗕笉鍙搷浣�");
+            }
+            return (true, "鎴愬姛");
+        }
+
+        public (List<Dt_StockInfo>, Dt_OutboundOrderDetail, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(Dt_OutboundOrderDetail outboundOrderDetail, List<StockSelectViewDTO> stockSelectViews)
+        {
+            (bool, string) checkResult = CheckSelectStockDeital(outboundOrderDetail, stockSelectViews);
+            if (!checkResult.Item1) throw new Exception(checkResult.Item2);
+
+            Dt_OutboundOrder outboundOrder = _outboundRepository.OutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetail.OrderId);
+            float originalNeedQuantity = outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity;
+
+            float needQuantity = originalNeedQuantity;
+
+            List<Dt_StockInfo> outStocks = _stockService.StockInfoService.Repository.GetStockInfosByPalletCodes(stockSelectViews.Select(x => x.PalletCode).ToList());
+            float assignQuantity = 0;
+            outStocks.ForEach(x =>
+            {
+                x.Details.ForEach(v =>
+                {
+                    assignQuantity += v.StockQuantity - v.OutboundQuantity;
+                });
+            });
+
+            outboundOrderDetail.LockQuantity += assignQuantity;
+            outStocks.ForEach(x =>
+            {
+                x.Details.ForEach(v =>
+                {
+                    v.OutboundQuantity = v.StockQuantity;
+                });
+            });
+            needQuantity -= assignQuantity;
+            if (outboundOrderDetail.OrderQuantity > outboundOrderDetail.LockQuantity)
+            {
+                List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(outboundOrderDetail.MaterielCode, outboundOrder.WarehouseId);
+                stockInfos = stockInfos.Where(x => !stockSelectViews.Select(v => v.PalletCode).Contains(x.PalletCode)).ToList();
+                List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, outboundOrderDetail.MaterielCode, needQuantity, out float residueQuantity);
+                outboundOrderDetail.LockQuantity += needQuantity - residueQuantity;
+                outStocks.AddRange(autoAssignStocks);
+                outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.AssignOver.ObjToInt();
+                if (residueQuantity > 0)
+                {
+                    outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.AssignOverPartial.ObjToInt();
+                }
+            }
+
+            List<Dt_OutStockLockInfo> outStockLockInfos = _outStockLockInfoService.GetOutStockLockInfos(outboundOrder, outboundOrderDetail, outStocks);
+
+            List<Dt_LocationInfo> locationInfos = _basicService.LocationInfoService.Repository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList());
+
+            return (outStocks, outboundOrderDetail, outStockLockInfos, locationInfos);
+        }
+
+        public WebResponseContent LockOutboundStock(int orderDetailId, List<StockSelectViewDTO> stockSelectViews)
+        {
+            try
+            {
+                Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == orderDetailId);
+                (bool, string) checkResult = CheckSelectStockDeital(outboundOrderDetail, stockSelectViews);
+                if (!checkResult.Item1) throw new Exception(checkResult.Item2);
+
+                (List<Dt_StockInfo>, Dt_OutboundOrderDetail, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) result = AssignStockOutbound(outboundOrderDetail, stockSelectViews);
+
+                _unitOfWorkManage.BeginTran();
+                WebResponseContent content = LockOutboundStockDataUpdate(result.Item1, new List<Dt_OutboundOrderDetail> { result.Item2 }, result.Item3, result.Item4);
+                if (content.Status)
+                {
+                    _unitOfWorkManage.CommitTran();
+                }
+                else
+                {
+                    _unitOfWorkManage.RollbackTran();
+                }
+                return content;
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
+
+        public WebResponseContent LockOutboundStock(int[] keys)
+        {
+            try
+            {
+                List<StockSelectViewDTO> stockSelectViews = new List<StockSelectViewDTO>();
+                List<Dt_StockInfo> stockInfos = new List<Dt_StockInfo>();
+                List<Dt_OutboundOrderDetail> outboundOrderDetails = new List<Dt_OutboundOrderDetail>();
+                List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
+                List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
+                foreach (var item in keys)
+                {
+                    Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == item);
+                    (bool, string) checkResult = CheckSelectStockDeital(outboundOrderDetail, stockSelectViews);
+                    if (!checkResult.Item1) throw new Exception(checkResult.Item2);
+
+                    (List<Dt_StockInfo>, Dt_OutboundOrderDetail, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) result = AssignStockOutbound(outboundOrderDetail, stockSelectViews);
+                    if (result.Item1.Count > 0)
+                    {
+                        stockInfos.AddRange(result.Item1);
+                        outboundOrderDetails.Add(result.Item2);
+                        outStockLockInfos.AddRange(result.Item3);
+                        locationInfos.AddRange(result.Item4);
+                    }
+                }
+
+                _unitOfWorkManage.BeginTran();
+                WebResponseContent content = LockOutboundStockDataUpdate(stockInfos, outboundOrderDetails, outStockLockInfos, locationInfos);
+                if (content.Status)
+                {
+                    _unitOfWorkManage.CommitTran();
+                }
+                else
+                {
+                    _unitOfWorkManage.RollbackTran();
+                }
+                return content;
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
+
+        public WebResponseContent LockOutboundStockDataUpdate(List<Dt_StockInfo> stockInfos, List<Dt_OutboundOrderDetail> outboundOrderDetails, List<Dt_OutStockLockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null)
+        {
+            try
+            {
+                _stockService.StockInfoService.Repository.UpdateData(stockInfos);
+                List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
+                foreach (var item in stockInfos)
+                {
+                    stockInfoDetails.AddRange(item.Details);
+                }
+                _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails);
+                BaseDal.UpdateData(outboundOrderDetails);
+
+                List<Dt_OutStockLockInfo> addOutStockLockInfos = outStockLockInfos.Where(x => x.Id == 0).ToList();
+                if (addOutStockLockInfos != null && addOutStockLockInfos.Any())
+                {
+                    _outStockLockInfoService.Repository.AddData(addOutStockLockInfos);
+                }
+                List<Dt_OutStockLockInfo> updateOutStockLockInfos = outStockLockInfos.Where(x => x.Id > 0).ToList();
+                if (updateOutStockLockInfos != null && updateOutStockLockInfos.Any())
+                {
+                    _outStockLockInfoService.Repository.UpdateData(updateOutStockLockInfos);
+                }
+
+                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfos, locationStatus, LocationChangeType.OutboundAssignLocation, "", tasks?.Select(x => x.TaskNum).ToList());
+                _basicService.LocationInfoService.Repository.UpdateLocationStatus(locationInfos, locationStatus);
+                return WebResponseContent.Instance.OK();
+            }
+            catch (Exception ex)
+            {
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
+
+        private (bool, string) CheckSelectStockDeital(Dt_OutboundOrderDetail outboundOrderDetail, List<StockSelectViewDTO> stockSelectViews)
+        {
+            if (outboundOrderDetail == null)
+            {
+                return (false, "鏈壘鍒板嚭搴撳崟鏄庣粏淇℃伅");
+            }
+            if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt() && outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt())
+            {
+                return (false, "璇ユ槑缁嗕笉鍙搷浣�");
+            }
+            if (stockSelectViews.Sum(x => x.UseableQuantity) > outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity)
+            {
+                return (false, "閫夋嫨鏁伴噺瓒呭嚭鍗曟嵁鏁伴噺");
+            }
+            return (true, "鎴愬姛");
+        }
+
+        public WebResponseContent RevokeLockOutboundStock(int orderDetailId)
+        {
+            Dt_OutboundOrderDetail outboundOrderDetail = BaseDal.QueryFirst(x => x.Id == orderDetailId);
+            (bool, string) result = CheckRevoke(outboundOrderDetail);
+            if (!result.Item1) return WebResponseContent.Instance.Error(result.Item2);
+
+            List<Dt_OutStockLockInfo> outStockLockInfos = _outStockLockInfoService.GetByOrderDetailId(orderDetailId, OutLockStockStatusEnum.宸插垎閰�);
+            if (outStockLockInfos.Count > 0)
+            {
+                List<Dt_StockInfo> stocks = _stockService.StockInfoService.Repository.GetStockInfosByPalletCodes(outStockLockInfos.Select(x => x.PalletCode).ToList());
+                if (stocks.Count > 0)
+                {
+                    stocks.ForEach(x =>
+                    {
+                        x.Details.ForEach(v =>
+                        {
+                            v.OutboundQuantity = 0;
+                        });
+                    });
+
+                    outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt();
+                    outboundOrderDetail.LockQuantity = outboundOrderDetail.OverOutQuantity;
+
+                    outStockLockInfos.ForEach(x =>
+                    {
+                        x.Status = OutLockStockStatusEnum.鎾ら攢.ObjToInt();
+                    });
+
+                    List<Dt_LocationInfo> locationInfos = _basicService.LocationInfoService.Repository.GetLocationInfos(stocks.Select(x => x.LocationCode).ToList());
+
+                    return LockOutboundStockDataUpdate(stocks, new List<Dt_OutboundOrderDetail> { outboundOrderDetail }, outStockLockInfos, locationInfos, LocationStatusEnum.InStock);
+                }
+                return WebResponseContent.Instance.Error("鏈壘鍒板簱瀛樹俊鎭�");
+            }
+            return WebResponseContent.Instance.Error("鏈壘鍒板凡鍒嗛厤鐨勫嚭搴撲俊鎭�");
+
+        }
+
+        private (bool, string) CheckRevoke(Dt_OutboundOrderDetail outboundOrderDetail)
+        {
+            if (outboundOrderDetail == null)
+            {
+                return (false, "鏈壘鍒板嚭搴撳崟鏄庣粏淇℃伅");
+            }
+            if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt() && outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt() && outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.AssignOver.ObjToInt())
+            {
+                return (false, "璇ユ槑缁嗕笉鍙搷浣�");
+            }
+            return (true, "鎴愬姛");
         }
     }
 }

--
Gitblit v1.9.3