LAPTOP-JI2IUVG1\26646 1 tydzień temu
rodzic
commit
386ddad3e6

+ 2 - 1
pipe-network-service/zksy-system/src/main/java/com/zksy/base/domain/EquipmentType.java

@@ -1,6 +1,7 @@
 package com.zksy.base.domain;
 
 import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.AllArgsConstructor;
@@ -18,7 +19,7 @@ public class EquipmentType {
 
     @TableId(value = "id", type = IdType.ASSIGN_UUID)
     /**
-     * 主键
+     * 数据库主键ID
      */
     private String id;
 

+ 3 - 0
pipe-network-service/zksy-system/src/main/java/com/zksy/base/domain/vo/EquipmentFullVO.java

@@ -40,6 +40,9 @@ public class EquipmentFullVO {
     @ApiModelProperty("设备类别ID")
     private String equipmentTypeId;
 
+    @ApiModelProperty("设备类别名称")
+    private String equipmentTypeName;
+
     @ApiModelProperty("资产价值")
     private Double assetValue;
 

+ 89 - 11
pipe-network-service/zksy-system/src/main/java/com/zksy/base/service/impl/EquipmentBaseServiceImpl.java

@@ -92,9 +92,9 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
     @Override
     public boolean saveWithCheck(EquipmentBase entity) {
         String equipmentTypeId = entity.getEquipmentTypeId();
-        EquipmentType equipmentType = equipmentTypeMapper.selectById(equipmentTypeId);
+        EquipmentType equipmentType = getEquipmentTypeById(equipmentTypeId);
         if (equipmentType == null) {
-            throw new ServiceException("设备类型不存在,请检查equipment_type_id:");
+            throw new ServiceException("设备类型不存在,请检查equipment_type_id:" + equipmentTypeId);
         }
         LambdaQueryWrapper<EquipmentBase> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(EquipmentBase::getEquipmentCode, entity.getEquipmentCode());
@@ -124,7 +124,7 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
         }
 
         for (EquipmentBase entity : entityList) {
-            if (equipmentTypeMapper.selectById(entity.getEquipmentTypeId()) == null) {
+            if (getEquipmentTypeById(entity.getEquipmentTypeId()) == null) {
                 throw new ServiceException("设备类型不存在:" + entity.getEquipmentTypeId());
             }
             LambdaQueryWrapper<EquipmentBase> wrapper = new LambdaQueryWrapper<>();
@@ -145,7 +145,7 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
         }
 
         String newTypeId = entity.getEquipmentTypeId();
-        if (newTypeId != null && equipmentTypeMapper.selectById(newTypeId) == null) {
+        if (newTypeId != null && getEquipmentTypeById(newTypeId) == null) {
             throw new ServiceException("设备类型不存在:" + newTypeId);
         }
 
@@ -279,10 +279,13 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
         if (CollUtil.isNotEmpty(outDTOList)) {
             //查询设备类型
             List<String> equipmentTypeIdList = outDTOList.stream().map(EquipmentBaseOutDTO::getEquipmentTypeId).distinct().collect(Collectors.toList());
-            List<EquipmentType> equipmentTypeList = equipmentTypeMapper.selectBatchIds(equipmentTypeIdList);
+            LambdaQueryWrapper<EquipmentType> batchQw = new LambdaQueryWrapper<>();
+            batchQw.in(EquipmentType::getId, equipmentTypeIdList);
+            List<EquipmentType> equipmentTypeList = equipmentTypeMapper.selectList(batchQw);
             Map<String, EquipmentType> equipmentTypeMap = new HashMap<>();
             if(CollUtil.isNotEmpty(equipmentTypeList)){
-                equipmentTypeMap.putAll(equipmentTypeList.stream().collect(Collectors.toMap(EquipmentType::getId, Function.identity(), (key1, key2) -> key2)));
+                equipmentTypeMap.putAll(equipmentTypeList.stream().collect(
+                    Collectors.toMap(t -> String.valueOf(t.getId()), Function.identity(), (key1, key2) -> key2)));
             }
             //查询设备状态
             List<String> equipmentIdList = outDTOList.stream().map(EquipmentBaseOutDTO::getEquipmentId).distinct().collect(Collectors.toList());
@@ -356,10 +359,13 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
         List<EquipmentBaseOutDTO> outDTOList = BeanUtil.copyToList(equipmentBaseList, EquipmentBaseOutDTO.class);
         //查询设备类型
         List<String> equipmentTypeIdList = outDTOList.stream().map(EquipmentBaseOutDTO::getEquipmentTypeId).distinct().collect(Collectors.toList());
-        List<EquipmentType> equipmentTypeList = equipmentTypeMapper.selectBatchIds(equipmentTypeIdList);
+        LambdaQueryWrapper<EquipmentType> batchQw = new LambdaQueryWrapper<>();
+        batchQw.in(EquipmentType::getId, equipmentTypeIdList);
+        List<EquipmentType> equipmentTypeList = equipmentTypeMapper.selectList(batchQw);
         Map<String, EquipmentType> equipmentTypeMap = new HashMap<>();
         if(CollUtil.isNotEmpty(equipmentTypeList)){
-            equipmentTypeMap.putAll(equipmentTypeList.stream().collect(Collectors.toMap(EquipmentType::getId, Function.identity(), (key1, key2) -> key2)));
+            equipmentTypeMap.putAll(equipmentTypeList.stream().collect(
+                Collectors.toMap(t -> String.valueOf(t.getId()), Function.identity(), (key1, key2) -> key2)));
         }
         //查询设备状态
         List<String> equipmentIdList = outDTOList.stream().map(EquipmentBaseOutDTO::getEquipmentId).distinct().collect(Collectors.toList());
@@ -457,7 +463,7 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
         result.put("equipmentInfo", equipment);
 
         if (equipment.getEquipmentTypeId() != null) {
-            EquipmentType type = equipmentTypeMapper.selectById(equipment.getEquipmentTypeId());
+            EquipmentType type = getEquipmentTypeById(equipment.getEquipmentTypeId());
             result.put("typeInfo", type);
         }
 
@@ -513,7 +519,26 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
             qw.like(EquipmentBase::getManufacturer, manufacturer);
         }
         if (equipmentTypeId != null && !equipmentTypeId.isEmpty()) {
-            qw.eq(EquipmentBase::getEquipmentTypeId, equipmentTypeId);
+            // 查找该类型是否为父类(parent_type_id=0)
+            LambdaQueryWrapper<EquipmentType> typeQw = new LambdaQueryWrapper<>();
+            typeQw.eq(EquipmentType::getTypeId, equipmentTypeId);
+            typeQw.eq(EquipmentType::getParentTypeId, "0");
+            List<EquipmentType> parentTypes = equipmentTypeMapper.selectList(typeQw);
+
+            if (parentTypes != null && !parentTypes.isEmpty()) {
+                // 是父类,查找所有子类型的id
+                EquipmentType parentType = parentTypes.get(0);
+                LambdaQueryWrapper<EquipmentType> childQw = new LambdaQueryWrapper<>();
+                childQw.eq(EquipmentType::getParentTypeId, String.valueOf(parentType.getId()));
+                List<EquipmentType> childTypes = equipmentTypeMapper.selectList(childQw);
+                List<String> typeIds = childTypes.stream()
+                        .map(t -> String.valueOf(t.getId())).collect(Collectors.toList());
+                typeIds.add(String.valueOf(parentType.getId())); // 也包含父类的id
+                qw.in(EquipmentBase::getEquipmentTypeId, typeIds);
+            } else {
+                // 不是父类,直接精确匹配
+                qw.eq(EquipmentBase::getEquipmentTypeId, equipmentTypeId);
+            }
         }
         qw.orderByDesc(EquipmentBase::getCreateTime);
         Page<EquipmentBase> basePage = this.page(page, qw);
@@ -558,6 +583,22 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
                     .collect(Collectors.toMap(PipeNetworkBase::getNetworkId, Function.identity(), (a, b) -> a));
         }
 
+        // 批量查设备类型(用于解析类型名称)
+        List<String> equipmentTypeIdList = equipmentList.stream()
+                .map(EquipmentBase::getEquipmentTypeId)
+                .filter(Objects::nonNull)
+                .distinct().collect(Collectors.toList());
+        Map<String, EquipmentType> equipmentTypeMap = new HashMap<>();
+        if (CollUtil.isNotEmpty(equipmentTypeIdList)) {
+            LambdaQueryWrapper<EquipmentType> batchTypeQw = new LambdaQueryWrapper<>();
+            batchTypeQw.in(EquipmentType::getId, equipmentTypeIdList);
+            List<EquipmentType> equipmentTypeList = equipmentTypeMapper.selectList(batchTypeQw);
+            if (CollUtil.isNotEmpty(equipmentTypeList)) {
+                equipmentTypeMap.putAll(equipmentTypeList.stream().collect(
+                    Collectors.toMap(t -> String.valueOf(t.getId()), Function.identity(), (key1, key2) -> key2)));
+            }
+        }
+
         // 6. 组装VO
         List<EquipmentFullVO> voList = new ArrayList<>();
         for (EquipmentBase equip : equipmentList) {
@@ -599,6 +640,12 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
             }
 
             voList.add(vo);
+
+            // 解析设备类型名称
+            EquipmentType eqType = equipmentTypeMap.get(equip.getEquipmentTypeId());
+            if (eqType != null) {
+                vo.setEquipmentTypeName(eqType.getTypeName());
+            }
         }
 
         Page<EquipmentFullVO> resultPage = new Page<>(pageNum, pageSize, basePage.getTotal());
@@ -663,7 +710,7 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
                 }
             } else {
                 // 无子类别:直接统计该类别的设备数量
-                EquipmentType equipmentType = equipmentTypeMapper.selectById(equipmentTypeId);
+                EquipmentType equipmentType = getEquipmentTypeById(equipmentTypeId);
                 long count = this.count(new LambdaQueryWrapper<EquipmentBase>()
                         .eq(EquipmentBase::getEquipmentTypeId, equipmentTypeId));
                 EquipmentCountByTypeOutDTO outDTO = new EquipmentCountByTypeOutDTO();
@@ -734,6 +781,37 @@ public class EquipmentBaseServiceImpl extends ServiceImpl<EquipmentBaseMapper, E
         return result;
     }
 
