diff --git a/yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java b/yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java index 19ee8972d2..d26e5f2ec9 100644 --- a/yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java +++ b/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 { // ========== 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 对应的产品物模型已存在"); } diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.http b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.http index bc0229bfbf..b29ae8ddf2 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.http +++ b/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}} Authorization: Bearer {{token}} { + "productId": 1, "productKey": "123456", "properties": [ { @@ -51,6 +52,8 @@ tenant-id: {{adminTenentId}} Authorization: Bearer {{token}} { + "id": 1, + "productId": 1, "productKey": "123456", "properties": [ { @@ -90,7 +93,7 @@ Authorization: Bearer {{token}} "events": "{}" } -### 请求 /iot/think-model-function/get 接口 => 成功 -GET {{baseUrl}}/iot/think-model-function/get?productKey=123456 +### 请求 /iot/think-model-function/get-by-product-key 接口 => 成功 +GET {{baseUrl}}/iot/think-model-function/get-by-product-key?productKey=123456 tenant-id: {{adminTenentId}} Authorization: Bearer {{token}} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.java index cebaca4f55..a0051d9eb3 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.java @@ -1,22 +1,21 @@ package cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction; -import org.springframework.web.bind.annotation.*; -import jakarta.annotation.Resource; -import org.springframework.validation.annotation.Validated; -import org.springframework.security.access.prepost.PreAuthorize; -import io.swagger.v3.oas.annotations.tags.Tag; -import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.Operation; - -import jakarta.validation.*; - import cn.iocoder.yudao.framework.common.pojo.CommonResult; -import cn.iocoder.yudao.framework.common.util.object.BeanUtils; -import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; - -import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.*; +import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionRespVO; +import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionSaveReqVO; +import cn.iocoder.yudao.module.iot.convert.thinkmodelfunction.IotThinkModelFunctionConvert; import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO; import cn.iocoder.yudao.module.iot.service.thinkmodelfunction.IotThinkModelFunctionService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import jakarta.validation.Valid; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; @Tag(name = "管理后台 - IoT 产品物模型") @RestController @@ -38,7 +37,7 @@ public class IotThinkModelFunctionController { @Operation(summary = "更新IoT 产品物模型") @PreAuthorize("@ss.hasPermission('iot:think-model-function:update')") public CommonResult updateThinkModelFunction(@Valid @RequestBody IotThinkModelFunctionSaveReqVO updateReqVO) { - thinkModelFunctionService.updateThinkModelFunctionByProductKey(updateReqVO); + thinkModelFunctionService.updateThinkModelFunction(updateReqVO); return success(true); } @@ -51,13 +50,23 @@ public class IotThinkModelFunctionController { return success(true); } - @GetMapping("/get") + @GetMapping("/get-by-product-key") @Operation(summary = "获得IoT 产品物模型") @Parameter(name = "productKey", description = "产品Key", required = true, example = "1024") @PreAuthorize("@ss.hasPermission('iot:think-model-function:query')") - public CommonResult getThinkModelFunctionByProductKey(@RequestParam("productKey") String productKey) { + public CommonResult getThinkModelFunctionByProductKey(@RequestParam("productKey") String productKey) { IotThinkModelFunctionDO thinkModelFunction = thinkModelFunctionService.getThinkModelFunctionByProductKey(productKey); - return success(BeanUtils.toBean(thinkModelFunction, IotThinkModelFunctionRespVO.class)); + IotThinkModelFunctionRespVO respVO = IotThinkModelFunctionConvert.INSTANCE.convert(thinkModelFunction); + return success(respVO); } -} \ No newline at end of file + @GetMapping("/get-by-product-id") + @Operation(summary = "获得IoT 产品物模型") + @Parameter(name = "productId", description = "产品ID", required = true, example = "1024") + @PreAuthorize("@ss.hasPermission('iot:think-model-function:query')") + public CommonResult getThinkModelFunctionByProductId(@RequestParam("productId") Long productId) { + IotThinkModelFunctionDO thinkModelFunction = thinkModelFunctionService.getThinkModelFunctionByProductId(productId); + IotThinkModelFunctionRespVO respVO = IotThinkModelFunctionConvert.INSTANCE.convert(thinkModelFunction); + return success(respVO); + } +} diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThinkModelFunctionRespVO.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThinkModelFunctionRespVO.java index 74f7bfb6fa..5d525e5b5e 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThinkModelFunctionRespVO.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThinkModelFunctionRespVO.java @@ -1,10 +1,12 @@ package cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo; +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; +import com.alibaba.excel.annotation.ExcelProperty; import io.swagger.v3.oas.annotations.media.Schema; -import lombok.*; +import lombok.Data; import java.time.LocalDateTime; -import com.alibaba.excel.annotation.*; +import java.util.List; @Schema(description = "管理后台 - IoT 产品物模型 Response VO") @Data @@ -21,7 +23,7 @@ public class IotThinkModelFunctionRespVO { @Schema(description = "属性列表", requiredMode = Schema.RequiredMode.REQUIRED) @ExcelProperty("属性列表") - private String properties; + private List properties; @Schema(description = "服务列表") @ExcelProperty("服务列表") diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThinkModelFunctionSaveReqVO.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThinkModelFunctionSaveReqVO.java index 200e7682cc..7058462102 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThinkModelFunctionSaveReqVO.java +++ b/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.*; @Data public class IotThinkModelFunctionSaveReqVO { + @Schema(description = "编号", example = "1") + private Long id; + + @Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED) + @NotNull(message = "产品ID不能为空") + private Long productId; + @Schema(description = "产品标识", requiredMode = Schema.RequiredMode.REQUIRED) @NotEmpty(message = "产品标识不能为空") private String productKey; diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/convert/thinkmodelfunction/IotThinkModelFunctionConvert.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/convert/thinkmodelfunction/IotThinkModelFunctionConvert.java new file mode 100644 index 0000000000..783ef658a7 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/convert/thinkmodelfunction/IotThinkModelFunctionConvert.java @@ -0,0 +1,51 @@ +package cn.iocoder.yudao.module.iot.convert.thinkmodelfunction; + +import cn.hutool.json.JSONUtil; +import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThingModelProperty; +import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionRespVO; +import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionSaveReqVO; +import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO; +import org.mapstruct.AfterMapping; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.MappingTarget; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +@Mapper +public interface IotThinkModelFunctionConvert { + + IotThinkModelFunctionConvert INSTANCE = Mappers.getMapper(IotThinkModelFunctionConvert.class); + + // 将 SaveReqVO 转换为 DO + @Mapping(target = "properties", ignore = true) + IotThinkModelFunctionDO convert(IotThinkModelFunctionSaveReqVO bean); + + // 将 DO 转换为 RespVO + @Mapping(target = "properties", ignore = true) + IotThinkModelFunctionRespVO convert(IotThinkModelFunctionDO bean); + + // 处理 properties 字段的转换,从 VO 到 DO + @AfterMapping + default void convertPropertiesToDO(IotThinkModelFunctionSaveReqVO source, @MappingTarget IotThinkModelFunctionDO target) { + target.setProperties(JSONUtil.toJsonStr(source.getProperties())); + } + + // 处理 properties 字段的转换,从 DO 到 VO + @AfterMapping + default void convertPropertiesToVO(IotThinkModelFunctionDO source, @MappingTarget IotThinkModelFunctionRespVO target) { + target.setProperties(JSONUtil.toList(source.getProperties(), IotThingModelProperty.class)); + } + + // 批量转换 DO 列表到 RespVO 列表 + List convertList(List list); + + // 批量转换处理 properties 字段 + @AfterMapping + default void convertPropertiesListToVO(List sourceList, @MappingTarget List targetList) { + for (int i = 0; i < sourceList.size(); i++) { + convertPropertiesToVO(sourceList.get(i), targetList.get(i)); + } + } +} diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/thinkmodelfunction/IotThinkModelFunctionDO.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/thinkmodelfunction/IotThinkModelFunctionDO.java index 7d9ea4589c..564eefb2b5 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/thinkmodelfunction/IotThinkModelFunctionDO.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/thinkmodelfunction/IotThinkModelFunctionDO.java @@ -1,8 +1,10 @@ package cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction; -import lombok.*; -import com.baomidou.mybatisplus.annotation.*; import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; +import com.baomidou.mybatisplus.annotation.KeySequence; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.*; /** * IoT 产品物模型 DO @@ -24,18 +26,27 @@ public class IotThinkModelFunctionDO extends BaseDO { */ @TableId private Long id; + + /** + * 产品标识 + */ + private Long productId; + /** * 产品标识 */ private String productKey; + /** * 属性列表 */ private String properties; + /** * 服务列表 */ private String services; + /** * 事件列表 */ diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/thinkmodelfunction/IotThinkModelFunctionMapper.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/thinkmodelfunction/IotThinkModelFunctionMapper.java index 5475a723b9..21ae1967a5 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/thinkmodelfunction/IotThinkModelFunctionMapper.java +++ b/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().eq(IotThinkModelFunctionDO::getProductKey, productKey)); } - default int updateByProductKey(IotThinkModelFunctionDO thinkModelFunction) { - return update(thinkModelFunction, new LambdaQueryWrapperX() - .eq(IotThinkModelFunctionDO::getProductKey, thinkModelFunction.getProductKey()) - ); + default IotThinkModelFunctionDO selectByProductId(Long productId){ + return selectOne(new LambdaQueryWrapperX().eq(IotThinkModelFunctionDO::getProductId, productId)); } + } \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionService.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionService.java index a521428100..d24ce00316 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionService.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionService.java @@ -27,17 +27,25 @@ public interface IotThinkModelFunctionService { void deleteThinkModelFunction(Long id); /** - * 获得IoT 产品物模型 + * 获得IoT 产品物模型,通过产品Key * * @param productKey 产品Key * @return IoT 产品物模型 */ IotThinkModelFunctionDO getThinkModelFunctionByProductKey(String productKey); + /** + * 获得IoT 产品物模型,通过产品ID + * + * @param productId 产品ID + * @return IoT 产品物模型 + */ + IotThinkModelFunctionDO getThinkModelFunctionByProductId(Long productId); + /** * 更新IoT 产品物模型 * * @param updateReqVO 更新信息 */ - void updateThinkModelFunctionByProductKey(@Valid IotThinkModelFunctionSaveReqVO updateReqVO); + void updateThinkModelFunction(@Valid IotThinkModelFunctionSaveReqVO updateReqVO); } \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java index 1f1355dd3d..89c5f6b98a 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java @@ -1,22 +1,20 @@ package cn.iocoder.yudao.module.iot.service.thinkmodelfunction; -import cn.hutool.json.JSONUtil; -import cn.iocoder.yudao.framework.common.util.object.BeanUtils; +import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionSaveReqVO; +import cn.iocoder.yudao.module.iot.convert.thinkmodelfunction.IotThinkModelFunctionConvert; import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO; import cn.iocoder.yudao.module.iot.dal.mysql.thinkmodelfunction.IotThinkModelFunctionMapper; import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.THINK_MODEL_FUNCTION_EXISTS_BY_PRODUCT_KEY; import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.THINK_MODEL_FUNCTION_NOT_EXISTS; -/** - * IoT 产品物模型 Service 实现类 - * - * @author 芋道源码 - */ +@Slf4j @Service @Validated public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionService { @@ -26,17 +24,25 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe @Override public Long createThinkModelFunction(IotThinkModelFunctionSaveReqVO createReqVO) { + log.info("创建物模型,参数:{}", createReqVO); + // 验证 ProductKey 对应的产品物模型是否已存在 + validateThinkModelFunctionNotExistsByProductKey(createReqVO.getProductKey()); // 插入 - IotThinkModelFunctionDO thinkModelFunction = BeanUtils.toBean(createReqVO, IotThinkModelFunctionDO.class); - // properties 字段,需要转换成 JSON - thinkModelFunction.setProperties(JSONUtil.toJsonStr(createReqVO.getProperties())); + IotThinkModelFunctionDO thinkModelFunction = IotThinkModelFunctionConvert.INSTANCE.convert(createReqVO); thinkModelFunctionMapper.insert(thinkModelFunction); // 返回 return thinkModelFunction.getId(); } + private void validateThinkModelFunctionNotExistsByProductKey(String productKey) { + if (thinkModelFunctionMapper.selectByProductKey(productKey) != null) { + throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_PRODUCT_KEY); + } + } + @Override public void deleteThinkModelFunction(Long id) { + log.info("删除物模型,id:{}", id); // 校验存在 validateThinkModelFunctionExists(id); // 删除 @@ -49,26 +55,32 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe } } - private void validateThinkModelFunctionExistsByProductKey(String productKey) { - if (thinkModelFunctionMapper.selectByProductKey(productKey) == null) { - throw exception(THINK_MODEL_FUNCTION_NOT_EXISTS); - } - } - @Override public IotThinkModelFunctionDO getThinkModelFunctionByProductKey(String productKey) { return thinkModelFunctionMapper.selectByProductKey(productKey); } @Override - public void updateThinkModelFunctionByProductKey(IotThinkModelFunctionSaveReqVO updateReqVO) { - // 校验存在 - validateThinkModelFunctionExistsByProductKey(updateReqVO.getProductKey()); - // 更新 - IotThinkModelFunctionDO thinkModelFunction = BeanUtils.toBean(updateReqVO, IotThinkModelFunctionDO.class); - // properties 字段,需要转换成 JSON - thinkModelFunction.setProperties(JSONUtil.toJsonStr(updateReqVO.getProperties())); - thinkModelFunctionMapper.updateByProductKey(thinkModelFunction); + public IotThinkModelFunctionDO getThinkModelFunctionByProductId(Long productId) { + return thinkModelFunctionMapper.selectByProductId(productId); } + @Override + public void updateThinkModelFunction(IotThinkModelFunctionSaveReqVO updateReqVO) { + log.info("更新物模型,参数:{}", updateReqVO); + // 校验存在 + validateThinkModelFunctionExists(updateReqVO.getId()); + // 校验 productKey 是否重复 + validateProductKeyUnique(updateReqVO.getId(), updateReqVO.getProductKey()); + // 更新 + IotThinkModelFunctionDO thinkModelFunction = IotThinkModelFunctionConvert.INSTANCE.convert(updateReqVO); + thinkModelFunctionMapper.updateById(thinkModelFunction); + } + + private void validateProductKeyUnique(Long id, String productKey) { + IotThinkModelFunctionDO existingFunction = thinkModelFunctionMapper.selectByProductKey(productKey); + if (existingFunction != null && !existingFunction.getId().equals(id)) { + throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_PRODUCT_KEY); + } + } } \ No newline at end of file