服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2024-12-17 738df70120209daecd85566d0c50dbf56d9e4453
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
using Tiger.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Rhea.Common;
using System.Net;
using System.Linq;
using Newtonsoft.Json;
using Tiger.IBusiness;
using Microsoft.AspNetCore.Http;
using Tiger.Model.Entitys.MES.BizMesWoBatch;
using Tiger.Model.Entitys.MES.BizMesWo;
using Tiger.Model.Entitys.MES.Position;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Drawing.Printing;
using System.Net.NetworkInformation;
using Tiger.Model.MES.Yada;
using static Microsoft.CodeAnalysis.CSharp.SyntaxTokenParser;
using Tiger.Model.Minsun;
 
namespace Tiger.Business.MES
{
    public partial class BizMesWo : IBIZ_MES_WO
    {
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="wo"></param>
        /// <returns></returns>
        public async Task<ApiAction> SaveMesWo(BIZ_MES_WO wo)
        {
            var result = new ApiAction();
            try
            {
                var _wo = Biz.Db.Queryable<BIZ_MES_WO>().Where(x => x.ORDER_NO == wo.ORDER_NO).First();
                if (_wo != null && _wo.STATUS != (int)BIZ_MES_WO.STATUSs.Init)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单已经存在且不是初始化状态,不能保存修改!");
                    return result;
                }
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    var y = db.Storageable(wo)
                       .WhereColumns(t => new { t.ORDER_NO, t.GHOST_ROW })
                       .ToStorage();
                    y.AsInsertable.ExecuteCommand();
                    y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.Message = $"保存工单异常";
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "保存工单异常");
            }
            return await Task.FromResult(result);
        }
 
        /// <summary>
        /// 删除工单
        /// </summary>
        /// <param name="woId"></param>
        /// <returns></returns>
        public async Task<ApiAction> DeleteMesWo(string woId)
        {
            var result = new ApiAction();
            try
            {
                //查询是否已经有工单在用
                var _wo = Biz.Db.Queryable<BIZ_MES_WO>().Where(x => x.ID == woId).First();
                if (_wo != null && _wo.STATUS != (int)BIZ_MES_WO.STATUSs.Init)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单不是初始化状态,不能删除!");
                    return result;
                }
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    db.Deleteable<BIZ_MES_WO>().Where(x => x.ID == woId).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"删除工单异常");
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "删除工单异常");
            }
            return await Task.FromResult(result);
        }
 
        /// <summary>
        /// 下发时保存批次工单信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction> SaveMesBatchWo(SaveWoBatchInput input)
        {
            var result = new ApiAction();
            try
            {
                //保存前的判断,并生成批次号
                var _wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(x => x.ORDER_NO == input.Wo.ORDER_NO).FirstAsync();
                if (_wo.IsNullOrEmpty())
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单不存在,不能下发!");
                    return result;
                }
                if (_wo.ROUTE_STATUS != BIZ_MES_WO.ROUTE_STATUSs.Finish.GetValue())
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单工艺路线未配置,不能下发!");
                    return result;
                }
                if (_wo.STATUS > (int)BIZ_MES_WO.STATUSs.Working)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单不是初始化状态,不能下发!");
                    return result;
                }
                var batchs = await Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Where(x => x.ORDER_NO == input.Wo.ORDER_NO).ToListAsync();
                if ((batchs.Count > 0 && batchs.Sum(x => x.PLAN_QTY) + input.WoBatch.PLAN_QTY > _wo.PLAN_QTY) || input.WoBatch.PLAN_QTY > _wo.PLAN_QTY)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单下发的数量超过工单计划数量,不能下发!");
                    return result;
                }
                var _batch = Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Where(x => x.ORDER_NO == input.Wo.ORDER_NO && x.ACT_LINE == input.WoBatch.ACT_LINE && x.STATUS < BIZ_MES_WO.STATUSs.Closed.GetValue()).First();
                if (_batch != null)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单[{input.Wo.ORDER_NO}]已经存在工单批次[{_batch.BATCH_NO}]在线体[{input.WoBatch.ACT_LINE}]中状态为[{_batch.STATUS.GetEnumDesc<BIZ_MES_WO.STATUSs>()}],不能下发!");
                    return result;
                }
                //WoContext.RemoveBatch(input.WoBatch.BATCH_NO);
                input.Wo.ACT_LINE = input.WoBatch.ACT_LINE;
                input.Wo.RELEASE_TIME = DateTime.Now;
                input.Wo.RELEASE_USER = input.WoBatch.UPDATE_USER;
 
                var _batchWos = await Biz.Db.Queryable<V_MES_WO_BATCH>().Where(x => x.ORDER_NO == input.WoBatch.ORDER_NO).ToListAsync();
                if (_batchWos.Count > 0)
                {
                    input.WoBatch.BATCH_NO = $"{input.WoBatch.ORDER_NO}-{(_batchWos.Max(x => x.BATCH) + 1).ToString("D2")}";
                }
                else
                {
                    input.WoBatch.BATCH_NO = $"{input.WoBatch.ORDER_NO}-01";
                }
 
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    var y = db.Storageable(input.Wo)
                       .WhereColumns(t => new { t.ORDER_NO, t.GHOST_ROW })
                       .ToStorage();
                    y.AsInsertable.ExecuteCommand();
                    y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
 
                    db.Insertable(input.WoBatch).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"保存批次工单异常");
                }
                //如果成功,根据条件异步保存工艺路线到产品的客户
                if (result.IsSuccessed && input.IfToCust)
                {
                    Work.DoAsync(() =>
                    {
                        try
                        {
                            SugarParameter[] pars = Biz.Db.Ado.GetParameters(new { WO = input.Wo.ORDER_NO, ERR_CODE = 0, ERR_MSG = "" });
                            pars[1].Direction = System.Data.ParameterDirection.Output;
                            pars[2].Direction = System.Data.ParameterDirection.Output;
                            Biz.Db.Ado.UseStoredProcedure().ExecuteCommand("SP_MES_WO2CUST", pars);
                            if (pars[1].Value.ToInt32() == 0)
                            {
                                Logger.Default.Info($"工单[{input.Wo.ORDER_NO}]下发完成后保存工艺路线数据到产品[{input.Wo.ITEM_CODE}]的客户[{input.Wo.CUST_CODE ?? ""}]的工艺路线中,{pars[2].Value.ToString()}");
                            }
                            else
                            {
                                Logger.Default.Error($"工单[{input.Wo.ORDER_NO}]下发完成后保存工艺路线数据到产品[{input.Wo.ITEM_CODE}]的客户[{input.Wo.CUST_CODE ?? ""}]的工艺路线出错,{pars[2].Value.ToString()}");
                            }
                        }
                        catch (Exception ex)
                        {
                            result.CatchExceptionWithLog(ex, "保存批次工单异常");
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "保存批次工单异常");
            }
            return result;
        }
 
        /// <summary>
        /// 更新工单状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiAction> UpdateWoStatus(BizMesWoInput input)
        {
            var result = new ApiAction();
            try
            {
                //保存前的判断,并生成批次号
                var _wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(x => x.ORDER_NO == input.WorkOrder).FirstAsync();
                if (_wo != null)
                {
                    _wo.CHANGE_TIME = _wo.STATUS == BIZ_MES_WO.STATUSs.Paused.GetValue() && input.Status == BIZ_MES_WO.STATUSs.Working.GetValue() ? DateTime.Now : _wo.CHANGE_TIME;
                    _wo.CHANGE_USER = _wo.STATUS == BIZ_MES_WO.STATUSs.Paused.GetValue() && input.Status == BIZ_MES_WO.STATUSs.Working.GetValue() ? input.UserId : _wo.CHANGE_USER;
                    _wo.STATUS = input.Status < 0 ? _wo.STATUS : input.Status;
                    _wo.ROUTE_STATUS = input.RouteStatus;
                }
                else
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单[{input.WorkOrder}]不存在");
                    return result;
                }
 
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    db.Updateable(_wo, input.UserId).UpdateColumns(x => new { x.STATUS, x.ROUTE_STATUS, x.UPDATE_USER, x.UPDATE_TIME }).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"更新工单状态异常");
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "更新工单状态异常");
            }
            return result;
        }
 
        /// <summary>
        /// 更新工单批次状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiAction> UpdateWoBatchStatus(BizMesWoInput input)
        {
            var result = new ApiAction();
            try
            {
                //保存前的判断
                var _wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(x => x.ORDER_NO == input.WorkOrder).FirstAsync();
                var _woBatch = await Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Where(x => x.BATCH_NO == input.WoBatch).FirstAsync();
                if (_woBatch != null)
                {
                    _woBatch.CHANGE_TIME = _woBatch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Paused.GetValue() && input.Status == BIZ_MES_WO_BATCH.STATUSs.Working.GetValue()? DateTime.Now: _woBatch.CHANGE_TIME;
                    _woBatch.CHANGE_USER = _woBatch.STATUS == BIZ_MES_WO_BATCH.STATUSs.Paused.GetValue() && input.Status == BIZ_MES_WO_BATCH.STATUSs.Working.GetValue() ? input.UserId: _woBatch.CHANGE_USER;
                    _woBatch.STATUS = input.Status < 0 ? _woBatch.STATUS : input.Status;
                    if (input.Status == BIZ_MES_WO_BATCH.STATUSs.Paused.GetValue())
                    {
                        //WoContext.RemoveBatch(input.WoBatch);
                    }
                    //如果是下发
                    if (input.Status == BIZ_MES_WO_BATCH.STATUSs.Release.GetValue())
                    {
                        if (Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Any(x => x.ORDER_NO == input.WorkOrder && x.BATCH_NO != input.WoBatch && x.ACT_LINE == input.ActLine && BIZ_MES_WO.STATUSs.Imported.GetValue() < x.STATUS && x.STATUS < BIZ_MES_WO.STATUSs.Closed.GetValue()))
                        {
                            result.IsSuccessed = false;
                            result.LocaleMsg = new($"工单存在未完成的工单批次,不能下发");
                            return result;
                        }
                        _woBatch.RELEASE_TIME = DateTime.Now;
                        _woBatch.RELEASE_USER = input.UserId;
                        _woBatch.ACT_LINE = input.ActLine;
                        _woBatch.CHANGE_USER = input.UserId;
                        _woBatch.CHANGE_TIME = DateTime.Now;
                    }
                    if (!Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Where(x => x.ORDER_NO == input.WorkOrder && x.STATUS> BIZ_MES_WO_BATCH.STATUSs.Init.GetValue()).Any()) {
                        _wo.STATUS = BIZ_MES_WO.STATUSs.Init.GetValue();
                    }
                }
                else
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单批次[{input.WoBatch}]不存在");
                    return result;
                }
 
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    //如果是下发
                    if (input.Status == BIZ_MES_WO_BATCH.STATUSs.Release.GetValue())
                    {
                        db.Updateable(_woBatch, input.UserId).UpdateColumns(x => new { x.STATUS, x.RELEASE_USER, x.RELEASE_TIME, x.ACT_LINE, x.UPDATE_USER, x.UPDATE_TIME, x.CHANGE_USER,x.CHANGE_TIME }).ExecuteCommand();
                    }
                    else
                    {
                        db.Updateable(_woBatch, input.UserId).UpdateColumns(x => new { x.STATUS, x.UPDATE_USER, x.UPDATE_TIME, x.CHANGE_USER, x.CHANGE_TIME }).ExecuteCommand();
                    }
                    if (!_wo.IsNullOrEmpty() && _wo.STATUS == BIZ_MES_WO.STATUSs.Init.GetValue())
                    {
                        db.Updateable(_wo, input.UserId).UpdateColumns(x => new { x.STATUS, x.UPDATE_USER, x.UPDATE_TIME }).ExecuteCommand();
                    }
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"更新工单批次状态异常");
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "更新工单批次状态异常");
            }
            return result;
        }
 
        /// <summary>
        /// 工单模板变量修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction> AddOrEditLabelVarByWorkOrder(BAS_LABEL_VAR_WO input)
        {
            var result = new ApiAction();
            try
            {
                //保存前的判断
                if (input.WORK_ORDER.IsNullOrEmpty() && input.PROD_CODE.IsNullOrEmpty())
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单号或者产品编码不能为空!");
                    return result;
                }
                List<BAS_LABEL_VAR_WO> varWoList = new();
                string imageFileName = Path.GetFileNameWithoutExtension(input.LABEL_VIEW_PATH);
                string path = @$"{BizConfig.Configuration["UploadAddress"]}//Upload/Template//Temp//{imageFileName}.png";
                if (File.Exists(path))
                {
                    varWoList = await Biz.Db.Queryable<BAS_LABEL_VAR_WO>()
                        .Where(x => x.LABEL_ID == input.LABEL_ID)
                        .WhereIF(!input.WORK_ORDER.IsNullOrEmpty(), x => x.WORK_ORDER == input.WORK_ORDER)
                        .WhereIF(!input.PROD_CODE.IsNullOrEmpty(), x => x.PROD_CODE == input.PROD_CODE)
                        .ToListAsync();
                    foreach (var item in varWoList)
                    {
                        item.LABEL_VIEW_PATH = $"{BizConfig.Configuration["DownloadAddress"]}//Template//Temp//{imageFileName}.png";
                    }
                }
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    Expression<Func<BAS_LABEL_VAR_WO, object>> predicate = !input.WORK_ORDER.IsNullOrEmpty() ? t => new { t.LABEL_ID, t.WORK_ORDER, t.VAR_NAME, t.GHOST_ROW } : 
                    !input.PROD_CODE.IsNullOrEmpty() ? t => new { t.LABEL_ID, t.PROD_CODE, t.VAR_NAME, t.GHOST_ROW } : t => new { t.LABEL_ID, t.VAR_NAME, t.GHOST_ROW };
                    var y = db.Storageable(input)
                       .WhereColumns(predicate)
                       .ToStorage();
                    y.AsInsertable.ExecuteCommand();
                    y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
 
                    if (varWoList.Count > 0)
                    {
                        db.Updateable(varWoList).UpdateColumns(q => q.LABEL_VIEW_PATH).ExecuteCommand();
                    }
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"更新工单模板变量异常");
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "更新工单模板变量异常");
            }
            return result;
        }
 
        /// <summary>
        /// 获取工单模板变量
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction<QueryAble<BAS_LABEL_VAR_WO>>> GetLabelVarByWorkOrder(BizLabelVarWoInput input)
        {
            var result = new ApiAction<QueryAble<BAS_LABEL_VAR_WO>>();
            try
            {
                QueryAble<BAS_LABEL_VAR_WO> query = new();
                //if (input.WorkOrder.IsNullOrEmpty())
                //{
                //    result.IsSuccessed = false;
                //    result.LocaleMsg = new($"工单号不能为空!");
                //    return result;
                //}
                var labeltemp = Biz.Db.Queryable<BAS_LABEL_TEMP>().Where(q => q.LABEL_CODE.Equals(input.LabelId) || q.ID.Equals(input.LabelId)).First();
                if (labeltemp == null)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"模板不存在!");
                    return result;
                }
                var lableVars = await Biz.Db.Queryable<BAS_LABEL_VAR_WO>().Where(q => SqlFunc.IsNullOrEmpty(q.WORK_ORDER)
                && SqlFunc.IsNullOrEmpty(q.PROD_CODE)
                && SqlFunc.IsNullOrEmpty(q.CUST_CODE)
                && q.LABEL_ID.Equals(labeltemp.ID)).ToListAsync();
                var queryable = Biz.Db.Queryable<BAS_LABEL_VAR_WO>()
                    .Where(q => q.LABEL_ID.Equals(labeltemp.ID))
                    .WhereIF(!input.WorkOrder.IsNullOrEmpty(), q => q.WORK_ORDER.Equals(input.WorkOrder))
                    .WhereIF(!input.ProdCode.IsNullOrEmpty(), q => q.PROD_CODE.Equals(input.ProdCode))
                    .WhereIF(!input.CustCode.IsNullOrEmpty(), q => q.PROD_CODE.Equals(input.CustCode));
                query.page = await queryable.ToPageAsync(input.page, input.pageSize);
                //如果不存在,则返回初始数据
                if (query.page.totals == 0)
                {
                    query.page = await Biz.Db.Queryable<BAS_LABEL_VAR_WO>().Where(q => SqlFunc.IsNullOrEmpty(q.WORK_ORDER)
                     && SqlFunc.IsNullOrEmpty(q.PROD_CODE)
                     && SqlFunc.IsNullOrEmpty(q.CUST_CODE)
                     && q.LABEL_ID.Equals(labeltemp.ID))
                     .ToPageAsync(input.page, input.pageSize);
                    query.Items = query.page.data;
                }
                //如果初始变量数大于工单设置的变量数
                if (lableVars.Count > query.page.totals)
                {
                    foreach (var item in lableVars)
                    {
                        if (queryable.ToList().Any(q => q.VAR_NAME == item.VAR_NAME))
                        {
                            var list = queryable.ToList().Where(q => q.VAR_NAME == item.VAR_NAME).First();
                            item.ID = list?.ID;
                            item.WORK_ORDER = input.WorkOrder;
                            item.PROD_CODE = input.ProdCode;
                            item.VAR_VALUE = list?.VAR_VALUE;
                            item.LABEL_VIEW_PATH = list?.LABEL_VIEW_PATH;
                        }
                    }
                    query.Items = lableVars.Skip((input.page - 1) * input.pageSize).Take(input.pageSize).ToList();
                    query.page.totals = lableVars.Count;
                    query.page.data = query.Items;
                }
                result.Data = query;
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "获取工单模板变量异常");
            }
            return result;
        }
 
        /// <summary>
        /// 更新行为中的箱号
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<ApiAction> SavePrintLabelInAct(string code)
        {
            var result = new ApiAction();
            try
            {
                var woSn = await Biz.Db.Queryable<BIZ_MES_WO_SN>().Where(q=>q.SN == code || q.FLOW_SN == code || q.OUTER_SN == code).FirstAsync();
                if (woSn == null)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"条码不存在!");
                    return result;
                }
                //取得包装列表
                var pkgList = Biz.Db.Queryable<MES_WIP_PKG>().Where(q => q.PARENT_SN == woSn.OUTER_SN && q.OPER_CODE == "SpecPackingNode").ToList();
                if (pkgList.Count == 0)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"条码不存在!");
                    return result;
                }
                List<MES_WIP_ACT> actlist = new List<MES_WIP_ACT>();
                //客户标签
                var wipAct = Biz.Db.Queryable<MES_WIP_ACT>().Where(q => q.CARTON_SN == woSn.OUTER_SN && q.NODE_ID == pkgList[0].NODE_ID && !SqlFunc.IsNullOrEmpty(q.ACT_VALUE_3)).First();
                if (!wipAct.IsNullOrEmpty())
                {
                    var label = JsonConvert.DeserializeObject<BAS_LABEL_TEMP>(wipAct.ACT_VALUE_3);
                    var cardNo = label.Variables.Where(q => q.VAR_NAME == "CardQR").FirstOrDefault();
                    if (!woSn.OUTER_SN.IsNullOrEmpty() && !cardNo.IsNullOrEmpty() && !cardNo.Value.StartsWith(woSn.OUTER_SN))
                    {
                        cardNo.Value = $"{woSn.OUTER_SN}{cardNo.Value}";
                    }
                    string ACT_VALUE_3 = JsonConvert.SerializeObject(label);
                    foreach (var pkg in pkgList)
                    {
                        var actSn = Biz.Db.Queryable<MES_WIP_ACT>().Where(q => q.WIP_ID == pkg.WIP_ID && q.NODE_ID == pkg.NODE_ID && q.ACT_VALUE_4 == "Customer").First();
                        if (!actSn.IsNullOrEmpty())
                        {
                            actSn.ACT_VALUE_3 = ACT_VALUE_3;
                            actlist.Add(actSn);
                        }
                    }
                }
                //入库标签
                var wipActInStore = Biz.Db.Queryable<MES_WIP_ACT>().Where(q => q.SN == woSn.SN && q.NODE_ID == pkgList[0].NODE_ID && !SqlFunc.IsNullOrEmpty(q.ACT_VALUE_3) && q.ACT_VALUE_4 == "InStore").First();
                if (!wipActInStore.IsNullOrEmpty())
                {
                    var label = JsonConvert.DeserializeObject<BAS_LABEL_TEMP>(wipActInStore.ACT_VALUE_3);
                    var cardNo = label.Variables.Where(q => q.VAR_NAME == "CardQR").FirstOrDefault();
                    if (!woSn.OUTER_SN.IsNullOrEmpty() && !cardNo.IsNullOrEmpty() && !cardNo.Value.StartsWith(woSn.OUTER_SN))
                    {
                        cardNo.Value = $"{woSn.OUTER_SN}{cardNo.Value}";
                    }
                    var xh = label.Variables.Where(q => q.VAR_NAME == "XH").FirstOrDefault();
                    if (!woSn.OUTER_SN.IsNullOrEmpty() && !xh.IsNullOrEmpty())
                    {
                        xh.Value = $"{woSn.OUTER_SN}";
                    }
                    string ACT_VALUE_3 = JsonConvert.SerializeObject(label);
                    foreach (var pkg in pkgList)
                    {
                        var actSn = Biz.Db.Queryable<MES_WIP_ACT>().Where(q => q.WIP_ID == pkg.WIP_ID && q.NODE_ID == pkg.NODE_ID && q.ACT_VALUE_4 == "InStore").First();
                        if (!actSn.IsNullOrEmpty())
                        {
                            actSn.ACT_VALUE_3 = ACT_VALUE_3;
                            actlist.Add(actSn);
                        }
                    }
                }
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    db.Updateable(actlist).UpdateColumns(q => new { q.ACT_VALUE_3 }).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"更新行为日志中的箱号异常");
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "更新行为日志中的箱号异常");
            }
            return result;
        }
 
        /// <summary>
        /// 获取重打标签信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction> GetRePrintInfo(RePrintInput input)
        {
            var result = new ApiAction();
            try
            {
                //先查出工单条码中是否存在
                var outerSn = input.Code.Split('|')[0];
                var woSn = await Biz.Db.Queryable<BIZ_MES_WO_SN>().Where(q => q.SN.Equals(input.Code) || q.FLOW_SN.Equals(input.Code) || q.OUTER_SN == outerSn).FirstAsync();
                if (woSn == null)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"条码不存在!");
                    return result;
                }
                var woNode = await Biz.Db.Queryable<MES_WO_NODE>().Where(q => q.WORK_ORDER.Equals(woSn.WORK_ORDER) && q.OPER_CODE== "SpecPackingNode").FirstAsync();
                if (woNode == null)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工艺路线节点[SpecPackingNode-装箱]不存在");
                    return result;
                }
                string printParam = "";
                var labelPrint = await Biz.Db.Queryable<LOG_LABEL_PRINT>()
                    .WhereIF(input.ReqType == 0, q => q.SN.Equals(woSn.SN) || q.SN.Equals(woSn.FLOW_SN)) //白盒标签
                    .WhereIF(input.ReqType == 1, q => q.SN.Equals(woSn.OUTER_SN) && q.PRINT_LABEL != "InStoreLabel") //箱标签-客户
                    .WhereIF(input.ReqType == 2, q => q.SN.Equals(woSn.OUTER_SN) && q.PRINT_LABEL == "InStoreLabel") //箱标签-入库
                    .FirstAsync();
                //如果查不到就去行为日志查
                if (labelPrint.IsNullOrEmpty() && input.ReqType == 1)
                {
                    printParam = Biz.Db.Queryable<MES_WIP_ACT>().Where(q => q.CARTON_SN.Equals(woSn.OUTER_SN) && q.ACT_VALUE_4 == "Customer" && !SqlFunc.IsNullOrEmpty(q.ACT_VALUE_3)).First()?.ACT_VALUE_3 ?? "";
                }
                else if (labelPrint.IsNullOrEmpty() && input.ReqType == 2)
                {
                    printParam = Biz.Db.Queryable<MES_WIP_ACT>().Where(q => q.SN.Equals(woSn.SN) && q.ACT_VALUE_4 == "InStore").First()?.ACT_VALUE_3 ?? "";
                }
                else
                {
                    printParam = labelPrint?.PRINT_PARAM ?? "";
                }
                result.Data = printParam;
                result.LocaleMsg = new($"重打条码[{input.Code}]的标签成功");
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "获取重打标签信息异常");
            }
            return result;
        }
 
        /// <summary>
        /// 获取出货信息
        /// </summary>
        /// <param name="wo"></param>
        /// <returns></returns>
        public async Task<ApiAction<ShippingOutput>> GetShipList(ShippingInput input)
        {
            var result = new ApiAction<ShippingOutput>();
            try
            {
                DbClient db = Biz.DataSource["YadaU9C"].Client;
                var org = await Biz.Db.Queryable<SYS_ORGANIZATION>().Where(q => q.ORG_CODE == input.orgCode).FirstAsync();
                string wo = input.wo;
                if (!input.SN.IsNullOrEmpty() && input.wo.IsNullOrEmpty()) {
                    var woSn = Biz.Db.Queryable<BIZ_MES_WO_SN>().Where(q => q.SN == input.SN || q.FLOW_SN == input.SN).First();
                    if (!woSn.IsNullOrEmpty()) {
                        wo = woSn.WORK_ORDER;
                    }
                }
                var ship = db.Queryable<mes_ShipList>().Where(q => q.MoDoc.Equals(wo) && q.Org == org.ID.ToInt64() && q.Status == 2 ).First();
                if (!ship.IsNullOrEmpty())
                {
                    result.Data = ship.BusinessDate.Date == DateTime.Now.Date ? new()
                    {
                        ShipDoc = ship.ShipDoc,
                        ShippingQty = ship.ShipQty,
                        Tag = "Customer"
                    } : new()
                    {
                        ShipDoc = ship.ShipDoc,
                        ShippingQty = ship.ShipQty,
                        Tag = "InStoreLabel"
                    };
                    result.LocaleMsg = new($"出货信息存在!");
                }
                else
                {
                    result.Data = new()
                    {
                        ShipDoc = "",
                        ShippingQty = 0,
                        Tag = "InStoreLabel"
                    };
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "获取出货信息异常");
            }
            return result;
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="SN"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task<ApiAction> UnbindWipSnFromWO(UnbindWipSnInput input)
        {
            var result = new ApiAction();
            try
            {
                var cartonSn = input.SN.Split('|')[0];
                var wipList = await Biz.Db.Queryable<MES_WIP_DATA>().Where(q => q.SN.Equals(input.SN) || q.FLOW_SN.Equals(input.SN) || q.CARTON_SN == cartonSn).ToListAsync();
                if (wipList.Count == 0)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"解绑的条码不存在当前操作表中!");
                    return result;
                }
                var wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(q => q.ORDER_NO == wipList[0].WORK_ORDER).FirstAsync();
                result = WorkBatch.UnbindWipSnFromWO(wo, wipList, input.user);
                if (result.IsSuccessed)
                {
                    var newList = await Biz.Db.Queryable<MES_WIP_PKG>().Where(q => q.WORK_ORDER == wo.ORDER_NO && q.BATCH_NO == wipList[0].BATCH_NO && SqlFunc.IsNullOrEmpty(q.PARENT_SN) && q.QTY > 0).ToListAsync();
                    result.Data = newList;
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "解绑条码异常");
            }
            return result;
        }
 
        /// <summary>
        /// 生成入库获取条码工单信息及包装信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<ApiAction<InStoreInfo>> GetErpProdInBth(string code)
        {
            var result = new ApiAction<InStoreInfo>();
            try
            {
                //得到工单条码记录
                var cartonSn = code.Split('|')[0];
                var woSns = await Biz.Db.Queryable<BIZ_MES_WO_SN>().Where(q => q.OUTER_SN == cartonSn).ToListAsync();
                if (woSns.Count == 0)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"条码不存在!");
                    return result;
                }
                var wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(q => q.ORDER_NO == woSns[0].WORK_ORDER ).IncludesAllFirstLayer().IncludesAllSecondLayer(q => q._ItemInfos).FirstAsync();
                if (wo == null)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"条码对应的工单不存在");
                    return result;
                }
                //判断工单是否已经全部入库
 
 
                InStoreInfo inStoreInfo = new() {
                    ErpProdInBth= new() { 
                        WORK_ORDER = wo.ORDER_NO,
                        BATCH_NO = woSns[0].BATCH_NO,
                        SALES_ORDER = wo.SALES_ORDER,
                        ITEM_CODE = wo.ITEM_CODE,
                        ITEM_NAME = wo.ItemInfo?.ITEM_DESC,
                        SapCode = wo.ItemInfo?.ExtInfo?.SapCode,
                        SCAN_QTY = woSns.Count,
                        IS_HANDLED = "N" //Y-已生成,N-未生成,F-失败
                    },
                    InStoreScanInfo = new() { 
                        SN = cartonSn,
                        SALES_ORDER = wo.SALES_ORDER,
                        WORK_ORDER = wo.ORDER_NO,
                        BATCH_NO = woSns[0].BATCH_NO,
                        SCAN_QTY = woSns.Count
                    }
                };
                result.Data = inStoreInfo;
                result.LocaleMsg = new($"获取条码[{code}]工单信息及包装信息成功");
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, $"获取条码[{code}]工单信息及包装信息异常");
            }
            return result;
        }
 
        #region 提交前通用方法
        /// <summary>
        /// 提交前检查是否岗位、工单和不良代码
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<ApiAction<CheckCodeOutput>> CheckCode(string code)
        {
            var action = new ApiAction<CheckCodeOutput>($"检查条码{code}成功", new CheckCodeOutput());
            try
            {
                action.Data.Position = DbCache.Cache.Position[code];
                action.Data.WorkOrder = await Biz.Db.Queryable<BIZ_MES_WO>().Where(q => q.ORDER_NO == code).FirstAsync();
                action.Data.Defect = DbCache.Cache.Defect[code];
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"检查条码{code}异常");
            }
            return action;
        }
        #endregion
    }
}