| | |
| | | using WIDESEA_DTO.Basic; |
| | | using WIDESEA_DTO.Task; |
| | | using WIDESEA_IBasicService; |
| | | using WIDESEA_IRecordService; |
| | | using WIDESEA_Model.Models; |
| | | |
| | | namespace WIDESEA_BasicService |
| | |
| | | private readonly IMapper _mapper; |
| | | private readonly IRepository<Dt_Task> _taskRepository; |
| | | private readonly IRepository<Dt_StockInfo> _stockInfoRepository; |
| | | private readonly IRecordService _recordService; |
| | | private readonly IRepository<Dt_Warehouse> _warehouseRepository; |
| | | |
| | | /// <summary> |
| | | /// 构造函数 |
| | |
| | | IRepository<Dt_LocationInfo> baseDal, |
| | | IRepository<Dt_Task> taskRepository, |
| | | IRepository<Dt_StockInfo> stockInfoRepository, |
| | | IMapper mapper) : base(baseDal) |
| | | IRepository<Dt_Warehouse> warehouseRepository, |
| | | IMapper mapper, |
| | | IRecordService recordService) : base(baseDal) |
| | | { |
| | | _taskRepository = taskRepository; |
| | | _stockInfoRepository = stockInfoRepository; |
| | | _mapper = mapper; |
| | | _recordService = recordService; |
| | | _warehouseRepository = warehouseRepository; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | |
| | | /// <summary> |
| | | /// 根据巷道获取空闲货位信息 |
| | | /// 排序策略:深度优先(二深位优先),其次按层、列、行升序 |
| | | /// </summary> |
| | | /// <param name="roadwayNo">巷道编号</param> |
| | | /// <returns>空闲货位信息,如果未找到则返回null</returns> |
| | | /// <returns>空闲货位信息,如果空闲货位不足则返回null</returns> |
| | | public async Task<Dt_LocationInfo?> GetLocationInfo(string roadwayNo) |
| | | { |
| | | var locations = await BaseDal.QueryDataAsync(x => |
| | | x.EnableStatus == EnableStatusEnum.Normal.GetHashCode() && |
| | | x.RoadwayNo == roadwayNo && |
| | | x.LocationStatus == LocationStatusEnum.Free.GetHashCode()); |
| | | // HC 巷道使用 Capacity 类型,其他巷道使用 ShelfCapacity 类型 |
| | | var locationType = roadwayNo.Contains("HC") |
| | | ? (int)LocationTypeEnum.Capacity |
| | | : (int)LocationTypeEnum.ShelfCapacity; |
| | | |
| | | return locations? |
| | | var enableStatus = EnableStatusEnum.Normal.GetHashCode(); |
| | | var freeStatus = LocationStatusEnum.Free.GetHashCode(); |
| | | |
| | | // 数据库端 COUNT 检查空闲货位数量(仅返回一个数字,无数据传输开销) |
| | | var freeCount = await BaseDal.Db.Queryable<Dt_LocationInfo>() |
| | | .Where(x => x.EnableStatus == enableStatus |
| | | && x.RoadwayNo == roadwayNo |
| | | && x.LocationStatus == freeStatus |
| | | && x.LocationType == locationType) |
| | | .CountAsync(); |
| | | |
| | | // 空闲货位不足最低保留数量时返回null,避免将巷道分配耗尽 |
| | | const int minFreeLocationThreshold = 5; |
| | | if (freeCount < minFreeLocationThreshold) return null; |
| | | |
| | | // 数据库端排序取第一条(只传输单行数据) |
| | | return await BaseDal.Db.Queryable<Dt_LocationInfo>() |
| | | .Where(x => x.EnableStatus == enableStatus |
| | | && x.RoadwayNo == roadwayNo |
| | | && x.LocationStatus == freeStatus |
| | | && x.LocationType == locationType) |
| | | .OrderByDescending(x => x.Depth) |
| | | .OrderBy(x => x.Layer) |
| | | .ThenByDescending(x => x.Depth) |
| | | .ThenBy(x => x.Column) |
| | | .ThenBy(x => x.Row) |
| | | .FirstOrDefault(); |
| | | .OrderBy(x => x.Column) |
| | | .OrderBy(x => x.Row) |
| | | .FirstAsync(); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据货位ID获取货位信息 |
| | | /// </summary> |
| | | /// <param name="id">货位id</param> |
| | | /// <returns>货位信息</returns> |
| | | public async Task<Dt_LocationInfo> GetLocationInfoAsync(int id) |
| | | { |
| | | return await BaseDal.QueryFirstAsync(x => x.Id == id); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 更新货位信息 |
| | | /// </summary> |
| | | /// <param name="locationInfo">货位信息对象</param> |
| | | /// <returns>更新是否成功</returns> |
| | | public async Task<bool> UpdateLocationInfoAsync(Dt_LocationInfo locationInfo) |
| | | { |
| | | return await BaseDal.UpdateDataAsync(locationInfo); |
| | | var beforeLocation = await BaseDal.QueryFirstAsync(x => x.Id == locationInfo.Id); |
| | | var result = await BaseDal.UpdateDataAsync(locationInfo); |
| | | if (!result) |
| | | return false; |
| | | |
| | | return beforeLocation == null |
| | | || await _recordService.AddLocationChangeRecordAsync(beforeLocation, locationInfo, LocationChangeType.HandUpdate, remark: "货位更新"); |
| | | } |
| | | |
| | | public override WebResponseContent UpdateData(Dt_LocationInfo entity) |
| | | { |
| | | var beforeLocation = BaseDal.QueryFirst(x => x.Id == entity.Id); |
| | | var result = base.UpdateData(entity); |
| | | if (!result.Status || beforeLocation == null) |
| | | return result; |
| | | |
| | | var saveRecordResult = _recordService.AddLocationChangeRecordAsync(beforeLocation, entity, LocationChangeType.HandUpdate, remark: "货位更新").GetAwaiter().GetResult(); |
| | | return saveRecordResult ? result : WebResponseContent.Instance.Error("货位状态变更记录保存失败"); |
| | | } |
| | | |
| | | public override WebResponseContent UpdateData(List<Dt_LocationInfo> entities) |
| | | { |
| | | var beforeLocations = entities |
| | | .Select(entity => BaseDal.QueryFirst(x => x.Id == entity.Id)) |
| | | .Where(location => location != null) |
| | | .ToDictionary(location => location!.Id, location => location!); |
| | | |
| | | var result = base.UpdateData(entities); |
| | | if (!result.Status) |
| | | return result; |
| | | |
| | | foreach (var entity in entities) |
| | | { |
| | | if (!beforeLocations.TryGetValue(entity.Id, out var beforeLocation)) |
| | | continue; |
| | | |
| | | var saveRecordResult = _recordService.AddLocationChangeRecordAsync(beforeLocation, entity, LocationChangeType.HandUpdate, remark: "批量货位更新").GetAwaiter().GetResult(); |
| | | if (!saveRecordResult) |
| | | return WebResponseContent.Instance.Error("货位状态变更记录保存失败"); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | // 判断该位置是否有库存 |
| | | var stockInfo = await _stockInfoRepository.QueryDataNavFirstAsync(x => |
| | | x.LocationCode == newLocationID && |
| | | x.StockStatus == StockStatusEmun.入库完成.GetHashCode() && |
| | | (x.StockStatus == StockStatusEmun.入库完成.GetHashCode() || x.StockStatus == StockStatusEmun.空托盘库存.GetHashCode()) && |
| | | x.LocationDetails.LocationStatus == LocationStatusEnum.InStock.GetHashCode()); |
| | | if (stockInfo == null) |
| | | { |
| | |
| | | }; |
| | | |
| | | var createdTask = await _taskRepository.Db.Insertable(newTransferTask).ExecuteReturnEntityAsync(); |
| | | var beforeStock = CloneStockSnapshot(stockInfo); |
| | | var beforeSourceLocation = stockInfo.LocationDetails == null ? null : CloneLocationSnapshot(stockInfo.LocationDetails); |
| | | var beforeTargetLocation = CloneLocationSnapshot(emptyLocation); |
| | | |
| | | // 创建移库任务后,立即锁定库存和相关货位,避免并发重复分配 |
| | | stockInfo.StockStatus = StockStatusEmun.移库锁定.GetHashCode(); |
| | |
| | | throw new Exception("创建移库任务后更新库存状态或货位状态失败"); |
| | | } |
| | | |
| | | var saveStockRecordResult = await _recordService.AddStockChangeRecordAsync( |
| | | beforeStock, |
| | | stockInfo, |
| | | StockChangeTypeEnum.Relocation, |
| | | createdTask.TaskNum, |
| | | createdTask.OrderNo, |
| | | "移库任务预占库存"); |
| | | if (!saveStockRecordResult) |
| | | { |
| | | throw new Exception("创建移库任务后记录库存变更失败"); |
| | | } |
| | | |
| | | if (beforeSourceLocation != null && stockInfo.LocationDetails != null) |
| | | { |
| | | var saveSourceLocationRecordResult = await _recordService.AddLocationChangeRecordAsync( |
| | | beforeSourceLocation, |
| | | stockInfo.LocationDetails, |
| | | LocationChangeType.RelocationAssignLocation, |
| | | createdTask.TaskNum, |
| | | createdTask.OrderNo, |
| | | null, |
| | | "移库任务锁定源货位"); |
| | | if (!saveSourceLocationRecordResult) |
| | | { |
| | | throw new Exception("创建移库任务后记录源货位变更失败"); |
| | | } |
| | | } |
| | | |
| | | var saveTargetLocationRecordResult = await _recordService.AddLocationChangeRecordAsync( |
| | | beforeTargetLocation, |
| | | emptyLocation, |
| | | LocationChangeType.RelocationAssignLocation, |
| | | createdTask.TaskNum, |
| | | createdTask.OrderNo, |
| | | null, |
| | | "移库任务锁定目标货位"); |
| | | if (!saveTargetLocationRecordResult) |
| | | { |
| | | throw new Exception("创建移库任务后记录目标货位变更失败"); |
| | | } |
| | | |
| | | return createdTask; |
| | | } |
| | | |
| | | private static Dt_LocationInfo CloneLocationSnapshot(Dt_LocationInfo location) |
| | | { |
| | | return new Dt_LocationInfo |
| | | { |
| | | Id = location.Id, |
| | | WarehouseId = location.WarehouseId, |
| | | LocationCode = location.LocationCode, |
| | | LocationName = location.LocationName, |
| | | RoadwayNo = location.RoadwayNo, |
| | | Row = location.Row, |
| | | Column = location.Column, |
| | | Layer = location.Layer, |
| | | Depth = location.Depth, |
| | | LocationType = location.LocationType, |
| | | LocationStatus = location.LocationStatus, |
| | | EnableStatus = location.EnableStatus, |
| | | Remark = location.Remark |
| | | }; |
| | | } |
| | | |
| | | private static Dt_StockInfo CloneStockSnapshot(Dt_StockInfo stockInfo) |
| | | { |
| | | return new Dt_StockInfo |
| | | { |
| | | Id = stockInfo.Id, |
| | | PalletCode = stockInfo.PalletCode, |
| | | PalletType = stockInfo.PalletType, |
| | | LocationId = stockInfo.LocationId, |
| | | LocationCode = stockInfo.LocationCode, |
| | | WarehouseId = stockInfo.WarehouseId, |
| | | StockStatus = stockInfo.StockStatus, |
| | | Remark = stockInfo.Remark, |
| | | OutboundDate = stockInfo.OutboundDate, |
| | | Details = stockInfo.Details?.Select(detail => new Dt_StockInfoDetail |
| | | { |
| | | Id = detail.Id, |
| | | StockId = detail.StockId, |
| | | MaterielCode = detail.MaterielCode, |
| | | MaterielName = detail.MaterielName, |
| | | OrderNo = detail.OrderNo, |
| | | BatchNo = detail.BatchNo, |
| | | ProductionDate = detail.ProductionDate, |
| | | EffectiveDate = detail.EffectiveDate, |
| | | SerialNumber = detail.SerialNumber, |
| | | StockQuantity = detail.StockQuantity, |
| | | OutboundQuantity = detail.OutboundQuantity, |
| | | Status = detail.Status, |
| | | Unit = detail.Unit, |
| | | InboundOrderRowNo = detail.InboundOrderRowNo, |
| | | Remark = detail.Remark |
| | | }).ToList() |
| | | }; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <param name="layer">层数</param> |
| | | /// <param name="depth">深度</param> |
| | | /// <returns>货位信息对象</returns> |
| | | private static Dt_LocationInfo CreateLocationInfo(string roadwayNo, int row, int col, int layer, int depth) |
| | | private Dt_LocationInfo CreateLocationInfo(string roadwayNo, int row, int col, int layer, int depth) |
| | | { |
| | | var warehouse = _warehouseRepository.QueryData(x => x.WarehouseCode == roadwayNo).FirstOrDefault(); |
| | | |
| | | if (warehouse == null) |
| | | { |
| | | throw new InvalidOperationException($"未找到巷道编号为 {roadwayNo} 的仓库信息"); |
| | | } |
| | | |
| | | return new Dt_LocationInfo |
| | | { |
| | | WarehouseId = 0, |
| | | WarehouseId = warehouse.WarehouseId, |
| | | Row = row, |
| | | Column = col, |
| | | Layer = layer, |
| | |
| | | EnableStatus = EnableStatusEnum.Normal.GetHashCode(), |
| | | LocationStatus = LocationStatusEnum.Free.GetHashCode(), |
| | | LocationType = LocationTypeEnum.Undefined.GetHashCode(), |
| | | //LocationCode = $"{roadwayNo}-{row:D3}-{col:D3}-{layer:D3}", |
| | | LocationCode = $"{row:D3}-{col:D3}-{layer:D3}", |
| | | LocationName = $"{roadwayNo}巷道{row:D3}行{col:D3}列{layer:D3}层{depth:D2}深" |
| | | }; |
| | |
| | | |
| | | #endregion 私有方法 |
| | | } |
| | | } |
| | | } |