package cn.iocoder.yudao.module.iot.dal.mysql.device; import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; 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 cn.iocoder.yudao.module.iot.enums.product.IotProductDeviceTypeEnum; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import jakarta.annotation.Nullable; import org.apache.ibatis.annotations.Mapper; import java.time.LocalDateTime; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * IoT 设备 Mapper * * @author 芋道源码 */ @Mapper public interface IotDeviceMapper extends BaseMapperX { default PageResult selectPage(IotDevicePageReqVO reqVO) { return selectPage(reqVO, new LambdaQueryWrapperX() .likeIfPresent(IotDeviceDO::getDeviceName, reqVO.getDeviceName()) .eqIfPresent(IotDeviceDO::getProductId, reqVO.getProductId()) .eqIfPresent(IotDeviceDO::getDeviceType, reqVO.getDeviceType()) .likeIfPresent(IotDeviceDO::getNickname, reqVO.getNickname()) .eqIfPresent(IotDeviceDO::getState, reqVO.getStatus()) .eqIfPresent(IotDeviceDO::getGatewayId, reqVO.getGatewayId()) .apply(ObjectUtil.isNotNull(reqVO.getGroupId()), "FIND_IN_SET(" + reqVO.getGroupId() + ",group_ids) > 0") .orderByDesc(IotDeviceDO::getId)); } default IotDeviceDO selectByDeviceName(String deviceName) { return selectOne(IotDeviceDO::getDeviceName, deviceName); } default IotDeviceDO selectByProductKeyAndDeviceName(String productKey, String deviceName) { return selectOne(IotDeviceDO::getProductKey, productKey, IotDeviceDO::getDeviceName, deviceName); } default long selectCountByGatewayId(Long id) { return selectCount(IotDeviceDO::getGatewayId, id); } default Long selectCountByProductId(Long productId) { return selectCount(IotDeviceDO::getProductId, productId); } default List selectListByCondition(@Nullable Integer deviceType, @Nullable Long productId) { return selectList(new LambdaQueryWrapperX() .eqIfPresent(IotDeviceDO::getDeviceType, deviceType) .eqIfPresent(IotDeviceDO::getProductId, productId)); } default List selectListByState(Integer state) { return selectList(IotDeviceDO::getState, state); } default List selectListByProductId(Long productId) { return selectList(IotDeviceDO::getProductId, productId); } default Long selectCountByGroupId(Long groupId) { return selectCount(new LambdaQueryWrapperX() .apply("FIND_IN_SET(" + groupId + ",group_ids) > 0")); } default Long selectCountByCreateTime(@Nullable LocalDateTime createTime) { return selectCount(new LambdaQueryWrapperX() .geIfPresent(IotDeviceDO::getCreateTime, createTime)); } default List selectByProductKeyAndDeviceNames(String productKey, Collection deviceNames) { return selectList(new LambdaQueryWrapperX() .eq(IotDeviceDO::getProductKey, productKey) .in(IotDeviceDO::getDeviceName, deviceNames)); } default IotDeviceDO selectBySerialNumber(String serialNumber) { return selectOne(IotDeviceDO::getSerialNumber, serialNumber); } /** * 查询指定产品下的设备数量 * * @return 产品编号 -> 设备数量的映射 */ 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()) )); } /** * 查询各个状态下的设备数量 * * @return 设备状态 -> 设备数量的映射 */ 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()) )); } /** * 查询有位置信息的设备列表 * * @return 设备列表 */ default List selectListByHasLocation() { return selectList(new LambdaQueryWrapperX() .isNotNull(IotDeviceDO::getLatitude) .isNotNull(IotDeviceDO::getLongitude)); } // ========== 网关-子设备绑定相关 ========== /** * 根据网关编号查询子设备列表 * * @param gatewayId 网关设备编号 * @return 子设备列表 */ default List selectListByGatewayId(Long gatewayId) { return selectList(IotDeviceDO::getGatewayId, gatewayId); } /** * 分页查询未绑定网关的子设备 * * @param reqVO 分页查询参数 * @return 子设备分页 */ default PageResult selectUnboundSubDevicePage(IotDevicePageReqVO reqVO) { return selectPage(reqVO, new LambdaQueryWrapperX() .likeIfPresent(IotDeviceDO::getDeviceName, reqVO.getDeviceName()) .likeIfPresent(IotDeviceDO::getNickname, reqVO.getNickname()) .eqIfPresent(IotDeviceDO::getProductId, reqVO.getProductId()) // 仅查询子设备 + 未绑定网关 .eq(IotDeviceDO::getDeviceType, IotProductDeviceTypeEnum.GATEWAY_SUB.getType()) .isNull(IotDeviceDO::getGatewayId) .orderByDesc(IotDeviceDO::getId)); } /** * 批量更新设备的网关编号 * * @param ids 设备编号列表 * @param gatewayId 网关设备编号(可以为 null,表示解绑) */ default void updateGatewayIdBatch(Collection ids, Long gatewayId) { update(null, new LambdaUpdateWrapper() .set(IotDeviceDO::getGatewayId, gatewayId) .in(IotDeviceDO::getId, ids)); } }