From c383154f87b2da856668f7c66e3001d4f79ac7da Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期四, 09 一月 2025 09:27:25 +0800
Subject: [PATCH] 1

---
 代码管理/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/MesOutboundOrderService.cs |  131 ++++++++++++++++++++++++++++++++++---------
 1 files changed, 102 insertions(+), 29 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/MesOutboundOrderService.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/MesOutboundOrderService.cs"
index 3c67a27..fa7fd73 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/MesOutboundOrderService.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/MesOutboundOrderService.cs"
@@ -16,55 +16,128 @@
 using WIDESEA_IBasicRepository;
 using WIDESEA_Common.WareHouseEnum;
 using WIDESEA_Core;
+using WIDESEA_IStockService;
+using WIDESEA_IBasicService;
+using WIDESEA_Common.LocationEnum;
+using WIDESEA_IRecordService;
 
 namespace WIDESEA_OutboundService
 {
     public class MesOutboundOrderService : ServiceBase<Dt_MesOutboundOrder, IMesOutboundOrderRepository>, IMesOutboundOrderService
     {
         private readonly IBasicRepository _basicRepository;
+        private readonly IStockService _stockService;
+        private readonly IOutStockLockInfoService _outStockLockInfoService;
+        private readonly IBasicService _basicService;
+        private readonly IRecordService _recordService;
 
-        public MesOutboundOrderService(IMesOutboundOrderRepository BaseDal, IBasicRepository basicRepository) : base(BaseDal)
+        public IMesOutboundOrderRepository Repository => BaseDal;
+
+        public MesOutboundOrderService(IMesOutboundOrderRepository BaseDal, IBasicRepository basicRepository, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService) : base(BaseDal)
         {
             _basicRepository = basicRepository;
+            _stockService = stockService;
+            _outStockLockInfoService = outStockLockInfoService;
+            _basicService = basicService;
+            _recordService = recordService;
         }
 
-        public MesResponseContent SubstrateOut(SubstrateOutModel model)
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="mesOutboundOrder"></param>
+        /// <returns></returns>
+        /// <exception cref="Exception"></exception>
+        public (List<Dt_StockInfo>, Dt_MesOutboundOrder, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(Dt_MesOutboundOrder mesOutboundOrder)
+        {
+            List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
+
+            List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
+            List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
+
+            float originalNeedQuantity = mesOutboundOrder.OrderQuantity;
+
+            float needQuantity = originalNeedQuantity;
+
+            List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(mesOutboundOrder.MaterialCode, "", mesOutboundOrder.WarehouseId);
+            if (!stockInfos.Any())
+            {
+                throw new Exception($"鏈壘鍒板彲鍒嗛厤搴撳瓨");
+            }
+            List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, mesOutboundOrder.MaterialCode, needQuantity, out float residueQuantity);
+            mesOutboundOrder.LockQuantity += needQuantity - residueQuantity;
+            outStocks.AddRange(autoAssignStocks);
+            float assignQuantity = needQuantity - residueQuantity;
+
+            float orderQuantity = mesOutboundOrder.OrderQuantity;
+            for (int j = 0; j < autoAssignStocks.Count; j++)
+            {
+                float detailAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == mesOutboundOrder.MaterialCode).Sum(x => x.AssignQuantity);//鍑哄簱璁㈠崟鏄庣粏宸插垎閰嶆暟閲�
+
+                float palletAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == mesOutboundOrder.MaterialCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//鍑哄簱璇︽儏宸插垎閰嶆暟閲�
+
+                float palletOutboundQuantity = autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity);
+                if (palletAssignQuantity < palletOutboundQuantity)//濡傛灉鍑哄簱璇︽儏宸插垎閰嶆暟閲忓皬浜庢墭鐩樺凡鍒嗛厤鏁伴噺锛屽垯鍙互缁х画娣诲姞璇ユ墭鐩樺嚭搴撲俊鎭�
+                {
+                    float orderDetailNeedQuantity = mesOutboundOrder.OrderQuantity - detailAssignQuantity;
+                    if (orderDetailNeedQuantity > autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity)
+                    {
+                        mesOutboundOrder.LockQuantity += autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity;
+                        Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(mesOutboundOrder, autoAssignStocks[j], autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity);
+                        outStockLockInfos.Add(outStockLockInfo);
+                    }
+                    else
+                    {
+                        Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(mesOutboundOrder, autoAssignStocks[j], mesOutboundOrder.OrderQuantity - mesOutboundOrder.LockQuantity);
+                        outStockLockInfos.Add(outStockLockInfo);
+                        mesOutboundOrder.LockQuantity = mesOutboundOrder.OrderQuantity;
+                        break;
+                    }
+                }
+            }
+            locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList()));
+
+            return (outStocks, mesOutboundOrder, outStockLockInfos, locationInfos);
+        }
+
+        public WebResponseContent LockOutboundStockDataUpdate(List<Dt_StockInfo> stockInfos, List<Dt_OutStockLockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null)
         {
             try
             {
-                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA57.ToString());
-                if (warehouse == null)
+                _stockService.StockInfoService.Repository.UpdateData(stockInfos);
+                List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
+                foreach (var item in stockInfos)
                 {
-                    return MesResponseContent.Instance.Error($"浠撳簱鍩虹淇℃伅鏈厤缃�");
+                    stockInfoDetails.AddRange(item.Details);
+                }
+                _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails);
+
+                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);
                 }
 
-                Dt_MaterielInfo materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterielCode == model.MaterialCode);
-                if (materielInfo == null)
-                {
-                    return MesResponseContent.Instance.Error($"鏈壘鍒拌鐗╂枡淇℃伅");
-                }
-
-                Dt_MesOutboundOrder mesOutboundOrder = new Dt_MesOutboundOrder()
-                {
-                    CreateType = OrderCreateTypeEnum.UpperSystemPush.ObjToInt(),
-                    Line = model.Line,
-                    MaterialCode = model.MaterialCode,
-                    MaterialName = model.MaterialName,
-                    OrderQuantity = model.RequiredQuantity,
-                    TaskNo = model.TaskNo,
-                    Unit = model.Unit,
-                    OrderType = MesOutboundOrderTypeEnum.SubstrateOut.ObjToInt(),
-                    OrderStatus = OutOrderStatusEnum.鏈紑濮�.ObjToInt(),
-                    WarehouseId = warehouse.WarehouseId
-                };
-
-                BaseDal.AddData(mesOutboundOrder);
-
-                return MesResponseContent.Instance.OK();
+                _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 MesResponseContent.Instance.Error(ex.Message);
+                return WebResponseContent.Instance.Error(ex.Message);
             }
         }
     }

--
Gitblit v1.9.3