收款处理-接口调用优化

This commit is contained in:
李贵强 2025-03-31 09:42:00 +08:00
parent 68814180fd
commit d9dff33b09
4 changed files with 166 additions and 45 deletions

View File

@ -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<String, String> head = ApiUtils.buildHead(INTERFACE_ID1, RECEIVER_ID);
HashMap<String, Object> body = ApiUtils.buildBody(companyCode, this.getUserCode(), startDate, endDate);
HashMap<String, Object> data = (HashMap<String, Object>) body.get("data");
HashMap<String, Object> 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<String, String> head = ApiUtils.buildHead(INTERFACE_ID2, RECEIVER_ID);
HashMap<String, Object> 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<String, Object> 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<String,Object> getData(String companyCode, Date startDate, Date endDate){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
HashMap<String,Object> 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<String, Object> data, String apiName) {
JSONObject result = JSONObject.parseObject(response);
HashMap<String, Object> 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;
}
}

View File

@ -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");

View File

@ -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<String,String> buildHead(String interfaceId,String receiverId){
HashMap<String,String> 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<String,Object> 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<String,Object> buildBody(HashMap<String,Object> 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<String,Object> 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<String,Object> 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 请求
public static String sendPost(Map<String, ?> header, Map<String, ?> body, String url) throws IOException {
long startTime = System.currentTimeMillis();
try {
HttpPost httpPost = new HttpPost(url);
// 设置请求头
for (Object o : header.keySet()) {
httpPost.setHeader((String) o, (String) header.get(o));
// 设置星瀚环境请求头
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());
}
// 设置请求体
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 responseBody;
} catch (Exception e) {
logger.error("ESB请求异常 => URL: " + url, e);
throw e;
}
return responseData;
}
/**

View File

@ -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;
}
}