Commit 801a6b970eeaf2ba31778d8cd6a37181a9809407

Authored by YunaiV
1 parent da60e649

feat:【IoT 物联网】优化设备数量统计逻辑,简化查询方法并返回更直观的结果映射

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/device/IotDeviceMapper.java
@@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; @@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
6 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; 6 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
7 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.device.IotDevicePageReqVO; 7 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.device.IotDevicePageReqVO;
8 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; 8 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
  9 +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
9 import jakarta.annotation.Nullable; 10 import jakarta.annotation.Nullable;
10 import org.apache.ibatis.annotations.Mapper; 11 import org.apache.ibatis.annotations.Mapper;
11 12
@@ -13,6 +14,7 @@ import java.time.LocalDateTime; @@ -13,6 +14,7 @@ import java.time.LocalDateTime;
13 import java.util.Collection; 14 import java.util.Collection;
14 import java.util.List; 15 import java.util.List;
15 import java.util.Map; 16 import java.util.Map;
  17 +import java.util.stream.Collectors;
16 18
17 /** 19 /**
18 * IoT 设备 Mapper 20 * IoT 设备 Mapper
@@ -80,19 +82,33 @@ public interface IotDeviceMapper extends BaseMapperX<IotDeviceDO> { @@ -80,19 +82,33 @@ public interface IotDeviceMapper extends BaseMapperX<IotDeviceDO> {
80 } 82 }
81 83
82 /** 84 /**
83 - * 查询指定产品下各状态的设备数量 85 + * 查询指定产品下的设备数量
84 * 86 *
85 - * @return 设备数量统计列表 87 + * @return 产品编号 -> 设备数量的映射
86 */ 88 */
87 - // TODO @super:通过 mybatis-plus 来写哈,然后返回 Map 貌似就行了?!  
88 - List<Map<String, Object>> selectDeviceCountMapByProductId(); 89 + default Map<Long, Integer> selectDeviceCountMapByProductId() {
  90 + List<Map<String, Object>> result = selectMaps(new QueryWrapper<IotDeviceDO>()
  91 + .select("product_id AS productId", "COUNT(1) AS deviceCount")
  92 + .groupBy("product_id"));
  93 + return result.stream().collect(Collectors.toMap(
  94 + map -> Long.valueOf(map.get("productId").toString()),
  95 + map -> Integer.valueOf(map.get("deviceCount").toString())
  96 + ));
  97 + }
89 98
90 - // TODO @super:通过 mybatis-plus 来写哈,然后返回 Map 貌似就行了?!  
91 /** 99 /**
92 * 查询各个状态下的设备数量 100 * 查询各个状态下的设备数量
93 * 101 *
94 - * @return 设备数量统计列表 102 + * @return 设备状态 -> 设备数量的映射
95 */ 103 */
96 - List<Map<String, Object>> selectDeviceCountGroupByState(); 104 + default Map<Integer, Long> selectDeviceCountGroupByState() {
  105 + List<Map<String, Object>> result = selectMaps(new QueryWrapper<IotDeviceDO>()
  106 + .select("state", "COUNT(1) AS deviceCount")
  107 + .groupBy("state"));
  108 + return result.stream().collect(Collectors.toMap(
  109 + map -> Integer.valueOf(map.get("state").toString()),
  110 + map -> Long.valueOf(map.get("deviceCount").toString())
  111 + ));
  112 + }
97 113
98 } 114 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java
@@ -36,7 +36,6 @@ import org.springframework.validation.annotation.Validated; @@ -36,7 +36,6 @@ import org.springframework.validation.annotation.Validated;
36 import javax.annotation.Nullable; 36 import javax.annotation.Nullable;
37 import java.time.LocalDateTime; 37 import java.time.LocalDateTime;
38 import java.util.*; 38 import java.util.*;
39 -import java.util.stream.Collectors;  
40 39
41 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; 40 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
42 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; 41 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
@@ -430,25 +429,14 @@ public class IotDeviceServiceImpl implements IotDeviceService { @@ -430,25 +429,14 @@ public class IotDeviceServiceImpl implements IotDeviceService {
430 return deviceMapper.selectCountByCreateTime(createTime); 429 return deviceMapper.selectCountByCreateTime(createTime);
431 } 430 }
432 431
433 - // TODO @super:简化  
434 @Override 432 @Override
435 public Map<Long, Integer> getDeviceCountMapByProductId() { 433 public Map<Long, Integer> getDeviceCountMapByProductId() {
436 - // 查询结果转换成Map  
437 - List<Map<String, Object>> list = deviceMapper.selectDeviceCountMapByProductId();  
438 - return list.stream().collect(Collectors.toMap(  
439 - map -> Long.valueOf(map.get("key").toString()),  
440 - map -> Integer.valueOf(map.get("value").toString())  
441 - )); 434 + return deviceMapper.selectDeviceCountMapByProductId();
442 } 435 }
443 436
444 @Override 437 @Override
445 public Map<Integer, Long> getDeviceCountMapByState() { 438 public Map<Integer, Long> getDeviceCountMapByState() {
446 - // 查询结果转换成Map  
447 - List<Map<String, Object>> list = deviceMapper.selectDeviceCountGroupByState();  
448 - return list.stream().collect(Collectors.toMap(  
449 - map -> Integer.valueOf(map.get("key").toString()),  
450 - map -> Long.valueOf(map.get("value").toString())  
451 - )); 439 + return deviceMapper.selectDeviceCountGroupByState();
452 } 440 }
453 441
454 @Override 442 @Override
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductCategoryServiceImpl.java
@@ -17,6 +17,8 @@ import java.time.LocalDateTime; @@ -17,6 +17,8 @@ import java.time.LocalDateTime;
17 import java.util.*; 17 import java.util.*;
18 18
19 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; 19 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  20 +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
  21 +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.getSumValue;
