服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
5 天以前 beca28ecb3a730ffb33c21e0c55c729774725faf
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
using Rhea.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tiger.Model;
using Tiger.IBusiness;
using Tiger.Model.Sharetronic.Shelf;
using Tiger.Business.WMS.Sharetronic.Shelf;
using Tiger.Model.Entitys.MES.U9C;
using Tiger.Model.MES.Yada;
 
namespace Tiger.Business.WMS.Transaction
{
    /// <summary>
    /// 收货单(U9)上架事务
    /// </summary>
    public class In_BIZ_U9_RECEIPT : WmsTask, IIn_BIZ_U9_RECEIPT
    {
 
        public IIn_BIZ_U9_RECEIPT Init(string id, string userCode, string apiHost, string orgCode)
        {
            base.Init(id, userCode, apiHost, orgCode);
            Logger.Console.Info($"Start {this.GetType().Name} Transaction[ID: {TransID}]");
            return this;
        }
 
        #region Propertys & Variables
        public BIZ_U9_RECEIPT CurOrder { get; set; }
        #endregion
 
        #region Functions
        /// <summary>
        /// 扫描入口
        /// </summary>
        public async Task<ApiAction<ScanOutput>> Scan(BaseInput input)
        {
            var action = new ApiAction<ScanOutput>(new ScanOutput());
            try
            {
                if (input.Command == "ScanOrder")
                {
                    var inputOrder = new BaseInput<BIZ_U9_RECEIPT>(input);
                    if (inputOrder.Data?.ORDER_NO == null)
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("选择的单号不能为空");
                        action.LocaleMsg = Biz.L("WMS.Default.ScanOrder.OrderNoEmptyFailure");
                        return SetOutPutMqttMsg(action, input.Locale);
                    }
                    action = await ScanOrder(inputOrder);
                }
                else
                {
                    if (CurOrder.IsNullOrEmpty())
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("上架前请先选择需要上架的收货单");
                        action.LocaleMsg = Biz.L("WMS.In_BIZ_U9_RECEIPT.ScanItem.NoScanOrder");
                        action.Data.Command = "ScanOrder";
                        return SetOutPutMqttMsg(action, input.Locale);
                    }
                    if (input.SN.IsNullOrEmpty())
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("条码不能为空");
                        action.LocaleMsg = Biz.L("WMS.Default.ScanItem.SnEmptyFailure");
                        return SetOutPutMqttMsg(action, input.Locale);
                    }
                    //判断扫描的是否货架
                    var whUnit = await MainDB.Queryable<V_WH_UNIT>().Where(t => (t.SHELF_CODE.ToUpper() == input.SN || t.LOCATION_CODE.ToUpper() == input.SN) && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync();
                    //if (string.IsNullOrEmpty(CurScanShelf?.ShelfCode))
                    if (!whUnit.IsNullOrEmpty() || string.IsNullOrEmpty(CurScanShelf?.ShelfCode))
                    {
                        action = await ScanShelf(input.SN, whUnit);
                    }
                    else//扫描物料并复核
                    {
                        action = await ScanItem(input);
                    }
                }
            }
            catch (Exception ex)
            {
                //取消当前操作
                ResetTrans();
                //action.CatchExceptionWithLog(ex, $"扫描[{input.SN}]异常");
                action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.Scan.ScanException", input.SN));
            }
            return SetOutPutMqttMsg(action, input.Locale);
        }
 
        /// <summary>
        /// 扫描下架单据号码
        /// </summary>
        public async Task<ApiAction<ScanOutput>> ScanOrder(BaseInput<BIZ_U9_RECEIPT> input)
        {
            var action = new ApiAction<ScanOutput>(new ScanOutput());
            try
            {
                var receipt = await MainDB.Queryable<BIZ_U9_RECEIPT>().Where(q => q.ORDER_NO == input.Data.ORDER_NO).Includes(q => q.Details, d => d._ItemInfos).IncludesAllFirstLayer().FirstAsync();
                if (receipt.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    //action.LocaleMsg = Biz.L("找不到收货单[{0}]的信息,请重新选择正确的收货单");
                    action.LocaleMsg = Biz.L("WMS.In_BIZ_U9_RECEIPT.ScanOrder.ReceiptNotExists", input.Data.ORDER_NO);
                    return SetOutPutMqttMsg(action, input.Locale);
                }
                action.Data.Command = "Normal";
                action.Data.Data = CurOrder = receipt;
            }
            catch (Exception ex)
            {
                //取消当前操作
                ResetTrans();
                //action.CatchExceptionWithLog(ex, $"选择收货单异常(Data: {0})");
                action.CatchExceptionWithLog(ex, Biz.L("WMS.In_BIZ_U9_RECEIPT.ScanOrderException", input?.Data));
            }
            return action;
        }
 
        /// <summary>
        /// 扫描物料并复核,如果物料已经完成移库则货架上亮灯提醒储位
        /// </summary>
        public async Task<ApiAction<ScanOutput>> ScanItem(BaseInput input)
        {
            var action = new ApiAction<ScanOutput>(new ScanOutput() { Command = Command });
            try
            {
                if (CurInvItem.IsNullOrEmpty())
                {
                    //解析条码
                    Result<IInventory> result = GetInventory(input.SN, input.AuthOption, true);
                    if (!result.IsSuccessed)
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = result.LocaleMsg;
                        return SetOutPutMqttMsg(action, input.Locale);
                    }
                    var inv = result.Data as Inventory;
 
                    //验证条码是否正确
                    if (!inv.isNormalStatus || inv.Status != WMS_ITEM.STATUSs.WaitIn)
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("状态[{0}]异常,请重新扫描");
                        action.LocaleMsg = Biz.L("WMS.Default.ScanItem.StatusException", string.Join(',', inv.StatusList.Select(q => q.GetDesc())));
                        return SetOutPutMqttMsg(action, input.Locale);
                    }
                    //物料验证
                    if (inv.ItemInfo.IsNullOrEmpty() || inv.ItemInfo.IS_ACTIVE == "N")
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("物料编码[{0}]不存在或者该物料未启用");
                        action.LocaleMsg = Biz.L("WMS.Default.ScanItem.ItemCodeNotExistsOrNotActive", inv.ItemInfo.ITEM_CODE.IsNullOrEmpty(inv.Barcode.ItemCode));
                        return SetOutPutMqttMsg(action, input.Locale);
                    }
                    //默认储位验证
                    if (!inv.ItemInfo.DEFAULT_LOCATION.IsNullOrEmpty() && inv.ItemInfo.DEFAULT_LOCATION != CurScanShelf.LocationCode)
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("物料[{0}]只允许上架到储位[{1}],请重新扫描储位");
                        action.LocaleMsg = Biz.L("WMS.Default.ScanItem.DefaultLocationError", inv.ItemInfo.ITEM_CODE, inv.ItemInfo.DEFAULT_LOCATION);
                        return SetOutPutMqttMsg(action, input.Locale);
                    }
                    //判断当前条码是否属于当前收货单
                    if (inv.Items.First().TRANS_NO != CurOrder.ORDER_NO)
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("条码所属的单据[{0}]不是选择的收货单[{1}],请重新扫描正确的条码或选择其他收货单");
                        action.LocaleMsg = Biz.L("WMS.In_BIZ_U9_RECEIPT.ScanItem.NotCurOrder", CurInvItem.Items.First().TRANS_NO, CurOrder.ORDER_NO);
                        return SetOutPutMqttMsg(action, input.Locale);
                    }
                    CurInvItem = inv;
                }
 
                var receipt = await MainDB.Queryable<BIZ_U9_RECEIPT>().Where(q => q.ORDER_NO == CurOrder.ORDER_NO).Includes(q => q.Details, d => d._ItemInfos).IncludesAllFirstLayer().FirstAsync();
                var receiptDtl = receipt.Details.First(q => q.LINE_NO == CurInvItem.Items.First().TRANS_LINE);
                if (receiptDtl.STATUS > BIZ_U9_RECEIPT.STATUSs.Storing.GetValue())
                {
                    action.IsSuccessed = false;
                    //action.LocaleMsg = Biz.L("收货单中行[{0}]状态[{1}]异常,请扫描正确的条码");
                    action.LocaleMsg = Biz.L("WMS.In_BIZ_U9_RECEIPT.ScanItem.ReceiptDtlStatusException", receiptDtl.LINE_NO, receiptDtl.STATUS.GetEnumDesc<BIZ_U9_RECEIPT.STATUSs>());
                    return SetOutPutMqttMsg(action, input.Locale);
                }
                var receiptSn = receipt.SnList.Where(q => CurInvItem.Items.Any(i => i.SN == q.SN)).ToList();
                if (receiptSn.Any(q => q.STATUS != WMS_ITEM.STATUSs.WaitIn.GetValue()))
                {
                    action.IsSuccessed = false;
                    //action.LocaleMsg = Biz.L("收货单中条码[{0}]状态[{1}]异常,请扫描正确的条码");
                    action.LocaleMsg = Biz.L("WMS.In_BIZ_U9_RECEIPT.ScanItem.ReceiptSnStatusException", CurInvItem.SN, receiptSn.First(q => q.STATUS != WMS_ITEM.STATUSs.WaitIn.GetValue()).STATUS.GetEnumDesc<WMS_ITEM.STATUSs>());
                    return SetOutPutMqttMsg(action, input.Locale);
                }
                //从U9获取收货单行信息
                var u9Dtl = Biz.DataSource["YadaU9C"].Client.Queryable<mes_ReturnedDocInfo>().Where(x => x.RcvId == receipt.ID.ToDouble() && x.DocLineNo == receiptDtl.LINE_NO.ToDouble() && x.SplitFlag != 1).First();
                //收货单的状态判断是否已提交IQC审核,未通过IQC不能上架
                if (!u9Dtl.IsNullOrEmpty() && u9Dtl.Status != 3)
                {
                    action.IsSuccessed = false;
                    //action.LocaleMsg = Biz.L("收货单行状态[{0}]异常,请先提交IQC检验结果");
                    action.LocaleMsg = Biz.L("WMS.In_BIZ_U9_RECEIPT.ScanItem.U9StatusException", u9Dtl.StatusName);
                    return SetOutPutMqttMsg(action, input.Locale);
                }
                if (!u9Dtl.IsNullOrEmpty() && u9Dtl.RcvQtyTU != receiptDtl.QTY_OK)
                {
                    receiptDtl.ID = u9Dtl.RcvLineId.ToString();
                    receiptDtl.QTY_OK = u9Dtl.RcvQtyTU;
                    receiptDtl.QTY_NG = receiptDtl.QTY - receiptDtl.QTY_OK;
                    receiptDtl.ERP_STATUS_CODE = u9Dtl.Status.ToString();
                    receiptDtl.ERP_STATUS_NAME = u9Dtl.StatusName;
                }
 
                var nLocation = new WMS_LOCATION();
                // 判断是否智能货架
                if (CurScanShelf.Shelf.IsLightShelf)
                {
                    ShelfApiResult shelfApiResult = await Share.Shelf.PutOn(TransID, CurScanShelf.Shelf, CurInvItem.Items[0]);
                    if (!shelfApiResult.IsSuccess)
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = Biz.L(shelfApiResult.GetData<string>());
                        return SetOutPutMqttMsg(action, input.Locale);
                    }
                    var reaultShelf = shelfApiResult.GetData<ShelfChangeModel>();
 
                    nLocation = reaultShelf.GetLocation();
                    if (nLocation == null)
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L($"货架[{0}]中不存在id为[{1}]的储位,请先维护货架信息");
                        action.LocaleMsg = Biz.L("WMS.Default.ScanItem.LocationNotExistsInShelf", CurScanShelf.Shelf.SHELF_CODE, reaultShelf.ledAddr);
                        return SetOutPutMqttMsg(action, input.Locale);
                    }
                    var locationData = MainDB.Queryable<WMS_ITEM>().Where(q => q.LOCATION_ID == nLocation.ID).First();
                    if (!locationData.IsNullOrEmpty())
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("储位[{0}]已存有物料[{1}],请检查系统库存信息");
                        action.LocaleMsg = Biz.L($"WMS.Default.ScanShelf.ItemAlreadyExistsInLocation", nLocation.LOCATION_CODE, locationData.SN);
                        return SetOutPutMqttMsg(action, input.Locale);
                    }
                    CurScanShelf.Location = nLocation;
                    CurScanShelf.LocationCode = nLocation.LOCATION_CODE;
                }
                else
                {
                    if (CurScanShelf.LocationCode.IsNullOrEmpty() || CurScanShelf.WarehouseCode.IsNullOrEmpty())
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("请输入或扫描有效的货架/储位码");
                        action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ShelfCanNotEmpty");
                        return SetOutPutMqttMsg(action, input.Locale);
                    }
 
                    nLocation = await MainDB.Queryable<WMS_LOCATION>().Where(t => t.LOCATION_CODE == CurScanShelf.LocationCode && t.AUTH_ORG == OrgCode).FirstAsync();
                }
 
                //判断储位是否单放
                if (nLocation.IS_SINGLE == "Y" && CurInvItem.Items.Count > 1)
                {
                    action.IsSuccessed = false;
                    //action.LocaleMsg = Biz.L("储位[{0}]只能存放一个物料");
                    action.LocaleMsg = Biz.L("WMS.Default.ScanItem.LocationSingleFailure", nLocation.LOCATION_CODE);
                    ResetTrans();
                    return SetOutPutMqttMsg(action, input.Locale);
                }
 
                CurOrder = receipt;
 
                //执行上架数据处理
                foreach (var item in CurInvItem.Items)
                {
                    item.IS_LOCKED = "Y";
                    item.SOURCE_CODE = item.TRANS_CODE;
                    item.SOURCE_ORDER = item.TRANS_NO;
                    item.SOURCE_LINE = item.TRANS_LINE;
                    item.TRANS_CODE = nameof(BIZ_U9_RECEIPT);
                    item.TRANS_NO = receipt.ORDER_NO;
                    item.TRANS_LINE = receiptDtl.LINE_NO;
                    item.PROD_DATE = item.PROD_DATE < new DateTime(2000, 1, 1) ? DateTime.Now : item.PROD_DATE;
                    item.FIRST_IN_DATE = item.FIRST_IN_DATE < new DateTime(2000, 1, 1) ? DateTime.Now : item.FIRST_IN_DATE;
                }
                foreach (var item in CurInvItem.Packages)
                {
                    item.SOURCE_CODE = item.TRANS_CODE;
                    item.SOURCE_ORDER = item.TRANS_NO;
                    item.SOURCE_LINE = item.TRANS_LINE;
                    item.TRANS_CODE = nameof(BIZ_U9_RECEIPT);
                    item.TRANS_NO = receipt.ORDER_NO;
                    item.TRANS_LINE = receiptDtl.LINE_NO;
                }
                Result putonResult = PutOn(input.AuthOption, nLocation.LOCATION_CODE);
                if (!putonResult.IsSuccessed)
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = putonResult.LocaleMsg;
                    ResetTrans();
                    return SetOutPutMqttMsg(action, input.Locale);
                }
 
                //更新单据信息
                foreach (var sn in receiptSn)
                {
                    sn.STATUS = WMS_ITEM.STATUSs.InStore.GetValue();
                    sn.IS_IN = "Y";
                }
                //如果当前行上架完成标记为待审核
                receiptDtl.QTY_IN = receipt.SnList.Where(q => q.LINE_NO == receiptDtl.LINE_NO && q.STATUS == WMS_ITEM.STATUSs.InStore.GetValue()).Sum(q => q.QTY);
                if (receiptDtl.QTY_IN == receiptDtl.QTY_OK)
                {
                    receiptDtl.STATUS = BIZ_U9_RECEIPT.STATUSs.Review.GetValue();
                    //当前单据明细行已经全部上架完成,可以调用U9行审接口,如果行审失败则报错,最后一个上架失败
                    var iInput = new SubmitLineInput
                    {
                        userId = UserCode,
                        IsLogin = true,
                        param = new()
                        {
                            RcvLineID = receiptDtl.ID,
                            RcvDocNo = receiptDtl.ORDER_NO,
                            DocLineNo = receiptDtl.LINE_NO,
                            OrgCode = receiptDtl.AUTH_ORG,
                        }
                    };
                    var result = await DI.Resolve<IWMS_U9C>().ReceivementApproveLine(iInput);
                    if (!result.IsSuccessed)
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = result.LocaleMsg;
                        return SetOutPutMqttMsg(action, input.Locale);
                    }
                    else
                    {
                        receiptDtl.STATUS = BIZ_U9_RECEIPT.STATUSs.Finished.GetValue();
                    }
                }
                else
                {
                    receiptDtl.STATUS = BIZ_U9_RECEIPT.STATUSs.Storing.GetValue();
                }
                //更新单据状态
                if (receipt.Details.Any(q => q.STATUS < BIZ_U9_RECEIPT.STATUSs.Review.GetValue()))
                {
                    receipt.STATUS = BIZ_U9_RECEIPT.STATUSs.Storing.GetValue();
                }
                else if (receipt.Details.Any(q => q.STATUS < BIZ_U9_RECEIPT.STATUSs.Finished.GetValue()))
                {
                    receipt.STATUS = BIZ_U9_RECEIPT.STATUSs.Review.GetValue();
                }
                else
                {
                    receipt.STATUS = BIZ_U9_RECEIPT.STATUSs.Finished.GetValue();
                }
 
                //创建变量克隆对象用于传入DBSubmitAction中保存当前需要暂存的数据值
                var _receipt = receipt.Clone();
                AddCommitAction("Receipt", () =>
                {
                    //使用统一的事务DB对象
                    var db = GetCommitDB();
                    //数据保存逻辑
                    db.Updateable(receipt, UserCode).UpdateColumns(q => new { q.STATUS, q.UPDATE_TIME, q.UPDATE_USER }).ExecuteCommand();
                    db.Updateable(receiptDtl, UserCode).UpdateColumns(q => new { q.ID, q.STATUS, q.QTY_IN, q.QTY_OK, q.QTY_NG, q.ERP_STATUS_CODE, q.ERP_STATUS_NAME, q.UPDATE_TIME, q.UPDATE_USER }).ExecuteCommand();
                    db.Updateable(receiptSn, UserCode).UpdateColumns(q => new { q.STATUS, q.IS_IN, q.UPDATE_TIME, q.UPDATE_USER }).ExecuteCommand();
                });
 
                //完成所有处理后使用事务保存数据
                action = DoIfFinish(action, input.Locale, () =>
                {
                    //设置当前物料的默认储位
                    if (CurInvItem.ItemInfo.DEFAULT_LOCATION.IsNullOrEmpty())
                    {
                        SetDefaultLocation(MainDB, CurInvItem.ItemInfo, nLocation);
                    }
                });
                //if (!action.Data.Data.IsNullOrEmpty() && action.Data.Data is DefaultInStoreOutput)
                //{
                //    (action.Data.Data as DefaultInStoreOutput).OrderInfo = receipt;
                //}
            }
            catch (Exception ex)
            {
                //取消当前操作
                ResetTrans();
                //action.CatchExceptionWithLog(ex, $"扫描条码[{input.SN}]复核异常");
                action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.ScanItem.ScanException", input.SN));
            }
            return SetOutPutMqttMsg(action, input.Locale);
        }
 
        /// <summary>
        /// 扫描货架或者储位
        /// </summary>
        public async Task<ApiAction<ScanOutput>> ScanShelf(string Code, V_WH_UNIT whUnit)
        {
            var action = new ApiAction<ScanOutput>(new ScanOutput() { Command = Command });
            try
            {
                if (Code.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    //action.LocaleMsg = Biz.L("请输入或扫描有效的货架/储位码");
                    action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ShelfCanNotEmpty");
                    return action;
                }
                if (!CurOrder.Details.Any(q => q.ItemInfo.DEFAULT_LOCATION.IsNullOrEmpty() || q.ItemInfo.DEFAULT_LOCATION == Code))
                {
                    action.IsSuccessed = false;
                    //action.LocaleMsg = Biz.L("当前储位[{0}]不在收货单需要上架的储位列表中:{1}");
                    action.LocaleMsg = Biz.L("WMS.In_BIZ_U9_RECEIPT.ScanShelf.NoContainLocation", Code, string.Join(", ", CurOrder.Details.Select(q => q.ItemInfo.DEFAULT_LOCATION)));
                    return action;
                }
 
                CurScanShelf = new ScanShelfInfo();
 
                // 查询货架信息
                whUnit = whUnit.IsNullOrEmpty(await MainDB.Queryable<V_WH_UNIT>().Where(t => (t.SHELF_CODE.ToUpper() == Code || t.LOCATION_CODE.ToUpper() == Code) && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync());
 
                // 扫描货架代码,且为智能货架
                if (!whUnit.IsNullOrEmpty() && whUnit.SHELF_CODE  == Code)
                {
                    CurScanShelf.Shelf = whUnit.Shelf;
                    CurScanShelf.WarehouseCode = whUnit.WH_CODE;
                    CurScanShelf.RegionCode = whUnit.REGION_CODE;
                    CurScanShelf.ShelfCode = whUnit.SHELF_CODE;
                    CurScanShelf.ShelfType = whUnit.SHELF_TYPE;
                    CurScanShelf.IsSmartRack = true;
                }
                // 扫描库位代码
                else if (!whUnit.IsNullOrEmpty() && whUnit.LOCATION_CODE == Code)
                {
                    //var nLocation = await MainDB.Queryable<V_WH_UNIT>().Where(t => t.LOCATION_CODE.ToUpper() == Code.ToUpper() && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync();
                    //if (nLocation == null)
                    //{
                    //    action.IsSuccessed = false;
                    //    //action.LocaleMsg = Biz.L("储位[{0}]不存在");
                    //    action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.LocationNotExist", Code);
                    //    return action;
                    //}
                    if (whUnit.IS_ACTIVE == "N")
                    {
                        action.IsSuccessed = false;
                        //action.LocaleMsg = Biz.L("扫描的储位[{0}]未启用");
                        action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ShelfOrLocationDisabled", Code);
                        return action;
                    }
                    if (whUnit.Shelf.IsLightShelf || whUnit.Location.IS_SINGLE == "Y")
                    {
                        var locationData = MainDB.Queryable<WMS_ITEM>().Where(q => q.LOCATION_ID == whUnit.LOCATION_ID && q.AUTH_ORG == OrgCode).First();
                        if (!locationData.IsNullOrEmpty())
                        {
                            action.IsSuccessed = false;
                            //action.LocaleMsg = Biz.L("储位[{0}]已存有物料[{1}],请检查系统库存信息");
                            action.LocaleMsg = Biz.L($"WMS.Default.ScanShelf.ItemAlreadyExistsInLocation", Code, locationData.SN);
                            return action;
                        }
                    }
                    CurScanShelf.Shelf = whUnit.Shelf;
                    CurScanShelf.Location = whUnit.Location;
                    CurScanShelf.WarehouseCode = whUnit.WH_CODE;
                    CurScanShelf.RegionCode = whUnit.REGION_CODE;
                    CurScanShelf.ShelfCode = whUnit .SHELF_CODE;
                    CurScanShelf.LocationCode = whUnit.LOCATION_CODE;
                    CurScanShelf.IsSmartRack = false;
                }
                else
                {
                    action.IsSuccessed = false;
                    //action.LocaleMsg = Biz.L("请输入或扫描有效的货架/储位码");
                    action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ShelfCanNotEmpty");
                    return action;
                }
 
                CurScanShelf.IsScanShelf = true;
                //action.LocaleMsg = Biz.L("扫描货架/储位[{0}]成功");
                action.LocaleMsg = Biz.L("WMS.Default.ScanShelf.ScanSucceeded", Code);
                action.Data.Data = CurScanShelf;
            }
            catch (Exception ex)
            {
                //取消当前操作
                ResetTrans();
                //action.CatchExceptionWithLog(ex, $"扫描货架/储位[{0}]异常");
                action.CatchExceptionWithLog(ex, Biz.L("WMS.Default.ScanShelf.ScanException", Code));
            }
            return action;
        }
 
        /// <summary>
        /// 完成所有处理后使用事务保存数据
        /// </summary>
        /// <param name="action"></param>
        /// <param name="locale"></param>
        /// <param name="doAfterSave"></param>
        /// <returns></returns>
        public ApiAction<ScanOutput> DoIfFinish(ApiAction<ScanOutput> action, string locale, Action doAfterSave = null)
        {
            //保存数据库
            SaveCommitListToDB();
 
            //保存数据成功后执行
            doAfterSave?.Invoke();
 
            // 返回数据
            action.Data.Data = new DefaultInStoreOutput
            {
                Barcode = CurInvItem.SN,
                MaterialCode = CurInvItem.ItemInfo.ITEM_CODE,
                MaterialName = CurInvItem.ItemInfo.ITEM_NAME,
                CurrentQty = CurInvItem.Items[0].QTY,
                Unit = CurInvItem.Items[0].UNIT,
                DateCode = CurInvItem.Items[0].PROD_DATE,
                WarehouseCode = CurInvItem.Warehouse.WH_CODE,
                RegionCode = CurInvItem.Region.REGION_CODE,
                ShelfCode = CurInvItem.Shelf.SHELF_CODE,
                LocationCode = CurInvItem.Location.LOCATION_CODE,
            };
 
            //action.LocaleMsg = Biz.L($"扫描条码[{0}]上架到储位[{1}]成功");
            action.LocaleMsg = Biz.L("WMS.Default.ScanItem.PutOnSucceeded", CurInvItem.SN, CurInvItem.Location.LOCATION_CODE);
 
            //重置工序
            ResetScan();
            return SetOutPutMqttMsg(action, locale);
        }
 
        /// <summary>
        ///  获取需要上架的收货单列表(带分页)
        /// </summary>
        /// <returns></returns>
        public async Task<ApiAction<PageAble<BIZ_U9_RECEIPT>>> GetOrderList(BaseInputWithPage input)
        {
            var action = new ApiAction<PageAble<BIZ_U9_RECEIPT>>();
            var query = Biz.DataSource["YadaU9C"].Client.Queryable<mes_ReturnedDocInfo>().Where(q => q.Status == 3)
                                    .WhereIF(!input.OrderNo.IsNullOrEmpty(), q => q.DocNo.Contains(input.OrderNo) || SqlFunc.Subqueryable<mes_AsnDocInfo>().Where(a => a.AsnId == q.SrcASNDocId && q.DocNo.Contains(input.OrderNo)).Any())
                                    .OrderBy(q => q.DocNo).Select(q => new { q.DocNo, q.Status, q.StatusName }).Distinct().ToList();
 
            action.Data = await MainDB.Queryable<BIZ_U9_RECEIPT>().Where(q => query.Select(o => o.DocNo).Contains(q.ORDER_NO)).ToPageAsync(input.pageIndex, input.pageSize);
            foreach (var order in action.Data.data)
            {
                order.ERP_STATUS_CODE = query.FirstOrDefault(q => q.DocNo == order.ORDER_NO)?.Status.ToString();
                order.ERP_STATUS_NAME = query.FirstOrDefault(q => q.DocNo == order.ORDER_NO)?.StatusName;
            }
            MainDB.Updateable(action.Data.data, UserCode).UpdateColumns(q => new { q.UPDATE_TIME, q.UPDATE_USER, q.ERP_STATUS_CODE, q.ERP_STATUS_NAME }).ExecuteCommand();
            return action;
        }
 
        /// <summary>
        ///  获取当前收货单信息
        /// </summary>
        /// <returns></returns>
        public async Task<ApiAction<BIZ_U9_RECEIPT>> GetCurOrder()
        {
            var action = new ApiAction<BIZ_U9_RECEIPT>();
            action.Data = CurOrder;
            return action;
        }
 
        #endregion
 
        /// <summary>
        /// 重置事务数据,有需要则重写此方法
        /// </summary>
        public override void ResetTrans()
        {
            ResetScan();
            CurScanShelf = null;
            base.ResetTrans();
        }
 
        /// <summary>
        /// 重置本次扫码信息
        /// </summary>
        public override void ResetScan()
        {
            Command = "Normal";
            CurInvItem = null;
            base.ResetScan();
        }
 
        public override bool Close(bool needSaveHistoryLog = false)
        {
            this.IsFinished = true;
            return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
        }
 
    }//endClass
}