wangxinhui
2025-03-19 d8db1698c125618c1b5f62b009204ddc5d4eed5a
´úÂë¹ÜÀí/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/MesRworkOutboundOrderService.cs
@@ -3,7 +3,11 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Helper;
using WIDESEA_IBasicRepository;
using WIDESEA_IBasicService;
using WIDESEA_IOutboundRepository;
@@ -22,16 +26,127 @@
        private readonly IOutStockLockInfoService _outStockLockInfoService;
        private readonly IBasicService _basicService;
        private readonly IRecordService _recordService;
        private readonly IOutProStockInfoService _outProStockInfoService;
        public IMesRworkOutboundOrderRepository Repository => BaseDal;
        public MesRworkOutboundOrderService(IMesRworkOutboundOrderRepository BaseDal, IBasicRepository basicRepository, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService) : base(BaseDal)
        public MesRworkOutboundOrderService(IMesRworkOutboundOrderRepository BaseDal, IBasicRepository basicRepository, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService, IOutProStockInfoService outProStockInfoService) : base(BaseDal)
        {
            _basicRepository = basicRepository;
            _stockService = stockService;
            _outStockLockInfoService = outStockLockInfoService;
            _basicService = basicService;
            _recordService = recordService;
            _outProStockInfoService = outProStockInfoService;
        }
        /// <summary>
        /// æåº“任务分配库存
        /// </summary>
        public (List<Dt_ProStockInfo>?,Dt_MesRworkOutboundOrder?,List<Dt_OutProStockInfo>?,List<Dt_LocationInfo>) AssignMesStocks(Dt_MesRworkOutboundOrder mesRworkOutboundOrder)
        {
            List<Dt_ProStockInfo> proStockInfos = new List<Dt_ProStockInfo>();
            Dt_MesRworkOutboundOrder assignOutOrder= new Dt_MesRworkOutboundOrder();
            List<Dt_OutProStockInfo> outProStockInfos=new List<Dt_OutProStockInfo>();
            List<Dt_LocationInfo> locationInfos=new List<Dt_LocationInfo>();
            float originalNeedQuantity = mesRworkOutboundOrder.RequiredQuantity;
            float needQuantity = originalNeedQuantity;
            //查找可用库存
            List<Dt_ProStockInfo> stockInfoss = _stockService.ProStockInfoService.GetUseableStocks(mesRworkOutboundOrder);
            if (!stockInfoss.Any())
            {
                throw new Exception("未找到可分配库存");
            }
            List<Dt_ProStockInfo> autoAssignStocks = _stockService.ProStockInfoService.GetOutboundStocks(stockInfoss,mesRworkOutboundOrder, needQuantity,out float residueQuantity);
            mesRworkOutboundOrder.LockQuantity += needQuantity - residueQuantity;
            autoAssignStocks.OrderBy(x => x.proStockInfoDetails.FirstOrDefault()?.StockPcsQty).ToList();
            proStockInfos.AddRange(autoAssignStocks);
            float assignQuantity = needQuantity - residueQuantity;
            float orderQuantity = mesRworkOutboundOrder.RequiredQuantity;
            for (int j = 0; j < autoAssignStocks.Count; j++)
            {
                //出库订单明细已分配数量
                float detailAssignQuantity = outProStockInfos
                    .Where(x => x.SaleOrder == mesRworkOutboundOrder.SaleOrder
                    && x.PCode == mesRworkOutboundOrder.ProductCode
                    && x.PVer == mesRworkOutboundOrder.ProductVersion)
                    .Sum(x => x.AssignQuantity);
                //出库详情已分配数量
                float palletAssignQuantity = outProStockInfos
                    .Where(x => x.SaleOrder == mesRworkOutboundOrder.SaleOrder
                    && x.PCode == mesRworkOutboundOrder.ProductCode
                    && x.PVer == mesRworkOutboundOrder.ProductVersion
                    && x.PalletCode == autoAssignStocks[j].PalletCode)
                    .Sum(x => x.AssignQuantity);
                float palletOutboundQuantity = autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity);
                if (palletAssignQuantity < palletOutboundQuantity)//如果出库详情已分配数量小于托盘已分配数量,则可以继续添加该托盘出库信息
                {
                    float orderDetailNeedQuantity = mesRworkOutboundOrder.RequiredQuantity - detailAssignQuantity;
                    if (orderDetailNeedQuantity > autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity)
                    {
                        mesRworkOutboundOrder.LockQuantity += autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity;
                        Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(mesRworkOutboundOrder, autoAssignStocks[j], autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity);
                        outProStockInfos.Add(outStockLockInfo);
                    }
                    else
                    {
                        Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(mesRworkOutboundOrder, autoAssignStocks[j], mesRworkOutboundOrder.RequiredQuantity-mesRworkOutboundOrder.LockQuantity);
                        outProStockInfos.Add(outStockLockInfo);
                        mesRworkOutboundOrder.LockQuantity = mesRworkOutboundOrder.RequiredQuantity;
                        break;
                    }
                }
            }
            locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(proStockInfos.Select(x => x.LocationCode).ToList()));
            return (proStockInfos, assignOutOrder, outProStockInfos, locationInfos);
        }
        public WebResponseContent LockOutboundStockDataUpdate(List<Dt_ProStockInfo> stockInfos, List<Dt_OutProStockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null)
        {
            try
            {
                stockInfos.ForEach(x => {
                    x.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
                });
                _stockService.ProStockInfoService.Repository.UpdateData(stockInfos);
                List<Dt_ProStockInfoDetail> stockInfoDetails = new List<Dt_ProStockInfoDetail>();
                foreach (var item in stockInfos)
                {
                    stockInfoDetails.AddRange(item.proStockInfoDetails);
                }
                _stockService.ProStockInfoDetailService.Repository.UpdateData(stockInfoDetails);
                List<Dt_OutProStockInfo> 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;
                        });
                    }
                    _outProStockInfoService.Repository.AddData(addOutStockLockInfos);
                }
                List<Dt_OutProStockInfo> updateOutStockLockInfos = outStockLockInfos.Where(x => x.Id > 0).ToList();
                if (updateOutStockLockInfos != null && updateOutStockLockInfos.Any())
                {
                    _outProStockInfoService.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);
            }
        }
    }
}