|  |  |  | 
|---|
|  |  |  | using System.Linq; | 
|---|
|  |  |  | using System.Text; | 
|---|
|  |  |  | using System.Threading.Tasks; | 
|---|
|  |  |  | using WIDESEA_Common.LocationEnum; | 
|---|
|  |  |  | using WIDESEA_Core; | 
|---|
|  |  |  | using WIDESEA_Core.BaseServices; | 
|---|
|  |  |  | using WIDESEA_IBasicRepository; | 
|---|
|  |  |  | using WIDESEA_IBasicService; | 
|---|
|  |  |  | 
|---|
|  |  |  | private readonly IOutStockLockInfoService _outStockLockInfoService; | 
|---|
|  |  |  | private readonly IBasicService _basicService; | 
|---|
|  |  |  | private readonly IRecordService _recordService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private readonly IMesPPOutboundOrderService _mesPPOutboundOrderService; | 
|---|
|  |  |  | public IMesPPOutboundOrderDetailRepository Repository => BaseDal; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public MesPPOutboundOrderDetailService(IMesPPOutboundOrderDetailRepository BaseDal, IBasicRepository basicRepository, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService) : base(BaseDal) | 
|---|
|  |  |  | public MesPPOutboundOrderDetailService(IMesPPOutboundOrderDetailRepository BaseDal,IMesPPOutboundOrderService mesPPOutboundOrderService ,IBasicRepository basicRepository, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService) : base(BaseDal) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _basicRepository = basicRepository; | 
|---|
|  |  |  | _stockService = stockService; | 
|---|
|  |  |  | _outStockLockInfoService = outStockLockInfoService; | 
|---|
|  |  |  | _basicService = basicService; | 
|---|
|  |  |  | _recordService = recordService; | 
|---|
|  |  |  | _mesPPOutboundOrderService=mesPPOutboundOrderService; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public (List<Dt_StockInfo>, List<Dt_MesPPOutboundOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>, Dt_MesPPOutboundOrder  mesPPOutboundOrder) AssignStockOutbound(List<Dt_MesPPOutboundOrderDetail> mesPPOutboundOrderDetails) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>(); | 
|---|
|  |  |  | List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>(); | 
|---|
|  |  |  | Dt_MesPPOutboundOrder OutboundOrder = BaseDal.Db.Queryable<Dt_MesPPOutboundOrder>().Where(x => x.Id == mesPPOutboundOrderDetails.FirstOrDefault().OrderId).First(); | 
|---|
|  |  |  | float originalNeedQuantity = OutboundOrder.OrderQuantity; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | float needQuantity = originalNeedQuantity; | 
|---|
|  |  |  | //Dt_MesPPOutboundOrder outboundOrder = _outboundRepository.QueryFirst(x => x.Id == outboundOrderDetails.FirstOrDefault().OrderId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(OutboundOrder.MaterialCode, "", mesPPOutboundOrderDetails.FirstOrDefault()?.CarrierCode ?? "", OutboundOrder.WarehouseId); | 
|---|
|  |  |  | if (!stockInfos.Any()) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception($"æªæ¾å°å¯åé
åºå"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, OutboundOrder.MaterialCode, needQuantity, out float residueQuantity); | 
|---|
|  |  |  | OutboundOrder.LockQuantity += needQuantity - residueQuantity; | 
|---|
|  |  |  | autoAssignStocks.OrderBy(x => x.Details.FirstOrDefault()?.StockQuantity).ToList(); | 
|---|
|  |  |  | outStocks.AddRange(autoAssignStocks); | 
|---|
|  |  |  | float assignQuantity = needQuantity - residueQuantity; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | float orderQuantity = OutboundOrder.OrderQuantity; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (int j = 0; j < autoAssignStocks.Count; j++) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | float detailAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == OutboundOrder.MaterialCode).Sum(x => x.AssignQuantity);//åºåºè®¢åæç»å·²åé
æ°é | 
|---|
|  |  |  |  | 
|---|
|  |  |  | float palletAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == OutboundOrder.MaterialCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//åºåºè¯¦æ
å·²åé
æ°é | 
|---|
|  |  |  |  | 
|---|
|  |  |  | float palletOutboundQuantity = autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity); | 
|---|
|  |  |  | if (palletAssignQuantity < palletOutboundQuantity)//妿åºåºè¯¦æ
å·²åé
æ°éå°äºæçå·²åé
æ°éï¼åå¯ä»¥ç»§ç»æ·»å è¯¥æçåºåºä¿¡æ¯ | 
|---|
|  |  |  | { | 
|---|
|  |  |  | float orderDetailNeedQuantity = OutboundOrder.OrderQuantity - detailAssignQuantity; | 
|---|
|  |  |  | if (orderDetailNeedQuantity > autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | OutboundOrder.LockQuantity += autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity; | 
|---|
|  |  |  | //Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(OutboundOrder, autoAssignStocks[j], autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity); | 
|---|
|  |  |  | Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(OutboundOrder, autoAssignStocks[j], assignQuantity); | 
|---|
|  |  |  | outStockLockInfos.Add(outStockLockInfo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(OutboundOrder, autoAssignStocks[j], OutboundOrder.OrderQuantity - OutboundOrder.LockQuantity); | 
|---|
|  |  |  | Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(OutboundOrder, autoAssignStocks[j], assignQuantity); | 
|---|
|  |  |  | outStockLockInfos.Add(outStockLockInfo); | 
|---|
|  |  |  | OutboundOrder.LockQuantity = OutboundOrder.OrderQuantity; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList())); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return (outStocks, mesPPOutboundOrderDetails, outStockLockInfos, locationInfos, OutboundOrder); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// åºåºåºååé
åï¼æ´æ°æ°æ®åºæ°æ® | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | /// <param name="stockInfos"></param> | 
|---|
|  |  |  | /// <param name="outboundOrderDetails"></param> | 
|---|
|  |  |  | /// <param name="outStockLockInfos"></param> | 
|---|
|  |  |  | /// <param name="locationInfos"></param> | 
|---|
|  |  |  | /// <param name="locationStatus"></param> | 
|---|
|  |  |  | /// <param name="tasks"></param> | 
|---|
|  |  |  | /// <returns></returns> | 
|---|
|  |  |  | public WebResponseContent LockOutboundStockDataUpdate(Dt_MesPPOutboundOrder mesPPOutboundOrder, List<Dt_StockInfo> stockInfos, List<Dt_MesPPOutboundOrderDetail> 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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (mesPPOutboundOrder != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _mesPPOutboundOrderService.UpdateData(mesPPOutboundOrder); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | _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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|