From 2b25f973bb6d72ce6971d6f9c3cdccf51b7962ab Mon Sep 17 00:00:00 2001
From: wangxinhui <wangxinhui@hnkhzn.com>
Date: 星期六, 06 九月 2025 14:27:08 +0800
Subject: [PATCH] Merge branch 'master' of http://115.159.85.185:8098/r/ZhiHuiQiCe/LongDeLiLiKu

---
 项目代码/WMS/WMSServices/WIDESEA_OutboundService/OutBSTOrderDetailService.cs |  166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 166 insertions(+), 0 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WMSServices/WIDESEA_OutboundService/OutBSTOrderDetailService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WMSServices/WIDESEA_OutboundService/OutBSTOrderDetailService.cs"
new file mode 100644
index 0000000..1e12beb
--- /dev/null
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WMSServices/WIDESEA_OutboundService/OutBSTOrderDetailService.cs"
@@ -0,0 +1,166 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using WIDESEA_Common.LocationEnum;
+using WIDESEA_Common.WareHouseEnum;
+using WIDESEA_Core;
+using WIDESEA_Core.BaseServices;
+using WIDESEA_IBasicRepository;
+using WIDESEA_IBasicService;
+using WIDESEA_IOutboundRepository;
+using WIDESEA_IOutboundService;
+using WIDESEA_IRecordService;
+using WIDESEA_IStockService;
+using WIDESEA_Model.Models;
+using WIDESEA_OutboundRepository;
+
+namespace WIDESEA_OutboundService
+{
+    public class OutBSTOrderDetailService : ServiceBase<Dt_OutBSTOrderDetail, IOutBSTOrderDetailRepository>, IOutBSTOrderDetailService
+    {
+        public IOutBSTOrderDetailRepository Repository => BaseDal;
+        private IOutBSTOrderRepository _outBSTOrderRepository;
+        private IBasicRepository _basicRepository;
+        private IStockService _stockService;
+        private IOutStockLockInfoService _outStockLockInfoService;
+        private IBasicService _basicService;
+        private ILocationStatusChangeRecordService _locationStatusChangeRecordService;
+
+        public OutBSTOrderDetailService(IOutBSTOrderDetailRepository BaseDal, IOutBSTOrderRepository outBSTOrderRepository,IBasicRepository basicRepository, IStockService stockService, IOutStockLockInfoService outStockLockInfoService,IBasicService basicService, ILocationStatusChangeRecordService locationStatusChangeRecordService) : base(BaseDal)
+        {
+            _outBSTOrderRepository = outBSTOrderRepository;
+            _basicRepository = basicRepository;
+            _stockService = stockService;
+            _outStockLockInfoService = outStockLockInfoService;
+            _basicService = basicService;
+            _locationStatusChangeRecordService=locationStatusChangeRecordService;
+        }
+        /// <summary>
+        /// 鍒嗛厤搴撳瓨
+        /// </summary>
+        public (List<Dt_StockInfo>, List<Dt_OutBSTOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(List<Dt_OutBSTOrderDetail> outboundOrderDetails)
+        {
+            if (!outboundOrderDetails.Any())
+            {
+                throw new Exception($"鏈壘鍒板嚭搴撳崟鏄庣粏淇℃伅");
+            }
+
+            if (outboundOrderDetails.GroupBy(x => x.OutBSTOrderId).Count() > 1)
+            {
+                throw new Exception($"璇峰嬁鍚屾椂鎿嶄綔澶氫釜鍗曟嵁鏄庣粏");
+            }
+            //鑾峰彇鎺掔▼涓昏〃
+            Dt_OutBSTOrder outBSTOrder = _outBSTOrderRepository.QueryFirst(x => x.Id == outboundOrderDetails.FirstOrDefault().OutBSTOrderId);
+            List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
+            //瀵瑰嚭搴撶墿鏂欒繘琛屽垎缁�
+            List<Dt_OutBSTOrderDetail> groupDetails = outboundOrderDetails.GroupBy(x => new { x.MaterialId,x.Width }).Select(x => 
+            new Dt_OutBSTOrderDetail { 
+                XqLen = x.Sum(v => v.XqLen),
+                MaterialId = x.Key.MaterialId,
+                Width=x.Key.Width
+            }
+            ).ToList();
+            Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == outBSTOrder.WarehouseId);
+            //鍑哄簱璇︽儏
+            List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
+            //璐т綅瀛樺偍
+            List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
+
+            foreach (var item in groupDetails)
+            {
+                decimal needQuantity = item.XqLen;
+                //鑾峰彇鍙敤搴撳瓨
+                List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterialId,item.Width, outBSTOrder.WarehouseId);
+                if (!stockInfos.Any())
+                {
+                    throw new Exception($"鏈壘鍒板彲鍒嗛厤搴撳瓨");
+                }
+                //鍒嗛厤瀹為檯搴撳瓨
+                List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos,  needQuantity).OrderBy(x => x.StockLength - x.StockOutLength).ToList();
+                //娣诲姞搴撳瓨鍒嗛厤
+                outStocks.AddRange(autoAssignStocks);
+                //鑾峰彇鎵�鏈夎鐗╂枡鍗曟嵁
+                List<Dt_OutBSTOrderDetail> details = outboundOrderDetails.Where(x => x.MaterialId == item.MaterialId).ToList();
+
+                for (int i = 0; i < details.Count; i++)
+                {
+                    //璁㈠崟鏁伴噺
+                    decimal orderQuantity = details[i].XqLen;
+
+                    for (int j = 0; j < autoAssignStocks.Count; j++)
+                    {
+                        //鍑哄簱璁㈠崟鏄庣粏宸插垎閰嶆暟閲�
+                        decimal detailAssignQuantity = outStockLockInfos.Where(x => x.OrderDetailId == details[i].Id).Sum(x => x.AssignQuantity);
+
+                        decimal orderDetailNeedQuantity = details[i].XqLen - detailAssignQuantity;
+
+                        decimal useStockLength = autoAssignStocks[j].StockLength;
+
+                        if (orderDetailNeedQuantity > useStockLength)
+                        {
+                            //鐢熸垚璇︽儏
+                            Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outBSTOrder, details[i], autoAssignStocks[j], useStockLength);
+                            outStockLockInfos.Add(outStockLockInfo);
+                            details[i].AssignTotalUsage += useStockLength;
+
+                        }
+                        else
+                        {
+                            //鐢熸垚璇︽儏
+                            Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outBSTOrder, details[i], autoAssignStocks[j], orderDetailNeedQuantity);
+                            outStockLockInfos.Add(outStockLockInfo);
+                            details[i].AssignTotalUsage = orderQuantity;
+                            break;
+                        }
+                    }
+                }
+
+                locationInfos.AddRange(_basicRepository.LocationInfoRepository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList()));
+            }
+
+            return (outStocks, outboundOrderDetails, outStockLockInfos, locationInfos);
+        }
+        /// <summary>
+        /// 鍑哄簱搴撳瓨鍒嗛厤鍚庯紝鏇存柊鏁版嵁搴撴暟鎹�
+        /// </summary>
+        public WebResponseContent LockOutboundStockDataUpdate(List<Dt_StockInfo> stockInfos, List<Dt_OutBSTOrderDetail> outboundOrderDetails, List<Dt_OutStockLockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null)
+        {
+            try
+            {
+                _stockService.StockInfoService.Repository.UpdateData(stockInfos);
+                BaseDal.UpdateData(outboundOrderDetails);
+
+                List<Dt_OutStockLockInfo> addOutStockLockInfos = outStockLockInfos.Where(x => x.Id == 0).ToList();
+                if (addOutStockLockInfos != null && addOutStockLockInfos.Any())
+                {
+                    if (tasks != null)
+                    {
+                        addOutStockLockInfos.ForEach(x =>
+                        {
+                            x.TaskNum = tasks.FirstOrDefault(v => v.PalletCode == x.PalletCode)?.TaskNum;
+                        });
+                    }
+
+                    _outStockLockInfoService.Repository.AddData(addOutStockLockInfos);
+                }
+                List<Dt_OutStockLockInfo> updateOutStockLockInfos = outStockLockInfos.Where(x => x.Id > 0).ToList();
+                if (updateOutStockLockInfos != null && updateOutStockLockInfos.Any())
+                {
+                    _outStockLockInfoService.Repository.UpdateData(updateOutStockLockInfos);
+                }
+                //娣诲姞璐т綅鐘舵��
+                _locationStatusChangeRecordService.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);
+            }
+        }
+
+    }
+}

--
Gitblit v1.9.3