huanghongfeng
3 天以前 1d8897348d578648421b024d0dc5ff3d626e05f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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;
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<Dt_LocationInfo, ILocationInfoRepository>, ILocationInfoService
    {
        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)
        {
            _basicRepository = basicRepository;
        }
 
        public override WebResponseContent AddData(SaveModel saveModel)
        {
            Dt_LocationInfo locationInfo = saveModel.MainData.DicToModel<Dt_LocationInfo>();
            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<Dt_LocationInfo> 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<Dt_LocationInfo> 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 });
        }
 
 
        //传入巷道号2   货架类型10
        public Dt_LocationInfo? GetLocation(string roadway,int Locationtype)
        {
            lock (_locker)
            {
                List<LocationCache> removeItems = locationCaches.Where(x => (DateTime.Now - x.DateTime).TotalMinutes > 20).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 && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && !lockLocations.Contains(x.LocationCode));//查询巷道所有货位信息
 
                
                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
                        {
                            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? GetLocation2(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 && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && !lockLocations.Contains(x.LocationCode));//查询巷道所有货位信息
 
 
                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
                        {
                            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<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
                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,
                            };
 
                            if (initializationLocationDTO.IsSingleDepth)
                            {
                                locationInfo.Depth = 1;
                                locationInfo.LocationCode = $"R{initializationLocationDTO.Roadway.ToString().PadLeft(2, '0')}-{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 = $"R{initializationLocationDTO.Roadway.ToString().PadLeft(2, '0')}巷道{locationInfo.Row.ToString().PadLeft(3, '0')}行{locationInfo.Column.ToString().PadLeft(3, '0')}列{locationInfo.Layer.ToString().PadLeft(3, '0')}层{locationInfo.Depth.ToString().PadLeft(2, '0')}深";
                            }
                            else
                            {
                                if (initializationLocationDTO.FirstDepthRows.Contains(i + 1))
                                {
                                    locationInfo.Depth = 1;
                                }
                                else
                                {
                                    locationInfo.Depth = 2;
                                }
                                locationInfo.LocationCode = $"R{initializationLocationDTO.Roadway.ToString().PadLeft(2, '0')}-{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 = $"R{initializationLocationDTO.Roadway.ToString().PadLeft(2, '0')}巷道{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);
            }
        }
 
        public class LocationCache
        {
            public string LocationCode { get; set; }
 
            public DateTime DateTime { get; set; }
        }
    }
}