Browse Source

【优化功能】 新增产品物模型,校验优化

安浩浩 9 months ago
parent
commit
5a456a95e7

+ 3 - 1
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 {
     // ========== IoT 产品物模型 1-050-002-000 ============
     ErrorCode THINK_MODEL_FUNCTION_NOT_EXISTS = new ErrorCode(1_050_002_000, "产品物模型不存在");
     ErrorCode THINK_MODEL_FUNCTION_EXISTS_BY_PRODUCT_KEY = new ErrorCode(1_050_002_001, "ProductKey 对应的产品物模型已存在");
-    ErrorCode THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER = new ErrorCode(1_050_002_002, "产品物模型标识已存在");
+    ErrorCode THINK_MODEL_FUNCTION_IDENTIFIER_EXISTS = new ErrorCode(1_050_002_002, "存在重复的功能标识符。");
+    ErrorCode THINK_MODEL_FUNCTION_NAME_EXISTS = new ErrorCode(1_050_002_003, "存在重复的功能名称。");
+    ErrorCode THINK_MODEL_FUNCTION_IDENTIFIER_INVALID = new ErrorCode(1_050_002_003, "产品物模型标识无效");
 
     // ========== IoT 设备 1-050-003-000 ============
     ErrorCode DEVICE_NOT_EXISTS = new ErrorCode(1_050_003_000, "设备不存在");

+ 5 - 0
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
                 .likeIfPresent(IotThinkModelFunctionDO::getName, reqVO.getName())
                 .eqIfPresent(IotThinkModelFunctionDO::getType, reqVO.getType())
                 .eqIfPresent(IotThinkModelFunctionDO::getProductId, reqVO.getProductId())
+                .notIn(IotThinkModelFunctionDO::getIdentifier, "get", "set", "post")
                 .orderByDesc(IotThinkModelFunctionDO::getId));
     }
 
@@ -50,4 +51,8 @@ public interface IotThinkModelFunctionMapper extends BaseMapperX<IotThinkModelFu
                 .in(IotThinkModelFunctionDO::getType, types));
     }
 
+    default IotThinkModelFunctionDO selectByProductIdAndName(Long productId, String name) {
+        return selectOne(IotThinkModelFunctionDO::getProductId, productId,
+                IotThinkModelFunctionDO::getName, name);
+    }
 }

+ 28 - 6
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;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
-import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER;
-import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.THINK_MODEL_FUNCTION_NOT_EXISTS;
+import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
 
 /**
  * IoT 产品物模型 Service 实现类
@@ -52,21 +51,44 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
         // 1. 校验功能标识符在同一产品下是否唯一
         validateIdentifierUnique(createReqVO.getProductId(), createReqVO.getIdentifier());
 
-        // 2. 插入数据库
+        // 2. 功能名称在同一产品下是否唯一
+        validateNameUnique(createReqVO.getProductId(), createReqVO.getName());
+
+        // 3. 系统保留字段,不能用于标识符定义
+        validateNotDefaultEventAndService(createReqVO.getIdentifier());
+
+        // 3. 插入数据库
         IotThinkModelFunctionDO function = IotThinkModelFunctionConvert.INSTANCE.convert(createReqVO);
         thinkModelFunctionMapper.insert(function);
 
-        // 3. 如果创建的是属性,需要更新默认的事件和服务
+        // 4. 如果创建的是属性,需要更新默认的事件和服务
         if (Objects.equals(createReqVO.getType(), IotProductFunctionTypeEnum.PROPERTY.getType())) {
             createDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey());
         }
         return function.getId();
     }
 
+    private void validateNotDefaultEventAndService(String identifier) {
+        // set, get, post, property, event, time, value 是系统保留字段,不能用于标识符定义
+        if (CollUtil.containsAny(Arrays.asList("set", "get", "post", "property", "event", "time", "value"), Collections.singletonList(identifier))) {
+            throw exception(THINK_MODEL_FUNCTION_IDENTIFIER_INVALID);
+        }
+//        if (CollUtil.containsAny(Arrays.asList("post", "set", "get"), identifier)) {
+//            throw exception(THINK_MODEL_FUNCTION_IDENTIFIER_INVALID);
+//        }
+    }
+
+    private void validateNameUnique(Long productId, String name) {
+        IotThinkModelFunctionDO function = thinkModelFunctionMapper.selectByProductIdAndName(productId, name);
+        if (function != null) {
+            throw exception(THINK_MODEL_FUNCTION_NAME_EXISTS);
+        }
+    }
+
     private void validateIdentifierUnique(Long productId, String identifier) {
         IotThinkModelFunctionDO function = thinkModelFunctionMapper.selectByProductIdAndIdentifier(productId, identifier);
         if (function != null) {
-            throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER);
+            throw exception(THINK_MODEL_FUNCTION_IDENTIFIER_EXISTS);
         }
     }
 
@@ -92,7 +114,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
     private void validateIdentifierUniqueForUpdate(Long id, Long productId, String identifier) {
         IotThinkModelFunctionDO function = thinkModelFunctionMapper.selectByProductIdAndIdentifier(productId, identifier);
         if (function != null && ObjectUtil.notEqual(function.getId(), id)) {
-            throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER);
+            throw exception(THINK_MODEL_FUNCTION_IDENTIFIER_EXISTS);
         }
     }