Admin
3 天以前 7a70c16d9fb28646a0f22e9f699746df6c651e8c
´úÂë¹ÜÀí/WMS/WIDESEA_WMSServer/WIDESEA_BasicService/Base/LocationInfoService.cs
@@ -1,10 +1,13 @@
using SqlSugar;
using Microsoft.Data.SqlClient;
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;
@@ -20,6 +23,9 @@
    {
        private readonly IBasicRepository _basicRepository;
        public ILocationInfoRepository Repository => BaseDal;
        private readonly static object _locker = new object();
        public static List<LocationCache> locationCaches = new List<LocationCache>();
        public LocationInfoService(ILocationInfoRepository BaseDal, IBasicRepository basicRepository) : base(BaseDal)
        {
@@ -47,7 +53,7 @@
            List<Dt_LocationInfo> locationInfos = Repository.QueryData(x => keys.Contains(x.Id));
            locationInfos.ForEach(x =>
            {
                x.EnableStatus = EnableEnum.Enable.ObjToInt();
                x.EnableStatus = EnableStatusEnum.Normal.ObjToInt();
            });
            Repository.UpdateData(locationInfos);
@@ -59,7 +65,7 @@
            List<Dt_LocationInfo> locationInfos = Repository.QueryData(x => keys.Contains(x.Id));
            locationInfos.ForEach(x =>
            {
                x.EnableStatus = EnableEnum.Disable.ObjToInt();
                x.EnableStatus = EnableStatusEnum.Disable.ObjToInt();
            });
            Repository.UpdateData(locationInfos);
@@ -76,6 +82,205 @@
            return LocationDisableStatus(new int[] { key });
        }
        //传入巷道号2   è´§æž¶ç±»åž‹10
        /// <summary>
        /// è´§ä½æŸ¥æ‰¾
        /// </summary>
        /// <param name="roadway">巷道号</param>
        /// <param name="Locationtype">货位类型</param>
        /// <param name="Startingcolumn">起始列</param>
        /// <param name="Terminationcolumn">终止列</param>
        /// <returns></returns>
        public Dt_LocationInfo? GetLocation(string roadway,int Locationtype, int Startingcolumn, int Terminationcolumn)
        {
            lock (_locker)
            {
                List<LocationCache> removeItems = locationCaches.Where(x => (DateTime.Now - x.DateTime).TotalMinutes > 5).ToList();//查询添加静态变量超过10分钟的货位
                int count = removeItems.Count;
                for (int i = 0; i < count; i++)
                {
                    locationCaches.Remove(removeItems[i]);//移除查询添加静态变量超过5分钟的货位
                }
                List<string> lockLocations = locationCaches.Select(x => x.LocationCode).ToList();
                List<Dt_LocationInfo> locationInfos = null;
                if (Startingcolumn !=0 && Terminationcolumn != 0)
                {
                    locationInfos=BaseDal.QueryData(x => x.RoadwayNo == roadway && x.LocationType == Locationtype && x.Column >= Startingcolumn && x.Column <= Terminationcolumn && x.EnableStatus == (int)EnableStatusEnum.Normal);
                }
                else
                {
                    locationInfos=BaseDal.QueryData(x => x.RoadwayNo == roadway && x.LocationType == Locationtype && x.EnableStatus == (int)EnableStatusEnum.Normal);
                }
                List<Dt_LocationInfo> 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();//查询空货位信息并排除20分钟内分配的货位,根据层、列、深度、行排序
                for (int i = 0; i < emptyLocations.Count; i++)
                {
                    if (emptyLocations[i].Depth == 1)//判断是否1深货位
                    {
                        //查询深库位
                        int emprow = emptyLocations[i].Row==2?1:4;
                        Dt_LocationInfo? sencondDepthLocationShen = emptyLocations.FirstOrDefault(x => x.Row== emprow && x.Layer == emptyLocations[i].Layer && x.Column == emptyLocations[i].Column);
                        if (sencondDepthLocationShen != null && sencondDepthLocationShen.LocationStatus == LocationStatusEnum.Free.ObjToInt())
                        {
                            locationCaches.Add(new LocationCache { DateTime = DateTime.Now, LocationCode = sencondDepthLocationShen.LocationCode });
                            return sencondDepthLocationShen;//1深货位及2深货位都为空的情况下,优先分配2深货位
                        }
                        else
                        {
                            Dt_LocationInfo? sencondDepthLo = locationInfos.FirstOrDefault(x => x.Row == emprow && x.Layer == emptyLocations[i].Layer && x.Column == emptyLocations[i].Column);
                            if (sencondDepthLo != null && sencondDepthLo.LocationStatus != LocationStatusEnum.Lock.ObjToInt() && sencondDepthLo.LocationStatus == LocationStatusEnum.InStock.ObjToInt() && !lockLocations.Contains(sencondDepthLo.LocationCode))
                            {
                                locationCaches.Add(new LocationCache { DateTime = DateTime.Now, LocationCode = emptyLocations[i].LocationCode });
                                return emptyLocations[i];
                            }
                        }
                    }
                    else
                    {
                        //查询2深货位对应的1深货位是否为空
                        int emprow = emptyLocations[i].Row == 4 ? 3 : 2;
                        Dt_LocationInfo? sencondDepthLocationQian = emptyLocations.FirstOrDefault(x => x.Row== emprow && x.Layer == emptyLocations[i].Layer && x.Column == emptyLocations[i].Column);
                        if (sencondDepthLocationQian != null && sencondDepthLocationQian.LocationStatus == LocationStatusEnum.Free.ObjToInt())
                        {
                            locationCaches.Add(new LocationCache { DateTime = DateTime.Now, LocationCode = emptyLocations[i].LocationCode });
                            return emptyLocations[i];
                        }
                    }
                }
                return null;
            }
        }
        //传入巷道号2   è´§æž¶ç±»åž‹10
        public Dt_LocationInfo? GetLocation3(string roadway, int Locationtype)
        {
            lock (_locker)
            {
                List<LocationCache> removeItems = locationCaches.Where(x => (DateTime.Now - x.DateTime).TotalMinutes > 5).ToList();//查询添加静态变量超过20分钟的货位
                int count = removeItems.Count;
                for (int i = 0; i < count; i++)
                {
                    locationCaches.Remove(removeItems[i]);//移除查询添加静态变量超过5分钟的货位
                }
                List<string> lockLocations = locationCaches.Select(x => x.LocationCode).ToList();
                List<Dt_LocationInfo> locationInfos = BaseDal.QueryData(x => x.RoadwayNo == roadway && x.LocationType == Locationtype);//查询巷道所有货位信息
                List<Dt_LocationInfo> 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();//查询空货位信息并排除20分钟内分配的货位,根据层、列、深度、行排序
                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
                        {
                            if (sencondDepthLocation != null && sencondDepthLocation.LocationStatus != LocationStatusEnum.Lock.ObjToInt())   //判断二深无任务
                            {
                                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? GetLocation4(string roadway, int Locationtype, int Startingcolumn, int Terminationcolumn)
        {
            lock (_locker)
            {
                List<LocationCache> removeItems = locationCaches.Where(x => (DateTime.Now - x.DateTime).TotalMinutes > 20).ToList();//查询添加静态变量超过5分钟的货位
                int count = removeItems.Count;
                for (int i = 0; i < count; i++)
                {
                    locationCaches.Remove(removeItems[i]);//移除查询添加静态变量超过5分钟的货位
                }
                List<string> lockLocations = locationCaches.Select(x => x.LocationCode).ToList();
                List<Dt_LocationInfo> locationInfos = BaseDal.QueryData(x => x.RoadwayNo == roadway && x.LocationType == Locationtype && x.Column > Startingcolumn && x.Column < Terminationcolumn);//查询巷道所有货位信息
                List<Dt_LocationInfo> 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
                        {
                            if (sencondDepthLocation != null && sencondDepthLocation.LocationStatus != LocationStatusEnum.Lock.ObjToInt())
                            {
                                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
@@ -136,5 +341,71 @@
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public class LocationCache
        {
            public string LocationCode { get; set; }
            public DateTime DateTime { get; set; }
        }
        /*public WebResponseContent RetrieveStorageLocationInformation()
        {
            List<Dt_LocationInfo> dt_Locations = BaseDal.QueryData();
            //获取成品库位数量(æ·±)
            List<Dt_LocationInfo> Shallowlocation = dt_Locations.Where(x => x.RoadwayNo == "1" && x.LocationStatus == LocationStatusEnum.Free.ObjToInt()&& x.EnableStatus == (int)EnableStatusEnum.Normal && x.Layer <= 9 && x.Depth==1).ToList();
            List<Dt_LocationInfo> Finishedlocation = dt_Locations.Where(x => x.RoadwayNo == "1" && x.LocationStatus == LocationStatusEnum.Free.ObjToInt()&& x.EnableStatus == (int)EnableStatusEnum.Normal && x.Layer <= 9 && x.Depth == 2).ToList();
            //获取14-37列库位数量
            //获取空托库位数量
            //获取原材料库位数量
            //获取空托数量
        }*/
        public WebResponseContent RetrieveStorageLocationInformation()
        {
            // 1. æŸ¥æ‰¾æ·±åº“位
            List<Dt_LocationInfo> dt_Locations = BaseDal.QueryData(
                x => x.RoadwayNo == "1"
                    && x.EnableStatus == 0
                    && x.LocationStatus == 0
                    && x.Depth == 2
                    && x.Layer <= 9
            );
            if (dt_Locations == null || !dt_Locations.Any())
            {
                return WebResponseContent.Instance.OK(new List<string>().ToJson());
            }
            // 2. æŸ¥è¯¢æ‰€æœ‰æµ…库位
            var allShallow = BaseDal.QueryData(
                x => x.RoadwayNo == "1" && x.Depth == 1
            );
            List<string> locationCodes = new List<string>();
            if (allShallow != null)
            {
                // åœ¨å†…存中过滤并提取LocationCode
                locationCodes = allShallow
                    .Where(x =>
                        dt_Locations.Any(deep =>
                            deep.Layer == x.Layer
                            && deep.RoadwayNo == x.RoadwayNo
                            && deep.Column == x.Column
                            && ((deep.Row == 1 && x.Row == 2) || (deep.Row != 1 && x.Row == 3))
                        )
                        && x.LocationStatus != 0
                    )
                    .Select(x => x.LocationCode)  // åªé€‰æ‹©LocationCode字段
                    .ToList();
            }
            return WebResponseContent.Instance.OK(locationCodes.ToJson());
        }
    }
}