Commit 1beb5c039cad0fc7679c5abc176fd9ad328bf316
1 parent
9d6b37c4
feat:【IoT 物联网】完成告警配置的管理
Showing
9 changed files
with
110 additions
and
31 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/alert/IotAlertConfigController.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.controller.admin.alert; |
| 2 | 2 | |
| 3 | -import org.springframework.web.bind.annotation.*; | |
| 3 | +import cn.iocoder.yudao.framework.common.pojo.CommonResult; | |
| 4 | +import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 5 | +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | |
| 6 | +import cn.iocoder.yudao.module.iot.controller.admin.alert.vo.IotAlertConfigPageReqVO; | |
| 7 | +import cn.iocoder.yudao.module.iot.controller.admin.alert.vo.IotAlertConfigRespVO; | |
| 8 | +import cn.iocoder.yudao.module.iot.controller.admin.alert.vo.IotAlertConfigSaveReqVO; | |
| 9 | +import cn.iocoder.yudao.module.iot.dal.dataobject.alert.IotAlertConfigDO; | |
| 10 | +import cn.iocoder.yudao.module.iot.service.alert.IotAlertConfigService; | |
| 11 | +import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 12 | +import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; | |
| 13 | +import io.swagger.v3.oas.annotations.Operation; | |
| 14 | +import io.swagger.v3.oas.annotations.Parameter; | |
| 15 | +import io.swagger.v3.oas.annotations.tags.Tag; | |
| 4 | 16 | import jakarta.annotation.Resource; |
| 5 | -import org.springframework.validation.annotation.Validated; | |
| 17 | +import jakarta.validation.Valid; | |
| 6 | 18 | import org.springframework.security.access.prepost.PreAuthorize; |
| 7 | -import io.swagger.v3.oas.annotations.tags.Tag; | |
| 8 | -import io.swagger.v3.oas.annotations.Parameter; | |
| 9 | -import io.swagger.v3.oas.annotations.Operation; | |
| 19 | +import org.springframework.validation.annotation.Validated; | |
| 20 | +import org.springframework.web.bind.annotation.*; | |
| 10 | 21 | |
| 11 | -import jakarta.validation.constraints.*; | |
| 12 | -import jakarta.validation.*; | |
| 13 | -import jakarta.servlet.http.*; | |
| 14 | -import java.util.*; | |
| 15 | -import java.io.IOException; | |
| 22 | +import java.util.Map; | |
| 23 | +import java.util.Objects; | |
| 24 | +import java.util.stream.Collectors; | |
| 16 | 25 | |
| 17 | -import cn.iocoder.yudao.framework.common.pojo.PageParam; | |
| 18 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 19 | -import cn.iocoder.yudao.framework.common.pojo.CommonResult; | |
| 20 | -import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | |
| 21 | 26 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 22 | - | |
| 23 | -import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; | |
| 24 | - | |
| 25 | -import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog; | |
| 26 | -import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*; | |
| 27 | - | |
| 28 | -import cn.iocoder.yudao.module.iot.controller.admin.alert.vo.*; | |
| 29 | -import cn.iocoder.yudao.module.iot.dal.dataobject.alert.IotAlertConfigDO; | |
| 30 | -import cn.iocoder.yudao.module.iot.service.alert.IotAlertConfigService; | |
| 27 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSetByFlatMap; | |
| 31 | 28 | |
| 32 | 29 | @Tag(name = "管理后台 - IoT 告警配置") |
| 33 | 30 | @RestController |
| ... | ... | @@ -38,6 +35,9 @@ public class IotAlertConfigController { |
| 38 | 35 | @Resource |
| 39 | 36 | private IotAlertConfigService alertConfigService; |
| 40 | 37 | |
| 38 | + @Resource | |
| 39 | + private AdminUserApi adminUserApi; | |
| 40 | + | |
| 41 | 41 | @PostMapping("/create") |
| 42 | 42 | @Operation(summary = "创建告警配置") |
| 43 | 43 | @PreAuthorize("@ss.hasPermission('iot:alert-config:create')") |
| ... | ... | @@ -76,7 +76,12 @@ public class IotAlertConfigController { |
| 76 | 76 | @PreAuthorize("@ss.hasPermission('iot:alert-config:query')") |
| 77 | 77 | public CommonResult<PageResult<IotAlertConfigRespVO>> getAlertConfigPage(@Valid IotAlertConfigPageReqVO pageReqVO) { |
| 78 | 78 | PageResult<IotAlertConfigDO> pageResult = alertConfigService.getAlertConfigPage(pageReqVO); |
| 79 | - return success(BeanUtils.toBean(pageResult, IotAlertConfigRespVO.class)); | |
| 79 | + // 转换返回 | |
| 80 | + Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap( | |
| 81 | + convertSetByFlatMap(pageResult.getList(), config -> config.getReceiveUserIds().stream())); | |
| 82 | + return success(BeanUtils.toBean(pageResult, IotAlertConfigRespVO.class, vo -> | |
| 83 | + vo.setReceiveUserNames(vo.getReceiveUserIds().stream().map(userMap::get) | |
| 84 | + .filter(Objects::nonNull).map(AdminUserRespDTO::getNickname).collect(Collectors.toList())))); | |
| 80 | 85 | } |
| 81 | 86 | |
| 82 | 87 | } |
| 83 | 88 | \ No newline at end of file | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/alert/vo/IotAlertConfigRespVO.java
| ... | ... | @@ -31,6 +31,9 @@ public class IotAlertConfigRespVO { |
| 31 | 31 | @Schema(description = "接收的用户编号数组", requiredMode = Schema.RequiredMode.REQUIRED, example = "100,200") |
| 32 | 32 | private List<Long> receiveUserIds; |
| 33 | 33 | |
| 34 | + @Schema(description = "接收的用户名称数组", requiredMode = Schema.RequiredMode.REQUIRED, example = "张三,李四") | |
| 35 | + private List<String> receiveUserNames; | |
| 36 | + | |
| 34 | 37 | @Schema(description = "接收的类型数组", requiredMode = Schema.RequiredMode.REQUIRED, example = "1,2,3") |
| 35 | 38 | private List<Integer> receiveTypes; |
| 36 | 39 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/rule/IotRuleSceneController.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.controller.admin.rule; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | |
| 3 | 4 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 6 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| ... | ... | @@ -18,7 +19,10 @@ import org.springframework.security.access.prepost.PreAuthorize; |
| 18 | 19 | import org.springframework.validation.annotation.Validated; |
| 19 | 20 | import org.springframework.web.bind.annotation.*; |
| 20 | 21 | |
| 22 | +import java.util.List; | |
| 23 | + | |
| 21 | 24 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 25 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | |
| 22 | 26 | |
| 23 | 27 | @Tag(name = "管理后台 - IoT 场景联动") |
| 24 | 28 | @RestController |
| ... | ... | @@ -70,6 +74,14 @@ public class IotRuleSceneController { |
| 70 | 74 | return success(BeanUtils.toBean(pageResult, IotRuleSceneRespVO.class)); |
| 71 | 75 | } |
| 72 | 76 | |
| 77 | + @GetMapping("/simple-list") | |
| 78 | + @Operation(summary = "获取场景联动的精简信息列表", description = "主要用于前端的下拉选项") | |
| 79 | + public CommonResult<List<IotRuleSceneRespVO>> getRuleSceneSimpleList() { | |
| 80 | + List<IotRuleSceneDO> list = ruleSceneService.getRuleSceneListByStatus(CommonStatusEnum.ENABLE.getStatus()); | |
| 81 | + return success(convertList(list, scene -> // 只返回 id、name 字段 | |
| 82 | + new IotRuleSceneRespVO().setId(scene.getId()).setName(scene.getName()))); | |
| 83 | + } | |
| 84 | + | |
| 73 | 85 | @GetMapping("/test") |
| 74 | 86 | @PermitAll |
| 75 | 87 | public void test() { | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/alert/IotAlertConfigDO.java
| ... | ... | @@ -6,7 +6,7 @@ import cn.iocoder.yudao.framework.mybatis.core.type.IntegerListTypeHandler; |
| 6 | 6 | import cn.iocoder.yudao.framework.mybatis.core.type.LongListTypeHandler; |
| 7 | 7 | import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotRuleSceneDO; |
| 8 | 8 | import cn.iocoder.yudao.module.iot.enums.DictTypeConstants; |
| 9 | -import cn.iocoder.yudao.module.iot.enums.alert.IotAlertConfigReceiveTypeEnum; | |
| 9 | +import cn.iocoder.yudao.module.iot.enums.alert.IotAlertReceiveTypeEnum; | |
| 10 | 10 | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| 11 | 11 | import com.baomidou.mybatisplus.annotation.KeySequence; |
| 12 | 12 | import com.baomidou.mybatisplus.annotation.TableField; |
| ... | ... | @@ -24,7 +24,7 @@ import java.util.List; |
| 24 | 24 | * |
| 25 | 25 | * @author 芋道源码 |
| 26 | 26 | */ |
| 27 | -@TableName("iot_alert_config") | |
| 27 | +@TableName(value = "iot_alert_config", autoResultMap = true) | |
| 28 | 28 | @KeySequence("iot_alert_config_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。 |
| 29 | 29 | @Data |
| 30 | 30 | @Builder |
| ... | ... | @@ -76,7 +76,7 @@ public class IotAlertConfigDO extends BaseDO { |
| 76 | 76 | /** |
| 77 | 77 | * 接收的类型数组 |
| 78 | 78 | * |
| 79 | - * 枚举 {@link IotAlertConfigReceiveTypeEnum} | |
| 79 | + * 枚举 {@link IotAlertReceiveTypeEnum} | |
| 80 | 80 | */ |
| 81 | 81 | @TableField(typeHandler = IntegerListTypeHandler.class) |
| 82 | 82 | private List<Integer> receiveTypes; | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/rule/IotRuleSceneMapper.java
| ... | ... | @@ -7,6 +7,8 @@ import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.scene.IotRuleScenePa |
| 7 | 7 | import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotRuleSceneDO; |
| 8 | 8 | import org.apache.ibatis.annotations.Mapper; |
| 9 | 9 | |
| 10 | +import java.util.List; | |
| 11 | + | |
| 10 | 12 | /** |
| 11 | 13 | * IoT 场景联动 Mapper |
| 12 | 14 | * |
| ... | ... | @@ -24,4 +26,8 @@ public interface IotRuleSceneMapper extends BaseMapperX<IotRuleSceneDO> { |
| 24 | 26 | .orderByDesc(IotRuleSceneDO::getId)); |
| 25 | 27 | } |
| 26 | 28 | |
| 29 | + default List<IotRuleSceneDO> selectListByStatus(Integer status) { | |
| 30 | + return selectList(IotRuleSceneDO::getStatus, status); | |
| 31 | + } | |
| 32 | + | |
| 27 | 33 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/enums/alert/IotAlertConfigReceiveTypeEnum.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/enums/alert/IotAlertReceiveTypeEnum.java
| ... | ... | @@ -7,21 +7,22 @@ import lombok.RequiredArgsConstructor; |
| 7 | 7 | import java.util.Arrays; |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | - * IoT 告警配置的接收方式枚举 | |
| 10 | + * IoT 告警的接收方式枚举 | |
| 11 | 11 | * |
| 12 | 12 | * @author 芋道源码 |
| 13 | 13 | */ |
| 14 | 14 | @RequiredArgsConstructor |
| 15 | 15 | @Getter |
| 16 | -public enum IotAlertConfigReceiveTypeEnum implements ArrayValuable<Integer> { | |
| 16 | +public enum IotAlertReceiveTypeEnum implements ArrayValuable<Integer> { | |
| 17 | 17 | |
| 18 | 18 | SMS(1), // 短信 |
| 19 | 19 | MAIL(2), // 邮箱 |
| 20 | - NOTIFY(3); // 通知 | |
| 20 | + NOTIFY(3); // 站内信 | |
| 21 | + // TODO 待实现(欢迎 pull request):webhook 4 | |
| 21 | 22 | |
| 22 | 23 | private final Integer type; |
| 23 | 24 | |
| 24 | - public static final Integer[] ARRAYS = Arrays.stream(values()).map(IotAlertConfigReceiveTypeEnum::getType).toArray(Integer[]::new); | |
| 25 | + public static final Integer[] ARRAYS = Arrays.stream(values()).map(IotAlertReceiveTypeEnum::getType).toArray(Integer[]::new); | |
| 25 | 26 | |
| 26 | 27 | @Override |
| 27 | 28 | public Integer[] array() { | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/alert/IotAlertConfigServiceImpl.java
| ... | ... | @@ -6,6 +6,8 @@ import cn.iocoder.yudao.module.iot.controller.admin.alert.vo.IotAlertConfigPageR |
| 6 | 6 | import cn.iocoder.yudao.module.iot.controller.admin.alert.vo.IotAlertConfigSaveReqVO; |
| 7 | 7 | import cn.iocoder.yudao.module.iot.dal.dataobject.alert.IotAlertConfigDO; |
| 8 | 8 | import cn.iocoder.yudao.module.iot.dal.mysql.alert.IotAlertConfigMapper; |
| 9 | +import cn.iocoder.yudao.module.iot.service.rule.scene.IotRuleSceneService; | |
| 10 | +import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | |
| 9 | 11 | import jakarta.annotation.Resource; |
| 10 | 12 | import org.springframework.stereotype.Service; |
| 11 | 13 | import org.springframework.validation.annotation.Validated; |
| ... | ... | @@ -25,8 +27,18 @@ public class IotAlertConfigServiceImpl implements IotAlertConfigService { |
| 25 | 27 | @Resource |
| 26 | 28 | private IotAlertConfigMapper alertConfigMapper; |
| 27 | 29 | |
| 30 | + @Resource | |
| 31 | + private IotRuleSceneService ruleSceneService; | |
| 32 | + | |
| 33 | + @Resource | |
| 34 | + private AdminUserApi adminUserApi; | |
| 35 | + | |
| 28 | 36 | @Override |
| 29 | 37 | public Long createAlertConfig(IotAlertConfigSaveReqVO createReqVO) { |
| 38 | + // 校验关联数据是否存在 | |
| 39 | + ruleSceneService.validateRuleSceneList(createReqVO.getSceneRuleIds()); | |
| 40 | + adminUserApi.validateUserList(createReqVO.getReceiveUserIds()); | |
| 41 | + | |
| 30 | 42 | IotAlertConfigDO alertConfig = BeanUtils.toBean(createReqVO, IotAlertConfigDO.class); |
| 31 | 43 | alertConfigMapper.insert(alertConfig); |
| 32 | 44 | return alertConfig.getId(); |
| ... | ... | @@ -36,6 +48,10 @@ public class IotAlertConfigServiceImpl implements IotAlertConfigService { |
| 36 | 48 | public void updateAlertConfig(IotAlertConfigSaveReqVO updateReqVO) { |
| 37 | 49 | // 校验存在 |
| 38 | 50 | validateAlertConfigExists(updateReqVO.getId()); |
| 51 | + // 校验关联数据是否存在 | |
| 52 | + ruleSceneService.validateRuleSceneList(updateReqVO.getSceneRuleIds()); | |
| 53 | + adminUserApi.validateUserList(updateReqVO.getReceiveUserIds()); | |
| 54 | + | |
| 39 | 55 | // 更新 |
| 40 | 56 | IotAlertConfigDO updateObj = BeanUtils.toBean(updateReqVO, IotAlertConfigDO.class); |
| 41 | 57 | alertConfigMapper.updateById(updateObj); | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/scene/IotRuleSceneService.java
| ... | ... | @@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotRuleSceneDO; |
| 8 | 8 | import cn.iocoder.yudao.module.iot.enums.rule.IotRuleSceneTriggerTypeEnum; |
| 9 | 9 | import jakarta.validation.Valid; |
| 10 | 10 | |
| 11 | +import java.util.Collection; | |
| 11 | 12 | import java.util.List; |
| 12 | 13 | |
| 13 | 14 | /** |
| ... | ... | @@ -56,6 +57,22 @@ public interface IotRuleSceneService { |
| 56 | 57 | PageResult<IotRuleSceneDO> getRuleScenePage(IotRuleScenePageReqVO pageReqVO); |
| 57 | 58 | |
| 58 | 59 | /** |
| 60 | + * 校验规则场景编号们是否存在。如下情况,视为无效: | |
| 61 | + * 1. 规则场景编号不存在 | |
| 62 | + * | |
| 63 | + * @param ids 规则场景编号数组 | |
| 64 | + */ | |
| 65 | + void validateRuleSceneList(Collection<Long> ids); | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * 获得指定状态的场景联动列表 | |
| 69 | + * | |
| 70 | + * @param status 状态 | |
| 71 | + * @return 场景联动列表 | |
| 72 | + */ | |
| 73 | + List<IotRuleSceneDO> getRuleSceneListByStatus(Integer status); | |
| 74 | + | |
| 75 | + /** | |
| 59 | 76 | * 【缓存】获得指定设备的场景列表 |
| 60 | 77 | * |
| 61 | 78 | * @param productKey 产品 Key | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/scene/IotRuleSceneServiceImpl.java
| ... | ... | @@ -42,9 +42,12 @@ import org.springframework.validation.annotation.Validated; |
| 42 | 42 | import java.util.HashMap; |
| 43 | 43 | import java.util.List; |
| 44 | 44 | import java.util.Map; |
| 45 | +import java.util.Collection; | |
| 46 | +import java.util.Set; | |
| 45 | 47 | |
| 46 | 48 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 47 | 49 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| 50 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; | |
| 48 | 51 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; |
| 49 | 52 | import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.RULE_SCENE_NOT_EXISTS; |
| 50 | 53 | |
| ... | ... | @@ -109,6 +112,22 @@ public class IotRuleSceneServiceImpl implements IotRuleSceneService { |
| 109 | 112 | return ruleSceneMapper.selectPage(pageReqVO); |
| 110 | 113 | } |
| 111 | 114 | |
| 115 | + @Override | |
| 116 | + public void validateRuleSceneList(Collection<Long> ids) { | |
| 117 | + if (CollUtil.isEmpty(ids)) { | |
| 118 | + return; | |
| 119 | + } | |
| 120 | + // 批量查询存在的规则场景 | |
| 121 | + List<IotRuleSceneDO> existingScenes = ruleSceneMapper.selectByIds(ids); | |
| 122 | + if (existingScenes.size() != ids.size()) { | |
| 123 | + throw exception(RULE_SCENE_NOT_EXISTS); | |
| 124 | + } | |
| 125 | + } | |
| 126 | + | |
| 127 | + @Override | |
| 128 | + public List<IotRuleSceneDO> getRuleSceneListByStatus(Integer status) { | |
| 129 | + return ruleSceneMapper.selectListByStatus(status); | |
| 130 | + } | |
| 112 | 131 | |
| 113 | 132 | // TODO 芋艿,缓存待实现 |
| 114 | 133 | @Override | ... | ... |