yanjinhui
2 天以前 fe596f9db05103917c9257348edcbd3ecb5b46e8
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO;
using WIDESEAWCS_DTO.RGV.FOURBOT;
using WIDESEAWCS_IBasicInfoService;
using WIDESEAWCS_Model.Models;
 
namespace WIDESEAWCS_BasicInfoService
{
    public class ApiInfoService : ServiceBase<Dt_ApiInfo, IRepository<Dt_ApiInfo>>, IApiInfoService
    {
        private readonly IInterfaceLogService _interfaceLogService;
        public ApiInfoService(IRepository<Dt_ApiInfo> BaseDal, IInterfaceLogService interfaceLogService) : base(BaseDal)
        {
            _interfaceLogService = interfaceLogService;
        }
 
        public IRepository<Dt_ApiInfo> Repository => BaseDal;
 
        /// <summary>
        /// Post接口请求
        /// </summary>
        /// <param name="apiCode">接口编号</param>
        /// <param name="requestParameters">请求内容</param>
        /// <param name="remark">备注</param>
        /// <param name="isAdd">是否添加日志</param>
        /// <returns></returns>
        public WebResponseContent PostInterfaceRequest(string apiCode, string requestParameters, string remark, bool isAdd = true)
        {
            WebResponseContent content = new WebResponseContent();
            string response = string.Empty;
            string requestHash = string.Empty;
            string responseHash = string.Empty;
            Dt_ApiInfo? apiInfo = null;
            try
            {
                apiInfo = BaseDal.QueryFirst(x => x.ApiCode == apiCode) ?? throw new Exception($"未找到{remark}接口配置信息!请检查接口配置");
                response = HttpHelper.Post(apiInfo.ApiAddress, requestParameters);
                requestHash = ComputeHash(requestParameters);
                responseHash = ComputeHash(response);
                if (apiInfo.Remark.Contains("四向车"))
                {
                    FOURBOTReturn fOURBOTReturn = response.DeserializeObject<FOURBOTReturn>();
                    if (fOURBOTReturn == null) throw new Exception($"{apiInfo.Remark}响应内容转换实体失败!");
                    if (fOURBOTReturn.returnCode != 0) throw new Exception(fOURBOTReturn.returnUserMsg);
                    content.Data = fOURBOTReturn;
                }
                else if (apiInfo.Remark.Contains("凯乐士"))
                {
 
                }
                else if (apiInfo.Remark.Contains("海康"))
                {
 
                }
                else if (apiInfo.Remark.Contains("WMS"))
                {
 
                }
                content.OK();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            finally
            {
                if (isAdd && !string.IsNullOrEmpty(response) && apiInfo != null)
                {
                    #region 查询当天是否存在相同记录
                    var today = DateTime.Now.Date;
                    var existingLog = _interfaceLogService.Repository.QueryFirst(x =>
                        x.ApiCode == apiCode &&
                        x.RequestParametersHash == requestHash &&
                        x.ResponseParametersHash == responseHash &&
                        x.CreateDate.Date == today);
                    if (existingLog != null)
                    {
                        existingLog.PushFrequency = existingLog.PushFrequency + 1;
                        existingLog.PushState = content.Status ? 1 : 2;
                        existingLog.ResponseParameters = response;
                        existingLog.Remark = content.Status ? remark : content.Message;
                        existingLog.Modifier = App.User?.UserName ?? "System";
                        existingLog.ModifyDate = DateTime.Now;
                        _interfaceLogService.Repository.UpdateData(existingLog);
                    }
                    #endregion
                    else
                    {
                        Dt_InterfaceLog interfaceLog = new Dt_InterfaceLog()
                        {
                            ApiCode = apiCode,
                            RequestParameters = requestParameters,
                            ApiAddress = apiInfo.ApiAddress,
                            ApiName = apiInfo.ApiName,
                            RequestParametersHash = requestHash,
                            ResponseParametersHash = responseHash,
                            PushFrequency = 1,
                            PushState = content.Status ? 1 : 2,
                            Requestor = "WCS",
                            Recipient = apiInfo.Remark,
                            ResponseParameters = response,
                            Creater = "System",
                            Remark = content.Status ? remark : content.Message,
                        };
                        _interfaceLogService.Repository.AddData(interfaceLog);
                    }
                }
            }
            return content;
        }
 
        /// <summary>
        /// 计算字符串的SHA256哈希值
        /// </summary>
        private static string ComputeHash(string input)
        {
            if (string.IsNullOrEmpty(input))
                return string.Empty;
 
            using (SHA256 sha256 = SHA256.Create())
            {
                byte[] bytes = Encoding.UTF8.GetBytes(input);
                byte[] hashBytes = sha256.ComputeHash(bytes);
                return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
            }
        }
    }
}