收款处理-接口调用优化
This commit is contained in:
parent
68814180fd
commit
d9dff33b09
|
|
@ -2,6 +2,7 @@ package shjh.jhzj7.fi.fi.plugin.form;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
|
||||||
import kd.bos.dataentity.entity.DynamicObject;
|
import kd.bos.dataentity.entity.DynamicObject;
|
||||||
import kd.bos.dataentity.entity.DynamicObjectCollection;
|
import kd.bos.dataentity.entity.DynamicObjectCollection;
|
||||||
import kd.bos.dataentity.utils.StringUtils;
|
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 shjh.jhzj7.fi.fi.utils.EsbUtils;
|
||||||
|
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -166,17 +168,13 @@ public class FeeControlApiPlugin extends AbstractFormPlugin implements Plugin {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String supplierCode = supplier.getString("number");
|
String supplierCode = supplier.getString("number");
|
||||||
HashMap<String, String> head = ApiUtils.buildHead(INTERFACE_ID1, RECEIVER_ID);
|
HashMap<String, Object> data = this.getData(companyCode,startDate,endDate);
|
||||||
HashMap<String, Object> body = ApiUtils.buildBody(companyCode, this.getUserCode(), startDate, endDate);
|
|
||||||
HashMap<String, Object> data = (HashMap<String, Object>) body.get("data");
|
|
||||||
|
|
||||||
// 添加额外的参数
|
// 添加额外的参数
|
||||||
data.put("SupplierCode", supplierCode);
|
data.put("SupplierCode", supplierCode);
|
||||||
data.put("IsWriteOffDetail", true);
|
data.put("IsWriteOffDetail", true);
|
||||||
|
String response = ApiUtils.sendPost(INTERFACE_ID1,RECEIVER_ID,data);
|
||||||
String response = ApiUtils.sendPost(head, body, "https://hipint-stg.jahwa.com.cn:6443/gateway/HIP_ReceiveFromFM/1.0/fm/send");
|
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
Boolean result = parseResponse(response, billNumber, String.valueOf(body), "预付款单清单查询接口");
|
Boolean result = parseResponse(response, billNumber, data, "预付款单清单查询接口");
|
||||||
if (result) {
|
if (result) {
|
||||||
handlePaymentApiResponse(response);
|
handlePaymentApiResponse(response);
|
||||||
}
|
}
|
||||||
|
|
@ -197,11 +195,10 @@ public class FeeControlApiPlugin extends AbstractFormPlugin implements Plugin {
|
||||||
*/
|
*/
|
||||||
private void handleLoanApi(String companyCode, Date startDate, Date endDate, String billNumber) {
|
private void handleLoanApi(String companyCode, Date startDate, Date endDate, String billNumber) {
|
||||||
try {
|
try {
|
||||||
HashMap<String, String> head = ApiUtils.buildHead(INTERFACE_ID2, RECEIVER_ID);
|
HashMap<String, Object> data = this.getData(companyCode,startDate,endDate);
|
||||||
HashMap<String, Object> body = ApiUtils.buildBody(companyCode, this.getUserCode(), startDate, endDate);
|
String response = ApiUtils.sendPost(INTERFACE_ID2, RECEIVER_ID, data);
|
||||||
String response = ApiUtils.sendPost(head, body, "https://hipint-stg.jahwa.com.cn:6443/gateway/HIP_ReceiveFromFM/1.0/fm/send");
|
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
Boolean result = parseResponse(response, billNumber, String.valueOf(body), "借款单清单查询接口");
|
Boolean result = parseResponse(response, billNumber, data, "借款单清单查询接口");
|
||||||
if (result) {
|
if (result) {
|
||||||
handleLoanApiResponse(response);
|
handleLoanApiResponse(response);
|
||||||
}
|
}
|
||||||
|
|
@ -423,40 +420,62 @@ public class FeeControlApiPlugin extends AbstractFormPlugin implements Plugin {
|
||||||
return number;
|
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 response 响应参数
|
||||||
* @param billNumber 本单单号
|
* @param billNumber 本单单号
|
||||||
* @param body 请求体
|
* @param data 请求体
|
||||||
* @param apiName 接口名
|
* @param apiName 接口名
|
||||||
* @return
|
* @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);
|
JSONObject result = JSONObject.parseObject(response);
|
||||||
|
HashMap<String, Object> body = ApiUtils.buildBody(data);
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
if (null != result) {
|
if (null != result) {
|
||||||
String code = result.getString("code");
|
String code = result.getString("code");
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case "0":
|
case "0":
|
||||||
success = true;
|
success = true;
|
||||||
EsbUtils.saveLog(billNumber, apiName, body, response, true, "ESBQueryApi", "查询成功");
|
EsbUtils.saveLog(billNumber, apiName, String.valueOf(body), response, true, "ESBQueryApi", "查询成功");
|
||||||
break;
|
break;
|
||||||
case "1":
|
case "1":
|
||||||
this.getView().showMessage("服务异常:" + result.getString("msg"));
|
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;
|
break;
|
||||||
case "2":
|
case "2":
|
||||||
this.getView().showMessage("三方服务异常:" + result.getString("msg"));
|
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;
|
break;
|
||||||
case "3":
|
case "3":
|
||||||
this.getView().showMessage("业务异常:" + result.getString("msg"));
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
this.getView().showMessage(result.getString("msg"));
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -190,7 +190,7 @@ public class RecPushVoucherOperation extends AbstractOperationServicePlugIn impl
|
||||||
IS_HEADER.put("BUKRS", recBill.getString("org.number"));
|
IS_HEADER.put("BUKRS", recBill.getString("org.number"));
|
||||||
|
|
||||||
// 凭证类型(留空)
|
// 凭证类型(留空)
|
||||||
IS_HEADER.put("BLART", "");
|
IS_HEADER.put("BLART", "DZ");
|
||||||
|
|
||||||
// 凭证日期
|
// 凭证日期
|
||||||
Date bizDate = recBill.getDate("bizdate");
|
Date bizDate = recBill.getDate("bizdate");
|
||||||
|
|
|
||||||
|
|
@ -5,25 +5,50 @@ import com.alibaba.fastjson.JSONObject;
|
||||||
import kd.bos.logging.Log;
|
import kd.bos.logging.Log;
|
||||||
import kd.bos.logging.LogFactory;
|
import kd.bos.logging.LogFactory;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
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.impl.client.HttpClients;
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author LiGuiQiang
|
* @author LiGuiQiang
|
||||||
*/
|
*/
|
||||||
public class ApiUtils {
|
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){
|
public static HashMap<String,String> buildHead(String interfaceId,String receiverId){
|
||||||
HashMap<String,String> header = new HashMap<>(5);
|
HashMap<String,String> header = new HashMap<>(5);
|
||||||
header.put("Content-Type","application/json;charset=UTF-8");
|
header.put("Content-Type","application/json;charset=UTF-8");
|
||||||
|
|
@ -33,6 +58,14 @@ public class ApiUtils {
|
||||||
return header;
|
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){
|
public static HashMap<String,Object> buildBody(String companyCode,String userCode,Date queryBeginDate,Date queryEndDate){
|
||||||
// 生成唯一事务ID
|
// 生成唯一事务ID
|
||||||
String rootContextId = UUID.randomUUID().toString();
|
String rootContextId = UUID.randomUUID().toString();
|
||||||
|
|
@ -53,6 +86,62 @@ public class ApiUtils {
|
||||||
return body;
|
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
|
* @param header
|
||||||
|
|
@ -61,32 +150,35 @@ public class ApiUtils {
|
||||||
* @return
|
* @return
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public static String sendPost(HashMap header, HashMap body, String url) throws IOException {
|
public static String sendPost(Map<String, ?> header, Map<String, ?> body, String url) throws IOException {
|
||||||
String responseData = "";
|
long startTime = System.currentTimeMillis();
|
||||||
// 准备要发送的 JSON 数据
|
try {
|
||||||
// 使用 FastJSON 将 Map 转换为 JSON 字符串
|
HttpPost httpPost = new HttpPost(url);
|
||||||
String jsonString = JSON.toJSONString(body);
|
|
||||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
// 设置星瀚环境请求头
|
||||||
// 创建 HttpPost 请求
|
header.forEach((k, v) -> httpPost.setHeader(k, String.valueOf(v)));
|
||||||
HttpPost httpPost = new HttpPost(url);
|
|
||||||
// 设置请求头
|
// 使用更高效的JSON序列化
|
||||||
for (Object o : header.keySet()) {
|
String jsonBody = JSON.toJSONString(body);
|
||||||
httpPost.setHeader((String) o, (String) header.get(o));
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
package shjh.jhzj7.fi.fi.utils;
|
package shjh.jhzj7.fi.fi.utils;
|
||||||
|
|
||||||
import kd.bos.dataentity.entity.DynamicObject;
|
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.Log;
|
||||||
import kd.bos.logging.LogFactory;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue