Commit a83ce3b667be12ea9662f62bf0b8c2c455e01ece

Authored by 安浩浩
1 parent 1e69face

修改:增加 IOT 前缀

Showing 9 changed files with 110 additions and 160 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/ProductController.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/IotProductController.java
... ... @@ -6,11 +6,11 @@ import cn.iocoder.yudao.framework.common.pojo.PageParam;
6 6 import cn.iocoder.yudao.framework.common.pojo.PageResult;
7 7 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
8 8 import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
9   -import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductPageReqVO;
10   -import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductRespVO;
11   -import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductSaveReqVO;
12   -import cn.iocoder.yudao.module.iot.dal.dataobject.product.ProductDO;
13   -import cn.iocoder.yudao.module.iot.service.product.ProductService;
  9 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductPageReqVO;
  10 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductRespVO;
  11 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductSaveReqVO;
  12 +import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
  13 +import cn.iocoder.yudao.module.iot.service.product.IotProductService;
14 14 import io.swagger.v3.oas.annotations.Operation;
15 15 import io.swagger.v3.oas.annotations.Parameter;
16 16 import io.swagger.v3.oas.annotations.tags.Tag;
... ... @@ -27,27 +27,26 @@ import java.util.List;
27 27 import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
28 28 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
29 29  
30   -// TODO @haohao:Iot 前缀要加,不然很容易重复哈
31 30 @Tag(name = "管理后台 - IOT 产品")
32 31 @RestController
33 32 @RequestMapping("/iot/product")
34 33 @Validated
35   -public class ProductController {
  34 +public class IotProductController {
36 35  
37 36 @Resource
38   - private ProductService productService;
  37 + private IotProductService productService;
39 38  
40 39 @PostMapping("/create")
41 40 @Operation(summary = "创建产品")
42 41 @PreAuthorize("@ss.hasPermission('iot:product:create')")
43   - public CommonResult<Long> createProduct(@Valid @RequestBody ProductSaveReqVO createReqVO) {
  42 + public CommonResult<Long> createProduct(@Valid @RequestBody IotProductSaveReqVO createReqVO) {
44 43 return success(productService.createProduct(createReqVO));
45 44 }
46 45  
47 46 @PutMapping("/update")
48 47 @Operation(summary = "更新产品")
49 48 @PreAuthorize("@ss.hasPermission('iot:product:update')")
50   - public CommonResult<Boolean> updateProduct(@Valid @RequestBody ProductSaveReqVO updateReqVO) {
  49 + public CommonResult<Boolean> updateProduct(@Valid @RequestBody IotProductSaveReqVO updateReqVO) {
51 50 productService.updateProduct(updateReqVO);
52 51 return success(true);
53 52 }
... ... @@ -76,30 +75,17 @@ public class ProductController {
76 75 @Operation(summary = "获得产品")
77 76 @Parameter(name = "id", description = "编号", required = true, example = "1024")
78 77 @PreAuthorize("@ss.hasPermission('iot:product:query')")
79   - public CommonResult<ProductRespVO> getProduct(@RequestParam("id") Long id) {
80   - ProductDO product = productService.getProduct(id);
81   - return success(BeanUtils.toBean(product, ProductRespVO.class));
  78 + public CommonResult<IotProductRespVO> getProduct(@RequestParam("id") Long id) {
  79 + IotProductDO product = productService.getProduct(id);
  80 + return success(BeanUtils.toBean(product, IotProductRespVO.class));
82 81 }
83 82  
84 83 @GetMapping("/page")
85 84 @Operation(summary = "获得产品分页")
86 85 @PreAuthorize("@ss.hasPermission('iot:product:query')")
87   - public CommonResult<PageResult<ProductRespVO>> getProductPage(@Valid ProductPageReqVO pageReqVO) {
88   - PageResult<ProductDO> pageResult = productService.getProductPage(pageReqVO);
89   - return success(BeanUtils.toBean(pageResult, ProductRespVO.class));
90   - }
91   -
92   - @GetMapping("/export-excel")
93   - @Operation(summary = "导出产品 Excel")
94   - @PreAuthorize("@ss.hasPermission('iot:product:export')")
95   - @ApiAccessLog(operateType = EXPORT)
96   - public void exportProductExcel(@Valid ProductPageReqVO pageReqVO,
97   - HttpServletResponse response) throws IOException {
98   - pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
99   - List<ProductDO> list = productService.getProductPage(pageReqVO).getList();
100   - // 导出 Excel
101   - ExcelUtils.write(response, "产品.xls", "数据", ProductRespVO.class,
102   - BeanUtils.toBean(list, ProductRespVO.class));
  86 + public CommonResult<PageResult<IotProductRespVO>> getProductPage(@Valid IotProductPageReqVO pageReqVO) {
  87 + PageResult<IotProductDO> pageResult = productService.getProductPage(pageReqVO);
  88 + return success(BeanUtils.toBean(pageResult, IotProductRespVO.class));
103 89 }
104 90  
105 91 }
106 92 \ No newline at end of file
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/ProductPageReqVO.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/IotProductPageReqVO.java
... ... @@ -5,55 +5,18 @@ import io.swagger.v3.oas.annotations.media.Schema;
5 5 import lombok.Data;
6 6 import lombok.EqualsAndHashCode;
7 7 import lombok.ToString;
8   -import org.springframework.format.annotation.DateTimeFormat;
9   -
10   -import java.time.LocalDateTime;
11   -
12   -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
13 8  
14 9 // TODO @haohao:涉及到 iot 的拼写,要不都用 IoT,貌似更规范
15   -// TODO 芋艿:需要清理掉一些无用字段
16 10 @Schema(description = "管理后台 - iot 产品分页 Request VO")
17 11 @Data
18 12 @EqualsAndHashCode(callSuper = true)
19 13 @ToString(callSuper = true)
20   -public class ProductPageReqVO extends PageParam {
  14 +public class IotProductPageReqVO extends PageParam {
21 15  
22 16 @Schema(description = "产品名称", example = "李四")
23 17 private String name;
24 18  
25   - @Schema(description = "创建时间")
26   - @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
27   - private LocalDateTime[] createTime;
28   -
29 19 @Schema(description = "产品标识")
30 20 private String productKey;
31 21  
32   - @Schema(description = "接入网关协议", example = "2")
33   - private Integer protocolType;
34   -
35   - @Schema(description = "协议编号(脚本解析 id)", example = "13177")
36   - private Long protocolId;
37   -
38   - @Schema(description = "产品所属品类标识符", example = "14237")
39   - private Long categoryId;
40   -
41   - @Schema(description = "产品描述", example = "你猜")
42   - private String description;
43   -
44   - @Schema(description = "数据校验级别", example = "1")
45   - private Integer validateType;
46   -
47   - @Schema(description = "产品状态", example = "1")
48   - private Integer status;
49   -
50   - @Schema(description = "设备类型", example = "2")
51   - private Integer deviceType;
52   -
53   - @Schema(description = "联网方式", example = "2")
54   - private Integer netType;
55   -
56   - @Schema(description = "数据格式", example = "0")
57   - private Integer dataFormat;
58   -
59 22 }
60 23 \ No newline at end of file
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/ProductRespVO.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/IotProductRespVO.java
... ... @@ -10,7 +10,7 @@ import java.time.LocalDateTime;
10 10 @Schema(description = "管理后台 - iot 产品 Response VO")
11 11 @Data
12 12 @ExcelIgnoreUnannotated
13   -public class ProductRespVO {
  13 +public class IotProductRespVO {
14 14  
15 15 @Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "26087")
16 16 @ExcelProperty("产品ID")
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/ProductSaveReqVO.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/IotProductSaveReqVO.java
... ... @@ -3,13 +3,13 @@ package cn.iocoder.yudao.module.iot.controller.admin.product.vo;
3 3 import cn.iocoder.yudao.framework.common.validation.InEnum;
4 4 import cn.iocoder.yudao.module.iot.enums.product.*;
5 5 import io.swagger.v3.oas.annotations.media.Schema;
6   -import lombok.*;
7   -import java.util.*;
8   -import jakarta.validation.constraints.*;
  6 +import jakarta.validation.constraints.NotEmpty;
  7 +import jakarta.validation.constraints.NotNull;
  8 +import lombok.Data;
9 9  
10 10 @Schema(description = "管理后台 - iot 产品新增/修改 Request VO")
11 11 @Data
12   -public class ProductSaveReqVO {
  12 +public class IotProductSaveReqVO {
13 13  
14 14 @Schema(description = "产品编号", requiredMode = Schema.RequiredMode.AUTO, example = "1")
15 15 private Long id;
... ... @@ -26,15 +26,15 @@ public class ProductSaveReqVO {
26 26 @NotNull(message = "设备类型不能为空")
27 27 private Integer deviceType;
28 28  
29   - @Schema(description = "联网方式", requiredMode = Schema.RequiredMode.REQUIRED,example = "0")
  29 + @Schema(description = "联网方式", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
30 30 @InEnum(value = IotNetTypeEnum.class, message = "联网方式必须是 {value}")
31 31 private Integer netType;
32 32  
33   - @Schema(description = "接入网关协议", requiredMode = Schema.RequiredMode.REQUIRED,example = "0")
  33 + @Schema(description = "接入网关协议", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
34 34 @InEnum(value = IotProtocolTypeEnum.class, message = "接入网关协议必须是 {value}")
35 35 private Integer protocolType;
36 36  
37   - @Schema(description = "数据格式",requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
  37 + @Schema(description = "数据格式", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
38 38 @InEnum(value = IotDataFormatEnum.class, message = "数据格式必须是 {value}")
39 39 @NotNull(message = "数据格式不能为空")
40 40 private Integer dataFormat;
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/product/ProductDO.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/product/IotProductDO.java
... ... @@ -19,7 +19,7 @@ import lombok.*;
19 19 @Builder
20 20 @NoArgsConstructor
21 21 @AllArgsConstructor
22   -public class ProductDO extends BaseDO {
  22 +public class IotProductDO extends BaseDO {
23 23  
24 24 /**
25 25 * 产品ID
... ... @@ -37,7 +37,7 @@ public class ProductDO extends BaseDO {
37 37 private String productKey;
38 38 /**
39 39 * 产品所属品类编号
40   - *
  40 + * <p>
41 41 * TODO 外键:后续加
42 42 */
43 43 private Long categoryId;
... ... @@ -48,44 +48,44 @@ public class ProductDO extends BaseDO {
48 48  
49 49 /**
50 50 * 产品状态
51   - *
  51 + * <p>
52 52 * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum}
53 53 */
54 54 private Integer status;
55 55 /**
56 56 * 设备类型
57   - *
  57 + * <p>
58 58 * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotProductDeviceTypeEnum}
59 59 */
60 60 private Integer deviceType;
61 61 /**
62 62 * 联网方式
63   - *
  63 + * <p>
64 64 * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotNetTypeEnum}
65 65 */
66 66 private Integer netType;
67 67  
68 68 /**
69 69 * 接入网关协议
70   - *
  70 + * <p>
71 71 * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotProtocolTypeEnum}
72 72 */
73 73 private Integer protocolType;
74 74 /**
75 75 * 协议编号
76   - *
  76 + * <p>
77 77 * TODO 外键:后续加
78 78 */
79 79 private Long protocolId;
80 80 /**
81 81 * 数据格式
82   - *
  82 + * <p>
83 83 * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotDataFormatEnum}
84 84 */
85 85 private Integer dataFormat;
86 86 /**
87 87 * 数据校验级别
88   - *
  88 + * <p>
89 89 * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotValidateTypeEnum}
90 90 */
91 91 private Integer validateType;
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/product/IotProductMapper.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.dal.mysql.product;
  2 +
  3 +import cn.iocoder.yudao.framework.common.pojo.PageResult;
  4 +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
  5 +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
  6 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductPageReqVO;
  7 +import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
  8 +import org.apache.ibatis.annotations.Mapper;
  9 +
  10 +/**
  11 + * iot 产品 Mapper
  12 + *
  13 + * @author ahh
  14 + */
  15 +@Mapper
  16 +public interface IotProductMapper extends BaseMapperX<IotProductDO> {
  17 +
  18 + default PageResult<IotProductDO> selectPage(IotProductPageReqVO reqVO) {
  19 + return selectPage(reqVO, new LambdaQueryWrapperX<IotProductDO>()
  20 + .likeIfPresent(IotProductDO::getName, reqVO.getName())
  21 + .likeIfPresent(IotProductDO::getProductKey, reqVO.getProductKey())
  22 + .orderByDesc(IotProductDO::getId));
  23 + }
  24 +
  25 + default IotProductDO selectByProductKey(String productKey) {
  26 + return selectOne(new LambdaQueryWrapperX<IotProductDO>().eq(IotProductDO::getProductKey, productKey));
  27 + }
  28 +}
0 29 \ No newline at end of file
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/product/ProductMapper.java deleted
1   -package cn.iocoder.yudao.module.iot.dal.mysql.product;
2   -
3   -import cn.iocoder.yudao.framework.common.pojo.PageResult;
4   -import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
5   -import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
6   -import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductPageReqVO;
7   -import cn.iocoder.yudao.module.iot.dal.dataobject.product.ProductDO;
8   -import org.apache.ibatis.annotations.Mapper;
9   -
10   -/**
11   - * iot 产品 Mapper
12   - *
13   - * @author ahh
14   - */
15   -@Mapper
16   -public interface ProductMapper extends BaseMapperX<ProductDO> {
17   -
18   - default PageResult<ProductDO> selectPage(ProductPageReqVO reqVO) {
19   - return selectPage(reqVO, new LambdaQueryWrapperX<ProductDO>()
20   - .likeIfPresent(ProductDO::getName, reqVO.getName())
21   - .betweenIfPresent(ProductDO::getCreateTime, reqVO.getCreateTime())
22   - .likeIfPresent(ProductDO::getProductKey, reqVO.getProductKey())
23   - .eqIfPresent(ProductDO::getProtocolId, reqVO.getProtocolId())
24   - .eqIfPresent(ProductDO::getCategoryId, reqVO.getCategoryId())
25   - .eqIfPresent(ProductDO::getDescription, reqVO.getDescription())
26   - .eqIfPresent(ProductDO::getValidateType, reqVO.getValidateType())
27   - .eqIfPresent(ProductDO::getStatus, reqVO.getStatus())
28   - .eqIfPresent(ProductDO::getDeviceType, reqVO.getDeviceType())
29   - .eqIfPresent(ProductDO::getNetType, reqVO.getNetType())
30   - .eqIfPresent(ProductDO::getProtocolType, reqVO.getProtocolType())
31   - .eqIfPresent(ProductDO::getDataFormat, reqVO.getDataFormat())
32   - .orderByDesc(ProductDO::getId));
33   - }
34   -
35   -}
36 0 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/ProductService.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductService.java
1 1 package cn.iocoder.yudao.module.iot.service.product;
2 2  
3 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4   -import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductPageReqVO;
5   -import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductSaveReqVO;
6   -import cn.iocoder.yudao.module.iot.dal.dataobject.product.ProductDO;
  4 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductPageReqVO;
  5 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductSaveReqVO;
  6 +import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
7 7 import jakarta.validation.Valid;
8 8  
9 9 /**
... ... @@ -11,7 +11,7 @@ import jakarta.validation.Valid;
11 11 *
12 12 * @author ahh
13 13 */
14   -public interface ProductService {
  14 +public interface IotProductService {
15 15  
16 16 /**
17 17 * 创建产品
... ... @@ -19,14 +19,14 @@ public interface ProductService {
19 19 * @param createReqVO 创建信息
20 20 * @return 编号
21 21 */
22   - Long createProduct(@Valid ProductSaveReqVO createReqVO);
  22 + Long createProduct(@Valid IotProductSaveReqVO createReqVO);
23 23  
24 24 /**
25 25 * 更新产品
26 26 *
27 27 * @param updateReqVO 更新信息
28 28 */
29   - void updateProduct(@Valid ProductSaveReqVO updateReqVO);
  29 + void updateProduct(@Valid IotProductSaveReqVO updateReqVO);
30 30  
31 31 /**
32 32 * 删除产品
... ... @@ -41,7 +41,7 @@ public interface ProductService {
41 41 * @param id 编号
42 42 * @return 产品
43 43 */
44   - ProductDO getProduct(Long id);
  44 + IotProductDO getProduct(Long id);
45 45  
46 46 /**
47 47 * 获得产品分页
... ... @@ -49,7 +49,7 @@ public interface ProductService {
49 49 * @param pageReqVO 分页查询
50 50 * @return 产品分页
51 51 */
52   - PageResult<ProductDO> getProductPage(ProductPageReqVO pageReqVO);
  52 + PageResult<IotProductDO> getProductPage(IotProductPageReqVO pageReqVO);
53 53  
54 54 /**
55 55 * 更新产品状态
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/ProductServiceImpl.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductServiceImpl.java
1 1 package cn.iocoder.yudao.module.iot.service.product;
2 2  
  3 +import cn.hutool.core.util.StrUtil;
3 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 5 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
5   -import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductPageReqVO;
6   -import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductSaveReqVO;
7   -import cn.iocoder.yudao.module.iot.dal.dataobject.product.ProductDO;
8   -import cn.iocoder.yudao.module.iot.dal.mysql.product.ProductMapper;
  6 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductPageReqVO;
  7 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductSaveReqVO;
  8 +import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
  9 +import cn.iocoder.yudao.module.iot.dal.mysql.product.IotProductMapper;
9 10 import cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum;
10 11 import jakarta.annotation.Resource;
11 12 import org.springframework.stereotype.Service;
... ... @@ -25,17 +26,17 @@ import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_STATU
25 26 */
26 27 @Service
27 28 @Validated
28   -public class ProductServiceImpl implements ProductService {
  29 +public class IotProductServiceImpl implements IotProductService {
29 30  
30 31 @Resource
31   - private ProductMapper productMapper;
  32 + private IotProductMapper productMapper;
32 33  
33 34 @Override
34   - public Long createProduct(ProductSaveReqVO createReqVO) {
35   - // 生成 ProductKey
  35 + public Long createProduct(IotProductSaveReqVO createReqVO) {
  36 + // 1. 生成 ProductKey
36 37 createProductKey(createReqVO);
37   - // 插入
38   - ProductDO product = BeanUtils.toBean(createReqVO, ProductDO.class);
  38 + // 2. 插入
  39 + IotProductDO product = BeanUtils.toBean(createReqVO, IotProductDO.class);
39 40 productMapper.insert(product);
40 41 return product.getId();
41 42 }
... ... @@ -45,64 +46,71 @@ public class ProductServiceImpl implements ProductService {
45 46 *
46 47 * @param createReqVO 创建信息
47 48 */
48   - private void createProductKey(ProductSaveReqVO createReqVO) {
49   - // TODO @haohao:应该前端没传递的时候,才生成哇?ps:需要校验下唯一性,万一有重复;
50   - // 生成随机的 11 位字符串
51   - String productKey = UUID.randomUUID().toString().replace("-", "").substring(0, 11);
  49 + private void createProductKey(IotProductSaveReqVO createReqVO) {
  50 + String productKey = createReqVO.getProductKey();
  51 + // 1. productKey为空,生成随机的 11 位字符串
  52 + if (StrUtil.isEmpty(productKey)) {
  53 + productKey = UUID.randomUUID().toString().replace("-", "").substring(0, 11);
  54 + }
  55 + // 2. 校验唯一性
  56 + if (productMapper.selectByProductKey(productKey) != null) {
  57 + throw exception(PRODUCT_NOT_EXISTS);
  58 + }
52 59 createReqVO.setProductKey(productKey);
53 60 }
54 61  
55 62 @Override
56   - public void updateProduct(ProductSaveReqVO updateReqVO) {
  63 + public void updateProduct(IotProductSaveReqVO updateReqVO) {
57 64 updateReqVO.setProductKey(null); // 不更新产品标识
58   - // 校验存在
59   - validateProductExists(updateReqVO.getId());
60   - // TODO @haohao:如果已经发布,允许编辑么?
61   - // 更新
62   - ProductDO updateObj = BeanUtils.toBean(updateReqVO, ProductDO.class);
  65 + // 1.1 校验存在
  66 + IotProductDO iotProductDO = validateProductExists(updateReqVO.getId());
  67 + // 1.2 发布状态不可更新
  68 + validateProductStatus(iotProductDO);
  69 + // 2. 更新
  70 + IotProductDO updateObj = BeanUtils.toBean(updateReqVO, IotProductDO.class);
63 71 productMapper.updateById(updateObj);
64 72 }
65 73  
66 74 @Override
67 75 public void deleteProduct(Long id) {
68   - // TODO @haohao:这里最好只查询一次哈
69 76 // 1.1 校验存在
70   - validateProductExists(id);
  77 + IotProductDO iotProductDO = validateProductExists(id);
71 78 // 1.2 发布状态不可删除
72   - validateProductStatus(id);
  79 + validateProductStatus(iotProductDO);
73 80 // 2. 删除
74 81 productMapper.deleteById(id);
75 82 }
76 83  
77   - private void validateProductExists(Long id) {
78   - if (productMapper.selectById(id) == null) {
  84 + private IotProductDO validateProductExists(Long id) {
  85 + IotProductDO iotProductDO = productMapper.selectById(id);
  86 + if (iotProductDO == null) {
79 87 throw exception(PRODUCT_NOT_EXISTS);
80 88 }
  89 + return iotProductDO;
81 90 }
82 91  
83   - private void validateProductStatus(Long id) {
84   - ProductDO product = productMapper.selectById(id);
85   - if (Objects.equals(product.getStatus(), IotProductStatusEnum.PUBLISHED.getType())) {
  92 + private void validateProductStatus(IotProductDO iotProductDO) {
  93 + if (Objects.equals(iotProductDO.getStatus(), IotProductStatusEnum.PUBLISHED.getType())) {
86 94 throw exception(PRODUCT_STATUS_NOT_DELETE);
87 95 }
88 96 }
89 97  
90 98 @Override
91   - public ProductDO getProduct(Long id) {
  99 + public IotProductDO getProduct(Long id) {
92 100 return productMapper.selectById(id);
93 101 }
94 102  
95 103 @Override
96   - public PageResult<ProductDO> getProductPage(ProductPageReqVO pageReqVO) {
  104 + public PageResult<IotProductDO> getProductPage(IotProductPageReqVO pageReqVO) {
97 105 return productMapper.selectPage(pageReqVO);
98 106 }
99 107  
100 108 @Override
101 109 public void updateProductStatus(Long id, Integer status) {
102   - // 校验存在
  110 + // 1. 校验存在
103 111 validateProductExists(id);
104   - // 更新
105   - ProductDO updateObj = ProductDO.builder().id(id).status(status).build();
  112 + // 2. 更新
  113 + IotProductDO updateObj = IotProductDO.builder().id(id).status(status).build();
106 114 productMapper.updateById(updateObj);
107 115 }
108 116  
... ...