mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-04 20:28:44 +08:00 
			
		
		
		
	【代码评审】IOT:产品的管理
This commit is contained in:
		@@ -15,9 +15,10 @@
 | 
			
		||||
    <name>${project.artifactId}</name>
 | 
			
		||||
    <description>
 | 
			
		||||
        物联网 模块,主要实现 产品管理、设备管理、协议管理等功能。
 | 
			
		||||
        <!-- TODO 芋艿:后续补充下 -->
 | 
			
		||||
    </description>
 | 
			
		||||
 | 
			
		||||
    <dependencies>  <!-- 5. 新增依赖,这里引入的都是比较常用的业务组件、技术组件 -->
 | 
			
		||||
    <dependencies>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>cn.iocoder.boot</groupId>
 | 
			
		||||
            <artifactId>yudao-module-iot-api</artifactId>
 | 
			
		||||
@@ -53,7 +54,7 @@
 | 
			
		||||
            <artifactId>yudao-spring-boot-starter-excel</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
 | 
			
		||||
        <!-- mqtt -->
 | 
			
		||||
        <!-- MQTT -->
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>org.eclipse.paho</groupId>
 | 
			
		||||
            <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
 | 
			
		||||
 
 | 
			
		||||
@@ -27,6 +27,7 @@ import java.util.List;
 | 
			
		||||
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
 | 
			
		||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 | 
			
		||||
 | 
			
		||||
// TODO @haohao:Iot 前缀要加,不然很容易重复哈
 | 
			
		||||
@Tag(name = "管理后台 - IOT 产品")
 | 
			
		||||
@RestController
 | 
			
		||||
@RequestMapping("/iot/product")
 | 
			
		||||
 
 | 
			
		||||
@@ -1,14 +1,18 @@
 | 
			
		||||
package cn.iocoder.yudao.module.iot.controller.admin.product.vo;
 | 
			
		||||
 | 
			
		||||
import lombok.*;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import io.swagger.v3.oas.annotations.media.Schema;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
 | 
			
		||||
import io.swagger.v3.oas.annotations.media.Schema;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.EqualsAndHashCode;
 | 
			
		||||
import lombok.ToString;
 | 
			
		||||
import org.springframework.format.annotation.DateTimeFormat;
 | 
			
		||||
 | 
			
		||||
import java.time.LocalDateTime;
 | 
			
		||||
 | 
			
		||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
 | 
			
		||||
 | 
			
		||||
// TODO @haohao:涉及到 iot 的拼写,要不都用 IoT,貌似更规范
 | 
			
		||||
// TODO 芋艿:需要清理掉一些无用字段
 | 
			
		||||
@Schema(description = "管理后台 - iot 产品分页 Request VO")
 | 
			
		||||
@Data
 | 
			
		||||
@EqualsAndHashCode(callSuper = true)
 | 
			
		||||
@@ -25,6 +29,9 @@ public class ProductPageReqVO extends PageParam {
 | 
			
		||||
    @Schema(description = "产品标识")
 | 
			
		||||
    private String productKey;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "接入网关协议", example = "2")
 | 
			
		||||
    private Integer protocolType;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "协议编号(脚本解析 id)", example = "13177")
 | 
			
		||||
    private Long protocolId;
 | 
			
		||||
 | 
			
		||||
@@ -34,22 +41,19 @@ public class ProductPageReqVO extends PageParam {
 | 
			
		||||
    @Schema(description = "产品描述", example = "你猜")
 | 
			
		||||
    private String description;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "数据校验级别, 0: 强校验, 1: 弱校验, 2: 免校验", example = "1")
 | 
			
		||||
    @Schema(description = "数据校验级别", example = "1")
 | 
			
		||||
    private Integer validateType;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "产品状态, 0: DEVELOPMENT_STATUS, 1: RELEASE_STATUS", example = "1")
 | 
			
		||||
    @Schema(description = "产品状态", example = "1")
 | 
			
		||||
    private Integer status;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "设备类型, 0: 直连设备, 1: 网关子设备, 2: 网关设备", example = "2")
 | 
			
		||||
    @Schema(description = "设备类型", example = "2")
 | 
			
		||||
    private Integer deviceType;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "联网方式, 0: Wi-Fi, 1: Cellular, 2: Ethernet, 3: 其他", example = "2")
 | 
			
		||||
    @Schema(description = "联网方式", example = "2")
 | 
			
		||||
    private Integer netType;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "接入网关协议, 0: modbus, 1: opc-ua, 2: customize, 3: ble, 4: zigbee", example = "2")
 | 
			
		||||
    private Integer protocolType;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "数据格式, 0: 透传模式, 1: Alink JSON")
 | 
			
		||||
    @Schema(description = "数据格式", example = "0")
 | 
			
		||||
    private Integer dataFormat;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,17 +1,21 @@
 | 
			
		||||
