|
@@ -6,7 +6,9 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.zksy.base.domain.EquipmentBase;
|
|
import com.zksy.base.domain.EquipmentBase;
|
|
|
import com.zksy.base.domain.EquipmentPointRel;
|
|
import com.zksy.base.domain.EquipmentPointRel;
|
|
|
|
|
+import com.zksy.base.domain.EquipmentType;
|
|
|
import com.zksy.base.domain.MonitorPoint;
|
|
import com.zksy.base.domain.MonitorPoint;
|
|
|
|
|
+import com.zksy.base.enums.MonitorPointTypeEnum;
|
|
|
import com.zksy.base.mapper.EquipmentBaseMapper;
|
|
import com.zksy.base.mapper.EquipmentBaseMapper;
|
|
|
import com.zksy.base.mapper.EquipmentPointRelMapper;
|
|
import com.zksy.base.mapper.EquipmentPointRelMapper;
|
|
|
import com.zksy.base.mapper.EquipmentTypeMapper;
|
|
import com.zksy.base.mapper.EquipmentTypeMapper;
|
|
@@ -37,7 +39,8 @@ public class MonitorPointServiceImpl extends ServiceImpl<MonitorPointMapper, Mon
|
|
|
private EquipmentTypeMapper equipmentTypeMapper;
|
|
private EquipmentTypeMapper equipmentTypeMapper;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public Page<MonitorPoint> findByPage(long pageNum, long pageSize, String pointCode, String pointName, String associationStatus) {
|
|
|
|
|
|
|
+ public Page<MonitorPoint> findByPage(long pageNum, long pageSize, String pointCode, String pointName,
|
|
|
|
|
+ String associationStatus, Integer pointType) {
|
|
|
Page<MonitorPoint> page = new Page<>(pageNum, pageSize);
|
|
Page<MonitorPoint> page = new Page<>(pageNum, pageSize);
|
|
|
LambdaQueryWrapper<MonitorPoint> queryWrapper = new LambdaQueryWrapper<>();
|
|
LambdaQueryWrapper<MonitorPoint> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
if (pointCode != null && !pointCode.isEmpty()) {
|
|
if (pointCode != null && !pointCode.isEmpty()) {
|
|
@@ -46,6 +49,9 @@ public class MonitorPointServiceImpl extends ServiceImpl<MonitorPointMapper, Mon
|
|
|
if (pointName != null && !pointName.isEmpty()) {
|
|
if (pointName != null && !pointName.isEmpty()) {
|
|
|
queryWrapper.like(MonitorPoint::getPointName, pointName);
|
|
queryWrapper.like(MonitorPoint::getPointName, pointName);
|
|
|
}
|
|
}
|
|
|
|
|
+ if (pointType != null) {
|
|
|
|
|
+ queryWrapper.eq(MonitorPoint::getPointType, pointType);
|
|
|
|
|
+ }
|
|
|
if ("linked".equalsIgnoreCase(associationStatus) || "pending".equalsIgnoreCase(associationStatus)) {
|
|
if ("linked".equalsIgnoreCase(associationStatus) || "pending".equalsIgnoreCase(associationStatus)) {
|
|
|
List<String> linkedPointIds = equipmentPointRelMapper.selectList(new LambdaQueryWrapper<>()).stream()
|
|
List<String> linkedPointIds = equipmentPointRelMapper.selectList(new LambdaQueryWrapper<>()).stream()
|
|
|
.map(EquipmentPointRel::getPointId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
|
|
.map(EquipmentPointRel::getPointId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
|
|
@@ -64,6 +70,7 @@ public class MonitorPointServiceImpl extends ServiceImpl<MonitorPointMapper, Mon
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public boolean saveWithCheck(MonitorPoint entity) {
|
|
public boolean saveWithCheck(MonitorPoint entity) {
|
|
|
|
|
+ validatePointType(entity.getPointType());
|
|
|
LambdaQueryWrapper<MonitorPoint> queryWrapper = new LambdaQueryWrapper<>();
|
|
LambdaQueryWrapper<MonitorPoint> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
queryWrapper.eq(MonitorPoint::getPointCode, entity.getPointCode());
|
|
queryWrapper.eq(MonitorPoint::getPointCode, entity.getPointCode());
|
|
|
long count = this.count(queryWrapper);
|
|
long count = this.count(queryWrapper);
|
|
@@ -77,6 +84,7 @@ public class MonitorPointServiceImpl extends ServiceImpl<MonitorPointMapper, Mon
|
|
|
public boolean saveBatchWithCheck(List<MonitorPoint> entityList) {
|
|
public boolean saveBatchWithCheck(List<MonitorPoint> entityList) {
|
|
|
Set<String> codeSet = new HashSet<>();
|
|
Set<String> codeSet = new HashSet<>();
|
|
|
for (MonitorPoint entity : entityList) {
|
|
for (MonitorPoint entity : entityList) {
|
|
|
|
|
+ validatePointType(entity.getPointType());
|
|
|
String code = entity.getPointCode();
|
|
String code = entity.getPointCode();
|
|
|
if (codeSet.contains(code)) {
|
|
if (codeSet.contains(code)) {
|
|
|
throw new ServiceException("列表中存在重复编码:" + code);
|
|
throw new ServiceException("列表中存在重复编码:" + code);
|
|
@@ -98,9 +106,15 @@ public class MonitorPointServiceImpl extends ServiceImpl<MonitorPointMapper, Mon
|
|
|
@Transactional
|
|
@Transactional
|
|
|
public boolean updateWithCheck(MonitorPoint entity) {
|
|
public boolean updateWithCheck(MonitorPoint entity) {
|
|
|
String pointId = entity.getPointId();
|
|
String pointId = entity.getPointId();
|
|
|
- if (this.getById(pointId) == null) {
|
|
|
|
|
|
|
+ MonitorPoint existingPoint = this.getById(pointId);
|
|
|
|
|
+ if (existingPoint == null) {
|
|
|
throw new ServiceException("监测点不存在,无法修改:" + pointId);
|
|
throw new ServiceException("监测点不存在,无法修改:" + pointId);
|
|
|
}
|
|
}
|
|
|
|
|
+ validatePointType(entity.getPointType());
|
|
|
|
|
+ if (!Objects.equals(existingPoint.getPointType(), entity.getPointType())
|
|
|
|
|
+ && equipmentCount(pointId) > 0) {
|
|
|
|
|
+ throw new ServiceException("监测点已关联设备,不能修改监测点类型,请先解除全部设备关联");
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
String newCode = entity.getPointCode();
|
|
String newCode = entity.getPointCode();
|
|
|
if (newCode != null) {
|
|
if (newCode != null) {
|
|
@@ -174,12 +188,16 @@ public class MonitorPointServiceImpl extends ServiceImpl<MonitorPointMapper, Mon
|
|
|
throw new ServiceException("设备ID列表不能为空");
|
|
throw new ServiceException("设备ID列表不能为空");
|
|
|
}
|
|
}
|
|
|
equipmentIds = new ArrayList<>(new LinkedHashSet<>(equipmentIds));
|
|
equipmentIds = new ArrayList<>(new LinkedHashSet<>(equipmentIds));
|
|
|
|
|
+ Map<Integer, EquipmentType> equipmentTypeMap = loadEquipmentTypeMap();
|
|
|
|
|
|
|
|
for (String equipmentId : equipmentIds) {
|
|
for (String equipmentId : equipmentIds) {
|
|
|
EquipmentBase equipment = equipmentBaseMapper.selectById(equipmentId);
|
|
EquipmentBase equipment = equipmentBaseMapper.selectById(equipmentId);
|
|
|
if (equipment == null) {
|
|
if (equipment == null) {
|
|
|
throw new ServiceException("设备不存在:" + equipmentId);
|
|
throw new ServiceException("设备不存在:" + equipmentId);
|
|
|
}
|
|
}
|
|
|
|
|
+ if (!belongsToPointType(equipment.getEquipmentTypeId(), point.getPointType(), equipmentTypeMap)) {
|
|
|
|
|
+ throw new ServiceException("设备[" + equipment.getEquipmentCode() + "]类型与监测点类型不匹配");
|
|
|
|
|
+ }
|
|
|
LambdaQueryWrapper<EquipmentPointRel> checkWrapper = new LambdaQueryWrapper<>();
|
|
LambdaQueryWrapper<EquipmentPointRel> checkWrapper = new LambdaQueryWrapper<>();
|
|
|
checkWrapper.eq(EquipmentPointRel::getEquipmentId, equipmentId);
|
|
checkWrapper.eq(EquipmentPointRel::getEquipmentId, equipmentId);
|
|
|
List<EquipmentPointRel> existing = equipmentPointRelMapper.selectList(checkWrapper);
|
|
List<EquipmentPointRel> existing = equipmentPointRelMapper.selectList(checkWrapper);
|
|
@@ -243,24 +261,38 @@ public class MonitorPointServiceImpl extends ServiceImpl<MonitorPointMapper, Mon
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public Map<String, Object> statistics() {
|
|
|
|
|
- List<EquipmentPointRel> relations = equipmentPointRelMapper.selectList(new LambdaQueryWrapper<>());
|
|
|
|
|
|
|
+ public Map<String, Object> statistics(Integer pointType) {
|
|
|
|
|
+ LambdaQueryWrapper<MonitorPoint> pointWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ pointWrapper.eq(pointType != null, MonitorPoint::getPointType, pointType);
|
|
|
|
|
+ List<MonitorPoint> points = this.list(pointWrapper);
|
|
|
|
|
+ Set<String> pointIds = points.stream().map(MonitorPoint::getPointId).collect(Collectors.toSet());
|
|
|
|
|
+ List<EquipmentPointRel> relations = pointIds.isEmpty()
|
|
|
|
|
+ ? Collections.emptyList()
|
|
|
|
|
+ : equipmentPointRelMapper.selectList(new LambdaQueryWrapper<EquipmentPointRel>()
|
|
|
|
|
+ .in(EquipmentPointRel::getPointId, pointIds));
|
|
|
Set<String> linkedPointIds = relations.stream().map(EquipmentPointRel::getPointId).filter(Objects::nonNull).collect(Collectors.toSet());
|
|
Set<String> linkedPointIds = relations.stream().map(EquipmentPointRel::getPointId).filter(Objects::nonNull).collect(Collectors.toSet());
|
|
|
Map<String, Object> result = new LinkedHashMap<>();
|
|
Map<String, Object> result = new LinkedHashMap<>();
|
|
|
- result.put("totalPoints", this.count());
|
|
|
|
|
|
|
+ result.put("totalPoints", points.size());
|
|
|
result.put("linkedPoints", linkedPointIds.size());
|
|
result.put("linkedPoints", linkedPointIds.size());
|
|
|
- result.put("pendingPoints", Math.max(0, this.count() - linkedPointIds.size()));
|
|
|
|
|
|
|
+ result.put("pendingPoints", Math.max(0, points.size() - linkedPointIds.size()));
|
|
|
result.put("totalDevices", relations.stream().map(EquipmentPointRel::getEquipmentId).filter(Objects::nonNull).distinct().count());
|
|
result.put("totalDevices", relations.stream().map(EquipmentPointRel::getEquipmentId).filter(Objects::nonNull).distinct().count());
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public List<Map<String, Object>> listEquipmentForAssociation(String pointId) {
|
|
public List<Map<String, Object>> listEquipmentForAssociation(String pointId) {
|
|
|
|
|
+ MonitorPoint point = this.getById(pointId);
|
|
|
|
|
+ if (point == null) {
|
|
|
|
|
+ throw new ServiceException("监测点不存在:" + pointId);
|
|
|
|
|
+ }
|
|
|
Map<String, String> ownerByEquipment = equipmentPointRelMapper.selectList(new LambdaQueryWrapper<>()).stream()
|
|
Map<String, String> ownerByEquipment = equipmentPointRelMapper.selectList(new LambdaQueryWrapper<>()).stream()
|
|
|
.filter(rel -> rel.getEquipmentId() != null)
|
|
.filter(rel -> rel.getEquipmentId() != null)
|
|
|
.collect(Collectors.toMap(EquipmentPointRel::getEquipmentId, EquipmentPointRel::getPointId, (left, right) -> left));
|
|
.collect(Collectors.toMap(EquipmentPointRel::getEquipmentId, EquipmentPointRel::getPointId, (left, right) -> left));
|
|
|
- List<EquipmentBase> devices = equipmentBaseMapper.selectList(null);
|
|
|
|
|
- fillEquipmentTypeNames(devices);
|
|
|
|
|
|
|
+ Map<Integer, EquipmentType> equipmentTypeMap = loadEquipmentTypeMap();
|
|
|
|
|
+ List<EquipmentBase> devices = equipmentBaseMapper.selectList(null).stream()
|
|
|
|
|
+ .filter(device -> belongsToPointType(device.getEquipmentTypeId(), point.getPointType(), equipmentTypeMap))
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ fillEquipmentTypeNames(devices, equipmentTypeMap);
|
|
|
return devices.stream().map(device -> {
|
|
return devices.stream().map(device -> {
|
|
|
Map<String, Object> row = new LinkedHashMap<>();
|
|
Map<String, Object> row = new LinkedHashMap<>();
|
|
|
row.put("equipmentId", device.getEquipmentId());
|
|
row.put("equipmentId", device.getEquipmentId());
|
|
@@ -276,17 +308,44 @@ public class MonitorPointServiceImpl extends ServiceImpl<MonitorPointMapper, Mon
|
|
|
|
|
|
|
|
private void fillEquipmentTypeNames(List<EquipmentBase> devices) {
|
|
private void fillEquipmentTypeNames(List<EquipmentBase> devices) {
|
|
|
if (devices == null || devices.isEmpty()) return;
|
|
if (devices == null || devices.isEmpty()) return;
|
|
|
- Set<Integer> typeIds = devices.stream()
|
|
|
|
|
- .map(EquipmentBase::getEquipmentTypeId)
|
|
|
|
|
- .filter(Objects::nonNull)
|
|
|
|
|
- .collect(Collectors.toSet());
|
|
|
|
|
- if (typeIds.isEmpty()) return;
|
|
|
|
|
- equipmentTypeMapper.selectBatchIds(typeIds).forEach(type -> devices.stream()
|
|
|
|
|
- .filter(device -> Objects.equals(device.getEquipmentTypeId(), type.getId()))
|
|
|
|
|
- .forEach(device -> device.setEquipmentTypeName(type.getTypeName())));
|
|
|
|
|
|
|
+ fillEquipmentTypeNames(devices, loadEquipmentTypeMap());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void fillEquipmentTypeNames(List<EquipmentBase> devices, Map<Integer, EquipmentType> equipmentTypeMap) {
|
|
|
|
|
+ if (devices == null || devices.isEmpty()) return;
|
|
|
|
|
+ devices.forEach(device -> {
|
|
|
|
|
+ EquipmentType type = equipmentTypeMap.get(device.getEquipmentTypeId());
|
|
|
|
|
+ device.setEquipmentTypeName(type == null ? null : type.getTypeName());
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Map<Integer, EquipmentType> loadEquipmentTypeMap() {
|
|
|
|
|
+ return equipmentTypeMapper.selectList(null).stream()
|
|
|
|
|
+ .collect(Collectors.toMap(EquipmentType::getId, type -> type, (left, right) -> left));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean belongsToPointType(Integer equipmentTypeId, Integer pointType,
|
|
|
|
|
+ Map<Integer, EquipmentType> equipmentTypeMap) {
|
|
|
|
|
+ if (pointType == null) return true;
|
|
|
|
|
+ if (equipmentTypeId == null) return false;
|
|
|
|
|
+ Integer currentTypeId = equipmentTypeId;
|
|
|
|
|
+ Set<Integer> visited = new HashSet<>();
|
|
|
|
|
+ while (currentTypeId != null && currentTypeId != 0 && visited.add(currentTypeId)) {
|
|
|
|
|
+ if (Objects.equals(currentTypeId, pointType)) return true;
|
|
|
|
|
+ EquipmentType currentType = equipmentTypeMap.get(currentTypeId);
|
|
|
|
|
+ if (currentType == null) return false;
|
|
|
|
|
+ currentTypeId = currentType.getParentId();
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private long equipmentCount(String pointId) {
|
|
private long equipmentCount(String pointId) {
|
|
|
return equipmentPointRelMapper.selectCount(new LambdaQueryWrapper<EquipmentPointRel>().eq(EquipmentPointRel::getPointId, pointId));
|
|
return equipmentPointRelMapper.selectCount(new LambdaQueryWrapper<EquipmentPointRel>().eq(EquipmentPointRel::getPointId, pointId));
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private void validatePointType(Integer pointType) {
|
|
|
|
|
+ if (!MonitorPointTypeEnum.isValid(pointType)) {
|
|
|
|
|
+ throw new ServiceException("监测点类型无效:" + pointType);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|