using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEA_Common.LocationEnum; using WIDESEA_Common.StockEnum; using WIDESEA_Core; using WIDESEA_Core.BaseServices; using WIDESEA_Core.Helper; 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 MesRworkOutboundOrderService : ServiceBase, IMesRworkOutboundOrderService { private readonly IBasicRepository _basicRepository; private readonly IStockService _stockService; private readonly IOutStockLockInfoService _outStockLockInfoService; private readonly IBasicService _basicService; private readonly IRecordService _recordService; private readonly IOutProStockInfoService _outProStockInfoService; public IMesRworkOutboundOrderRepository Repository => BaseDal; public MesRworkOutboundOrderService(IMesRworkOutboundOrderRepository BaseDal, IBasicRepository basicRepository, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService, IOutProStockInfoService outProStockInfoService) : base(BaseDal) { _basicRepository = basicRepository; _stockService = stockService; _outStockLockInfoService = outStockLockInfoService; _basicService = basicService; _recordService = recordService; _outProStockInfoService = outProStockInfoService; } /// /// 提库任务分配库存 /// public (List?,Dt_MesRworkOutboundOrder?,List?,List) AssignMesStocks(Dt_MesRworkOutboundOrder mesRworkOutboundOrder) { List proStockInfos = new List(); Dt_MesRworkOutboundOrder assignOutOrder= new Dt_MesRworkOutboundOrder(); List outProStockInfos=new List(); List locationInfos=new List(); float originalNeedQuantity = mesRworkOutboundOrder.RequiredQuantity; float needQuantity = originalNeedQuantity; //查找可用库存 List stockInfoss = _stockService.ProStockInfoService.GetUseableStocks(mesRworkOutboundOrder); if (!stockInfoss.Any()) { throw new Exception("未找到可分配库存"); } List autoAssignStocks = _stockService.ProStockInfoService.GetOutboundStocks(stockInfoss,mesRworkOutboundOrder, needQuantity,out float residueQuantity); mesRworkOutboundOrder.LockQuantity += needQuantity - residueQuantity; autoAssignStocks.OrderBy(x => x.proStockInfoDetails.FirstOrDefault()?.StockPcsQty).ToList(); proStockInfos.AddRange(autoAssignStocks); float assignQuantity = needQuantity - residueQuantity; float orderQuantity = mesRworkOutboundOrder.RequiredQuantity; for (int j = 0; j < autoAssignStocks.Count; j++) { //出库订单明细已分配数量 float detailAssignQuantity = outProStockInfos .Where(x => x.SaleOrder == mesRworkOutboundOrder.SaleOrder && x.PCode == mesRworkOutboundOrder.ProductCode && x.PVer == mesRworkOutboundOrder.ProductVersion) .Sum(x => x.AssignQuantity); //出库详情已分配数量 float palletAssignQuantity = outProStockInfos .Where(x => x.SaleOrder == mesRworkOutboundOrder.SaleOrder && x.PCode == mesRworkOutboundOrder.ProductCode && x.PVer == mesRworkOutboundOrder.ProductVersion && x.PalletCode == autoAssignStocks[j].PalletCode) .Sum(x => x.AssignQuantity); float palletOutboundQuantity = autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity); if (palletAssignQuantity < palletOutboundQuantity)//如果出库详情已分配数量小于托盘已分配数量,则可以继续添加该托盘出库信息 { float orderDetailNeedQuantity = mesRworkOutboundOrder.RequiredQuantity - detailAssignQuantity; if (orderDetailNeedQuantity > autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity) { mesRworkOutboundOrder.LockQuantity += autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity; Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(mesRworkOutboundOrder, autoAssignStocks[j], autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity); outProStockInfos.Add(outStockLockInfo); } else { Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(mesRworkOutboundOrder, autoAssignStocks[j], mesRworkOutboundOrder.RequiredQuantity-mesRworkOutboundOrder.LockQuantity); outProStockInfos.Add(outStockLockInfo); mesRworkOutboundOrder.LockQuantity = mesRworkOutboundOrder.RequiredQuantity; break; } } } locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(proStockInfos.Select(x => x.LocationCode).ToList())); return (proStockInfos, assignOutOrder, outProStockInfos, locationInfos); } public WebResponseContent LockOutboundStockDataUpdate(List stockInfos, List outStockLockInfos, List locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List? tasks = null) { try { stockInfos.ForEach(x => { x.StockStatus = StockStatusEmun.出库锁定.ObjToInt(); }); _stockService.ProStockInfoService.Repository.UpdateData(stockInfos); List stockInfoDetails = new List(); foreach (var item in stockInfos) { stockInfoDetails.AddRange(item.proStockInfoDetails); } _stockService.ProStockInfoDetailService.Repository.UpdateData(stockInfoDetails); 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; }); } _outProStockInfoService.Repository.AddData(addOutStockLockInfos); } List updateOutStockLockInfos = outStockLockInfos.Where(x => x.Id > 0).ToList(); if (updateOutStockLockInfos != null && updateOutStockLockInfos.Any()) { _outProStockInfoService.Repository.UpdateData(updateOutStockLockInfos); } _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); } } } }