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

This commit is contained in:
YunaiV
2025-06-29 18:59:55 +08:00
parent da60e649df
commit 801a6b970e
4 changed files with 38 additions and 59 deletions

View File

@@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.device.IotDevicePageReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Nullable;
import org.apache.ibatis.annotations.Mapper;
@@ -13,6 +14,7 @@ import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* IoT 设备 Mapper
@@ -80,19 +82,33 @@ public interface IotDeviceMapper extends BaseMapperX<IotDeviceDO> {
}
/**
* 查询指定产品下各状态的设备数量
* 查询指定产品下的设备数量
*
* @return 设备数量统计列表
* @return 产品编号 -> 设备数量的映射
*/
// TODO @super通过 mybatis-plus 来写哈,然后返回 Map 貌似就行了?!
List<Map<String, Object>> selectDeviceCountMapByProductId();
default Map<Long, Integer> selectDeviceCountMapByProductId() {
List<Map<String, Object>> result = selectMaps(new QueryWrapper<IotDeviceDO>()
.select("product_id AS productId", "COUNT(1) AS deviceCount")
.groupBy("product_id"));
return result.stream().collect(Collectors.toMap(
map -> Long.valueOf(map.get("productId").toString()),
map -> Integer.valueOf(map.get("deviceCount").toString())
));
}
// TODO @super通过 mybatis-plus 来写哈,然后返回 Map 貌似就行了?!
/**
* 查询各个状态下的设备数量
*
* @return 设备数量统计列表
* @return 设备状态 -> 设备数量的映射
*/
List<Map<String, Object>> selectDeviceCountGroupByState();
default Map<Integer, Long> selectDeviceCountGroupByState() {
List<Map<String, Object>> result = selectMaps(new QueryWrapper<IotDeviceDO>()
.select("state", "COUNT(1) AS deviceCount")
.groupBy("state"));
return result.stream().collect(Collectors.toMap(
map -> Integer.valueOf(map.get("state").toString()),
map -> Long.valueOf(map.get("deviceCount").toString())
));
}
}

View File

@@ -36,7 +36,6 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
@@ -430,25 +429,14 @@ public class IotDeviceServiceImpl implements IotDeviceService {
return deviceMapper.selectCountByCreateTime(createTime);
}
// TODO @super简化
@Override
public Map<Long, Integer> getDeviceCountMapByProductId() {
// 查询结果转换成Map
List<Map<String, Object>> list = deviceMapper.selectDeviceCountMapByProductId();
return list.stream().collect(Collectors.toMap(
map -> Long.valueOf(map.get("key").toString()),
map -> Integer.valueOf(map.get("value").toString())
));
return deviceMapper.selectDeviceCountMapByProductId();
}
@Override
public Map<Integer, Long> getDeviceCountMapByState() {
// 查询结果转换成Map
List<Map<String, Object>> list = deviceMapper.selectDeviceCountGroupByState();
return list.stream().collect(Collectors.toMap(
map -> Integer.valueOf(map.get("key").toString()),
map -> Long.valueOf(map.get("value").toString())
));
return deviceMapper.selectDeviceCountGroupByState();
}
@Override

View File

@@ -17,6 +17,8 @@ import java.time.LocalDateTime;
import java.util.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.getSumValue;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_CATEGORY_NOT_EXISTS;
/**
@@ -99,23 +101,21 @@ public class IotProductCategoryServiceImpl implements IotProductCategoryService
@Override
public Map<String, Integer> getProductCategoryDeviceCountMap() {
// 1. 获取所有数据
List<IotProductCategoryDO> categoryList = iotProductCategoryMapper.selectList();
List<IotProductDO> productList = productService.getProductList();
// TODO @super不要 list 查询,返回内存,而是查询一个 Map<productId, count>
List<IotProductCategoryDO> categories = iotProductCategoryMapper.selectList();
List<IotProductDO> products = productService.getProductList();
Map<Long, Integer> deviceCountMapByProductId = deviceService.getDeviceCountMapByProductId();
// 2. 统计每个分类下的设备数量
Map<String, Integer> categoryDeviceCountMap = new HashMap<>();
for (IotProductCategoryDO category : categoryList) {
categoryDeviceCountMap.put(category.getName(), 0);
// TODO @superCollectionUtils.getSumValue(),看看能不能简化下
// 2.2 找到该分类下的所有产品,累加设备数量
for (IotProductDO product : productList) {
if (Objects.equals(product.getCategoryId(), category.getId())) {
Integer deviceCount = deviceCountMapByProductId.getOrDefault(product.getId(), 0);
categoryDeviceCountMap.merge(category.getName(), deviceCount, Integer::sum);
}
}
for (IotProductCategoryDO category : categories) {
// 2.1 找到该分类下的所有产品
List<IotProductDO> categoryProducts = filterList(products,
product -> Objects.equals(product.getCategoryId(), category.getId()));
// 2.2 累加设备数量
Integer totalDeviceCount = getSumValue(categoryProducts,
product -> deviceCountMapByProductId.getOrDefault(product.getId(), 0),
Integer::sum, 0);
categoryDeviceCountMap.put(category.getName(), totalDeviceCount);
}
return categoryDeviceCountMap;
}

View File

@@ -1,25 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.iot.dal.mysql.device.IotDeviceMapper">
<select id="selectDeviceCountGroupByState" resultType="java.util.Map">
SELECT
state AS `key`,
COUNT(1) AS `value`
FROM iot_device
WHERE deleted = 0
GROUP BY state
</select>
<select id="selectDeviceCountMapByProductId" resultType="java.util.Map">
SELECT
product_id AS `key`,
COUNT(1) AS `value`
FROM iot_device
WHERE deleted = 0
GROUP BY product_id
</select>
</mapper>