+    /**
+     * 根据 typeId 查询设备类型,避免 selectOne 多结果异常
+     * 原因:equipment_type 表中 type_id 存在重复记录,直接 selectById 会触发 TooManyResultsException
+     * @return 找到的第一条记录,或 null
+     */
+    private EquipmentType getEquipmentTypeByTypeId(String typeId) {
+        if (typeId == null) {
+            return null;
+        }
+        LambdaQueryWrapper<EquipmentType> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(EquipmentType::getTypeId, typeId);
+        wrapper.last("LIMIT 1");
+        List<EquipmentType> list = equipmentTypeMapper.selectList(wrapper);
+        return CollUtil.isNotEmpty(list) ? list.get(0) : null;
+    }
+
+    /**
+     * 根据 id 查询设备类型(按数据库主键id查询)
+     * @return 找到的第一条记录,或 null
+     */
+    private EquipmentType getEquipmentTypeById(String id) {
+        if (id == null || id.isEmpty()) {
+            return null;
+        }
+        LambdaQueryWrapper<EquipmentType> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(EquipmentType::getId, id);
+        wrapper.last("LIMIT 1");
+        List<EquipmentType> list = equipmentTypeMapper.selectList(wrapper);
+        return CollUtil.isNotEmpty(list) ? list.get(0) : null;
+    }
+
     @Override
     public List<EquipmentBase> findByTopLevelType(String typeName) {
         EquipmentType topType = equipmentTypeMapper.selectOne(