许鹏程
2025-02-17 99c693af3da1391d00872f89ebd18fcc85278200
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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<JSONObject> getFaceControlList(FieldSetEntity face, DataTableEntity faceControlDt) {
        List<JSONObject> result = new ArrayList<>();
        List<FieldSetEntity> data = faceControlDt.getData();
        Map<String, List<FieldSetEntity>> 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<String, List<FieldSetEntity>> groupContainer = data.stream()
                .filter(item -> !StringUtils.isEmpty(item.getString(FaceConst.FIELD_PARENT_UUID))) //过滤 取子级
                .collect(Collectors.groupingBy(item -> item.getString(FaceConst.FIELD_PARENT_UUID))); //分组
        int maxFormId = 100;
        for (Map.Entry<String, List<FieldSetEntity>> entry : collect.entrySet()) {
            List<FieldSetEntity> propertyList = entry.getValue();
            JSONObject propertyJson = getPropertyJson(propertyList, groupContainer);
            Integer o = propertyJson.getInteger("~formId~");
            if (o != null && o > maxFormId) {
                maxFormId = o;
            }
            propertyJson.remove("~formId~");
            Boolean eventProperty = propertyJson.getBoolean("event_property");
            if (eventProperty != null && eventProperty && propertyJson.get("event_array") != null) {
                JSONArray eventArray = propertyJson.getJSONArray("event_array");
                for (int i = 0; i < eventArray.size(); i++) {
                    String params = eventArray.getJSONObject(i).getString("params");
                    String[] paramsArray;
                    if (StringUtils.isEmpty(params)) {
                        paramsArray = new String[]{};
                    } else {
                        paramsArray = params.split(",");
                    }
                    eventArray.getJSONObject(i).put("params", paramsArray);
                }
                face.setValue("events", eventArray);
                continue;
            }
            result.add(propertyJson);
        }
        result.sort(Comparator.comparing(item -> item.getInteger(FaceConst.CONTROL_SEQUENCE)));
        face.setValue("maxFormId", maxFormId);
        return result;
    }
 
    public static JSONObject getPropertyJson(List<FieldSetEntity> propertyList, Map<String, List<FieldSetEntity>> 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)) {
                    //影响表单设计器还原input配置的默认值 暂时先注释掉
//                    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);
                            if ("__config__".equals(propertyName)) {
                                String o = propertyJson.getString("formId");
                                if (NumberUtil.isNumber(o)) {
                                    Integer formId = property.getInteger("formId");
                                    if (formId == null || formId < NumberUtil.parseInt(o)) {
                                        property.put("~formId~", NumberUtil.parseInt(o));
                                    }
                                }
                                propertyJson.remove("~formId~");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            throw e;
                        }
                        break;
                    case "4"://子级数组对象
                        List<FieldSetEntity> propertyListChild = groupContainer.get(groupUuid);
                        if (propertyListChild != null && !propertyListChild.isEmpty()) {
                            List<Object> array = propertyListChild.stream()
                                    .collect(Collectors.groupingBy(item -> item.getString(FaceConst.FIELD_GROUP_UUID)))
                                    .values().stream().map(item -> {
                                        JSONObject propertyJson = getPropertyJson(item, groupContainer);
                                        String o = propertyJson.getString("~formId~");
                                        if (NumberUtil.isNumber(o)) {
                                            Integer formId = property.getInteger("formId");
                                            if (formId == null || formId < NumberUtil.parseInt(o)) {
                                                property.put("~formId~", NumberUtil.parseInt(o));
                                            }
                                        }
                                        propertyJson.remove("~formId~");
 
                                        return propertyJson;
                                    }).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;
    }
 
 
}