schangxiang@126.com
2025-09-17 ff43ddf18764629ff875478e4e47a7281cbd230a
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//using Admin.NET.Core.BasicInformation.Enum;
//using Admin.NET.Core.TaskModule.Enum;
//using iWareModel.Entity.MES;
//using iWareModel.Entity.WCS;
//using iWareModel.Struct;
//using iWareSql.DataAccess;
//using iWareSql.MyDbContext;
//using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data.Entity;
//using System.Linq;
 
 
//namespace iWareCC.Common.Helper
//{
//    /// <summary>
//    /// 寻找空库位帮助类 【EditBy shaocx,2022-02-05】
//    /// </summary>
//    public class FindEmptyLocationHelper
//    {
//        #region 为入库寻找空库位
 
//        /// <summary>
//        /// 入库,1、2巷道寻找空库位
//        /// </summary>
//        ///  <param name="isMoveTask">是否是移库任务</param>
//        ///  <param name="isNeedCalcReservedMoveLocationNum">是否需要计算 移库预留空闲数</param>
//        /// <param name="mycontext"></param>
//        /// <param name="WcsIp"></param>
//        /// <param name="containerCode">需要移动的托盘号</param>
//        /// <param name="PalletType"></param>
//        /// <param name="output"></param>
//        /// <param name="lane">指定巷道</param>
//        /// <returns></returns>
//        public static bool FindEmptyLocatonListForLane12(bool isMoveTask, bool isNeedCalcReservedMoveLocationNum, MyDbContext mycontext, string WcsIp, string containerCode,
//            ref int PalletType, ref CriterionContainerOutput output, int lane = 0)
//        {
//            int srmNo = 0;
//            List<int> useSrmList = new List<int>();
//            MyExtendHelper.GetDevicesInfo(mycontext, WcsIp, ref useSrmList);//判断一二巷道堆垛机是否故障
 
//            if (useSrmList.Count == 0)
//            {
//                return false;
//            }
 
//            //获取 目标空库位列表
//            var criterionRetionOutputs1 = _FindEmptyLocatonListForLane12(isNeedCalcReservedMoveLocationNum, mycontext, containerCode, ref PalletType, lane);
//            if (criterionRetionOutputs1 == null || criterionRetionOutputs1.Count == 0)
//            {
//                return false;
//            }
 
//            if (isMoveTask == false)
//            {//如果不是移库任务
//                if (useSrmList.Count == 1)
//                {
//                    srmNo = useSrmList.First();
//                }
//                else
//                {
//                    var lane_1_list = criterionRetionOutputs1.Where(x => x.Lane == 1).Count();
//                    var lane_2_list = criterionRetionOutputs1.Where(x => x.Lane == 2).Count();
//                    srmNo = lane_1_list > lane_2_list ? 1 : 2;//谁的空库位多就优先入哪个巷道
//                }
 
//                //筛选 目标空库位列表
//                output = FilterLocationForPutFor12(mycontext, srmNo, criterionRetionOutputs1);
//                if (output == null)
//                {
//                    if (srmNo == 1)
//                    {
//                        srmNo++;
//                    }
//                    else if (srmNo == 2)
//                    {
//                        srmNo--;
//                    }
//                    //筛选 目标空库位列表
//                    output = FilterLocationForPutFor12(mycontext, srmNo, criterionRetionOutputs1);
//                    if (output == null)
//                    {
//                        return false;
//                    }
//                }
//            }
//            else
//            {//是移库任务
//                if (lane != 1 && lane != 2)
//                {
//                    SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "堆垛机状态查询模拟";
//                    return false;
//                }
//                srmNo = lane;
//                //筛选 目标空库位列表
//                output = FilterLocationForPutFor12(mycontext, srmNo, criterionRetionOutputs1);
//                if (output == null)
//                {
//                    return false;
//                }
//            }
//            return true;
//        }
 
