using HslCommunication.WebSocket; using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEA_Common.CommonEnum; using WIDESEA_Common.LocationEnum; using WIDESEA_Common.StockEnum; using WIDESEA_Core; using WIDESEA_Core.BaseRepository; using WIDESEA_Core.BaseServices; using WIDESEA_Core.Const; using WIDESEA_Core.DB; using WIDESEA_Core.Enums; using WIDESEA_Core.Helper; using WIDESEA_Core.Seed; using WIDESEA_Core.Utilities; using WIDESEA_DTO.Basic; using WIDESEA_IBasicService; using WIDESEA_Model.Models; namespace WIDESEA_BasicService { public partial class LocationInfoService : ServiceBase>, ILocationInfoService { private readonly IUnitOfWorkManage _unitOfWorkManage; private readonly IRepository _stockInfoRepository; public IRepository Repository => BaseDal; public LocationInfoService(IRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, IRepository stockInfoRepository) : base(BaseDal) { _unitOfWorkManage = unitOfWorkManage; _stockInfoRepository = stockInfoRepository; } /// /// 批量启用货位 /// /// 货位主键数组 /// public WebResponseContent LocationEnableStatus(int[] keys) { List locationInfos = Repository.QueryData(x => keys.Contains(x.Id)); locationInfos.ForEach(x => { x.EnableStatus = EnableStatusEnum.Normal.ObjToInt(); }); Repository.UpdateData(locationInfos); return WebResponseContent.Instance.OK(); } /// /// 批量禁用货位 /// /// 货位主键数组 /// public WebResponseContent LocationDisableStatus(int[] keys) { List locationInfos = Repository.QueryData(x => keys.Contains(x.Id)); locationInfos.ForEach(x => { x.EnableStatus = EnableStatusEnum.Disable.ObjToInt(); }); Repository.UpdateData(locationInfos); return WebResponseContent.Instance.OK(); } /// /// 单个启用货位 /// /// 货位主键 /// public WebResponseContent LocationEnableStatus(int key) { return LocationEnableStatus(new int[] { key }); } /// /// 单个禁用货位 /// /// 货位主键 /// public WebResponseContent LocationDisableStatus(int key) { return LocationDisableStatus(new int[] { key }); } /// /// 初始化货位 /// /// /// public WebResponseContent InitializationLocation(InitializationLocationDTO initializationLocationDTO) { try { (bool, string, object?) result = ModelValidate.ValidateModelData(initializationLocationDTO); if (!result.Item1) return WebResponseContent.Instance.Error(result.Item2); int depth = initializationLocationDTO.Depth; List locationInfos = new List(); for (int i = 0; i < initializationLocationDTO.MaxRow; i++) { if ((i + 1) % initializationLocationDTO.MaxRow == 1) { depth = initializationLocationDTO.Depth; } else if ((i + 1) % initializationLocationDTO.MaxRow == initializationLocationDTO.Depth + 1) { depth = 1; } else if ((i + 1) % initializationLocationDTO.MaxRow > 1 && (i + 1) % initializationLocationDTO.MaxRow <= initializationLocationDTO.Depth) { depth -= 1; } else { depth += 1; } for (int j = 0; j < initializationLocationDTO.MaxColumn; j++) { for (int k = 0; k < initializationLocationDTO.MaxLayer; k++) { Dt_LocationInfo locationInfo = new Dt_LocationInfo() { WarehouseId = 0, Column = j + 1, EnableStatus = EnableStatusEnum.Normal.ObjToInt(), Layer = k + 1, LocationStatus = LocationStatusEnum.Free.ObjToInt(), LocationType = LocationTypeEnum.Undefined.ObjToInt(), RoadwayNo = $"{initializationLocationDTO.Roadway.ToString()}", Row = i + 1, Depth = depth, }; locationInfo.LocationCode = $"{locationInfo.RoadwayNo}-{locationInfo.Row.ToString().PadLeft(3, '0')}-{locationInfo.Column.ToString().PadLeft(3, '0')}-{locationInfo.Layer.ToString().PadLeft(3, '0')}-{locationInfo.Depth.ToString().PadLeft(2, '0')}"; locationInfo.LocationName = $"{locationInfo.RoadwayNo}巷道{locationInfo.Row.ToString().PadLeft(3, '0')}行{locationInfo.Column.ToString().PadLeft(3, '0')}列{locationInfo.Layer.ToString().PadLeft(3, '0')}层{locationInfo.Depth.ToString().PadLeft(2, '0')}深"; locationInfos.Add(locationInfo); } } } BaseDal.AddData(locationInfos); return WebResponseContent.Instance.OK(); } catch (Exception ex) { return WebResponseContent.Instance.Error(ex.Message); } } private readonly static object _locker = new object(); static List locationCaches = new List(); /// /// 分配货位 /// /// /// /// /// /// /// /// public Dt_LocationInfo? AssignLocation(string roadwayNo, int warehouseId, string beRelocationCode = "") { lock (_locker) { List removeItems = locationCaches.Where(x => (DateTime.Now - x.DateTime).TotalMinutes > 5).ToList(); int count = removeItems.Count; for (int i = 0; i < count; i++) { locationCaches.Remove(removeItems[i]); } List lockLocationCodes = locationCaches.Select(x => x.LocationCode).ToList(); List locationInfos = Repository.QueryData(x => x.RoadwayNo == roadwayNo); if (locationInfos == null || locationInfos.Count == 0) { throw new Exception($"未找到该巷道的货位信息,巷道号:{roadwayNo}"); } if (!string.IsNullOrEmpty(beRelocationCode)) { Dt_LocationInfo? beRelocation = locationInfos.FirstOrDefault(x => x.LocationCode == beRelocationCode); if (beRelocation == null) { throw new Exception($"未找到货位信息"); } int maxDepth = locationInfos.Max(x => x.Depth); int mathCurrentRow = beRelocation.Row - Convert.ToInt32(Math.Ceiling(beRelocation.Row / maxDepth / 2.0)) * maxDepth * 2; if (mathCurrentRow <= maxDepth) { locationInfos = locationInfos.Where(x => x.Row - Convert.ToInt32(Math.Ceiling(x.Row / maxDepth / 2.0)) * maxDepth * 2 <= maxDepth).ToList(); } else { locationInfos = locationInfos.Where(x => x.Row - Convert.ToInt32(Math.Ceiling(x.Row / maxDepth / 2.0)) * maxDepth * 2 > maxDepth).ToList(); } } //未定义类型的空货位 List undefinedTypeEmptyLocations = locationInfos.Where(x => (x.EnableStatus == EnableStatusEnum.Normal.ObjToInt() || x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt()) && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && x.LocationType == LocationTypeEnum.Undefined.ObjToInt() && !lockLocationCodes.Contains(x.LocationCode)).OrderByDescending(x => x.Depth).ThenBy(x => x.Layer).ThenBy(x => x.Column).ThenBy(x => x.Row).ToList(); List definedTypeEmptyLocations = locationInfos.Where(x => (x.EnableStatus == EnableStatusEnum.Normal.ObjToInt() || x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt()) && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && !lockLocationCodes.Contains(x.LocationCode)).OrderByDescending(x => x.Depth).ThenBy(x => x.Layer).ThenBy(x => x.Column).ThenBy(x => x.Row).ToList(); if (definedTypeEmptyLocations.Any()) { for (int i = 0; i < definedTypeEmptyLocations.Count; i++) { Dt_LocationInfo definedTypeEmptyLocation = definedTypeEmptyLocations[i]; Dt_LocationInfo? locationInfo = GetUsableLocation_BC(locationInfos, definedTypeEmptyLocation); //判断货位两深及以上是否存在出库锁定货位 if (locationInfo?.Depth == 1) { Dt_LocationInfo? IsBebusyLocation = locationInfos.Where(x => x.Row == (locationInfo?.Row == 2 ? locationInfo?.Row - 1 : locationInfo?.Row + 1) && x.Column == locationInfo?.Column && x.Layer == locationInfo?.Layer).FirstOrDefault(); if (IsBebusyLocation != null && (IsBebusyLocation.LocationStatus < LocationStatusEnum.InStock.ObjToInt())) { continue; } } if (locationInfo != null) { locationCaches.Add(new LocationCache { LocationCode = locationInfo.LocationCode, DateTime = DateTime.Now }); return locationInfo; } } } if ((undefinedTypeEmptyLocations.Any()))//如果已定义类型货位未超过比例,且有未定义类型的货位 { Dt_LocationInfo undefinedTypeEmptyLocation = undefinedTypeEmptyLocations.FirstOrDefault(); Dt_LocationInfo? locationInfo = GetUsableLocation_BC(locationInfos, undefinedTypeEmptyLocation); locationCaches.Add(new LocationCache { LocationCode = locationInfo.LocationCode, DateTime = DateTime.Now }); return locationInfo; } return null; } } /// /// 获取可用货位 /// /// /// /// /// private Dt_LocationInfo? GetUsableLocation_BC(List locationInfos, Dt_LocationInfo emptyLocation) { //Dt_LocationInfo? nearLocation = null; //if (emptyLocation.Column % 2 == 1) // nearLocation = locationInfos.FirstOrDefault(x => x.Row == emptyLocation.Row && x.Layer == emptyLocation.Layer && x.Depth == emptyLocation.Depth && x.Column == emptyLocation.Column - 1); //else if (emptyLocation.Column % 2 == 0) // nearLocation = locationInfos.FirstOrDefault(x => x.Row == emptyLocation.Row && x.Layer == emptyLocation.Layer && x.Depth == emptyLocation.Depth && x.Column == emptyLocation.Column + 1); //if (nearLocation != null && DepthLocationIsEmpty_BC(locationInfos, nearLocation) != null) //{ Dt_LocationInfo? locationInfo = DepthLocationIsEmpty_BC(locationInfos, emptyLocation); if (locationInfo != null) { return locationInfo; } //} return null; } /// /// 判断不同深度的同组货位状态是否为空闲空位 /// /// /// /// private Dt_LocationInfo? DepthLocationIsEmpty_BC(List locationInfos, Dt_LocationInfo emptyLocation) { List locations = GetGroupLocations(locationInfos, emptyLocation); bool moreDepthFlag = false; bool littleDepthFlag = false; if (emptyLocation.LocationType == 0) { List moreDepth = locations.Where(x => x.Depth > emptyLocation.Depth).ToList(); moreDepthFlag = moreDepth.FirstOrDefault(x => x.LocationStatus != LocationStatusEnum.InStock.ObjToInt() && (x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt() || x.EnableStatus == EnableStatusEnum.Normal.ObjToInt()) && x.LocationType != 0) == null;//查询大于当前货位深度的集合里是否有状态不为有货的货位,如果是true,则表示深货位有未被使用的情况 List littleDepth = locations.Where(x => x.Depth <= emptyLocation.Depth).ToList(); littleDepthFlag = littleDepth.FirstOrDefault(x => x.LocationStatus != LocationStatusEnum.Free.ObjToInt() && (x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt() || x.EnableStatus == EnableStatusEnum.Normal.ObjToInt()) && x.LocationType != 0) == null; //查询小于当前货位深度的集合里是否有状态不为空,且禁用状态不为禁用以及只入的货位,如果是true,则表示浅货位被使用或者被禁用的情况 } else { List moreDepth = locations.Where(x => x.Depth > emptyLocation.Depth).ToList(); moreDepthFlag = moreDepth.FirstOrDefault(x => x.LocationStatus != LocationStatusEnum.InStock.ObjToInt() && (x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt() || x.EnableStatus == EnableStatusEnum.Normal.ObjToInt()) && x.LocationType != emptyLocation.LocationType) == null;//查询大于当前货位深度的集合里是否有状态不为有货的货位,如果是true,则表示深货位有未被使用的情况 List littleDepth = locations.Where(x => x.Depth <= emptyLocation.Depth).ToList(); littleDepthFlag = littleDepth.FirstOrDefault(x => x.LocationStatus != LocationStatusEnum.Free.ObjToInt() && (x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt() || x.EnableStatus == EnableStatusEnum.Normal.ObjToInt()) && x.LocationType != emptyLocation.LocationType) == null; //查询小于当前货位深度的集合里是否有状态不为空,且禁用状态不为禁用以及只入的货位,如果是true,则表示浅货位被使用或者被禁用的情况 } if (moreDepthFlag && littleDepthFlag) { return emptyLocation; } return null; } /// /// 获取不同深度的同组货位信息 /// /// /// /// public List GetGroupLocations(List locationInfos, Dt_LocationInfo location) { List groupLocations = new List() { location }; int maxDepth = locationInfos.Max(x => x.Depth); int row = location.Row; for (int j = location.Depth + 1; j <= maxDepth; j++) { row += 1; Dt_LocationInfo? locationInfo = locationInfos.FirstOrDefault(x => x.Depth == j && x.Column == location.Column && x.Layer == location.Layer && x.Row == row); if (locationInfo != null) { groupLocations.Add(locationInfo); } } for (int j = location.Depth - 1; j >= 1; j--) { row -= 1; Dt_LocationInfo? locationInfo = locationInfos.FirstOrDefault(x => x.Depth == j && x.Column == location.Column && x.Layer == location.Layer && x.Row == row); if (locationInfo != null) { groupLocations.Add(locationInfo); } } return groupLocations; } public class LocationCache { public string LocationCode { get; set; } public DateTime DateTime { get; set; } } } }