刘光辉
4 小时以前 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
package jnpf.dmsService;
 
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jnpf.exception.DataException;
import jnpf.permission.UserApi;
import jnpf.permission.entity.UserEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.*;
 
/** Snapshots native form personnel fields at registration; workflow configuration owns role assignment. */
@Service
@RequiredArgsConstructor
@Slf4j
public class DmsApprovedVersionPropertiesService {
    private final UserApi users;
    private final ObjectMapper json;
 
    public JsonNode create(String preparedBy, String storedReviewerIds) {
        LinkedHashSet<String> reviewerIds = reviewerIds(storedReviewerIds);
        Set<String> ids = new LinkedHashSet<>(reviewerIds);
        if (StringUtils.hasText(preparedBy)) ids.add(preparedBy);
        Map<String, String> names = new HashMap<>();
        if (!ids.isEmpty()) {
            List<UserEntity> found = lookupNames(ids);
            for (UserEntity user : found) {
                if (user != null && ids.contains(user.getId()))
                    names.put(user.getId(), user.getRealName() == null ? "" : user.getRealName());
            }
        }
        ObjectNode result = json.createObjectNode();
        result.put("prepared_by_name", names.getOrDefault(preparedBy, ""));
        var reviewers = result.putArray("reviewers");
        for (String id : reviewerIds) reviewers.addObject().put("user_id", id).put("name", names.getOrDefault(id, ""));
        return result;
    }
 
    private List<UserEntity> lookupNames(Set<String> ids) {
        // Names are optional display snapshots; directory availability must not block registration.
        try {
            List<UserEntity> found = users.getUserName(new ArrayList<>(ids));
            return found == null ? List.of() : found;
        } catch (RuntimeException e) {
            log.warn("版本人员姓名查询失败,保留人员ID继续登记,异常类型:{}", e.getClass().getSimpleName());
            return List.of();
        }
    }
 
    private LinkedHashSet<String> reviewerIds(String stored) {
        LinkedHashSet<String> ids = new LinkedHashSet<>();
        if (!StringUtils.hasText(stored)) return ids;
        String valueText = stored.trim();
        // Workflow operation mapping writes the current operator ID as a scalar.
        // Keep the version/API contract as an array and accept existing multi-user values.
        if (!valueText.startsWith("[")) {
            if ("null".equals(valueText) || valueText.matches(".*[\\[\\]{}\",].*"))
                throw new DataException("审核人字段必须为人员ID或人员ID数组");
            ids.add(valueText);
            return ids;
        }
        try {
            // Native multi-user fields are persisted as JSON arrays by FlowFormDataUtil.
            JsonNode value = json.readTree(stored);
            if (value == null || !value.isArray()) throw new DataException("审核人字段必须为人员ID数组");
            for (JsonNode id : value) {
                if (!id.isTextual() || !StringUtils.hasText(id.textValue()))
                    throw new DataException("审核人字段包含无效人员ID");
                ids.add(id.textValue().trim());
            }
            return ids;
        } catch (java.io.IOException e) {
            throw new DataException("审核人字段格式不正确");
        }
    }
 
}