刘光辉
5 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package jnpf.flowable.util;
 
import com.alibaba.fastjson.JSON;
import jnpf.base.ActionResult;
import jnpf.base.entity.DictionaryDataEntity;
import jnpf.base.model.datainterface.DataInterfacePage;
import jnpf.base.model.datainterface.DataInterfaceModel;
import jnpf.permission.entity.OrganizeEntity;
import jnpf.permission.entity.PositionEntity;
import jnpf.permission.entity.RoleEntity;
import jnpf.permission.entity.GroupEntity;
import jnpf.permission.entity.UserEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
 
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/** Resolves form component keys before they are written into flow titles. */
@Component
@RequiredArgsConstructor
public class FlowAuditTitleValueResolver implements FlowAuditTitleUtil.ValueResolver {
    private final ServiceUtil serviceUtil;
 
    @Override
    public String resolve(Map<String, Object> field, Map<String, Object> config, Object value,
                          Map<String, Object> row) {
        String raw = FlowAuditTitleUtil.display(value);
        if (raw == null) {
            return null;
        }
        String key = string(config.get("jnpfKey"));
        List<String> ids = values(value);
        switch (key) {
            case "userSelect":
            case "createUser":
            case "modifyUser":
                return names(ids, serviceUtil.getUserName(ids).stream()
                        .collect(Collectors.toMap(UserEntity::getId, UserEntity::getRealName, (a, b) -> a)));
            case "customUserSelect":
                return customUserNames(ids);
            case "comSelect":
            case "depSelect":
            case "currDept":
            case "currOrganize":
                return names(ids, serviceUtil.getOrganizeList(ids).stream()
                        .collect(Collectors.toMap(OrganizeEntity::getId, OrganizeEntity::getFullName, (a, b) -> a)));
            case "posSelect":
            case "currPosition":
                return names(ids, serviceUtil.getPosList(ids).stream()
                        .collect(Collectors.toMap(PositionEntity::getId, PositionEntity::getFullName, (a, b) -> a)));
            case "roleSelect":
                return names(ids, serviceUtil.getRoleList(ids).stream()
                        .collect(Collectors.toMap(RoleEntity::getId, RoleEntity::getFullName, (a, b) -> a)));
            case "groupSelect":
                return names(ids, serviceUtil.getGroupList(ids).stream()
                        .collect(Collectors.toMap(GroupEntity::getId, GroupEntity::getFullName, (a, b) -> a)));
            case "select":
            case "treeSelect":
            case "radio":
            case "checkbox":
            case "cascader":
                return optionNames(field, config, ids, row);
            case "popupSelect":
            case "popupTableSelect":
                return popupNames(field, config, ids, row);
            case "date":
            case "dateCalculate":
            case "createTime":
            case "modifyTime":
                return date(field, value, raw);
            default:
                return raw;
        }
    }
 
    private String customUserNames(List<String> ids) {
        List<String> names = new ArrayList<>();
        for (String value : ids) {
            int separator = value.lastIndexOf("--");
            String id = separator < 0 ? value : value.substring(0, separator);
            String type = separator < 0 ? "user" : value.substring(separator + 2);
            String name;
            switch (type) {
                case "company":
                case "department":
                    name = names(Collections.singletonList(id), serviceUtil.getOrganizeList(Collections.singletonList(id))
                            .stream().collect(Collectors.toMap(OrganizeEntity::getId, OrganizeEntity::getFullName)));
                    break;
                case "position":
                    name = names(Collections.singletonList(id), serviceUtil.getPosList(Collections.singletonList(id))
                            .stream().collect(Collectors.toMap(PositionEntity::getId, PositionEntity::getFullName)));
                    break;
                case "role":
                    name = names(Collections.singletonList(id), serviceUtil.getRoleList(Collections.singletonList(id))
                            .stream().collect(Collectors.toMap(RoleEntity::getId, RoleEntity::getFullName)));
                    break;
                case "group":
                    name = names(Collections.singletonList(id), serviceUtil.getGroupList(Collections.singletonList(id))
                            .stream().collect(Collectors.toMap(GroupEntity::getId, GroupEntity::getFullName)));
                    break;
                default:
                    name = names(Collections.singletonList(id), serviceUtil.getUserName(Collections.singletonList(id))
                            .stream().collect(Collectors.toMap(UserEntity::getId, UserEntity::getRealName)));
            }
            if (name != null) {
                names.add(name);
            }
        }
        return names.isEmpty() ? null : String.join(",", names);
    }
 
    private String optionNames(Map<String, Object> field, Map<String, Object> config,
                               List<String> ids, Map<String, Object> row) {
        String type = string(config.get("dataType"));
        List<Map<String, Object>> options;
        if ("dictionary".equals(type)) {
            List<DictionaryDataEntity> dictionary = serviceUtil.getDictionaryData(string(config.get("dictionaryType")));
            Map<String, String> labels = new LinkedHashMap<>();
            for (DictionaryDataEntity item : dictionary) {
                labels.put(item.getId(), item.getFullName());
                labels.put(item.getEnCode(), item.getFullName());
            }
            return names(ids, labels);
        }
        if ("dynamic".equals(type)) {
            ActionResult<Object> result = serviceUtil.infoToId(string(config.get("propsUrl")), parameters(config, row));
            options = result != null && result.getData() instanceof List
                    ? maps((List<?>) result.getData()) : Collections.emptyList();
        } else {
            options = maps(list(field.get("options")));
        }
        Map<String, Object> props = map(field.get("props"));
        String valueKey = string(props.getOrDefault("value", "id"));
        String labelKey = string(props.getOrDefault("label", "fullName"));
        Map<String, String> labels = new LinkedHashMap<>();
        collectOptions(options, valueKey, labelKey, string(props.get("children")), labels);
        return names(ids, labels);
    }
 
