using Castle.Components.DictionaryAdapter.Xml; using Microsoft.AspNetCore.Mvc; using Microsoft.Data.SqlClient; using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; using Org.BouncyCastle.Bcpg.Sig; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEA_Core; using WIDESEA_Core.BaseRepository; using WIDESEA_Core.BaseServices; using WIDESEA_Core.Enums; using WIDESEA_Core.Helper; using WIDESEA_Core.Utilities; using WIDESEA_DTO.Basic; using WIDESEA_IBasicRepository; using WIDESEA_IBasicService; using WIDESEA_Model.Models; namespace WIDESEA_BasicService { public partial class LocationInfoService : ServiceBase, ILocationInfoService { private readonly IBasicRepository _basicRepository; public ILocationInfoRepository Repository => BaseDal; private readonly static object _locker = new object(); public static List locationCaches = new List(); public LocationInfoService(ILocationInfoRepository BaseDal, IBasicRepository basicRepository) : base(BaseDal) { _basicRepository = basicRepository; } public override WebResponseContent AddData(SaveModel saveModel) { Dt_LocationInfo locationInfo = saveModel.MainData.DicToModel(); return base.AddData(locationInfo); } public override WebResponseContent UpdateData(SaveModel saveModel) { return base.UpdateData(saveModel); } public override WebResponseContent DeleteData(object[] keys) { return base.DeleteData(keys); } 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 Dt_LocationInfo? GetLocation(string roadway,int Locationtype) { lock (_locker) { List removeItems = locationCaches.Where(x => (DateTime.Now - x.DateTime).TotalMinutes > 1).ToList();//查询添加静态变量超过1分钟的货位 int count = removeItems.Count; for (int i = 0; i < count; i++) { locationCaches.Remove(removeItems[i]);//移除查询添加静态变量超过5分钟的货位 } List lockLocations = locationCaches.Select(x => x.LocationCode).ToList(); List locationInfos = BaseDal.QueryData();//查询巷道所有货位信息 if (roadway != null && roadway !="") { locationInfos = BaseDal.QueryData(x => x.Layer >= int.Parse(roadway)); if (int.Parse(roadway) >= 3 && locationInfos.Count==0) { locationInfos = BaseDal.QueryData(); } } List emptyLocations = locationInfos.Where(x => x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && !lockLocations.Contains(x.LocationCode)).OrderBy(x => x.Layer).ThenByDescending(x => x.Column).ThenBy(x => x.Row).ToList();//查询空货位信息并排除1分钟内分配的货位,根据层、列、行排序 if (Locationtype==3) { emptyLocations = locationInfos.Where(x => x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && !lockLocations.Contains(x.LocationCode)).OrderBy(x => x.Layer).ThenBy(x => x.Column).ThenBy(x => x.Row).ToList(); } for (int i = 0; i < emptyLocations.Count; i++) { locationCaches.Add(new LocationCache { DateTime = DateTime.Now, LocationCode = emptyLocations[i].LocationCode }); return emptyLocations[i]; } return null; } } public Dt_LocationInfo? GetLocation2(string roadway, int Locationtype,int Startingcolumn,int Terminationcolumn) { lock (_locker) { List removeItems = locationCaches.Where(x => (DateTime.Now - x.DateTime).TotalMinutes > 5).ToList();//查询添加静态变量超过5分钟的货位 int count = removeItems.Count; for (int i = 0; i < count; i++) { locationCaches.Remove(removeItems[i]);//移除查询添加静态变量超过5分钟的货位 } List lockLocations = locationCaches.Select(x => x.LocationCode).ToList(); List locationInfos = BaseDal.QueryData(x => x.RoadwayNo == roadway && x.LocationType == Locationtype && x.Column> Startingcolumn && x.Column< Terminationcolumn);//查询巷道所有货位信息 List emptyLocations = locationInfos.Where(x => x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && !lockLocations.Contains(x.LocationCode)).OrderBy(x => x.Layer).ThenBy(x => x.Column).ThenByDescending(x => x.Depth).ThenBy(x => x.Row).ToList();//查询空货位信息并排除5分钟内分配的货位,根据层、列、深度、行排序 for (int i = 0; i < emptyLocations.Count; i++) { if (emptyLocations[i].Depth == 1)//判断是否1深货位 { Dt_LocationInfo? sencondDepthLocation = locationInfos.FirstOrDefault(x => Math.Abs(x.Row - emptyLocations[i].Row) == 1 && x.Layer == emptyLocations[i].Layer && x.Column == emptyLocations[i].Column);//查询2深货位对应的1深货位是否为空,防止出现分配1深货位而2深货位为空的情况 if (sencondDepthLocation != null && sencondDepthLocation.LocationStatus == LocationStatusEnum.Free.ObjToInt()) { locationCaches.Add(new LocationCache { DateTime = DateTime.Now, LocationCode = sencondDepthLocation.LocationCode }); return sencondDepthLocation;//1深货位及2深货位都为空的情况下,优先分配2深货位 } else { locationCaches.Add(new LocationCache { DateTime = DateTime.Now, LocationCode = emptyLocations[i].LocationCode }); return emptyLocations[i]; } } else { Dt_LocationInfo? sencondDepthLocation = locationInfos.FirstOrDefault(x => Math.Abs(x.Row - emptyLocations[i].Row) == 1 && x.Layer == emptyLocations[i].Layer && x.Column == emptyLocations[i].Column);//查询2深货位对应的1深货位是否为空 if (sencondDepthLocation != null && sencondDepthLocation.LocationStatus == LocationStatusEnum.Free.ObjToInt()) { locationCaches.Add(new LocationCache { DateTime = DateTime.Now, LocationCode = emptyLocations[i].LocationCode }); return emptyLocations[i]; } } } return null; } } public Dt_LocationInfo? GetLocationplatform(string LocationCode) { Dt_LocationInfo locationInfos = BaseDal.QueryFirst(x => x.LocationCode == LocationCode); if(locationInfos !=null) { return locationInfos; } else { return null; } } public WebResponseContent InitializationLocation(InitializationLocationDTO initializationLocationDTO) { try { (bool, string, object?) result = ModelValidate.ValidateModelData(initializationLocationDTO); if (!result.Item1) return WebResponseContent.Instance.Error(result.Item2); int side = 1; List locationInfos = new List(); for (int i = 0; i < initializationLocationDTO.MaxRow; i++) { for (int j = 0; j < initializationLocationDTO.MaxColumn; j++) { for (int k = 0; k < initializationLocationDTO.MaxLayer; k++) { Dt_LocationInfo locationInfo = new Dt_LocationInfo() { AreaId = 0, Column = j + 1, EnableStatus = EnableStatusEnum.Normal.ObjToInt(), Layer = k + 1, LocationStatus = LocationStatusEnum.Free.ObjToInt(), LocationType = LocationTypeEnum.Cube.ObjToInt(), RoadwayNo = initializationLocationDTO.Roadway, Row = i + 1, Creater="admin" }; if (initializationLocationDTO.IsSingleDepth) { locationInfo.Depth = 1; locationInfo.LocationCode = $"A-{locationInfo.Column.ToString().PadLeft(3, '0')}-{locationInfo.Layer.ToString().PadLeft(3, '0')}"; locationInfo.LocationName = $"A行{locationInfo.Column.ToString().PadLeft(3, '0')}列{locationInfo.Layer.ToString().PadLeft(3, '0')}层"; } else { if (initializationLocationDTO.FirstDepthRows.Contains(i + 1)) { locationInfo.Depth = 1; } else { locationInfo.Depth = 2; } locationInfo.LocationCode = $"A-{locationInfo.Column.ToString().PadLeft(3, '0')}-{locationInfo.Layer.ToString().PadLeft(3, '0')}"; locationInfo.LocationName = $"A行{locationInfo.Column.ToString().PadLeft(3, '0')}列{locationInfo.Layer.ToString().PadLeft(3, '0')}层"; } locationInfos.Add(locationInfo); } } } BaseDal.AddData(locationInfos); return WebResponseContent.Instance.OK(); } catch (Exception ex) { return WebResponseContent.Instance.Error(ex.Message); } } public class LocationCache { public string LocationCode { get; set; } public DateTime DateTime { get; set; } } /// /// 获取货位信息 /// /// /// public object GetLocationStatu(string reley) { var data = BaseDal.QueryData(x=>x.Row== int.Parse(reley)).ToList(); List layers = new List(); foreach (var layer in data.GroupBy(t => t.Layer)) { var rows = new List(); var data_rows = layer.GroupBy(t => t.Row); foreach (var data_row in data_rows) { var cols = new List(); foreach (var data_col in data_row) { cols.Add(new LocationCol() { //列 index = data_col.Column, location_state = data_col.LocationStatus.ToString(), location_lock = data_col.EnableStatus==3?true:false, remark = data_col.Remark }); } cols = cols.OrderBy(t => t.index).ToList(); rows.Add(new LocationRow() { //行 index = data_row.Key, cols = cols }); } rows = rows.OrderBy(t => t.index).ToList(); layers.Add(new LocationLayer() { //层 index = layer.Key, rows = rows }); } layers = layers.OrderBy(t => t.index).ToList(); return layers; } /// /// 获取货位信息 /// /// /// public object GetLocationStatu2() { var data = BaseDal.QueryData(x=>x.LocationName != "99").ToList(); var group1 = data.Where(x=>x.Row==1 && x.Layer==6).ToList(); var group2 = data.Where(x => x.Row == 1 && x.Layer == 5).OrderBy(x => x.Column).ToList(); var group3 = data.Where(x => x.Row == 1 && x.Layer == 4).OrderBy(x => x.Column).ToList(); var group4 = data.Where(x => x.Row == 1 && x.Layer == 3).OrderBy(x => x.Column).ToList(); var group5 = data.Where(x => x.Row == 1 && x.Layer == 2).OrderBy(x => x.Column).ToList(); var group6 = data.Where(x => x.Row == 1 && x.Layer == 1).OrderBy(x=>x.Column).ToList(); var group7 = data.Where(x => x.Row == 2 && x.Layer == 6).OrderBy(x => x.Column).ToList(); var group8 = data.Where(x => x.Row == 2 && x.Layer == 5).OrderBy(x => x.Column).ToList(); var group9 = data.Where(x => x.Row == 2 && x.Layer == 4).OrderBy(x => x.Column).ToList(); var group10 = data.Where(x => x.Row == 2 && x.Layer == 3).OrderBy(x => x.Column).ToList(); var group11 = data.Where(x => x.Row == 2 && x.Layer == 2).OrderBy(x => x.Column).ToList(); var group12 = data.Where(x => x.Row == 2 && x.Layer == 1).OrderBy(x => x.Column).ToList(); var combinedGroups = group1.Concat(group2) .Concat(group3) .Concat(group4) .Concat(group5) .Concat(group6) .Concat(group7) .Concat(group8) .Concat(group9) .Concat(group10) .Concat(group11) .Concat(group12) .ToList(); List col2s=new List< LocationCol2 >(); foreach (var item in combinedGroups) { col2s.Add(new LocationCol2() { index = item.LocationCode, location_state = item.LocationStatus.ToString(), location_lock = item.EnableStatus == 3 ? true : false, }); } return col2s; } public class LocationLayer { public int index { get; set; } public List rows { get; set; } } public class LocationRow { public int index { get; set; } public List cols { get; set; } } public class LocationCol { public int index { get; set; } /// /// 检测柜状态 /// public string remark { get; set; } /// /// 货位状态 /// public string location_state { get; set; } /// /// 锁定状态 /// public bool location_lock { get; set; } } public class LocationCol2 { public string index { get; set; } /// /// 货位状态 /// public string location_state { get; set; } /// /// 锁定状态 /// public bool location_lock { get; set; } } } }