using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SqlSugar.Extensions; using WIDESEA_Common.LocationEnum; using WIDESEA_Common.OrderEnum; using WIDESEA_Core; using WIDESEA_Core.BaseServices; using WIDESEA_DTO.Stock; 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 MesPPCutOutboundOrderDetailService : ServiceBase, IMesPPCutOutboundOrderDetailService { private readonly IBasicRepository _basicRepository; private readonly IStockService _stockService; private readonly IOutStockLockInfoService _outStockLockInfoService; private readonly IBasicService _basicService; private readonly IRecordService _recordService; public IMesPPCutOutboundOrderDetailRepository Repository => BaseDal; public MesPPCutOutboundOrderDetailService(IMesPPCutOutboundOrderDetailRepository BaseDal, IBasicRepository basicRepository, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService) : base(BaseDal) { _basicRepository = basicRepository; _stockService = stockService; _outStockLockInfoService = outStockLockInfoService; _basicService = basicService; _recordService = recordService; } public (List, Dt_MesPPCutOutboundOrderDetail, List, List) AssignStockOutbound(Dt_MesPPCutOutboundOrderDetail mesPPOutboundOrderDetails, List stockSelectViews) { #region //Dt_MesPPCutOutboundOrder OutboundOrder = BaseDal.Db.Queryable().Where(x => x.Id == mesPPOutboundOrderDetails.OrderId).First(); //List outStocks = new List(); //List groupDetails = mesPPOutboundOrderDetails.GroupBy(x => new { x.MaterielCode, x.BatchNo }).Select(x => new Dt_MesPPCutOutboundOrderDetail { OrderQuantity = x.Sum(v => v.OrderQuantity) - x.Sum(v => v.LockQuantity), MaterielCode = x.Key.MaterielCode, BatchNo = x.Key.BatchNo, }).ToList(); //List outStockLockInfos = new List(); //List locationInfos = new List(); //foreach (var item in groupDetails) //{ // float originalNeedQuantity = item.OrderQuantity; // float needQuantity = originalNeedQuantity; // List stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo, OutboundOrder.WarehouseId); // if (!stockInfos.Any()) // { // throw new Exception($"未找到可分配库存"); // } // List autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, item.MaterielCode, needQuantity, out float residueQuantity); // item.LockQuantity += needQuantity - residueQuantity; // outStocks.AddRange(autoAssignStocks); // float assignQuantity = needQuantity - residueQuantity; // List details = mesPPOutboundOrderDetails.Where(x => !string.IsNullOrEmpty(x.BatchNo) ? x.BatchNo == item.BatchNo : true && x.MaterielCode == item.MaterielCode).ToList(); // for (int i = 0; i < details.Count; i++) // { // float orderQuantity = details[i].OrderQuantity; // for (int j = 0; j < autoAssignStocks.Count; j++) // { // float detailAssignQuantity = outStockLockInfos.Where(x => !string.IsNullOrEmpty(x.BatchNo) ? x.BatchNo == item.BatchNo : true && x.MaterielCode == item.MaterielCode && x.OrderDetailId == details[i].Id).Sum(x => x.AssignQuantity);//出库订单明细已分配数量 // float palletAssignQuantity = outStockLockInfos.Where(x => x.BatchNo == item.BatchNo && x.MaterielCode == item.MaterielCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//出库详情已分配数量 // if (string.IsNullOrEmpty(item.BatchNo)) // { // palletAssignQuantity = outStockLockInfos.Where(x => x.MaterielCode == item.MaterielCode && x.PalletCode == autoAssignStocks[j].PalletCode).Sum(x => x.AssignQuantity);//出库详情已分配数量 // } // float palletOutboundQuantity = autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity); // if (palletAssignQuantity < palletOutboundQuantity)//如果出库详情已分配数量小于托盘已分配数量,则可以继续添加该托盘出库信息 // { // float orderDetailNeedQuantity = details[i].OrderQuantity - detailAssignQuantity; // if (orderDetailNeedQuantity > autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity) // { // details[i].LockQuantity += autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity; // Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(OutboundOrder, details[i], autoAssignStocks[j], autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity) - palletAssignQuantity); // outStockLockInfos.Add(outStockLockInfo); // } // else // { // Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(OutboundOrder, details[i], autoAssignStocks[j], details[i].OrderQuantity - details[i].LockQuantity); // outStockLockInfos.Add(outStockLockInfo); // details[i].LockQuantity = details[i].OrderQuantity; // break; // } // } // } // } // locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList())); //} #endregion (bool, string) checkResult = CheckSelectStockDeital(mesPPOutboundOrderDetails, stockSelectViews); if (!checkResult.Item1) throw new Exception(checkResult.Item2); Dt_MesPPCutOutboundOrder OutboundOrder = BaseDal.Db.Queryable().Where(x => x.Id == mesPPOutboundOrderDetails.OrderId).First(); float originalNeedQuantity = mesPPOutboundOrderDetails.OrderQuantity - mesPPOutboundOrderDetails.LockQuantity; float needQuantity = originalNeedQuantity; List outStocks = _stockService.StockInfoService.Repository.GetStockInfosByPalletCodes(stockSelectViews.Select(x => x.PalletCode).ToList()); float assignQuantity = 0; outStocks.ForEach(x => { x.Details.ForEach(v => { assignQuantity += v.StockQuantity - v.OutboundQuantity; }); }); mesPPOutboundOrderDetails.LockQuantity += assignQuantity; outStocks.ForEach(x => { x.Details.ForEach(v => { v.OutboundQuantity = v.StockQuantity; }); }); needQuantity -= assignQuantity; if (mesPPOutboundOrderDetails.OrderQuantity > mesPPOutboundOrderDetails.LockQuantity) { List stockInfos = _stockService.StockInfoService.GetUseableStocks(mesPPOutboundOrderDetails.MaterielCode, mesPPOutboundOrderDetails.BatchNo, OutboundOrder.WarehouseId); stockInfos = stockInfos.Where(x => !stockSelectViews.Select(v => v.PalletCode).Contains(x.PalletCode)).ToList(); List autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, mesPPOutboundOrderDetails.MaterielCode, needQuantity, out float residueQuantity); mesPPOutboundOrderDetails.LockQuantity += needQuantity - residueQuantity; outStocks.AddRange(autoAssignStocks); mesPPOutboundOrderDetails.OrderDetailStatus = OrderDetailStatusEnum.AssignOver.ObjToInt(); if (residueQuantity > 0) { mesPPOutboundOrderDetails.OrderDetailStatus = OrderDetailStatusEnum.AssignOverPartial.ObjToInt(); } } List outStockLockInfos = _outStockLockInfoService.GetOutStockLockInfos(OutboundOrder, mesPPOutboundOrderDetails, outStocks); List locationInfos = _basicService.LocationInfoService.Repository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList()); return (outStocks, mesPPOutboundOrderDetails, outStockLockInfos, locationInfos); } private (bool, string) CheckSelectStockDeital(Dt_MesPPCutOutboundOrderDetail outboundOrderDetail, List stockSelectViews) { if (outboundOrderDetail == null) { return (false, "未找到出库单明细信息"); } if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt() && outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt()) { return (false, "该明细不可操作"); } if (stockSelectViews.Sum(x => x.UseableQuantity) > outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity) { return (false, "选择数量超出单据数量"); } return (true, "成功"); } /// /// 出库库存分配后,更新数据库数据 /// /// /// /// /// /// /// /// public WebResponseContent LockOutboundStockDataUpdate(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); } _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); } } } }