//        /// <summary>
//        /// 入库,寻找空库位列表 (1,2巷道专用)
//        /// </summary>
//        /// <param name="mycontext"></param>
//        /// <param name="containerCode"></param>
//        /// <param name="PalletType"></param>
//        /// <returns></returns>
//        private static List<CriterionContainerOutput> _FindEmptyLocatonListForLane12(bool isNeedCalcReservedMoveLocationNum, MyDbContext mycontext, string containerCode, ref int PalletType, int lane = 0)
//        {
//            //var containers = GetWareContainerType(mycontext, containerCode);//查询容器类型
//            var criterionRetionOutputs = GetLocationDetailsForEnableFor12(isNeedCalcReservedMoveLocationNum, mycontext, lane);
 
//            if (criterionRetionOutputs == null || criterionRetionOutputs.Count == 0)
//            {
//                return criterionRetionOutputs;
//            }
//            //匹配容器规格合适的库位
//            PalletType = 0;
//            bool isLane3 = false;
//            PalletType = MyExtendHelper.GetPalletType(mycontext, containerCode, ref isLane3);
//            if (isLane3)//判断是否为托盘
//            {
//                throw new Exception("此方法不支持3巷道寻找空库位");
//            }
 
//            var criterionRetionOutputs1 = new List<CriterionContainerOutput>();
//            if (PalletType == 2)
//            {
//                //criterionRetionOutputs1 = criterionRetionOutputs.Where(x => x.LocTypeId.ToString() == "321122000007237").ToList();
//                criterionRetionOutputs1 = criterionRetionOutputs.Where(x => x.LocTypeId.ToString() == WmsStorageType.StorageType_小料箱.ToString()).ToList();
//                if (criterionRetionOutputs1 == null || criterionRetionOutputs1.Count == 0)
//                {
//                    //criterionRetionOutputs1 = criterionRetionOutputs.Where(x => x.LocTypeId.ToString() == "321121948225605").ToList();
//                    criterionRetionOutputs1 = criterionRetionOutputs.Where(x => x.LocTypeId.ToString() == WmsStorageType.StorageType_大料箱.ToString()).ToList();
//                }
//            }
 
//            if (PalletType == 1)
//            {
//                //criterionRetionOutputs1 = criterionRetionOutputs.Where(x => x.LocTypeId.ToString() == "321121948225605").ToList();
//                criterionRetionOutputs1 = criterionRetionOutputs.Where(x => x.LocTypeId.ToString() == WmsStorageType.StorageType_大料箱.ToString()).ToList();
//            }
 
//            return criterionRetionOutputs1;
//        }
 
//        /// <summary>
//        /// 1,2巷道 入库任务,筛选目标空库位
//        /// </summary>
//        /// <param name="mycontext"></param>
//        /// <param name="Lane"></param>
//        /// <param name="criterionRetionOutputs1">要筛选的目标</param>
//        /// <returns></returns>
//        private static CriterionContainerOutput FilterLocationForPutFor12(MyDbContext mycontext, int Lane, List<CriterionContainerOutput> criterionRetionOutputs1)
//        {
//            var criterionRetionOutputs2 = new List<CriterionContainerOutput>();
 
//            criterionRetionOutputs2 = criterionRetionOutputs1.Where(x => x.Lane == Lane && (x.Row == 1 || x.Row == 4)).ToList();//先放1,4列
//            if (criterionRetionOutputs2 == null || criterionRetionOutputs2.Count <= 0)
//            {
//                criterionRetionOutputs2 = criterionRetionOutputs1.Where(x => x.Lane == Lane && (x.Row == 2 || x.Row == 3)).ToList();
//            }
//            else
//            {
//                criterionRetionOutputs2.AddRange(criterionRetionOutputs1.Where(x => x.Lane == Lane && (x.Row == 2 || x.Row == 3)).ToList());
//            }
 
//            List<CriterionContainerOutput> linShiList = new List<CriterionContainerOutput>();
//            //匹配容器规格合适的库位
//            CriterionContainerOutput output = null;
 
//            //入库时判断内侧库位是否有未完成任务,如果有就找下一个库位
//            if (criterionRetionOutputs2 != null && criterionRetionOutputs2.Count > 0)
//            {
//                var tasks = mycontext.wms_task.Where(x => (x.IsDeleted == null || x.IsDeleted == false))
//                .Where(TaskHandler.CommonFilterExpressionForNoFinishAndNoCancel())
//                .ToList();
//                foreach (var item in criterionRetionOutputs2)
//                {
//                    var arr = item.Code.Split('-');
//                    if (arr[2] == "2" || arr[2] == "3")
//                    {//如果找到的空库位是 外侧库位2和3
 
