From 18e789d4fb988dd4014a65fb4bda5ea90aa8307e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E6=B5=A9=E6=B5=A9?= <1036606149@qq.com> Date: Wed, 11 Sep 2024 23:00:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=20IOT=20=E7=89=A9=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/iot/enums/ErrorCodeConstants.java | 5 +- .../IotThinkModelFunctionController.http | 96 +++++++++++++++++++ .../IotThinkModelFunctionController.java | 63 ++++++++++++ .../vo/IotThingModelProperty.java | 90 +++++++++++++++++ .../vo/IotThinkModelFunctionRespVO.java | 38 ++++++++ .../vo/IotThinkModelFunctionSaveReqVO.java | 26 +++++ .../IotThinkModelFunctionDO.java | 44 +++++++++ .../IotThinkModelFunctionMapper.java | 25 +++++ .../product/IotProductServiceImpl.java | 5 +- .../IotThinkModelFunctionService.java | 43 +++++++++ .../IotThinkModelFunctionServiceImpl.java | 74 ++++++++++++++ .../IotThinkModelFunctionMapper.xml | 12 +++ .../IotThinkModelFunctionServiceImplTest.java | 71 ++++++++++++++ 13 files changed, 588 insertions(+), 4 deletions(-) create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.http create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.java create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThingModelProperty.java create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThinkModelFunctionRespVO.java create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThinkModelFunctionSaveReqVO.java create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/thinkmodelfunction/IotThinkModelFunctionDO.java create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/thinkmodelfunction/IotThinkModelFunctionMapper.java create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionService.java create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/thinkmodelfunction/IotThinkModelFunctionMapper.xml create mode 100644 yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImplTest.java 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 3fd09ce8e..19ee8972d 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 @@ -9,8 +9,11 @@ import cn.iocoder.yudao.framework.common.exception.ErrorCode; */ public interface ErrorCodeConstants { - // ========== 产品相关 1-050-001-000 ============ + // ========== IoT 产品相关 1-050-001-000 ============ ErrorCode PRODUCT_NOT_EXISTS = new ErrorCode(1_050_001_000, "产品不存在"); ErrorCode PRODUCT_IDENTIFICATION_EXISTS = new ErrorCode(1_050_001_001, "产品标识已经存在"); ErrorCode PRODUCT_STATUS_NOT_DELETE = new ErrorCode(1_050_001_002, "产品状是发布状态,不允许删除"); + + // ========== IoT 产品物模型 1-050-002-000 ============ + ErrorCode THINK_MODEL_FUNCTION_NOT_EXISTS = new ErrorCode(1_050_002_000, "产品物模型不存在"); } 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 new file mode 100644 index 000000000..bc0229bfb --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.http @@ -0,0 +1,96 @@ +### 请求 /iot/think-model-function/create 接口 => 成功 +POST {{baseUrl}}/iot/think-model-function/create +Content-Type: application/json +tenant-id: {{adminTenentId}} +Authorization: Bearer {{token}} + +{ + "productKey": "123456", + "properties": [ + { + "identifier": "CurrentTemperature", + "name": "当前温度", + "accessMode": "r", + "required": true, + "dataType": { + "type": "float", + "specs": { + "min": "-40", + "max": "120", + "unit": "°C", + "unitName": "摄氏度", + "step": "0.1" + } + } + }, + { + "identifier": "CurrentHumidity", + "name": "当前湿度", + "accessMode": "r", + "required": true, + "dataType": { + "type": "float", + "specs": { + "min": "0", + "max": "100", + "unit": "%", + "unitName": "百分比", + "step": "0.1" + } + } + } + ], + "services": "{}", + "events": "{}" +} + +### 请求 /iot/think-model-function/update 接口 => 成功 +PUT {{baseUrl}}/iot/think-model-function/update +Content-Type: application/json +tenant-id: {{adminTenentId}} +Authorization: Bearer {{token}} + +{ + "productKey": "123456", + "properties": [ + { + "identifier": "CurrentTemperature", + "name": "当前温度", + "accessMode": "r", + "required": true, + "dataType": { + "type": "float", + "specs": { + "min": "-40", + "max": "130", + "unit": "°C", + "unitName": "摄氏度", + "step": "0.1" + } + } + }, + { + "identifier": "CurrentHumidity", + "name": "当前湿度", + "accessMode": "r", + "required": true, + "dataType": { + "type": "float", + "specs": { + "min": "0", + "max": "100", + "unit": "%", + "unitName": "百分比", + "step": "0.1" + } + } + } + ], + "services": "{}", + "events": "{}" +} + +### 请求 /iot/think-model-function/get 接口 => 成功 +GET {{baseUrl}}/iot/think-model-function/get?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 new file mode 100644 index 000000000..cebaca4f5 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.java @@ -0,0 +1,63 @@ +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.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO; +import cn.iocoder.yudao.module.iot.service.thinkmodelfunction.IotThinkModelFunctionService; + +@Tag(name = "管理后台 - IoT 产品物模型") +@RestController +@RequestMapping("/iot/think-model-function") +@Validated +public class IotThinkModelFunctionController { + + @Resource + private IotThinkModelFunctionService thinkModelFunctionService; + + @PostMapping("/create") + @Operation(summary = "创建IoT 产品物模型") + @PreAuthorize("@ss.hasPermission('iot:think-model-function:create')") + public CommonResult createThinkModelFunction(@Valid @RequestBody IotThinkModelFunctionSaveReqVO createReqVO) { + return success(thinkModelFunctionService.createThinkModelFunction(createReqVO)); + } + + @PutMapping("/update") + @Operation(summary = "更新IoT 产品物模型") + @PreAuthorize("@ss.hasPermission('iot:think-model-function:update')") + public CommonResult updateThinkModelFunction(@Valid @RequestBody IotThinkModelFunctionSaveReqVO updateReqVO) { + thinkModelFunctionService.updateThinkModelFunctionByProductKey(updateReqVO); + return success(true); + } + + @DeleteMapping("/delete") + @Operation(summary = "删除IoT 产品物模型") + @Parameter(name = "id", description = "编号", required = true) + @PreAuthorize("@ss.hasPermission('iot:think-model-function:delete')") + public CommonResult deleteThinkModelFunction(@RequestParam("id") Long id) { + thinkModelFunctionService.deleteThinkModelFunction(id); + return success(true); + } + + @GetMapping("/get") + @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) { + IotThinkModelFunctionDO thinkModelFunction = thinkModelFunctionService.getThinkModelFunctionByProductKey(productKey); + return success(BeanUtils.toBean(thinkModelFunction, IotThinkModelFunctionRespVO.class)); + } + +} \ 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/vo/IotThingModelProperty.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThingModelProperty.java new file mode 100644 index 000000000..0187ae514 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThingModelProperty.java @@ -0,0 +1,90 @@ +package cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.util.List; + +@Schema(description = "管理后台 - IoT 产品物模型属性") +@Data +public class IotThingModelProperty { + + @Schema(description = "属性标识符") + private String identifier; + + @Schema(description = "属性名称") + private String name; + + @Schema(description = "访问模式 (r/rw)") + private String accessMode; + + @Schema(description = "是否必需") + private boolean required; + + @Schema(description = "数据类型") + private DataType dataType; + + @Schema(description = "数据类型") + @Data + public static class DataType { + + @Schema(description = "数据类型(float, double, struct, enum等)") + private String type; + + @Schema(description = "单一类型的规格(适用于float, double等)") + private Specs specs; + + @Schema(description = "结构体字段(适用于struct类型)") + private List structSpecs; + + @Schema(description = "规格") + @Data + public static class Specs { + + @Schema(description = "最小值") + private String min; + + @Schema(description = "最大值") + private String max; + + @Schema(description = "单位符号") + private String unit; + + @Schema(description = "单位名称") + private String unitName; + + @Schema(description = "步进值") + private String step; + } + + @Schema(description = "结构体字段") + @Data + public static class StructField { + + @Schema(description = "字段标识符") + private String identifier; + + @Schema(description = "字段名称") + private String name; + + @Schema(description = "字段的数据类型") + private DataType dataType; + } + } + + @Schema(description = "枚举规格") + @Data + public static class EnumSpecs { + + @Schema(description = "枚举值") + private int value; + + @Schema(description = "枚举名称") + private String name; + + public EnumSpecs(int value, String name) { + this.value = value; + this.name = name; + } + } +} 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 new file mode 100644 index 000000000..74f7bfb6f --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThinkModelFunctionRespVO.java @@ -0,0 +1,38 @@ +package cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.*; + +import java.time.LocalDateTime; +import com.alibaba.excel.annotation.*; + +@Schema(description = "管理后台 - IoT 产品物模型 Response VO") +@Data +@ExcelIgnoreUnannotated +public class IotThinkModelFunctionRespVO { + + @Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "21816") + @ExcelProperty("产品ID") + private Long id; + + @Schema(description = "产品标识", requiredMode = Schema.RequiredMode.REQUIRED) + @ExcelProperty("产品标识") + private String productKey; + + @Schema(description = "属性列表", requiredMode = Schema.RequiredMode.REQUIRED) + @ExcelProperty("属性列表") + private String properties; + + @Schema(description = "服务列表") + @ExcelProperty("服务列表") + private String services; + + @Schema(description = "事件列表") + @ExcelProperty("事件列表") + private String events; + + @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) + @ExcelProperty("创建时间") + private LocalDateTime createTime; + +} \ 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/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 new file mode 100644 index 000000000..200e7682c --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/vo/IotThinkModelFunctionSaveReqVO.java @@ -0,0 +1,26 @@ +package cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.*; +import java.util.*; +import jakarta.validation.constraints.*; + +@Schema(description = "管理后台 - IoT 产品物模型新增/修改 Request VO") +@Data +public class IotThinkModelFunctionSaveReqVO { + + @Schema(description = "产品标识", requiredMode = Schema.RequiredMode.REQUIRED) + @NotEmpty(message = "产品标识不能为空") + private String productKey; + + @Schema(description = "属性列表", requiredMode = Schema.RequiredMode.REQUIRED) + @NotEmpty(message = "属性列表不能为空") + private List properties; + + @Schema(description = "服务列表") + private String services; + + @Schema(description = "事件列表") + private String events; + +} \ No newline at end of file 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 new file mode 100644 index 000000000..7d9ea4589 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/thinkmodelfunction/IotThinkModelFunctionDO.java @@ -0,0 +1,44 @@ +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; + +/** + * IoT 产品物模型 DO + * + * @author 芋道源码 + */ +@TableName("iot_think_model_function") +@KeySequence("iot_think_model_function_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。 +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class IotThinkModelFunctionDO extends BaseDO { + + /** + * 产品ID + */ + @TableId + private Long id; + /** + * 产品标识 + */ + private String productKey; + /** + * 属性列表 + */ + private String properties; + /** + * 服务列表 + */ + private String services; + /** + * 事件列表 + */ + private String events; + +} \ No newline at end of file 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 new file mode 100644 index 000000000..5475a723b --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/thinkmodelfunction/IotThinkModelFunctionMapper.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.module.iot.dal.mysql.thinkmodelfunction; + +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; +import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO; +import org.apache.ibatis.annotations.Mapper; + +/** + * IoT 产品物模型 Mapper + * + * @author 芋道源码 + */ +@Mapper +public interface IotThinkModelFunctionMapper extends BaseMapperX { + + default IotThinkModelFunctionDO selectByProductKey(String productKey) { + return selectOne(new LambdaQueryWrapperX().eq(IotThinkModelFunctionDO::getProductKey, productKey)); + } + + default int updateByProductKey(IotThinkModelFunctionDO thinkModelFunction) { + return update(thinkModelFunction, new LambdaQueryWrapperX() + .eq(IotThinkModelFunctionDO::getProductKey, thinkModelFunction.getProductKey()) + ); + } +} \ 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/product/IotProductServiceImpl.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductServiceImpl.java index 844e074d9..96975c27f 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductServiceImpl.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductServiceImpl.java @@ -16,8 +16,7 @@ import java.util.Objects; 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; -import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_STATUS_NOT_DELETE; +import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*; /** * IoT 产品 Service 实现类 @@ -54,7 +53,7 @@ public class IotProductServiceImpl implements IotProductService { } // 2. 校验唯一性 if (productMapper.selectByProductKey(productKey) != null) { - throw exception(PRODUCT_NOT_EXISTS); + throw exception(PRODUCT_IDENTIFICATION_EXISTS); } createReqVO.setProductKey(productKey); } 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 new file mode 100644 index 000000000..a52142810 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionService.java @@ -0,0 +1,43 @@ +package cn.iocoder.yudao.module.iot.service.thinkmodelfunction; + +import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionSaveReqVO; +import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO; +import jakarta.validation.Valid; + +/** + * IoT 产品物模型 Service 接口 + * + * @author 芋道源码 + */ +public interface IotThinkModelFunctionService { + + /** + * 创建IoT 产品物模型 + * + * @param createReqVO 创建信息 + * @return 编号 + */ + Long createThinkModelFunction(@Valid IotThinkModelFunctionSaveReqVO createReqVO); + + /** + * 删除IoT 产品物模型 + * + * @param id 编号 + */ + void deleteThinkModelFunction(Long id); + + /** + * 获得IoT 产品物模型 + * + * @param productKey 产品Key + * @return IoT 产品物模型 + */ + IotThinkModelFunctionDO getThinkModelFunctionByProductKey(String productKey); + + /** + * 更新IoT 产品物模型 + * + * @param updateReqVO 更新信息 + */ + void updateThinkModelFunctionByProductKey(@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 new file mode 100644 index 000000000..1f1355dd3 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java @@ -0,0 +1,74 @@ +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.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionSaveReqVO; +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 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_NOT_EXISTS; + +/** + * IoT 产品物模型 Service 实现类 + * + * @author 芋道源码 + */ +@Service +@Validated +public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionService { + + @Resource + private IotThinkModelFunctionMapper thinkModelFunctionMapper; + + @Override + public Long createThinkModelFunction(IotThinkModelFunctionSaveReqVO createReqVO) { + // 插入 + IotThinkModelFunctionDO thinkModelFunction = BeanUtils.toBean(createReqVO, IotThinkModelFunctionDO.class); + // properties 字段,需要转换成 JSON + thinkModelFunction.setProperties(JSONUtil.toJsonStr(createReqVO.getProperties())); + thinkModelFunctionMapper.insert(thinkModelFunction); + // 返回 + return thinkModelFunction.getId(); + } + + @Override + public void deleteThinkModelFunction(Long id) { + // 校验存在 + validateThinkModelFunctionExists(id); + // 删除 + thinkModelFunctionMapper.deleteById(id); + } + + private void validateThinkModelFunctionExists(Long id) { + if (thinkModelFunctionMapper.selectById(id) == null) { + throw exception(THINK_MODEL_FUNCTION_NOT_EXISTS); + } + } + + 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); + } + +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/thinkmodelfunction/IotThinkModelFunctionMapper.xml b/yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/thinkmodelfunction/IotThinkModelFunctionMapper.xml new file mode 100644 index 000000000..525a32bd6 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/thinkmodelfunction/IotThinkModelFunctionMapper.xml @@ -0,0 +1,12 @@ + + + + + + + \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImplTest.java b/yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImplTest.java new file mode 100644 index 000000000..762f6021b --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImplTest.java @@ -0,0 +1,71 @@ +package cn.iocoder.yudao.module.iot.service.thinkmodelfunction; + +import org.junit.jupiter.api.Test; + +import jakarta.annotation.Resource; + +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; + +import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.*; +import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO; +import cn.iocoder.yudao.module.iot.dal.mysql.thinkmodelfunction.IotThinkModelFunctionMapper; + +import org.springframework.context.annotation.Import; + +import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*; +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*; +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; +import static org.junit.jupiter.api.Assertions.*; + +/** + * {@link IotThinkModelFunctionServiceImpl} 的单元测试类 + * + * @author 芋道源码 + */ +@Import(IotThinkModelFunctionServiceImpl.class) +public class IotThinkModelFunctionServiceImplTest extends BaseDbUnitTest { + + @Resource + private IotThinkModelFunctionServiceImpl thinkModelFunctionService; + + @Resource + private IotThinkModelFunctionMapper thinkModelFunctionMapper; + + @Test + public void testCreateThinkModelFunction_success() { + // 准备参数 + IotThinkModelFunctionSaveReqVO createReqVO = randomPojo(IotThinkModelFunctionSaveReqVO.class); + + // 调用 + Long thinkModelFunctionId = thinkModelFunctionService.createThinkModelFunction(createReqVO); + // 断言 + assertNotNull(thinkModelFunctionId); + // 校验记录的属性是否正确 + IotThinkModelFunctionDO thinkModelFunction = thinkModelFunctionMapper.selectById(thinkModelFunctionId); + assertPojoEquals(createReqVO, thinkModelFunction, "id"); + } + + @Test + public void testDeleteThinkModelFunction_success() { + // mock 数据 + IotThinkModelFunctionDO dbThinkModelFunction = randomPojo(IotThinkModelFunctionDO.class); + thinkModelFunctionMapper.insert(dbThinkModelFunction);// @Sql: 先插入出一条存在的数据 + // 准备参数 + Long id = dbThinkModelFunction.getId(); + + // 调用 + thinkModelFunctionService.deleteThinkModelFunction(id); + // 校验数据不存在了 + assertNull(thinkModelFunctionMapper.selectById(id)); + } + + @Test + public void testDeleteThinkModelFunction_notExists() { + // 准备参数 + Long id = randomLongId(); + + // 调用, 并断言异常 + assertServiceException(() -> thinkModelFunctionService.deleteThinkModelFunction(id), THINK_MODEL_FUNCTION_NOT_EXISTS); + } + +} \ No newline at end of file