杨前锦
2025-07-01 a93b0e99036c24b9bd58c79bf5e7364b1ba28bae
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
using HH.WCS.Mobox3.FJJT.util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace HH.WCS.Mobox3.FJJT.wms {
    internal class LocationHelper {
        public class LocationExt {
            public string S_PICKUP_POINT { get; internal set; }
            public string S_LOC_CODE { get; internal set; }
            public string S_AGV_SITE { get; internal set; }
        }
        private static Dictionary<string, Location> Locations = null;
        private static Dictionary<string, LocationExt> LocationExts = null;
 
        static LocationHelper() {
            try {
                //初始化Location加入到字典缓存
                Locations = new Dictionary<string, Location>();
                var list = GetAllLocList();
                if (list.Count > 0) {
                    list.ForEach(a => {
                        if (!Locations.ContainsKey(a.S_CODE)) {
                            Locations.Add(a.S_CODE, a);
                        }
                    });
                }
                //初始化LocationExt加入到集合缓存
                //LocationExts = new Dictionary<string, LocationExt>();
                //var exts = GetAllLocExtList();
                //if (exts.Count > 0) {
                //    exts.ForEach(a => {
                //        LocationExts.Add($"{a.S_LOC_CODE.Trim()}_{a.S_PICKUP_POINT.Trim()}", a);
                //    });
                //}
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
        }
 
        internal static bool CheckExist(string loc) {
            return Locations.Keys.Contains(loc);
        }
        internal static Location GetLocation(string loc) {
            if (CheckExist(loc.Trim())) {
                return Locations[loc.Trim()];
            }
            return null;
        }
        internal static List<FunctionArea> GetFunctionAreas(string masterCls, string msterCode) {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<FunctionArea>().Where(a => a.S_MASTER_CLS == masterCls && a.S_MASTER_CODE == msterCode).ToList();
        }
        internal static List<FunctionArea> GetFunctionAreas(string masterCls, string msterCode, int nType) {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<FunctionArea>().Where(a => a.S_MASTER_CLS == masterCls && a.S_MASTER_CODE == msterCode && a.N_TYPE == nType).ToList();
        }
        internal static FunctionArea GetFunctionAreaByCode(string faCode, int faType, int nType) {
            var db = new SqlHelper<object>().GetInstance();
            // && a.N_FA_TYPE == faType 
            return db.Queryable<FunctionArea>().Where(a => a.S_FA_CODE == faCode && a.N_TYPE == nType).First();
        }
        /// <summary>
        /// 获取货位站点信息
        /// </summary>
        /// <param name="loc"></param>
        /// <returns></returns>
        internal static string GetAgvSite(string loc) {
            var site = "0";
            if (Locations.Keys.Contains(loc.Trim())) {
                var Location = Locations[loc.Trim()];
                site = Location.S_AGV_SITE;
            }
            else {
                var Location = GetLoc(loc.Trim());
                if (Location != null) {
                    Locations.Add(loc.Trim(), Location);
                    site = Location.S_AGV_SITE;
                }
            }
            return site;
        }
 
        internal static int GetAgvSite(string loc, string actionType) {
            var site = 0;
            var key = $"{loc.Trim()}_{actionType.Trim()}";
            if (LocationExts.Keys.Contains(loc.Trim())) {
                var Location = LocationExts[loc.Trim()];
                site = int.Parse(Location.S_AGV_SITE);
            }
            return site;
        }
 
        /// <summary>
        /// 获取所有货位信息
        /// </summary>
        /// <returns></returns>
        internal static List<Location> GetAllLocList() {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<Location>().ToList();
        }
        internal static Location GetLoc(string code) {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<Location>().Where(a => a.S_CODE.Trim() == code).First();
        }
        internal static Location GetEmptyLoc(string areaCode)
        {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<Location>().Where(a => a.S_AREA_CODE.Trim() == areaCode && a.N_CURRENT_NUM == 0 && a.N_LOCK_STATE == 0 && a.C_ENABLE == "Y").First();
        }
 
        internal static bool checkAreaExistCntr(List<string> areaCodes ,string cntrCode)
        {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<Location>()
                    .LeftJoin<LocCntrRel>((a,b) => a.S_CODE == b.S_LOC_CODE)
                    .Where((a, b) => areaCodes.Contains(a.S_AREA_CODE.Trim())
                                && a.N_CURRENT_NUM == 0 
                                && a.N_LOCK_STATE == 0 
                                && a.C_ENABLE == "Y"
                                && b.S_CNTR_CODE == cntrCode
                                ).Count() > 0;
        }
 
        /// <summary>
        ///获取所有货位扩展信息 
        /// </summary>
        /// <returns></returns>
        internal static List<LocationExt> GetAllLocExtList() {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<LocationExt>().ToList();
        }
 
        /// <summary>
        /// 判断没有入库锁和出库锁
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        internal static bool CheckLocFree(string code) {
            var result = false;
            var loc = GetLoc(code);
            if (loc != null) {
                result = loc.N_LOCK_STATE == 0;
            }
            return result;
        }
        internal static List<LocCntrRel> GetLocCntr(string loc) {
            var result = new List<LocCntrRel>();
            //1.0 查货位容器表
            var db = new SqlHelper<object>().GetInstance();
            var list = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == loc).OrderBy(a => a.T_CREATE).ToList();
            if (list.Count > 0) {
                list.ForEach(a => {
                    //获取容器信息
                    var cntr = db.Queryable<Container>().Where(b => b.S_CODE.Trim() == a.S_CNTR_CODE).First();
                    if (cntr != null) {
                        a.Container = cntr;
                        result.Add(a);
                    }
                });
            }
            return result;
        }
 
        internal static LocCntrRel GetLocCntrByCntr(string cntrCode)
        {
            var result = new List<LocCntrRel>();
            //1.0 查货位容器表
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<LocCntrRel>().Where(a => a.S_CNTR_CODE.Trim() == cntrCode).OrderBy(a => a.T_CREATE).First();
        }
 
        internal static List<LocCntrRel> GetLocCntrRel(string loc) {
            //1.0 查货位容器表
            var db = new SqlHelper<object>().GetInstance();
            var result = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == loc.Trim()).ToList();
            return result;
 
        }
 
        /// <summary>
        /// 根据货位集合获取有容器的货位
        /// </summary>
        /// <param name="loc"></param>
        /// <returns></returns>
        internal static List<Location> GetLocList(List<string> loc) {
            //1.0 查货位容器表
            var db = new SqlHelper<object>().GetInstance();
            var list = db.Queryable<Location>().Where(a => loc.Contains(a.S_CODE) && a.N_CURRENT_NUM > 0).ToList();
            return list;
 
        }
 
        /// <summary>
        /// 根据货位编码获取货位信息,不管有没有容器
        /// </summary>
        /// <param name="loc"></param>
        /// <returns></returns>
        internal static List<Location> GetLocListAny(List<string> loc) {
            //1.0 查货位容器表
            var db = new SqlHelper<object>().GetInstance();
            var list = db.Queryable<Location>().Where(a => loc.Contains(a.S_CODE)).ToList();
            return list;
 
        }
 
        /// <summary>
        /// 根据货位集合获取有容器 没有锁的货位
        /// </summary>
        /// <param name="loc"></param>
        /// <returns></returns>
        internal static List<Location> GetLocListFree(List<string> loc) {
            //1.0 查货位容器表
            var db = new SqlHelper<object>().GetInstance();
            var list = db.Queryable<Location>().Where(a => loc.Contains(a.S_CODE) && a.N_CURRENT_NUM > 0 && a.N_LOCK_STATE == 0).ToList();
            return list;
 
        }
 
        /// <summary>
        /// 获取固定数量容器 没有锁的货位
        /// </summary>
        /// <param name="loc"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        internal static List<Location> GetLocListFree(List<string> loc, int current) {
            //1.0 查货位容器表
            var db = new SqlHelper<object>().GetInstance();
            var list = db.Queryable<Location>().Where(a => loc.Contains(a.S_CODE) && a.N_CURRENT_NUM == current && a.N_LOCK_STATE == 0).ToList();
            return list;
 
        }
 
        /// <summary>
        /// 获取数量大于等于固定数量容器 没有锁的货位
        /// </summary>
        /// <param name="loc"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        internal static List<Location> GetLocList(List<string> loc, int current) {
            //1.0 查货位容器表
            var db = new SqlHelper<object>().GetInstance();
            var list = db.Queryable<Location>().Where(a => loc.Contains(a.S_CODE) && a.N_CURRENT_NUM >= current && a.N_LOCK_STATE == 0).ToList();
            return list;
 
        }
 
        /// <summary>
        /// 根据货位集合获取 没有容器 没有锁的货位
        /// </summary>
        /// <param name="loc"></param>
        /// <returns></returns>
        internal static List<Location> GetLocListEmptyFree(List<string> loc) {
            //1.0 查货位容器表
            var db = new SqlHelper<object>().GetInstance();
            var list = db.Queryable<Location>().Where(a => loc.Contains(a.S_CODE) && a.N_CURRENT_NUM == 0 && a.N_LOCK_STATE == 0).ToList();
            return list;
 
        }
 
        /// <summary>
        /// 入库锁定终点,出库锁定起点
        /// </summary>
        /// <param name="loc"></param>
        /// <param name="lockState">1:入库锁、2:出库锁、2:其它锁</param>
        /// <returns></returns>
        public static bool LockLoc(string loc, int lockState) {
            var res = false;
            var db = new SqlHelper<object>().GetInstance();
            var model = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == loc).First();
            if (model != null && model.N_LOCK_STATE == 0) {
                model.N_LOCK_STATE = lockState;
                model.S_LOCK_STATE = Location.GetLockStateStr(lockState);
                res = db.Updateable(model).UpdateColumns(it => new { it.N_LOCK_STATE, it.S_LOCK_STATE }).ExecuteCommand() > 0;
            }
            return res;
        }
 
        /// <summary>
        /// 取货完解锁起点,卸货完解锁终点,可检验锁的来源,也可以不校验
        /// </summary>
        /// <param name="loc"></param>
        /// <returns></returns>
        public static bool UnLockLoc(string loc) {
            LogHelper.Info("UnLockLoc:" + loc);
            var res = false;
            var db = new SqlHelper<object>().GetInstance();
            var model = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == loc).First();
            if (model != null) {
                model.N_LOCK_STATE = 0;
                model.S_LOCK_STATE = Location.GetLockStateStr(model.N_LOCK_STATE);
                res = db.Updateable(model).UpdateColumns(it => new { it.N_LOCK_STATE, it.S_LOCK_STATE }).ExecuteCommand() > 0;
                LogHelper.Info("UnLockLoc:解锁结果" + res);
            }
            else {
                LogHelper.Info("UnLockLoc 失败");
            }
            return res;
        }
 
        /// <summary>
        /// 删除货位容器表记录,修改货位容器数量
        /// </summary>
        /// <param name="loc"></param>
        /// <returns></returns>
        public static bool UnBindingLoc(string loc)
        {
            var res = false;
            var db = new SqlHelper<object>().GetInstance();
            var location = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == loc).First();
            try
            {
                db.BeginTran();
                db.Deleteable<LocCntrRel>().Where(it => it.S_LOC_CODE.Trim() == loc).ExecuteCommand();
                location.N_CURRENT_NUM = 0;
                location.N_LOCK_STATE = 0;
                location.S_LOCK_STATE = Location.GetLockStateStr(location.N_LOCK_STATE);
                db.Updateable(location).UpdateColumns(it => new { it.N_CURRENT_NUM, it.N_LOCK_STATE, it.S_LOCK_STATE }).ExecuteCommand();
                db.CommitTran();
                res = true;
            }
            catch (Exception ex)
            {
                db.RollbackTran();
            }
            return res;
        }
 
        /// <summary>
        /// 删除货位容器表记录,修改货位容器数量
        /// </summary>
        /// <param name="loc"></param>
        /// <param name="cntrs"></param>
        /// <returns></returns>
        public static bool UnBindingLoc(string loc, List<string> cntrs) {
            var res = false;
            var db = new SqlHelper<object>().GetInstance();
            var location = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == loc).First();
            try {
                db.BeginTran();
                var lcrList = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == loc).ToList();
                if (lcrList.Count > 0) {
                    var count = db.Deleteable<LocCntrRel>().Where(it => cntrs.Contains(it.S_CNTR_CODE.Trim()) && it.S_LOC_CODE.Trim() == loc).ExecuteCommand();
                    location.N_CURRENT_NUM = lcrList.Count - count;
                    location.N_LOCK_STATE = 0;
                    location.S_LOCK_STATE = Location.GetLockStateStr(location.N_LOCK_STATE);
                    db.Updateable(location).UpdateColumns(it => new { it.N_CURRENT_NUM, it.N_LOCK_STATE, it.S_LOCK_STATE }).ExecuteCommand();
                }
                db.CommitTran();
                res = true;
            }
            catch (Exception ex) {
                db.RollbackTran();
            }
            return res;
        }
        /// <summary>
        /// 货位绑定容器
        /// </summary>
        /// <param name="loc"></param>
        /// <param name="cntrs"></param>
        /// <returns></returns>
        public static bool BindingLoc(string loc, List<string> cntrs) {
            var res = false;
            var db = new SqlHelper<object>().GetInstance();
            var location = db.Queryable<Location>().Where(a => a.S_CODE.Trim() == loc).First();
            try {
                var lcrList = db.Queryable<LocCntrRel>().Includes(a => a.Container).Where(a => a.S_LOC_CODE.Trim() == loc).ToList();
                db.BeginTran();
                int count = 0;
                cntrs.ForEach(a => {
                    if (lcrList.Count(b => b.S_CNTR_CODE.Trim() == a) == 0) {
                        db.Insertable<LocCntrRel>(new LocCntrRel { S_LOC_CODE = loc, S_CNTR_CODE = a }).ExecuteCommand();
                        count++;
                    }
                });
                location.N_CURRENT_NUM = lcrList.Count + count;
                location.N_LOCK_STATE = 0;
                location.S_LOCK_STATE = Location.GetLockStateStr(location.N_LOCK_STATE);
 
                res = db.Updateable(location).UpdateColumns(it => new { it.N_CURRENT_NUM, it.N_LOCK_STATE, it.S_LOCK_STATE }).ExecuteCommand() > 0;
                db.CommitTran();
                res = true;
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
                db.RollbackTran();
            }
            return res;
        }
 
        /// <summary>
        /// 判断逻辑库区是否有可用货位
        /// </summary>
        /// <param name="zone"></param>
        /// <exception cref="NotImplementedException"></exception>
        internal static bool CheckZoneFree(string zone) {
            var db = new SqlHelper<object>().GetInstance();
            var count = db.Queryable<ZoneLoc>().Includes(a => a.Loc).Where(a => a.S_ZONE_CODE == zone && a.Loc.N_LOCK_STATE == 0).Count();
            return count > 0;
        }
        internal static ZoneLoc GetZoneLoc(string zone) {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<ZoneLoc>().Includes(a => a.Loc).Where(a => a.S_ZONE_CODE == zone && a.Loc.N_LOCK_STATE == 0 && a.Loc.N_CURRENT_NUM == 0).First();
 
        }
        internal static List<ZoneLoc> GetZoneByLoc(string loc) {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<ZoneLoc>().Where(a => a.S_LOC_CODE == loc).ToList();
 
        }
 
        internal static object GetFunctionCodeByArea(string v1, int v2, int v3) {
            throw new NotImplementedException();
        }
    }
}