Commit 9e77692414dc805be24c8ee12d5ccc3c05d1bb23

Authored by 安浩浩
1 parent 18e789d4

修改:优化 IOT 物模型 修改和查询接口

yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java
... ... @@ -16,4 +16,5 @@ public interface ErrorCodeConstants {
16 16  
17 17 // ========== IoT 产品物模型 1-050-002-000 ============
18 18 ErrorCode THINK_MODEL_FUNCTION_NOT_EXISTS = new ErrorCode(1_050_002_000, "产品物模型不存在");
  19 + ErrorCode THINK_MODEL_FUNCTION_EXISTS_BY_PRODUCT_KEY = new ErrorCode(1_050_002_001, "ProductKey 对应的产品物模型已存在");
19 20 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.http
... ... @@ -5,6 +5,7 @@ tenant-id: {{adminTenentId}}
5 5 Authorization: Bearer {{token}}
6 6  
7 7 {
  8 + "productId": 1,
8 9 "productKey": "123456",
9 10 "properties": [
10 11 {
... ... @@ -51,6 +52,8 @@ tenant-id: {{adminTenentId}}
51 52 Authorization: Bearer {{token}}
52 53  
53 54 {
  55 + "id": 1,
  56 + "productId": 1,
54 57 "productKey": "123456",
55 58 "properties": [
56 59 {
... ... @@ -90,7 +93,7 @@ Authorization: Bearer {{token}}
90 93 "events": "{}"
91 94 }
92 95  
93   -### 请求 /iot/think-model-function/get 接口 => 成功
94   -GET {{baseUrl}}/iot/think-model-function/get?productKey=123456
  96 +### 请求 /iot/think-model-function/get-by-product-key 接口 => 成功
  97 +GET {{baseUrl}}/iot/think-model-function/get-by-product-key?productKey=123456
95 98 tenant-id: {{adminTenentId}}
96 99 Authorization: Bearer {{token}}
97 100 \ No newline at end of file
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.java
1 1 package cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction;
2 2  
3   -import org.springframework.web.bind.annotation.*;
  3 +import cn.iocoder.yudao.framework.common.pojo.CommonResult;
  4 +import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionRespVO;
  5 +import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionSaveReqVO;
  6 +import cn.iocoder.yudao.module.iot.convert.thinkmodelfunction.IotThinkModelFunctionConvert;
  7 +import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO;
  8 +import cn.iocoder.yudao.module.iot.service.thinkmodelfunction.IotThinkModelFunctionService;
  9 +import io.swagger.v3.oas.annotations.Operation;
  10 +import io.swagger.v3.oas.annotations.Parameter;
  11 +import io.swagger.v3.oas.annotations.tags.Tag;
4 12 import jakarta.annotation.Resource;
5   -import org.springframework.validation.annotation.Validated;
  13 +import jakarta.validation.Valid;
6 14 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;
10   -
11   -import jakarta.validation.*;
  15 +import org.springframework.validation.annotation.Validated;
  16 +import org.springframework.web.bind.annotation.*;
12 17  
13   -import cn.iocoder.yudao.framework.common.pojo.CommonResult;
14   -import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
15 18 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
16 19  
17   -import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.*;
18   -import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO;
19   -import cn.iocoder.yudao.module.iot.service.thinkmodelfunction.IotThinkModelFunctionService;
20   -
21 20 @Tag(name = "管理后台 - IoT 产品物模型")
22 21 @RestController
23 22 @RequestMapping("/iot/think-model-function")
... ... @@ -38,7 +37,7 @@ public class IotThinkModelFunctionController {
38 37 @Operation(summary = "更新IoT 产品物模型")
39 38 @PreAuthorize("@ss.hasPermission('iot:think-model-function:update')")
40 39 public CommonResult<Boolean> updateThinkModelFunction(@Valid @RequestBody IotThinkModelFunctionSaveReqVO updateReqVO) {
41   - thinkModelFunctionService.updateThinkModelFunctionByProductKey(updateReqVO);
  40 + thinkModelFunctionService.updateThinkModelFunction(updateReqVO);
42 41 return success(true);
43 42 }
44 43  
... ... @@ -51,13 +50,23 @@ public class IotThinkModelFunctionController {
51 50 return success(true);
52 51 }
53 52  
54   - @GetMapping("/get")
  53 + @GetMapping("/get-by-product-key")
55 54 @Operation(summary = "获得IoT 产品物模型")
56 55 @Parameter(name = "productKey", description = "产品Key", required = true, example = "1024")
57 56 @PreAuthorize("@ss.hasPermission('iot:think-model-function:query')")
58   - public CommonResult<IotThinkModelFunctionRespVO> getThinkModelFunctionByProductKey(@RequestParam("productKey") String productKey) {
  57 + public CommonResult<IotThinkModelFunctionRespVO> getThinkModelFunctionByProductKey(@RequestParam("productKey") String productKey) {
59 58 IotThinkModelFunctionDO thinkModelFunction = thinkModelFunctionService.getThinkModelFunctionByProductKey(productKey);
60   - return success(BeanUtils.toBean(thinkModelFunction, IotThinkModelFunctionRespVO.class));
  59 + IotThinkModelFunctionRespVO respVO = IotThinkModelFunctionConvert.INSTANCE.convert(thinkModelFunction);
  60 + return success(respVO);
61 61 }
62 62  
63   -}
64 63 \ No newline at end of file
  64 + @GetMapping("/get-by-product-id")
  65 + @Operation(summary = "获得IoT 产品物模型")
  66 + @Parameter(name = "productId", description = "产品ID", required = true, example = "1024")
  67 + @PreAuthorize("@ss.hasPermission('iot:think-model-function:query')")
  68 + public CommonResult<IotThinkModelFunctionRespVO> getThinkModelFunctionByProductId(@RequestParam("productId") Long productId) {
  69 + IotThinkModelFunctionDO thinkModelFunction = thinkModelFunctionService.getThinkModelFunctionByProductId(productId);
  70 + IotThinkModelFunctionRespVO respVO = IotThinkModelFunctionConvert.INSTANCE.convert(thinkModelFunction);
  71 + return success(respVO);
  72 + }
  73 +}
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThinkModelFunctionRespVO.java
1 1 package cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo;
2 2  
  3 +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
  4 +import com.alibaba.excel.annotation.ExcelProperty;
3 5 import io.swagger.v3.oas.annotations.media.Schema;
4   -import lombok.*;
  6 +import lombok.Data;
5 7  
6 8 import java.time.LocalDateTime;
7   -import com.alibaba.excel.annotation.*;
  9 +import java.util.List;
8 10  
9 11 @Schema(description = "管理后台 - IoT 产品物模型 Response VO")
10 12 @Data
... ... @@ -21,7 +23,7 @@ public class IotThinkModelFunctionRespVO {
21 23  
22 24 @Schema(description = "属性列表", requiredMode = Schema.RequiredMode.REQUIRED)
23 25 @ExcelProperty("属性列表")
24   - private String properties;
  26 + private List<IotThingModelProperty> properties;
25 27  
26 28 @Schema(description = "服务列表")
27 29 @ExcelProperty("服务列表")
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThinkModelFunctionSaveReqVO.java
... ... @@ -9,6 +9,13 @@ import jakarta.validation.constraints.*;
9 9 @Data
10 10 public class IotThinkModelFunctionSaveReqVO {
11 11  
  12 + @Schema(description = "编号", example = "1")
  13 + private Long id;
  14 +
  15 + @Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED)
  16 + @NotNull(message = "产品ID不能为空")
  17 + private Long productId;
  18 +
12 19 @Schema(description = "产品标识", requiredMode = Schema.RequiredMode.REQUIRED)
13 20 @NotEmpty(message = "产品标识不能为空")
14 21 private String productKey;
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/convert/thinkmodelfunction/IotThinkModelFunctionConvert.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.convert.thinkmodelfunction;
  2 +
  3 +import cn.hutool.json.JSONUtil;
  4 +import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThingModelProperty;
  5 +import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionRespVO;
  6 +import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionSaveReqVO;
  7 +import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO;
  8 +import org.mapstruct.AfterMapping;
  9 +import org.mapstruct.Mapper;
  10 +import org.mapstruct.Mapping;
  11 +import org.mapstruct.MappingTarget;
  12 +import org.mapstruct.factory.Mappers;
  13 +
  14 +import java.util.List;
  15 +
  16 +@Mapper
  17 +public interface IotThinkModelFunctionConvert {
  18 +
  19 + IotThinkModelFunctionConvert INSTANCE = Mappers.getMapper(IotThinkModelFunctionConvert.class);
  20 +
  21 + // 将 SaveReqVO 转换为 DO
  22 + @Mapping(target = "properties", ignore = true)
  23 + IotThinkModelFunctionDO convert(IotThinkModelFunctionSaveReqVO bean);
  24 +
  25 + // 将 DO 转换为 RespVO
  26 + @Mapping(target = "properties", ignore = true)
  27 + IotThinkModelFunctionRespVO convert(IotThinkModelFunctionDO bean);
  28 +
  29 + // 处理 properties 字段的转换,从 VO 到 DO
  30 + @AfterMapping
  31 + default void convertPropertiesToDO(IotThinkModelFunctionSaveReqVO source, @MappingTarget IotThinkModelFunctionDO target) {
  32 + target.setProperties(JSONUtil.toJsonStr(source.getProperties()));
  33 + }
  34 +
  35 + // 处理 properties 字段的转换,从 DO 到 VO
  36 + @AfterMapping
  37 + default void convertPropertiesToVO(IotThinkModelFunctionDO source, @MappingTarget IotThinkModelFunctionRespVO target) {
  38 + target.setProperties(JSONUtil.toList(source.getProperties(), IotThingModelProperty.class));
  39 + }
  40 +
  41 + // 批量转换 DO 列表到 RespVO 列表
  42 + List<IotThinkModelFunctionRespVO> convertList(List<IotThinkModelFunctionDO> list);
  43 +
  44 + // 批量转换处理 properties 字段
  45 + @AfterMapping
  46 + default void convertPropertiesListToVO(List<IotThinkModelFunctionDO> sourceList, @MappingTarget List<IotThinkModelFunctionRespVO> targetList) {
  47 + for (int i = 0; i < sourceList.size(); i++) {
  48 + convertPropertiesToVO(sourceList.get(i), targetList.get(i));
  49 + }
  50 + }
  51 +}
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/thinkmodelfunction/IotThinkModelFunctionDO.java
1 1 package cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction;
2 2  
3   -import lombok.*;
4   -import com.baomidou.mybatisplus.annotation.*;
5 3 import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
  4 +import com.baomidou.mybatisplus.annotation.KeySequence;
  5 +import com.baomidou.mybatisplus.annotation.TableId;
  6 +import com.baomidou.mybatisplus.annotation.TableName;
  7 +import lombok.*;
6 8  
7 9 /**
8 10 * IoT 产品物模型 DO
... ... @@ -24,18 +26,27 @@ public class IotThinkModelFunctionDO extends BaseDO {
24 26 */
25 27 @TableId
26 28 private Long id;
  29 +
  30 + /**
  31 + * 产品标识
  32 + */
  33 + private Long productId;
  34 +
27 35 /**
28 36 * 产品标识
29 37 */
30 38 private String productKey;
  39 +
31 40 /**
32 41 * 属性列表
33 42 */
34 43 private String properties;
  44 +
35 45 /**
36 46 * 服务列表
37 47 */
38 48 private String services;
  49 +
39 50 /**
40 51 * 事件列表
41 52 */
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/thinkmodelfunction/IotThinkModelFunctionMapper.java
... ... @@ -17,9 +17,8 @@ public interface IotThinkModelFunctionMapper extends BaseMapperX&lt;IotThinkModelFu
17 17 return selectOne(new LambdaQueryWrapperX<IotThinkModelFunctionDO>().eq(IotThinkModelFunctionDO::getProductKey, productKey));
18 18 }
19 19  
20   - default int updateByProductKey(IotThinkModelFunctionDO thinkModelFunction) {
21   - return update(thinkModelFunction, new LambdaQueryWrapperX<IotThinkModelFunctionDO>()
22   - .eq(IotThinkModelFunctionDO::getProductKey, thinkModelFunction.getProductKey())
23   - );
  20 + default IotThinkModelFunctionDO selectByProductId(Long productId){
  21 + return selectOne(new LambdaQueryWrapperX<IotThinkModelFunctionDO>().eq(IotThinkModelFunctionDO::getProductId, productId));
24 22 }
  23 +
25 24 }
26 25 \ No newline at end of file
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionService.java
... ... @@ -27,7 +27,7 @@ public interface IotThinkModelFunctionService {
27 27 void deleteThinkModelFunction(Long id);
28 28  
29 29 /**
30   - * 获得IoT 产品物模型
  30 + * 获得IoT 产品物模型,通过产品Key
31 31 *
32 32 * @param productKey 产品Key
33 33 * @return IoT 产品物模型
... ... @@ -35,9 +35,17 @@ public interface IotThinkModelFunctionService {
35 35 IotThinkModelFunctionDO getThinkModelFunctionByProductKey(String productKey);
36 36  
37 37 /**
  38 + * 获得IoT 产品物模型,通过产品ID
  39 + *
  40 + * @param productId 产品ID
  41 + * @return IoT 产品物模型
  42 + */
  43 + IotThinkModelFunctionDO getThinkModelFunctionByProductId(Long productId);
  44 +
  45 + /**
38 46 * 更新IoT 产品物模型
39 47 *
40 48 * @param updateReqVO 更新信息
41 49 */
42   - void updateThinkModelFunctionByProductKey(@Valid IotThinkModelFunctionSaveReqVO updateReqVO);
  50 + void updateThinkModelFunction(@Valid IotThinkModelFunctionSaveReqVO updateReqVO);
43 51 }
44 52 \ 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
1 1 package cn.iocoder.yudao.module.iot.service.thinkmodelfunction;
2 2  
3   -import cn.hutool.json.JSONUtil;
4   -import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
  3 +import cn.iocoder.yudao.framework.common.exception.ServiceException;
5 4 import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionSaveReqVO;
  5 +import cn.iocoder.yudao.module.iot.convert.thinkmodelfunction.IotThinkModelFunctionConvert;
6 6 import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO;
7 7 import cn.iocoder.yudao.module.iot.dal.mysql.thinkmodelfunction.IotThinkModelFunctionMapper;
8 8 import jakarta.annotation.Resource;
  9 +import lombok.extern.slf4j.Slf4j;
9 10 import org.springframework.stereotype.Service;
10 11 import org.springframework.validation.annotation.Validated;
11 12  
12 13 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  14 +import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.THINK_MODEL_FUNCTION_EXISTS_BY_PRODUCT_KEY;
13 15 import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.THINK_MODEL_FUNCTION_NOT_EXISTS;
14 16  
15   -/**
16   - * IoT 产品物模型 Service 实现类
17   - *
18   - * @author 芋道源码
19   - */
  17 +@Slf4j
20 18 @Service
21 19 @Validated
22 20 public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionService {
... ... @@ -26,17 +24,25 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
26 24  
27 25 @Override
28 26 public Long createThinkModelFunction(IotThinkModelFunctionSaveReqVO createReqVO) {
  27 + log.info("创建物模型,参数:{}", createReqVO);
  28 + // 验证 ProductKey 对应的产品物模型是否已存在
  29 + validateThinkModelFunctionNotExistsByProductKey(createReqVO.getProductKey());
29 30 // 插入
30   - IotThinkModelFunctionDO thinkModelFunction = BeanUtils.toBean(createReqVO, IotThinkModelFunctionDO.class);
31   - // properties 字段,需要转换成 JSON
32   - thinkModelFunction.setProperties(JSONUtil.toJsonStr(createReqVO.getProperties()));
  31 + IotThinkModelFunctionDO thinkModelFunction = IotThinkModelFunctionConvert.INSTANCE.convert(createReqVO);
33 32 thinkModelFunctionMapper.insert(thinkModelFunction);
34 33 // 返回
35 34 return thinkModelFunction.getId();
36 35 }
37 36  
  37 + private void validateThinkModelFunctionNotExistsByProductKey(String productKey) {
  38 + if (thinkModelFunctionMapper.selectByProductKey(productKey) != null) {
  39 + throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_PRODUCT_KEY);
  40 + }
  41 + }
  42 +
38 43 @Override
39 44 public void deleteThinkModelFunction(Long id) {
  45 + log.info("删除物模型,id:{}", id);
40 46 // 校验存在
41 47 validateThinkModelFunctionExists(id);
42 48 // 删除
... ... @@ -49,26 +55,32 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
49 55 }
50 56 }
51 57  
52   - private void validateThinkModelFunctionExistsByProductKey(String productKey) {
53   - if (thinkModelFunctionMapper.selectByProductKey(productKey) == null) {
54   - throw exception(THINK_MODEL_FUNCTION_NOT_EXISTS);
55   - }
56   - }
57   -
58 58 @Override
59 59 public IotThinkModelFunctionDO getThinkModelFunctionByProductKey(String productKey) {
60 60 return thinkModelFunctionMapper.selectByProductKey(productKey);
61 61 }
62 62  
63 63 @Override
64   - public void updateThinkModelFunctionByProductKey(IotThinkModelFunctionSaveReqVO updateReqVO) {
  64 + public IotThinkModelFunctionDO getThinkModelFunctionByProductId(Long productId) {
  65 + return thinkModelFunctionMapper.selectByProductId(productId);
  66 + }
  67 +
  68 + @Override
  69 + public void updateThinkModelFunction(IotThinkModelFunctionSaveReqVO updateReqVO) {
  70 + log.info("更新物模型,参数:{}", updateReqVO);
65 71 // 校验存在
66   - validateThinkModelFunctionExistsByProductKey(updateReqVO.getProductKey());
  72 + validateThinkModelFunctionExists(updateReqVO.getId());
  73 + // 校验 productKey 是否重复
  74 + validateProductKeyUnique(updateReqVO.getId(), updateReqVO.getProductKey());
67 75 // 更新
68   - IotThinkModelFunctionDO thinkModelFunction = BeanUtils.toBean(updateReqVO, IotThinkModelFunctionDO.class);
69   - // properties 字段,需要转换成 JSON
70   - thinkModelFunction.setProperties(JSONUtil.toJsonStr(updateReqVO.getProperties()));
71   - thinkModelFunctionMapper.updateByProductKey(thinkModelFunction);
  76 + IotThinkModelFunctionDO thinkModelFunction = IotThinkModelFunctionConvert.INSTANCE.convert(updateReqVO);
  77 + thinkModelFunctionMapper.updateById(thinkModelFunction);
72 78 }
73 79  
  80 + private void validateProductKeyUnique(Long id, String productKey) {
  81 + IotThinkModelFunctionDO existingFunction = thinkModelFunctionMapper.selectByProductKey(productKey);
  82 + if (existingFunction != null && !existingFunction.getId().equals(id)) {
  83 + throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_PRODUCT_KEY);
  84 + }
  85 + }
74 86 }
75 87 \ No newline at end of file
... ...