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