diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/device/IotDeviceMapper.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/device/IotDeviceMapper.java index 32477221ac..7cc7d5de81 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/device/IotDeviceMapper.java +++ b/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; 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 { } /** - * 查询指定产品下各状态的设备数量 + * 查询指定产品下的设备数量 * - * @return 设备数量统计列表 + * @return 产品编号 -> 设备数量的映射 */ - // TODO @super:通过 mybatis-plus 来写哈,然后返回 Map 貌似就行了?! - List> selectDeviceCountMapByProductId(); + default Map selectDeviceCountMapByProductId() { + List> result = selectMaps(new QueryWrapper() + .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> selectDeviceCountGroupByState(); + default Map selectDeviceCountGroupByState() { + List> result = selectMaps(new QueryWrapper() + .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()) + )); + } } diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java index 4bb090db41..ccbd652d6e 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java +++ b/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; 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 getDeviceCountMapByProductId() { - // 查询结果转换成Map - List> 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 getDeviceCountMapByState() { - // 查询结果转换成Map - List> 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 diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductCategoryServiceImpl.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductCategoryServiceImpl.java index 9eb401c634..3c64caedb7 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductCategoryServiceImpl.java +++ b/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; 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 getProductCategoryDeviceCountMap() { // 1. 获取所有数据 - List categoryList = iotProductCategoryMapper.selectList(); - List productList = productService.getProductList(); - // TODO @super:不要 list 查询,返回内存,而是查询一个 Map + List categories = iotProductCategoryMapper.selectList(); + List products = productService.getProductList(); Map deviceCountMapByProductId = deviceService.getDeviceCountMapByProductId(); // 2. 统计每个分类下的设备数量 Map categoryDeviceCountMap = new HashMap<>(); - for (IotProductCategoryDO category : categoryList) { - categoryDeviceCountMap.put(category.getName(), 0); - // TODO @super:CollectionUtils.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 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; } diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDeviceMapper.xml b/yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDeviceMapper.xml deleted file mode 100644 index 8404729cce..0000000000 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDeviceMapper.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - \ No newline at end of file