服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2025-03-04 990807a09cf35a73b20e213a4df82acddec40f4b
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
using Rhea.Common;
using Microsoft.AspNetCore.Http;
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 Apache.NMS;
using System.Drawing.Drawing2D;
using Tiger.Model.Sharetronic.Shelf;
using Tiger.IBusiness;
using Tiger.Model.MES.Yada;
using Apache.NMS.ActiveMQ.Commands;
using System.Diagnostics;
using static Microsoft.CodeAnalysis.CSharp.SyntaxTokenParser;
using Tiger.Model.Entitys.MES.U9C;
using Tiger.Model.Entitys.MES.BizPrintInstoreDoc;
 
namespace Tiger.Business.WMS.Transaction
{
    /// <summary>
    /// 半成品入库打印标签事务
    /// </summary>
    public class In_SemiProd : WMSTransactionBase, IIn_SemiProd
    {
        private readonly IMES_U9C _IMES_U9C = DI.Resolve<IMES_U9C>();
        public IIn_SemiProd Init(string id, string apiHost, InstoreDocInput input)
        {
            TransID = id;
            UserCode = input.UserCode;
            ApiHost = apiHost;
            OrgCode = input.OrgCode;
            pageSize = input.pageSize;
            _GetScannedList();
            Logger.Console.Info($"Start {this.GetType().Name} Transaction[ID: {TransID}]");
            return this;
        }
 
        #region Propertys & Variables
        public string UserCode { get; set; }
        public long UserId { get; set; }
        public string OrgCode { get; set; }
        public List<V_WMS_ITEM> Vitem { get; set; } = new(); 
        public int pageSize { get; set; }
        public RcvRptDocCreateInput RcvRptInput { get; set; } = new RcvRptDocCreateInput();
        public List<ProdInBatch> ProdInBatchs { get; set; } = new List<ProdInBatch>();
        public List<BIZ_ERP_PROD_IN_BTH> ErpProdInBths { get; set; } = new List<BIZ_ERP_PROD_IN_BTH>();
        public List<BIZ_ERP_PROD_IN> ErpProdIns { get; set; } = new List<BIZ_ERP_PROD_IN>();
        /// <summary>
        /// 扫描列表 
        /// </summary>
        public List<InStoreScanInfo> InStoreScanInfos { get; set; } = new List<InStoreScanInfo>();
        public List<BIZ_ERP_PROD_IN_SN> ErpProdInSns { get; set; } = new List<BIZ_ERP_PROD_IN_SN>();
        public InStoreInfos inStoreInfos { get; set; }
 
        #endregion
 
