Commit d23be8616483862d0cfe034fa532c9312ff2e3fb
1 parent
d24e3ad7
【代码评审】IoT:物模型的实现
Showing
4 changed files
with
22 additions
and
36 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thingmodel/IotThingModelController.java
| ... | ... | @@ -7,7 +7,6 @@ import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelL |
| 7 | 7 | import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelPageReqVO; |
| 8 | 8 | import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelRespVO; |
| 9 | 9 | import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelSaveReqVO; |
| 10 | -import cn.iocoder.yudao.module.iot.convert.thingmodel.IotThingModelConvert; | |
| 11 | 10 | import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO; |
| 12 | 11 | import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService; |
| 13 | 12 | import io.swagger.v3.oas.annotations.Operation; |
| ... | ... | @@ -62,7 +61,7 @@ public class IotThingModelController { |
| 62 | 61 | @PreAuthorize("@ss.hasPermission('iot:thing-model:query')") |
| 63 | 62 | public CommonResult<IotThingModelRespVO> getThingModel(@RequestParam("id") Long id) { |
| 64 | 63 | IotThingModelDO thingModel = thingModelService.getThingModel(id); |
| 65 | - return success(IotThingModelConvert.INSTANCE.convert(thingModel)); | |
| 64 | + return success(BeanUtils.toBean(thingModel, IotThingModelRespVO.class)); | |
| 66 | 65 | } |
| 67 | 66 | |
| 68 | 67 | @GetMapping("/list-by-product-id") |
| ... | ... | @@ -71,7 +70,7 @@ public class IotThingModelController { |
| 71 | 70 | @PreAuthorize("@ss.hasPermission('iot:thing-model:query')") |
| 72 | 71 | public CommonResult<List<IotThingModelRespVO>> getThingModelListByProductId(@RequestParam("productId") Long productId) { |
| 73 | 72 | List<IotThingModelDO> list = thingModelService.getThingModelListByProductId(productId); |
| 74 | - return success(IotThingModelConvert.INSTANCE.convertList(list)); | |
| 73 | + return success(BeanUtils.toBean(list, IotThingModelRespVO.class)); | |
| 75 | 74 | } |
| 76 | 75 | |
| 77 | 76 | // TODO @puhui @super:getThingModelListByProductId 和 getThingModelListByProductId 可以融合么? |
| ... | ... | @@ -80,7 +79,7 @@ public class IotThingModelController { |
| 80 | 79 | @PreAuthorize("@ss.hasPermission('iot:thing-model:query')") |
| 81 | 80 | public CommonResult<List<IotThingModelRespVO>> getThingModelListByProductId(@Valid IotThingModelListReqVO reqVO) { |
| 82 | 81 | List<IotThingModelDO> list = thingModelService.getThingModelList(reqVO); |
| 83 | - return success(IotThingModelConvert.INSTANCE.convertList(list)); | |
| 82 | + return success(BeanUtils.toBean(list, IotThingModelRespVO.class)); | |
| 84 | 83 | } |
| 85 | 84 | |
| 86 | 85 | @GetMapping("/page") | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/convert/package-info.java
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/convert/thingmodel/IotThingModelConvert.java
| ... | ... | @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.iot.convert.thingmodel; |
| 3 | 3 | import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelEvent; |
| 4 | 4 | import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelProperty; |
| 5 | 5 | import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelService; |
| 6 | -import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelRespVO; | |
| 7 | 6 | import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelSaveReqVO; |
| 8 | 7 | import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO; |
| 9 | 8 | import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum; |
| ... | ... | @@ -12,7 +11,6 @@ import org.mapstruct.Mapping; |
| 12 | 11 | import org.mapstruct.Named; |
| 13 | 12 | import org.mapstruct.factory.Mappers; |
| 14 | 13 | |
| 15 | -import java.util.List; | |
| 16 | 14 | import java.util.Objects; |
| 17 | 15 | |
| 18 | 16 | @Mapper |
| ... | ... | @@ -20,21 +18,11 @@ public interface IotThingModelConvert { |
| 20 | 18 | |
| 21 | 19 | IotThingModelConvert INSTANCE = Mappers.getMapper(IotThingModelConvert.class); |
| 22 | 20 | |
| 23 | - // 将 SaveReqVO 转换为 DO | |
| 24 | 21 | @Mapping(target = "property", expression = "java(convertToProperty(bean))") |
| 25 | 22 | @Mapping(target = "event", expression = "java(convertToEvent(bean))") |
| 26 | 23 | @Mapping(target = "service", expression = "java(convertToService(bean))") |
| 27 | 24 | IotThingModelDO convert(IotThingModelSaveReqVO bean); |
| 28 | 25 | |
| 29 | - // 将 DO 转换为 RespVO | |
| 30 | - @Mapping(target = "property", source = "property") | |
| 31 | - @Mapping(target = "event", source = "event") | |
| 32 | - @Mapping(target = "service", source = "service") | |
| 33 | - IotThingModelRespVO convert(IotThingModelDO bean); | |
| 34 | - | |
| 35 | - // 批量转换 | |
| 36 | - List<IotThingModelRespVO> convertList(List<IotThingModelDO> list); | |
| 37 | - | |
| 38 | 26 | @Named("convertToProperty") |
| 39 | 27 | default ThingModelProperty convertToProperty(IotThingModelSaveReqVO bean) { |
| 40 | 28 | if (Objects.equals(bean.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thingmodel/IotThingModelServiceImpl.java
| ... | ... | @@ -58,9 +58,7 @@ public class IotThingModelServiceImpl implements IotThingModelService { |
| 58 | 58 | validateIdentifierUnique(createReqVO.getProductId(), createReqVO.getIdentifier()); |
| 59 | 59 | // 1.2 功能名称在同一产品下是否唯一 |
| 60 | 60 | validateNameUnique(createReqVO.getProductId(), createReqVO.getName()); |
| 61 | - // 1.3 系统保留字段,不能用于标识符定义 | |
| 62 | - validateNotDefaultEventAndService(createReqVO.getIdentifier()); | |
| 63 | - // 1.4 校验产品状态,发布状态下,不允许新增功能 | |
| 61 | + // 1.3 校验产品状态,发布状态下,不允许新增功能 | |
| 64 | 62 | validateProductStatus(createReqVO.getProductId()); |
| 65 | 63 | |
| 66 | 64 | // 2. 插入数据库 |
| ... | ... | @@ -71,7 +69,6 @@ public class IotThingModelServiceImpl implements IotThingModelService { |
| 71 | 69 | if (Objects.equals(createReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { |
| 72 | 70 | createDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey()); |
| 73 | 71 | } |
| 74 | - // TODO @puhui999: 服务和事件的情况 method 怎么设置?在前端设置还是后端设置? | |
| 75 | 72 | |
| 76 | 73 | // 4. 删除缓存 |
| 77 | 74 | deleteThingModelListCache(createReqVO.getProductKey()); |
| ... | ... | @@ -177,14 +174,6 @@ public class IotThingModelServiceImpl implements IotThingModelService { |
| 177 | 174 | } |
| 178 | 175 | } |
| 179 | 176 | |
| 180 | - // TODO @芋艿:在 review 下 | |
| 181 | - private void validateNotDefaultEventAndService(String identifier) { | |
| 182 | - // 系统保留字段,不能用于标识符定义 | |
| 183 | - if (StrUtil.equalsAny(identifier, "set", "get", "post", "property", "event", "time", "value")) { | |
| 184 | - throw exception(THING_MODEL_IDENTIFIER_INVALID); | |
| 185 | - } | |
| 186 | - } | |
| 187 | - | |
| 188 | 177 | private void validateNameUnique(Long productId, String name) { |
| 189 | 178 | IotThingModelDO thingModel = thingModelMapper.selectByProductIdAndName(productId, name); |
| 190 | 179 | if (thingModel != null) { |
| ... | ... | @@ -193,6 +182,12 @@ public class IotThingModelServiceImpl implements IotThingModelService { |
| 193 | 182 | } |
| 194 | 183 | |
| 195 | 184 | private void validateIdentifierUnique(Long productId, String identifier) { |
| 185 | + // 系统保留字段,不能用于标识符定义 | |
| 186 | + if (StrUtil.equalsAny(identifier, "set", "get", "post", "property", "event", "time", "value")) { | |
| 187 | + throw exception(THING_MODEL_IDENTIFIER_INVALID); | |
| 188 | + } | |
| 189 | + | |
| 190 | + // 校验唯一 | |
| 196 | 191 | IotThingModelDO thingModel = thingModelMapper.selectByProductIdAndIdentifier(productId, identifier); |
| 197 | 192 | if (thingModel != null) { |
| 198 | 193 | throw exception(THING_MODEL_IDENTIFIER_EXISTS); |
| ... | ... | @@ -215,17 +210,17 @@ public class IotThingModelServiceImpl implements IotThingModelService { |
| 215 | 210 | // 2.1 生成属性上报事件 |
| 216 | 211 | ThingModelEvent propertyPostEvent = generatePropertyPostEvent(properties); |
| 217 | 212 | if (propertyPostEvent != null) { |
| 218 | - newThingModels.add(buildEventThingModelDO(productId, productKey, propertyPostEvent, "属性上报事件")); | |
| 213 | + newThingModels.add(buildEventThingModel(productId, productKey, propertyPostEvent, "属性上报事件")); | |
| 219 | 214 | } |
| 220 | 215 | // 2.2 生成属性设置服务 |
| 221 | 216 | ThingModelService propertySetService = generatePropertySetService(properties); |
| 222 | 217 | if (propertySetService != null) { |
| 223 | - newThingModels.add(buildServiceThingModelDO(productId, productKey, propertySetService, "属性设置服务")); | |
| 218 | + newThingModels.add(buildServiceThingModel(productId, productKey, propertySetService, "属性设置服务")); | |
| 224 | 219 | } |
| 225 | 220 | // 2.3 生成属性获取服务 |
| 226 | 221 | ThingModelService propertyGetService = generatePropertyGetService(properties); |
| 227 | 222 | if (propertyGetService != null) { |
| 228 | - newThingModels.add(buildServiceThingModelDO(productId, productKey, propertyGetService, "属性获取服务")); | |
| 223 | + newThingModels.add(buildServiceThingModel(productId, productKey, propertyGetService, "属性获取服务")); | |
| 229 | 224 | } |
| 230 | 225 | |
| 231 | 226 | // 3.1 获取数据库中的默认的旧事件和服务列表 |
| ... | ... | @@ -269,8 +264,8 @@ public class IotThingModelServiceImpl implements IotThingModelService { |
| 269 | 264 | /** |
| 270 | 265 | * 构建事件功能对象 |
| 271 | 266 | */ |
| 272 | - private IotThingModelDO buildEventThingModelDO(Long productId, String productKey, | |
| 273 | - ThingModelEvent event, String description) { | |
| 267 | + private IotThingModelDO buildEventThingModel(Long productId, String productKey, | |
| 268 | + ThingModelEvent event, String description) { | |
| 274 | 269 | return new IotThingModelDO().setProductId(productId).setProductKey(productKey) |
| 275 | 270 | .setIdentifier(event.getIdentifier()).setName(event.getName()).setDescription(description) |
| 276 | 271 | .setType(IotThingModelTypeEnum.EVENT.getType()).setEvent(event); |
| ... | ... | @@ -279,13 +274,14 @@ public class IotThingModelServiceImpl implements IotThingModelService { |
| 279 | 274 | /** |
| 280 | 275 | * 构建服务功能对象 |
| 281 | 276 | */ |
| 282 | - private IotThingModelDO buildServiceThingModelDO(Long productId, String productKey, | |
| 283 | - ThingModelService service, String description) { | |
| 277 | + private IotThingModelDO buildServiceThingModel(Long productId, String productKey, | |
| 278 | + ThingModelService service, String description) { | |
| 284 | 279 | return new IotThingModelDO().setProductId(productId).setProductKey(productKey) |
| 285 | 280 | .setIdentifier(service.getIdentifier()).setName(service.getName()).setDescription(description) |
| 286 | 281 | .setType(IotThingModelTypeEnum.SERVICE.getType()).setService(service); |
| 287 | 282 | } |
| 288 | 283 | |
| 284 | + // TODO @haohao:是不是不用生成这个?目前属性上报,是个批量接口 | |
| 289 | 285 | /** |
| 290 | 286 | * 生成属性上报事件 |
| 291 | 287 | */ |
| ... | ... | @@ -301,6 +297,7 @@ public class IotThingModelServiceImpl implements IotThingModelService { |
| 301 | 297 | .setOutputParams(buildInputOutputParam(thingModels, IotThingModelParamDirectionEnum.OUTPUT)); |
| 302 | 298 | } |
| 303 | 299 | |
| 300 | + // TODO @haohao:是不是不用生成这个?目前属性上报,是个批量接口 | |
| 304 | 301 | /** |
| 305 | 302 | * 生成属性设置服务 |
| 306 | 303 | */ | ... | ... |