package cn.iocoder.yudao.module.iot.controller.admin.product.vo;
 | 
			
		||||
 | 
			
		||||
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
 | 
			
		||||
import com.alibaba.excel.annotation.ExcelProperty;
 | 
			
		||||
import io.swagger.v3.oas.annotations.media.Schema;
 | 
			
		||||
import lombok.*;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import org.springframework.format.annotation.DateTimeFormat;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
 | 
			
		||||
import java.time.LocalDateTime;
 | 
			
		||||
import com.alibaba.excel.annotation.*;
 | 
			
		||||
 | 
			
		||||
@Schema(description = "管理后台 - iot 产品 Response VO")
 | 
			
		||||
@Data
 | 
			
		||||
@ExcelIgnoreUnannotated
 | 
			
		||||
public class ProductRespVO {
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "26087")
 | 
			
		||||
    @ExcelProperty("产品ID")
 | 
			
		||||
    private Long id;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "产品名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "李四")
 | 
			
		||||
    @ExcelProperty("产品名称")
 | 
			
		||||
    private String name;
 | 
			
		||||
@@ -20,14 +24,14 @@ public class ProductRespVO {
 | 
			
		||||
    @ExcelProperty("创建时间")
 | 
			
		||||
    private LocalDateTime createTime;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "26087")
 | 
			
		||||
    @ExcelProperty("产品ID")
 | 
			
		||||
    private Long id;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "产品标识", requiredMode = Schema.RequiredMode.REQUIRED)
 | 
			
		||||
    @ExcelProperty("产品标识")
 | 
			
		||||
    private String productKey;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "接入网关协议", example = "2")
 | 
			
		||||
    @ExcelProperty("接入网关协议")
 | 
			
		||||
    private Integer protocolType;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "协议编号(脚本解析 id)", requiredMode = Schema.RequiredMode.REQUIRED, example = "13177")
 | 
			
		||||
    @ExcelProperty("协议编号(脚本解析 id)")
 | 
			
		||||
    private Long protocolId;
 | 
			
		||||
