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 new file mode 100644 index 0000000..60ee4d3 --- /dev/null +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/form/ApiMappingBillPlugin.java @@ -0,0 +1,231 @@ +package shkd.sys.sys.plugin.form; + +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.entity.datamodel.events.PropertyChangedArgs; +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.sdk.plugin.Plugin; + +import java.math.BigDecimal; +import java.util.*; + +/** + * 动态表单插件 + */ +public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin { + @Override + public void registerListener(EventObject e) { + Toolbar tbmain = this.getView().getControl("tbmain"); + Toolbar advcontoolbarap = this.getView().getControl("advcontoolbarap"); + tbmain.addItemClickListener(this); + advcontoolbarap.addItemClickListener(this); + } + + @Override + public void propertyChanged(PropertyChangedArgs e) { + + } + + @Override + public void itemClick(ItemClickEvent evt) { + String itemKey = evt.getItemKey(); + if ("shkd_generatejson".equals(itemKey)) { + DynamicObject dataEntity = this.getModel().getDataEntity(true); + // 生成JSON + DynamicObjectCollection dynamicObjectCollection = dataEntity.getDynamicObjectCollection("entryentity"); + + // 提取所有层级并存储在 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)); + } + }); + + List> floors = new ArrayList<>(); + for (int i = 1; i <= tiers.size(); i++) { + floors.add(new ArrayList<>()); + } + + // 分层 + for (DynamicObject dynamicObject : dynamicObjectCollection) { + int tier = Integer.parseInt(dynamicObject.getString("shkd_tartier")); + floors.get(tier - 1).add(dynamicObject); + } + + // 获取代码编辑器控件 + CodeEdit codeEdit = this.getView().getControl("shkd_codeeditap"); + // 获取组装body类型 + String shkd_bodytype = dataEntity.getString("shkd_bodytype"); + if ("数组".equals(shkd_bodytype)) { + JSONArray jsonArray = new JSONArray(); + JSONObject json = new JSONObject(); + for (DynamicObject dynamicObject : dynamicObjectCollection) { + 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 ("Integer".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)) { + JSONObject childJson = new JSONObject(); + processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors); + 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); + childJsonArray.add(childJson); + } + json.put(key, childJsonArray); + } + } + jsonArray.add(json); + codeEdit.setText(jsonArray.toJSONString()); + } else { + JSONObject resultJson = new JSONObject(); + processFloor(resultJson, "data", floors.get(0), floors); + codeEdit.setText(resultJson.toJSONString()); + } + } + + if ("shkd_analyzejson".equals(itemKey)) { + CodeEdit codeEdit = this.getView().getControl("shkd_codeeditap"); + String text = codeEdit.getText(); + Object obj = JSON.parse(text); + if (obj instanceof JSONObject) { + JSONObject jsonObject = JSON.parseObject(text); + parseJson(jsonObject, 1, null); + } else if (obj instanceof JSONArray) { + JSONArray jsonArray = JSON.parseArray(text); + parseJson(jsonArray, 1, null); + } + } + } + + private void processFloor(JSONObject parentJson, String parentKey, List currentFloor, List> floors) { + if (currentFloor.isEmpty()) { + return; + } + + if (currentFloor.size() == 1 && "对象".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 ("数组".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 ("Integer".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)) { + JSONObject childJson = new JSONObject(); + processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors); + 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); + 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 ("Integer".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)) { + JSONObject childJson = new JSONObject(); + processFloor(childJson, key, getChildren(floors, dynamicObject.getString("shkd_tarfield")), floors); + parentJson.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); + childJsonArray.add(childJson); + parentJson.put(key, childJsonArray); + } + } + } + } + + private 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; + } + + private void parseJson(Object obj, int level, String parentKey) { + if (obj instanceof JSONObject) { + JSONObject json = (JSONObject) obj; + for (String key : json.keySet()) { + Object value = json.get(key); + String type = value instanceof JSONObject ? "对象" : value instanceof JSONArray ? "数组" : value.getClass().getSimpleName(); + String defaultValue = value instanceof JSONObject || value instanceof JSONArray ? "无" : value.toString(); + setEntryEntity(key, level, type, defaultValue, parentKey); + if (value instanceof JSONObject) { + parseJson(value, level + 1, key); + } else if (value instanceof JSONArray) { + parseJson(value, level + 1, key); + } + } + } else if (obj instanceof JSONArray) { + JSONArray jsonArray = (JSONArray) obj; + for (Object value : jsonArray) { + parseJson(value, level, parentKey); + } + } + } + + + private void setEntryEntity(String key, int level, String type, String def, String pKey) { + this.getModel().batchCreateNewEntryRow("entryentity", 1); + DynamicObjectCollection dynamicObjectCollection = this.getModel().getEntryEntity("entryentity"); + this.getModel().setValue("shkd_tarfield", key, dynamicObjectCollection.size() - 1);// + this.getModel().setValue("shkd_tartier", level, dynamicObjectCollection.size() - 1); + this.getModel().setValue("shkd_tartype", type, dynamicObjectCollection.size() - 1); + this.getModel().setValue("shkd_defaultdata", def, dynamicObjectCollection.size() - 1); + this.getModel().setValue("shkd_parentfield", pKey, dynamicObjectCollection.size() - 1); + } +} \ No newline at end of file