服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2025-02-22 f2e2c131b1db5ac84e214f2b6dd1a27b816a41cd
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
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.Minsun;
using Org.BouncyCastle.Asn1.Tsp;
using Tiger.Model.Entitys.MES.Position;
using Apache.NMS.ActiveMQ.Commands;
using System.Security.Cryptography;
using System.Diagnostics;
using Tiger.Model.Entitys.MES.U9C;
using Newtonsoft.Json.Linq;
using System.Security.Policy;
using NetTaste;
 
namespace Tiger.Business.MES
{
    public partial class MES_U9C : IMES_U9C
    {
        public static string U9CAuthLoginUrl { get; set; } = ApiConfig.IsTestServer ? Cache.SysParam["U9CAuthLoginUrl_Test", "U9CUrl"].PARAM_VALUE : Cache.SysParam["U9CAuthLoginUrl", "U9CUrl"].PARAM_VALUE; // "http://172.16.80.20/U9C/webapi/OAuth2/AuthLogin?"; 测试
        //public static string U9CAuthLoginUrl { get; set; } = Cache.SysParam["U9CAuthLoginUrl", "U9CUrl"].PARAM_VALUE; //正式
        public static string secret = "&entcode=02&orgcode=101&clientid=mes&clientsecret=12c8ee7e9bb74ad2a6a0fb3315c8af20";
        public static string RcvRptDocCreateUrl
        { get; set; } = ApiConfig.IsTestServer ? Cache.SysParam["RcvRptDocCreateUrl_Test", "U9CUrl"].PARAM_VALUE : Cache.SysParam["RcvRptDocCreateUrl", "U9CUrl"].PARAM_VALUE;  //"http://172.16.80.20/u9C/webapi/RcvRptDoc/Create"; 测试
        //public static string RcvRptDocCreateUrl
        //{ get; set; } = Cache.SysParam["RcvRptDocCreateUrl", "U9CUrl"].PARAM_VALUE;  //正式 
 
        public static string U9CBarcodeCreateUrl
        { get; set; } = ApiConfig.IsTestServer ? Cache.SysParam["U9CBarcodeCreateUrl_Test", "U9CUrl"].PARAM_VALUE : Cache.SysParam["U9CBarcodeCreateUrl", "U9CUrl"].PARAM_VALUE;
 
        public static string CreateBarCodeByAssignQtyUrl
        { get; set; } = ApiConfig.IsTestServer ? Cache.SysParam["CreateBarCodeByAssignQty_Test", "U9CUrl"].PARAM_VALUE : Cache.SysParam["CreateBarCodeByAssignQty", "U9CUrl"].PARAM_VALUE;
 
