Commit 5a456a95e7832f35e4632740afc5af47b6dab0f3
1 parent
1995c322
【优化功能】 新增产品物模型,校验优化
Showing
3 changed files
with
36 additions
and
7 deletions
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java
| ... | ... | @@ -17,7 +17,9 @@ public interface ErrorCodeConstants { |
| 17 | 17 | // ========== IoT 产品物模型 1-050-002-000 ============ |
| 18 | 18 | ErrorCode THINK_MODEL_FUNCTION_NOT_EXISTS = new ErrorCode(1_050_002_000, "产品物模型不存在"); |
| 19 | 19 | ErrorCode THINK_MODEL_FUNCTION_EXISTS_BY_PRODUCT_KEY = new ErrorCode(1_050_002_001, "ProductKey 对应的产品物模型已存在"); |
| 20 | - ErrorCode THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER = new ErrorCode(1_050_002_002, "产品物模型标识已存在"); | |
| 20 | + ErrorCode THINK_MODEL_FUNCTION_IDENTIFIER_EXISTS = new ErrorCode(1_050_002_002, "存在重复的功能标识符。"); | |
| 21 | + ErrorCode THINK_MODEL_FUNCTION_NAME_EXISTS = new ErrorCode(1_050_002_003, "存在重复的功能名称。"); | |
| 22 | + ErrorCode THINK_MODEL_FUNCTION_IDENTIFIER_INVALID = new ErrorCode(1_050_002_003, "产品物模型标识无效"); | |
| 21 | 23 | |
| 22 | 24 | // ========== IoT 设备 1-050-003-000 ============ |
| 23 | 25 | ErrorCode DEVICE_NOT_EXISTS = new ErrorCode(1_050_003_000, "设备不存在"); | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/thinkmodelfunction/IotThinkModelFunctionMapper.java
| ... | ... | @@ -23,6 +23,7 @@ public interface IotThinkModelFunctionMapper extends BaseMapperX<IotThinkModelFu |
| 23 | 23 | .likeIfPresent(IotThinkModelFunctionDO::getName, reqVO.getName()) |
| 24 | 24 | .eqIfPresent(IotThinkModelFunctionDO::getType, reqVO.getType()) |
| 25 | 25 | .eqIfPresent(IotThinkModelFunctionDO::getProductId, reqVO.getProductId()) |
| 26 | + .notIn(IotThinkModelFunctionDO::getIdentifier, "get", "set", "post") | |
| 26 | 27 | .orderByDesc(IotThinkModelFunctionDO::getId)); |
| 27 | 28 | } |
| 28 | 29 | |
| ... | ... | @@ -50,4 +51,8 @@ public interface IotThinkModelFunctionMapper extends BaseMapperX<IotThinkModelFu |
| 50 | 51 | .in(IotThinkModelFunctionDO::getType, types)); |
| 51 | 52 | } |
| 52 | 53 | |
| 54 | + default IotThinkModelFunctionDO selectByProductIdAndName(Long productId, String name) { | |
| 55 | + return selectOne(IotThinkModelFunctionDO::getProductId, productId, | |
| 56 | + IotThinkModelFunctionDO::getName, name); | |
| 57 | + } | |
| 53 | 58 | } |
| 54 | 59 | \ No newline at end of file | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java
| ... | ... | @@ -30,8 +30,7 @@ import java.util.stream.Collectors; |
| 30 | 30 | |
| 31 | 31 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 32 | 32 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList; |
| 33 | -import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER; | |
| 34 | -import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.THINK_MODEL_FUNCTION_NOT_EXISTS; | |
| 33 | +import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*; | |
| 35 | 34 | |
| 36 | 35 | /** |
| 37 | 36 | * IoT 产品物模型 Service 实现类 |
| ... | ... | @@ -52,21 +51,44 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe |
| 52 | 51 | // 1. 校验功能标识符在同一产品下是否唯一 |
| 53 | 52 | validateIdentifierUnique(createReqVO.getProductId(), createReqVO.getIdentifier()); |
| 54 | 53 | |
| 55 | - // 2. 插入数据库 | |
| 54 | + // 2. 功能名称在同一产品下是否唯一 | |
| 55 | + validateNameUnique(createReqVO.getProductId(), createReqVO.getName()); | |
| 56 | + | |
| 57 | + // 3. 系统保留字段,不能用于标识符定义 | |
| 58 | + validateNotDefaultEventAndService(createReqVO.getIdentifier()); | |
| 59 | + | |
| 60 | + // 3. 插入数据库 | |
| 56 | 61 | IotThinkModelFunctionDO function = IotThinkModelFunctionConvert.INSTANCE.convert(createReqVO); |
| 57 | 62 | thinkModelFunctionMapper.insert(function); |
| 58 | 63 | |
| 59 | - // 3. 如果创建的是属性,需要更新默认的事件和服务 | |
| 64 | + // 4. 如果创建的是属性,需要更新默认的事件和服务 | |
| 60 | 65 | if (Objects.equals(createReqVO.getType(), IotProductFunctionTypeEnum.PROPERTY.getType())) { |
| 61 | 66 | createDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey()); |
| 62 | 67 | } |
| 63 | 68 | return function.getId(); |
| 64 | 69 | } |
| 65 | 70 | |
| 71 | + private void validateNotDefaultEventAndService(String identifier) { | |
| 72 | + // set, get, post, property, event, time, value 是系统保留字段,不能用于标识符定义 | |
| 73 | + if (CollUtil.containsAny(Arrays.asList("set", "get", "post", "property", "event", "time", "value"), Collections.singletonList(identifier))) { | |
| 74 | + throw exception(THINK_MODEL_FUNCTION_IDENTIFIER_INVALID); | |
| 75 | + } | |
| 76 | +// if (CollUtil.containsAny(Arrays.asList("post", "set", "get"), identifier)) { | |
| 77 | +// throw exception(THINK_MODEL_FUNCTION_IDENTIFIER_INVALID); | |
| 78 | +// } | |
| 79 | + } | |
| 80 | + | |
| 81 | + private void validateNameUnique(Long productId, String name) { | |
| 82 | + IotThinkModelFunctionDO function = thinkModelFunctionMapper.selectByProductIdAndName(productId, name); | |
| 83 | + if (function != null) { | |
| 84 | + throw exception(THINK_MODEL_FUNCTION_NAME_EXISTS); | |
| 85 | + } | |
| 86 | + } | |
| 87 | + | |
| 66 | 88 | private void validateIdentifierUnique(Long productId, String identifier) { |
| 67 | 89 | IotThinkModelFunctionDO function = thinkModelFunctionMapper.selectByProductIdAndIdentifier(productId, identifier); |
| 68 | 90 | if (function != null) { |
| 69 | - throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER); | |
| 91 | + throw exception(THINK_MODEL_FUNCTION_IDENTIFIER_EXISTS); | |
| 70 | 92 | } |
| 71 | 93 | } |
| 72 | 94 | |
| ... | ... | @@ -92,7 +114,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe |
| 92 | 114 | private void validateIdentifierUniqueForUpdate(Long id, Long productId, String identifier) { |
| 93 | 115 | IotThinkModelFunctionDO function = thinkModelFunctionMapper.selectByProductIdAndIdentifier(productId, identifier); |
| 94 | 116 | if (function != null && ObjectUtil.notEqual(function.getId(), id)) { |
| 95 | - throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER); | |
| 117 | + throw exception(THINK_MODEL_FUNCTION_IDENTIFIER_EXISTS); | |
| 96 | 118 | } |
| 97 | 119 | } |
| 98 | 120 | ... | ... |