kyy
3 天以前 c78e94462c0ba8b3c1ec6fbcddaa3eec9ffcb980
WebApi/Gs.Toolbox/InterfaceUtil.cs
@@ -18,22 +18,30 @@
    /// <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)
    public static (int, string) HttpPostErp(string param,
        string edtUserGuid = "", string abtGuid = "", string hNo = "",
        int urlType = 0, string keyUrl = "")
    {
        int _rtn = 0;
        string docNo = string.Empty;
        //日志详细,发送的时候,记录日志,存储过程调用的时候,再累加上mes业务的操作结果
        System.Text.StringBuilder sbLog = new System.Text.StringBuilder();
        sbLog.Append(DateTime.Now.ToString() + "开始发送");
        string strLogGuid = Guid.NewGuid().ToString();
        string url = AppSettingsHelper.getValueByKey("TestErpUrl");
        string url = AppSettingsHelper.getValueByKey("TestErpUrl") + keyUrl;
        if (urlType == 2)
            url = AppSettingsHelper.getValueByKey("TestErpUrl2");
            url = AppSettingsHelper.getValueByKey("TestErpUrl2") + keyUrl;
        LogHelper.Debug("InterfaceUtil.HttpPostErp",
            $"方法入口 - url:{url}, urlType:{urlType}, hNo:{hNo}, param长度:{param.Length}, param前200字符:{(param.Length > 200 ? param.Substring(0, 200) : param)}");
        HttpWebRequest request = null;
        StreamWriter requestStream = null;
        WebResponse response = null;
        string responseStr = "";
        try
        {
            LogHelper.Debug("InterfaceUtil.HttpPostErp", $"开始创建HTTP请求 - URL:{url}");
            request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json";
@@ -41,23 +49,31 @@
            request.Timeout = 150000;
            request.AllowAutoRedirect = false;
            request.ServicePoint.Expect100Continue = false;
            HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            HttpRequestCachePolicy noCachePolicy =
                new HttpRequestCachePolicy(HttpRequestCacheLevel
                    .NoCacheNoStore);
            request.CachePolicy = noCachePolicy;
            LogHelper.Debug("InterfaceUtil.HttpPostErp", "HTTP请求配置完成,开始发送数据");
            requestStream = new StreamWriter(request.GetRequestStream());
            requestStream.Write(param);
            requestStream.Close();
            LogHelper.Debug("InterfaceUtil.HttpPostErp", "数据发送完成,等待响应");
            response = request.GetResponse();
            LogHelper.Debug("InterfaceUtil.HttpPostErp", "收到响应");
            if (response != null)
            {
                var reader = new StreamReader(response.GetResponseStream(),
                    Encoding.UTF8);
                responseStr = reader.ReadToEnd();
                reader.Close();
                LogHelper.Debug("InterfaceUtil.HttpPostErp",
                    $"响应读取完成 - 响应长度:{responseStr.Length}, 响应内容:{responseStr}");
            }
        }
        catch (Exception ex)
        {
            LogHelper.Debug(url, "HttpPostErp response:" + param + ",ex:" + ex.Message);
            LogHelper.Debug(url,
                "HttpPostErp response:" + param + ",ex:" + ex.Message);
            responseStr = ex.Message;
            _rtn = -1;
        }
@@ -67,51 +83,130 @@
            requestStream = null;
            response = null;
        }
        /*if (_rtn != -1)
              {
                  Result _result = JsonConvert.DeserializeObject<Result>(responseStr);
                  if ("200".Equals(_result.code))
                      _rtn = 1;
              }*/
        if (_rtn != -1)
        {
            Result _result = JsonConvert.DeserializeObject<Result>(responseStr);
            if ("200".Equals(_result.state))
                _rtn = 1;
                LogHelper.Debug("InterfaceUtil.HttpPostErp", $"开始解析响应JSON - responseStr:{responseStr}");
                Result _result = JsonConvert.DeserializeObject<Result>(responseStr);
                LogHelper.Debug("InterfaceUtil.HttpPostErp",
                    $"JSON解析完成 - status:{_result?.status}, message:{_result?.message}, doc_no:{_result?.data?.doc_no}");
                // 新判断规则:status=0 且 message=Success 才算成功
                if (_result?.status == "0" && _result?.message == "Success")
                {
                    _rtn = 1;
                    LogHelper.Debug("InterfaceUtil.HttpPostErp", "判断结果:成功");
                }
                else
                {
                    LogHelper.Debug("InterfaceUtil.HttpPostErp",
                        $"判断结果:失败 - status不为0或message不为Success");
                }
               docNo = _result.data.doc_no;
        }
        sbLog.Append("," + DateTime.Now.ToString() + "结束发送");
        if (_rtn > 0)
        {
            sbLog.Append(",发送成功");
            //发送成功反写erp单号
            LogHelper.Debug("InterfaceUtil.HttpPostErp",
                $"开始调用存储过程反写ERP单号 - hNo:{hNo}, erpNo:{docNo}, Api:{keyUrl}");
            SqlParameter[] parameters =
               {
                new("@hNo", hNo),
                new("@erpNo",docNo),
                new("@Api", keyUrl)  ,
                new("@in1", ""),
                new("@in2", ""),
               };
            DbHelperSQL.RunProcedure("[prc_Rev_ErpNo]", parameters);
            LogHelper.Debug("InterfaceUtil.HttpPostErp", "反写ERP单号完成");
        }
        else
        {
            sbLog.Append(",发送失败,mes退出操作");
            LogHelper.Debug("InterfaceUtil.HttpPostErp", $"发送失败 - _rtn:{_rtn}");
        }
        try
        {
            LogHelper.Debug("InterfaceUtil.HttpPostErp", "开始写入日志表");
            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),
        };
            {
                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);
            LogHelper.Debug("InterfaceUtil.HttpPostErp", "日志表写入完成");
        }
        catch (Exception ex)
        {
            LogHelper.Debug(url, "HttpPostErp 写入日志表" + ex.Message);
        }
        LogHelper.Debug("InterfaceUtil.HttpPostErp",
            $"方法结束 - 返回值:(_rtn:{_rtn}, {(_rtn > 0 ? "strLogGuid:" + strLogGuid : "responseStr:" + responseStr)})");
        return (_rtn, (_rtn > 0 ? strLogGuid : responseStr));
    }
}
/// <summary>
/// 接口返回结果根对象
/// </summary>
public class Result
{
    /// <summary>
    /// 200成功,否则失败
    /// 状态码:0表示成功,-1表示失败
    /// </summary>
    public string? state { get; set; }
    public string? msg { get; set; }
    public string? status { get; set; }
    /// <summary>
    /// 状态描述:Success表示成功,失败时为具体异常信息
    /// </summary>
    public string? message { get; set; }
    /// <summary>
    /// 数据体:成功时返回到货单信息,失败时为具体异常信息
    /// </summary>
    public ResultData? data { get; set; }
}
/// <summary>
/// 接口返回的Data对象(强类型)
/// 对应JSON:{"doc_type_no":"370","doc_no":"20251209001","msg":"创建到货单成功."}
/// </summary>
public class ResultData
{
    /// <summary>
    /// 单据类型编号
    /// </summary>
    public string? doc_type_no { get; set; }
    /// <summary>
    /// 单据编号
    /// </summary>
    public string? doc_no { get; set; }
    /// <summary>
    /// 操作结果描述
    /// </summary>
    public string? msg { get; set; }
}