diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/BIPService.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/BIPService.java index dc933e7..307d4c4 100644 --- a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/BIPService.java +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/BIPService.java @@ -3,8 +3,13 @@ package shkd.sys.sys.mservice; import com.alibaba.fastjson.JSON; 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.logging.Log; import kd.bos.logging.LogFactory; +import kd.bos.orm.query.QCP; +import kd.bos.orm.query.QFilter; +import kd.bos.servicehelper.BusinessDataServiceHelper; import shkd.sys.sys.common.ApiEntity; import javax.crypto.Mac; @@ -17,9 +22,7 @@ import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.security.cert.X509Certificate; -import java.util.Base64; -import java.util.HashMap; -import java.util.Map; +import java.util.*; /** * @Description @@ -139,56 +142,184 @@ public class BIPService { /** * 组装推送BIP付款单JSON * + * @param dynamic 某个单据的 某条需要推送的数据 + * @param systemName 推送系统名称 * @return */ - public static JSONObject paymentSlipsJson() { - ApiEntity apiEntity = new ApiEntity(); + public static JSONObject paymentSlipsJson(DynamicObject dynamic, String systemName) { + // 获取推送的单据标识 + String billMark = dynamic.getDataEntityType().getName(); - JSONObject root = new JSONObject(); + DynamicObject[] objects = BusinessDataServiceHelper.load("shkd_apimapping", "id,billno," + + "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", + new QFilter("shkd_sourcebill", QCP.equals, billMark).and( + new QFilter("shkd_targetsystem", QCP.equals, systemName)).toArray()); - // 创建"data"节点 - JSONObject data = new JSONObject(); + // 获取数据表信息 + DynamicObjectCollection dynamicObjectCollection = objects[0].getDynamicObjectCollection("entryentity"); - // 设置"data"节点的属性 - data.put("accentity_code", "SIG1040100"); - data.put("vouchdate", "2022-04-20"); - data.put("tradetype_code", "cmp_fund_payment_other"); - data.put("exchangeRateType_code", "01"); - data.put("exchRate", 1); - data.put("currency_code", "CNY"); - data.put("_status", "Insert"); + // 提取所有层级并存储在 Set 中 + Set tiers = new HashSet<>(); + dynamicObjectCollection.forEach(dynamicObject -> { + String shkd_tartier = dynamicObject.getString("shkd_tartier"); + if (shkd_tartier != null && !shkd_tartier.isEmpty()) { + tiers.add(Integer.parseInt(shkd_tartier)); + } + }); - // 创建"FundPayment_b"列表 - JSONArray fundPaymentList = new JSONArray(); + List> floors = new ArrayList<>(); + // 目前定的4层,之后需要弄成动态的 + for (int i = 1; i <= tiers.size(); i++) { + floors.add(new ArrayList<>()); + } - // 创建"FundPayment_b"列表中的第一个对象 - JSONObject fundPayment = new JSONObject(); - fundPayment.put("quickType_code", "6"); - fundPayment.put("settlestatus", "1"); - fundPayment.put("oriSum", 5000); - fundPayment.put("caobject", 2); - fundPayment.put("oppositeobjectname", "11510107MB1526717D"); - fundPayment.put("_status", "Insert"); + // 分层 + for (DynamicObject dynamicObject : dynamicObjectCollection) { + 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); + return resultJson; + } - // 将"FundPayment_b"对象添加到列表中 - fundPaymentList.add(fundPayment); - // 将"FundPayment_b"列表添加到"data"节点中 - data.put("FundPayment_b", fundPaymentList); - // 将"data"节点添加到根JSON对象中 - root.put("data", data); + public static void traverseJson(JSONObject jsonObject, List path, String level) { + for (String key : jsonObject.keySet()) { + Object value = jsonObject.get(key); + String fullPath = buildPath(path, key, level); + System.out.println(fullPath + ": " + value); - apiEntity.setURL(""); - apiEntity.setRequestBody(root); - apiEntity.setMethod("POST"); + 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); + } + } + } - Map queryParams = new HashMap<>(); - queryParams.put("access_token", getBIPToken()); - apiEntity.setQueryParams(queryParams); + public static void traverseJsonArray(JSONArray jsonArray, List 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); + } + } + } - Map headers = new HashMap<>(); - headers.put("Content-Type", "application/json"); - apiEntity.setHeaders(headers); + public static String buildPath(List path, String key, String level) { + StringBuilder sb = new StringBuilder(); + sb.append("层级 ").append(level).append(": "); + for (int i = 0; i < path.size(); i++) { + sb.append(path.get(i)).append("."); + } + sb.append(key); + return sb.toString(); + } - return ApiEntity.getResponseBody(apiEntity); + /** + * 遍历JSON对象 + * + * @param parentJson + * @param parentKey + * @param currentFloor + * @param floors + */ + public static void processFloor(JSONObject parentJson, String parentKey, List currentFloor, List> floors) { + if (currentFloor.isEmpty()) { + return; + } + + if (currentFloor.size() == 1 && "object".equals(currentFloor.get(0).getString("shkd_tartype"))) { + parentJson.put(parentKey, new JSONObject()); + processFloor(parentJson.getJSONObject(parentKey), parentKey, getChildren(floors, currentFloor.get(0).getString("shkd_tarfield")), floors); + return; + } + + if ("arrayList".equals(currentFloor.get(0).getString("shkd_tartype"))) { + JSONArray jsonArray = new JSONArray(); + //目前JSONArray都是一层 + JSONObject json = new JSONObject(); + for (DynamicObject dynamicObject : currentFloor) { + String key = dynamicObject.getString("shkd_tarfield"); + Object 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); + 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); + childJsonArray.add(childJson); + } + json.put(key, childJsonArray); + } + } + jsonArray.add(json); + parentJson.put(parentKey, jsonArray); + } else { + for (DynamicObject dynamicObject : currentFloor) { + String key = dynamicObject.getString("shkd_tarfield"); + Object 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); + 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); + childJsonArray.add(childJson); + parentJson.put(key, childJsonArray); + } + } + } + } + + /** + * 获取子节点 + * + * @param floors + * @param parentKey + * @return + */ + public static List getChildren(List> floors, String parentKey) { + List children = new ArrayList<>(); + for (List floor : floors) { + for (DynamicObject dynamicObject : floor) { + if (parentKey.equals(dynamicObject.getString("shkd_parentfield"))) { + children.add(dynamicObject); + } + } + } + return children; } }