Commit c0e2bdbdd48c4f8554724776dcb9149294c433a7
1 parent
2932314e
【代码评审】IOT:物模型的 review
Showing
4 changed files
with
46 additions
and
45 deletions
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/db/TenantDatabaseInterceptor.java
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/thinkmodelfunction/IotThinkModelFunctionMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.dal.mysql.thinkmodelfunction; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 4 | -import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | |
| 5 | 4 | import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO; |
| 6 | 5 | import org.apache.ibatis.annotations.Mapper; |
| 7 | 6 | |
| ... | ... | @@ -16,16 +15,17 @@ import java.util.List; |
| 16 | 15 | public interface IotThinkModelFunctionMapper extends BaseMapperX<IotThinkModelFunctionDO> { |
| 17 | 16 | |
| 18 | 17 | default IotThinkModelFunctionDO selectByProductIdAndIdentifier(Long productId, String identifier) { |
| 19 | - return selectOne(new LambdaQueryWrapperX<IotThinkModelFunctionDO>().eq(IotThinkModelFunctionDO::getProductId, productId) | |
| 20 | - .eq(IotThinkModelFunctionDO::getIdentifier, identifier)); | |
| 18 | + return selectOne(IotThinkModelFunctionDO::getProductId, productId, | |
| 19 | + IotThinkModelFunctionDO::getIdentifier, identifier); | |
| 21 | 20 | } |
| 22 | 21 | |
| 23 | 22 | default List<IotThinkModelFunctionDO> selectListByProductId(Long productId) { |
| 24 | - return selectList(new LambdaQueryWrapperX<IotThinkModelFunctionDO>().eq(IotThinkModelFunctionDO::getProductId, productId)); | |
| 23 | + return selectList(IotThinkModelFunctionDO::getProductId, productId); | |
| 25 | 24 | } |
| 26 | 25 | |
| 27 | 26 | default List<IotThinkModelFunctionDO> selectListByProductIdAndType(Long productId, Integer type) { |
| 28 | - return selectList(new LambdaQueryWrapperX<IotThinkModelFunctionDO>().eq(IotThinkModelFunctionDO::getProductId, productId) | |
| 29 | - .eq(IotThinkModelFunctionDO::getType, type)); | |
| 27 | + return selectList(IotThinkModelFunctionDO::getProductId, productId, | |
| 28 | + IotThinkModelFunctionDO::getType, type); | |
| 30 | 29 | } |
| 30 | + | |
| 31 | 31 | } |
| 32 | 32 | \ 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
| ... | ... | @@ -14,7 +14,7 @@ import java.util.List; |
| 14 | 14 | public interface IotThinkModelFunctionService { |
| 15 | 15 | |
| 16 | 16 | /** |
| 17 | - * 创建IoT 产品物模型 | |
| 17 | + * 创建产品物模型 | |
| 18 | 18 | * |
| 19 | 19 | * @param createReqVO 创建信息 |
| 20 | 20 | * @return 编号 |
| ... | ... | @@ -23,32 +23,32 @@ public interface IotThinkModelFunctionService { |
| 23 | 23 | |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | - * 更新IoT 产品物模型 | |
| 26 | + * 更新产品物模型 | |
| 27 | 27 | * |
| 28 | 28 | * @param updateReqVO 更新信息 |
| 29 | 29 | */ |
| 30 | 30 | void updateThinkModelFunction(@Valid IotThinkModelFunctionSaveReqVO updateReqVO); |
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | - * 删除IoT 产品物模型 | |
| 33 | + * 删除产品物模型 | |
| 34 | 34 | * |
| 35 | 35 | * @param id 编号 |
| 36 | 36 | */ |
| 37 | 37 | void deleteThinkModelFunction(Long id); |
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | - * 获得IoT 产品物模型 | |
| 40 | + * 获得产品物模型 | |
| 41 | 41 | * |
| 42 | 42 | * @param id 编号 |
| 43 | - * @return IoT 产品物模型 | |
| 43 | + * @return 产品物模型 | |
| 44 | 44 | */ |
| 45 | 45 | IotThinkModelFunctionDO getThinkModelFunction(Long id); |
| 46 | 46 | |
| 47 | 47 | /** |
| 48 | - * 获得IoT 产品物模型列表 | |
| 48 | + * 获得产品物模型列表 | |
| 49 | 49 | * |
| 50 | 50 | * @param productId 产品编号 |
| 51 | - * @return IoT 产品物模型列表 | |
| 51 | + * @return 产品物模型列表 | |
| 52 | 52 | */ |
| 53 | 53 | List<IotThinkModelFunctionDO> getThinkModelFunctionListByProductId(Long productId); |
| 54 | 54 | } |
| 55 | 55 | \ 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.iocoder.yudao.framework.common.validation.Telephone; | |
| 4 | 3 | import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelEvent; |
| 5 | 4 | import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelProperty; |
| 6 | 5 | import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelService; |
| ... | ... | @@ -16,7 +15,6 @@ import cn.iocoder.yudao.module.iot.enums.product.IotThingModelTypeEnum; |
| 16 | 15 | import jakarta.annotation.Resource; |
| 17 | 16 | import lombok.extern.slf4j.Slf4j; |
| 18 | 17 | import org.springframework.stereotype.Service; |
| 19 | -import org.springframework.transaction.annotation.Transactional; | |
| 20 | 18 | import org.springframework.validation.annotation.Validated; |
| 21 | 19 | |
| 22 | 20 | import java.util.ArrayList; |
| ... | ... | @@ -28,81 +26,83 @@ import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionU |
| 28 | 26 | import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER; |
| 29 | 27 | import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.THINK_MODEL_FUNCTION_NOT_EXISTS; |
| 30 | 28 | |
| 31 | -@Slf4j | |
| 29 | +/** | |
| 30 | + * IoT 产品物模型 Service 实现类 | |
| 31 | + * | |
| 32 | + * @author 芋道源码 | |
| 33 | + */ | |
| 32 | 34 | @Service |
| 33 | 35 | @Validated |
| 36 | +@Slf4j | |
| 34 | 37 | public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionService { |
| 35 | 38 | |
| 36 | 39 | @Resource |
| 37 | 40 | private IotThinkModelFunctionMapper thinkModelFunctionMapper; |
| 38 | 41 | |
| 39 | 42 | @Override |
| 43 | + // TODO @haohao:事务 | |
| 40 | 44 | public Long createThinkModelFunction(IotThinkModelFunctionSaveReqVO createReqVO) { |
| 41 | - // 校验功能标识符在同一产品下是否唯一 | |
| 45 | + // 1. 校验功能标识符在同一产品下是否唯一 | |
| 42 | 46 | validateIdentifierUnique(createReqVO.getProductId(), createReqVO.getIdentifier()); |
| 43 | 47 | |
| 44 | - // 转换请求对象为数据对象 | |
| 45 | - IotThinkModelFunctionDO thinkModelFunction = IotThinkModelFunctionConvert.INSTANCE.convert(createReqVO); | |
| 46 | - | |
| 47 | - // 插入数据库 | |
| 48 | - thinkModelFunctionMapper.insert(thinkModelFunction); | |
| 48 | + // 2. 插入数据库 | |
| 49 | + IotThinkModelFunctionDO function = IotThinkModelFunctionConvert.INSTANCE.convert(createReqVO); | |
| 50 | + thinkModelFunctionMapper.insert(function); | |
| 49 | 51 | |
| 50 | - // 如果创建的是属性,需要更新默认的事件和服务 | |
| 52 | + // 3. 如果创建的是属性,需要更新默认的事件和服务 | |
| 51 | 53 | if (Objects.equals(createReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { |
| 54 | + // TODO @haohao:最好使用 createDefaultEventsAndServices。原因是:generate 更多在目前项目里,是创建对象,不涉及到 insert db。 | |
| 52 | 55 | generateDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey()); |
| 53 | 56 | } |
| 54 | - | |
| 55 | - // 返回生成的 ID | |
| 56 | - return thinkModelFunction.getId(); | |
| 57 | + return function.getId(); | |
| 57 | 58 | } |
| 58 | 59 | |
| 59 | 60 | private void validateIdentifierUnique(Long productId, String identifier) { |
| 60 | - IotThinkModelFunctionDO existingFunction = thinkModelFunctionMapper.selectByProductIdAndIdentifier(productId, identifier); | |
| 61 | - if (existingFunction != null) { | |
| 61 | + IotThinkModelFunctionDO function = thinkModelFunctionMapper.selectByProductIdAndIdentifier(productId, identifier); | |
| 62 | + if (function != null) { | |
| 62 | 63 | throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER); |
| 63 | 64 | } |
| 64 | 65 | } |
| 65 | 66 | |
| 66 | 67 | @Override |
| 68 | + // TODO @haohao:事务 | |
| 67 | 69 | public void updateThinkModelFunction(IotThinkModelFunctionSaveReqVO updateReqVO) { |
| 68 | - // 校验功能是否存在 | |
| 70 | + // 1.1 校验功能是否存在 | |
| 69 | 71 | validateThinkModelFunctionExists(updateReqVO.getId()); |
| 70 | - | |
| 71 | - // 校验功能标识符是否唯一 | |
| 72 | + // 1.2 校验功能标识符是否唯一 | |
| 72 | 73 | validateIdentifierUniqueForUpdate(updateReqVO.getId(), updateReqVO.getProductId(), updateReqVO.getIdentifier()); |
| 73 | 74 | |
| 74 | - // 转换请求对象为数据对象 | |
| 75 | + // 2. 更新数据库 | |
| 75 | 76 | IotThinkModelFunctionDO thinkModelFunction = IotThinkModelFunctionConvert.INSTANCE.convert(updateReqVO); |
| 76 | - | |
| 77 | - // 更新数据库 | |
| 78 | 77 | thinkModelFunctionMapper.updateById(thinkModelFunction); |
| 79 | 78 | |
| 80 | - // 如果更新的是属性,需要更新默认的事件和服务 | |
| 79 | + // 3. 如果更新的是属性,需要更新默认的事件和服务 | |
| 81 | 80 | if (Objects.equals(updateReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { |
| 82 | 81 | generateDefaultEventsAndServices(updateReqVO.getProductId(), updateReqVO.getProductKey()); |
| 83 | 82 | } |
| 84 | 83 | } |
| 85 | 84 | |
| 86 | 85 | private void validateIdentifierUniqueForUpdate(Long id, Long productId, String identifier) { |
| 87 | - IotThinkModelFunctionDO existingFunction = thinkModelFunctionMapper.selectByProductIdAndIdentifier(productId, identifier); | |
| 88 | - if (existingFunction != null && !existingFunction.getId().equals(id)) { | |
| 86 | + IotThinkModelFunctionDO function = thinkModelFunctionMapper.selectByProductIdAndIdentifier(productId, identifier); | |
| 87 | + // TODO !function.getId().equals(id) 使用 ObjectUtil.notEquals 。逻辑里,尽量避免 ! 取反。用不等于会比 ! 更容易理解 | |
| 88 | + if (function != null && !function.getId().equals(id)) { | |
| 89 | 89 | throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER); |
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - | |
| 94 | 93 | @Override |
| 94 | + // TODO @haohao:事务 | |
| 95 | 95 | public void deleteThinkModelFunction(Long id) { |
| 96 | - // 校验功能是否存在 | |
| 96 | + // 1. 校验功能是否存在 | |
| 97 | 97 | IotThinkModelFunctionDO functionDO = thinkModelFunctionMapper.selectById(id); |
| 98 | 98 | if (functionDO == null) { |
| 99 | 99 | throw exception(THINK_MODEL_FUNCTION_NOT_EXISTS); |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - // 删除功能 | |
| 102 | + // 2. 删除功能 | |
| 103 | 103 | thinkModelFunctionMapper.deleteById(id); |
| 104 | 104 | |
| 105 | - // 如果删除的是属性,需要更新默认的事件和服务 | |
| 105 | + // 3. 如果删除的是属性,需要更新默认的事件和服务 | |
| 106 | 106 | if (Objects.equals(functionDO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { |
| 107 | 107 | generateDefaultEventsAndServices(functionDO.getProductId(), functionDO.getProductKey()); |
| 108 | 108 | } |
| ... | ... | @@ -159,10 +159,12 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe |
| 159 | 159 | * 生成属性上报事件 |
| 160 | 160 | */ |
| 161 | 161 | private ThingModelEvent generatePropertyPostEvent(List<IotThinkModelFunctionDO> propertyList) { |
| 162 | + // TODO @haohao:用 CollUtil.isNotEmpty 会更容易哈 | |
| 162 | 163 | if (propertyList == null || propertyList.isEmpty()) { |
| 163 | 164 | return null; |
| 164 | 165 | } |
| 165 | 166 | |
| 167 | + // TODO @haohao:可以考虑链式调用,简化整个方法的长度;然后,把相同类型的户型,尽量再放同一行,看起来轻松点;其它类似的,也可以试试看哈 | |
| 166 | 168 | ThingModelEvent event = new ThingModelEvent(); |
| 167 | 169 | event.setIdentifier("post"); |
| 168 | 170 | event.setName("属性上报"); |
| ... | ... | @@ -183,7 +185,6 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe |
| 183 | 185 | outputData.add(arg); |
| 184 | 186 | } |
| 185 | 187 | event.setOutputData(outputData); |
| 186 | - | |
| 187 | 188 | return event; |
| 188 | 189 | } |
| 189 | 190 | |
| ... | ... | @@ -222,7 +223,6 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe |
| 222 | 223 | service.setInputData(inputData); |
| 223 | 224 | // 属性设置服务一般不需要输出参数 |
| 224 | 225 | service.setOutputData(new ArrayList<>()); |
| 225 | - | |
| 226 | 226 | return service; |
| 227 | 227 | } |
| 228 | 228 | |
| ... | ... | @@ -237,6 +237,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe |
| 237 | 237 | List<ThingModelArgument> outputData = new ArrayList<>(); |
| 238 | 238 | for (IotThinkModelFunctionDO functionDO : propertyList) { |
| 239 | 239 | ThingModelProperty property = functionDO.getProperty(); |
| 240 | + // TODO @haohao:r、rw 是不是枚举起来 | |
| 240 | 241 | if ("r".equals(property.getAccessMode()) || "rw".equals(property.getAccessMode())) { |
| 241 | 242 | ThingModelArgument arg = new ThingModelArgument(); |
| 242 | 243 | arg.setIdentifier(property.getIdentifier()); |
| ... | ... | @@ -279,7 +280,6 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe |
| 279 | 280 | |
| 280 | 281 | service.setInputData(Collections.singletonList(inputArg)); |
| 281 | 282 | service.setOutputData(outputData); |
| 282 | - | |
| 283 | 283 | return service; |
| 284 | 284 | } |
| 285 | 285 | |
| ... | ... | @@ -298,6 +298,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe |
| 298 | 298 | functionDO.setType(IotThingModelTypeEnum.EVENT.getType()); |
| 299 | 299 | functionDO.setEvent(event); |
| 300 | 300 | |
| 301 | + // TODO @haohao:会不会存在删除的情况哈?另外,项目里有 diffList 方法,看看是不是可以方便的,适合这个场景。具体怎么用,可以全局搜 | |
| 301 | 302 | if (existingEvent != null) { |
| 302 | 303 | // 更新事件 |
| 303 | 304 | functionDO.setId(existingEvent.getId()); | ... | ... |