//                        if (arr[2] == "2")
//                        {
//                            var locationCode = arr[0] + "-" + arr[1] + "-1-" + arr[3];
//                            if (tasks == null || tasks.Count <= 0)
//                            {
//                                output = item;//空库位找到
//                                break;
//                            }
//                            var findTask = tasks.Where(x => x.ToLocationCode == locationCode || x.FromLocationCode == locationCode).FirstOrDefault();
//                            if (findTask == null)
//                            {
//                                output = item;//空库位找到
//                                break;
//                            }
//                            else
//                            {
//                                //任务状态 0-未下发WCS ,1-已下发WCS 
//                                if (findTask.State != 1)
//                                {
//                                    linShiList.Add(item);
//                                }
//                            }
//                        }
//                        if (arr[2] == "3")
//                        {
//                            var locationCode = arr[0] + "-" + arr[1] + "-4-" + arr[3];
//                            if (tasks == null || tasks.Count <= 0)
//                            {
//                                output = item;//空库位找到
//                                break;
//                            }
//                            var findTask = tasks.Where(x => x.ToLocationCode == locationCode || x.FromLocationCode == locationCode).FirstOrDefault();
//                            if (findTask == null)
//                            {
//                                output = item;//空库位找到
//                                break;
//                            }
//                            else
//                            {
//                                //任务状态 0-未下发WCS ,1-已下发WCS 
//                                if (findTask.State != 1)
//                                {
//                                    linShiList.Add(item);
//                                }
//                            }
//                        }
//                    }
//                    else
//                    {//如果找到的空库位是 内侧库位1和4
//                        var locationCode = "";
//                        if (arr[2] == "1")
//                        {
//                            locationCode = arr[0] + "-" + arr[1] + "-2-" + arr[3];
//                        }
//                        if (arr[2] == "4")
//                        {
//                            locationCode = arr[0] + "-" + arr[1] + "-3-" + arr[3];
//                        }
//                        var wareLocation = mycontext.ware_location.Where(x => x.IsLocked == 1 && (x.IsDeleted == null || x.IsDeleted == false) && x.Code == locationCode).FirstOrDefault();
//                        var wareLocationVsContainer = mycontext.ware_location_vs_container.Where(x => (x.IsDeleted == null || x.IsDeleted == false) && x.WareLocationCode == locationCode).FirstOrDefault();
//                        var noFinishTask = tasks.Where(x => x.ToLocationCode == locationCode || x.FromLocationCode == locationCode).FirstOrDefault();
//                        if (wareLocation == null && wareLocationVsContainer == null && noFinishTask == null)
//                        {//如果找到的空库位  外侧库位2和3 没有被锁定,并且没有库存,并且没有未结束的任务
//                            output = item;
//                            break;
//                        }
//                    }
//                }
//            }
 
//            if (output == null)
//            {
//                //没办法了,移库只能进入 外侧库位2和3位置了  【Editby shaocx,2023-12-28】
//                output = linShiList.FirstOrDefault();
//            }
 
//            return output;
//        }
 
 
//        /// <summary>
//        /// 查找可用库位(仅支持1,2巷道)
//        /// </summary>
//        /// <param name="mycontext"></param>
//        /// <returns></returns>
//        public static List<CriterionContainerOutput> GetLocationDetailsForEnableFor12(bool isNeedCalcReservedMoveLocationNum, MyDbContext mycontext, int lane)
//        {
//            ////查询库位未删除无货未锁定的库位
//            string sql = @"SELECT A.Code,A.Name,A.Long,A.Width,A.High AS Height,A.Lane,A.[Column],A.Row,A.Layer,A.LocTypeId,A.IsDeleted,A.IsVirtual,A.IsLocked,
//                       (select top(1) WareLocationCode from ware_location_vs_container B where B.WareLocationCode=A.Code and B.IsDeleted =0 ) WareLocationCode
//                       FROM dbo.ware_location A WHERE  A.IsDeleted = 0 AND A.IsVirtual = 0 and A.IsLocked=0 ";
//            if (lane > 0)
//            {
//                sql += $" and A.Lane={lane} ";
//            }
 
