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; } }