using AutoMapper; 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 OutSGOrderDetailService : ServiceBase, IOutSGOrderDetailService { public IOutSGOrderDetailRepository Repository => BaseDal; private IBasicRepository _basicRepository; private IStockService _stockService; private IOutStockLockInfoService _outStockLockInfoService; private IBasicService _basicService; private ILocationStatusChangeRecordService _locationStatusChangeRecordService; private readonly IMapper _mapper; private readonly IOutboundRepository _outboundRepository; public OutSGOrderDetailService(IOutSGOrderDetailRepository BaseDal, IBasicRepository basicRepository, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IMapper mapper, IOutboundRepository outboundRepository) : base(BaseDal) { _basicRepository = basicRepository; _stockService = stockService; _outStockLockInfoService = outStockLockInfoService; _basicService = basicService; _locationStatusChangeRecordService = locationStatusChangeRecordService; _mapper = mapper; _outboundRepository = outboundRepository; } /// /// 分配库存 /// public (List, List, List, List) AssignStockOutbound(List outboundOrderDetails) { if (!outboundOrderDetails.Any()) { throw new Exception($"未找到出库单明细信息"); } //获取所有排程主表 List outBSTOrders = _outboundRepository.OutSGOrderRepository.QueryData(x => outboundOrderDetails.Select(x=>x.OutSGOrderId).Distinct().Contains(x.Id)); List outStocks = new List(); //对出库物料进行分组 List groupDetails = outboundOrderDetails.GroupBy(x => new { x.MaterialNo, x.Width, x.MachineName }).Select(x => new Dt_OutSGOrderDetail { XqLen = x.Sum(v => v.XqLen), MaterialNo = x.Key.MaterialNo, Width = x.Key.Width, MachineName = x.Key.MachineName } ).ToList(); Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == outBSTOrders.FirstOrDefault().WarehouseId); //出库详情 List outStockLockInfos = new List(); //货位存储 List locationInfos = new List(); foreach (var item in groupDetails) { decimal needQuantity = item.XqLen; //获取可用库存 List stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterialNo, item.Width, outBSTOrders.FirstOrDefault().WarehouseId).Where(x=>!outStocks.Select(x=>x.PalletCode).Contains(x.PalletCode)).ToList(); if (!stockInfos.Any()) { throw new Exception($"未找到可分配库存"); } //分配实际库存 List autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, needQuantity).OrderBy(x => x.StockLength - x.StockOutLength).ToList(); //添加库存分配 outStocks.AddRange(autoAssignStocks); //获取所有该物料单据 List details = outboundOrderDetails.Where(x => x.MaterialNo == item.MaterialNo && x.Width == item.Width && x.MachineName == item.MachineName).ToList(); autoAssignStocks.ForEach(x => { x.StockOutLength = 0; }); for (int i = 0; i < details.Count; i++) { //订单数量 decimal orderQuantity = details[i].XqLen; bool assignStop = true; while (assignStop) { //出库订单明细已分配数量 decimal detailAssignQuantity = outStockLockInfos.Where(x => x.OrderDetailId == details[i].Id).Sum(x => x.AssignQuantity); decimal orderDetailNeedQuantity = details[i].XqLen - detailAssignQuantity; decimal useStockLength = autoAssignStocks[0].StockLength- autoAssignStocks[0].StockOutLength; Dt_OutSGOrder? sGOrder = outBSTOrders.FirstOrDefault(x => x.Id == details[i].OutSGOrderId); if (orderDetailNeedQuantity > useStockLength) { //生成详情 Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(sGOrder, details[i], autoAssignStocks[0], useStockLength); outStockLockInfos.Add(outStockLockInfo); details[i].AssignTotalUsage += useStockLength; autoAssignStocks.Remove(autoAssignStocks[0]); } else { //生成详情 Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(sGOrder, details[i], autoAssignStocks[0], orderDetailNeedQuantity); outStockLockInfos.Add(outStockLockInfo); details[i].AssignTotalUsage = orderQuantity; autoAssignStocks[0].StockOutLength+= orderDetailNeedQuantity; if (autoAssignStocks[0].StockOutLength== autoAssignStocks[0].StockLength) { autoAssignStocks.Remove(autoAssignStocks[0]); } assignStop =false; } } } locationInfos.AddRange(_basicRepository.LocationInfoRepository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList())); } return (outStocks, outboundOrderDetails, outStockLockInfos, locationInfos); } /// /// 出库库存分配后,更新数据库数据 /// public WebResponseContent LockOutboundStockDataUpdate(List stockInfos, List outboundOrderDetails, List outStockLockInfos, List locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List? tasks = null) { try { _stockService.StockInfoService.Repository.UpdateData(stockInfos); 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); } //添加货位状态 _locationStatusChangeRecordService.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); } } } }