From 595626abfb3612b30d3a744c08bd7c1293893628 Mon Sep 17 00:00:00 2001 From: zoujiangtao Date: Fri, 22 Nov 2024 16:32:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=BA=EF=BC=9A=E9=82=B9?= =?UTF-8?q?=E6=B1=9F=E6=B6=9B=20=E4=BF=AE=E6=94=B9=E5=86=85=E5=AE=B9?= =?UTF-8?q?=EF=BC=9AAPI=E6=98=A0=E5=B0=84=E5=85=AC=E5=85=B1=E5=8D=95?= =?UTF-8?q?=E6=8D=AE=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=AD=97=E6=AE=B5=E6=98=A0?= =?UTF-8?q?=E5=B0=84=20=E4=BF=AE=E6=94=B9=E6=97=B6=E9=97=B4=EF=BC=9A2024/1?= =?UTF-8?q?1/22?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shkd/sys/sys/mservice/ApiService.java | 71 +++++++-- .../sys/plugin/form/ApiMappingBillPlugin.java | 137 ++++++++++++++---- 2 files changed, 171 insertions(+), 37 deletions(-) diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/ApiService.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/ApiService.java index 368b426..ccb864a 100644 --- a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/ApiService.java +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/mservice/ApiService.java @@ -180,7 +180,7 @@ public class ApiService { floors.get(tier - 1).add(dynamicObject); } JSONObject resultJson = new JSONObject(); - processFloor(resultJson, "data", floors.get(0), floors); + processFloor(resultJson, "data", floors.get(0), floors, dynamic); logger.info("调用接口系统:{}\n调用接口单据:{}\n调用JSON:{}", systemName, dynamic, resultJson); return resultJson; } @@ -224,8 +224,8 @@ public class ApiService { 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("."); + for (String s : path) { + sb.append(s).append("."); } sb.append(key); return sb.toString(); @@ -239,14 +239,14 @@ public class ApiService { * @param currentFloor * @param floors */ - public static void processFloor(JSONObject parentJson, String parentKey, List currentFloor, List> floors) { + public static void processFloor(JSONObject parentJson, String parentKey, List currentFloor, List> floors, DynamicObject dynamic) { 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); + processFloor(parentJson.getJSONObject(parentKey), parentKey, getChildren(floors, currentFloor.get(0).getString("shkd_tarfield")), floors, dynamic); return; } @@ -256,7 +256,31 @@ public class ApiService { JSONObject json = new JSONObject(); for (DynamicObject dynamicObject : currentFloor) { String key = dynamicObject.getString("shkd_tarfield"); - Object value = dynamicObject.get("shkd_defaultdata"); + 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)) { @@ -265,13 +289,13 @@ public class ApiService { 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); + 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); + processFloor(childJson, key, Collections.singletonList(childDynamicObject), floors, dynamic); childJsonArray.add(childJson); } json.put(key, childJsonArray); @@ -282,7 +306,30 @@ public class ApiService { } else { for (DynamicObject dynamicObject : currentFloor) { String key = dynamicObject.getString("shkd_tarfield"); - Object value = dynamicObject.get("shkd_defaultdata"); + 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)) { @@ -291,12 +338,12 @@ public class ApiService { 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); + 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); + processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, dynamic); childJsonArray.add(childJson); parentJson.put(key, childJsonArray); } @@ -322,4 +369,6 @@ public class ApiService { } return children; } + + } diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/form/ApiMappingBillPlugin.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/form/ApiMappingBillPlugin.java index c1761da..1258939 100644 --- a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/form/ApiMappingBillPlugin.java +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/form/ApiMappingBillPlugin.java @@ -1,5 +1,7 @@ package shkd.sys.sys.plugin.form; +import kd.bos.logging.Log; +import kd.bos.logging.LogFactory; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; @@ -10,6 +12,9 @@ import kd.bos.form.control.CodeEdit; import kd.bos.form.control.Toolbar; import kd.bos.form.control.events.ItemClickEvent; import kd.bos.form.plugin.AbstractFormPlugin; +import kd.bos.orm.query.QCP; +import kd.bos.orm.query.QFilter; +import kd.bos.servicehelper.BusinessDataServiceHelper; import kd.sdk.plugin.Plugin; import shkd.sys.sys.common.ApiEntity; import shkd.sys.sys.mservice.ApiService; @@ -21,6 +26,8 @@ import java.util.*; * 动态表单插件 */ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin { + private static final Log logger = LogFactory.getLog(ApiMappingBillPlugin.class); + @Override public void registerListener(EventObject e) { Toolbar tbmain = this.getView().getControl("tbmain"); @@ -41,6 +48,14 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin { // 获取代码编辑器控件 CodeEdit codeEdit = this.getView().getControl("shkd_codeeditap"); if ("shkd_generatejson".equals(itemKey)) { + // 获取想要推送单据编码 + String billNumber = dataEntity.getString("shkd_sourcenumber"); + // 获取想要推送单据类型 + String billMark = dataEntity.getDynamicObject("shkd_sourcebill").getDataEntityType().getName(); + // 获取推送单据对象 + DynamicObject billObject = BusinessDataServiceHelper.loadSingle(billMark, new QFilter("billno", QCP.equals, billNumber).toArray()); + logger.info("获取推送对象 → billObject:{}", billObject); + // 生成JSON DynamicObjectCollection dynamicObjectCollection = dataEntity.getDynamicObjectCollection("entryentity"); @@ -71,7 +86,34 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin { JSONObject json = new JSONObject(); for (DynamicObject dynamicObject : dynamicObjectCollection) { String key = dynamicObject.getString("shkd_tarfield"); - Object value = dynamicObject.get("shkd_defaultdata"); + + // 将映射字段塞入对应JSON中 + 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 = 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 { + value = billObject.getDynamicObject(parts[0]).get(parts[1]); + } + } else if (parts.length == 3) { + if ("entry".equals(parts[0])) { + DynamicObjectCollection dynamicObjectCollection1 = billObject.getDynamicObjectCollection(parts[0]); + value = dynamicObjectCollection1.get(0).getDynamicObject(parts[1]).get(parts[2]); + } else { + value = billObject.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)) { @@ -82,13 +124,13 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin { json.put(key, new BigDecimal(value.toString())); } else if ("对象".equals(tartype)) { JSONObject childJson = new JSONObject(); - processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors); + processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, billObject); json.put(key, childJson); } else if ("数组".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); + processFloor(childJson, key, Collections.singletonList(childDynamicObject), floors, billObject); childJsonArray.add(childJson); } json.put(key, childJsonArray); @@ -98,7 +140,7 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin { codeEdit.setText(jsonArray.toJSONString()); } else { JSONObject resultJson = new JSONObject(); - processFloor(resultJson, "data", floors.get(0), floors); + processFloor(resultJson, "data", floors.get(0), floors, billObject); codeEdit.setText(resultJson.toJSONString()); } } @@ -132,41 +174,63 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin { } } - private void processFloor(JSONObject parentJson, String parentKey, List currentFloor, List> floors) { + private void processFloor(JSONObject parentJson, String parentKey, List currentFloor, List> floors, DynamicObject dynamic) { if (currentFloor.isEmpty()) { return; } - if (currentFloor.size() == 1 && "对象".equals(currentFloor.get(0).getString("shkd_tartype"))) { + 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); + processFloor(parentJson.getJSONObject(parentKey), parentKey, getChildren(floors, currentFloor.get(0).getString("shkd_tarfield")), floors, dynamic); return; } - if ("数组".equals(currentFloor.get(0).getString("shkd_tartype"))) { + 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"); + 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)) { + if ("string".equals(tartype) || "date".equals(tartype)) { json.put(key, value); - } else if ("Integer".equals(tartype)) { + } else if ("int".equals(tartype)) { json.put(key, Integer.parseInt(value.toString())); - } else if ("BigDecimal".equals(tartype)) { - json.put(key, new BigDecimal(value.toString())); - } else if ("对象".equals(tartype)) { + } else if ("object".equals(tartype)) { JSONObject childJson = new JSONObject(); - processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors); + processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, dynamic); json.put(key, childJson); - } else if ("数组".equals(tartype)) { + } 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); + processFloor(childJson, key, Collections.singletonList(childDynamicObject), floors, dynamic); childJsonArray.add(childJson); } json.put(key, childJsonArray); @@ -177,23 +241,44 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin { } else { for (DynamicObject dynamicObject : currentFloor) { String key = dynamicObject.getString("shkd_tarfield"); - Object value = dynamicObject.get("shkd_defaultdata"); + 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)) { + if ("string".equals(tartype) || "date".equals(tartype)) { parentJson.put(key, value); - } else if ("Integer".equals(tartype)) { + } else if ("int".equals(tartype)) { parentJson.put(key, Integer.parseInt(value.toString())); - } else if ("BigDecimal".equals(tartype)) { - parentJson.put(key, new BigDecimal(value.toString())); - } else if ("对象".equals(tartype)) { + } else if ("object".equals(tartype)) { JSONObject childJson = new JSONObject(); - processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors); + processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, dynamic); parentJson.put(key, childJson); - } else if ("数组".equals(tartype)) { + } else if ("arrayList".equals(tartype)) { JSONArray childJsonArray = new JSONArray(); JSONObject childJson = new JSONObject(); - processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors); + processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors, dynamic); childJsonArray.add(childJson); parentJson.put(key, childJsonArray); }