        /// <summary>
        /// 入库单接口
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction<RcvRptDocCreateInput>> RcvRptDocCreate(RcvRptDocCreateInput input)
        {
            Logger.Interface.Info($"/**\r\n *进入入库单接口....\r\n */\r\n"); //固定写法
            var action = new ApiAction<RcvRptDocCreateInput>();
            try
            {
                //保存成功,调用U9C接口-测试用
                //int i = 1;
                //foreach (var item in input.RcvRptDocBases.Where(q => q.Status != "Y"))
                //{
                //    var ErpProdInBth = input.ErpProdInBths.Where(q => q.WORK_ORDER == item.WorkOrder).FirstOrDefault();
                //    item.Status = "Y";
                //    if (!ErpProdInBth.IsNullOrEmpty())
                //    {
                //        ErpProdInBth.ORDER_NO = $"YDRKD24120000{5+i}";
                //        ErpProdInBth.IS_HANDLED = "Y";
                //        ErpProdInBth.HANDLED_DATE = DateTime.Now;
                //    }
                //    foreach (var p in input.PrintJsons)
                //    {
                //        var d = p.Items.Where(q => q.WORK_ORDER == item.WorkOrder).FirstOrDefault();
                //        if (!d.IsNullOrEmpty())
                //        {
                //            d.ORDER_NO = $"YDRKD24120000{5 + i}";
                //            d.IS_HANDLED = "Y";
                //        }
                //    }
                //    i++;
                //}
                //var _db = Biz.Db;
                //var _dbTran = _db.UseTran(() =>
                //{
                //    _db.Updateable(input.ErpProdInBths, "U9C_RcvRptDocCreate").UpdateColumns(x => new { x.ORDER_NO, x.REMARK, x.IS_HANDLED, x.HANDLED_DATE, x.UPDATE_USER, x.UPDATE_TIME }).ExecuteCommand();
                //});
                //if (!_dbTran.IsSuccess)
                //{
                //    action.CatchExceptionWithLog(_dbTran.ErrorException, $"数据处理失败");
                //    Logger.Interface.Error(action.Message);
                //    IsSuccessed = false;
                //}
 
                //保存成功,调用U9C接口
                var u9CLoginResult = await HttpHelper.GetAsync<U9CLoginResult>($"{U9CAuthLoginUrl}userCode={input.userId}{secret}");
                if (u9CLoginResult.Success)
                {
                    var token = u9CLoginResult.Data;
                    Logger.Interface.Info($"U9C登录token: {token}");
                    foreach (var item in input.RcvRptDocBases.Where(q => q.Status != "Y"))
                    {
                        List<RcvRptDocCreateParam> param = new() {
                                new() {
                                    CompleteList = new(){
                                       new CompleteList
                                       {
                                            MOKey = new MOKey { DocNo = item.WorkOrder },
                                            Wh = new Wh { Code = item.WhCode },
                                            DescFlexField = new DescFlexField { PrivateDescSeg1 = item.PkgQty.ToString() },
                                            Item = new Item { Code = item.ItemCode },
                                            CompleteQty = item.CompleteQty,
                                            OutputType = item.OutputType,
                                            StorageType = item.StorageType,
                                            DocState = item.DocState,
                                       }
                                    },
                                    Remark = "接口生成",
                                    BusinessDate = DateTime.Now,
                                }
                            };
                        var response = await HttpHelper.PostAsync(RcvRptDocCreateUrl, JsonConvert.SerializeObject(param), new Dictionary<string, string>() { { "token", token } });
                        var result = JsonConvert.DeserializeObject<U9CResult>(response.Message);
                        var ErpProdInBth = input.ErpProdInBths.Where(q => q.WORK_ORDER == item.WorkOrder).FirstOrDefault();
                        if (result != null)
                        {
                            if (result.Success && result.Data[0].m_isSucess)
                            {
                                Logger.Interface.Info($"生成入库单接口提交Json: {JsonConvert.SerializeObject(param)},返回Json: {response.Message}");
                                item.Status = "Y";
                                if (!ErpProdInBth.IsNullOrEmpty())
                                {
                                    ErpProdInBth.ORDER_NO = result.Data[0].m_code;
                                    ErpProdInBth.IS_HANDLED = "Y";
                                    ErpProdInBth.HANDLED_DATE = DateTime.Now;
                                }
                                foreach (var p in input.PrintJsons)
                                {
                                    var d = p.Items.Where(q => q.WORK_ORDER == item.WorkOrder && q.IS_HANDLED != "P").FirstOrDefault();
                                    if (!d.IsNullOrEmpty())
                                    {
                                        d.ORDER_NO = result.Data[0].m_code;
                                        d.IS_HANDLED = "Y";
                                    }
                                }
                                Logger.Interface.Info($"生成入库单成功");
 
                                //条码主档
                                BarcodeCreateInput barcodeCreateInput = new()
                                {
                                    RevDocId = result.Data[0].m_iD,
                                    userId = input.userId,
                                    token = token,
                                    IsLogin = false,
                                };
                                await U9CBarcodeCreate(barcodeCreateInput);
                            }
                            else
                            {
                                action.IsSuccessed = false;
                                action.LocaleMsg = new($"{action.Message}; {result.Data[0].m_errorMsg}");
                                item.Status = "F";
                                if (!ErpProdInBth.IsNullOrEmpty())
                                {
                                    ErpProdInBth.REMARK = result.Data[0].m_errorMsg;
                                    ErpProdInBth.IS_HANDLED = "F";
                                    ErpProdInBth.HANDLED_DATE = DateTime.Now;
                                }
                                foreach (var p in input.PrintJsons)
                                {
                                    var d = p.Items.Where(q => q.WORK_ORDER == item.WorkOrder && q.IS_HANDLED != "P").FirstOrDefault();
                                    if (!d.IsNullOrEmpty())
                                    {
                                        d.ORDER_NO = result.Data[0].m_code;
                                        d.IS_HANDLED = "F";
                                    }
                                }
                                Logger.Interface.Info($"生成入库单失败:{result.Data[0].m_errorMsg}");
                            }
                        }
                    }
                    var _db = Biz.Db;
                    var _dbTran = _db.UseTran(() =>
                    {
                        _db.Updateable(input.ErpProdInBths, "U9C_RcvRptDocCreate").UpdateColumns(x => new { x.ORDER_NO, x.REMARK, x.IS_HANDLED, x.HANDLED_DATE, x.UPDATE_USER, x.UPDATE_TIME }).ExecuteCommand();
                    });
                    if (!_dbTran.IsSuccess)
                    {
                        action.CatchExceptionWithLog(_dbTran.ErrorException, $"数据处理失败");
                        Logger.Interface.Error(action.Message);
                    }
                }
                input.labels = GetLabels(input.PrintJsons);
                action.Data = input;
            }
            catch (System.Exception ex)
            {
                action.CatchExceptionWithLog(ex, "生成入库单异常");
            }
            Logger.Interface.Info($"/* 入库单接口结束 */\r\n"); //固定写法
            return action;
        }
 
