| | |
| | | using WIDESEA_Common.LocationEnum; |
| | | using Mapster; |
| | | using MapsterMapper; |
| | | using WIDESEA_Common.CommonEnum; |
| | | using WIDESEA_Common.LocationEnum; |
| | | using WIDESEA_Common.StockEnum; |
| | | using WIDESEA_Common.TaskEnum; |
| | | using WIDESEA_Core; |
| | | using WIDESEA_Core.BaseRepository; |
| | | using WIDESEA_Core.BaseServices; |
| | | using WIDESEA_Core.Utilities; |
| | | using WIDESEA_DTO.Basic; |
| | | using WIDESEA_DTO.Task; |
| | | using WIDESEA_IBasicService; |
| | | using WIDESEA_Model.Models; |
| | | |
| | | namespace WIDESEA_BasicService |
| | | { |
| | | /// <summary> |
| | | /// 货位信息服务实现类 |
| | | /// </summary> |
| | | public partial class LocationInfoService : ServiceBase<Dt_LocationInfo, IRepository<Dt_LocationInfo>>, ILocationInfoService |
| | | { |
| | | public IRepository<Dt_LocationInfo> Repository => BaseDal; |
| | | public IRepository<Dt_Task> _taskRepository { get; } |
| | | private readonly IMapper _mapper; |
| | | private readonly IRepository<Dt_Task> _taskRepository; |
| | | private readonly IRepository<Dt_StockInfo> _stockInfoRepository; |
| | | |
| | | public IRepository<Dt_StockInfo> _stockInfoRepository { get; set; } |
| | | |
| | | public LocationInfoService(IRepository<Dt_LocationInfo> BaseDal, IRepository<Dt_Task> taskRepository, IRepository<Dt_StockInfo> stockInfoRepository) : base(BaseDal) |
| | | /// <summary> |
| | | /// 构造函数 |
| | | /// </summary> |
| | | /// <param name="baseDal">基础数据访问对象</param> |
| | | /// <param name="taskRepository">任务仓储</param> |
| | | /// <param name="stockInfoRepository">库存信息仓储</param> |
| | | public LocationInfoService( |
| | | IRepository<Dt_LocationInfo> baseDal, |
| | | IRepository<Dt_Task> taskRepository, |
| | | IRepository<Dt_StockInfo> stockInfoRepository, |
| | | IMapper mapper) : base(baseDal) |
| | | { |
| | | _taskRepository = taskRepository; |
| | | _stockInfoRepository = stockInfoRepository; |
| | | _mapper = mapper; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取货位信息仓储 |
| | | /// </summary> |
| | | public IRepository<Dt_LocationInfo> Repository => BaseDal; |
| | | |
| | | /// <summary> |
| | | /// 批量启用货位 |
| | | /// </summary> |
| | | /// <param name="keys">货位主键数组</param> |
| | | /// <returns>操作结果</returns> |
| | | public WebResponseContent LocationEnableStatus(int[] keys) |
| | | { |
| | | var locationInfos = Repository.QueryData(x => keys.Contains(x.Id)); |
| | |
| | | /// <summary> |
| | | /// 批量禁用货位 |
| | | /// </summary> |
| | | /// <param name="keys">货位主键数组</param> |
| | | /// <returns>操作结果</returns> |
| | | public WebResponseContent LocationDisableStatus(int[] keys) |
| | | { |
| | | var locationInfos = Repository.QueryData(x => keys.Contains(x.Id)); |
| | |
| | | /// <summary> |
| | | /// 单个启用货位 |
| | | /// </summary> |
| | | /// <param name="key">货位主键</param> |
| | | /// <returns>操作结果</returns> |
| | | public WebResponseContent LocationEnableStatus(int key) => LocationEnableStatus(new[] { key }); |
| | | |
| | | /// <summary> |
| | | /// 单个禁用货位 |
| | | /// </summary> |
| | | /// <param name="key">货位主键</param> |
| | | /// <returns>操作结果</returns> |
| | | public WebResponseContent LocationDisableStatus(int key) => LocationDisableStatus(new[] { key }); |
| | | |
| | | /// <summary> |
| | | /// 初始化货位 |
| | | /// </summary> |
| | | /// <param name="dto">初始化货位数据传输对象</param> |
| | | /// <returns>操作结果</returns> |
| | | public WebResponseContent InitializationLocation(InitializationLocationDTO dto) |
| | | { |
| | | try |
| | |
| | | } |
| | | } |
| | | |
| | | private static int CalculateDepth(int row, int maxRow, int maxDepth, int currentDepth) |
| | | { |
| | | int mod = row % maxRow; |
| | | if (mod == 1) return maxDepth; |
| | | if (mod == maxDepth + 1) return 1; |
| | | if (mod > 1 && mod <= maxDepth) return currentDepth - 1; |
| | | return currentDepth + 1; |
| | | } |
| | | |
| | | private static Dt_LocationInfo CreateLocationInfo(string roadwayNo, int row, int col, int layer, int depth) |
| | | { |
| | | return new Dt_LocationInfo |
| | | { |
| | | WarehouseId = 0, |
| | | Row = row, |
| | | Column = col, |
| | | Layer = layer, |
| | | Depth = depth, |
| | | RoadwayNo = roadwayNo, |
| | | EnableStatus = EnableStatusEnum.Normal.GetHashCode(), |
| | | LocationStatus = LocationStatusEnum.Free.GetHashCode(), |
| | | LocationType = LocationTypeEnum.Undefined.GetHashCode(), |
| | | LocationCode = $"{row:D3}-{col:D3}-{layer:D3}", //$"{roadwayNo}-{row:D3}-{col:D3}-{layer:D3}-{depth:D2}" |
| | | LocationName = $"{roadwayNo}巷道{row:D3}行{col:D3}列{layer:D3}层{depth:D2}深" |
| | | }; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取空闲货位信息(根据巷道查询) |
| | | /// 根据巷道获取空闲货位信息 |
| | | /// </summary> |
| | | /// <param name="roadwayNo">巷道编号</param> |
| | | /// <returns>空闲货位信息,如果未找到则返回null</returns> |
| | | public async Task<Dt_LocationInfo?> GetLocationInfo(string roadwayNo) |
| | | { |
| | | var locations = await BaseDal.QueryDataAsync(x => |
| | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取货位信息(根据巷道和货位编码查询) |
| | | /// 根据巷道和货位编码获取空闲货位信息 |
| | | /// </summary> |
| | | /// <param name="roadwayNo">巷道编号</param> |
| | | /// <param name="locationCode">货位编码</param> |
| | | /// <returns>货位信息,如果未找到则返回null</returns> |
| | | public async Task<Dt_LocationInfo?> GetLocationInfo(string roadwayNo, string locationCode) |
| | | { |
| | | return await BaseDal.QueryFirstAsync(x => x.RoadwayNo == roadwayNo && x.LocationCode == locationCode); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据货位编码获取货位信息 |
| | | /// </summary> |
| | | /// <param name="locationCode">货位编码</param> |
| | | /// <returns>货位信息</returns> |
| | | public async Task<Dt_LocationInfo> GetLocationInfoAsync(string locationCode) |
| | | { |
| | | return await BaseDal.QueryFirstAsync(x => x.LocationCode == locationCode); |
| | |
| | | /// <summary> |
| | | /// 更新货位信息 |
| | | /// </summary> |
| | | /// <param name="locationInfo">货位信息对象</param> |
| | | /// <returns>更新是否成功</returns> |
| | | public async Task<bool> UpdateLocationInfoAsync(Dt_LocationInfo locationInfo) |
| | | { |
| | | return await BaseDal.UpdateDataAsync(locationInfo); |
| | |
| | | /// <summary> |
| | | /// 检查并生成移库任务或返回出库任务 |
| | | /// </summary> |
| | | /// <param name="locationID">任务号</param> |
| | | /// <returns>任务对象</returns> |
| | | /// <param name="taskNum">任务号</param> |
| | | /// <returns>任务信息</returns> |
| | | public async Task<WebResponseContent> TransferCheckAsync(int taskNum) |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | var content = new WebResponseContent(); |
| | | try |
| | | { |
| | | // 根据任务号获取任务 |
| | | var outboundTask = await _taskRepository.QueryFirstAsync(x => x.TaskNum == taskNum); |
| | | if (outboundTask == null) |
| | | return null; |
| | | return content.Error("任务不存在"); |
| | | |
| | | var location = await BaseDal.QueryFirstAsync(x => x.LocationCode == outboundTask.SourceAddress); |
| | | var location = await BaseDal.QueryFirstAsync(x => x.LocationCode == outboundTask.SourceAddress && x.RoadwayNo == outboundTask.Roadway); |
| | | |
| | | // 检查是否需要进行移库 |
| | | if (CheckForInternalTransfer(location)) |
| | | { |
| | | // 计算对应位置的相对库位 (奇数行的下一行或者偶数行的上一行) |
| | | // 计算对应位置的相对库位(奇数行的下一行或者偶数行的上一行) |
| | | var newLocationID = GetRelativeLocationID(location); |
| | | |
| | | // 获取新的库位的任务 |
| | |
| | | // 如果新的库位没有找到对应的任务 |
| | | if (internalTransferTask == null) |
| | | { |
| | | return content.OK("获取到移库任务", await HandleNoTaskAtLocation(outboundTask.SourceAddress, newLocationID, outboundTask)); |
| | | return content.OK("获取到移库任务", await HandleNoTaskAtLocation(newLocationID, outboundTask)); |
| | | } |
| | | |
| | | // 直接返回一深位出库任务 |
| | |
| | | } |
| | | } |
| | | |
| | | #region 移库方法 |
| | | #region 私有方法 |
| | | |
| | | /// <summary> |
| | | /// 计算相对的库位ID |
| | | /// </summary> |
| | | /// <param name="locationID">当前库位ID</param> |
| | | /// <param name="locationInfo">货位信息</param> |
| | | /// <returns>相对的库位ID</returns> |
| | | private string GetRelativeLocationID(Dt_LocationInfo locationInfo) |
| | | private static string GetRelativeLocationID(Dt_LocationInfo locationInfo) |
| | | { |
| | | int line = locationInfo.Row; |
| | | |
| | |
| | | int relativeLine = line % 2 == 1 ? line + 1 : line - 1; |
| | | |
| | | // 构建新的库位ID |
| | | string[] newLocationParts = new string[] { relativeLine.ToString().PadLeft(3, '0'), locationInfo.Column.ToString(), locationInfo.Layer.ToString() }; |
| | | string[] newLocationParts = new string[] { relativeLine.ToString().PadLeft(3, '0'), locationInfo.Column.ToString().PadLeft(3, '0'), locationInfo.Layer.ToString().PadLeft(3, '0') }; |
| | | return string.Join("-", newLocationParts); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 处理没有任务的库位情况 |
| | | /// </summary> |
| | | /// <param name="originalLocationID">原始库位ID</param> |
| | | /// <param name="newLocationID">新的库位ID</param> |
| | | /// <param name="outboundTask">出库任务</param> |
| | | /// <returns>生成的移库任务或原始出库任务</returns> |
| | | private async Task<Dt_Task> HandleNoTaskAtLocation(string originalLocationID, string newLocationID, Dt_Task outboundTask) |
| | | private async Task<Dt_Task> HandleNoTaskAtLocation(string newLocationID, Dt_Task outboundTask) |
| | | { |
| | | // 判断该位置是否有库存 |
| | | var stockInfo = await _stockInfoRepository.QueryFirstAsync(x => x.LocationCode == newLocationID); |
| | | |
| | | var stockInfo = await _stockInfoRepository.QueryDataNavFirstAsync(x => |
| | | x.LocationCode == newLocationID && |
| | | x.StockStatus == StockStatusEmun.入库完成.GetHashCode() && |
| | | x.LocationDetails.LocationStatus == LocationStatusEnum.InStock.GetHashCode()); |
| | | if (stockInfo == null) |
| | | { |
| | | // 如果没有库存,直接返回当前出库任务 |
| | | return outboundTask; |
| | | } |
| | | else |
| | | { |
| | | // 如果有库存,生成移库任务 |
| | | var emptyLocation = await GetTransferLocationEmptyAsync(outboundTask.Roadway); |
| | | var taskNo = await _taskRepository.GetTaskNo(); |
| | | Dt_Task newTransferTask = new Dt_Task() |
| | | { |
| | | CreateDate = DateTime.Now, |
| | | Creater = App.User.UserName, |
| | | CurrentAddress = originalLocationID, |
| | | Grade = 99, |
| | | NextAddress = emptyLocation.LocationCode, |
| | | PalletCode = stockInfo.PalletCode, |
| | | Remark = "移库", |
| | | Roadway = stockInfo.LocationDetails.RoadwayNo, |
| | | SourceAddress = originalLocationID, |
| | | TaskNum = taskNo, |
| | | TargetAddress = emptyLocation.LocationCode, |
| | | TaskType = TaskTypeEnum.Relocation.GetHashCode(), |
| | | }; |
| | | |
| | | return await _taskRepository.Db.Insertable(newTransferTask).ExecuteReturnEntityAsync(); |
| | | // 如果有库存,生成移库任务 |
| | | var emptyLocation = await GetTransferLocationEmptyAsync(outboundTask.Roadway); |
| | | var taskNo = await _taskRepository.GetTaskNo(); |
| | | |
| | | var newTransferTask = new Dt_Task |
| | | { |
| | | CreateDate = DateTime.Now, |
| | | Creater = App.User.UserName ?? "system", |
| | | CurrentAddress = newLocationID, |
| | | Grade = 99, |
| | | NextAddress = emptyLocation.LocationCode, |
| | | PalletCode = stockInfo.PalletCode, |
| | | Remark = "移库", |
| | | Roadway = stockInfo.LocationDetails.RoadwayNo, |
| | | SourceAddress = newLocationID, |
| | | TaskNum = taskNo, |
| | | TargetAddress = emptyLocation.LocationCode, |
| | | TaskType = TaskRelocationTypeEnum.Relocation.GetHashCode(), |
| | | TaskStatus = TaskRelocationStatusEnum.RelocationNew.GetHashCode(), |
| | | WarehouseId = stockInfo.WarehouseId |
| | | }; |
| | | |
| | | var createdTask = await _taskRepository.Db.Insertable(newTransferTask).ExecuteReturnEntityAsync(); |
| | | |
| | | // 创建移库任务后,立即锁定库存和相关货位,避免并发重复分配 |
| | | stockInfo.StockStatus = StockStatusEmun.移库锁定.GetHashCode(); |
| | | var updateStockResult = await _stockInfoRepository.UpdateDataAsync(stockInfo); |
| | | |
| | | var locationsToUpdate = new List<Dt_LocationInfo>(); |
| | | if (stockInfo.LocationDetails != null) |
| | | { |
| | | stockInfo.LocationDetails.LocationStatus = LocationStatusEnum.InStockLock.GetHashCode(); |
| | | locationsToUpdate.Add(stockInfo.LocationDetails); |
| | | } |
| | | |
| | | emptyLocation.LocationStatus = LocationStatusEnum.FreeLock.GetHashCode(); |
| | | locationsToUpdate.Add(emptyLocation); |
| | | |
| | | var updateLocationResult = await BaseDal.UpdateDataAsync(locationsToUpdate); |
| | | if (!updateStockResult || !updateLocationResult) |
| | | { |
| | | throw new Exception("创建移库任务后更新库存状态或货位状态失败"); |
| | | } |
| | | |
| | | return createdTask; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据货位是否需要移库 |
| | | /// 检查货位是否需要移库 |
| | | /// </summary> |
| | | /// <param name="locationID">货位ID</param> |
| | | /// <param name="location">货位信息</param> |
| | | /// <returns>是否需要移库</returns> |
| | | private bool CheckForInternalTransfer(Dt_LocationInfo location) |
| | | private static bool CheckForInternalTransfer(Dt_LocationInfo location) |
| | | { |
| | | return location.Depth == 2 ? true : false; |
| | | return location.Depth == 2; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据巷道获取二深位的空库位 |
| | | /// </summary> |
| | | /// <param name="roadway">巷道</param> |
| | | /// <param name="roadway">巷道编号</param> |
| | | /// <returns>货位对象</returns> |
| | | private async Task<Dt_LocationInfo> GetTransferLocationEmptyAsync(string roadway) |
| | | { |
| | | return await BaseDal.QueryFirstAsync(x => x.Depth == 2 && x.LocationStatus == (LocationStatusEnum.Free.GetHashCode()) && x.RoadwayNo == roadway); |
| | | |
| | | //Db.Queryable<Dt_LocationInfo>() |
| | | //.Where(x => x.Status == LocationEnum.Free.ObjToInt()) |
| | | //.Where(x => x.Depth == 2.ToString()) |
| | | //.Where(x => x.Roadway == roadway) |
| | | //.First(); |
| | | return await BaseDal.QueryFirstAsync(x => x.Depth == 2 && x.LocationStatus == LocationStatusEnum.Free.GetHashCode() && x.RoadwayNo == roadway); |
| | | } |
| | | |
| | | #endregion 移库方法 |
| | | /// <summary> |
| | | /// 计算深度 |
| | | /// </summary> |
| | | /// <param name="row">行号</param> |
| | | /// <param name="maxRow">最大行数</param> |
| | | /// <param name="maxDepth">最大深度</param> |
| | | /// <param name="currentDepth">当前深度</param> |
| | | /// <returns>计算后的深度</returns> |
| | | private static int CalculateDepth(int row, int maxRow, int maxDepth, int currentDepth) |
| | | { |
| | | int mod = row % maxRow; |
| | | if (mod == 1) return maxDepth; |
| | | if (mod == maxDepth + 1) return 1; |
| | | if (mod > 1 && mod <= maxDepth) return currentDepth - 1; |
| | | return currentDepth + 1; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 创建货位信息 |
| | | /// </summary> |
| | | /// <param name="roadwayNo">巷道编号</param> |
| | | /// <param name="row">行号</param> |
| | | /// <param name="col">列号</param> |
| | | /// <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) |
| | | { |
| | | return new Dt_LocationInfo |
| | | { |
| | | WarehouseId = 0, |
| | | Row = row, |
| | | Column = col, |
| | | Layer = layer, |
| | | Depth = depth, |
| | | RoadwayNo = roadwayNo, |
| | | EnableStatus = EnableStatusEnum.Normal.GetHashCode(), |
| | | LocationStatus = LocationStatusEnum.Free.GetHashCode(), |
| | | LocationType = LocationTypeEnum.Undefined.GetHashCode(), |
| | | LocationCode = $"{row:D3}-{col:D3}-{layer:D3}", |
| | | LocationName = $"{roadwayNo}巷道{row:D3}行{col:D3}列{layer:D3}层{depth:D2}深" |
| | | }; |
| | | } |
| | | |
| | | #endregion 私有方法 |
| | | } |
| | | } |
| | | } |