using SqlSugar; using WIDESEA_Common.CommonEnum; using WIDESEA_Common.LocationEnum; using WIDESEA_Core; using WIDESEA_Core.BaseRepository; using WIDESEA_Core.BaseServices; using WIDESEA_Core.Utilities; using WIDESEA_DTO.Basic; using WIDESEA_IBasicService; using WIDESEA_Model.Models; namespace WIDESEA_BasicService { public partial class LocationInfoService : ServiceBase>, ILocationInfoService { public IRepository Repository => BaseDal; public LocationInfoService(IRepository BaseDal) : base(BaseDal) { } /// /// 批量启用货位 /// public WebResponseContent LocationEnableStatus(int[] keys) { var locationInfos = Repository.QueryData(x => keys.Contains(x.Id)); locationInfos.ForEach(x => x.EnableStatus = EnableStatusEnum.Normal.GetHashCode()); Repository.UpdateData(locationInfos); return WebResponseContent.Instance.OK(); } /// /// 批量禁用货位 /// public WebResponseContent LocationDisableStatus(int[] keys) { var locationInfos = Repository.QueryData(x => keys.Contains(x.Id)); locationInfos.ForEach(x => x.EnableStatus = EnableStatusEnum.Disable.GetHashCode()); Repository.UpdateData(locationInfos); return WebResponseContent.Instance.OK(); } /// /// 单个启用货位 /// public WebResponseContent LocationEnableStatus(int key) => LocationEnableStatus(new[] { key }); /// /// 单个禁用货位 /// public WebResponseContent LocationDisableStatus(int key) => LocationDisableStatus(new[] { key }); /// /// 初始化货位 /// public WebResponseContent InitializationLocation(InitializationLocationDTO dto) { try { var (isValid, errorMsg, _) = ModelValidate.ValidateModelData(dto); if (!isValid) return WebResponseContent.Instance.Error(errorMsg); var locationInfos = new List(); int depth = dto.Depth; for (int row = 1; row <= dto.MaxRow; row++) { depth = CalculateDepth(row, dto.MaxRow, dto.Depth, depth); for (int col = 1; col <= dto.MaxColumn; col++) { for (int layer = 1; layer <= dto.MaxLayer; layer++) { var location = CreateLocationInfo(dto.Roadway, row, col, layer, depth); locationInfos.Add(location); } } } BaseDal.AddData(locationInfos); return WebResponseContent.Instance.OK(); } catch (Exception ex) { return WebResponseContent.Instance.Error(ex.Message); } } 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 = $"{roadwayNo}-{row:D3}-{col:D3}-{layer:D3}-{depth:D2}", LocationName = $"{roadwayNo}巷道{row:D3}行{col:D3}列{layer:D3}层{depth:D2}深" }; } /// /// 获取空闲货位信息(根据巷道查询) /// public async Task GetLocationInfo(string roadwayNo) { var locations = await BaseDal.QueryDataAsync(x => x.EnableStatus == EnableStatusEnum.Normal.GetHashCode() && x.RoadwayNo == roadwayNo && x.LocationStatus == LocationStatusEnum.Free.GetHashCode()); return locations? .OrderBy(x => x.Layer) .ThenBy(x => x.Depth) .ThenBy(x => x.Column) .ThenBy(x => x.Row) .FirstOrDefault(); } /// /// 获取货位信息(根据巷道和货位编码查询) /// public async Task GetLocationInfo(string roadwayNo, string locationCode) { return await BaseDal.QueryFirstAsync(x => x.RoadwayNo == roadwayNo && x.LocationCode == locationCode); } /// /// 更新货位信息 /// public async Task UpdateLocationInfoAsync(Dt_LocationInfo locationInfo) { return await BaseDal.UpdateDataAsync(locationInfo); } } }