using System; using System.Collections.Generic; 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; using WIDESEA_IOutboundRepository; using WIDESEA_IOutboundService; using WIDESEA_IRecordService; using WIDESEA_IStockService; using WIDESEA_Model.Models; using WIDESEA_OutboundRepository; namespace WIDESEA_OutboundService { public class MesPPOutboundOrderDetailService : ServiceBase, IMesPPOutboundOrderDetailService { private readonly IBasicRepository _basicRepository; private readonly IStockService _stockService; private readonly IOutStockLockInfoService _outStockLockInfoService; private readonly IBasicService _basicService; private readonly IRecordService _recordService; private readonly IMesPPOutboundOrderService _mesPPOutboundOrderService; public IMesPPOutboundOrderDetailRepository Repository => 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, List, List, List, Dt_MesPPOutboundOrder mesPPOutboundOrder) AssignStockOutbound(List mesPPOutboundOrderDetails) { List outStocks = new List(); List outStockLockInfos = new List(); List locationInfos = new List(); Dt_MesPPOutboundOrder OutboundOrder = BaseDal.Db.Queryable().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 stockInfos = _stockService.StockInfoService.GetUseableStocks(OutboundOrder.MaterialCode, "", mesPPOutboundOrderDetails.FirstOrDefault()?.CarrierCode ?? "", OutboundOrder.WarehouseId); if (!stockInfos.Any()) { throw new Exception($"未找到可分配库存"); } List 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); } /// /// 出库库存分配后,更新数据库数据 /// /// /// /// /// /// /// /// public WebResponseContent LockOutboundStockDataUpdate(Dt_MesPPOutboundOrder mesPPOutboundOrder, List stockInfos, List outboundOrderDetails, List outStockLockInfos, List locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List? tasks = null) { try { _stockService.StockInfoService.Repository.UpdateData(stockInfos); List stockInfoDetails = new List(); foreach (var item in stockInfos) { stockInfoDetails.AddRange(item.Details); } _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetails); BaseDal.UpdateData(outboundOrderDetails); List 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 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); } } } }