From 3294e5eabf7bab045ed1f0ca563497f7c804814f Mon Sep 17 00:00:00 2001 From: zoujiangtao Date: Tue, 19 Nov 2024 14:58:08 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=BA=EF=BC=9A?= =?UTF-8?q?=E9=82=B9=E6=B1=9F=E6=B6=9B=20=E4=BF=AE=E6=94=B9=E5=86=85?= =?UTF-8?q?=E5=AE=B9=EF=BC=9AAPI=E6=98=A0=E5=B0=84=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E5=8D=95=E6=8D=AE=20=E4=BF=AE=E6=94=B9=E6=97=B6=E9=97=B4?= =?UTF-8?q?=EF=BC=9A2024/11/19?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shkd/sys/sys/mservice/BIPService.java | 215 ++++++++++++++---- 1 file changed, 173 insertions(+), 42 deletions(-) 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; } } From e7961e9bc9b72a8f88e9da7e4ce57ab291e7454c Mon Sep 17 00:00:00 2001 From: zoujiangtao Date: Tue, 19 Nov 2024 15:02:19 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=BA=EF=BC=9A?= =?UTF-8?q?=E9=82=B9=E6=B1=9F=E6=B6=9B=20=E4=BF=AE=E6=94=B9=E5=86=85?= =?UTF-8?q?=E5=AE=B9=EF=BC=9AAPI=E6=98=A0=E5=B0=84=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E5=8D=95=E6=8D=AE=20=E4=BF=AE=E6=94=B9=E6=97=B6=E9=97=B4?= =?UTF-8?q?=EF=BC=9A2024/11/19?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/shkd/sys/sys/mservice/BIPService.java | 1 + 1 file changed, 1 insertion(+) 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 307d4c4..d848993 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 @@ -182,6 +182,7 @@ public class BIPService { } JSONObject resultJson = new JSONObject(); processFloor(resultJson, "data", floors.get(0), floors); + logger.info("调用接口系统:{}\n调用接口单据:{}\n调用JSON:{}", systemName, dynamic, resultJson); return resultJson; } From cfc62e800d045586ec1b007dcdb41a517b2c82f3 Mon Sep 17 00:00:00 2001 From: zoujiangtao Date: Tue, 19 Nov 2024 15:04:30 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=BA=EF=BC=9A?= =?UTF-8?q?=E9=82=B9=E6=B1=9F=E6=B6=9B=20=E4=BF=AE=E6=94=B9=E5=86=85?= =?UTF-8?q?=E5=AE=B9=EF=BC=9AAPI=E6=98=A0=E5=B0=84=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E5=8D=95=E6=8D=AE=20=E4=BF=AE=E6=94=B9=E6=97=B6=E9=97=B4?= =?UTF-8?q?=EF=BC=9A2024/11/19?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sys/mservice/{BIPService.java => ApiService.java} | 5 ++--- .../java/shkd/sys/sys/plugin/form/ApiTestBillPlugin.java | 9 ++++----- 2 files changed, 6 insertions(+), 8 deletions(-) rename sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/{BIPService.java => ApiService.java} (99%) 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/ApiService.java similarity index 99% rename from sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/BIPService.java rename to sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/ApiService.java index d848993..368b426 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/ApiService.java @@ -10,7 +10,6 @@ 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; import javax.crypto.spec.SecretKeySpec; @@ -29,8 +28,8 @@ import java.util.*; * @Author Tao * @Date 2024/11/11 */ -public class BIPService { - private static final Log logger = LogFactory.getLog(BIPService.class); +public class ApiService { + private static final Log logger = LogFactory.getLog(ApiService.class); public static String getBIPToken() { String access_token = null; diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/form/ApiTestBillPlugin.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/form/ApiTestBillPlugin.java index b0be6fd..a397524 100644 --- a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/form/ApiTestBillPlugin.java +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/form/ApiTestBillPlugin.java @@ -1,11 +1,10 @@ package shkd.sys.sys.plugin.form; -import com.alibaba.fastjson.JSONObject; import kd.bos.bill.AbstractBillPlugIn; import kd.bos.form.control.Toolbar; import kd.bos.form.control.events.ItemClickEvent; import kd.sdk.plugin.Plugin; -import shkd.sys.sys.mservice.BIPService; +import shkd.sys.sys.mservice.ApiService; import java.util.EventObject; @@ -33,13 +32,13 @@ public class ApiTestBillPlugin extends AbstractBillPlugIn implements Plugin { String key = evt.getItemKey(); if ("shkd_token".equals(key)) { - String bipToken = BIPService.getBIPToken(); + String bipToken = ApiService.getBIPToken(); this.getView().showTipNotification(bipToken); } if ("shkd_api".equals(key)) { - JSONObject jsonObject = BIPService.paymentSlipsJson(); - this.getView().showTipNotification(jsonObject.toJSONString()); +// JSONObject jsonObject = BIPService.paymentSlipsJson(); +// this.getView().showTipNotification(jsonObject.toJSONString()); } }