kazelee
2 天以前 6d2faa6e299bdfe545ee339269e64f2900229147
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
using HH.WCS.Mobox3.AnGang.models;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static HH.WCS.Mobox3.AnGang.api.ApiModel;
using HH.WCS.Mobox3.AnGang.util;
 
namespace HH.WCS.Mobox3.AnGang.wms {
    public class LocationHelper
    {
        private static Dictionary<string, TN_Location> Locations = null;
 
        static LocationHelper()
        {
            try
            {
                //初始化Location加入到字典缓存
                Locations = new Dictionary<string, TN_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);
            }
        }
 
        /// <summary>
        /// 获取所有货位信息
        /// </summary>
        /// <returns></returns>
        internal static List<TN_Location> GetAllLocList()
        {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<TN_Location>().ToList();
        }
 
 
        internal static TN_Location GetLocation(string loc)
        {
            if (Locations.Keys.Contains(loc))
            {
                return Locations[loc.Trim()];
            }
            return null;
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="loc"></param>
        /// <returns></returns>
        public static bool IsFree(TN_Location loc) {
            return loc != null 
                && loc.N_LOCK_STATE == 0 
                && loc.S_LOCK_STATE == "无" 
                && loc.C_ENABLE == "Y";
        }
 
        /// <summary>
        /// 获取货位站点信息
        /// </summary>
        /// <param name="loc"></param>
        /// <param name="isEmpty">是否属于人工空托区到空托缓存库区</param>
        /// <returns></returns>
        internal static string GetAgvSite(string loc, bool isEmpty = false)
        {
            var site = "0";
            if (Locations.Keys.Contains(loc.Trim()) && !isEmpty)
            {
                var Location = Locations[loc.Trim()];
                site = Location.S_AGV_SITE;
            }
            else
            {
                var Location = GetLoc(loc.Trim());
                if (Location != null)
                {
                    site = Location.S_AGV_SITE;
 
                    if (isEmpty)
                    {
                        if (Location.N_CURRENT_NUM == 0)
                        {
                            site = Location.S_AGV_SITE;
                        }
                    }
                }
            }
            return site;
        }
 
        internal static TN_Location GetLoc(string code)
        {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<TN_Location>().Where(a => a.S_CODE.Trim() == code).First();
        }
 
        /// <summary>
        /// 起点出库锁:2 --- 终点入库锁:1
        /// </summary>
        /// <param name="loc"></param>
        /// <param name="lockState">1:入库锁、2:出库锁、3:其它锁</param>
        /// <param name="lockSource">锁的来源=任务号</param>
        /// <returns></returns>
        public static bool LockLoc(ref TN_Location loc, int lockState, string lockSource = "") {
            //if (loc == null || loc.N_LOCK_STATE != 0) {
            //    return false;
            //}
 
            loc.N_LOCK_STATE = lockState;
            loc.S_LOCK_STATE = TN_Location.GetLockStateStr(lockState);
            loc.S_LOCK_OP = lockSource;
            loc.T_MODIFY = System.DateTime.Now;
 
            return true;
        }
 
        /// <summary>
        /// 入库锁定终点,出库锁定起点
        /// 你创建任务锁定货位的时候,把锁的来源就是任务号也写上去(加锁的方法加个参数,可空的参数),解锁的时候把来源置空。
        /// </summary>
        /// <param name="loc"></param>
        /// <param name="lockState">1:入库锁、2:出库锁、3:其它锁</param>
        /// <param name="lockSource">锁的来源=任务号</param>
        /// <returns></returns>
        public static bool LockLoc(string loc, int lockState, string lockSource = "")
        {
            var res = false;
            var db = new SqlHelper<object>().GetInstance();
            var model = db.Queryable<TN_Location>().Where(a => a.S_CODE == loc).First();
            LogHelper.Info($"锁货位{loc},状态{lockState},信息" + JsonConvert.SerializeObject(model));
            if (model != null && model.N_LOCK_STATE == 0)
            {
                model.N_LOCK_STATE = lockState;
                model.S_LOCK_STATE = TN_Location.GetLockStateStr(lockState);
                model.S_LOCK_OP = lockSource;
                model.T_MODIFY = System.DateTime.Now;
                res = db.Updateable<TN_Location>(model).UpdateColumns(it => new { it.N_LOCK_STATE, it.S_LOCK_STATE, it.S_LOCK_OP, it.T_MODIFY }).ExecuteCommand() > 0;
            }
            LogHelper.Info($"锁货位{loc}返回{res}");
 
            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<TN_Location>().Where(a => a.S_CODE == loc).First();
            if (model != null)
            {
                model.S_LOCK_STATE = "无";
                model.N_LOCK_STATE = 0;
                model.S_LOCK_OP = "";
                model.T_MODIFY = System.DateTime.Now;
                res = db.Updateable(model).UpdateColumns(it => new { it.N_LOCK_STATE, it.S_LOCK_STATE, it.S_LOCK_OP, it.T_MODIFY }).ExecuteCommand() > 0;
                LogHelper.Info("UnLockLoc:解锁结果" + res);
            }
            else
            {
                LogHelper.Info("UnLockLoc 失败" + loc);
            }
            return res;
        }
 
        /// <summary>
        /// 货位解绑容器
        /// </summary>
        /// <param name="loc"></param>
        /// <param name="cntrs"></param>
        /// <returns></returns>
        public static string UnBindingLoc(string loc, List<string> cntrs)
        {
            var db = new SqlHelper<object>().GetInstance();
            var logs = $"货位:{loc},容器:{JsonConvert.SerializeObject(cntrs)}";
            try
            {
                var lcrList = db.Queryable<TN_Loc_Container>().Where(a => cntrs.Contains(a.S_CNTR_CODE) && a.S_LOC_CODE == loc).ToList();
                if (lcrList.Count == 0)
                {
                    LogHelper.Info($"货位无需解绑容器,在数据库中未找到{JsonConvert.SerializeObject(cntrs)}相关的货位容器关系表信息");
                }
                cntrs = lcrList.Select(a => a.S_CNTR_CODE).ToList();
 
 
                var log = JsonConvert.SerializeObject(cntrs);
                var location = db.Queryable<TN_Location>().First(a => a.S_CODE == loc);
                if (location != null)
                {
                    location.N_CURRENT_NUM = 0;
                    location.S_LOCK_STATE = "无";
                    location.N_LOCK_STATE = 0;
 
                    using (var tran = db.Ado.UseTran())
                    {
                        if (db.Deleteable<TN_Loc_Container>().Where(it => cntrs.Contains(it.S_CNTR_CODE) && it.S_LOC_CODE == loc).ExecuteCommand() > 0 && 
                            db.Updateable<TN_CG_Detail>().SetColumns(d => d.S_LOC_CODE == null).Where(d => cntrs.Contains(d.S_CNTR_CODE)).ExecuteCommand() > 0) // ADD:补充针对PDA分拣
                        {
                            LogHelper.Info($"删除货位容器关系表成功,{log}");
                        }
                        else
                        {
                            tran.RollbackTran();
 
                            LogHelper.Info($"删除货位容器关系表失败,{log}");
 
                            return "货位解绑容器失败," + logs;
                        }
 
                        log = JsonConvert.SerializeObject(location);
                        if (db.Updateable(location).UpdateColumns(it => new { it.N_CURRENT_NUM, it.S_LOCK_STATE, it.N_LOCK_STATE }).ExecuteCommand() > 0)
                        {
                            tran.CommitTran();
 
                            LogHelper.Info($"更新货位表成功,{log}");
                        }
                        else
                        {
                            tran.RollbackTran();
 
                            LogHelper.Info($"更新货位表失败,{log}");
 
                            return "货位解绑容器失败," + logs;
                        }
                    }
                }
                else
                {
                    LogHelper.Info($"在数据库中未找到该货位,无需更新,货位:{loc}");
                }
                return "货位解绑容器成功," + logs;
            }
            catch (Exception ex)
            {
                LogHelper.Info($"发生了异常,货位解绑容器失败,{ex.Message}");
                return "货位绑定容器失败," + logs;
            }
        }
 
        /// <summary>
        /// 货位绑定容器
        /// </summary>
        /// <param name="loc"></param>
        /// <param name="cntrs"></param>
        /// <returns></returns>
        public static string BindingLoc(string loc, List<string> cntrs)
        {
            var db = new SqlHelper<object>().GetInstance();
            var logs = $"货位:{loc},容器:{JsonConvert.SerializeObject(cntrs)}";
            try
            {
                var lcrList = db.Queryable<TN_Loc_Container>().Where(a => cntrs.Contains(a.S_CNTR_CODE) && a.S_LOC_CODE == loc).ToList();
 
                if (lcrList.Count > 0)
                {
                    cntrs = cntrs.Except(lcrList.Select(a => a.S_CNTR_CODE).ToList()).ToList();
                }
 
                var bindLocCntList = new List<TN_Loc_Container>();
                foreach (var item in cntrs)
                {
                    // 补充逻辑:避免解绑导致的信息丢失 (N_LOCK_STATE与CG_Detail保持一致所以直接赋值就行;IMGURL货位移动了就删除,也很合理)
                    var cgDetail = db.Queryable<TN_CG_Detail>().Where(d => d.S_CNTR_CODE == item).First();
                    //bindLocCntList.Add(new TN_Loc_Container() { S_LOC_CODE = loc, S_CNTR_CODE = item, S_IMG_URL = "", N_LOCK_STATE = cgDetail?.N_LOCK_STATE ?? 1 });
                    bindLocCntList.Add(new TN_Loc_Container() { S_LOC_CODE = loc, S_CNTR_CODE = item, S_IMG_URL = "", N_LOCK_STATE = cgDetail?.N_LOCK_STATE ?? 0 });
                }
 
                var log = JsonConvert.SerializeObject(bindLocCntList);
 
                using (var tran = db.Ado.UseTran())
                {
                    if (db.Insertable<TN_Loc_Container>(bindLocCntList).ExecuteCommand() > 0 &&
                        db.Updateable<TN_CG_Detail>().SetColumns(d => d.S_LOC_CODE == loc).Where(d => cntrs.Contains(d.S_CNTR_CODE)).ExecuteCommand() > 0) // ADD:补充针对PDA分拣
                    {
                        LogHelper.Info($"插入货位容器关系表成功,{log}");
                    }
                    else
                    {
                        db.RollbackTran();
                        LogHelper.Info($"插入货位容器关系表失败,{log}");
                        return "货位绑定容器失败," + logs;
                    }
 
                    var location = db.Queryable<TN_Location>().First(a => a.S_CODE == loc);
                    if (location != null)
                    {
                        location.N_CURRENT_NUM += cntrs.Count;
                        location.S_LOCK_STATE = "无";
                        location.N_LOCK_STATE = 0;
                        log = JsonConvert.SerializeObject(location);
 
                        if (db.Updateable(location).UpdateColumns(it => new { it.N_CURRENT_NUM, it.S_LOCK_STATE, it.N_LOCK_STATE }).ExecuteCommand() > 0)
                        {
                            db.CommitTran();
 
                            LogHelper.Info($"更新货位表成功,{log}");
                        }
                        else
                        {
                            db.RollbackTran();
 
                            LogHelper.Info($"更新货位表失败,{log}");
 
                            return "货位绑定容器失败," + logs;
                        }
                    }
                    else
                    {
                        db.RollbackTran();
                        LogHelper.Info($"未找到该货位{loc},或者已锁定,{log}");
                    }
                }
                return "货位绑定容器成功," + logs;
            }
            catch (Exception ex)
            {
                LogHelper.Info($"发生了异常,货位绑定容器失败,");
                return "货位绑定容器失败," + ex.Message;
            }
        }
 
 
        /// <summary>
        /// 人工手动PDA解绑:删除货位/容器/货品三方信息,1=解绑货位与容器,2=解绑容器与货品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult PdaUnBind(PdaUnBindInfo model)
        {
            var simpleResult = new SimpleResult();
            if (model.reqCode == 1)
            {
                var res = UnBindingLoc(model.locID, new List<string>() { model.cntID });
                simpleResult.resultCode = res.Contains("失败") ? -1 : 0;
                simpleResult.resultMsg = res;
                return simpleResult;
            }
            if (model.reqCode == 2)
            {
                var res = ContainerHelper.UnBindingCG(model.cntID, new List<string>() { model.CgId });
                simpleResult.resultCode = res.Contains("失败") ? -1 : 0;
                simpleResult.resultMsg = res;
                return simpleResult;
            }
            simpleResult.resultCode = 3;
            simpleResult.resultMsg = "人工手动PDA解绑:删除货位/容器/货品三方信息,1=解绑货位与容器,2=解绑容器与货品";
            return simpleResult;
        }
 
        /// <summary>
        /// 异常货位容器重置:重置货位/容器/货品三方信息,1=根据货位重置,2=直接根据容器重置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult ErrorLocCntReset(ErrorLocCntResetInfo model)
        {
            LogHelper.Info("触发API:货位容器重置" + JsonConvert.SerializeObject(model), "API");
 
            var result = new SimpleResult();
            var db = new SqlHelper<object>().GetInstance();
 
            try
            {
                if (model.locID != string.Empty && model.reqCode == 1)
                {
                    var locCntrRels = db.Queryable<TN_Loc_Container>().Where(a => a.S_LOC_CODE == model.locID).ToList();
 
                    if (locCntrRels.Count == 0)
                    {
                        result.resultCode = 2;
                        result.resultMsg = $"未找到货位容器关系信息{model.locID}";
                        LogHelper.Info(result.resultMsg);
                        return result;
                    }
 
                    foreach (var item in locCntrRels)
                    {
                        var cG_Detail = db.Queryable<TN_CG_Detail>().Where(a => a.S_CNTR_CODE == item.S_CNTR_CODE).ToList();
                        if (cG_Detail.Count > 0)
                        {
                            if (db.Deleteable<TN_CG_Detail>(cG_Detail).ExecuteCommand() <= 0)
                            {
                                LogHelper.Info($"删除容器{model.cntID}货品明细表信息失败");
                                continue;
                            }
                        }
                    }
                    if (db.Deleteable<TN_Loc_Container>(locCntrRels).ExecuteCommand() <= 0)
                    {
                        result.resultCode = 3;
                        result.resultMsg = $"删除货位{model.locID}容器{model.cntID}关系表信息失败";
                        LogHelper.Info(result.resultMsg);
                        return result;
                    }
 
                    var loc = db.Queryable<TN_Location>().First(a => a.S_CODE == model.locID);
                    if (loc != null)
                    {
                        loc.N_CURRENT_NUM = 0;
                        loc.N_LOCK_STATE = 0;
                        loc.S_LOCK_STATE = "无";
                        loc.C_ENABLE = "Y";
                        if (db.Updateable<TN_Location>(loc).UpdateColumns(it => new { it.N_CURRENT_NUM, it.N_LOCK_STATE, it.S_LOCK_STATE, it.C_ENABLE }).ExecuteCommand() <= 0)
                        {
                            result.resultCode = 4;
                            result.resultMsg = $"更新货位{loc.S_CODE}信息失败";
                            LogHelper.Info(result.resultMsg);
                            return result;
                        }
                    }
                    string ackLog = $"重置成功,货位{model.locID},容器{JsonConvert.SerializeObject(locCntrRels.Select(a => a.S_CNTR_CODE).ToList())}";
                    LogHelper.Info(ackLog);
                    result.resultCode = 0;
                    result.resultMsg = ackLog;
                    return result;
                }
 
                if (model.cntID != string.Empty && model.reqCode == 2)
                {
                    var locCntrRel = db.Queryable<TN_Loc_Container>().First(a => a.S_CNTR_CODE == model.cntID);
                    var cG_Detail = db.Queryable<TN_CG_Detail>().Where(a => a.S_CNTR_CODE == locCntrRel.S_CNTR_CODE).ToList();
                    if (cG_Detail.Count > 0)
                    {
                        if (db.Deleteable<TN_CG_Detail>(cG_Detail).ExecuteCommand() <= 0)
                        {
                            result.resultCode = 5;
                            result.resultMsg = $"删除容器{model.cntID}货品明细表信息失败";
                            LogHelper.Info(result.resultMsg);
                            return result;
                        }
                    }
                    var loc = db.Queryable<TN_Location>().First(a => a.S_CODE == locCntrRel.S_LOC_CODE);
                    if (loc != null)
                    {
                        loc.N_CURRENT_NUM = 0;
                        loc.N_LOCK_STATE = 0;
                        loc.S_LOCK_STATE = "无";
                        loc.C_ENABLE = "Y";
                        if (db.Updateable<TN_Location>(loc).UpdateColumns(it => new { it.N_CURRENT_NUM, it.N_LOCK_STATE, it.S_LOCK_STATE, it.C_ENABLE }).ExecuteCommand() <= 0)
                        {
                            result.resultCode = 6;
                            result.resultMsg = $"更新货位{loc.S_CODE}信息失败";
                            LogHelper.Info(result.resultMsg);
                            return result;
                        }
                    }
                }
                result.resultCode = 0;
                result.resultMsg = $"重置成功,容器{model.cntID}";
                LogHelper.Info(result.resultMsg);
                return result;
            }
            catch (Exception ex)
            {
                result.resultCode = -1;
                result.resultMsg = $"发生了异常:{ex.Message}";
                LogHelper.Info(result.resultMsg);
                return result;
            }
        }
 
        public static string GetStrByOk(int state)
        {
            if (state == 0)
            {
                return "合格"; 
            }
            if (state == 1)
            {
                return "待检";
            }
            if (state == 2)
            {
                return "不合格";
            }
            if (state == 3)
            {
                return "正在检验";
            }
            return "待检";
        }
    }
 
    public class PdaUnBindInfo
    {
        public string cntID { get; set; }//容器ID
        public string locID { get; set; }//货位ID
        public string CgId { get; set; }//物料编码
        public int reqCode { get; set; }//请求代码
    }
 
    public class EmptyUnBindInfo
    {
        public string cntID { get; set; }//容器ID
    }
 
    public class PDAFullInAreaInfo
    {
        public string StartLoc { get; set; }//起点
        public string RfId { get; set; }//容器号
        public string Spe { get; set; }//规格
        public string CarCode { get; set; }//车号
    }
 
    public class ErrorLocCntResetInfo
    {
        public string cntID { get; set; }//容器ID
        public string locID { get; set; }//货位ID
        public int reqCode { get; set; }//请求代码
    }
 
    public class PDAFullCheckInfo
    {
        public string RfId { get; set; }//容器号
    }
 
    public class ShowCntCountBySpeInfo
    {
        public string Spe { get; set; }//规格
 
    }
}