刘光辉
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
package jnpf.dmsService;
 
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jnpf.dmsEntity.DmsJiluDengjiPeizhi;
import jnpf.dmsMapper.DmsJiluDengjiPeizhiMapper;
import jnpf.dmsPermission.DmsCurrentIdentity;
import jnpf.exception.DataException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
@Service
@RequiredArgsConstructor
public class DmsJiluShuxingService {
    private final DmsJiluDengjiPeizhiMapper mapper;
    private final DmsCurrentIdentity identity;
    private final ObjectMapper json;
 
    public DmsJiluDengjiPeizhi resolve(String type, String grade) throws DataException {
        if (type == null || type.isBlank() || grade == null || grade.isBlank()) {
            throw new DataException("请选择记录类型和等级");
        }
        DmsJiluDengjiPeizhi match = null;
        for (DmsJiluDengjiPeizhi config : mapper.selectEnabledByType(identity.tenantId(), type.trim())) {
            JsonNode grades;
            try {
                grades = json.readTree(config.getJiluDengji());
                if (grades == null || !grades.isArray()) throw new IllegalArgumentException();
                for (JsonNode item : grades) {
                    if (!item.isTextual()) throw new IllegalArgumentException();
                }
            } catch (Exception error) {
                throw new DataException("记录等级配置的等级集合格式错误:" + config.getId());
            }
            boolean contains = false;
            for (JsonNode item : grades) contains |= grade.trim().equals(item.textValue());
            if (!contains) continue;
            if (match != null) throw new DataException("记录类型和等级匹配到多条有效配置,请修正重叠配置");
            match = config;
        }
        if (match == null) {
            // No matching configuration is a valid uncontrolled snapshot, without a fabricated source ID.
            DmsJiluDengjiPeizhi fallback = new DmsJiluDengjiPeizhi();
            fallback.setJiluLeixing(type.trim());
            fallback.setJiluDengji(json.createArrayNode().add(grade.trim()).toString());
            fallback.setShifouShoukong("no");
            return fallback;
        }
        if ((!"yes".equals(match.getShifouShoukong()) && !"no".equals(match.getShifouShoukong()))
                || match.getVersion() == null) {
            throw new DataException("记录等级配置的是否受控或版本基线无效");
        }
        return match;
    }
}