服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-12-19 eb0ad83719de660e5c4f7676aea4710625b6bd51
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
using Newtonsoft.Json;
using Rhea.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tiger.Business.SqlSugarHepler;
using Tiger.IBusiness;
using Tiger.Model;
using Tiger.Model.Entitys.WMS.DTOS;
 
namespace Tiger.Business.WMS.Transaction
{
    public class Resell : WMSTransactionBase, IResell
    {
        /// <summary>
        /// T100接口URL
        /// </summary>
        public string t100Url { get; set; } = @"http://172.18.8.11/wstopprd/ws/r/awsp920";
        /// <summary>
        /// 事务初始化函数
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userCode"></param>
        /// <param name="apiHost"></param>
        /// <param name="orgCode"></param>
        /// <returns></returns>
        public IResell 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 BillCode { get; set; }
        public string TARGET_ORG { get; set; }
        public string UserCode { get; set; }
        public long UserId { get; set; }
        public string RackCode { get; set; }
        public string WarehouseCode { get; set; }
        public string WarehouseName { get; set; }
        public string OrgCode { get; set; }
        public WMS_SHELF Shelf { get; set; }
 
        public ResellPuton ResellPuton1 { get; set; }
        public BIZ_ERP_SALE_OUT res { get; set; }
        #endregion
 
