Commit 832b36ac47182aa75fde0d14592fb6961e05346d

Authored by haohao
1 parent 5086e122

feat:【iot】新增数据流转规则和目的名称唯一性校验功能

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/rule/IotDataRuleMapper.java
... ... @@ -35,4 +35,8 @@ public interface IotDataRuleMapper extends BaseMapperX<IotDataRuleDO> {
35 35 return selectList(IotDataRuleDO::getStatus, status);
36 36 }
37 37  
  38 + default IotDataRuleDO selectByName(String name) {
  39 + return selectOne(IotDataRuleDO::getName, name);
  40 + }
  41 +
38 42 }
39 43 \ No newline at end of file
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/rule/IotDataSinkMapper.java
... ... @@ -29,4 +29,8 @@ public interface IotDataSinkMapper extends BaseMapperX<IotDataSinkDO> {
29 29 return selectList(IotDataSinkDO::getStatus, status);
30 30 }
31 31  
  32 + default IotDataSinkDO selectByName(String name) {
  33 + return selectOne(IotDataSinkDO::getName, name);
  34 + }
  35 +
32 36 }
33 37 \ No newline at end of file
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java
... ... @@ -65,10 +65,12 @@ public interface ErrorCodeConstants {
65 65  
66 66 // ========== IoT 数据流转规则 1-050-010-000 ==========
67 67 ErrorCode DATA_RULE_NOT_EXISTS = new ErrorCode(1_050_010_000, "数据流转规则不存在");
  68 + ErrorCode DATA_RULE_NAME_EXISTS = new ErrorCode(1_050_010_001, "数据流转规则名称已存在");
68 69  
69 70 // ========== IoT 数据流转目的 1-050-011-000 ==========
70 71 ErrorCode DATA_SINK_NOT_EXISTS = new ErrorCode(1_050_011_000, "数据桥梁不存在");
71 72 ErrorCode DATA_SINK_DELETE_FAIL_USED_BY_RULE = new ErrorCode(1_050_011_001, "数据流转目的正在被数据流转规则使用,无法删除");
  73 + ErrorCode DATA_SINK_NAME_EXISTS = new ErrorCode(1_050_011_002, "数据流转目的名称已存在");
72 74  
73 75 // ========== IoT 场景联动 1-050-012-000 ==========
74 76 ErrorCode RULE_SCENE_NOT_EXISTS = new ErrorCode(1_050_012_000, "场景联动不存在");
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/data/IotDataRuleServiceImpl.java
... ... @@ -32,6 +32,7 @@ import java.util.*;
32 32  
33 33 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
34 34 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
  35 +import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DATA_RULE_NAME_EXISTS;
35 36 import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DATA_RULE_NOT_EXISTS;
36 37  
37 38 /**
... ... @@ -62,6 +63,8 @@ public class IotDataRuleServiceImpl implements IotDataRuleService {
62 63 @Override
63 64 @CacheEvict(value = RedisKeyConstants.DATA_RULE_LIST, allEntries = true)
64 65 public Long createDataRule(IotDataRuleSaveReqVO createReqVO) {
  66 + // 校验名称唯一
  67 + validateDataRuleNameUnique(null, createReqVO.getName());
65 68 // 校验数据源配置和数据目的
66 69 validateDataRuleConfig(createReqVO);
67 70 // 新增
... ... @@ -75,6 +78,8 @@ public class IotDataRuleServiceImpl implements IotDataRuleService {
75 78 public void updateDataRule(IotDataRuleSaveReqVO updateReqVO) {
76 79 // 校验存在
77 80 validateDataRuleExists(updateReqVO.getId());
  81 + // 校验名称唯一
  82 + validateDataRuleNameUnique(updateReqVO.getId(), updateReqVO.getName());
78 83 // 校验数据源配置和数据目的
79 84 validateDataRuleConfig(updateReqVO);
80 85  
... ... @@ -99,6 +104,29 @@ public class IotDataRuleServiceImpl implements IotDataRuleService {
99 104 }
100 105  
101 106 /**
  107 + * 校验数据流转规则名称唯一性
  108 + *
  109 + * @param id 数据流转规则编号(用于更新时排除自身)
  110 + * @param name 数据流转规则名称
  111 + */
  112 + private void validateDataRuleNameUnique(Long id, String name) {
  113 + if (StrUtil.isBlank(name)) {
  114 + return;
  115 + }
  116 + IotDataRuleDO dataRule = dataRuleMapper.selectByName(name);
  117 + if (dataRule == null) {
  118 + return;
  119 + }
  120 + // 如果 id 为空,说明不用比较是否为相同 id 的规则
  121 + if (id == null) {
  122 + throw exception(DATA_RULE_NAME_EXISTS);
  123 + }
  124 + if (!dataRule.getId().equals(id)) {
  125 + throw exception(DATA_RULE_NAME_EXISTS);
  126 + }
  127 + }
  128 +
  129 + /**
102 130 * 校验数据流转规则配置
103 131 *
104 132 * @param reqVO 数据流转规则保存请求VO
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/data/IotDataSinkServiceImpl.java
1 1 package cn.iocoder.yudao.module.iot.service.rule.data;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
  4 +import cn.hutool.core.util.StrUtil;
4 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 6 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
6 7 import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.data.sink.IotDataSinkPageReqVO;
... ... @@ -19,6 +20,7 @@ import java.util.List;
19 20  
20 21 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
21 22 import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DATA_SINK_DELETE_FAIL_USED_BY_RULE;
  23 +import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DATA_SINK_NAME_EXISTS;
22 24 import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DATA_SINK_NOT_EXISTS;
23 25  
24 26 /**
... ... @@ -39,6 +41,9 @@ public class IotDataSinkServiceImpl implements IotDataSinkService {
39 41  
40 42 @Override
41 43 public Long createDataSink(IotDataSinkSaveReqVO createReqVO) {
  44 + // 校验名称唯一
  45 + validateDataSinkNameUnique(null, createReqVO.getName());
  46 + // 新增
42 47 IotDataSinkDO dataBridge = BeanUtils.toBean(createReqVO, IotDataSinkDO.class);
43 48 dataSinkMapper.insert(dataBridge);
44 49 return dataBridge.getId();
... ... @@ -48,6 +53,8 @@ public class IotDataSinkServiceImpl implements IotDataSinkService {
48 53 public void updateDataSink(IotDataSinkSaveReqVO updateReqVO) {
49 54 // 校验存在
50 55 validateDataBridgeExists(updateReqVO.getId());
  56 + // 校验名称唯一
  57 + validateDataSinkNameUnique(updateReqVO.getId(), updateReqVO.getName());
51 58 // 更新
52 59 IotDataSinkDO updateObj = BeanUtils.toBean(updateReqVO, IotDataSinkDO.class);
53 60 dataSinkMapper.updateById(updateObj);
... ... @@ -71,6 +78,29 @@ public class IotDataSinkServiceImpl implements IotDataSinkService {
71 78 }
72 79 }
73 80  
  81 + /**
  82 + * 校验数据流转目的名称唯一性
  83 + *
  84 + * @param id 数据流转目的编号(用于更新时排除自身)
  85 + * @param name 数据流转目的名称
  86 + */
  87 + private void validateDataSinkNameUnique(Long id, String name) {
  88 + if (StrUtil.isBlank(name)) {
  89 + return;
  90 + }
  91 + IotDataSinkDO dataSink = dataSinkMapper.selectByName(name);
  92 + if (dataSink == null) {
  93 + return;
  94 + }
  95 + // 如果 id 为空,说明不用比较是否为相同 id 的目的
  96 + if (id == null) {
  97 + throw exception(DATA_SINK_NAME_EXISTS);
  98 + }
  99 + if (!dataSink.getId().equals(id)) {
  100 + throw exception(DATA_SINK_NAME_EXISTS);
  101 + }
  102 + }
  103 +
74 104 @Override
75 105 public IotDataSinkDO getDataSink(Long id) {
76 106 return dataSinkMapper.selectById(id);
... ...