From 844c7c3ce2c39139490a6ecb0f35170f6cade290 Mon Sep 17 00:00:00 2001
From: wankeda <Administrator@DESKTOP-HAU3ST3>
Date: 星期二, 28 四月 2026 16:31:54 +0800
Subject: [PATCH] 1

---
 WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Service/NewOutboundOrderDetailService.cs |  238 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 238 insertions(+), 0 deletions(-)

diff --git a/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Service/NewOutboundOrderDetailService.cs b/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Service/NewOutboundOrderDetailService.cs
new file mode 100644
index 0000000..8690286
--- /dev/null
+++ b/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/Service/NewOutboundOrderDetailService.cs
@@ -0,0 +1,238 @@
+锘縰sing OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using WIDESEA_Core;
+using WIDESEA_Core.BaseRepository;
+using WIDESEA_Core.BaseServices;
+using WIDESEA_Core.Enums;
+using WIDESEA_Core.Helper;
+using WIDESEA_DTO.ERP;
+using WIDESEA_DTO.Stock;
+using WIDESEA_IBasicRepository;
+using WIDESEA_IBasicService;
+using WIDESEA_IOutboundRepository;
+using WIDESEA_IOutboundService;
+using WIDESEA_IRecordService;
+using WIDESEA_IStockRepository;
+using WIDESEA_IStockService;
+using WIDESEA_Model.Models;
+using WIDESEA_OutboundRepository;
+using WIDESEA_StockRepository;
+
+namespace WIDESEA_OutboundService
+{
+    public partial class NewOutboundOrderDetailService : ServiceBase<Dt_NewOutboundOrderDetail, INewOutboundOrderDetailRepository>, INewOutboundOrderDetailService
+    {
+
+
+        /// <summary>
+        /// 鎴愬搧鍒嗛厤
+        /// </summary>
+        /// <param name="outboundOrderDetails"></param>
+        /// <returns></returns>
+        /// <exception cref="Exception"></exception>
+        public (List<Dt_StockInfo>, List<Dt_NewOutboundOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) CPAssignStockOutbound(List<Dt_NewOutboundOrderDetail> outboundOrderDetails)
+        {
+            if (!outboundOrderDetails.Any())
+            {
+                throw new Exception($"鏈壘鍒板嚭搴撳崟鏄庣粏淇℃伅");
+            }
+
+            if (outboundOrderDetails.GroupBy(x => x.OrderId).Count() > 1)
+            {
+                throw new Exception($"璇峰嬁鍚屾椂鎿嶄綔澶氫釜鍗曟嵁鏄庣粏");
+            }
+            Dt_NewOutboundOrder outboundOrder = _outboundRepository.NewOutboundOrderRepository.QueryFirst(x => x.Id == outboundOrderDetails.FirstOrDefault().OrderId);
+            List<Dt_NewOutboundOrderDetail> dt_OutboundOrderDetails = _outboundRepository.NewOutboundOrderDetailRepository.QueryData(x => x.OrderId == outboundOrder.Id);
+            List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
+            List<Dt_NewOutboundOrderDetail> groupDetails = outboundOrderDetails.GroupBy(x => new { x.MaterielCode, x.LocationName }).Select(x => new Dt_NewOutboundOrderDetail { OrderQuantity = x.Sum(v => v.OrderQuantity) - x.Sum(v => v.LockQuantity), MaterielCode = x.Key.MaterielCode, LocationName = x.Key.LocationName }).ToList();
+            List<Dt_Warehouse> warehouse = new List<Dt_Warehouse>();
+
+            warehouse = _basicService.WarehouseService.Repository.QueryData(x => x.WarehouseDes == outboundOrder.OutWareHouse);
+
+            List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
+            List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
+            List<string> PalletCodes = new List<string>();
+            foreach (var groupDetail in groupDetails)
+            {
+                Dt_StockInfo dt_StockInfo = new Dt_StockInfo();
+                List<Dt_StockInfoDetail> dt_StockInfoDetails = _stockService.StockInfoDetailService.Repository.QueryData(x => x.MaterielCode == groupDetail.MaterielCode);
+                foreach (var stockInfoDetail in dt_StockInfoDetails)
+                {
+                    dt_StockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.Id == stockInfoDetail.StockId && x.StockStatus == StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt());
+                    if (dt_StockInfo != null && !PalletCodes.Any(x => x == dt_StockInfo.PalletCode))
+                    {
+                        PalletCodes.Add(dt_StockInfo.PalletCode);
+                    }
+                }
+
+                if (!PalletCodes.Any())
+                {
+                    throw new Exception($"鏈壘鍒板彲鍒嗛厤搴撳瓨");
+                }
+
+            }
+            foreach (var PalletCode in PalletCodes.Distinct())
+            {
+                List<Dt_StockInfo> stockInfos = new List<Dt_StockInfo>();
+                if (outboundOrderDetails.All(x => x.OrderQuantity == x.LockQuantity)) break;
+                foreach (var item in groupDetails)
+                {
+                    if (item.OrderQuantity == item.LockQuantity) continue;
+                    ///鍑哄簱鎸囧畾搴撲綅鍑哄簱鍒ゆ柇
+                    if (item.LocationName != null && item.LocationName != "")
+                    {
+                        //stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, warehouse).Where(x => x.LocationCode == item.LocationName).ToList();
+                        List<Dt_StockInfo> nowStockInfos = Db.Queryable<Dt_StockInfo>()
+                            .Where(x => x.PalletCode == PalletCode && x.Details.Any(v => v.MaterielCode == item.MaterielCode) && x.LocationCode == item.LocationName)
+                            .Includes(x => x.Details).ToList();
+                        if (stockInfos.Count() == 0 && nowStockInfos.Count() > 0)
+                        {
+                            stockInfos.AddRange(nowStockInfos);
+                        }
+                    }
+                    else
+                    {
+                        //stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, warehouse);
+                        List<Dt_StockInfo> nowStockInfos = Db.Queryable<Dt_StockInfo>()
+                            .Where(x => x.PalletCode == PalletCode && x.Details.Any(v => v.MaterielCode == item.MaterielCode))
+                            .Includes(x => x.Details).ToList();
+                        if (stockInfos.Count() == 0 && nowStockInfos.Count() > 0)
+                        {
+                            stockInfos.AddRange(nowStockInfos);
+                        }
+                    }
+
+                }
+                if (stockInfos.Count() == 0)
+                {
+                    continue;
+                }
+                List<Dt_StockInfo> autoAssignStocks = new List<Dt_StockInfo>();
+                List<IStockInfoService.residueQuantity> newResidueQuantitys = new List<IStockInfoService.residueQuantity>();
+                autoAssignStocks = _stockService.StockInfoService.CPGetOutboundStocks(stockInfos, dt_OutboundOrderDetails, out List<IStockInfoService.residueQuantity> residueQuantitys);
+                newResidueQuantitys.AddRange(residueQuantitys);
+                outStocks.AddRange(autoAssignStocks);
+                foreach (var residueQuantity in newResidueQuantitys)
+                {
+                    foreach (var item in dt_OutboundOrderDetails)
+                    {
+                        if (residueQuantity.MaterielCode == item.MaterielCode)
+                        {
+                            decimal originalNeedQuantity = item.OrderQuantity;
+
+                            decimal needQuantity = originalNeedQuantity - item.LockQuantity;
+
+                            //item.LockQuantity += needQuantity - residueQuantity.NewNeendQuantity;
+
+                            decimal assignQuantity = needQuantity - residueQuantity.NewNeendQuantity;
+
+                            List<Dt_NewOutboundOrderDetail> details = dt_OutboundOrderDetails.Where(x => x.MaterielCode == item.MaterielCode).ToList();
+
+                            for (int i = 0; i < details.Count; i++)
+                            {
+                                decimal orderQuantity = details[i].OrderQuantity;
+                                for (int j = 0; j < autoAssignStocks.Count; j++)
+                                {
+                                    decimal detailAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == item.MaterielCode && x.OrderDetailId == details[i].Id).Sum(x => x.AssignQuantity);//鍑哄簱璁㈠崟鏄庣粏宸插垎閰嶆暟閲�
+
+                                    decimal palletAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == item.MaterielCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//鍑哄簱璇︽儏宸插垎閰嶆暟閲�
+                                    if (string.IsNullOrEmpty(item.BatchNo))
+                                    {
+                                        palletAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == item.MaterielCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//鍑哄簱璇︽儏宸插垎閰嶆暟閲�
+                                    }
+                                    decimal palletOutboundQuantity = 0;
+
+                                    palletOutboundQuantity = autoAssignStocks[j].Details.Where(x => x.MaterielCode == item.MaterielCode).Sum(x => x.OutboundQuantity);
+
+                                    if (palletAssignQuantity < palletOutboundQuantity)//濡傛灉鍑哄簱璇︽儏宸插垎閰嶆暟閲忓皬浜庢墭鐩樺凡鍒嗛厤鏁伴噺锛屽垯鍙互缁х画娣诲姞璇ユ墭鐩樺嚭搴撲俊鎭�
+                                    {
+                                        decimal orderDetailNeedQuantity = details[i].OrderQuantity - detailAssignQuantity;
+                                        if (orderDetailNeedQuantity > autoAssignStocks[j].Details.Where(x => x.MaterielCode == details[i].MaterielCode).Sum(v => v.OutboundQuantity) - palletAssignQuantity)
+                                        {
+                                            //details[i].LockQuantity += autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity;
+                                            Dt_StockInfoDetail dt_StockInfoDetail = new Dt_StockInfoDetail();
+                                            foreach (var autoStockDestail in autoAssignStocks[j].Details)
+                                            {
+                                                if (autoStockDestail.MaterielCode == details[i].MaterielCode)
+                                                {
+                                                    dt_StockInfoDetail = autoStockDestail;
+                                                }
+                                            }
+                                            Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outboundOrder, details[i], autoAssignStocks[j], dt_StockInfoDetail.OutboundQuantity);
+
+
+                                            outStockLockInfos.Add(outStockLockInfo);
+                                            details[i].LockQuantity += outStockLockInfo.AssignQuantity;
+                                            break;
+                                        }
+                                        else
+                                        {
+                                            Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outboundOrder, details[i], autoAssignStocks[j], details[i].OrderQuantity - details[i].LockQuantity);
+                                            outStockLockInfos.Add(outStockLockInfo);
+                                            details[i].LockQuantity = details[i].OrderQuantity;
+                                            break;
+                                        }
+
+                                    }
+                                }
+                            }
+                            locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(autoAssignStocks.Select(x => x.LocationCode).ToList()));
+                        }
+                    }
+
+                }
+                newResidueQuantitys = new List<IStockInfoService.residueQuantity>();
+            }
+            return (outStocks, dt_OutboundOrderDetails, outStockLockInfos, locationInfos);
+        }
+
+
+        public WebResponseContent CPLockOutboundStockDataUpdate(List<Dt_StockInfo> stockInfos, List<Dt_NewOutboundOrderDetail> 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())
+                {
+                    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);
+                }
+
+                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfos, locationStatus.ObjToInt(), StockChangeType.Outbound.ObjToInt(), "", 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