        #region Functions
        /// <summary>
        /// 扫描入口
        /// </summary>
        public async Task<ApiAction<ScanOutput>> Submit(BaseInput input)
        {
            var action = new ApiAction<ScanOutput>(new ScanOutput());
            try
            {
                if (input.SN.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.InSemiProd.ScanItem.SnEmptyFailure");
                    return SetOutPutMqttMsg(action, input.Locale);
                }
                var wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(q => q.ORDER_NO == input.SN).IncludesAllFirstLayer().IncludesAllSecondLayer(q => q._ItemInfos).FirstAsync();
                if (wo == null)
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.InSemiProd.ScanItem.WoEmptyFailure", input.SN); // $"工单[{input.SN}]不存在"
                    return action;
                }
 
                var semiTemplateQty = (input.Data ?? "").JsonToObject<SemiTemplateQty>() ?? new SemiTemplateQty();
 
                InStoreInfo Info = new()
                {
                    ErpProdInBth = new()
                    {
                        WORK_ORDER = wo.ORDER_NO,
                        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 = semiTemplateQty.PackQty,
                        CompleteQty = semiTemplateQty.CompleteQty,
                        IS_HANDLED = "N" //Y-已生成,N-未生成,F-失败
                    },
                    InStoreScanInfo = new()
                    {
                        SN = "",
                        SALES_ORDER = wo.SALES_ORDER,
                        WORK_ORDER = wo.ORDER_NO,
                        BATCH_NO = "",
                        SCAN_QTY = semiTemplateQty.PackQty.ToDouble()
                    }
                };
 
                //要存入的批次入库单数据 
                int n = InStoreScanInfos.Where(q => q.WORK_ORDER == Info.ErpProdInBth.WORK_ORDER).Count();
                if (!ErpProdInBths.Any(q => q.WORK_ORDER == Info.ErpProdInBth.WORK_ORDER))
                {
                    Info.ErpProdInBth.AUTH_ORG = OrgCode;
                    Info.ErpProdInBth.BATCH_NO = "";
                    Info.ErpProdInBth.CREATE_USER = UserCode;
                    Info.ErpProdInBth.CREATE_TIME = DateTime.Now;
                    Info.ErpProdInBth.SCANED_DATE = DateTime.Now;
                    Info.ErpProdInBth.HANDLED_DATE = DateTime.Now;
                    Info.ErpProdInBth.IS_SCANED = "Y";
                    Info.ErpProdInBth.ORDER_NO = UserCode;
                    Info.ErpProdInBth.IS_HANDLED = "N";
                    Info.ErpProdInBth.CartonQty = n == 0 ? 1 : n;
                    BIZ_ERP_PROD_IN prodIn = new()
                    {
                        AUTH_ORG = OrgCode,
                        BILLCODE = UserCode,
                        BILLDATE = DateTime.Now,
                        STATUS = 0,
                        HANDLED = -1,
                        WAREHOUSECODE = "",
                        SOURCECODE = Info.ErpProdInBth.WORK_ORDER,
                        HANDLED_DATE = DateTime.Now,
                    };
                    ErpProdInBths.Add(Info.ErpProdInBth);
                    ErpProdIns.Add(prodIn);
                }
                //else
                //{
                //    if (!InStoreScanInfos.Any(q => q.WORK_ORDER == Info?.ErpProdInBth.WORK_ORDER && q.SN == CurSN))
                //    {
                //        var erpbth = ErpProdInBths.Where(q => q.WORK_ORDER == Info.ErpProdInBth.WORK_ORDER).FirstOrDefault();
                //        if (!erpbth.IsNullOrEmpty())
                //        {
                //            erpbth.SCAN_QTY += Info.InStoreScanInfo.SCAN_QTY.ToDouble();
                //            erpbth.CartonQty = n + 1;
                //        }
                //    }
                //}
 
                //生成打印实体
                if (RcvRptInput.PrintJsons.Count == 0)
                {
                    RcvRptInput.PrintJsons.Add(new()
                    {
                        ID = Guid.NewGuid().ToString("N"),
                        Items = new() {
                            Info.ErpProdInBth
                        }
                    });
                }
                else
                {
                    //拼分页
                    bool isAdded = false;
                    foreach (var item in RcvRptInput.PrintJsons)
                    {
                        if (!item.Items.Any(q => q.WORK_ORDER == Info.ErpProdInBth.WORK_ORDER))
                        {
                            if (item.Items.Count < pageSize)
                            {
                                item.Items.Add(Info.ErpProdInBth);
                                isAdded = true;
                            }
                        }
                        else
                        {
                            isAdded = true;
                        }
                    }
                    if (!isAdded)
                    {
                        RcvRptInput.PrintJsons.Add(new()
                        {
                            ID = Guid.NewGuid().ToString("N"),
                            Items = new() {
                            Info.ErpProdInBth
                        }
                        });
                    }
                }
 
                //保存数据库
                var db = Business.Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    var x = db.Storageable(ErpProdInBths, $"InStoreOrderNo_{UserCode}")
                             .WhereColumns(t => new { t.ORDER_NO, t.WORK_ORDER, t.GHOST_ROW })
                             .ToStorage();
                    x.AsInsertable.ExecuteCommand();
                    x.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
 
                    //var s = db.Storageable(ErpProdInSns, $"InStoreOrderNo_{UserCode}")
                    //            .WhereColumns(t => new { t.CARTONNO, t.SN, t.GHOST_ROW })
                    //            .ToStorage();
                    //s.AsInsertable.ExecuteCommand();
                    //s.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
                    this.Close(!dbTran.IsSuccess);
                    throw dbTran.ErrorException;
                }
                RcvRptInput.RcvRptDocBases = new();
                RcvRptInput.labels = new();
                RcvRptInput.ErpProdInBths = ErpProdInBths;
                RcvRptInput.userId = UserCode;
                inStoreInfos = new()
                {
                    ErpProdInBths = ErpProdInBths,
                    InStoreScanInfos = InStoreScanInfos,
                };
 
