| using AutoMapper; | 
| 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_IRecordService; | 
| using WIDESEA_IBasicService; | 
| using WIDESEA_Model.Models; | 
| using WIDESEA_Common.CommonEnum; | 
| using WIDESEA_Common.LocationEnum; | 
|   | 
| namespace WIDESEA_BasicService | 
| { | 
|     public class LocationInfoService_Old | 
|     { | 
|         private readonly ILocationInfoRepository Repository; | 
|   | 
|         public LocationInfoService_Old(ILocationInfoRepository repository) | 
|         { | 
|             Repository = repository; | 
|         } | 
|   | 
|         double weightValue = 0.5; | 
|   | 
|         private readonly static object _locker = new object(); | 
|         static List<LocationCache> locationCaches = new List<LocationCache>(); | 
|   | 
|         /// <summary> | 
|         /// 货位分配逻辑 | 
|         /// </summary> | 
|         /// <param name="roadwayNo">巷道号</param> | 
|         /// <param name="palletType">托盘类型</param> | 
|         /// <returns></returns> | 
|         public Dt_LocationInfo? AssignLocation(string roadwayNo, PalletTypeEnum palletType) | 
|         { | 
|             lock (_locker) | 
|             { | 
|                 List<LocationCache> 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<string> lockLocationCodes = locationCaches.Select(x => x.LocationCode).ToList(); | 
|   | 
|                 List<Dt_LocationInfo> locationInfos = Repository.QueryData(x => x.RoadwayNo == roadwayNo); | 
|   | 
|                 //已定义货位类型的货位 | 
|                 List<Dt_LocationInfo> definedTypeLocations = locationInfos.Where(x => x.LocationType == palletType.ObjToInt()).ToList(); | 
|   | 
|                 //未定义类型的空货位 | 
|                 List<Dt_LocationInfo> 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.Row).ThenBy(x => x.Column).ToList(); | 
|   | 
|                 if (locationInfos.Count * weightValue >= definedTypeLocations.Count && undefinedTypeEmptyLocations.Count > 0)//如果已定义类型货位未超过比例,且有未定义类型的货位 | 
|                 { | 
|                     if (palletType == PalletTypeEnum.LargePallet) | 
|                     { | 
|                         undefinedTypeEmptyLocations = undefinedTypeEmptyLocations.Where(x => x.Column % 2 == 1).ToList(); | 
|                     } | 
|                     for (int i = 0; i < undefinedTypeEmptyLocations.Count; i++) | 
|                     { | 
|                         Dt_LocationInfo undefinedTypeEmptyLocation = undefinedTypeEmptyLocations[i]; | 
|                         Dt_LocationInfo? locationInfo = GetUsableLocation(locationInfos, undefinedTypeEmptyLocation, palletType); | 
|                         if (locationInfo != null) | 
|                         { | 
|                             //UpdateLocationStatus(locationInfo.LocationCode, palletType, LocationStatusEnum.Lock);  | 
|                             locationCaches.Add(new LocationCache { LocationCode = locationInfo.LocationCode, DateTime = DateTime.Now }); | 
|                             return locationInfo; | 
|                         } | 
|                     } | 
|                 } | 
|                 else | 
|                 { | 
|                     List<Dt_LocationInfo> definedTypeEmptyLocations = locationInfos.Where(x => (x.EnableStatus == EnableStatusEnum.Normal.ObjToInt() || x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt()) && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && x.LocationType == palletType.ObjToInt() && !lockLocationCodes.Contains(x.LocationCode)).OrderByDescending(x => x.Depth).ThenBy(x => x.Layer).ThenBy(x => x.Row).ThenBy(x => x.Column).ToList(); | 
|   | 
|                     for (int i = 0; i < definedTypeEmptyLocations.Count; i++) | 
|                     { | 
|                         Dt_LocationInfo definedTypeEmptyLocation = definedTypeEmptyLocations[i]; | 
|                         Dt_LocationInfo? locationInfo = GetUsableLocation(locationInfos, definedTypeEmptyLocation, palletType); | 
|                         if (locationInfo != null) | 
|                         { | 
|                             //UpdateLocationStatus(locationInfo.LocationCode, palletType, LocationStatusEnum.Lock); | 
|                             locationCaches.Add(new LocationCache { LocationCode = locationInfo.LocationCode, DateTime = DateTime.Now }); | 
|                             return locationInfo; | 
|                         } | 
|                     } | 
|                 } | 
|                 return null; | 
|             } | 
|         } | 
|   | 
|         public Dt_LocationInfo? GetUsableLocation(List<Dt_LocationInfo> locationInfos, Dt_LocationInfo undefinedTypeEmptyLocation, PalletTypeEnum palletType) | 
|         { | 
|             switch (palletType) | 
|             { | 
|                 case PalletTypeEnum.LargePallet: | 
|                     { | 
|                         Dt_LocationInfo? nearLocation = locationInfos.FirstOrDefault(x => x.Row == undefinedTypeEmptyLocation.Row && x.Layer == undefinedTypeEmptyLocation.Layer && x.Depth == undefinedTypeEmptyLocation.Depth && x.Column == undefinedTypeEmptyLocation.Column + 1); | 
|                         if (nearLocation != null && nearLocation.LocationStatus == LocationStatusEnum.Free.ObjToInt() && DepthLocationIsEmpty(locationInfos, undefinedTypeEmptyLocation) != null) | 
|                         { | 
|                             Dt_LocationInfo? locationInfo = DepthLocationIsEmpty(locationInfos, undefinedTypeEmptyLocation); | 
|                             if (locationInfo != null) | 
|                             { | 
|                                 return locationInfo; | 
|                             } | 
|                         } | 
|                     } | 
|                     break; | 
|                 case PalletTypeEnum.SmallPallet: | 
|                     { | 
|                         Dt_LocationInfo? locationInfo = DepthLocationIsEmpty(locationInfos, undefinedTypeEmptyLocation); | 
|                         if (locationInfo != null) | 
|                         { | 
|                             return locationInfo; | 
|                         } | 
|                     } | 
|                     break; | 
|             } | 
|             return null; | 
|         } | 
|   | 
|         private Dt_LocationInfo? DepthLocationIsEmpty(List<Dt_LocationInfo> locationInfos, Dt_LocationInfo undefinedTypeEmptyLocation) | 
|         { | 
|             int maxDepth = locationInfos.Max(x => x.Depth); | 
|             if (undefinedTypeEmptyLocation.Depth == 1 && maxDepth == 1) | 
|             { | 
|                 return undefinedTypeEmptyLocation; | 
|             } | 
|             else | 
|             { | 
|                 List<Dt_LocationInfo> locations = GetGroupLocations(locationInfos, undefinedTypeEmptyLocation); | 
|   | 
|                 List<Dt_LocationInfo> moreDepth = locations.Where(x => x.Depth > undefinedTypeEmptyLocation.Depth).ToList(); | 
|                 bool moreDepthFlag = moreDepth.FirstOrDefault(x => x.LocationStatus != LocationStatusEnum.InStock.ObjToInt()) == null;//查询大于当前货位深度的集合里是否有状态不为有货的货位,如果是true,则表示深货位有未被使用的情况 | 
|   | 
|                 List<Dt_LocationInfo> littleDepth = locations.Where(x => x.Depth <= undefinedTypeEmptyLocation.Depth).ToList(); | 
|                 bool littleDepthFlag = littleDepth.FirstOrDefault(x => x.LocationStatus != LocationStatusEnum.Free.ObjToInt() && x.EnableStatus != EnableStatusEnum.OnlyIn.ObjToInt() && x.EnableStatus != EnableStatusEnum.Normal.ObjToInt()) == null; //查询小于当前货位深度的集合里是否有状态不为空,且禁用状态不为禁用以及只入的货位,如果是true,则表示浅货位被使用或者被禁用的情况 | 
|   | 
|                 if (moreDepthFlag && littleDepthFlag) | 
|                 { | 
|                     return undefinedTypeEmptyLocation; | 
|                 } | 
|             } | 
|             return null; | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 修改货位状态及类型 | 
|         /// </summary> | 
|         /// <param name="locationCode">货位编号</param> | 
|         /// <param name="palletType">托盘类型</param> | 
|         /// <param name="locationStatus">货位状态</param> | 
|         public void UpdateLocationStatus(string locationCode, PalletTypeEnum palletType, LocationStatusEnum locationStatus) | 
|         { | 
|             Dt_LocationInfo location = Repository.QueryFirst(x => x.LocationCode == locationCode); | 
|             List<Dt_LocationInfo> locationInfos = Repository.QueryData(x => x.RoadwayNo == location.RoadwayNo); | 
|   | 
|             List<Dt_LocationInfo> locations = GetGroupLocations(locationInfos, location); | 
|             if (locationInfos.Max(x => x.Depth) < 3) | 
|             { | 
|                 for (int i = 0; i < locations.Count; i++) | 
|                 { | 
|                     locations[i].LocationStatus = locationStatus.ObjToInt(); | 
|                     locations[i].LocationType = palletType.ObjToInt(); | 
|                 } | 
|   | 
|                 Dt_LocationInfo? nearLocation = locationInfos.FirstOrDefault(x => x.Row == location.Row && x.Layer == location.Layer && x.Depth == location.Depth && x.Column == location.Column + 1); | 
|                 if (nearLocation != null) | 
|                 { | 
|                     List<Dt_LocationInfo> nearLocations = GetGroupLocations(locationInfos, nearLocation); | 
|                     for (int i = 0; i < nearLocations.Count; i++) | 
|                     { | 
|                         nearLocations[i].LocationType = palletType.ObjToInt(); | 
|                         if (palletType == PalletTypeEnum.LargePallet) | 
|                         { | 
|                             nearLocations[i].LocationStatus = locationStatus.ObjToInt(); | 
|                         } | 
|                     } | 
|                     locations.AddRange(nearLocations); | 
|                 } | 
|             } | 
|             else | 
|             { | 
|                 for (int i = 0; i < locations.Count; i++) | 
|                 { | 
|                     locations[i].LocationStatus = locationStatus.ObjToInt(); | 
|                     locations[i].LocationType = palletType.ObjToInt(); | 
|                 } | 
|             } | 
|             Repository.UpdateData(locations); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 修改货位状态及类型 | 
|         /// </summary> | 
|         /// <param name="location">货位对象</param> | 
|         /// <param name="palletType">托盘类型</param> | 
|         /// <param name="locationStatus">货位状态</param> | 
|         public void UpdateLocationStatus(Dt_LocationInfo location, PalletTypeEnum palletType, LocationStatusEnum locationStatus) | 
|         { | 
|             List<Dt_LocationInfo> locationInfos = Repository.QueryData(x => x.RoadwayNo == location.RoadwayNo); | 
|   | 
|             List<Dt_LocationInfo> locations = GetGroupLocations(locationInfos, location); | 
|             if (locationInfos.Max(x => x.Depth) < 3) | 
|             { | 
|                 for (int i = 0; i < locations.Count; i++) | 
|                 { | 
|                     locations[i].LocationStatus = locationStatus.ObjToInt(); | 
|                     locations[i].LocationType = palletType.ObjToInt(); | 
|                 } | 
|   | 
|                 Dt_LocationInfo? nearLocation = locationInfos.FirstOrDefault(x => x.Row == location.Row && x.Layer == location.Layer && x.Depth == location.Depth && x.Column == location.Column + 1); | 
|                 if (nearLocation != null) | 
|                 { | 
|                     List<Dt_LocationInfo> nearLocations = GetGroupLocations(locationInfos, nearLocation); | 
|                     for (int i = 0; i < nearLocations.Count; i++) | 
|                     { | 
|                         nearLocations[i].LocationType = palletType.ObjToInt(); | 
|                         if (palletType == PalletTypeEnum.LargePallet) | 
|                         { | 
|                             nearLocations[i].LocationStatus = locationStatus.ObjToInt(); | 
|                         } | 
|                     } | 
|                     locations.AddRange(nearLocations); | 
|                 } | 
|             } | 
|             else | 
|             { | 
|                 for (int i = 0; i < locations.Count; i++) | 
|                 { | 
|                     locations[i].LocationStatus = locationStatus.ObjToInt(); | 
|                     locations[i].LocationType = palletType.ObjToInt(); | 
|                 } | 
|             } | 
|             Repository.UpdateData(locations); | 
|         } | 
|   | 
|         private List<Dt_LocationInfo> GetGroupLocations(List<Dt_LocationInfo> locationInfos, Dt_LocationInfo location) | 
|         { | 
|             List<Dt_LocationInfo> groupLocations = new List<Dt_LocationInfo>() { location }; | 
|             int maxDepth = locationInfos.Max(x => x.Depth); | 
|             for (int j = location.Depth + 1; j <= maxDepth; j++) | 
|             { | 
|                 Dt_LocationInfo? locationInfo = locationInfos.FirstOrDefault(x => x.Depth == j && x.Column == location.Column && x.Layer == location.Layer); | 
|                 if (locationInfo != null) | 
|                 { | 
|                     groupLocations.Add(locationInfo); | 
|                 } | 
|             } | 
|   | 
|             for (int j = location.Depth - 1; j >= 1; j--) | 
|             { | 
|                 Dt_LocationInfo? locationInfo = locationInfos.FirstOrDefault(x => x.Depth == j && x.Column == location.Column && x.Layer == location.Layer); | 
|                 if (locationInfo != null) | 
|                 { | 
|                     groupLocations.Add(locationInfo); | 
|                 } | 
|             } | 
|             return groupLocations; | 
|         } | 
|     } | 
|   | 
|     public class LocationCache | 
|     { | 
|         public string LocationCode { get; set; } | 
|   | 
|         public DateTime DateTime { get; set; } | 
|     } | 
| } |