//            var bb = mycontext.Database.SqlQuery<CriterionContainerOutput>(sql).ToList();
//            //筛选出 WareLocationCode为NULL或者是空的库位,就表示 他是空库位
//            bb = bb.Where(x => string.IsNullOrWhiteSpace(x.WareLocationCode)).ToList();
 
//            //还需要筛选掉 当前有未结束任务占用的情况 【Editby shaocx,2023-03-08】
//            List<string> new_locationList = new List<string>();
//            var locationList = TaskHandler.GetLocationListByNoFinishedTask(mycontext);
 
//            //判断这些库位,如果存在外侧库位,那么 里侧的库位也不能允许使用 【Editby shaocx,2023-03-08】
//            foreach (var item in locationList)
//            {
//                var insideLocation = GetInsideLocation(item);
//                if (!string.IsNullOrEmpty(insideLocation))
//                {
//                    new_locationList.Add(insideLocation);
//                }
//            }
//            new_locationList.AddRange(locationList);
 
//            bb = bb.Where(x => !new_locationList.Contains(x.Code)).ToList();
 
//            //如果是需要预留空闲库位,就预留出 【Editby shaocx,2023-04-08】
//            if (isNeedCalcReservedMoveLocationNum)
//            {
//                // 移库预留空闲数计算 【Editby shaocx,2023-02-28】
//                bb = MyExtendHelper.RemoveEmptyLocatonForReservedMoveLocationNumByLane12(mycontext, bb);
//            }
 
//            return bb;
//        }
 
//        /// <summary>
//        /// 根据库位寻找 他的内测库位
//        /// </summary>
//        /// <param name="locationCode"></param>
//        public static string GetInsideLocation(string locationCode)
//        {
//            var retCode = string.Empty;
//            var arr = locationCode.Split('-');//这里只是立体库,不考虑平库 【Edity shaocx,2022-12-7】
//            if (arr.Length != 4)
//            {
//                return retCode;
//            }
//            if (arr[2] == "2" || arr[2] == "3")
//            {
//                if (arr[2] == "2")
//                {
//                    retCode = arr[0] + "-" + arr[1] + "-1-" + arr[3];
//                }
//                if (arr[2] == "3")
//                {
//                    retCode = arr[0] + "-" + arr[1] + "-4-" + arr[3];
//                }
//            }
//            return retCode;
//        }
 
//        /// <summary>
//        /// 根据库位寻找 他的外测库位
//        /// </summary>
//        /// <param name="locationCode"></param>
//        public static string GetWidesideLocation(string locationCode)
//        {
//            var retCode = string.Empty;
//            var arr = locationCode.Split('-');//这里只是立体库,不考虑平库 【Edity shaocx,2022-12-7】
//            if (arr.Length != 4)
//            {
//                return retCode;
//            }
//            if (arr[2] == "1")
//            {
//                retCode = arr[0] + "-" + arr[1] + "-2-" + arr[3];
//            }
//            if (arr[2] == "4")
//            {
//                retCode = arr[0] + "-" + arr[1] + "-3-" + arr[3];
//            }
//            return retCode;
//        }
 
 
//        /// <summary>
//        /// 根据库位判断 他是否是内测库位
//        /// </summary>
//        /// <param name="locationCode"></param>
//        /// <returns>true:是内侧库位,即1 4行;false:是外侧库位,即2 3 行</returns>
//        public static bool IsInsideLocation(string locationCode)
//        {
//            var retCode = string.Empty;
//            var arr = locationCode.Split('-');//这里只是立体库,不考虑平库 【Edity shaocx,2022-12-7】
//            if (arr.Length != 4)
//            {
//                return false;
//            }
//            if (arr[2] == "1" || arr[2] == "4")
//            {
//                return true;
//            }
//            return false;
//        }
 
 
//        #endregion
//    }
//}