20 import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_CATEGORY_NOT_EXISTS; 22 import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_CATEGORY_NOT_EXISTS;
21 23
22 /** 24 /**
@@ -99,23 +101,21 @@ public class IotProductCategoryServiceImpl implements IotProductCategoryService @@ -99,23 +101,21 @@ public class IotProductCategoryServiceImpl implements IotProductCategoryService
99 @Override 101 @Override
100 public Map<String, Integer> getProductCategoryDeviceCountMap() { 102 public Map<String, Integer> getProductCategoryDeviceCountMap() {
101 // 1. 获取所有数据 103 // 1. 获取所有数据
102 - List<IotProductCategoryDO> categoryList = iotProductCategoryMapper.selectList();  
103 - List<IotProductDO> productList = productService.getProductList();  
104 - // TODO @super:不要 list 查询,返回内存,而是查询一个 Map<productId, count> 104 + List<IotProductCategoryDO> categories = iotProductCategoryMapper.selectList();
  105 + List<IotProductDO> products = productService.getProductList();
105 Map<Long, Integer> deviceCountMapByProductId = deviceService.getDeviceCountMapByProductId(); 106 Map<Long, Integer> deviceCountMapByProductId = deviceService.getDeviceCountMapByProductId();
106 107
107 // 2. 统计每个分类下的设备数量 108 // 2. 统计每个分类下的设备数量
108 Map<String, Integer> categoryDeviceCountMap = new HashMap<>(); 109 Map<String, Integer> categoryDeviceCountMap = new HashMap<>();
109 - for (IotProductCategoryDO category : categoryList) {  
110 - categoryDeviceCountMap.put(category.getName(), 0);  
111 - // TODO @super:CollectionUtils.getSumValue(),看看能不能简化下  
112 - // 2.2 找到该分类下的所有产品,累加设备数量  
113 - for (IotProductDO product : productList) {  
114 - if (Objects.equals(product.getCategoryId(), category.getId())) {  
115 - Integer deviceCount = deviceCountMapByProductId.getOrDefault(product.getId(), 0);  
116 - categoryDeviceCountMap.merge(category.getName(), deviceCount, Integer::sum);  
117 - }  
118 - } 110 + for (IotProductCategoryDO category : categories) {
  111 + // 2.1 找到该分类下的所有产品
  112 + List<IotProductDO> categoryProducts = filterList(products,
  113 + product -> Objects.equals(product.getCategoryId(), category.getId()));
  114 + // 2.2 累加设备数量
  115 + Integer totalDeviceCount = getSumValue(categoryProducts,
  116 + product -> deviceCountMapByProductId.getOrDefault(product.getId(), 0),
  117 + Integer::sum, 0);
  118 + categoryDeviceCountMap.put(category.getName(), totalDeviceCount);
119 } 119 }
120 return categoryDeviceCountMap; 120 return categoryDeviceCountMap;
121 } 121 }
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDeviceMapper.xml deleted
1 -<?xml version="1.0" encoding="UTF-8"?>  
2 -<!DOCTYPE mapper  
3 - PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
4 - "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
5 -<mapper namespace="cn.iocoder.yudao.module.iot.dal.mysql.device.IotDeviceMapper">  
6 -  
7 - <select id="selectDeviceCountGroupByState" resultType="java.util.Map">  
8 - SELECT  
9 - state AS `key`,  
10 - COUNT(1) AS `value`  
11 - FROM iot_device  
12 - WHERE deleted = 0  
13 - GROUP BY state  
14 - </select>  
15 -  
16 - <select id="selectDeviceCountMapByProductId" resultType="java.util.Map">  
17 - SELECT  
18 - product_id AS `key`,  
19 - COUNT(1) AS `value`  
20 - FROM iot_device  
21 - WHERE deleted = 0  
22 - GROUP BY product_id  
23 - </select>  
24 -  
25 -</mapper>  
26 \ No newline at end of file 0 \ No newline at end of file