wangxinhui
2025-10-26 733c975cd8647f6d006736f1863bad731e32e6fb
ÏîÄ¿´úÂë/WMS/WMSServices/WIDESEA_OutboundService/OutMESOrderService.cs
@@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.MaterielEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
@@ -16,6 +17,7 @@
using WIDESEA_IRecordService;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
using WIDESEA_OutboundRepository;
namespace WIDESEA_OutboundService
{
@@ -27,15 +29,17 @@
        private IOutStockLockInfoService _outStockLockInfoService;
        private IBasicService _basicService;
        private ILocationStatusChangeRecordService _locationStatusChangeRecordService;
        private IOutboundRepository _outboundRepository;
        private readonly IMapper _mapper;
        public OutMESOrderService(IOutMESOrderRepository BaseDal,IBasicRepository basicRepository, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, ILocationStatusChangeRecordService locationStatusChangeRecordService,IMapper mapper) : base(BaseDal)
        public OutMESOrderService(IOutMESOrderRepository 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;
            _outboundRepository = outboundRepository;
            _mapper = mapper;
        }
        List<string> GradeCodes = new List<string>
@@ -101,5 +105,117 @@
            }
            return content;
        }
        /// <summary>
        /// å‡ºåº“库存分配后,更新数据库数据
        /// </summary>
        public WebResponseContent LockOutboundStockDataUpdate(List<Dt_StockInfo> stockInfos, List<Dt_OutMESOrder> outboundOrderDetails, List<Dt_OutStockLockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null)
        {
            try
            {
                _stockService.StockInfoService.Repository.UpdateData(stockInfos);
                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());
                //批量更新货位状态
                _basicService.LocationInfoService.Repository.UpdateLocationStatus(locationInfos, locationStatus);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// åˆ†é…åº“å­˜
        /// </summary>
        public (List<Dt_StockInfo>, List<Dt_OutMESOrder>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(List<Dt_OutMESOrder> outboundOrders)
        {
            if (!outboundOrders.Any())
            {
                throw new Exception($"未找到出库单明细信息");
            }
            List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
            //出库详情
            List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
            //货位存储
            List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
            foreach (var item in outboundOrders)
            {
                decimal needQuantity = item.ReqQuantity;
                //获取可用库存
                List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterialCode, item.WarehouseId).Where(x => !outStocks.Select(x => x.PalletCode).Contains(x.PalletCode)).ToList();
                if (!stockInfos.Any())
                {
                    throw new Exception($"未找到可分配库存");
                }
                //分配实际库存
                List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos, needQuantity).OrderBy(x => x.StockLength).ToList();
                //添加库存分配
                outStocks.AddRange(autoAssignStocks);
                //订单数量
                decimal orderQuantity = item.ReqQuantity;
                bool assignStop = true;
                while (assignStop)
                {
                    //出库订单明细已分配数量
                    decimal detailAssignQuantity = outStockLockInfos.Where(x => x.OrderDetailId == item.OutDetailId).Sum(x => x.AssignQuantity);
                    decimal orderDetailNeedQuantity = item.ReqQuantity - detailAssignQuantity;
                    decimal useStockLength = autoAssignStocks[0].StockLength - autoAssignStocks[0].StockOutLength;
                    if (orderDetailNeedQuantity > useStockLength)
                    {
                        //生成详情
                        Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(item, autoAssignStocks[0], useStockLength);
                        outStockLockInfos.Add(outStockLockInfo);
                        item.AssignTotalUsage += useStockLength;
                        autoAssignStocks.Remove(autoAssignStocks[0]);
                    }
                    else
                    {
                        //生成详情
                        Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(item, autoAssignStocks[0], orderDetailNeedQuantity);
                        outStockLockInfos.Add(outStockLockInfo);
                        item.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, outboundOrders, outStockLockInfos, locationInfos);
        }
    }
}