| | |
| | | using WIDESEA_Core.BaseRepository; |
| | | using WIDESEA_Core.BaseServices; |
| | | using WIDESEA_DTO.Stock; |
| | | using WIDESEA_IBasicService; |
| | | using WIDESEA_IStockService; |
| | | using WIDESEA_Model.Models; |
| | | |
| | |
| | | public IRepository<Dt_StockInfo> Repository => BaseDal; |
| | | |
| | | /// <summary> |
| | | /// 货位信息服务接口(用于获取仓库货位信息) |
| | | /// </summary> |
| | | private readonly ILocationInfoService _locationInfoService; |
| | | |
| | | /// <summary> |
| | | /// 仓库信息服务接口(用于获取仓库基本信息) |
| | | /// </summary> |
| | | private readonly IWarehouseService _warehouseService; |
| | | |
| | | /// <summary> |
| | | /// 构造函数 |
| | | /// </summary> |
| | | /// <param name="baseDal">基础数据访问对象</param> |
| | | public StockInfoService(IRepository<Dt_StockInfo> baseDal) : base(baseDal) |
| | | public StockInfoService(IRepository<Dt_StockInfo> baseDal, ILocationInfoService locationInfoService, IWarehouseService warehouseService) : base(baseDal) |
| | | { |
| | | _locationInfoService = locationInfoService; |
| | | _warehouseService = warehouseService; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | public async Task<Stock3DLayoutDTO> Get3DLayoutAsync(int warehouseId) |
| | | { |
| | | // 1. 查询仓库信息 |
| | | var warehouse = await Repository.Change<Dt_Warehouse>().GetFirstAsync(x => x.Id == warehouseId); |
| | | var warehouse = await _warehouseService.Repository.QueryFirstAsync(x => x.WarehouseId == warehouseId); |
| | | |
| | | // 2. 查询该仓库所有货位 |
| | | var locations = await Repository.Change<Dt_LocationInfo>().GetListAsync(x => x.WarehouseId == warehouseId); |
| | | var locations = await _locationInfoService.Repository.QueryDataAsync(x => x.WarehouseId == warehouseId); |
| | | |
| | | // 3. 查询该仓库所有库存信息(包含Details导航属性) |
| | | var stockInfos = await Repository.Change<Dt_StockInfo>().Includes(x => x.Details).GetListAsync(x => x.WarehouseId == warehouseId); |
| | | var stockInfos = await Repository.QueryDataNavAsync(x => x.WarehouseId == warehouseId && x.LocationId != 0); |
| | | |
| | | // 4. 提取物料编号和批次号列表(去重) |
| | | var materielCodeList = stockInfos |
| | | .Where(s => s.Details != null) |
| | | .SelectMany(s => s.Details) |
| | | .Select(d => d.MaterielCode) |
| | | .Where(c => !string.IsNullOrEmpty(c)) |
| | |
| | | .ToList(); |
| | | |
| | | var batchNoList = stockInfos |
| | | .Where(s => s.Details != null) |
| | | .SelectMany(s => s.Details) |
| | | .Select(d => d.BatchNo) |
| | | .Where(b => !string.IsNullOrEmpty(b)) |
| | |
| | | // 尝试从库存字典中获取库存信息 |
| | | if (stockDict.TryGetValue(loc.Id, out var stockInfo)) |
| | | { |
| | | // 空托盘也有库存记录,只是不包含明细 |
| | | item.PalletCode = stockInfo.PalletCode; |
| | | item.StockQuantity = stockInfo.Details.Sum(d => d.StockQuantity); |
| | | item.StockStatus = stockInfo.StockStatus; // 直接使用后端库存状态 |
| | | |
| | | // 获取第一个明细的物料信息(如果存在) |
| | | var firstDetail = stockInfo.Details.FirstOrDefault(); |
| | | if (firstDetail != null) |
| | | // 只有当Details不为null且有数据时才处理库存明细 |
| | | if (stockInfo.Details != null && stockInfo.Details.Any()) |
| | | { |
| | | item.MaterielCode = firstDetail.MaterielCode; |
| | | item.MaterielName = firstDetail.MaterielName; |
| | | item.BatchNo = firstDetail.BatchNo; |
| | | } |
| | | item.StockQuantity = stockInfo.Details.Sum(d => d.StockQuantity); |
| | | |
| | | // 计算库存状态 |
| | | var ratio = item.MaxCapacity > 0 ? item.StockQuantity / item.MaxCapacity : 0; |
| | | if (ratio >= 0.9f) |
| | | item.StockStatus = 3; // 已满 (FULL) |
| | | else if (ratio >= 0.1f) |
| | | item.StockStatus = 1; // 有货 (HAS_STOCK) |
| | | else if (ratio > 0) |
| | | item.StockStatus = 2; // 库存紧张 (LOW_STOCK) |
| | | // 获取第一个明细的物料信息(如果存在) |
| | | var firstDetail = stockInfo.Details.FirstOrDefault(); |
| | | if (firstDetail != null) |
| | | { |
| | | item.MaterielCode = firstDetail.MaterielCode; |
| | | item.MaterielName = firstDetail.MaterielName; |
| | | item.BatchNo = firstDetail.BatchNo; |
| | | } |
| | | |
| | | // 填充库存明细列表 |
| | | item.Details = stockInfo.Details.Select(d => new StockDetailItemDTO |
| | | { |
| | | Id = d.Id, |
| | | MaterielCode = d.MaterielCode, |
| | | MaterielName = d.MaterielName, |
| | | BatchNo = d.BatchNo, |
| | | StockQuantity = d.StockQuantity, |
| | | Unit = d.Unit, |
| | | ProductionDate = d.ProductionDate, |
| | | EffectiveDate = d.EffectiveDate, |
| | | OrderNo = d.OrderNo, |
| | | Status = d.Status |
| | | }).ToList(); |
| | | } |
| | | else |
| | | item.StockStatus = 0; // 无货 (EMPTY) |
| | | { |
| | | // 空托盘(无明细) |
| | | item.StockQuantity = 0; |
| | | item.Details = new List<StockDetailItemDTO>(); // 确保是空列表而非null |
| | | } |
| | | } |
| | | else |
| | | { |
| | | item.StockStatus = 0; // 无货 (EMPTY) |
| | | // 无库存记录,货位为空 |
| | | item.StockStatus = 0; // 空闲 |
| | | item.StockQuantity = 0; |
| | | } |
| | | |