using WIDESEA_Common.LocationEnum; using WIDESEA_Core; using WIDESEA_Core.BaseRepository; using WIDESEA_Core.BaseServices; using WIDESEA_IBasicService; using WIDESEA_IOutboundService; using WIDESEA_IRecordService; using WIDESEA_IStockService; using WIDESEA_Model.Models; namespace WIDESEA_OutboundService { public partial class OutboundOrderDetailService : ServiceBase>, IOutboundOrderDetailService { private readonly IUnitOfWorkManage _unitOfWorkManage; public IRepository Repository => BaseDal; private readonly IStockService _stockService; private readonly IOutStockLockInfoService _outStockLockInfoService; private readonly ILocationInfoService _locationInfoService; private readonly IBasicService _basicService; private readonly IRecordService _recordService; private readonly ILocationStatusChangeRecordService _locationStatusChangeRecordService; public OutboundOrderDetailService(IRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService, ILocationInfoService locationInfoService, ILocationStatusChangeRecordService locationStatusChangeRecordService) : base(BaseDal) { _unitOfWorkManage = unitOfWorkManage; _stockService = stockService; _outStockLockInfoService = outStockLockInfoService; _basicService = basicService; _recordService = recordService; _locationInfoService = locationInfoService; _locationStatusChangeRecordService = locationStatusChangeRecordService; } /// /// /// /// /// public (List, List, List, List) AssignStockOutbound(List outboundOrderDetails) { if (!outboundOrderDetails.Any()) { throw new Exception($"未找到出库单明细信息"); } if (outboundOrderDetails.GroupBy(x => x.OrderId).Count() > 1) { throw new Exception($"请勿同时操作多个单据明细"); } Dt_OutboundOrder outboundOrder = Repository.Db.Queryable().Where(x => x.Id == outboundOrderDetails.FirstOrDefault().OrderId).First(); List outStocks = new List(); List groupDetails = outboundOrderDetails.GroupBy(x => new { x.MaterielCode, x.BatchNo }).Select(x => new Dt_OutboundOrderDetail { 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) { var originalNeedQuantity = item.OrderQuantity; var needQuantity = originalNeedQuantity; List stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo); if (!stockInfos.Any()) { throw new Exception($"未找到可分配库存"); } List autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, item.MaterielCode, needQuantity, out decimal residueQuantity); item.LockQuantity += needQuantity - residueQuantity; outStocks.AddRange(autoAssignStocks); var assignQuantity = needQuantity - residueQuantity; List details = outboundOrderDetails.Where(x => !string.IsNullOrEmpty(x.BatchNo) ? x.BatchNo == item.BatchNo : true && x.MaterielCode == item.MaterielCode).ToList(); for (int i = 0; i < details.Count; i++) { var orderQuantity = details[i].OrderQuantity; for (int j = 0; j < autoAssignStocks.Count; j++) { var 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);//出库订单明细已分配数量 var 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);//出库详情已分配数量 } var palletOutboundQuantity = autoAssignStocks[j].Details.Sum(x => x.OutboundQuantity); if (palletAssignQuantity < palletOutboundQuantity)//如果出库详情已分配数量小于托盘已分配数量,则可以继续添加该托盘出库信息 { var 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(_locationInfoService.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); List stockInfoDetails = new List(); foreach (var item in stockInfos) { foreach (var detail in item.Details) { // 进行安全转换 if (detail.OutboundQuantity != null && decimal.TryParse(detail.OutboundQuantity.ToString(), out decimal outboundDecimal)) { decimal outboundDecimal1 = Convert.ToDecimal(detail.OutboundQuantity); } else { detail.OutboundQuantity = 0; // 默认值或记录错误 } } 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); } _locationStatusChangeRecordService.AddLocationStatusChangeRecord(locationInfos, locationStatus, LocationChangeType.OutboundAssignLocation, "", tasks?.Select(x => x.TaskNum).ToList()); _locationInfoService.UpdateLocationStatus(locationInfos, locationStatus); return WebResponseContent.Instance.OK(); } catch (Exception ex) { return WebResponseContent.Instance.Error(ex.Message); } } } }