From d9dff33b0983b9fdf70000a65008ba1cdebc2969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=B4=B5=E5=BC=BA?= Date: Mon, 31 Mar 2025 09:42:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B6=E6=AC=BE=E5=A4=84=E7=90=86-=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E8=B0=83=E7=94=A8=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fi/plugin/form/FeeControlApiPlugin.java | 55 ++++--- .../operate/RecPushVoucherOperation.java | 2 +- .../java/shjh/jhzj7/fi/fi/utils/ApiUtils.java | 144 ++++++++++++++---- .../java/shjh/jhzj7/fi/fi/utils/ObjUtils.java | 10 ++ 4 files changed, 166 insertions(+), 45 deletions(-) diff --git a/main/java/shjh/jhzj7/fi/fi/plugin/form/FeeControlApiPlugin.java b/main/java/shjh/jhzj7/fi/fi/plugin/form/FeeControlApiPlugin.java index 1129dcc..3d809f5 100644 --- a/main/java/shjh/jhzj7/fi/fi/plugin/form/FeeControlApiPlugin.java +++ b/main/java/shjh/jhzj7/fi/fi/plugin/form/FeeControlApiPlugin.java @@ -2,6 +2,7 @@ package shjh.jhzj7.fi.fi.plugin.form; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; + import kd.bos.dataentity.entity.DynamicObject; import kd.bos.dataentity.entity.DynamicObjectCollection; import kd.bos.dataentity.utils.StringUtils; @@ -20,6 +21,7 @@ import shjh.jhzj7.fi.fi.utils.ApiUtils; import shjh.jhzj7.fi.fi.utils.EsbUtils; +import java.text.SimpleDateFormat; import java.util.*; /** @@ -166,17 +168,13 @@ public class FeeControlApiPlugin extends AbstractFormPlugin implements Plugin { return; } String supplierCode = supplier.getString("number"); - HashMap head = ApiUtils.buildHead(INTERFACE_ID1, RECEIVER_ID); - HashMap body = ApiUtils.buildBody(companyCode, this.getUserCode(), startDate, endDate); - HashMap data = (HashMap) body.get("data"); - + HashMap data = this.getData(companyCode,startDate,endDate); // 添加额外的参数 data.put("SupplierCode", supplierCode); data.put("IsWriteOffDetail", true); - - String response = ApiUtils.sendPost(head, body, "https://hipint-stg.jahwa.com.cn:6443/gateway/HIP_ReceiveFromFM/1.0/fm/send"); + String response = ApiUtils.sendPost(INTERFACE_ID1,RECEIVER_ID,data); if (response != null) { - Boolean result = parseResponse(response, billNumber, String.valueOf(body), "预付款单清单查询接口"); + Boolean result = parseResponse(response, billNumber, data, "预付款单清单查询接口"); if (result) { handlePaymentApiResponse(response); } @@ -197,11 +195,10 @@ public class FeeControlApiPlugin extends AbstractFormPlugin implements Plugin { */ private void handleLoanApi(String companyCode, Date startDate, Date endDate, String billNumber) { try { - HashMap head = ApiUtils.buildHead(INTERFACE_ID2, RECEIVER_ID); - HashMap body = ApiUtils.buildBody(companyCode, this.getUserCode(), startDate, endDate); - String response = ApiUtils.sendPost(head, body, "https://hipint-stg.jahwa.com.cn:6443/gateway/HIP_ReceiveFromFM/1.0/fm/send"); + HashMap data = this.getData(companyCode,startDate,endDate); + String response = ApiUtils.sendPost(INTERFACE_ID2, RECEIVER_ID, data); if (response != null) { - Boolean result = parseResponse(response, billNumber, String.valueOf(body), "借款单清单查询接口"); + Boolean result = parseResponse(response, billNumber, data, "借款单清单查询接口"); if (result) { handleLoanApiResponse(response); } @@ -423,40 +420,62 @@ public class FeeControlApiPlugin extends AbstractFormPlugin implements Plugin { return number; } + /** + * 组装参数 + * @param companyCode 公司代码 + * @param startDate 开始日期 + * @param endDate 结束日期 + * @return + */ + private HashMap getData(String companyCode, Date startDate, Date endDate){ + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + HashMap data = new HashMap<>(10); + data.put("UserCode","GH017994"); + //data.put("UserCode",this.getUserCode()); + data.put("QueryBeginDate",sdf.format(startDate)); + data.put("QueryEndDate",sdf.format(endDate)); + data.put("page","1"); + data.put("rows","10"); + data.put("CompanyCode",companyCode); + + return data; + } + /** * 日志记录 * * @param response 响应参数 * @param billNumber 本单单号 - * @param body 请求体 + * @param data 请求体 * @param apiName 接口名 * @return */ - private Boolean parseResponse(String response, String billNumber, String body, String apiName) { + private Boolean parseResponse(String response, String billNumber, HashMap data, String apiName) { JSONObject result = JSONObject.parseObject(response); + HashMap body = ApiUtils.buildBody(data); boolean success = false; if (null != result) { String code = result.getString("code"); switch (code) { case "0": success = true; - EsbUtils.saveLog(billNumber, apiName, body, response, true, "ESBQueryApi", "查询成功"); + EsbUtils.saveLog(billNumber, apiName, String.valueOf(body), response, true, "ESBQueryApi", "查询成功"); break; case "1": this.getView().showMessage("服务异常:" + result.getString("msg")); - EsbUtils.saveLog(billNumber, apiName, body, response, false, "ESBQueryApi", "服务异常"); + EsbUtils.saveLog(billNumber, apiName, String.valueOf(body), response, false, "ESBQueryApi", "服务异常"); break; case "2": this.getView().showMessage("三方服务异常:" + result.getString("msg")); - EsbUtils.saveLog(billNumber, apiName, body, response, false, "ESBQueryApi", "三方服务异常"); + EsbUtils.saveLog(billNumber, apiName, String.valueOf(body), response, false, "ESBQueryApi", "三方服务异常"); break; case "3": this.getView().showMessage("业务异常:" + result.getString("msg")); - EsbUtils.saveLog(billNumber, apiName, body, response, false, "ESBQueryApi", "业务异常"); + EsbUtils.saveLog(billNumber, apiName, String.valueOf(body), response, false, "ESBQueryApi", "业务异常"); break; default: this.getView().showMessage(result.getString("msg")); - EsbUtils.saveLog(billNumber, apiName, body, response, false, "ESBQueryApi", "未知异常"); + EsbUtils.saveLog(billNumber, apiName, String.valueOf(body), response, false, "ESBQueryApi", "未知异常"); break; } } diff --git a/main/java/shjh/jhzj7/fi/fi/plugin/operate/RecPushVoucherOperation.java b/main/java/shjh/jhzj7/fi/fi/plugin/operate/RecPushVoucherOperation.java index c7aa85e..024e274 100644 --- a/main/java/shjh/jhzj7/fi/fi/plugin/operate/RecPushVoucherOperation.java +++ b/main/java/shjh/jhzj7/fi/fi/plugin/operate/RecPushVoucherOperation.java @@ -190,7 +190,7 @@ public class RecPushVoucherOperation extends AbstractOperationServicePlugIn impl IS_HEADER.put("BUKRS", recBill.getString("org.number")); // 凭证类型(留空) - IS_HEADER.put("BLART", ""); + IS_HEADER.put("BLART", "DZ"); // 凭证日期 Date bizDate = recBill.getDate("bizdate"); diff --git a/main/java/shjh/jhzj7/fi/fi/utils/ApiUtils.java b/main/java/shjh/jhzj7/fi/fi/utils/ApiUtils.java index b5c4c5b..3963ffe 100644 --- a/main/java/shjh/jhzj7/fi/fi/utils/ApiUtils.java +++ b/main/java/shjh/jhzj7/fi/fi/utils/ApiUtils.java @@ -5,25 +5,50 @@ import com.alibaba.fastjson.JSONObject; import kd.bos.logging.Log; import kd.bos.logging.LogFactory; import org.apache.http.HttpResponse; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; +import java.util.Map; import java.util.UUID; +import java.util.concurrent.TimeUnit; /** * @author LiGuiQiang */ public class ApiUtils { - static Log log = LogFactory.getLog(ApiUtils.class); + static Log logger = LogFactory.getLog(ApiUtils.class); + //接口地址-测试地址 + private static final String API_URL="https://hipint-stg.jahwa.com.cn:6443/gateway/HIP_ReceiveFromFM/1.0/fm/send"; + + // 线程安全的HTTP客户端 + private static final CloseableHttpClient HTTP_CLIENT = HttpClientBuilder.create() + .setMaxConnTotal(50) + .setMaxConnPerRoute(20) + .setConnectionTimeToLive(30, TimeUnit.SECONDS) + .setDefaultRequestConfig(RequestConfig.custom() + .setConnectTimeout(5000) + .setSocketTimeout(30000) + .build()) + .build(); + + /** + * 固定请求头 + * @param interfaceId 接口路由 + * @param receiverId 接收方 + * @return + */ public static HashMap buildHead(String interfaceId,String receiverId){ HashMap header = new HashMap<>(5); header.put("Content-Type","application/json;charset=UTF-8"); @@ -33,6 +58,14 @@ public class ApiUtils { return header; } + /** + * 固定请求体(部分) + * @param companyCode + * @param userCode + * @param queryBeginDate + * @param queryEndDate + * @return + */ public static HashMap buildBody(String companyCode,String userCode,Date queryBeginDate,Date queryEndDate){ // 生成唯一事务ID String rootContextId = UUID.randomUUID().toString(); @@ -53,6 +86,62 @@ public class ApiUtils { return body; } + /** + * 固定请求体(部分) + * @param data 请求数据 + * @return + */ + public static HashMap buildBody(HashMap data){ + // 生成唯一事务ID + String rootContextId = UUID.randomUUID().toString(); + // 获取当前请求时间,格式为 yyyy-MM-dd HH:mm:ss.SSS + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + String requestTime = sdf.format(new Date()); + HashMap body = new HashMap<>(5); + body.put("rootContextID",rootContextId); + body.put("requestTime",requestTime); + body.put("data",data); + return body; + } + + /** + * 接口调用 + * @param interfaceId 接口路由 + * @param receiverId 接收方 + * @param data 请求数据 + * @return 响应数据 + * @throws IOException + */ + public static String sendPost(String interfaceId,String receiverId,HashMap data) throws IOException { + long startTime = System.currentTimeMillis(); + try { + HttpPost httpPost = new HttpPost(API_URL); + + // 设置星瀚环境请求头 + buildHead( interfaceId,receiverId).forEach((k, v) -> httpPost.setHeader(k, String.valueOf(v))); + + // 使用更高效的JSON序列化 + String jsonBody = JSON.toJSONString(buildBody(data)); + httpPost.setEntity(new StringEntity(jsonBody, StandardCharsets.UTF_8)); + + // 执行请求(集成星瀚日志审计) + logger.debug("ESB请求开始 => URL: {}, Body: {}", API_URL, jsonBody); + HttpResponse response = HTTP_CLIENT.execute(httpPost); + String responseBody = EntityUtils.toString(response.getEntity()); + + // 记录监控指标 + long cost = System.currentTimeMillis() - startTime; + logger.info("ESB请求完成 => 耗时: {}ms, 状态码: {}", cost, response.getStatusLine().getStatusCode()); + + if (response.getStatusLine().getStatusCode() != 200) { + throw new IOException("HTTP状态码异常: " + response.getStatusLine().getStatusCode()); + } + return responseBody; + } catch (Exception e) { + logger.error("ESB请求异常 => URL: " + API_URL, e); + throw e; + } + } /** * 调接口 * @param header @@ -61,32 +150,35 @@ public class ApiUtils { * @return * @throws IOException */ - public static String sendPost(HashMap header, HashMap body, String url) throws IOException { - String responseData = ""; - // 准备要发送的 JSON 数据 - // 使用 FastJSON 将 Map 转换为 JSON 字符串 - String jsonString = JSON.toJSONString(body); - CloseableHttpClient httpClient = HttpClients.createDefault(); - // 创建 HttpPost 请求 - HttpPost httpPost = new HttpPost(url); - // 设置请求头 - for (Object o : header.keySet()) { - httpPost.setHeader((String) o, (String) header.get(o)); + public static String sendPost(Map header, Map body, String url) throws IOException { + long startTime = System.currentTimeMillis(); + try { + HttpPost httpPost = new HttpPost(url); + + // 设置星瀚环境请求头 + header.forEach((k, v) -> httpPost.setHeader(k, String.valueOf(v))); + + // 使用更高效的JSON序列化 + String jsonBody = JSON.toJSONString(body); + httpPost.setEntity(new StringEntity(jsonBody, StandardCharsets.UTF_8)); + + // 执行请求(集成星瀚日志审计) + logger.debug("ESB请求开始 => URL: {}, Body: {}", url, jsonBody); + HttpResponse response = HTTP_CLIENT.execute(httpPost); + String responseBody = EntityUtils.toString(response.getEntity()); + + // 记录监控指标 + long cost = System.currentTimeMillis() - startTime; + logger.info("ESB请求完成 => 耗时: {}ms, 状态码: {}", cost, response.getStatusLine().getStatusCode()); + + if (response.getStatusLine().getStatusCode() != 200) { + throw new IOException("HTTP状态码异常: " + response.getStatusLine().getStatusCode()); + } + return responseBody; + } catch (Exception e) { + logger.error("ESB请求异常 => URL: " + url, e); + throw e; } - // 设置请求体 - StringEntity jsonEntity = new StringEntity(jsonString,"UTF-8"); - httpPost.setEntity(jsonEntity); - // 发送请求并获取响应 - HttpResponse response = httpClient.execute(httpPost); - // 检查响应状态 - if (response.getStatusLine().getStatusCode() == 200) { - // 获取响应数据 - responseData = EntityUtils.toString(response.getEntity()); - log.info("SendPost,Response: " + responseData); - } else { - log.info("SendPost,Error: " + response.getStatusLine().getStatusCode()); - } - return responseData; } /** diff --git a/main/java/shjh/jhzj7/fi/fi/utils/ObjUtils.java b/main/java/shjh/jhzj7/fi/fi/utils/ObjUtils.java index c657b04..02b5c6d 100644 --- a/main/java/shjh/jhzj7/fi/fi/utils/ObjUtils.java +++ b/main/java/shjh/jhzj7/fi/fi/utils/ObjUtils.java @@ -1,6 +1,9 @@ package shjh.jhzj7.fi.fi.utils; import kd.bos.dataentity.entity.DynamicObject; +import kd.bos.entity.operate.result.OperateErrorInfo; +import kd.bos.entity.plugin.args.AfterOperationArgs; +import kd.bos.entity.validate.ErrorLevel; import kd.bos.logging.Log; import kd.bos.logging.LogFactory; @@ -27,5 +30,12 @@ public class ObjUtils { } } + public static OperateErrorInfo addOperationError(StringBuilder message,DynamicObject dataEntity){ + OperateErrorInfo operateErrorInfo = new OperateErrorInfo(); + operateErrorInfo.setMessage(String.valueOf(message)); + operateErrorInfo.setErrorLevel(ErrorLevel.Error.name()); + operateErrorInfo.setPkValue(dataEntity.getPkValue()); + return operateErrorInfo; + } }