        /// <summary>
        /// 获取转卖单列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ApiAction<PageAble<BIZ_ERP_SALE_OUT>> GetResellHeaders(int pageIndex, int pageSize, string orgcode, string billcode)
        {
            var action = new ApiAction<PageAble<BIZ_ERP_SALE_OUT>>();
            var query = Biz.Db.Queryable<BIZ_ERP_SALE_OUT>().Where(x =>
            (x.STATUS == BIZ_ERP_SALE_OUT.STATUSs.INIT.GetValue()
            || x.STATUS == BIZ_ERP_SALE_OUT.STATUSs.WORKING.GetValue())
            && x.AUTH_ORG == orgcode)
                .WhereIF(!billcode.IsNullOrEmpty(), x => x.BILLCODE.Contains(billcode))
                .OrderByDescending(x => x.UPDATE_TIME.ToString("yyyy-MM-dd"))
                .OrderBy(x => x.CREATE_TIME)
                .ToPage(pageIndex, pageSize);
            action.Data = query;
            return action;
        }
        /// <summary>
        /// 转卖单选择事件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction<Resell_Input>> SelectOrder(ResellInput input)
        {
            var action = new ApiAction<Resell_Input>();
            try
            {
                //SelectOrderOption = input.AuthOption;
                res = await Biz.Db.Queryable<BIZ_ERP_SALE_OUT>().Where(x => x.BILLCODE == input.ResellNo).Includes(q => q.DetailsWithGhost, d => d.ItemInfo).IncludesAllFirstLayer().FirstAsync();
                //验证明细是否正确
                if (!res.Details.Any())
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = new("WMS.ProdMReq.SelectOrder.DtlsException", input.ResellNo);
                    return action;
                }
                BillCode = input.ResellNo;//单号
                TARGET_ORG = res.TARGET_ORG;//目标据点
                //OrgCode//原据点
                //WareHouseCode//扫描仓库
                action.LocaleMsg = Biz.L("选择多角转卖单成功");
                action.Data = new Resell_Input
                {
                    BILLCODE = BillCode,//单据
                    WAREHOUSECODE = "",//原仓库
                    TARGET_WAREHOUSECODE = "",//目标仓库
                    AUTH_ORG = "",//原据点
                    TARGET_ORG = "",//目标据点
                };
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"多角转卖单选择异常");
            }
            return action;
        }
 
        /// <summary>
        /// 扫描物料并复核,如果物料已经完成移库则货架上亮灯提醒储位
        /// </summary>
        public async Task<ApiAction> ScanItem(ResellModel input)
        {
            var action = new ApiAction();
            try
            {
                if (string.IsNullOrEmpty(input.SN))
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.SnEmptyFailure"); // $"条码不能为空!";
                    return action;
                }
                //货架为空则扫描的认为是货架
                if (string.IsNullOrEmpty(RackCode))
                {
                    var _action = await ScanShelf(input.SN);
                    action.LocaleMsg = _action.LocaleMsg;
                    action.IsSuccessed = _action.IsSuccessed;
                    if (!action.IsSuccessed)
                    {
                        action.Data = _action.Data;
                        return action;
                    }
                    else
                    {
                        //调用T100接口传入WarehouseCode,修改业务表信息
                        var To100param = new Resell_Input 
                        {
                            BILLCODE=BillCode
                        };
 
                        var query = MaterialToErp(To100param);
                        //调用t100或修改业务表时发生错误
                        if(query.IsSuccessed == false)
                        {
                            action.IsSuccessed = false;
                            action.LocaleMsg = query.LocaleMsg;
                            return action;
                        }
                        //返回信息
                        action.Data = new Resell_Input
                        {
                          BILLCODE=BillCode,//单据
                          WAREHOUSECODE= RackCode,//原仓库
                          TARGET_WAREHOUSECODE="",//目标仓库
                          AUTH_ORG=OrgCode,//原据点
                          TARGET_ORG=TARGET_ORG,//目标据点
                        };
                    }
                    
                }
                else
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("该单已操作,请退出重新选单!");
                    return action;
                }
            }
            catch (Exception ex)
            {
                //取消当前操作
 
                action.CatchExceptionWithLog(ex, $"扫描物料[{input.SN}]复核异常");
            }
            return action;
        }
 
        /// <summary>
        /// 扫描货架或者储位
        /// </summary>
        public async Task<ApiAction<ResellPuton>> ScanShelf(string Code)
        {
            var action = new ApiAction<ResellPuton>();
            try
            {
                if (Code.IsNullOrEmpty())
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.ShelfCanNotEmpty");
                    return action;
                }
 
                ResellPuton1 = new ResellPuton();
 
                // 查询货架信息
                var whUnit = await Biz.Db.Queryable<V_WH_UNIT>().Where(t => t.WH_CODE.ToUpper() == Code.ToUpper() && t.AUTH_ORG == OrgCode).IncludesAllFirstLayer().FirstAsync();
 
                // 扫描货架代码,且为智能货架
                if (whUnit != null && whUnit.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue())
                {
                    ResellPuton1.WarehouseCode = whUnit.WH_CODE;
                    ResellPuton1.RackCode = whUnit.SHELF_CODE;
                    ResellPuton1.ShelfType = whUnit.SHELF_TYPE;
                    ResellPuton1.IsSmartRack = true;
                    Shelf = whUnit.Shelf;
 
                }
                // 扫描库位代码
                else
                {
                    var nWh_code = await Biz.Db.Queryable<V_WH_UNIT>().Where(t => t.WH_CODE.ToUpper() == Code.ToUpper() && t.AUTH_ORG == OrgCode).FirstAsync();
                    if (nWh_code == null)
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.NotExist", Code);
                        return action;
                    }
 
                    if (nWh_code.IS_ACTIVE == "N")
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = Biz.L("WMS.CustSupChk.ScanItem.ShelfOrLocationDisabled");
                        return action;
                    }
                    //if (nWh_code.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.Smart.GetValue() || nWh_code.SHELF_TYPE == WMS_SHELF.SHELF_TYPEs.QRCode.GetValue())
                    //{
                    //    var locationData = Biz.Db.Queryable<WMS_ITEM>().Where(q => q.LOCATION_ID == nWh_code.LOCATION_ID && q.AUTH_ORG == OrgCode).First();
                    //    if (!locationData.IsNullOrEmpty())
                    //    {
                    //        action.IsSuccessed = false;
                    //        action.LocaleMsg = Biz.L($"系统当前库位存在料盘,请检查系统库存信息");
                    //        return action;
 
                    //    }
                    //}
 
                    ResellPuton1.LocationCode = nWh_code.LOCATION_CODE;
                    ResellPuton1.WarehouseCode = nWh_code.WH_CODE;
                    //CustomerSupplyPuton.Capacity = nLocation.MAXSIZE - nUsedCapacity;
                    //CustomerSupplyPuton.LotCount = nLocation.LOTCOUNT;
                    //CustomerSupplyPuton.MaxSize = nLocation.MAXSIZE;
                    ResellPuton1.IsSmartRack = false;
                    ResellPuton1.RackCode = nWh_code.SHELF_CODE;
                }
                WarehouseCode = ResellPuton1.WarehouseCode;
                RackCode = ResellPuton1.RackCode;
                ResellPuton1.IsScanShelf = true;
                action.LocaleMsg = Biz.L("扫描成功");
                action.Data = ResellPuton1;
            }
            catch (Exception ex)
            {
                //取消当前操作
 
                action.CatchExceptionWithLog(ex, $"扫描货架或者储位[{Code}]异常");
            }
            return action;
        }
        /// <summary>
        /// 领料单过账到T100
        /// </summary>
        /// <param name="param">单号</param>
        /// <returns></returns>
        public ApiAction MaterialToErp(Resell_Input param)
        {
            var action = new ApiAction();
            List<Tot100pamar> dtl = new List<Tot100pamar>();
            //foreach (var item in res.Details)
            {
                var t100 = new Tot100pamar()
                {
                    l_seq="1",
                    imaxuc005=OrgCode,
                    imaxuc006=WarehouseCode,
                    imaxuc002=TARGET_ORG,
                    imaxuc003="",
                };
                dtl.Add(t100);
            }
 
            //Work.DoAsync(() =>
            {
                Thread.Sleep(3000);
                var authOrg = GetAuthOrg(param.BILLCODE);
 
                #region 创建请求实体
 
                var model = new BodyDTO<PayLoad<Std_Data<Tot100>>>()
                {
                    host = new HostDTO(),
                    datakey = new DataKey()
                    {
                        EntId = "88",
                        CompanyId = authOrg
                    },
                    service = new ServiceDTO()
                    {
                        name = "wms_get_cimm202"
                    },
                    payload = new PayLoad<Std_Data<Tot100>>()
                    {
                        std_data = new Std_Data<Tot100>()
                        {
                            parameter = new Tot100()
                            {
                                pamar = dtl
                            }
                        }
                    }
                };
 
                #endregion 创建请求实体
 
                #region 发起请求
 
                var result = new T100ActionResult<resultStd_inputdata>();
                try
                {
                    var requestJson = JsonConvert.SerializeObject(model);
                    var response = HttpHelper.PostAsync(t100Url, requestJson).Result;
                    result = JsonConvert.DeserializeObject<T100ActionResult<resultStd_inputdata>>(response.Message);
                    Logger.Scheduler.Info($"多角转卖对接T100过账提交Json: {requestJson},返回Json: {response.Message}");
                }
                catch (Exception ex)
                {
                    Logger.Default.Info($"多角转卖对接T100过账失败");
                    //return "0";
                }
 
                #endregion 发起请求
 
                var db = Biz.Db;
                var items = db.Queryable<WMS_ITEM>().Where(i => i.TRANS_NO == param.BILLCODE).ToList();
                var items_pkg = db.Queryable<WMS_ITEM_PKG>().Where(i => i.TRANS_NO == param.BILLCODE).ToList();
                var t100_res = result.payload.parameter?.returns;
                //var wh_id=db.Queryable<WMS_WAREHOUSE>().Where(q=>q.WH_CODE==WarehouseCode).ToString();
                List<WMS_ITEM_HIS> ItemHistorys = new List<WMS_ITEM_HIS>();
                if (result.payload.std_data?.execution.code == "0")
                {
                    if (items.Count > 0 && items_pkg.Count > 0)
                    {
 
                        foreach (var item in items)
                        {
                            item.AUTH_ORG = TARGET_ORG;
                            item.WH_ID = result.payload.parameter?.returns.FirstOrDefault().imaxuc003;
                            item.SOURCE_CODE = "BIZ_ERP_SALE_OUT";
 
 
                            WMS_ITEM_HIS his = new WMS_ITEM_HIS(item, $"多角转卖,销售单[{BillCode}][{string.Join(",", res.Details.Where(s => s.ITEM_CODE == item.ITEM_CODE).Select(q => q.BILLLINE))}]");
                            ItemHistorys.Add(his);
                        }
                        foreach (var item in items_pkg)
                        {
                            item.AUTH_ORG = TARGET_ORG;
                            item.WH_ID = result.payload.parameter?.returns.FirstOrDefault().imaxuc003;
                            item.SOURCE_CODE = "BIZ_ERP_SALE_OUT";
                        }
                        //业务表头
                        res.AUTH_ORG = TARGET_ORG;
                        res.WAREHOUSECODE = WarehouseCode;
                        res.STATUS = BIZ_ERP_SALE_OUT.STATUSs.COMPLETE.GetValue();
                        //业务表明细
                        foreach (var item in res.Details)
                        {
                            item.AUTH_ORG = TARGET_ORG;
                            item.WAREHOUSECODE = WarehouseCode;
                            item.LINESTATUS = BIZ_ERP_SALE_OUT.STATUSs.COMPLETE.GetValue();
                        }
                        //业务表条码
                        foreach (var item in res.SnList)
                        {
                            item.AUTH_ORG = TARGET_ORG;
                            //item.WAREHOUSECODE = WarehouseCode;
                            item.STATUS = WMS_ITEM.STATUSs.InStore.GetValue();
                        }
                    }
                    else
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = Biz.L($"库存信息不足");
                    }
 
                }
                else
                {
                    action.IsSuccessed = false;
                    action.LocaleMsg = Biz.L($"T100返回了错误[{result.payload.std_data?.execution.description}]");
                }
                //保存到数据库
                var db2 = Biz.Db;
                var dbTran = db2.UseTran(() =>
                {
                    // 插入单据表头
                    db2.Updateable(res, UserCode).ExecuteCommand();
                    var x = db2.Storageable(res.Details, UserCode).ToStorage();
                    x.AsInsertable.ExecuteCommand();//不存在插入
                    x.AsUpdateable.IgnoreColumns(z => new { z.CREATE_TIME, z.CREATE_USER }).ExecuteCommand();//存在更新,更新时忽略字段CreationTime、CreatorUserId
 
                    db2.Updateable(res.SnList, UserCode).ExecuteCommand();
 
                    db2.Updateable(items, UserCode).ExecuteCommand();
                    db2.Updateable(items_pkg, UserCode).ExecuteCommand();
                    db2.Insertable(ItemHistorys, UserCode).ExecuteCommand();
 
                });
                if (!dbTran.IsSuccess)
                {
                    Logger.Default.Fatal(dbTran.ErrorException, "Database transaction save exception");
                    this.Close(!dbTran.IsSuccess);
                    throw dbTran.ErrorException;
                }
            }
            //});
            return action;
        }
 
        /// <summary>
        /// 根据工单号获取据点名称
        /// </summary>
        /// <param name="workOrder"></param>
        /// <returns></returns>
        public string GetAuthOrg(string workOrder)
        {
            var authOrg = string.Empty;
            var org = workOrder.Substring(0, 2);
            switch (org)
            {
                #region 循环取值
 
                case "PH":
                    authOrg = "PHXC";
                    break;
 
                case "YX":
                    authOrg = "YXYY";
                    break;
 
                case "XP":
                    authOrg = "XCXP";
                    break;
 
                case "XN":
                    authOrg = "XCXN";
                    break;
 
                case "TH":
                    authOrg = "STTH";
                    break;
 
                case "XC":
                    authOrg = "XCSJ";
                    break;
 
                case "DG":
                    authOrg = "DGXC";
                    break;
 
                case "HK":
                    authOrg = "HKXC";
                    break;
 
                case "TX":
                    authOrg = "TXXC";
                    break;
 
                case "LR":
                    authOrg = "XCLR";
                    break;
 
                case "OM":
                    authOrg = "OMKJ";
                    break;
 
                case "AH":
                    authOrg = "AHXC";
                    break;
 
                case "YK":
                    authOrg = "XCYK";
                    break;
 
                case "CC":
                    authOrg = "XCCC";
                    break;
 
                case "TC":
                    authOrg = "XCTC";
                    break;
 
                case "TK":
                    authOrg = "STTK";
                    break;
 
                case "MM":
                    authOrg = "STMM";
                    break;
 
                default:
                    throw new Exception($"单号前两位:{org}未能找到对应的据点,请确认!");
 
                    #endregion 循环取值
            }
            return authOrg;
        }
 
        public override bool Close(bool needSaveHistoryLog = false)
        {
            //保存操作日志
 
            this.IsFinished = true;
            return IsFinished ? base.Close(needSaveHistoryLog) : IsFinished;
        }
    }
}