| | |
| | | using WIDESEA_Core.BaseRepository; |
| | | using Microsoft.Extensions.Logging; |
| | | using SqlSugar; |
| | | using WIDESEA_Common.LocationEnum; |
| | | using WIDESEA_Common.StockEnum; |
| | | using WIDESEA_Core; |
| | | using WIDESEA_Core.BaseRepository; |
| | | using WIDESEA_Core.BaseServices; |
| | | using WIDESEA_Core.Helper; |
| | | using WIDESEA_DTO.Stock; |
| | | using WIDESEA_IBasicService; |
| | | using WIDESEA_IOutboundService; |
| | | using WIDESEA_IRecordService; |
| | | using WIDESEA_IStockService; |
| | | using WIDESEA_Model.Models; |
| | | |
| | | namespace WIDESEA_OutboundService |
| | |
| | | public partial class OutboundOrderDetailService : ServiceBase<Dt_OutboundOrderDetail, IRepository<Dt_OutboundOrderDetail>>, IOutboundOrderDetailService |
| | | { |
| | | private readonly IUnitOfWorkManage _unitOfWorkManage; |
| | | |
| | | public IRepository<Dt_OutboundOrderDetail> Repository => BaseDal; |
| | | |
| | | public OutboundOrderDetailService(IRepository<Dt_OutboundOrderDetail> BaseDal, IUnitOfWorkManage unitOfWorkManage) : base(BaseDal) |
| | | |
| | | private readonly IStockService _stockService; |
| | | private readonly IOutStockLockInfoService _outStockLockInfoService; |
| | | private readonly ILocationInfoService _locationInfoService; |
| | | private readonly IBasicService _basicService; |
| | | private readonly IRecordService _recordService; |
| | | private readonly IOutboundOrderService _outboundOrderService; |
| | | private readonly ILocationStatusChangeRecordService _locationStatusChangeRecordService; |
| | | private readonly ILogger<OutboundOrderDetailService> _logger; |
| | | public OutboundOrderDetailService(IRepository<Dt_OutboundOrderDetail> BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService, ILocationInfoService locationInfoService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IOutboundOrderService outboundOrderService, ILogger<OutboundOrderDetailService> logger) : base(BaseDal) |
| | | { |
| | | _unitOfWorkManage = unitOfWorkManage; |
| | | _stockService = stockService; |
| | | _outStockLockInfoService = outStockLockInfoService; |
| | | _basicService = basicService; |
| | | _recordService = recordService; |
| | | _locationInfoService = locationInfoService; |
| | | _locationStatusChangeRecordService = locationStatusChangeRecordService; |
| | | _outboundOrderService = outboundOrderService; |
| | | _logger = logger; |
| | | } |
| | | |
| | | |
| | | |
| | | /// <summary> |
| | | /// åé
åºåºåºå æå
è¿å
åºåååé
|
| | | /// </summary> |
| | | public (List<Dt_StockInfo>, List<Dt_OutboundOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(List<Dt_OutboundOrderDetail> outboundOrderDetails) |
| | | { |
| | | if (!outboundOrderDetails.Any()) |
| | | { |
| | | throw new Exception("æªæ¾å°åºåºåæç»ä¿¡æ¯"); |
| | | } |
| | | |
| | | if (outboundOrderDetails.GroupBy(x => x.OrderId).Count() > 1) |
| | | { |
| | | throw new Exception("请å¿åæ¶æä½å¤ä¸ªåæ®æç»"); |
| | | } |
| | | |
| | | var outboundOrder = _outboundOrderService.Db.Queryable<Dt_OutboundOrder>() |
| | | .First(x => x.Id == outboundOrderDetails.First().OrderId); |
| | | |
| | | List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>(); |
| | | List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>(); |
| | | List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>(); |
| | | |
| | | // æç©æåæ¹æ¬¡åç»å¤ç |
| | | var groupDetails = outboundOrderDetails |
| | | .GroupBy(x => new { x.MaterielCode, x.BatchNo ,x.SupplyCode}) |
| | | .Select(x => new |
| | | { |
| | | MaterielCode = x.Key.MaterielCode, |
| | | BatchNo = x.Key.BatchNo, |
| | | SupplyCode = x.Key.SupplyCode, |
| | | Details = x.ToList(), |
| | | TotalNeedQuantity = x.Sum(v => v.OrderQuantity - v.OverOutQuantity - v.LockQuantity-v.MoveQty) |
| | | }) |
| | | .Where(x => x.TotalNeedQuantity > 0) |
| | | .ToList(); |
| | | |
| | | foreach (var item in groupDetails) |
| | | { |
| | | var needQuantity = item.TotalNeedQuantity; |
| | | |
| | | // è·åå¯ç¨åºåï¼æå
è¿å
åºæåºï¼ |
| | | List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo,item.SupplyCode); |
| | | if (!stockInfos.Any()) |
| | | { |
| | | throw new Exception($"ç©æ[{item.MaterielCode}]æ¹æ¬¡[{item.BatchNo}]æªæ¾å°å¯åé
åºå"); |
| | | } |
| | | |
| | | // åé
åºåï¼æå
è¿å
åºï¼ |
| | | var (autoAssignStocks, stockAllocations) = _stockService.StockInfoService.GetOutboundStocks( |
| | | stockInfos, item.MaterielCode, needQuantity, out decimal residueQuantity); |
| | | |
| | | if (residueQuantity > 0 && residueQuantity == needQuantity) |
| | | { |
| | | throw new Exception($"ç©æ[{item.MaterielCode}]åºåä¸è¶³ï¼éè¦{needQuantity}ï¼å¯ç¨{needQuantity - residueQuantity}"); |
| | | } |
| | | |
| | | outStocks.AddRange(autoAssignStocks); |
| | | |
| | | // æå
è¿å
åºåååé
é宿°éå°å个æç» |
| | | DistributeLockQuantityByFIFO(item.Details, autoAssignStocks, stockAllocations, outStockLockInfos, outboundOrder); |
| | | } |
| | | |
| | | locationInfos.AddRange(_locationInfoService.GetLocationInfos(outStocks.Select(x => x.LocationCode).Distinct().ToList())); |
| | | |
| | | return (outStocks, outboundOrderDetails, outStockLockInfos, locationInfos); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æå
è¿å
åºåååé
é宿°éå°å个æç» |
| | | /// </summary> |
| | | private void DistributeLockQuantityByFIFO( |
| | | List<Dt_OutboundOrderDetail> details, |
| | | List<Dt_StockInfo> assignStocks, |
| | | Dictionary<int, decimal> stockAllocations, |
| | | List<Dt_OutStockLockInfo> outStockLockInfos, |
| | | Dt_OutboundOrder outboundOrder) |
| | | { |
| | | // æå
è¿å
åºæåºåºåºåæç»ï¼å设å
å建çæç»éè¦ä¼å
æ»¡è¶³ï¼ |
| | | var sortedDetails = details |
| | | .Where(d => d.OrderQuantity - d.OverOutQuantity - d.LockQuantity -d.MoveQty> 0) // åªå¤çè¿éè¦åé
çæ°é |
| | | .OrderBy(x => x.Id) |
| | | .ToList(); |
| | | |
| | | if (!sortedDetails.Any()) return; |
| | | |
| | | // è·åææåé
äºåºåçæç»ï¼æå
è¿å
åºæåº |
| | | var allocatedStockDetails = assignStocks |
| | | .SelectMany(x => x.Details) |
| | | .Where(x => stockAllocations.ContainsKey(x.Id)) |
| | | .OrderBy(x => x.CreateDate) |
| | | .ThenBy(x => x.StockId) |
| | | .ToList(); |
| | | |
| | | // 为æ¯ä¸ªåºåæç»å建åé
è®°å½ |
| | | foreach (var stockDetail in allocatedStockDetails) |
| | | { |
| | | if (!stockAllocations.TryGetValue(stockDetail.Id, out decimal allocatedQuantity)) |
| | | continue; |
| | | |
| | | if (allocatedQuantity <= 0) continue; |
| | | |
| | | var stockInfo = assignStocks.First(x => x.Id == stockDetail.StockId); |
| | | decimal remainingAllocate = allocatedQuantity; |
| | | |
| | | // æé¡ºåºåé
ç»å个åºåºåæç» |
| | | foreach (var detail in sortedDetails) |
| | | { |
| | | if (remainingAllocate <= 0) break; |
| | | |
| | | // 计ç®è¿ä¸ªæç»è¿éè¦åé
çæ°é |
| | | var detailNeed = detail.OrderQuantity - detail.OverOutQuantity - detail.LockQuantity; |
| | | if (detailNeed <= 0) continue; |
| | | |
| | | // åé
æ°é |
| | | var assignQuantity = Math.Min(remainingAllocate, detailNeed); |
| | | |
| | | // éªè¯æ¡ç æ¯å¦åå¨ |
| | | if (string.IsNullOrEmpty(stockDetail.Barcode)) |
| | | { |
| | | throw new Exception($"åºåæç»ID[{stockDetail.Id}]çæ¡ç 为空"); |
| | | } |
| | | |
| | | // å建åºåºéå®ä¿¡æ¯ |
| | | var lockInfo = _outStockLockInfoService.GetOutStockLockInfo( |
| | | outboundOrder, detail, stockInfo, assignQuantity, stockDetail.Barcode); |
| | | outStockLockInfos.Add(lockInfo); |
| | | |
| | | // æ´æ°æç»çé宿°é |
| | | detail.LockQuantity += assignQuantity; |
| | | remainingAllocate -= assignQuantity; |
| | | } |
| | | |
| | | // å¦æè¿æå©ä½åé
æ°éï¼éæ°åé
æè®°å½è¦å |
| | | if (remainingAllocate > 0) |
| | | { |
| | | // éæ°åé
ç»å
¶ä»éè¦åé
çæç» |
| | | foreach (var detail in sortedDetails) |
| | | { |
| | | if (remainingAllocate <= 0) break; |
| | | |
| | | var detailNeed = detail.OrderQuantity - detail.OverOutQuantity - detail.LockQuantity; |
| | | if (detailNeed <= 0) continue; |
| | | |
| | | var assignQuantity = Math.Min(remainingAllocate, detailNeed); |
| | | |
| | | var lockInfo = _outStockLockInfoService.GetOutStockLockInfo( |
| | | outboundOrder, detail, stockInfo, assignQuantity, stockDetail.Barcode); |
| | | outStockLockInfos.Add(lockInfo); |
| | | |
| | | detail.LockQuantity += assignQuantity; |
| | | remainingAllocate -= assignQuantity; |
| | | } |
| | | |
| | | // å¦æè¿æå©ä½ï¼è®°å½è¦åä½ä¸æåºå¼å¸¸ |
| | | if (remainingAllocate > 0) |
| | | { |
| | | _logger.LogWarning($"åºååé
å仿å©ä½æ°éæªåé
: {remainingAllocate}, æ¡ç : {stockDetail.Barcode}"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åºåºåºååé
åï¼æ´æ°æ°æ®åºæ°æ® |
| | | /// </summary> |
| | | /// <param name="stockInfos"></param> |
| | | /// <param name="outboundOrderDetails"></param> |
| | | /// <param name="outStockLockInfos"></param> |
| | | /// <param name="locationInfos"></param> |
| | | /// <param name="locationStatus"></param> |
| | | /// <param name="tasks"></param> |
| | | /// <returns></returns> |
| | | public WebResponseContent LockOutboundStockDataUpdate(List<Dt_StockInfo> stockInfos, List<Dt_OutboundOrderDetail> outboundOrderDetails, |
| | | List<Dt_OutStockLockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos, |
| | | LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null) |
| | | { |
| | | try |
| | | { |
| | | // æ´æ°åºåç¶æ |
| | | stockInfos.ForEach(x => x.StockStatus = (int)StockStatusEmun.åºåºéå®); |
| | | _stockService.StockInfoService.Repository.UpdateData(stockInfos); |
| | | |
| | | // æ´æ°åºåæç» |
| | | var stockDetails = stockInfos.SelectMany(x => x.Details).ToList(); |
| | | _stockService.StockInfoDetailService.Repository.UpdateData(stockDetails); |
| | | BaseDal.UpdateData(outboundOrderDetails); |
| | | |
| | | List<Dt_OutStockLockInfo> 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<Dt_OutStockLockInfo> 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); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | public override PageGridData<Dt_OutboundOrderDetail> GetPageData(PageDataOptions options) |
| | | { |
| | | //var pageGridData = base.GetPageData(options); |
| | | |
| | | ISugarQueryable<Dt_OutboundOrderDetail> sugarQueryable1 = BaseDal.Db.Queryable<Dt_OutboundOrderDetail>(); |
| | | if (!string.IsNullOrEmpty(options.Wheres)) |
| | | { |
| | | |
| | | List<SearchParameters> searchParametersList = options.Wheres.DeserializeObject<List<SearchParameters>>(); |
| | | int totalCount = 0; |
| | | if (searchParametersList.Count > 0) |
| | | { |
| | | { |
| | | SearchParameters? searchParameters = searchParametersList.FirstOrDefault(x => x.Name == nameof(Dt_InboundOrderDetail.OrderId).FirstLetterToLower()); |
| | | if (searchParameters != null) |
| | | { |
| | | sugarQueryable1 = sugarQueryable1.Where(x => x.OrderId== searchParameters.Value.ObjToInt()); |
| | | var dataList = sugarQueryable1.ToPageList(options.Page, options.Rows, ref totalCount); |
| | | return new PageGridData<Dt_OutboundOrderDetail>(totalCount, dataList); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
| | | } |
| | | return new PageGridData<Dt_OutboundOrderDetail> (); |
| | | } |
| | | |
| | | } |
| | | } |