From bbc4a3a07baf111c9074ceee7728158fb3eedf1a Mon Sep 17 00:00:00 2001
From: pan <antony1029@163.com>
Date: 星期二, 25 十一月 2025 05:33:35 +0800
Subject: [PATCH] 提交
---
项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs | 297 ++++++++++++++++++++++++++++++++++++++++++++--------------
1 files changed, 224 insertions(+), 73 deletions(-)
diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs"
index 3e6904c..e856df7 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderDetailService.cs"
@@ -1,7 +1,13 @@
-锘縰sing WIDESEA_Common.LocationEnum;
+锘縰sing Microsoft.Extensions.Logging;
+using SqlSugar;
+using WIDESEA_Common.LocationEnum;
+using WIDESEA_Common.OrderEnum;
+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;
@@ -15,15 +21,17 @@
private readonly IUnitOfWorkManage _unitOfWorkManage;
public IRepository<Dt_OutboundOrderDetail> Repository => 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;
-
- public OutboundOrderDetailService(IRepository<Dt_OutboundOrderDetail> BaseDal, IUnitOfWorkManage unitOfWorkManage, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService, ILocationInfoService locationInfoService, ILocationStatusChangeRecordService locationStatusChangeRecordService) : base(BaseDal)
+ 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;
@@ -32,88 +40,145 @@
_recordService = recordService;
_locationInfoService = locationInfoService;
_locationStatusChangeRecordService = locationStatusChangeRecordService;
+ _outboundOrderService = outboundOrderService;
+ _logger = logger;
+
}
+
/// <summary>
- ///
+ /// 鍒嗛厤鍑哄簱搴撳瓨 鎸夊厛杩涘厛鍑哄師鍒欏垎閰�
/// </summary>
- /// <param name="outboundOrderDetails"></param>
- /// <returns></returns>
public (List<Dt_StockInfo>, List<Dt_OutboundOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(List<Dt_OutboundOrderDetail> outboundOrderDetails)
{
if (!outboundOrderDetails.Any())
{
- throw new Exception($"鏈壘鍒板嚭搴撳崟鏄庣粏淇℃伅");
+ throw new Exception("鏈壘鍒板嚭搴撳崟鏄庣粏淇℃伅");
}
if (outboundOrderDetails.GroupBy(x => x.OrderId).Count() > 1)
{
- throw new Exception($"璇峰嬁鍚屾椂鎿嶄綔澶氫釜鍗曟嵁鏄庣粏");
+ throw new Exception("璇峰嬁鍚屾椂鎿嶄綔澶氫釜鍗曟嵁鏄庣粏");
}
- Dt_OutboundOrder outboundOrder = Repository.Db.Queryable<Dt_OutboundOrder>().Where(x => x.Id == outboundOrderDetails.FirstOrDefault().OrderId).First();
- List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
- List<Dt_OutboundOrderDetail> 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();
+ 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
+ {
+ x.Key.MaterielCode,
+ x.Key.BatchNo,
+ 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 originalNeedQuantity = item.OrderQuantity;
+ var needQuantity = item.TotalNeedQuantity;
- var needQuantity = originalNeedQuantity;
+ // 鑾峰彇鍙敤搴撳瓨锛堝凡鎸夊厛杩涘厛鍑烘帓搴忥級
+ List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(
+ item.MaterielCode, item.BatchNo, item.SupplyCode);
- List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterielCode, item.BatchNo);
if (!stockInfos.Any())
{
- throw new Exception($"鏈壘鍒板彲鍒嗛厤搴撳瓨");
+ throw new Exception($"鐗╂枡[{item.MaterielCode}]鎵规[{item.BatchNo}]鏈壘鍒板彲鍒嗛厤搴撳瓨");
}
- List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, item.MaterielCode, needQuantity, out decimal residueQuantity);
- item.LockQuantity += needQuantity - residueQuantity;
- outStocks.AddRange(autoAssignStocks);
- var assignQuantity = needQuantity - residueQuantity;
+ // 鍒嗛厤搴撳瓨
+ var (autoAssignStocks, stockAllocations) = _stockService.StockInfoService.GetOutboundStocks(
+ stockInfos, item.MaterielCode, needQuantity, out decimal residueQuantity);
- List<Dt_OutboundOrderDetail> 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++)
+ // 妫�鏌ュ垎閰嶇粨鏋�
+ if (residueQuantity > 0)
{
- 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;
- }
-
- }
- }
+ var allocatedQuantity = needQuantity - residueQuantity;
+ throw new Exception($"鐗╂枡[{item.MaterielCode}]搴撳瓨涓嶈冻锛岄渶瑕亄needQuantity}锛屽疄闄呭垎閰峽allocatedQuantity}");
}
+ outStocks.AddRange(autoAssignStocks);
- locationInfos.AddRange(_locationInfoService.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList()));
+ // 鎸夊厛杩涘厛鍑哄垎閰嶉攣瀹氭暟閲�
+ 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 sortedStocks = assignStocks.OrderBy(x => x.CreateDate).ToList();
+
+ var totalNeedQuantity = details.Sum(d => d.OrderQuantity - d.OverOutQuantity - d.LockQuantity - d.MoveQty);
+ decimal allocatedQuantity = 0;
+
+ foreach (var stock in sortedStocks)
+ {
+ if (allocatedQuantity >= totalNeedQuantity) break;
+
+ if (!stockAllocations.TryGetValue(stock.Id, out decimal stockAllocation) || stockAllocation <= 0)
+ continue;
+
+ var sortedDetails = details
+ .Where(d => d.OrderQuantity - d.OverOutQuantity - d.LockQuantity - d.MoveQty > 0)
+ .OrderBy(x => x.Id)
+ .ToList();
+
+ foreach (var detail in sortedDetails)
+ {
+ if (stockAllocation <= 0) break;
+
+ var detailNeed = detail.OrderQuantity - detail.OverOutQuantity - detail.LockQuantity - detail.MoveQty;
+ if (detailNeed <= 0) continue;
+
+ var assignQuantity = Math.Min(stockAllocation, detailNeed);
+
+ // 浣跨敤搴撳瓨涓殑鏈夋晥鏉$爜
+ var barcode = stock.Details
+ .Where(d => !string.IsNullOrEmpty(d.Barcode))
+ .Select(d => d.Barcode)
+ .FirstOrDefault();
+
+ if (string.IsNullOrEmpty(barcode))
+ {
+ throw new Exception($"搴撳瓨ID[{stock.Id}]鐨勬潯鐮佷负绌�");
+ }
+
+ var lockInfo = _outStockLockInfoService.GetOutStockLockInfo(
+ outboundOrder, detail, stock, assignQuantity, barcode);
+ outStockLockInfos.Add(lockInfo);
+
+ detail.LockQuantity += assignQuantity;
+ stockAllocation -= assignQuantity;
+ allocatedQuantity += assignQuantity;
+
+ if (allocatedQuantity >= totalNeedQuantity) break;
+ }
+ }
+
+ // 楠岃瘉鏄惁瀹屽叏鍒嗛厤
+ if (allocatedQuantity < totalNeedQuantity)
+ {
+ _logger.LogWarning($"搴撳瓨鍒嗛厤涓嶅畬鍏紝闇�瑕亄totalNeedQuantity}锛屽疄闄呭垎閰峽allocatedQuantity}");
+ }
}
/// <summary>
@@ -126,30 +191,19 @@
/// <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)
+ 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);
- List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
- 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);
+ // 鏇存柊搴撳瓨鏄庣粏
+ 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();
@@ -181,5 +235,102 @@
}
}
+
+
+ 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>();
+ }
+
+
+ public (List<Dt_StockInfo>, Dt_OutboundOrderDetail, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(Dt_OutboundOrderDetail outboundOrderDetail, List<StockSelectViewDTO> stockSelectViews)
+ {
+ (bool, string) checkResult = CheckSelectStockDeital(outboundOrderDetail, stockSelectViews);
+ if (!checkResult.Item1) throw new Exception(checkResult.Item2);
+
+ Dt_OutboundOrder outboundOrder = _outboundOrderService.Repository.QueryFirst(x => x.Id == outboundOrderDetail.OrderId);
+ var originalNeedQuantity = outboundOrderDetail.OrderQuantity - outboundOrderDetail.LockQuantity - outboundOrderDetail.MoveQty;
+
+ var needQuantity = originalNeedQuantity;
+
+ List<Dt_StockInfo> outStocks = _stockService.StockInfoService.GetStockInfosByPalletCodes(stockSelectViews.Select(x => x.PalletCode).ToList());
+ var assignQuantity = 0m;
+ outStocks.ForEach(x =>
+ {
+ x.Details.ForEach(v =>
+ {
+ assignQuantity += v.StockQuantity - v.OutboundQuantity;
+ });
+ });
+
+ outboundOrderDetail.LockQuantity += assignQuantity;
+ outStocks.ForEach(x =>
+ {
+ x.Details.ForEach(v =>
+ {
+ v.OutboundQuantity = v.StockQuantity;
+ });
+ });
+ needQuantity -= assignQuantity;
+ if (outboundOrderDetail.OrderQuantity > outboundOrderDetail.LockQuantity)
+ {
+ List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(outboundOrderDetail.MaterielCode, outboundOrderDetail.BatchNo, "");
+ stockInfos = stockInfos.Where(x => !stockSelectViews.Select(v => v.PalletCode).Contains(x.PalletCode)).ToList();
+ var (autoAssignStocks, stockAllocations) = _stockService.StockInfoService.GetOutboundStocks(stockInfos, outboundOrderDetail.MaterielCode, needQuantity, out decimal residueQuantity);
+ outboundOrderDetail.LockQuantity += needQuantity - residueQuantity;
+ outStocks.AddRange(autoAssignStocks);
+ outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.AssignOver.ObjToInt();
+ if (residueQuantity > 0)
+ {
+ outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.AssignOverPartial.ObjToInt();
+ }
+ }
+
+ List<Dt_OutStockLockInfo> outStockLockInfos = _outStockLockInfoService.GetOutStockLockInfos(outboundOrder, outboundOrderDetail, outStocks);
+
+ List<Dt_LocationInfo> locationInfos = _locationInfoService.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList());
+
+ return (outStocks, outboundOrderDetail, outStockLockInfos, locationInfos);
+ }
+ private (bool, string) CheckSelectStockDeital(Dt_OutboundOrderDetail outboundOrderDetail, List<StockSelectViewDTO> 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 - outboundOrderDetail.MoveQty)
+ //{
+ // return (false, "閫夋嫨鏁伴噺瓒呭嚭鍗曟嵁鏁伴噺");
+ //}
+ return (true, "鎴愬姛");
+ }
+
}
}
--
Gitblit v1.9.3