Browse Source

修改:新增产品生成 ProductKey

安浩浩 7 months ago
parent
commit
f7b62b4bcc

+ 20 - 29
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/ProductSaveReqVO.java

@@ -9,46 +9,37 @@ import jakarta.validation.constraints.*;
 @Data
 public class ProductSaveReqVO {
 
-    @Schema(description = "产品名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "李四")
-    @NotEmpty(message = "产品名称不能为空")
-    private String name;
-
-    @Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "26087")
+    @Schema(description = "产品编号", requiredMode = Schema.RequiredMode.AUTO, example = "1")
     private Long id;
 
-    @Schema(description = "产品标识", requiredMode = Schema.RequiredMode.REQUIRED)
-    @NotEmpty(message = "产品标识不能为空")
+    @Schema(description = "产品Key", requiredMode = Schema.RequiredMode.AUTO, example = "12345abc")
     private String productKey;
 
-    @Schema(description = "协议编号(脚本解析 id)", requiredMode = Schema.RequiredMode.REQUIRED, example = "13177")
-    @NotNull(message = "协议编号(脚本解析 id)不能为空")
-    private Long protocolId;
-
-    @Schema(description = "产品所属品类标识符", example = "14237")
-    private Long categoryId;
-
-    @Schema(description = "产品描述", example = "你猜")
-    private String description;
-
-    @Schema(description = "数据校验级别, 0: 强校验, 1: 弱校验, 2: 免校验", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
-    @NotNull(message = "数据校验级别, 0: 强校验, 1: 弱校验, 2: 免校验不能为空")
-    private Integer validateType;
-
-    @Schema(description = "产品状态, 0: DEVELOPMENT_STATUS, 1: RELEASE_STATUS", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
-    @NotNull(message = "产品状态, 0: DEVELOPMENT_STATUS, 1: RELEASE_STATUS不能为空")
-    private Integer status;
+    @Schema(description = "产品名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "温湿度")
+    @NotEmpty(message = "产品名称不能为空")
+    private String name;
 
-    @Schema(description = "设备类型, 0: 直连设备, 1: 网关子设备, 2: 网关设备", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
-    @NotNull(message = "设备类型, 0: 直连设备, 1: 网关子设备, 2: 网关设备不能为空")
+    @Schema(description = "设备类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
+    @NotNull(message = "设备类型不能为空")
     private Integer deviceType;
 
-    @Schema(description = "联网方式, 0: Wi-Fi, 1: Cellular, 2: Ethernet, 3: 其他", example = "2")
+    @Schema(description = "联网方式", requiredMode = Schema.RequiredMode.REQUIRED,example = "0")
+    @NotNull(message = "联网方式不能为空")
     private Integer netType;
 
-    @Schema(description = "接入网关协议, 0: modbus, 1: opc-ua, 2: customize, 3: ble, 4: zigbee", example = "2")
+    @Schema(description = "接入网关协议", requiredMode = Schema.RequiredMode.REQUIRED,example = "0")
+    @NotNull(message = "接入网关协议不能为空")
     private Integer protocolType;
 
-    @Schema(description = "数据格式, 0: 透传模式, 1: Alink JSON")
+    @Schema(description = "数据格式",requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
+    @NotNull(message = "数据格式不能为空")
     private Integer dataFormat;
 
+    @Schema(description = "数据校验级别", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
+    @NotNull(message = "数据校验级别不能为空")
+    private Integer validateType;
+
+    @Schema(description = "产品描述", example = "描述")
+    private String description;
+
 }

+ 15 - 0
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/ProductServiceImpl.java

@@ -10,6 +10,8 @@ import jakarta.annotation.Resource;
 import org.springframework.stereotype.Service;
 import org.springframework.validation.annotation.Validated;
 
+import java.util.UUID;
+
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_NOT_EXISTS;
 
@@ -27,6 +29,8 @@ public class ProductServiceImpl implements ProductService {
 
     @Override
     public Long createProduct(ProductSaveReqVO createReqVO) {
+        // 生成 ProductKey
+        createProductKey(createReqVO);
         // 插入
         ProductDO product = BeanUtils.toBean(createReqVO, ProductDO.class);
         productMapper.insert(product);
@@ -34,6 +38,17 @@ public class ProductServiceImpl implements ProductService {
         return product.getId();
     }
 
+    /**
+     * 创建 ProductKey
+     *
+     * @param createReqVO 创建信息
+     */
+    private void createProductKey(ProductSaveReqVO createReqVO) {
+        // 生成随机的 11 位字符串
+        String productKey = UUID.randomUUID().toString().replace("-", "").substring(0, 11);
+        createReqVO.setProductKey(productKey);
+    }
+
     @Override
     public void updateProduct(ProductSaveReqVO updateReqVO) {
         // 校验存在