服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2025-02-21 09afa6099b919a3a33cf4e1796dc5f6e66b4993b
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
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;
 
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 userCode, string apiHost, string orgCode)
        {
            TransID = id;
            UserCode = userCode;
            ApiHost = apiHost;
            OrgCode = orgCode;
            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>> Scan(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;
                }
                
                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 = input.Data.ToInt32(),
                        IS_HANDLED = "N" //Y-已生成,N-未生成,F-失败
                    },
                    InStoreScanInfo = new()
                    {
                        SN = "",
                        SALES_ORDER = wo.SALES_ORDER,
                        WORK_ORDER = wo.ORDER_NO,
                        BATCH_NO = "",
                        SCAN_QTY = input.Data.ToInt32()
                    }
                };
 
                //要存入的批次入库单数据 
                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.LocaleMsg = Biz.L("WMS.InSemiProd.ScanItem.ScanSuccessed"); //$"工单条码[{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 async Task<ApiAction<ScanOutput>> ScanItem(BaseInput input)
        {
            var action = new ApiAction<ScanOutput>(new ScanOutput());
            try
            {
                
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, Biz.L("半成品入库失败"));
            }
            return action;
        }
 
 
        /// <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.ToInt32(),
                            CompleteQty = item.SCAN_QTY.ToInt32(),
                            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;
        }
 
        #endregion
 
        public override bool Close(bool needSaveHistoryLog = false)
        {
            needSaveHistoryLog = true;
            //保存操作日志
 
            this.IsFinished = true;
            return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
        }
 
    }//endClass
}