                action.Data.Data = inStoreInfos;
                action.Data.Command = "";
                action.LocaleMsg = Biz.L("WMS.InSemiProd.ScanItem.ScanSuccessed", input.SN); //$"工单条码[{input.SN}]扫描成功,请填入工单数量后点提交"
            }
            catch (Exception ex)
            {
                //action.CatchExceptionWithLog(ex, $"扫描[{input.SN}]异常");
                action.CatchExceptionWithLog(ex, Biz.L("WMS.RePrint.Scan.ScanException", input.SN));
            }
            return SetOutPutMqttMsg(action, input.Locale);
        }
 
        public ApiAction<InStoreInfos> GetInStoreInfos()
        {
            return new() { Data = inStoreInfos };
        }
 
 
        /// <summary>
        /// 查询已扫描列表
        /// </summary>
        /// <returns></returns>
        public async Task<ApiAction<InStoreInfos>> GetScannedList()
        {
            var action = new ApiAction<InStoreInfos>();
            try
            {
                _GetScannedList();
                action.Data = inStoreInfos;
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"查询已扫描列表异常");
            }
            return action;
        }
 
        /// <summary>
        /// 从数据库读回操作者原本操作的记录
        /// </summary>
        /// <returns></returns>
        private RcvRptDocCreateInput _GetScannedList()
        {
            RcvRptInput.RcvRptDocBases = new();
            RcvRptInput.ErpProdInBths = new();
            RcvRptInput.PrintJsons = new();
            RcvRptInput.labels = new();
            RcvRptInput.userId = UserCode;
            ErpProdInSns.Clear();
            ErpProdIns.Clear();
            ErpProdInBths.Clear();
            InStoreScanInfos.Clear();
            RcvRptInput.ErpProdInBths = Biz.Db.Queryable<BIZ_ERP_PROD_IN_BTH>()
                .Where(q => q.BATCH_NO == SqlFunc.Subqueryable<BIZ_ERP_PROD_IN_BTH>().Where(s => s.ORDER_NO == UserCode && s.GHOST_ROW == false).Select(s => s.BATCH_NO) && q.IS_HANDLED == "N")
                .IncludesAllFirstLayer().ToList();
            foreach (var item in RcvRptInput.ErpProdInBths)
            {
                var values = item.ProdInSns.GroupBy(x => x.CARTONNO)
                    .Select(g => new InStoreScanInfo() { SN = g.Key, SALES_ORDER = item.SALES_ORDER, BATCH_NO = item.BATCH_NO, WORK_ORDER = item.WORK_ORDER, SCAN_QTY = g.Sum(x => x.Qty).ToDouble() });
                foreach (var t in values)
                {
                    InStoreScanInfos.Add(t);
                }
                item.CartonQty = values.Count();
 
                ErpProdIns.Add(new()
                {
                    AUTH_ORG = OrgCode,
                    BILLCODE = UserCode,
                    BILLDATE = DateTime.Now,
                    STATUS = 0,
                    HANDLED = -1,
                    WAREHOUSECODE = "",
                    SOURCECODE = item.WORK_ORDER,
                    HANDLED_DATE = DateTime.Now,
                });
                ErpProdInSns.AddRange(item.ProdInSns);
 
                //生成打印实体
                if (RcvRptInput.PrintJsons.Count == 0)
                {
                    RcvRptInput.PrintJsons.Add(new()
                    {
                        ID = Guid.NewGuid().ToString("N"),
                        Items = new() {
                            item
                        }
                    });
                }
                else
                {
                    //拼分页
                    bool isAdded = false;
                    foreach (var p in RcvRptInput.PrintJsons)
                    {
                        if (!p.Items.Any(q => q.WORK_ORDER == item.WORK_ORDER))
                        {
                            if (p.Items.Count < pageSize)
                            {
                                p.Items.Add(item);
                                isAdded = true;
                            }
                        }
                        else
                        {
                            isAdded = true;
                        }
                    }
                    if (!isAdded)
                    {
                        RcvRptInput.PrintJsons.Add(new()
                        {
                            ID = Guid.NewGuid().ToString("N"),
                            Items = new() {
                            item
                        }
                        });
                    }
                }
            }
            ErpProdInBths = RcvRptInput.ErpProdInBths;
 
            inStoreInfos = new()
            {
                ErpProdInBths = ErpProdInBths,
                InStoreScanInfos = InStoreScanInfos,
            };
            return RcvRptInput;
        }
 
        /// <summary>
        /// 调用U9C接口生成入库单
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<ApiAction<RcvRptDocCreateInput>> GenerateInStoreOrder()
        {
            var action = new ApiAction<RcvRptDocCreateInput>();
            try
            {
                //如果扫描记录为0
                if (!RcvRptInput.ErpProdInBths.Any(q => q.IS_HANDLED == "N"))
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L($"没有要入库的产品数据,请扫描工单重新生成入库单据!");
                    return action;
                }
                if (RcvRptInput.RcvRptDocBases.IsNullOrEmpty() || RcvRptInput.RcvRptDocBases.Count == 0)
                {
                    foreach (var item in RcvRptInput.ErpProdInBths)
                    {
                        var DocBase = new RcvRptDocBase()
                        {
                            WorkOrder = item.WORK_ORDER,
                            ItemCode = item.ITEM_CODE,
                            WhCode = "10105",
                            PkgQty = item.SCAN_QTY,
                            CompleteQty = item.CompleteQty,
                            OutputType = 0,
                            StorageType = 4,
                            DocState = 1,
                            Status = "N"
                        };
                        if (!RcvRptInput.RcvRptDocBases.Any(q => q.WorkOrder == item.WORK_ORDER))
                        {
                            RcvRptInput.RcvRptDocBases.Add(DocBase);
                        }
                    }
                }
                else
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L($"有存在未提交的入库单,不能生成入库单");
                    return action;
                }
                action = await _IMES_U9C.RcvRptDocCreate(RcvRptInput);
                if (action.IsSuccessed)
                {
                    foreach (var sn in ErpProdInSns)
                    {
                        var d = action.Data.ErpProdInBths.Where(q => q.WORK_ORDER == sn.SOURCECODE).FirstOrDefault();
                        if (!d.IsNullOrEmpty())
                        {
                            sn.BUSINESSCODE = d.ORDER_NO;
                            sn.UPDATE_USER = UserCode;
                            sn.UPDATE_TIME = DateTime.Now;
                        }
                    }
                    foreach (var p in ErpProdIns)
                    {
                        var d = action.Data.ErpProdInBths.Where(q => q.WORK_ORDER == p.SOURCECODE).FirstOrDefault();
                        if (!d.IsNullOrEmpty())
                        {
                            p.BILLCODE = d.ORDER_NO;
                            p.UPDATE_USER = UserCode;
                            p.UPDATE_TIME = DateTime.Now;
                            p.HANDLED = 0;
                        }
                    }
                    //保存数据库
                    var db = Business.Biz.Db;
                    var dbTran = db.UseTran(() =>
                    {
                        db.Updateable(ErpProdInSns, $"InStoreOrderNo_{UserCode}").UpdateColumns(q => new { q.UPDATE_TIME, q.UPDATE_USER, q.BUSINESSCODE }).ExecuteCommand();
 
                        var x = db.Storageable(ErpProdIns, $"InStoreOrderNo_{UserCode}")
                                 .WhereColumns(t => new { t.BILLCODE, t.SOURCECODE, t.GHOST_ROW })
                                 .ToStorage();
                        x.AsInsertable.ExecuteCommand();
                        x.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                    });
                    if (!dbTran.IsSuccess)
                    {
                        Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
                        this.Close(!dbTran.IsSuccess);
                        throw dbTran.ErrorException;
                    }
                }
 
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"调用U9C接口生成入库单异常");
            }
            return action;
        }
 
        /// <summary>
        /// 清除
        /// </summary>
        /// <returns></returns>
        public async Task<ApiAction> Clear()
        {
            var action = new ApiAction();
            try
            {
                RcvRptInput = new();
                ProdInBatchs = new() { };
                ErpProdInBths = new() { };
                ErpProdIns = new();
                /// <summary>
                /// 扫描列表 
                /// </summary>
                InStoreScanInfos = new() { };
                ErpProdInSns = new();
                inStoreInfos = new() { };
 
                //保存数据库
                var db = Business.Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    db.Deleteable<BIZ_ERP_PROD_IN_BTH>().Where(x => x.ORDER_NO == UserCode && x.IS_HANDLED == "N").ExecuteCommand();
                    db.Deleteable<BIZ_ERP_PROD_IN_SN>().Where(x => x.BUSINESSCODE == UserCode).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
                    this.Close(!dbTran.IsSuccess);
                    throw dbTran.ErrorException;
                }
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"查询已扫描列表异常");
            }
            return action;
        }
 
        #endregion
 
        public override bool Close(bool needSaveHistoryLog = false)
        {
            needSaveHistoryLog = true;
            //保存操作日志
 
            this.IsFinished = true;
            return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
        }
 
    }//endClass
}