        /// <summary>
        /// 入库单条码主档接口
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction<BarcodeCreateInput>> U9CBarcodeCreate(BarcodeCreateInput input)
        {
            Logger.Interface.Info($"/**\r\n *进入入库单条码主档接口....\r\n */\r\n"); //固定写法
            var action = new ApiAction<BarcodeCreateInput>();
            try
            {
                if (input.IsLogin)
                {
                    var u9CLoginResult = await HttpHelper.GetAsync<U9CLoginResult>($"{U9CAuthLoginUrl}userCode={input.userId}{secret}");
                    if (u9CLoginResult.Success)
                    {
                        input.token = u9CLoginResult.Data;
                        Logger.Interface.Info($"U9C登录token: {input.token}");
                    }
                }
                List<BarcodeCreateParam> param = new() {
                     new() {
                     CreateEntityKeys = new(){
                                           new CreateEntityKeys
                                           {
                                                ID = input.RevDocId, //入库单ID
                                                EntityType="UFIDA.U9.Complete.RCVRpt.RcvRptDoc"
                                           }
                                        },
                                        IsCreateHeadBarCode = true,
                                        BarCodeRuleCode = "DJCPRK01",
                                        GenerateCount = 1,
                                        BCQty = 1,
                     }
                };
                var response = await HttpHelper.PostAsync(U9CBarcodeCreateUrl, JsonConvert.SerializeObject(param), new Dictionary<string, string>() { { "token", input.token } });
                var result = JsonConvert.DeserializeObject<U9CResult>(response.Message);
                if (result != null)
                {
                    if (result.Success && result.Data[0].m_isSucess)
                    {
                        Logger.Interface.Info($"生成入库单条码主档接口提交Json: {JsonConvert.SerializeObject(param)},返回Json: {response.Message}");
                    }
                    else
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = new($"{action.Message}; {result.Data[0].m_errorMsg}");
                        Logger.Interface.Info($"生成入库单条码主档失败:{result.Data[0].m_errorMsg}");
                    }
                }
            }
            catch (System.Exception ex)
            {
                action.CatchExceptionWithLog(ex, "生成入库单条码主档异常");
            }
            Logger.Interface.Info($"/* 入库单条码主档接口结束 */\r\n"); //固定写法
            return action;
        }
 
