提交内容:推送BIP数据公共类代码修改

备注:调整整体代码逻辑,推送逻辑
提交人:邹江涛
This commit is contained in:
zoujiangtao 2024-12-10 11:16:24 +08:00
parent 91f82c191d
commit 9343a1a821
1 changed files with 221 additions and 147 deletions

View File

@ -16,10 +16,17 @@ import okhttp3.Response;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.*;
/**
@ -157,11 +164,13 @@ public class ApiService {
* @param systemName 推送系统名称
* @return
*/
public static JSONObject paymentSlipsJson(DynamicObject dynamic, String systemName) {
public static String paymentSlipsJson(DynamicObject dynamic, String systemName) {
String requestBody;
// 获取推送的单据标识
String billMark = dynamic.getDataEntityType().getName();
DynamicObject[] objects = BusinessDataServiceHelper.load("shkd_apimapping", "id,billno," +
"shkd_name,shkd_sourcenumber,shkd_domainname,shkd_token,shkd_url," +
"shkd_sourcebill,shkd_targetsystem,entryentity,entryentity.shkd_tarfield,entryentity.shkd_tartier," +
"entryentity.shkd_tartype,entryentity.shkd_parentfield,entryentity.shkd_soufield,entryentity.shkd_defaultdata," +
"entryentity.shkd_required,entryentity.shkd_remarks",
@ -181,7 +190,6 @@ public class ApiService {
});
List<List<DynamicObject>> floors = new ArrayList<>();
// 目前定的4层之后需要弄成动态的
for (int i = 1; i <= tiers.size(); i++) {
floors.add(new ArrayList<>());
}
@ -191,68 +199,40 @@ public class ApiService {
int tier = Integer.parseInt(dynamicObject.getString("shkd_tartier"));
floors.get(tier - 1).add(dynamicObject);
}
JSONObject resultJson = new JSONObject();
processFloor(resultJson, "data", floors.get(0), floors, dynamic);
logger.info("调用接口系统:{}\n调用接口单据{}\n调用JSON{}", systemName, dynamic, resultJson);
return resultJson;
}
public static void traverseJson(JSONObject jsonObject, List<String> path, String level) {
for (String key : jsonObject.keySet()) {
Object value = jsonObject.get(key);
String fullPath = buildPath(path, key, level);
System.out.println(fullPath + ": " + value);
if (value instanceof JSONObject) {
path.add(key);
traverseJson((JSONObject) value, path, level + "." + (path.size() + 1));
path.remove(path.size() - 1);
} else if (value instanceof JSONArray) {
path.add(key);
traverseJsonArray((JSONArray) value, path, level + "." + (path.size() + 1));
path.remove(path.size() - 1);
// 获取组装body类型
String shkd_bodytype = objects[0].getString("shkd_bodytype");
if ("数组".equals(shkd_bodytype)) {
JSONArray jsonArray = new JSONArray();
JSONObject json = new JSONObject();
for (DynamicObject dynamicObject : dynamicObjectCollection) {
valueAssignment(dynamicObject, dynamic, json, floors);
}
jsonArray.add(json);
requestBody = jsonArray.toJSONString();
} else {
JSONObject resultJson = new JSONObject();
processFloor(resultJson, "data", floors.get(0), floors, dynamic);
requestBody = resultJson.toJSONString();
}
}
public static void traverseJsonArray(JSONArray jsonArray, List<String> path, String level) {
for (int i = 0; i < jsonArray.size(); i++) {
Object value = jsonArray.get(i);
if (value instanceof JSONObject) {
path.add("[" + i + "]");
traverseJson((JSONObject) value, path, level + "." + (i + 1));
path.remove(path.size() - 1);
} else if (value instanceof JSONArray) {
path.add("[" + i + "]");
traverseJsonArray((JSONArray) value, path, level + "." + (i + 1));
path.remove(path.size() - 1);
} else {
String fullPath = buildPath(path, "[" + i + "]", level);
System.out.println(fullPath + ": " + value);
}
}
}
public static String buildPath(List<String> path, String key, String level) {
StringBuilder sb = new StringBuilder();
sb.append("层级 ").append(level).append(": ");
for (String s : path) {
sb.append(s).append(".");
}
sb.append(key);
return sb.toString();
logger.info("请求URL{}\n组装请求body{}", objects[0].getString("shkd_url"), requestBody);
String responseBody = pushBill(objects[0], requestBody);
return responseBody;
}
/**
* 遍历JSON对象
* 处理楼层
*
* @param parentJson
* @param parentKey
* @param currentFloor
* @param floors
* @param parentJson 父JSON对象
* @param parentKey 父字段标识
* @param currentFloor 当前层级对象List
* @param floors 所有楼层对象List<List>
* @param dynamic 源单据对象
*/
public static void processFloor(JSONObject parentJson, String parentKey, List<DynamicObject> currentFloor, List<List<DynamicObject>> floors, DynamicObject dynamic) {
private static void processFloor(JSONObject parentJson, String parentKey, List<DynamicObject> currentFloor, List<List<DynamicObject>> floors, DynamicObject dynamic) {
logger.info("进入processFloor方法");
if (currentFloor.isEmpty()) {
logger.info("currentFloor.isEmpty()");
return;
}
@ -263,114 +243,153 @@ public class ApiService {
}
if ("arrayList".equals(currentFloor.get(0).getString("shkd_tartype"))) {
logger.info("进入数组");
JSONArray jsonArray = new JSONArray();
//目前JSONArray都是一层
JSONObject json = new JSONObject();
for (DynamicObject dynamicObject : currentFloor) {
String key = dynamicObject.getString("shkd_tarfield");
Object shkd_soufield = dynamicObject.get("shkd_soufield");
Object value = null;
if (shkd_soufield != null) {
String[] parts = shkd_soufield.toString().split("\\.");
if (parts.length == 1) {
value = dynamic.get(parts[0]);
} else if (parts.length == 2) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection = dynamic.getDynamicObjectCollection(parts[0]);
value = dynamicObjectCollection.get(0).get(parts[1]);
} else {
value = dynamic.getDynamicObject(parts[0]).get(parts[1]);
}
} else if (parts.length == 3) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection = dynamic.getDynamicObjectCollection(parts[0]);
value = dynamicObjectCollection.get(0).getDynamicObject(parts[1]).get(parts[2]);
} else {
value = dynamic.getDynamicObject(parts[0]).getDynamicObject(parts[1]).get(parts[2]);
}
}
} else {
value = dynamicObject.get("shkd_defaultdata");
}
String tartype = dynamicObject.getString("shkd_tartype");
if ("string".equals(tartype) || "date".equals(tartype)) {
json.put(key, value);
} else if ("int".equals(tartype)) {
json.put(key, Integer.parseInt(value.toString()));
} else if ("object".equals(tartype)) {
JSONObject childJson = new JSONObject();
processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, dynamic);
json.put(key, childJson);
} else if ("arrayList".equals(tartype)) {
JSONArray childJsonArray = new JSONArray();
for (DynamicObject childDynamicObject : getChildren(floors, dynamicObject.getString("shkd_tarfield"))) {
JSONObject childJson = new JSONObject();
processFloor(childJson, key, Collections.singletonList(childDynamicObject), floors, dynamic);
childJsonArray.add(childJson);
}
json.put(key, childJsonArray);
}
valueAssignment(dynamicObject, dynamic, json, floors);
}
jsonArray.add(json);
parentJson.put(parentKey, jsonArray);
} else {
logger.info("进入对象");
for (DynamicObject dynamicObject : currentFloor) {
String key = dynamicObject.getString("shkd_tarfield");
Object shkd_soufield = dynamicObject.get("shkd_soufield");
Object value = null;
if (shkd_soufield != null) {
String[] parts = shkd_soufield.toString().split("\\.");
if (parts.length == 1) {
value = dynamic.get(parts[0]);
} else if (parts.length == 2) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection = dynamic.getDynamicObjectCollection(parts[0]);
value = dynamicObjectCollection.get(0).get(parts[1]);
} else {
value = dynamic.getDynamicObject(parts[0]).get(parts[1]);
}
} else if (parts.length == 3) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection = dynamic.getDynamicObjectCollection(parts[0]);
value = dynamicObjectCollection.get(0).getDynamicObject(parts[1]).get(parts[2]);
} else {
value = dynamic.getDynamicObject(parts[0]).getDynamicObject(parts[1]).get(parts[2]);
}
}
} else {
value = dynamicObject.get("shkd_defaultdata");
}
String tartype = dynamicObject.getString("shkd_tartype");
if ("string".equals(tartype) || "date".equals(tartype)) {
parentJson.put(key, value);
} else if ("int".equals(tartype)) {
parentJson.put(key, Integer.parseInt(value.toString()));
} else if ("object".equals(tartype)) {
JSONObject childJson = new JSONObject();
processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, dynamic);
parentJson.put(key, childJson);
} else if ("arrayList".equals(tartype)) {
JSONArray childJsonArray = new JSONArray();
JSONObject childJson = new JSONObject();
processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, dynamic);
childJsonArray.add(childJson);
parentJson.put(key, childJsonArray);
}
valueAssignment(dynamicObject, dynamic, parentJson, floors);
}
}
}
/**
* 获取子节点
* 给value赋值
*
* @param floors
* @param parentKey
* @return
* @param dynamicObject 映射分录的单行数据
* @param billObject 源单数据
* @param jsonObject JSON对象
* @param floors 层集合
*/
public static List<DynamicObject> getChildren(List<List<DynamicObject>> floors, String parentKey) {
private static void valueAssignment(DynamicObject dynamicObject, DynamicObject billObject, JSONObject jsonObject, List<List<DynamicObject>> floors) {
String key = dynamicObject.getString("shkd_tarfield");
Object value = null;
// 将映射字段塞入对应JSON中
Object shkd_soufield = dynamicObject.get("shkd_soufield");
logger.info("shkd_soufield: {}", shkd_soufield);
if (shkd_soufield != null && !"".equals(shkd_soufield)) {
String[] parts = shkd_soufield.toString().split("\\.");
logger.info("billObject{}\nparts: {}", billObject, Arrays.toString(parts));
if (parts.length == 1) {
if ("payeetype".equals(parts[0]) || "payertype".equals(parts[0])) {
String objectType = billObject.getString(parts[0]);
switch (objectType) {
case "bos_org":
value = "4";
break;
case "bd_supplier":
value = "2";
break;
case "bd_customer":
value = "1";
break;
case "bos_user":
value = "3";
break;
case "other":
value = "4";
break;
}
} else if ("debitamount".equals(parts[0])) {
// 付款金额
BigDecimal debitamount = billObject.getBigDecimal("debitamount");
// 收款金额
BigDecimal creditamount = billObject.getBigDecimal("creditamount");
if (debitamount.compareTo(BigDecimal.ZERO) != 0) {
value = billObject.get("debitamount");
} else {
value = billObject.get("creditamount");
}
} else if ("direction".equals(parts[0])) {
// 付款金额
BigDecimal debitamount = billObject.getBigDecimal("debitamount");
// 收款金额
BigDecimal creditamount = billObject.getBigDecimal("creditamount");
if (debitamount.compareTo(BigDecimal.ZERO) != 0) {
value = "1";//支出
} else {
value = "2";//收入
}
} else if ("bizdate".equals(parts[0])) {
Date bizdate = billObject.getDate("bizdate");
// 创建一个SimpleDateFormat对象指定格式为"yyyy-MM-dd"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 格式化Date对象为字符串
value = sdf.format(bizdate);
} else {
value = billObject.get(parts[0]);
}
} else if (parts.length == 2) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection1 = billObject.getDynamicObjectCollection(parts[0]);
value = dynamicObjectCollection1.get(0).get(parts[1]);
} else {
DynamicObject object = billObject.getDynamicObject(parts[0]);
if (object != null) {
value = object.get(parts[1]);
}
}
} else if (parts.length == 3) {
if ("entry".equals(parts[0])) {
DynamicObjectCollection dynamicObjectCollection1 = billObject.getDynamicObjectCollection(parts[0]);
DynamicObject object = dynamicObjectCollection1.get(0).getDynamicObject(parts[1]);
if (object != null) {
value = object.get(parts[2]);
}
} else {
DynamicObject object = billObject.getDynamicObject(parts[0]);
if (object != null) {
DynamicObject object1 = object.getDynamicObject(parts[1]);
if (object1 != null) {
value = object1.get(parts[2]);
}
}
}
}
} else {
value = dynamicObject.get("shkd_defaultdata");
}
String tartype = dynamicObject.getString("shkd_tartype");
if ("String".equals(tartype) || "Date".equals(tartype)) {
jsonObject.put(key, value);
} else if ("Integer".equals(tartype)) {
jsonObject.put(key, Integer.parseInt(value.toString()));
} else if ("BigDecimal".equals(tartype)) {
jsonObject.put(key, new BigDecimal(value.toString()));
} else if ("对象".equals(tartype)) {
JSONObject childJson = new JSONObject();
processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, billObject);
jsonObject.put(key, childJson);
} else if ("数组".equals(tartype)) {
JSONArray childJsonArray = new JSONArray();
JSONObject childJson = new JSONObject();
processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, billObject);
childJsonArray.add(childJson);
jsonObject.put(key, childJsonArray);
}
}
/**
* 获取子对象集合
*
* @param floors 所有层级的所有对象
* @param parentKey 父节点的shkd_tarfield
* @return 子对象集合
*/
private static List<DynamicObject> getChildren(List<List<DynamicObject>> floors, String parentKey) {
List<DynamicObject> children = new ArrayList<>();
for (List<DynamicObject> floor : floors) {
for (DynamicObject dynamicObject : floor) {
@ -382,5 +401,60 @@ public class ApiService {
return children;
}
/**
* 推送单条数据
* @param dataEntity
* @param requestBody
* @return
*/
private static String pushBill(DynamicObject dataEntity, String requestBody) {
//响应数据
String formattedContent;
try {
//域名
String domainName = dataEntity.getString("shkd_domainname");
Map<String, Object> resultMap = ApiService.getBIPToken(domainName);
Object token = resultMap.get("token");
logger.info("获取token{}", token);
// 请求URL
URL url = new URL(dataEntity.getString("shkd_url") + "?access_token=" + token);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法为POST
connection.setRequestMethod("POST");
// 设置请求头
connection.setRequestProperty("Content-Type", "application/json");
// 允许输出
connection.setDoOutput(true);
// 写入请求体
try (OutputStream os = connection.getOutputStream()) {
byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
// 获取响应码
int responseCode = connection.getResponseCode();
// 读取响应内容
if (responseCode == HttpURLConnection.HTTP_OK) { // 成功响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
// 替换 \n 为实际的换行符
formattedContent = content.toString().replace("\\n", "\n").replace("\\", "");
} else {
formattedContent = "响应失败";
}
} catch (Exception e) {
formattedContent = "请求失败," + e.getMessage();
}
return formattedContent;
}
}