|
|
@@ -12,6 +12,7 @@ import com.zksy.base.enums.EquipmentTypeEnum;
|
|
|
import com.zksy.base.mapper.EquipmentBaseMapper;
|
|
|
import com.zksy.base.mapper.EquipmentTypeMapper;
|
|
|
import com.zksy.base.warning.domain.*;
|
|
|
+import com.zksy.base.warning.mapper.EquipmentWarningTypeMapper;
|
|
|
import com.zksy.base.warning.mapper.WarningTypeMapper;
|
|
|
import com.zksy.base.warning.service.WarningThresholdService;
|
|
|
import com.zksy.common.exception.ServiceException;
|
|
|
@@ -28,24 +29,27 @@ public class WarningThresholdServiceImpl extends ServiceImpl<WarningThresholdMap
|
|
|
implements WarningThresholdService {
|
|
|
private final EquipmentBaseMapper equipmentBaseMapper;
|
|
|
private final EquipmentTypeMapper equipmentTypeMapper;
|
|
|
- private final WarningTypeMapper warningTypeMapper;
|
|
|
+ private final EquipmentWarningTypeMapper warningTypeMapper;
|
|
|
+ private final WarningTypeMapper refWarningTypeMapper;
|
|
|
private final WarningThresholdMapper thresholdMapper;
|
|
|
|
|
|
public WarningThresholdServiceImpl(EquipmentBaseMapper equipmentBaseMapper,
|
|
|
EquipmentTypeMapper equipmentTypeMapper,
|
|
|
- WarningTypeMapper warningTypeMapper,
|
|
|
+ EquipmentWarningTypeMapper warningTypeMapper,
|
|
|
+ WarningTypeMapper refWarningTypeMapper,
|
|
|
WarningThresholdMapper thresholdMapper) {
|
|
|
this.equipmentBaseMapper = equipmentBaseMapper;
|
|
|
this.equipmentTypeMapper = equipmentTypeMapper;
|
|
|
this.warningTypeMapper = warningTypeMapper;
|
|
|
+ this.refWarningTypeMapper = refWarningTypeMapper;
|
|
|
this.thresholdMapper = thresholdMapper;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<EquipmentType> listTypes(Integer rootTypeId) {
|
|
|
Integer root = validRoot(rootTypeId);
|
|
|
- Set<Integer> configuredTypeIds = warningTypeMapper.selectList(Wrappers.lambdaQuery(WarningType.class))
|
|
|
- .stream().map(WarningType::getEquipmentTypeId).filter(Objects::nonNull).collect(Collectors.toSet());
|
|
|
+ Set<Integer> configuredTypeIds = warningTypeMapper.selectList(Wrappers.lambdaQuery(EquipmentWarningType.class))
|
|
|
+ .stream().map(EquipmentWarningType::getEquipmentTypeId).filter(Objects::nonNull).collect(Collectors.toSet());
|
|
|
if (configuredTypeIds.isEmpty()) return Collections.emptyList();
|
|
|
LambdaQueryWrapper<EquipmentType> wrapper = Wrappers.lambdaQuery(EquipmentType.class);
|
|
|
if (root == 0) {
|
|
|
@@ -99,22 +103,26 @@ public class WarningThresholdServiceImpl extends ServiceImpl<WarningThresholdMap
|
|
|
Map<Integer, EquipmentType> typeMap = equipmentTypeMapper.selectBatchIds(
|
|
|
devices.stream().map(d -> Integer.valueOf(d.getEquipmentTypeId())).distinct().collect(Collectors.toList()))
|
|
|
.stream().collect(Collectors.toMap(EquipmentType::getId, Function.identity(), (a, b) -> a));
|
|
|
- Map<Integer, List<WarningType>> metadata = warningTypeMapper.selectList(
|
|
|
- Wrappers.lambdaQuery(WarningType.class).in(WarningType::getEquipmentTypeId,
|
|
|
- typeMap.keySet())).stream().collect(Collectors.groupingBy(WarningType::getEquipmentTypeId));
|
|
|
+ Map<Integer, List<EquipmentWarningType>> metadata = warningTypeMapper.selectList(
|
|
|
+ Wrappers.lambdaQuery(EquipmentWarningType.class).in(EquipmentWarningType::getEquipmentTypeId,
|
|
|
+ typeMap.keySet())).stream().collect(Collectors.groupingBy(EquipmentWarningType::getEquipmentTypeId));
|
|
|
+ Map<Integer, WarningType> wtById = loadWarningTypes(metadata.values().stream().flatMap(List::stream).collect(Collectors.toList()));
|
|
|
|
|
|
List<WarningThresholdView> result = new ArrayList<>();
|
|
|
for (EquipmentBase device : devices) {
|
|
|
WarningThresholdView view = new WarningThresholdView();
|
|
|
view.setEquipmentId(device.getEquipmentId()); view.setEquipmentCode(device.getEquipmentCode());
|
|
|
view.setEquipmentName(device.getEquipmentName()); view.setEquipmentTypeId(device.getEquipmentTypeId() == null ? null : String.valueOf(device.getEquipmentTypeId()));
|
|
|
- EquipmentType type = typeMap.get(Integer.valueOf(device.getEquipmentTypeId()));
|
|
|
+ EquipmentType type = typeMap.get(device.getEquipmentTypeId());
|
|
|
view.setEquipmentTypeName(type == null ? device.getEquipmentTypeName() : type.getTypeName());
|
|
|
- for (WarningType meta : metadata.getOrDefault(Integer.valueOf(device.getEquipmentTypeId()), Collections.emptyList())) {
|
|
|
+ for (EquipmentWarningType meta : metadata.getOrDefault(device.getEquipmentTypeId(), Collections.emptyList())) {
|
|
|
+ WarningType wt = wtById.get(meta.getWarningTypeId());
|
|
|
+ String code = wt == null ? null : wt.getWarningCode();
|
|
|
WarningThresholdItem item = new WarningThresholdItem();
|
|
|
- item.setId(meta.getId()); item.setEquipmentTypeId(meta.getEquipmentTypeId()); item.setWarningCode(meta.getWarningCode());
|
|
|
- item.setWarningName(meta.getWarningName()); item.setWarningUnit(meta.getWarningUnit()); item.setDataType(meta.getDataType()); item.setRemark(meta.getRemark());
|
|
|
- WarningThreshold saved = configuredByKey.get(device.getEquipmentCode() + "\u0000" + meta.getWarningCode());
|
|
|
+ item.setId(meta.getEquipmentTypeId() != null && meta.getWarningTypeId() != null ? meta.getEquipmentTypeId() * 100000 + meta.getWarningTypeId() : null); item.setEquipmentTypeId(meta.getEquipmentTypeId()); item.setWarningCode(code);
|
|
|
+ item.setWarningName(wt == null ? null : wt.getWarningName()); item.setWarningUnit(wt == null ? null : wt.getWarningUnit());
|
|
|
+ item.setDataType(wt == null ? null : wt.getDataType());
|
|
|
+ WarningThreshold saved = configuredByKey.get(device.getEquipmentCode() + "\u0000" + code);
|
|
|
if (Boolean.TRUE.equals(configuredOnly) && saved == null) continue;
|
|
|
if (saved != null) {
|
|
|
item.setMinValue(saved.getMinValue()); item.setMaxValue(saved.getMaxValue());
|
|
|
@@ -168,14 +176,17 @@ public class WarningThresholdServiceImpl extends ServiceImpl<WarningThresholdMap
|
|
|
.filter(Objects::nonNull).map(Integer::valueOf).distinct().collect(Collectors.toList());
|
|
|
Map<Integer, EquipmentType> typeMap = equipmentTypeMapper.selectBatchIds(typeIds).stream()
|
|
|
.collect(Collectors.toMap(EquipmentType::getId, Function.identity(), (a, b) -> a));
|
|
|
- List<WarningType> warningTypes = warningTypeMapper.selectList(
|
|
|
- Wrappers.lambdaQuery(WarningType.class).in(WarningType::getEquipmentTypeId, typeIds));
|
|
|
+ List<EquipmentWarningType> warningTypes = warningTypeMapper.selectList(
|
|
|
+ Wrappers.lambdaQuery(EquipmentWarningType.class).in(EquipmentWarningType::getEquipmentTypeId, typeIds));
|
|
|
+ Map<Integer, WarningType> wtById = loadWarningTypes(warningTypes);
|
|
|
if (warningName != null && !warningName.trim().isEmpty()) {
|
|
|
+ final Map<Integer, WarningType> wtMap = wtById;
|
|
|
warningTypes = warningTypes.stream()
|
|
|
- .filter(type -> containsIgnoreCase(type.getWarningName(), warningName))
|
|
|
+ .filter(type -> { WarningType w = wtMap.get(type.getWarningTypeId()); return w != null && containsIgnoreCase(w.getWarningName(), warningName); })
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
- Set<String> warningCodes = warningTypes.stream().map(WarningType::getWarningCode)
|
|
|
+ Set<String> warningCodes = warningTypes.stream()
|
|
|
+ .map(type -> { WarningType w = wtById.get(type.getWarningTypeId()); return w == null ? null : w.getWarningCode(); })
|
|
|
.filter(Objects::nonNull).collect(Collectors.toSet());
|
|
|
if (warningCodes.isEmpty()) {
|
|
|
emptyPage.setRecords(Collections.emptyList());
|
|
|
@@ -189,15 +200,16 @@ public class WarningThresholdServiceImpl extends ServiceImpl<WarningThresholdMap
|
|
|
.orderByDesc(WarningThreshold::getCreateTime)
|
|
|
.orderByAsc(WarningThreshold::getWarningCode);
|
|
|
Page<WarningThreshold> thresholdPage = thresholdMapper.selectPage(new Page<>(current, size), thresholdWrapper);
|
|
|
- Map<Integer, Map<String, WarningType>> metadata = warningTypes.stream().collect(Collectors.groupingBy(
|
|
|
- WarningType::getEquipmentTypeId,
|
|
|
- Collectors.toMap(WarningType::getWarningCode, Function.identity(), (a, b) -> a)));
|
|
|
+ Map<Integer, Map<String, EquipmentWarningType>> metadata = warningTypes.stream()
|
|
|
+ .filter(type -> { WarningType w = wtById.get(type.getWarningTypeId()); return w != null && w.getWarningCode() != null; })
|
|
|
+ .collect(Collectors.groupingBy(EquipmentWarningType::getEquipmentTypeId,
|
|
|
+ Collectors.toMap(type -> wtById.get(type.getWarningTypeId()).getWarningCode(), Function.identity(), (a, b) -> a)));
|
|
|
List<WarningThresholdListItem> records = new ArrayList<>();
|
|
|
for (WarningThreshold threshold : thresholdPage.getRecords()) {
|
|
|
EquipmentBase device = deviceByCode.get(threshold.getDeviceCode());
|
|
|
if (device == null || device.getEquipmentTypeId() == null) continue;
|
|
|
Integer typeId = Integer.valueOf(device.getEquipmentTypeId());
|
|
|
- WarningType warningType = metadata.getOrDefault(typeId, Collections.emptyMap()).get(threshold.getWarningCode());
|
|
|
+ EquipmentWarningType warningType = metadata.getOrDefault(typeId, Collections.emptyMap()).get(threshold.getWarningCode());
|
|
|
if (warningType == null) continue;
|
|
|
WarningThresholdListItem row = new WarningThresholdListItem();
|
|
|
row.setEquipmentId(device.getEquipmentId());
|
|
|
@@ -206,11 +218,12 @@ public class WarningThresholdServiceImpl extends ServiceImpl<WarningThresholdMap
|
|
|
row.setEquipmentTypeId(device.getEquipmentTypeId() == null ? null : String.valueOf(device.getEquipmentTypeId()));
|
|
|
EquipmentType type = typeMap.get(typeId);
|
|
|
row.setEquipmentTypeName(type == null ? device.getEquipmentTypeName() : type.getTypeName());
|
|
|
- row.setId(warningType.getId());
|
|
|
+ row.setId(warningType.getEquipmentTypeId() != null && warningType.getWarningTypeId() != null ? warningType.getEquipmentTypeId() * 100000 + warningType.getWarningTypeId() : null);
|
|
|
row.setWarningCode(threshold.getWarningCode());
|
|
|
- row.setWarningName(warningType.getWarningName());
|
|
|
- row.setWarningUnit(warningType.getWarningUnit());
|
|
|
- row.setDataType(warningType.getDataType());
|
|
|
+ WarningType wt = wtById.get(warningType.getWarningTypeId());
|
|
|
+ row.setWarningName(wt == null ? null : wt.getWarningName());
|
|
|
+ row.setWarningUnit(wt == null ? null : wt.getWarningUnit());
|
|
|
+ row.setDataType(wt == null ? null : wt.getDataType());
|
|
|
row.setMinValue(threshold.getMinValue());
|
|
|
row.setMaxValue(threshold.getMaxValue());
|
|
|
row.setPeriodStart(threshold.getPeriodStart());
|
|
|
@@ -224,6 +237,18 @@ public class WarningThresholdServiceImpl extends ServiceImpl<WarningThresholdMap
|
|
|
return page;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据 equipment_warning_type.warning_type_id 批量关联 warning_type 主表,
|
|
|
+ * 返回 id -> WarningType 的映射(EquipmentWarningType 已不再冗余存储 warning_code/name/unit/dataType)。
|
|
|
+ */
|
|
|
+ private Map<Integer, WarningType> loadWarningTypes(Collection<EquipmentWarningType> ewts) {
|
|
|
+ Set<Integer> ids = ewts.stream().map(EquipmentWarningType::getWarningTypeId)
|
|
|
+ .filter(Objects::nonNull).collect(Collectors.toSet());
|
|
|
+ if (ids.isEmpty()) return Collections.emptyMap();
|
|
|
+ return refWarningTypeMapper.selectBatchIds(ids).stream()
|
|
|
+ .collect(Collectors.toMap(WarningType::getId, Function.identity(), (a, b) -> a));
|
|
|
+ }
|
|
|
+
|
|
|
private boolean containsIgnoreCase(String value, String keyword) {
|
|
|
return keyword == null || keyword.trim().isEmpty()
|
|
|
|| (value != null && value.toLowerCase(Locale.ROOT).contains(keyword.trim().toLowerCase(Locale.ROOT)));
|
|
|
@@ -244,13 +269,18 @@ public class WarningThresholdServiceImpl extends ServiceImpl<WarningThresholdMap
|
|
|
int count = 0;
|
|
|
for (String code : codes) {
|
|
|
Integer typeId = Integer.valueOf(byCode.get(code).getEquipmentTypeId());
|
|
|
- Map<String, WarningType> metadata = warningTypeMapper.selectList(Wrappers.lambdaQuery(WarningType.class).eq(WarningType::getEquipmentTypeId, typeId)).stream().collect(Collectors.toMap(WarningType::getWarningCode, Function.identity(), (a, b) -> a));
|
|
|
+ List<EquipmentWarningType> ewtList = warningTypeMapper.selectList(Wrappers.lambdaQuery(EquipmentWarningType.class).eq(EquipmentWarningType::getEquipmentTypeId, typeId));
|
|
|
+ Map<Integer, WarningType> wtById = loadWarningTypes(ewtList);
|
|
|
+ Map<String, EquipmentWarningType> metadata = ewtList.stream()
|
|
|
+ .filter(type -> { WarningType w = wtById.get(type.getWarningTypeId()); return w != null && w.getWarningCode() != null; })
|
|
|
+ .collect(Collectors.toMap(type -> wtById.get(type.getWarningTypeId()).getWarningCode(), Function.identity(), (a, b) -> a));
|
|
|
for (WarningThresholdSaveItem input : request.getThresholds()) {
|
|
|
- WarningType meta = metadata.get(input.getWarningCode());
|
|
|
+ EquipmentWarningType meta = metadata.get(input.getWarningCode());
|
|
|
if (meta == null) throw new ServiceException("指标不属于设备类型:" + input.getWarningCode());
|
|
|
- if (meta.getDataType() == 0 && input.getMinValue() != null && input.getMaxValue() != null && input.getMinValue() >= input.getMaxValue()) throw new ServiceException(meta.getWarningName() + "最小值必须小于最大值");
|
|
|
- if (meta.getDataType() == 1) input.setMaxValue(null);
|
|
|
- if (meta.getDataType() == 2) input.setMinValue(null);
|
|
|
+ WarningType wt = wtById.get(meta.getWarningTypeId());
|
|
|
+ if (wt != null && wt.getDataType() == 0 && input.getMinValue() != null && input.getMaxValue() != null && input.getMinValue() >= input.getMaxValue()) throw new ServiceException(wt.getWarningName() + "最小值必须小于最大值");
|
|
|
+ if (wt != null && wt.getDataType() == 1) input.setMaxValue(null);
|
|
|
+ if (wt != null && wt.getDataType() == 2) input.setMinValue(null);
|
|
|
WarningThreshold entity = thresholdMapper.selectOne(Wrappers.lambdaQuery(WarningThreshold.class).eq(WarningThreshold::getDeviceCode, code).eq(WarningThreshold::getWarningCode, input.getWarningCode()).last("limit 1"));
|
|
|
if (entity == null) { entity = new WarningThreshold(); entity.setDeviceCode(code); entity.setWarningCode(input.getWarningCode()); entity.setCreateTime(LocalDateTime.now()); }
|
|
|
entity.setMinValue(input.getMinValue()); entity.setMaxValue(input.getMaxValue()); entity.setUpdateTime(LocalDateTime.now());
|
|
|
@@ -291,8 +321,8 @@ public class WarningThresholdServiceImpl extends ServiceImpl<WarningThresholdMap
|
|
|
if (root == 0) wrapper.in(EquipmentType::getParentId, rootIds());
|
|
|
else wrapper.eq(EquipmentType::getParentId, root);
|
|
|
List<Integer> ids = equipmentTypeMapper.selectList(wrapper).stream().map(EquipmentType::getId).collect(Collectors.toList());
|
|
|
- Set<Integer> configuredTypeIds = warningTypeMapper.selectList(Wrappers.lambdaQuery(WarningType.class))
|
|
|
- .stream().map(WarningType::getEquipmentTypeId).filter(Objects::nonNull).collect(Collectors.toSet());
|
|
|
+ Set<Integer> configuredTypeIds = warningTypeMapper.selectList(Wrappers.lambdaQuery(EquipmentWarningType.class))
|
|
|
+ .stream().map(EquipmentWarningType::getEquipmentTypeId).filter(Objects::nonNull).collect(Collectors.toSet());
|
|
|
ids.retainAll(configuredTypeIds);
|
|
|
if (selectedType != null) { if (!ids.contains(selectedType)) throw new ServiceException("设备类型不属于当前子系统"); return Collections.singleton(String.valueOf(selectedType)); }
|
|
|
return ids.stream().map(String::valueOf).collect(Collectors.toSet());
|