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

This commit is contained in:
安浩浩 2024-09-13 22:30:19 +08:00
parent 18e789d4fb
commit 9e77692414
10 changed files with 159 additions and 56 deletions

View File

@ -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 对应的产品物模型已存在");
}

View File

@ -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}}

View File

@ -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<Boolean> 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<IotThinkModelFunctionRespVO> 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);
}
@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<IotThinkModelFunctionRespVO> getThinkModelFunctionByProductId(@RequestParam("productId") Long productId) {
IotThinkModelFunctionDO thinkModelFunction = thinkModelFunctionService.getThinkModelFunctionByProductId(productId);
IotThinkModelFunctionRespVO respVO = IotThinkModelFunctionConvert.INSTANCE.convert(thinkModelFunction);
return success(respVO);
}
}

View File

@ -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<IotThingModelProperty> properties;
@Schema(description = "服务列表")
@ExcelProperty("服务列表")

View File

@ -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;

View File

@ -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<IotThinkModelFunctionRespVO> convertList(List<IotThinkModelFunctionDO> list);
// 批量转换处理 properties 字段
@AfterMapping
default void convertPropertiesListToVO(List<IotThinkModelFunctionDO> sourceList, @MappingTarget List<IotThinkModelFunctionRespVO> targetList) {
for (int i = 0; i < sourceList.size(); i++) {
convertPropertiesToVO(sourceList.get(i), targetList.get(i));
}
}
}

View File

@ -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;
/**
* 事件列表
*/

View File

@ -17,9 +17,8 @@ public interface IotThinkModelFunctionMapper extends BaseMapperX<IotThinkModelFu
return selectOne(new LambdaQueryWrapperX<IotThinkModelFunctionDO>().eq(IotThinkModelFunctionDO::getProductKey, productKey));
}
default int updateByProductKey(IotThinkModelFunctionDO thinkModelFunction) {
return update(thinkModelFunction, new LambdaQueryWrapperX<IotThinkModelFunctionDO>()
.eq(IotThinkModelFunctionDO::getProductKey, thinkModelFunction.getProductKey())
);
default IotThinkModelFunctionDO selectByProductId(Long productId){
return selectOne(new LambdaQueryWrapperX<IotThinkModelFunctionDO>().eq(IotThinkModelFunctionDO::getProductId, productId));
}
}

View File

@ -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);
}

View File

@ -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);
}
}
}