using AutoMapper; using WIDESEA_Common.StockEnum; using WIDESEA_Core.BaseRepository; using WIDESEA_Core.BaseServices; using WIDESEA_IBasicService; using WIDESEA_IRecordService; using WIDESEA_IStockService; using WIDESEA_Model.Models; namespace WIDESEA_StockService { public partial class StockInfoService : ServiceBase>, IStockInfoService { private readonly IMapper _mapper; private readonly IRecordService _recordService; public IRepository Repository => BaseDal; private readonly IRepository _stockInfoDetailRepository; private readonly ILocationInfoService _locationInfoService; public StockInfoService(IRepository BaseDal, IMapper mapper, IRepository stockInfoDetailRepository, IRecordService recordService, ILocationInfoService locationInfoService) : base(BaseDal) { _mapper = mapper; _stockInfoDetailRepository = stockInfoDetailRepository; _recordService = recordService; _locationInfoService = locationInfoService; } /// /// 根据托盘号查询库存 /// /// /// public Dt_StockInfo? GetStockByPalletCode(string palletCode) { Dt_StockInfo stockInfo = BaseDal.QueryFirst(x => x.PalletCode == palletCode); if (stockInfo != null) { stockInfo.Details = _stockInfoDetailRepository.QueryData(x => x.StockId == stockInfo.Id); } return stockInfo; } public void AddMaterielGroup(Dt_StockInfo stockInfo) { decimal beforeQuantity = 0; List details = new List(); if (stockInfo.Id == 0) { if (stockInfo.Details != null && stockInfo.Details.Any()) { BaseDal.Db.InsertNav(stockInfo).Include(x => x.Details).ExecuteCommand(); } else { BaseDal.AddData(stockInfo); } details = stockInfo.Details; } else { beforeQuantity = stockInfo.Details.Where(x => x.Id != 0).Sum(x => x.StockQuantity); for (int i = 0; i < stockInfo.Details.Count; i++) { if (stockInfo.Details[i].Id == 0) { details.Add(_stockInfoDetailRepository.Db.Insertable(stockInfo.Details[i]).ExecuteReturnEntity()); } } } stockInfo.Details = details; _recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfo, stockInfo.Details, beforeQuantity, stockInfo.Details.Sum(x => x.StockQuantity) + beforeQuantity, WIDESEA_Common.StockEnum.StockChangeType.MaterielGroup); } /// /// /// /// /// /// /// /// public List GetOutboundStocks(List stockInfos, string materielCode, decimal needQuantity, out decimal residueQuantity) { List outStocks = new List(); var stockTotalQuantity = stockInfos.Select(x => x.Details.Sum(v => v.StockQuantity - v.OutboundQuantity)).Sum(x => x); //stockInfos = stockInfos.OrderBy(x => x.Id).ToList(); if (stockTotalQuantity >= needQuantity)//库存够 { int index = 0; while (needQuantity > 0) { Dt_StockInfo stockInfo = stockInfos[index]; // 计算可用库存时转换为decimal decimal useableStockQuantity = stockInfo.Details .Where(x => x.MaterielCode == materielCode) .Sum(x => (decimal)x.StockQuantity - (decimal)x.OutboundQuantity); // 将needQuantity转换为decimal进行比较 if (useableStockQuantity < (decimal)needQuantity && useableStockQuantity > 0) { stockInfo.Details.ForEach(x => x.OutboundQuantity = x.StockQuantity); // 使用decimal进行计算后再转回float needQuantity = needQuantity - useableStockQuantity; } else { stockInfo.Details.ForEach(x => { if (x.StockQuantity > x.OutboundQuantity && x.MaterielCode == materielCode) { // 将相关值转换为decimal进行精确计算 decimal currentStock = (decimal)x.StockQuantity; decimal currentOutbound = (decimal)x.OutboundQuantity; decimal currentNeed = (decimal)needQuantity; decimal available = currentStock - currentOutbound; if (available >= currentNeed) { x.OutboundQuantity = currentOutbound + currentNeed; needQuantity = 0; } else { needQuantity =currentNeed - available; x.OutboundQuantity = x.StockQuantity; } } }); } outStocks.Add(stockInfo); index++; } } else { throw new Exception("库存不足"); } residueQuantity = needQuantity; return outStocks; } public List GetStockInfos(string materielCode, string lotNo, List locationCodes) { List stockInfos = null; if (!string.IsNullOrEmpty(lotNo)) { var stockSort = Db.Queryable().Where(x => locationCodes.Contains(x.LocationCode)).Includes(x => x.Details).Where(x => x.Details.Any(v => v.MaterielCode == materielCode && v.BatchNo == lotNo)).ToList(); stockInfos = stockSort.OrderBy(x => x.Details.FirstOrDefault()?.EffectiveDate).ThenBy(x => x.Details.Sum(v => v.StockQuantity)).ToList(); } else { var stockSort = Db.Queryable().Where(x => locationCodes.Contains(x.LocationCode)).Includes(x => x.Details).Where(x => x.Details.Any(v => v.MaterielCode == materielCode)).ToList(); stockInfos = stockSort.OrderBy(x => x.Details.FirstOrDefault()?.EffectiveDate).ThenBy(x => x.Details.Sum(v => v.StockQuantity)).ToList(); } return stockInfos; //ISugarQueryable sugarQueryable = Db.Queryable().Where(x => locationCodes.Contains(x.LocationCode)); //ISugarQueryable sugarQueryable1 = Db.Queryable().Includes(x => x.Details).Where(x => x.Details.Any(v => v.MaterielCode == materielCode)); //return sugarQueryable.InnerJoin(sugarQueryable1, (a, b) => a.LocationCode == b.LocationCode).Select((a, b) => b).OrderBy(a => a.CreateDate).Includes(a => a.Details).ToList(); } public List GetUseableStocks(string materielCode, string batchNo) { List locationCodes = _locationInfoService.GetCanOutLocationCodes(); return GetStockInfos(materielCode, batchNo, locationCodes); } } }