package com.product.face.util; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.NumberUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.product.common.lang.StringUtils; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldSetEntity; import com.product.face.config.FaceConst; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * @Author cheng * @Date 2023/5/24 10:42 * @Desc */ public class FaceUtil { public static List getFaceControlList(FieldSetEntity face,DataTableEntity faceControlDt) { List result = new ArrayList<>(); List data = faceControlDt.getData(); Map> collect = data.stream() .filter(item -> StringUtils.isEmpty(item.getString(FaceConst.FIELD_PARENT_UUID))) //过滤 取父级 // .sorted(Comparator.comparing(item -> item.getInteger(FaceConst.CONTROL_SEQUENCE))) //排序 .collect(Collectors.groupingBy(item -> item.getString(FaceConst.FIELD_GROUP_UUID))); //分组 Map> groupContainer = data.stream() .filter(item -> !StringUtils.isEmpty(item.getString(FaceConst.FIELD_PARENT_UUID))) //过滤 取子级 .collect(Collectors.groupingBy(item -> item.getString(FaceConst.FIELD_PARENT_UUID))); //分组 for (Map.Entry> entry : collect.entrySet()) { List propertyList = entry.getValue(); JSONObject propertyJson = getPropertyJson(propertyList, groupContainer); Boolean eventProperty = propertyJson.getBoolean("event_property"); if(eventProperty!=null && eventProperty){ face.setValue("events",propertyJson.get("events")); continue; } result.add(propertyJson); } result.sort(Comparator.comparing(item -> item.getInteger(FaceConst.CONTROL_SEQUENCE))); return result; } private static JSONObject getPropertyJson(List propertyList, Map> groupContainer) { JSONObject property = new JSONObject(); String propertyValue = null; if (!CollectionUtil.isEmpty(propertyList)) { for (FieldSetEntity fs : propertyList) { //属性类型 1 String groupUuid = fs.getUUID(); String propertyType = fs.getString(FaceConst.FIELD_PROPERTY_TYPE); String propertyName = fs.getString(FaceConst.FIELD_PROPERTY_NAME); propertyValue = fs.getString(FaceConst.FIELD_PROPERTY_VALUE); if (StringUtils.equalsAny(propertyName, "renderKey", "formId")) { propertyValue = fs.getString("id"); } if ("componentType".equals(propertyName) && "design".equals(propertyValue)) { continue; } if ("layout".equals(propertyName)) { property.put("defaultValue", null); } switch (propertyType) { case "1"://字符串 property.put(propertyName, propertyValue); break; case "2"://数组 property.put(propertyName, propertyValue.split(",")); break; case "3"://子级对象 try { JSONObject propertyJson = getPropertyJson(groupContainer.get(groupUuid), groupContainer); property.put(propertyName, propertyJson); } catch (Exception e) { e.printStackTrace(); throw e; } break; case "4"://子级数组对象 List propertyListChild = groupContainer.get(groupUuid); if (propertyListChild != null && !propertyListChild.isEmpty()) { List array = propertyListChild.stream() .collect(Collectors.groupingBy(item -> item.getString(FaceConst.FIELD_GROUP_UUID))) .values().stream().map(item -> getPropertyJson(item, groupContainer)).sorted(Comparator.comparing(item -> item.getInteger(FaceConst.CONTROL_SEQUENCE))).collect(Collectors.toList()); property.put(propertyName, new JSONArray(array)); } // groupContainer.put(groupUuid, array); break; case "5": //boolean 类型 Boolean value = null; if ("1".equals(propertyValue)) { value = true; } else if ("0".equals(propertyValue)) { value = false; } property.put(propertyName, value); break; case "6":// int 类型 if (NumberUtil.isNumber(propertyValue)) { property.put(propertyName, NumberUtil.parseInt(propertyValue)); } break; case "7"://double 类型 if (NumberUtil.isDouble(propertyValue)) { property.put(propertyName, NumberUtil.parseDouble(propertyValue)); } break; } } } if (!StringUtils.isEmpty(propertyValue)) { Object o = groupContainer.get(propertyValue); if (o != null) { if (o instanceof JSONArray) { ((JSONArray) o).add(property); } else { ((JSONObject) o).putAll(property); groupContainer.remove(propertyValue); } } } return property; } }