|  |  | 
 |  |  | using WIDESEA_IBasicRepository; | 
 |  |  | using WIDESEA_Common.WareHouseEnum; | 
 |  |  | using WIDESEA_Core; | 
 |  |  | using WIDESEA_IStockService; | 
 |  |  | using WIDESEA_IBasicService; | 
 |  |  | using WIDESEA_Common.LocationEnum; | 
 |  |  | using WIDESEA_IRecordService; | 
 |  |  | using WIDESEA_Common.StockEnum; | 
 |  |  |  | 
 |  |  | 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) | 
 |  |  |                 stockInfos.ForEach(x => { | 
 |  |  |                     x.StockStatus = StockStatusEmun.åºåºéå®.ObjToInt(); | 
 |  |  |                 }); | 
 |  |  |                 _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); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } |