bklLiudl
2025-04-02 1bbbbc8bb49411b544626996a1370788142300e0
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
using Model.ModelDto.SysDto;
using Model.ModelVm.SysVm;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using WMS.DAL;
using WMS.Entity.Context;
using WMS.Entity.SysEntity;
using WMS.IBLL.ILogServer;
using WMS.IBLL.ISysServer;
 
namespace WMS.BLL.SysServer
{
    public class InterfaceServer : IInterfaceServer
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
 
        private readonly IOperationSysServer _operation; //操作日志
        public InterfaceServer(IOperationSysServer operation)
        {
            _operation = operation;
        }
        /// <summary>
        /// 获取接口列表
        /// </summary>
        /// <param name="model"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<InterfaceDto> GetInterfaceList(InterfaceVm model, out int count)
        {
            string sqlString = string.Empty;
            string sqlCount = string.Empty;
            string sqlPub = string.Empty;
            try
            {
                sqlCount += "SELECT DISTINCT COUNT(tb1.Id) FROM SysInterface AS tb1 ";
 
                sqlString += "SELECT DISTINCT tb1.*,tb2.RealName as CreateUserName,tb3.RealName as UpdateUserName FROM SysInterface AS tb1 ";
                sqlPub += "LEFT JOIN SysUserInfor AS tb2 ON tb1.CreateUser = tb2.Id ";
                sqlPub += "LEFT JOIN SysUserInfor AS tb3 ON tb1.UpdateUser = tb3.Id ";
                sqlPub += $"WHERE tb1.IsDel=0 ";
                if (!string.IsNullOrEmpty(model.InterfaceNo))
                {
                    sqlPub += $"AND tb1.InterfaceNo LIKE '%{model.InterfaceNo}%' ";
                }
                if (!string.IsNullOrEmpty(model.InterfaceName))
                {
                    sqlPub += $"AND tb1.InterfaceName LIKE '%{model.InterfaceName}%' ";
                }
                if (!string.IsNullOrEmpty(model.Direction))
                {
                    sqlPub += $"AND tb1.Direction = '{model.Direction}' ";
                }
                sqlCount += sqlPub;
                sqlPub += " order by tb1.Id ";
                if (model.Page == 0)
                {
                    model.Page = 1;
                }
                sqlString += sqlPub + $" offset {((model.Page - 1) * model.Limit)} rows fetch next {model.Limit} rows only;";
 
                var com = new Common();
                count = com.GetRowCount(sqlCount);
 
                var modelList = Db.Ado.SqlQuery<InterfaceDto>(sqlString);
 
                return modelList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取接口明细列表
        /// </summary>
        /// <param name="model"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<InterfaceDetailDto> GetInterfaceDetailList(InterfaceDetailVm model, out int count)
        {
            string sqlString = string.Empty;
            string sqlCount = string.Empty;
            int rowCount = 1;
            try
            {
                if (model.Page == 0)
                {
                    model.Page = 1;
                }
                sqlCount += $"SELECT COUNT(ID) FROM SysInterfaceDetail where InterfaceNo = '{model.InterfaceNo}' and isdel = '0';";
                var com = new Common();
                count = com.GetRowCount(sqlCount);
                if (count != 0)
                {
                    rowCount = count;
                }
 
                sqlString += "SELECT tb1.*,tb2.RealName as CreateUserName,tb3.RealName as UpdateUserName FROM SysInterfaceDetail AS tb1 ";
                sqlString += "LEFT JOIN SysUserInfor AS tb2 ON tb1.CreateUser = tb2.Id ";
                sqlString += "LEFT JOIN SysUserInfor AS tb3 ON tb1.UpdateUser = tb3.Id ";
                sqlString += $"WHERE tb1.InterfaceNo = '{model.InterfaceNo}' AND tb1.IsDel = '0' order by tb1.Id ";
                sqlString += $"offset {((model.Page - 1) * model.Limit)} rows fetch next {rowCount} rows only;";
 
                var modelList = Db.Ado.SqlQuery<InterfaceDetailDto>(sqlString);
 
                return modelList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        /// <summary>
        /// 添加接口信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string AddInterface(InterfaceVm model)
        {
            string strMessage = "";
            string sqlString = string.Empty;
            string sqlDetailStr = string.Empty;
            try
            {
                string sqlCount = $"SELECT COUNT(ID) FROM SysInterface where InterfaceNo = '{model.InterfaceNo}' and isdel = '0';";
                var com = new Common().GetRowCount(sqlCount);
                if (com > 0)
                {
                    strMessage = "-1:接口编号已存在,请勿重复添加!";
                    return strMessage;
                }
                //接口总信息
                sqlString += "Insert into SysInterface (InterfaceNo,InterfaceName,Direction,TransmissionMode,Remark,Url,DataSources,CreateUser) values ( ";
                sqlString += $"'{model.InterfaceNo}','{model.InterfaceName}','{model.Direction}','{model.TransmissionMode}','{model.Remark}','{model.Url}','{model.DataSources}','{model.CreateUser}' );";
 
                //接口明细
                var detailModels = model.InterfaceDetail;
                foreach (InterfaceDetailVm detailModel in detailModels)
                {
                    sqlDetailStr += "Insert into SysInterfaceDetail (InterfaceNo,ExtField,MapField,Field,";
                    sqlDetailStr += "FieldType,DataType,IsNull,Remark,FieldFather,CreateUser) values ( ";
                    sqlDetailStr += $"'{model.InterfaceNo}','{detailModel.ExtField}','{detailModel.MapField}','{detailModel.Field}', ";
                    sqlDetailStr += $"'{detailModel.FieldType}','{detailModel.DataType}','{detailModel.IsNull}','{detailModel.Remark}','{detailModel.FieldFather}','{model.CreateUser}'); ";
                }
 
                if (sqlDetailStr == string.Empty)
                {
                    return strMessage;
                }
                Db.Ado.BeginTran();
 
                int rowCount = Db.Ado.ExecuteCommand(sqlString);
                var rowDetailCount = 1;
                if (sqlDetailStr.Length > 0)
                {
                    rowDetailCount = Db.Ado.ExecuteCommand(sqlDetailStr);
                }
                Db.Ado.CommitTran();
                if (rowCount > 0 && rowDetailCount > 0)
                {
                    //维护操作日志
                    _operation.InsertOperation("系统设置", "接口管理", model.InterfaceNo, "添加", $"添加了接口编号为{model.InterfaceNo}的接口信息", Convert.ToInt32(model.CreateUser));
                    return strMessage;
                }
                else
                {
                    Db.Ado.RollbackTran();
                    return "-2:添加失败数据回滚!";
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
 
        /// <summary>
        /// 编辑接口信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string EditInterface(InterfaceVm model)
        {
            string strMessage = "";
            string sqlString = string.Empty;
            string sqlDetailStr = string.Empty;
            try
            {
                sqlString = "UPDATE SysInterface SET ";
                sqlString += $"InterfaceName = '{model.InterfaceName}',Direction = '{model.Direction}',";
                sqlString += $"TransmissionMode = '{model.TransmissionMode}',Remark = '{model.Remark}',Url = '{model.Url}',DataSources = '{model.DataSources}', ";
                sqlString += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' ";
                sqlString += $"WHERE InterfaceNo = '{model.InterfaceNo}'";
 
                var detailModels = model.InterfaceDetail;
                sqlDetailStr += $"SELECT * FROM SysInterfaceDetail WHERE InterfaceNo = '{model.InterfaceNo}' and isDel = '0';";
                var dbDetailModels = DataContext.Db.Ado.SqlQuery<SysInterfaceDetail>(sqlDetailStr);
 
                sqlDetailStr = string.Empty;
                // 处理已删除的明细
                foreach (SysInterfaceDetail dbDetailModel in dbDetailModels)
                {
                    if (detailModels.Count(it => it.Id == dbDetailModel.Id) == 0)
                    {
                        sqlDetailStr += $"UPDATE SysInterfaceDetail SET IsDel = '1',";
                        sqlDetailStr += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' ";
                        sqlDetailStr += $"WHERE Id = {dbDetailModel.Id};";
                    }
                }
 
                // 处理修改和添加的明细
                foreach (InterfaceDetailVm detailModel in detailModels)
                {
                    if (detailModel.Id == 0 || detailModel.Id == null)
                    {
                        sqlDetailStr += "Insert into SysInterfaceDetail (InterfaceNo,ExtField,MapField,Field,";
                        sqlDetailStr += "FieldType,DataType,IsNull,Remark,FieldFather,CreateUser) values ( ";
                        sqlDetailStr += $"'{model.InterfaceNo}','{detailModel.ExtField}','{detailModel.MapField}','{detailModel.Field}', ";
                        sqlDetailStr += $"'{detailModel.FieldType}','{detailModel.DataType}','{detailModel.IsNull}','{detailModel.Remark}','{detailModel.FieldFather}','{model.CreateUser}'); ";
                    }
                    else
                    {
                        //判断是否更改
                        int rowNum = dbDetailModels
                            .Count(it => it.Id == detailModel.Id
                                         && it.ExtField == detailModel.ExtField
                                         && it.MapField == detailModel.MapField
                                         && it.Field == detailModel.Field
                                         && it.FieldType == detailModel.FieldType
                                         && it.DataType == detailModel.DataType
                                         && it.IsNull == detailModel.IsNull
                                         && it.Remark == detailModel.Remark
                                         && it.FieldFather == detailModel.FieldFather);
                        if (rowNum > 0)
                        {
                            continue;
                        }
 
                        sqlDetailStr += $"UPDATE SysInterfaceDetail SET ";
                        sqlDetailStr += $"ExtField = '{detailModel.ExtField}',MapField = '{detailModel.MapField}',Field='{detailModel.Field}',FieldFather='{detailModel.FieldFather}', ";
                        sqlDetailStr += $"FieldType = '{detailModel.FieldType}',DataType = '{detailModel.DataType}', ";
                        sqlDetailStr += $"IsNull = '{detailModel.IsNull}',Remark = '{detailModel.Remark}', ";
                        sqlDetailStr += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' ";
                        sqlDetailStr += $"WHERE Id = {detailModel.Id};";
                    }
                }
 
                Db.Ado.BeginTran();
 
                int rowCount = Db.Ado.ExecuteCommand(sqlString);
                var rowDetailCount = 1;
                if (sqlDetailStr.Length > 0)
                {
                    rowDetailCount = Db.Ado.ExecuteCommand(sqlDetailStr);
                }
                Db.Ado.CommitTran();
                if (rowCount > 0 && rowDetailCount > 0)
                {
                    //维护操作日志
                    _operation.InsertOperation("系统设置", "接口管理", model.InterfaceNo, "编辑", $"编辑了接口编号为{model.InterfaceNo}的接口信息", Convert.ToInt32(model.CreateUser));
                    return strMessage;
                }
                else
                {
                    Db.Ado.RollbackTran();
                    return "-2:添加失败数据回滚!";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        /// <summary>
        /// 删除接口信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string DelInterface(InterfaceVm model)
        {
            try
            {
                string sqlString = "";
                // 删除总
                sqlString = $"UPDATE SysInterface SET IsDel = '1',";
                sqlString += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' ";
                sqlString += $"WHERE InterfaceNo = '{model.InterfaceNo}';";
 
                // 删除明细
                sqlString += $"UPDATE SysInterfaceDetail SET IsDel = '1',";
                sqlString += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' ";
                sqlString += $"WHERE InterfaceNo = '{model.InterfaceNo}';";
                Db.Ado.BeginTran();
                int rowCount = Db.Ado.ExecuteCommand(sqlString);
 
                if (rowCount < 2)
                {
                    Db.RollbackTran();
                    return "-1:删除失败!";
                }
                Db.Ado.CommitTran();
                _operation.InsertOperation("系统设置", "接口管理", model.InterfaceNo, "删除", $"删除了接口编号为{model.InterfaceNo}的接口信息", Convert.ToInt32(model.CreateUser));
                return "";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        /// <summary>
        /// 删除接口明细信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string DelInterfaceDetail(InterfaceDetailVm model)
        {
            try
            {
                string sqlString = "";
 
                // 删除明细
                sqlString += $"UPDATE SysInterfaceDetail SET IsDel = '1',";
                sqlString += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' ";
                sqlString += $"WHERE Id = '{model.Id}';";
                Db.Ado.BeginTran();
                int rowCount = Db.Ado.ExecuteCommand(sqlString);
 
                if (rowCount < 1)
                {
                    Db.RollbackTran();
                    return "-1:删除失败!";
                }
                Db.Ado.CommitTran();
                _operation.InsertOperation("系统设置", "接口管理", model.InterfaceNo, "删除", $"删除了接口编号为{model.InterfaceNo},本系统字段为{model.Field}的接口信息", Convert.ToInt32(model.CreateUser));
                return "";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        //public void ceshi()
        //{
        //    string interfaceNo = "A0001";
        //    string param = "'ASN2023110200002'";
        //    RespondResult(interfaceNo, param);
        //}
 
        /// <summary>
        /// 回传上游系统公共方法
        /// </summary>
        /// <param name="InterfaceNo">接口编号</param>
        /// <param name="param">存储过程参数</param>
        public void RespondResult(string InterfaceNo,string param)
        {
            try
            {
                var outDto = new Dictionary<string, object>();
                string sqlString = $@"select * from SysInterface where IsDel=0 and InterfaceNo='{InterfaceNo}'";
                //接口总信息
                var interfaceModel = Db.Ado.SqlQuery<SysInterface>(sqlString).FirstOrDefault();
                if (interfaceModel == null)
                {
                    throw new Exception("未查询到接口信息");
                }
                if (string.IsNullOrEmpty(interfaceModel.DataSources))
                {
                    throw new Exception("数据来源为空");
                }
                string detailSql = $@"select * from SysInterfaceDetail where IsDel=0 and InterfaceNo='{interfaceModel.InterfaceNo}'";
                //接口明细信息
                List<SysInterfaceDetail> _detailList = Db.Ado.SqlQuery<SysInterfaceDetail>(detailSql).ToList();
                if (_detailList.Count <= 0)
                {
                    throw new Exception("未查询到接口明细信息");
                }
                //执行存储过程查询数据
                string querySql = $@"EXEC {interfaceModel.DataSources} {param}";
                var requestDataList = Db.Ado.SqlQuery<RequestData>(querySql).ToList();
                //组织请求参数
                foreach (var item in _detailList)
                {
                    if (item.DataType == "0")//表头数据
                    {
                        var fileldFatherList = _detailList.Where(it => it.FieldFather == item.ExtField).ToList();//判找到该表头下的所有行
                        if (fileldFatherList.Count > 0)//判断该表头是否有行
                        {
                            var dicDetailList = new List<Dictionary<string, object>>();
                            foreach (var item2 in requestDataList)//行数据
                            {
                                var dicDetailModel = new Dictionary<string, object>();
                                foreach (var item3 in fileldFatherList)
                                {
                                    // 使用反射获取字段值
                                    string value = GetFieldValue(item2, item3.MapField);
                                    dicDetailModel.Add(item3.ExtField, value);
                                }
                                dicDetailList.Add(dicDetailModel);
                            }
                            outDto.Add(item.ExtField, dicDetailList);
                        }
                        else
                        {
                            var requestDataModel = requestDataList.FirstOrDefault();
                            // 使用反射获取字段值
                            string value = GetFieldValue(requestDataModel, item.MapField);
 
                            outDto.Add(item.ExtField, value);
                        }
                    }
                }
                //请求方式,0:Http+Xml  1:Http+Xml
                if (interfaceModel.TransmissionMode == "0")
                {
                    //请求参数
                    var jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(outDto);
                    //发起请求
                    var response = Utility.Tools.HttpHelper.DoPost(interfaceModel.Url, jsonData, interfaceModel.InterfaceName, interfaceModel.Remark);
                }
                else
                { 
                
                }               
            }
            catch (Exception ex)
            { 
            
            }
        }
        /// <summary>
        /// 通过字段名获取字段值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static string GetFieldValue(object obj, string fieldName)
        {
            //使用反射获取字段值
            Type type = obj.GetType();
            PropertyInfo propertyInfo = type.GetProperty(fieldName);
            if (propertyInfo != null)
            {
                return propertyInfo.GetValue(obj) == null ? "" : propertyInfo.GetValue(obj).ToString();
            }
            return null;
        }
    }
    public class RequestData
    { 
        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }
        public string Field4 { get; set; }
        public string Field5 { get; set; }
        public string Field6 { get; set; }
        public string Field7 { get; set; }
        public string Field8 { get; set; }
        public string Field9 { get; set; }
        public string Field10 { get; set; }
        public string Field11 { get; set; }
        public string Field12 { get; set; }
        public string Field13 { get; set; }
        public string Field14 { get; set; }
        public string Field15 { get; set; }
        public string Field16 { get; set; }
        public string Field17 { get; set; }
        public string Field18 { get; set; }
        public string Field19 { get; set; }
        public string Field20 { get; set; }
    }
}