kazelee
2025-05-29 e2303b1125817cb7887e36d5905c8f1661d685a3
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using HH.WCS.Mobox3.DSZSH.device;
using HH.WCS.Mobox3.DSZSH.models;
using HH.WCS.Mobox3.DSZSH.util;
using HH.WCS.Mobox3.DSZSH.wms;
using Microsoft.Win32;
 
using Newtonsoft.Json;
 
namespace HH.WCS.Mobox3.DSZSH.core {
    public class Monitor {
        public class ItemData {
            public string item_code { get; set; }
            public string batch_no { get; set;}
            public string cntr_code { get; set; }
        }
 
        public static void CheckInbound() {
            var taskName = TaskName.T托盘_满托下线入库;
            var db = new SqlHelper<object>().GetInstance();
            var info = "";
            try {
                // 查产线是否有物料信息
                foreach (var prod in Settings.ProductionLines) {
                    if (int.Parse(prod.Id) > 1) break; // TEST
 
                    //var prodDevice = new ProductionLineDevice(prod); // TODO 待需求确定后再处理
                    //TcpClientHelper.Link("127.0.0.1", 8550); // 用于测试
                    TcpClientHelper.Link(prod.PlcIp, prod.PlcPort);
 
                    // TCPClient传递信息的时候,不要用断点阻塞程序
                    // {"item_code":"CG1001","batch_no":"BN1001","cntr_code":"CN2505111"}
                    if (!TcpClientHelper.TryReadProductionLine(out byte[] read)) {
                        info = $"测试{prod.Id}号产线{prod.PlcIp}:{prod.PlcPort}:读取产线信息失败";
                        LogHelper.Info(info);
                        continue;
                    }
 
                    // 3. 将寄存器值转换为字节数组
                    //byte[] bytes = TcpClientHelper.ConvertRegistersToBytes(registers);
 
                    // 4. 将字节数组转换为字符串
                    string result = TcpClientHelper.ConvertBytesToString(read);
 
                    //var itemData = BitConverter.ToString(read);
                    LogHelper.Info($"读取的产线信息{result}");
                    var data = JsonConvert.DeserializeObject<ItemData>(result);
 
                    LogHelper.Info(JsonConvert.SerializeObject(data));
 
                    var itemCode = data.item_code;
                    var batchNo = data.batch_no;
                    var cntrCode = data.cntr_code;
 
                    var startLocCode = prod.OffLoc[0]; // 用于测试
                    //var startLocCode = "CX01"; // 用于测试
 
                    var cgDetail = new TN_CG_Detail {
                        S_ITEM_CODE = itemCode,
                        S_BATCH_NO = batchNo,
                        S_CNTR_CODE = cntrCode,
                    };
 
                    // BEG 每次轮询都检查对应容器是否已登记,登记则更新,未登记则插入
                    var cntrItemRel = db.Queryable<TN_Container_ItemType>()
                        .Where(i => i.S_CNTR_CODE == cntrCode).First();
 
                    var insertTable = cntrItemRel == null;
 
                    if (insertTable) {
                        // 暂定在满托下线入库的时候,登记托盘容器-物料号关系(后面可能会改成系统维护)
                        cntrItemRel = new TN_Container_ItemType {
                            S_ITEM_CODE = itemCode,
                            S_CNTR_CODE = cntrCode,
                        };
                    }
                    // END
 
                    var startLoc = db.Queryable<TN_Location>()
                        .Where(l => l.S_CODE == startLocCode) // 指定:起点货位号
                        .Where(l => l.N_LOCK_STATE == 0 && l.S_LOCK_STATE == "无" && l.C_ENABLE == "Y") // 筛选:未上锁
                        .Where(l => l.N_CURRENT_NUM == 0)
                        .First();
 
                    if (startLoc == null) {
                        info = $"起点位置 '{startLocCode}' 不存在或不具备取货要求";
                        LogHelper.Info(info);
                        continue;
                        //return;
                    }
 
                    // 绑定货位和容器号
                    var locCntrRel = new TN_Loc_Container {
                        S_LOC_CODE = startLocCode,
                        S_CNTR_CODE = cgDetail.S_CNTR_CODE,
                        S_CNTR_TYPE = "托盘",
                    };
 
                    //if (db.Insertable<TN_Loc_Container>(locCntrRel).ExecuteCommand() <= 0) {
                    //    info = $"插入货位容器关系失败:" + JsonConvert.SerializeObject(locCntrRel);
                    //    LogHelper.Info(info);
                    //    continue;
                    //    //return;
                    //}
 
                    var endLoc = db.Queryable<TN_Location>()
                        .Where(a => Settings.AreaMap[AreaName.K空托存放区].Contains(a.S_AREA_CODE))
                        .Where(a => a.N_LOCK_STATE == 0 && a.S_LOCK_STATE == "无" && a.C_ENABLE == "Y") // 筛选:未上锁
                        .Where(a => a.N_CURRENT_NUM == 0) // 筛选:空货位
                        .OrderBy(l => l.N_LAYER)
                        .First();
 
                    if (endLoc == null) {
                        info = "空托入库暂时没有合适的货位可以入库";
                        LogHelper.Info(info);
                        continue;
                    }
 
                    var cntId = locCntrRel.S_CNTR_CODE;
                    var task = WCSHelper.BuildTask(startLoc, endLoc, cntId, taskName);
 
                    LocationHelper.LockLoc(ref startLoc, 2); // 起点出库锁
                    LocationHelper.LockLoc(ref endLoc, 1); // 终点入库锁
 
                    using (var tran = db.Ado.UseTran()) {
                        if (db.Insertable<TN_CG_Detail>(cgDetail).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info = $"插入容器货品信息表失败:物料编码{cgDetail.S_ITEM_CODE},容器编码{cgDetail.S_CNTR_CODE}";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        // BEG 插入或更新容器与物料类型的绑定表
                        if (insertTable) {
                            if (db.Insertable<TN_Container_ItemType>(cntrItemRel).ExecuteCommand() <= 0) {
                                tran.RollbackTran();
                                info = $"登记容器物料类型绑定表失败:物料编码{cntrItemRel.S_ITEM_CODE},容器编码{cntrItemRel.S_CNTR_CODE}";
                                LogHelper.Info(info);
                                continue;
                            }
                        }
                        else {
                            if (db.Updateable<TN_Container_ItemType>(cntrItemRel).ExecuteCommand() <= 0) {
                                tran.RollbackTran();
                                info = $"登记容器物料类型绑定表失败:物料编码{cntrItemRel.S_ITEM_CODE},容器编码{cntrItemRel.S_CNTR_CODE}";
                                LogHelper.Info(info);
                                continue;
                            }
                        }
                        // END
 
                        if (db.Insertable<TN_Loc_Container>(locCntrRel).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info = $"插入货位容器关系表失败:货位编码{locCntrRel.S_LOC_CODE},容器编码{locCntrRel.S_CNTR_CODE}";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        if (db.Updateable<TN_Location>(startLoc).UpdateColumns(it => new {
                            it.N_LOCK_STATE,
                            it.S_LOCK_STATE,
                            it.S_LOCK_OP,
                            it.T_MODIFY
                        }).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info = $"生成任务'{taskName}'失败:更新起点货位{startLoc.S_CODE}锁状态失败";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        if (db.Updateable<TN_Location>(endLoc).UpdateColumns(it => new {
                            it.N_LOCK_STATE,
                            it.S_LOCK_STATE,
                            it.S_LOCK_OP,
                            it.T_MODIFY
                        }).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info = $"生成任务'{taskName}'失败:更新终点货位{endLoc.S_CODE}锁状态失败";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        if (db.Insertable<TN_Task>(task).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info = $"生成任务'{taskName}'失败,容器号{cntId},起点{startLoc.S_CODE},终点货架{endLoc.S_CODE}";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        tran.CommitTran();
                        info = $"生成任务'{taskName}'成功,容器号{cntId},起点{startLoc.S_CODE},终点货架{endLoc.S_CODE}";
                        LogHelper.Info(info);
                        continue;
                    }
                }
 
            }
            catch (Exception ex) {
                LogHelper.InfoEx(ex);
            }
 
        }
 
        public static void CheckOutboundOrder() {
            var taskName = TaskName.C成品胶出库;
            var db = new SqlHelper<object>().GetInstance();
            var info = $"轮询--{taskName}--";
 
            try {
                var orderList = db.Queryable<TN_Outbound_Order>()
                    .Where(c => c.N_B_STATE == 1)
                    .OrderBy(c => c.T_CREATE, SqlSugar.OrderByType.Asc)
                    .ToList();
 
                if (orderList.Count == 0) {
                    info += $"暂无待执行的{taskName}单";
                    LogHelper.Info(info);
                    return;
                }
 
                var detailList = new List<TN_Outbound_Detail>();
                foreach (var order in orderList) {
                    var doingCount = db.Queryable<TN_Outbound_Detail>()
                        .Count(d => d.S_OO_NO == order.S_NO && d.N_B_STATE >= 2); // 执行中
                    var allCount = db.Queryable<TN_Outbound_Detail>()
                        .Count(d => d.S_OO_NO == order.S_NO);
                    info += $"统计{taskName}单'{order.S_NO}'任务已下发:{doingCount}/{allCount}";
                    LogHelper.Info(info);
 
                    if (doingCount == allCount) {
                        order.N_B_STATE = 2; // 所有任务都已执行
                        db.Updateable<TN_Outbound_Order>(order).UpdateColumns(it => new { it.N_B_STATE }).ExecuteCommand();
                        continue;
                    }
 
                    var lastDetail = db.Queryable<TN_Outbound_Detail>()
                        .Where(d => d.S_OO_NO == order.S_NO && d.N_B_STATE == 2) // [NOTE] 或者改成查task
                        .First();
 
                    if (lastDetail != null) {
                        info += $"{taskName}单'{order.S_NO}'上一个任务仍在进行中:" + JsonConvert.SerializeObject(lastDetail);
                        LogHelper.Info(info);
                        continue;
                    }
 
                    var outboundDetail = db.Queryable<TN_Outbound_Detail>()
                        .Where(a => a.S_OO_NO == order.S_NO && a.N_B_STATE == 1) // 已下发
                        .First();
 
                    if (outboundDetail == null) {
                        info += $"仍有任务未执行完成,但当前没有已下发的任务";
                        LogHelper.Info(info);
                        continue;
                    }
 
                    detailList.Add(outboundDetail);
                }
 
                if (detailList.Count == 0) {
                    // 上面流程已经打印过日志
                    return;
                }
 
                foreach (var detail in detailList) {
                    var startLoc = db.Queryable<TN_Location>()
                        .LeftJoin<TN_Loc_Container>((l, c) => l.S_CODE == c.S_LOC_CODE)
                        .Where((l, c) => c.S_CNTR_CODE == detail.S_CNTR_CODE)
                        .Where(l => l.N_LOCK_STATE == 0 && l.S_LOCK_STATE == "无" && l.C_ENABLE == "Y") // 筛选:未上锁
                        .Where(l => l.N_CURRENT_NUM == 1)
                        .First();
 
                    if (startLoc == null) {
                        LogHelper.Info($"轮询--{taskName}:没有找到合适的起点货位!");
                        continue;
                    }
 
                    var endLoc = db.Queryable<TN_Location>()
                        .Where(a => a.S_AREA_CODE == detail.S_END_AREA)
                        .Where(a => a.N_LOCK_STATE == 0 && a.S_LOCK_STATE == "无" && a.C_ENABLE == "Y")
                        .Where(a => a.N_CURRENT_NUM == 0).First();
 
                    if (endLoc == null) {
                        LogHelper.Info($"轮询--{taskName}:没有找到合适的终点货位!单号'{detail.S_OO_NO}'要求终点库区为'{detail.S_END_AREA}'");
                        continue;
                    }
                    
                    detail.N_B_STATE = 2;
 
                    var cntId = detail.S_CNTR_CODE;
                    var task = WCSHelper.BuildTask(startLoc, endLoc, cntId, taskName);
 
                    LocationHelper.LockLoc(ref startLoc, 2); // 起点出库锁
                    LocationHelper.LockLoc(ref endLoc, 1); // 终点入库锁
 
                    using (var tran = db.Ado.UseTran()) {
                        if (db.Updateable<TN_Outbound_Detail>(detail).UpdateColumns(it => it.N_B_STATE).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info += $"修改明细表状态为完成--失败!";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        if (db.Updateable<TN_Location>(startLoc).UpdateColumns(it => new {
                            it.N_LOCK_STATE,
                            it.S_LOCK_STATE,
                            it.S_LOCK_OP,
                            it.T_MODIFY
                        }).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info += $"生成任务'{taskName}'失败:更新起点货位{startLoc.S_CODE}锁状态失败";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        if (db.Updateable<TN_Location>(endLoc).UpdateColumns(it => new {
                            it.N_LOCK_STATE,
                            it.S_LOCK_STATE,
                            it.S_LOCK_OP,
                            it.T_MODIFY
                        }).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info += $"生成任务'{taskName}'失败:更新终点货位{endLoc.S_CODE}锁状态失败";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        if (db.Insertable<TN_Task>(task).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info += $"生成任务'{taskName}'失败,容器号{cntId},起点{startLoc.S_CODE},终点货架{endLoc.S_CODE}";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        tran.CommitTran();
                        info += $"生成任务'{taskName}'成功,容器号{cntId},起点{startLoc.S_CODE},终点货架{endLoc.S_CODE}";
                        LogHelper.Info(info);
                        continue;
                    }
                }
 
 
                //// BEG 查找6个月以前的任务表和任务动作表
                //var oldTaskList = db.Queryable<TN_Task>()
                //    .Where(t => t.T_MODIFY < DateTime.Now.AddMonths(-6))
                //    .Where(t => t.N_B_STATE == 3 && t.S_B_STATE == "完成")
                //    .ToList();
 
                //var oldTaskActionList = db.Queryable<TN_Task, TN_Task_Action>((t, a) => t.S_CODE == a.S_TASK_CODE)
                //    .Where(t => t.T_MODIFY < DateTime.Now.AddMonths(-6))
                //    .Where(t => t.N_B_STATE == 3 && t.S_B_STATE == "完成")
                //    .Select((t, a) => a)
                //    .ToList();
                //// END
 
                //using (var tran = db.Ado.UseTran()) {
                //    // 在出库轮询中处理,查找任务单是否有半年前且已经完成的任务,如果有则删除
                //    // 这个操作不影响正常流程,如果失败也不需要回滚
                //    //if (db.Deleteable<TN_Task>()
                //    //    .Where(t => t.T_MODIFY < DateTime.Now.AddMonths(-6))
                //    //    .Where(t => t.N_B_STATE == 3 && t.S_B_STATE == "完成")
                //    //    .ExecuteCommand() <= 0) {
                //    //    info = $"轮询{taskName}单时尝试删除半年前的任务失败";
                //    //    LogHelper.Info(info);
                //    //}
                //    if (db.Deleteable<TN_Task>(oldTaskList).ExecuteCommand() <= 0
                //        && db.Deleteable<TN_Task_Action>(oldTaskActionList).ExecuteCommand() <= 0) {
                //        info = $"轮询{taskName}单时尝试删除半年前的任务失败";
                //        LogHelper.Info(info);
                //    }
                //}
 
            }
            catch (Exception ex) {
                info += $"发生了异常:{ex.Message}";
                LogHelper.InfoEx(ex);
            }
        }
 
        public static void CheckCheckOrder() {
            var taskName = TaskName.C抽检_出库;
            var db = new SqlHelper<object>().GetInstance();
            var info = "";
 
            try {
                var orderList = db.Queryable<TN_Spot_Check>()
                    .Where(c => c.N_B_STATE == 1)
                    .OrderBy(c => c.T_CREATE, SqlSugar.OrderByType.Asc)
                    .ToList();
 
                if (orderList.Count == 0) {
                    LogHelper.Info($"轮询--{taskName}--暂无待执行的{taskName}单");
                    return;
                }
 
                var detailList = new List<TN_SpotCheck_Detail>();
                foreach (var order in orderList) {
                    var doingCount = db.Queryable<TN_SpotCheck_Detail>()
                        .Count(d => d.S_OO_NO == order.S_NO && d.N_B_STATE >= 2); // 执行中
                    var allCount = db.Queryable<TN_SpotCheck_Detail>()
                        .Count(d => d.S_OO_NO == order.S_NO);
                    LogHelper.Info($"轮询--{taskName}--统计{taskName}单'{order.S_NO}'任务已下发:{doingCount}/{allCount}");
 
                    if (doingCount == allCount) {
                        order.N_B_STATE = 2; // 所有任务都已执行
                        db.Updateable<TN_Spot_Check>(order).UpdateColumns(it => new { it.N_B_STATE }).ExecuteCommand();
                        continue;
                    }
 
                    var checkDetailList = db.Queryable<TN_SpotCheck_Detail>()
                        .Where(a => a.S_OO_NO == order.S_NO && a.N_B_STATE == 1) // 已下发
                        .ToList();
 
                    if (checkDetailList.Count == 0) {
                        LogHelper.Info($"轮询--{taskName}--仍有任务未执行完成,但当前没有已下发的任务");
                        continue;
                    }
 
                    foreach (var checkDetail in checkDetailList) {
                        detailList.Add(checkDetail);
                    }
                }
 
                foreach (var detail in detailList) {
                    var startLoc = db.Queryable<TN_Location>()
                        .LeftJoin<TN_Loc_Container>((l, c) => l.S_CODE == c.S_LOC_CODE)
                        .Where((l, c) => c.S_CNTR_CODE == detail.S_CNTR_CODE)
                        .First();
 
                    if (startLoc == null) {
                        LogHelper.Info($"轮询--{taskName}:没有找到合适的起点货位!");
                        continue;
                    }
 
                    var endLoc = db.Queryable<TN_Location>()
                        .Where(l => l.S_AREA_CODE == detail.S_END_AREA)
                        .Where(a => a.N_LOCK_STATE == 0 && a.S_LOCK_STATE == "无" && a.C_ENABLE == "Y") // 筛选:未上锁
                        .Where(a => a.N_CURRENT_NUM == 0).First();
 
                    if (endLoc == null) {
                        LogHelper.Info($"轮询--{taskName}:没有找到合适的终点货位!");
                        continue;
                    }
 
                    detail.N_B_STATE = 2;
 
                    var cntId = detail.S_CNTR_CODE;
                    var task = WCSHelper.BuildTask(startLoc, endLoc, cntId, taskName);
 
                    LocationHelper.LockLoc(ref startLoc, 2); // 起点出库锁
                    LocationHelper.LockLoc(ref endLoc, 1); // 终点入库锁
 
                    using (var tran = db.Ado.UseTran()) {
                        if (db.Updateable<TN_SpotCheck_Detail>(detail).UpdateColumns(it => it.N_B_STATE).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            LogHelper.Info($"轮询--{taskName}:修改{taskName}单明细表状态为完成--失败!");
                            continue;
                        }
 
                        if (db.Updateable<TN_Location>(startLoc).UpdateColumns(it => new {
                            it.N_LOCK_STATE,
                            it.S_LOCK_STATE,
                            it.S_LOCK_OP,
                            it.T_MODIFY
                        }).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info = $"生成任务'{taskName}'失败:更新起点货位{startLoc.S_CODE}锁状态失败";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        if (db.Updateable<TN_Location>(endLoc).UpdateColumns(it => new {
                            it.N_LOCK_STATE,
                            it.S_LOCK_STATE,
                            it.S_LOCK_OP,
                            it.T_MODIFY
                        }).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info = $"生成任务'{taskName}'失败:更新终点货位{endLoc.S_CODE}锁状态失败";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        if (db.Insertable<TN_Task>(task).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info = $"生成任务'{taskName}'失败,容器号{cntId},起点{startLoc.S_CODE},终点货架{endLoc.S_CODE}";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        tran.CommitTran();
                        info = $"生成任务'{taskName}'成功,容器号{cntId},起点{startLoc.S_CODE},终点货架{endLoc.S_CODE}";
                        LogHelper.Info(info);
                        continue;
                    }
                }
            }
            catch (Exception ex) {
                LogHelper.InfoEx(ex);
            }
        }
 
        public static void CheckShiftOrder() {
            var taskName = TaskName.Y移库;
            var db = new SqlHelper<object>().GetInstance();
            var info = "";
            try {
                var orderList = db.Queryable<TN_Relocation_List>()
                    .Where(c => c.N_B_STATE == 1)
                    .OrderBy(c => c.T_CREATE, SqlSugar.OrderByType.Asc)
                    .ToList();
 
                if (orderList.Count == 0) {
                    LogHelper.Info($"轮询--{taskName}--暂无待执行的{taskName}单");
                    return;
                }
 
                var detailList = new List<TN_RelocationList_Detail>();
                foreach (var order in orderList) {
                    var doingCount = db.Queryable<TN_RelocationList_Detail>()
                        .Count(d => d.S_OO_NO == order.S_NO && d.N_B_STATE >= 2); // 执行中
                    var allCount = db.Queryable<TN_RelocationList_Detail>()
                        .Count(d => d.S_OO_NO == order.S_NO);
                    LogHelper.Info($"轮询--{taskName}--统计{taskName}单'{order.S_NO}'任务已下发:{doingCount}/{allCount}");
 
                    if (doingCount == allCount) {
                        order.N_B_STATE = 2; // 所有任务都已执行
                        db.Updateable<TN_Relocation_List>(order).UpdateColumns(it => new { it.N_B_STATE }).ExecuteCommand();
                        continue;
                    }
 
                    var checkDetailList = db.Queryable<TN_RelocationList_Detail>()
                        .Where(a => a.S_OO_NO == order.S_NO && a.N_B_STATE == 1) // 已下发
                        .ToList();
 
                    if (checkDetailList.Count == 0) {
                        LogHelper.Info($"轮询--{taskName}--仍有任务未执行完成,但当前没有已下发的任务");
                        continue;
                    }
 
                    foreach (var checkDetail in checkDetailList) {
                        detailList.Add(checkDetail);
                    }
                }
 
                foreach (var detail in detailList) {
                    var startLoc = db.Queryable<TN_Location>()
                        .LeftJoin<TN_Loc_Container>((l, c) => l.S_CODE == c.S_LOC_CODE)
                        .Where((l, c) => c.S_CNTR_CODE == detail.S_CNTR_CODE)
                        .Where(l => l.N_LOCK_STATE == 0 && l.S_LOCK_STATE == "无" && l.C_ENABLE == "Y") // 筛选:未上锁
                        .Where(l => l.N_CURRENT_NUM == 1)
                        .First();
 
                    if (startLoc == null) {
                        LogHelper.Info($"轮询--{taskName}:没有找到合适的起点货位!");
                        continue;
                    }
 
                    var endLoc = db.Queryable<TN_Location>()
                        .Where(l => l.S_AREA_CODE == detail.S_END_AREA)
                        .Where(a => a.N_LOCK_STATE == 0 && a.S_LOCK_STATE == "无" && a.C_ENABLE == "Y") // 筛选:未上锁
                        .Where(a => a.N_CURRENT_NUM == 0).First();
 
                    if (endLoc == null) {
                        LogHelper.Info($"轮询--{taskName}:没有找到合适的终点货位!");
                        continue;
                    }
 
                    detail.N_B_STATE = 2;
 
                    var cntId = detail.S_CNTR_CODE;
                    var task = WCSHelper.BuildTask(startLoc, endLoc, cntId, taskName);
 
                    LocationHelper.LockLoc(ref startLoc, 2); // 起点出库锁
                    LocationHelper.LockLoc(ref endLoc, 1); // 终点入库锁
 
                    using (var tran = db.Ado.UseTran()) {
                        if (db.Updateable<TN_RelocationList_Detail>(detail).UpdateColumns(it => it.N_B_STATE).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            LogHelper.Info($"轮询--{taskName}:修改{taskName}单明细表状态为完成--失败!");
                            continue;
                        }
 
                        if (db.Updateable<TN_Location>(startLoc).UpdateColumns(it => new {
                            it.N_LOCK_STATE,
                            it.S_LOCK_STATE,
                            it.S_LOCK_OP,
                            it.T_MODIFY
                        }).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info = $"生成任务'{taskName}'失败:更新起点货位{startLoc.S_CODE}锁状态失败";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        if (db.Updateable<TN_Location>(endLoc).UpdateColumns(it => new {
                            it.N_LOCK_STATE,
                            it.S_LOCK_STATE,
                            it.S_LOCK_OP,
                            it.T_MODIFY
                        }).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info = $"生成任务'{taskName}'失败:更新终点货位{endLoc.S_CODE}锁状态失败";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        if (db.Insertable<TN_Task>(task).ExecuteCommand() <= 0) {
                            tran.RollbackTran();
                            info = $"生成任务'{taskName}'失败,容器号{cntId},起点{startLoc.S_CODE},终点货架{endLoc.S_CODE}";
                            LogHelper.Info(info);
                            continue;
                        }
 
                        tran.CommitTran();
                        info = $"生成任务'{taskName}'成功,容器号{cntId},起点{startLoc.S_CODE},终点货架{endLoc.S_CODE}";
                        LogHelper.Info(info);
                        continue;
                    }
                }
            }
            catch (Exception ex) {
                LogHelper.InfoEx(ex);
            }
        }
    }
}