        /// <summary>
        /// 半成品生成条码接口
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction<List<string>>> U9CCreateBarCodeByAssignQty(BarcodeCreateByAssignQtyInput input)
        {
            Logger.Interface.Info($"/**\r\n *进入半成品生成条码接口....\r\n */\r\n"); //固定写法
            var action = new ApiAction<List<string>>(new List<string>());
            try
            {
                if (input.IsLogin)
                {
                    var u9CLoginResult = await HttpHelper.GetAsync<U9CLoginResult>($"{U9CAuthLoginUrl}userCode={input.userId}{secret}");
                    if (u9CLoginResult.Success)
                    {
                        input.token = u9CLoginResult.Data;
                        Logger.Interface.Info($"U9C登录token: {input.token}");
                    }
                }
                List<BarcodeCreateByAssignQtyParam> param = new() {
                     new() {
                        CreateBarCodeEntitys = new(){
                                           new CreateBarCodeEntitys
                                           {
                                                EntityID = input.RevDocLineId, //入库单ID
                                                EntityType="UFIDA.U9.Complete.RCVRpt.RcvRptDocLine"
                                           }
                                        },
                        BarCodeRuleID = "1002308220305008",
                        CreateBarCodeItemQty = input.CreateBarCodeItemQty,
                        LabelQty = input.LabelQty,
                     }
                };
                var response = await HttpHelper.PostAsync(CreateBarCodeByAssignQtyUrl, JsonConvert.SerializeObject(param), new Dictionary<string, string>() { { "token", input.token } });
                var result = JsonConvert.DeserializeObject<BarcodeCreateByAssignQtyResult>(response.Message);
                if (result != null)
                {
                    if (result.Success && result.Data.IsSuccess)
                    {
                        Logger.Interface.Info($"半成品生成条码接口提交Json: {JsonConvert.SerializeObject(param)},返回Json: {response.Message}");
                        action.Data = result.Data.Data;
                    }
                    else
                    {
                        action.IsSuccessed = false;
                        action.LocaleMsg = new($"{action.Message}; {result.Data.ErrorMessage}");
                        Logger.Interface.Info($"半成品生成条码失败:{result.Data.ErrorMessage}");
                    }
                }
            }
            catch (System.Exception ex)
            {
                action.CatchExceptionWithLog(ex, "半成品生成条码异常");
            }
            Logger.Interface.Info($"/* 半成品生成条码接口结束 */\r\n"); //固定写法
            return action;
        }
 
        private List<BAS_LABEL_TEMP> GetLabels(List<InStorePrintJson> printJsons)
        {
            List<BAS_LABEL_TEMP> labels = new List<BAS_LABEL_TEMP>();
            int i = 1;
            int j = 1;
            foreach (var p in printJsons)
            {
                BAS_LABEL_TEMP label = Biz.Db.Queryable<BAS_LABEL_TEMP>().Where(q => q.LABEL_CODE == "InstoreTemplate").IncludesAllFirstLayer().First();
                foreach (var v in p.Items.Where(q => q.IS_HANDLED != "F" && q.IS_HANDLED != "P"))
                {
                    foreach (var item in label.Variables)
                    {
                        if (item.VAR_NAME == "PageNum")
                        {
                            item.Value = $"第{i}页";
                        }
                        if (item.VAR_NAME == "PageSize")
                        {
                            item.Value = $"共{printJsons.Count}页";
                        }
                        if (item.VAR_NAME == "BusinessDate")
                        {
                            item.Value = DateTime.Now.ToString("yyyy.MM.dd");
                        }
                        if (item.VAR_NAME == $"RcvDoc{j}")
                        {
                            item.Value = v.ORDER_NO;
                        }
                        if (item.VAR_NAME == $"SoDoc{j}")
                        {
                            item.Value = v.SALES_ORDER;
                        }
                        if (item.VAR_NAME == $"MoDoc{j}")
                        {
                            item.Value = v.WORK_ORDER;
                        }
                        if (item.VAR_NAME == $"ItemCode{j}")
                        {
                            item.Value = v.ITEM_CODE;
                        }
                        if (item.VAR_NAME == $"Description{j}")
                        {
                            item.Value = v.ITEM_NAME;
                        }
                        if (item.VAR_NAME == $"BoxQty{j}")
                        {
                            item.Value = v.CartonQty.ToString();
                        }
                        if (item.VAR_NAME == $"InQty{j}")
                        {
                            item.Value = v.SCAN_QTY.ToString();
                        }
                        if (item.VAR_NAME == $"RcvQrCode{j}")
                        {
                            item.Value = v.ORDER_NO.ToString();
                        }
                        if (item.VAR_NAME == $"SapCode{j}")
                        {
                            item.Value = v.SapCode;
                        }
                        if (item.VAR_NAME == $"Seq{j}")
                        {
                            item.Value = j.ToString();
                        }
                    }
                    j++;
                }
                i++;
                labels.Add(label);
            }
            return labels;
        }
    }
}