kyy
2025-12-01 f0ac4ec65279aa31d568b933afeb8237bee73a72
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
using Newtonsoft.Json;
using System.Data.SqlClient;
using System.Dynamic;
using System.Net;
using System.Net.Cache;
using System.Text;
 
 
namespace Gs.Toolbox;
 
public class InterfaceUtil
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="param">json参数</param>
    /// <param name="edtUserGuid"></param>
    /// <param name="abtGuid"></param>
    /// <param name="hNo"></param>
    /// <param name="urlType">如果为2,则是更新工单状态</param>
    /// <returns>如果成功返回日志guid,否则返回串</returns>
    public static (int, string) HttpPostErp(string param, string edtUserGuid = "", string abtGuid = "", string hNo = "", int urlType = 0)
    {
        int _rtn = 0;
        //日志详细,发送的时候,记录日志,存储过程调用的时候,再累加上mes业务的操作结果
        System.Text.StringBuilder sbLog = new System.Text.StringBuilder();
        sbLog.Append(DateTime.Now.ToString() + "开始发送");
        string strLogGuid = Guid.NewGuid().ToString();
        string url = AppSettingsHelper.getValueByKey("TestErpUrl");
        if (urlType == 2)
        {
            url = AppSettingsHelper.getValueByKey("TestErpUrl2");
        }
        else if (urlType == 3)
        {
            url = AppSettingsHelper.getValueByKey("TestErpUrl3");
        }
        else if (urlType == 4)
        {
            url = AppSettingsHelper.getValueByKey("TestErpUrl4");
        }
        else if (urlType == 5)
        {
            url = AppSettingsHelper.getValueByKey("TestErpUrl5");
        }
        else if (urlType == 6)
        {
            url = AppSettingsHelper.getValueByKey("TestErpUrl6");
        }
        else if (urlType == 101)
        {
            url = AppSettingsHelper.getValueByKey("TestErpUrl101");
        }
        HttpWebRequest request = null;
        StreamWriter requestStream = null;
        WebResponse response = null;
        string responseStr = "";
        try
        {
            request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.Accept = "*/*";
            //request.Timeout = 150000;
            request.AllowAutoRedirect = false;
            request.ServicePoint.Expect100Continue = false;
            HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            request.CachePolicy = noCachePolicy;
            requestStream = new StreamWriter(request.GetRequestStream());
            requestStream.Write(param);
            requestStream.Close();
            response = request.GetResponse();
            if (response != null)
            {
                var reader = new StreamReader(response.GetResponseStream(),Encoding.UTF8);
                responseStr = reader.ReadToEnd();
                reader.Close();
            }
           // LogHelper.Debug(url, "HttpPostErp response:" + param + ",response:" + responseStr.ToString());
        }
        catch (Exception ex)
        {
            LogHelper.Debug(url, "HttpPostErp response:" + param + ",ex:" + ex.Message);
            responseStr = ex.Message;
            _rtn = -1;
        }
        finally
        {
            request = null;
            requestStream = null;
            response = null;
        }
        //{"msg":"操作成功","code":200,"data":{"number":"CGSL168397","code":"200","id":"268781"}}
        if (_rtn != -1)
        {
            try
            {
                Result _result = JsonConvert.DeserializeObject<Result>(responseStr);
                
                // 【优先判断】data.code(ERP真正的处理结果)
                if (_result?.data != null && "200".Equals(_result.data.code))
                {
                    _rtn = 1;
                    LogHelper.Debug(url, $"ERP处理成功,data.code=200, number={_result.data.number}, id={_result.data.id}");
                }
                // 【降级判断】如果data为空,则判断外层code(兼容旧接口)
               /* else if (_result != null && "200".Equals(_result.code))
                {
                    _rtn = 1;
                    LogHelper.Debug(url, $"ERP外层返回成功,code=200(无data层,可能是旧接口)");
                }*/
                else
                {
                    _rtn = -1;
                    string errorMsg = _result?.data?.code != null 
                        ? $"ERP处理失败,data.code={_result.data.code}" 
                        : $"ERP处理失败,外层code={_result?.code}";
                    LogHelper.Debug(url, errorMsg + $", msg={_result?.msg}, responseStr={responseStr}");
                }
            }
            catch (Exception ex)
            {
                _rtn = -1;
                LogHelper.Debug(url, $"ERP返回数据解析失败:{ex.Message}, responseStr={responseStr}");
            }
        }
        sbLog.Append("," + DateTime.Now.ToString() + "结束发送");
        if (_rtn > 0)
            sbLog.Append(",发送成功");
        else
            sbLog.Append(",发送失败,mes退出操作");
        try
        {
            SqlParameter[] parameters =
    {
            new("@edtUserGuid", edtUserGuid),
            new("@abtGuid", abtGuid),
            new("@abtTable", ""),
            new("@detail", sbLog.ToString()),
            new("@hNo", hNo),
            new("@RtnLogGuid", strLogGuid),
            new("@SendJson", param),
            new("@RtnJson", responseStr),
            new("@isSuccess", (_rtn>0?1:0)),
            new("@isErp", 1),
        };
            DbHelperSQL.RunProcedure("[prc_log_create]", parameters);
        }
        catch (Exception ex)
        {
            LogHelper.Debug(url, "HttpPostErp 写入日志表" + ex.Message);
        }
        return (_rtn, (_rtn > 0 ? strLogGuid : responseStr));
    }
}
//{"msg":"操作成功","code":200,"data":{"number":"CGSL168397","code":"200","id":"268781"}}
public class Result
{
    /// <summary>
    /// 200成功,否则失败(外层code)
    /// </summary>
    public string? msg { get; set; }
    public string? code { get; set; }
    
    /// <summary>
    /// data层,包含真正的ERP处理结果
    /// </summary>
    public ErpData? data { get; set; }
}
 
/// <summary>
/// ERP返回的data层数据结构
/// </summary>
public class ErpData
{
    /// <summary>
    /// ERP单据编号
    /// </summary>
    public string? number { get; set; }
    
    /// <summary>
    /// ERP处理结果代码("200"成功)
    /// </summary>
    public string? code { get; set; }
    
    /// <summary>
    /// ERP单据ID
    /// </summary>
    public string? id { get; set; }
}