    private String popupNames(Map<String, Object> field, Map<String, Object> config,
                              List<String> ids, Map<String, Object> row) {
        String interfaceId = string(field.get("interfaceId"));
        if (interfaceId.isEmpty()) {
            return null;
        }
        DataInterfacePage page = new DataInterfacePage();
        page.setIds(ids);
        page.setPropsValue(string(field.get("propsValue")));
        page.setRelationField(string(field.get("relationField")));
        List<DataInterfaceModel> params = new ArrayList<>();
        for (Object item : list(config.get("templateJson"))) {
            Map<String, Object> parameter = map(item);
            DataInterfaceModel model = JSON.parseObject(JSON.toJSONString(parameter), DataInterfaceModel.class);
            model.setDefaultValue(parameterValue(parameter, row));
            params.add(model);
        }
        page.setParamList(params);
        List<Map<String, Object>> records = serviceUtil.getInterfaceRows(interfaceId, page);
        Map<String, String> labels = new LinkedHashMap<>();
        for (Map<String, Object> record : records) {
            Object id = record.get(page.getPropsValue());
            Object label = record.get(page.getRelationField());
            if (id != null && label != null) {
                labels.put(String.valueOf(id), String.valueOf(label));
            }
        }
        return names(ids, labels);
    }
 
    private static Map<String, String> parameters(Map<String, Object> config, Map<String, Object> row) {
        Map<String, String> result = new LinkedHashMap<>();
        for (Object item : list(config.get("templateJson"))) {
            Map<String, Object> parameter = map(item);
            String field = string(parameter.get("field"));
            if (!field.isEmpty()) {
                result.put(field, parameterValue(parameter, row));
            }
        }
        return result;
    }
 
    private static String parameterValue(Map<String, Object> parameter, Map<String, Object> row) {
        Object source = parameter.get("sourceType");
        if (source != null && !"1".equals(String.valueOf(source))) {
            return string(parameter.get("defaultValue"));
        }
        return string(row.get(string(parameter.get("relationField"))));
    }
 
    private static String date(Map<String, Object> field, Object value, String fallback) {
        if (!(value instanceof Number) && !fallback.matches("\\d{10,13}")) {
            return fallback;
        }
        try {
            long timestamp = Long.parseLong(fallback);
            if (fallback.length() == 10) {
                timestamp *= 1000;
            }
            String format = string(field.get("format"));
            if (format.isEmpty()) {
                format = "yyyy-MM-dd HH:mm:ss";
            }
            LocalDateTime date = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
            return date.format(DateTimeFormatter.ofPattern(format));
        } catch (RuntimeException ignored) {
            return fallback;
        }
    }
 
    private static void collectOptions(List<Map<String, Object>> options, String valueKey, String labelKey,
                                       String childrenKey, Map<String, String> labels) {
        for (Map<String, Object> option : options) {
            Object value = option.get(valueKey);
            Object label = option.get(labelKey);
            if (value != null && label != null) {
                labels.put(String.valueOf(value), String.valueOf(label));
            }
            if (!childrenKey.isEmpty()) {
                collectOptions(maps(list(option.get(childrenKey))), valueKey, labelKey, childrenKey, labels);
            }
        }
    }
 
    private static String names(List<String> ids, Map<String, String> labels) {
        List<String> names = new ArrayList<>();
        for (String id : ids) {
            String name = labels.get(id);
            if (name != null && !name.trim().isEmpty()) {
                names.add(name);
            }
        }
        return names.isEmpty() ? null : String.join(",", names);
    }
 
    private static List<String> values(Object value) {
        List<?> list = value instanceof List ? (List<?>) value : list(value);
        if (list.isEmpty() && value != null) {
            list = Collections.singletonList(value);
        }
        List<String> result = new ArrayList<>();
        for (Object item : list) {
            if (item != null && !(item instanceof Map) && !String.valueOf(item).trim().isEmpty()) {
                result.add(String.valueOf(item).trim());
            }
        }
        return result;
    }
 
    private static List<?> list(Object value) {
        if (value instanceof List) {
            return (List<?>) value;
        }
        if (value instanceof String && ((String) value).trim().startsWith("[")) {
            try {
                return JSON.parseArray((String) value);
            } catch (RuntimeException ignored) {
                return Collections.emptyList();
            }
        }
        return Collections.emptyList();
    }
 
    @SuppressWarnings("unchecked")
    private static Map<String, Object> map(Object value) {
        return value instanceof Map ? (Map<String, Object>) value : Collections.emptyMap();
    }
 
    private static List<Map<String, Object>> maps(List<?> values) {
        List<Map<String, Object>> result = new ArrayList<>();
        for (Object value : values) {
            if (value instanceof Map) {
                result.add(map(value));
            }
        }
        return result;
    }
 
    private static String string(Object value) {
        return value == null ? "" : String.valueOf(value);
    }
}