@@ -40,28 +44,24 @@ public class ProductRespVO {
 | 
			
		||||
    @ExcelProperty("产品描述")
 | 
			
		||||
    private String description;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "数据校验级别, 0: 强校验, 1: 弱校验, 2: 免校验", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
 | 
			
		||||
    @ExcelProperty("数据校验级别, 0: 强校验, 1: 弱校验, 2: 免校验")
 | 
			
		||||
    @Schema(description = "数据校验级别", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
 | 
			
		||||
    @ExcelProperty("数据校验级别")
 | 
			
		||||
    private Integer validateType;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "产品状态, 0: DEVELOPMENT_STATUS, 1: RELEASE_STATUS", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
 | 
			
		||||
    @ExcelProperty("产品状态, 0: DEVELOPMENT_STATUS, 1: RELEASE_STATUS")
 | 
			
		||||
    @Schema(description = "产品状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
 | 
			
		||||
    @ExcelProperty("产品状态")
 | 
			
		||||
    private Integer status;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "设备类型, 0: 直连设备, 1: 网关子设备, 2: 网关设备", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
 | 
			
		||||
    @ExcelProperty("设备类型, 0: 直连设备, 1: 网关子设备, 2: 网关设备")
 | 
			
		||||
    @Schema(description = "设备类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
 | 
			
		||||
    @ExcelProperty("设备类型")
 | 
			
		||||
    private Integer deviceType;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "联网方式, 0: Wi-Fi, 1: Cellular, 2: Ethernet, 3: 其他", example = "2")
 | 
			
		||||
    @ExcelProperty("联网方式, 0: Wi-Fi, 1: Cellular, 2: Ethernet, 3: 其他")
 | 
			
		||||
    @Schema(description = "联网方式", example = "2")
 | 
			
		||||
    @ExcelProperty("联网方式")
 | 
			
		||||
    private Integer netType;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "接入网关协议, 0: modbus, 1: opc-ua, 2: customize, 3: ble, 4: zigbee", example = "2")
 | 
			
		||||
    @ExcelProperty("接入网关协议, 0: modbus, 1: opc-ua, 2: customize, 3: ble, 4: zigbee")
 | 
			
		||||
    private Integer protocolType;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "数据格式, 0: 透传模式, 1: Alink JSON")
 | 
			
		||||
    @ExcelProperty("数据格式, 0: 透传模式, 1: Alink JSON")
 | 
			
		||||
    @Schema(description = "数据格式")
 | 
			
		||||
    @ExcelProperty("数据格式")
 | 
			
		||||
    private Integer dataFormat;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,16 +1,15 @@
 | 
			
		||||
package cn.iocoder.yudao.module.iot.dal.dataobject.product;
 | 
			
		||||
 | 
			
		||||
import lombok.*;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.time.LocalDateTime;
 | 
			
		||||
import java.time.LocalDateTime;
 | 
			
		||||
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
 | 
			
		||||
 *
 | 
			
		||||
 * @author 芋道源码
 | 
			
		||||
 * @author ahh
 | 
			
		||||
 */
 | 
			
		||||
@TableName("iot_product")
 | 
			
		||||
@KeySequence("iot_product_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
 | 
			
		||||
@@ -22,54 +21,73 @@ import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
 | 
			
		||||
@AllArgsConstructor
 | 
			
		||||
public class ProductDO extends BaseDO {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 产品名称
 | 
			
		||||
     */
 | 
			
		||||
    private String name;
 | 
			
		||||
    /**
 | 
			
		||||
     * 产品ID
 | 
			
		||||
     */
 | 
			
		||||
    @TableId
 | 
			
		||||
    private Long id;
 | 
			
		||||
    /**
 | 
			
		||||
     * 产品名称
 | 
			
		||||
     */
 | 
			
		||||
    private String name;
 | 
			
		||||
    // TODO @haohao:这个字段,要不改成 identifier,和阿里云更统一些
 | 
			
		||||
    /**
 | 
			
		||||
     * 产品标识
 | 
			
		||||
     */
 | 
			
		||||
    private String productKey;
 | 
			
		||||
    /**
 | 
			
		||||
     * 协议编号(脚本解析 id)
 | 
			
		||||
     */
 | 
			
		||||
    private Long protocolId;
 | 
			
		||||
    /**
 | 
			
		||||
     * 产品所属品类标识符
 | 
			
		||||
     * 产品所属品类编号
 | 
			
		||||
     *
 | 
			
		||||
     * TODO 外键:后续加
 | 
			
		||||
     */
 | 
			
		||||
    private Long categoryId;
 | 
			
		||||
    /**
 | 
			
		||||
     * 产品描述
 | 
			
		||||
     */
 | 
			
		||||
    private String description;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 数据校验级别, 0: 强校验, 1: 弱校验, 2: 免校验
 | 
			
		||||
     */
 | 
			
		||||
    private Integer validateType;
 | 
			
		||||
    /**
 | 
			
		||||
     * 产品状态, 0: DEVELOPMENT_STATUS, 1: RELEASE_STATUS
 | 
			
		||||
     * 产品状态
 | 
			
		||||
     *
 | 
			
		||||
     * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum}
 | 
			
		||||
     */
 | 
			
		||||
    private Integer status;
 | 
			
		||||
    /**
 | 
			
		||||
     * 设备类型, 0: 直连设备, 1: 网关子设备, 2: 网关设备
 | 
			
		||||
     * 设备类型
 | 
			
		||||
     *
 | 
			
		||||
     * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotProductDeviceTypeEnum}
 | 
			
		||||
     */
 | 
			
		||||
    private Integer deviceType;
 | 
			
		||||
    /**
 | 
			
		||||
     * 联网方式, 0: Wi-Fi, 1: Cellular, 2: Ethernet, 3: 其他
 | 
			
		||||
     * 联网方式
 | 
			
		||||
     *
 | 
			
		||||
     * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotNetTypeEnum}
 | 
			
		||||
     */
 | 
			
		||||
    private Integer netType;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 接入网关协议, 0: modbus, 1: opc-ua, 2: customize, 3: ble, 4: zigbee
 | 
			
		||||
     * 接入网关协议
 | 
			
		||||
     *
 | 
			
		||||
     * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotProtocolTypeEnum}
 | 
			
		||||
     */
 | 
			
		||||
    private Integer protocolType;
 | 
			
		||||
    /**
 | 
			
		||||
     * 数据格式, 0: 透传模式, 1: Alink JSON
 | 
			
		||||
     * 协议编号
 | 
			
		||||
     *
 | 
			
		||||
     * TODO 外键:后续加
 | 
			
		||||
     */
 | 
			
		||||
    private Long protocolId;
 | 
			
		||||
    /**
 | 
			
		||||
     * 数据格式
 | 
			
		||||
     *
 | 
			
		||||
     * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotDataFormatEnum}
 | 
			
		||||
     */
 | 
			
		||||
    private Integer dataFormat;
 | 
			
		||||
    /**
 | 
			
		||||
     * 数据校验级别
 | 
			
		||||
     *
 | 
			
		||||
     * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotValidateTypeEnum}
 | 
			
		||||
     */
 | 
			
		||||
    private Integer validateType;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -10,7 +10,7 @@ import org.apache.ibatis.annotations.Mapper;
 | 
			
		||||
/**
 | 
			
		||||
 * iot 产品 Mapper
 | 
			
		||||
 *
 | 
			
		||||
 * @author 芋道源码
 | 
			
		||||
 * @author ahh
 | 
			
		||||
 */
 | 
			
		||||
@Mapper
 | 
			
		||||
public interface ProductMapper extends BaseMapperX<ProductDO> {
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@ import org.eclipse.paho.client.mqttv3.MqttMessage;
 | 
			
		||||
import org.springframework.context.annotation.Lazy;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
// TODO @芋艿:详细再瞅瞅
 | 
			
		||||
/**
 | 
			
		||||
 * 用于处理MQTT连接的回调,如连接断开、消息到达、消息发布完成、连接完成等事件。
 | 
			
		||||
 *
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,8 @@ import lombok.Data;
 | 
			
		||||
import org.springframework.boot.context.properties.ConfigurationProperties;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
// TODO @芋艿:详细再瞅瞅
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 配置类,用于读取MQTT连接的配置信息,如用户名、密码、连接地址等
 | 
			
		||||
 *
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.iot.emq.service;
 | 
			
		||||
import org.eclipse.paho.client.mqttv3.MqttClient;
 | 
			
		||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
 | 
			
		||||
 | 
			
		||||
// TODO @芋艿:在瞅瞅
 | 
			
		||||
/**
 | 
			
		||||
 * 用于处理MQTT消息的具体业务逻辑,如订阅回调
 | 
			
		||||
 *
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,8 @@ import org.eclipse.paho.client.mqttv3.MqttClient;
 | 
			
		||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
// TODO @芋艿:在瞅瞅
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 用于处理MQTT消息的具体业务逻辑,如订阅回调
 | 
			
		||||
 *
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,8 @@ import org.springframework.boot.ApplicationArguments;
 | 
			
		||||
import org.springframework.boot.ApplicationRunner;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
// TODO @芋艿:在瞅瞅
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 用于在应用启动时自动连接MQTT服务器
 | 
			
		||||
 *
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
/**
 | 
			
		||||
 * 属于 iot 模块的 framework 封装
 | 
			
		||||
 *
 | 
			
		||||
 * @author 芋道源码
 | 
			
		||||
 * @author ahh
 | 
			
		||||
 */
 | 
			
		||||
package cn.iocoder.yudao.module.iot.framework;
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ import org.springframework.context.annotation.Configuration;
 | 
			
		||||
/**
 | 
			
		||||
 * iot 模块的 web 组件的 Configuration
 | 
			
		||||
 *
 | 
			
		||||
 * @author 芋道源码
 | 
			
		||||
 * @author ahh
 | 
			
		||||
 */
 | 
			
		||||
@Configuration(proxyBeanMethods = false)
 | 
			
		||||
public class IotWebConfiguration {
 | 
			
		||||
 
 | 
			
		||||
@@ -9,7 +9,7 @@ import jakarta.validation.Valid;
 | 
			
		||||
/**
 | 
			
		||||
 * IOT 产品 Service 接口
 | 
			
		||||
 *
 | 
			
		||||
 * @author 芋道源码
 | 
			
		||||
 * @author ahh
 | 
			
		||||
 */
 | 
			
		||||
public interface ProductService {
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -21,7 +21,7 @@ import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_STATU
 | 
			
		||||
/**
 | 
			
		||||
 * IOT 产品 Service 实现类
 | 
			
		||||
 *
 | 
			
		||||
 * @author 芋道源码
 | 
			
		||||
 * @author ahh
 | 
			
		||||
 */
 | 
			
		||||
@Service
 | 
			
		||||
@Validated
 | 
			
		||||
@@ -37,7 +37,6 @@ public class ProductServiceImpl implements ProductService {
 | 
			
		||||
        // 插入
 | 
			
		||||
        ProductDO product = BeanUtils.toBean(createReqVO, ProductDO.class);
 | 
			
		||||
        productMapper.insert(product);
 | 
			
		||||
        // 返回
 | 
			
		||||
        return product.getId();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -47,6 +46,7 @@ public class ProductServiceImpl implements ProductService {
 | 
			
		||||
     * @param createReqVO 创建信息
 | 
			
		||||
     */
 | 
			
		||||
    private void createProductKey(ProductSaveReqVO createReqVO) {
 | 
			
		||||
        // TODO @haohao:应该前端没传递的时候,才生成哇?ps:需要校验下唯一性,万一有重复;
 | 
			
		||||
        // 生成随机的 11 位字符串
 | 
			
		||||
        String productKey = UUID.randomUUID().toString().replace("-", "").substring(0, 11);
 | 
			
		||||
        createReqVO.setProductKey(productKey);
 | 
			
		||||
@@ -54,8 +54,10 @@ public class ProductServiceImpl implements ProductService {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void updateProduct(ProductSaveReqVO updateReqVO) {
 | 
			
		||||
        updateReqVO.setProductKey(null); // 不更新产品标识
 | 
			
		||||
        // 校验存在
 | 
			
		||||
        validateProductExists(updateReqVO.getId());
 | 
			
		||||
        // TODO @haohao:如果已经发布,允许编辑么?
 | 
			
		||||
        // 更新
 | 
			
		||||
        ProductDO updateObj = BeanUtils.toBean(updateReqVO, ProductDO.class);
 | 
			
		||||
        productMapper.updateById(updateObj);
 | 
			
		||||
@@ -63,11 +65,12 @@ public class ProductServiceImpl implements ProductService {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void deleteProduct(Long id) {
 | 
			
		||||
        // 校验存在
 | 
			
		||||
        // TODO @haohao:这里最好只查询一次哈
 | 
			
		||||
        // 1.1 校验存在
 | 
			
		||||
        validateProductExists(id);
 | 
			
		||||
        // 发布状态不可删除
 | 
			
		||||
        // 1.2 发布状态不可删除
 | 
			
		||||
        validateProductStatus(id);
 | 
			
		||||
        // 删除
 | 
			
		||||
        // 2. 删除
 | 
			
		||||
        productMapper.deleteById(id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +0,0 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 | 
			
		||||
<mapper namespace="cn.iocoder.yudao.module.iot.dal.mysql.product.ProductMapper">
 | 
			
		||||
 | 
			
		||||
    <!--
 | 
			
		||||
        一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
 | 
			
		||||
        无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
 | 
			
		||||
        代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
 | 
			
		||||
        文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
 | 
			
		||||
     -->
 | 
			
		||||
 | 
			
		||||
</mapper>
 | 
			
		||||
@@ -1,174 +0,0 @@
 | 
			
		||||
package cn.iocoder.yudao.module.iot.service.product;
 | 
			
		||||
 | 
			
		||||
import org.junit.jupiter.api.Disabled;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
import org.springframework.boot.test.mock.mockito.MockBean;
 | 
			
		||||
 | 
			
		||||
import jakarta.annotation.Resource;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.*;
 | 
			
		||||
import cn.iocoder.yudao.module.iot.dal.dataobject.product.ProductDO;
 | 
			
		||||
import cn.iocoder.yudao.module.iot.dal.mysql.product.ProductMapper;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
 | 
			
		||||
 | 
			
		||||
import jakarta.annotation.Resource;
 | 
			
		||||
import org.springframework.context.annotation.Import;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.time.LocalDateTime;
 | 
			
		||||
 | 
			
		||||
import static cn.hutool.core.util.RandomUtil.*;
 | 
			
		||||
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
 | 
			
		||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
 | 
			
		||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
 | 
			
		||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*;
 | 
			
		||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
 | 
			
		||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.*;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.*;
 | 
			
		||||
import static org.mockito.Mockito.*;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * {@link ProductServiceImpl} 的单元测试类
 | 
			
		||||
 *
 | 
			
		||||
 * @author 芋道源码
 | 
			
		||||
 */
 | 
			
		||||
@Import(ProductServiceImpl.class)
 | 
			
		||||
public class ProductServiceImplTest extends BaseDbUnitTest {
 | 
			
		||||
 | 
			
		||||
    @Resource
 | 
			
		||||
    private ProductServiceImpl productService;
 | 
			
		||||
 | 
			
		||||
    @Resource
 | 
			
		||||
    private ProductMapper productMapper;
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testCreateProduct_success() {
 | 
			
		||||
        // 准备参数
 | 
			
		||||
        ProductSaveReqVO createReqVO = randomPojo(ProductSaveReqVO.class).setId(null);
 | 
			
		||||
 | 
			
		||||
        // 调用
 | 
			
		||||
        Long productId = productService.createProduct(createReqVO);
 | 
			
		||||
        // 断言
 | 
			
		||||
        assertNotNull(productId);
 | 
			
		||||
        // 校验记录的属性是否正确
 | 
			
		||||
        ProductDO product = productMapper.selectById(productId);
 | 
			
		||||
        assertPojoEquals(createReqVO, product, "id");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testUpdateProduct_success() {
 | 
			
		||||
        // mock 数据
 | 
			
		||||
        ProductDO dbProduct = randomPojo(ProductDO.class);
 | 
			
		||||
        productMapper.insert(dbProduct);// @Sql: 先插入出一条存在的数据
 | 
			
		||||
        // 准备参数
 | 
			
		||||
        ProductSaveReqVO updateReqVO = randomPojo(ProductSaveReqVO.class, o -> {
 | 
			
		||||
            o.setId(dbProduct.getId()); // 设置更新的 ID
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        // 调用
 | 
			
		||||
        productService.updateProduct(updateReqVO);
 | 
			
		||||
        // 校验是否更新正确
 | 
			
		||||
        ProductDO product = productMapper.selectById(updateReqVO.getId()); // 获取最新的
 | 
			
		||||
        assertPojoEquals(updateReqVO, product);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testUpdateProduct_notExists() {
 | 
			
		||||
        // 准备参数
 | 
			
		||||
        ProductSaveReqVO updateReqVO = randomPojo(ProductSaveReqVO.class);
 | 
			
		||||
 | 
			
		||||
        // 调用, 并断言异常
 | 
			
		||||
        assertServiceException(() -> productService.updateProduct(updateReqVO), PRODUCT_NOT_EXISTS);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testDeleteProduct_success() {
 | 
			
		||||
        // mock 数据
 | 
			
		||||
        ProductDO dbProduct = randomPojo(ProductDO.class);
 | 
			
		||||
        productMapper.insert(dbProduct);// @Sql: 先插入出一条存在的数据
 | 
			
		||||
        // 准备参数
 | 
			
		||||
        Long id = dbProduct.getId();
 | 
			
		||||
 | 
			
		||||
        // 调用
 | 
			
		||||
        productService.deleteProduct(id);
 | 
			
		||||
       // 校验数据不存在了
 | 
			
		||||
       assertNull(productMapper.selectById(id));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testDeleteProduct_notExists() {
 | 
			
		||||
        // 准备参数
 | 
			
		||||
        Long id = randomLongId();
 | 
			
		||||
 | 
			
		||||
        // 调用, 并断言异常
 | 
			
		||||
        assertServiceException(() -> productService.deleteProduct(id), PRODUCT_NOT_EXISTS);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    @Disabled  // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
 | 
			
		||||
    public void testGetProductPage() {
 | 
			
		||||
       // mock 数据
 | 
			
		||||
       ProductDO dbProduct = randomPojo(ProductDO.class, o -> { // 等会查询到
 | 
			
		||||
           o.setName(null);
 | 
			
		||||
           o.setCreateTime(null);
 | 
			
		||||
           o.setProductKey(null);
 | 
			
		||||
           o.setProtocolId(null);
 | 
			
		||||
           o.setCategoryId(null);
 | 
			
		||||
           o.setDescription(null);
 | 
			
		||||
           o.setValidateType(null);
 | 
			
		||||
           o.setStatus(null);
 | 
			
		||||
           o.setDeviceType(null);
 | 
			
		||||
           o.setNetType(null);
 | 
			
		||||
           o.setProtocolType(null);
 | 
			
		||||
           o.setDataFormat(null);
 | 
			
		||||
       });
 | 
			
		||||
       productMapper.insert(dbProduct);
 | 
			
		||||
       // 测试 name 不匹配
 | 
			
		||||
       productMapper.insert(cloneIgnoreId(dbProduct, o -> o.setName(null)));
 | 
			
		||||
       // 测试 createTime 不匹配
 | 
			
		||||
       productMapper.insert(cloneIgnoreId(dbProduct, o -> o.setCreateTime(null)));
 | 
			
		||||
       // 测试 productKey 不匹配
 | 
			
		||||
       productMapper.insert(cloneIgnoreId(dbProduct, o -> o.setProductKey(null)));
 | 
			
		||||
       // 测试 protocolId 不匹配
 | 
			
		||||
       productMapper.insert(cloneIgnoreId(dbProduct, o -> o.setProtocolId(null)));
 | 
			
		||||
       // 测试 categoryId 不匹配
 | 
			
		||||
       productMapper.insert(cloneIgnoreId(dbProduct, o -> o.setCategoryId(null)));
 | 
			
		||||
       // 测试 description 不匹配
 | 
			
		||||
       productMapper.insert(cloneIgnoreId(dbProduct, o -> o.setDescription(null)));
 | 
			
		||||
       // 测试 validateType 不匹配
 | 
			
		||||
       productMapper.insert(cloneIgnoreId(dbProduct, o -> o.setValidateType(null)));
 | 
			
		||||
       // 测试 status 不匹配
 | 
			
		||||
       productMapper.insert(cloneIgnoreId(dbProduct, o -> o.setStatus(null)));
 | 
			
		||||
       // 测试 deviceType 不匹配
 | 
			
		||||
       productMapper.insert(cloneIgnoreId(dbProduct, o -> o.setDeviceType(null)));
 | 
			
		||||
       // 测试 netType 不匹配
 | 
			
		||||
       productMapper.insert(cloneIgnoreId(dbProduct, o -> o.setNetType(null)));
 | 
			
		||||
       // 测试 protocolType 不匹配
 | 
			
		||||
       productMapper.insert(cloneIgnoreId(dbProduct, o -> o.setProtocolType(null)));
 | 
			
		||||
       // 测试 dataFormat 不匹配
 | 
			
		||||
       productMapper.insert(cloneIgnoreId(dbProduct, o -> o.setDataFormat(null)));
 | 
			
		||||
       // 准备参数
 | 
			
		||||
       ProductPageReqVO reqVO = new ProductPageReqVO();
 | 
			
		||||
       reqVO.setName(null);
 | 
			
		||||
       reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
 | 
			
		||||
       reqVO.setProductKey(null);
 | 
			
		||||
       reqVO.setProtocolId(null);
 | 
			
		||||
       reqVO.setCategoryId(null);
 | 
			
		||||
       reqVO.setDescription(null);
 | 
			
		||||
       reqVO.setValidateType(null);
 | 
			
		||||
       reqVO.setStatus(null);
 | 
			
		||||
       reqVO.setDeviceType(null);
 | 
			
		||||
       reqVO.setNetType(null);
 | 
			
		||||
       reqVO.setProtocolType(null);
 | 
			
		||||
       reqVO.setDataFormat(null);
 | 
			
		||||
 | 
			
		||||
       // 调用
 | 
			
		||||
       PageResult<ProductDO> pageResult = productService.getProductPage(reqVO);
 | 
			
		||||
       // 断言
 | 
			
		||||
       assertEquals(1, pageResult.getTotal());
 | 
			
		||||
       assertEquals(1, pageResult.getList().size());
 | 
			
		||||
       assertPojoEquals(dbProduct, pageResult.getList().get(0));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user