mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-01 02:38:43 +08:00 
			
		
		
		
	【代码评审】IOT:设备的 review
This commit is contained in:
		| @@ -1,19 +1,25 @@ | ||||
| package cn.iocoder.yudao.module.iot.enums.device; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.common.core.IntArrayValuable; | ||||
| import lombok.Getter; | ||||
|  | ||||
| import java.util.Arrays; | ||||
|  | ||||
| /** | ||||
|  * IoT 设备状态枚举 | ||||
|  * 设备状态:0 - 未激活,1 - 在线,2 - 离线,3 - 已禁用 | ||||
|  * | ||||
|  * @author haohao | ||||
|  */ | ||||
| @Getter | ||||
| public enum IotDeviceStatusEnum { | ||||
| public enum IotDeviceStatusEnum implements IntArrayValuable { | ||||
|  | ||||
|     INACTIVE(0, "未激活"), | ||||
|     ONLINE(1, "在线"), | ||||
|     OFFLINE(2, "离线"), | ||||
|     DISABLED(3, "已禁用"); | ||||
|  | ||||
|     public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(IotDeviceStatusEnum::getStatus).toArray(); | ||||
|  | ||||
|     /** | ||||
|      * 状态 | ||||
|      */ | ||||
| @@ -40,4 +46,10 @@ public enum IotDeviceStatusEnum { | ||||
|     public static boolean isValidStatus(Integer status) { | ||||
|         return fromStatus(status) != null; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public int[] array() { | ||||
|         return ARRAYS; | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -37,14 +37,14 @@ public class IotDeviceController { | ||||
|     private IotDeviceService deviceService; | ||||
|  | ||||
|     @PostMapping("/create") | ||||
|     @Operation(summary = "创建IoT 设备") | ||||
|     @Operation(summary = "创建设备") | ||||
|     @PreAuthorize("@ss.hasPermission('iot:device:create')") | ||||
|     public CommonResult<Long> createDevice(@Valid @RequestBody IotDeviceSaveReqVO createReqVO) { | ||||
|         return success(deviceService.createDevice(createReqVO)); | ||||
|     } | ||||
|  | ||||
|     @PutMapping("/update-status") | ||||
|     @Operation(summary = "更新IoT 设备状态") | ||||
|     @Operation(summary = "更新设备状态") | ||||
|     @Parameter(name = "id", description = "编号", required = true) | ||||
|     @Parameter(name = "status", description = "状态", required = true, example = "1") | ||||
|     @PreAuthorize("@ss.hasPermission('iot:device:update')") | ||||
| @@ -55,7 +55,7 @@ public class IotDeviceController { | ||||
|     } | ||||
|  | ||||
|     @PutMapping("/update") | ||||
|     @Operation(summary = "更新IoT 设备") | ||||
|     @Operation(summary = "更新设备") | ||||
|     @PreAuthorize("@ss.hasPermission('iot:device:update')") | ||||
|     public CommonResult<Boolean> updateDevice(@Valid @RequestBody IotDeviceSaveReqVO updateReqVO) { | ||||
|         deviceService.updateDevice(updateReqVO); | ||||
| @@ -63,7 +63,7 @@ public class IotDeviceController { | ||||
|     } | ||||
|  | ||||
|     @DeleteMapping("/delete") | ||||
|     @Operation(summary = "删除IoT 设备") | ||||
|     @Operation(summary = "删除设备") | ||||
|     @Parameter(name = "id", description = "编号", required = true) | ||||
|     @PreAuthorize("@ss.hasPermission('iot:device:delete')") | ||||
|     public CommonResult<Boolean> deleteDevice(@RequestParam("id") Long id) { | ||||
| @@ -72,7 +72,7 @@ public class IotDeviceController { | ||||
|     } | ||||
|  | ||||
|     @GetMapping("/get") | ||||
|     @Operation(summary = "获得IoT 设备") | ||||
|     @Operation(summary = "获得设备") | ||||
|     @Parameter(name = "id", description = "编号", required = true, example = "1024") | ||||
|     @PreAuthorize("@ss.hasPermission('iot:device:query')") | ||||
|     public CommonResult<IotDeviceRespVO> getDevice(@RequestParam("id") Long id) { | ||||
| @@ -81,7 +81,7 @@ public class IotDeviceController { | ||||
|     } | ||||
|  | ||||
|     @GetMapping("/page") | ||||
|     @Operation(summary = "获得IoT 设备分页") | ||||
|     @Operation(summary = "获得设备分页") | ||||
|     @PreAuthorize("@ss.hasPermission('iot:device:query')") | ||||
|     public CommonResult<PageResult<IotDeviceRespVO>> getDevicePage(@Valid IotDevicePageReqVO pageReqVO) { | ||||
|         PageResult<IotDeviceDO> pageResult = deviceService.getDevicePage(pageReqVO); | ||||
| @@ -89,7 +89,7 @@ public class IotDeviceController { | ||||
|     } | ||||
|  | ||||
|     @GetMapping("/export-excel") | ||||
|     @Operation(summary = "导出IoT 设备 Excel") | ||||
|     @Operation(summary = "导出设备 Excel") | ||||
|     @PreAuthorize("@ss.hasPermission('iot:device:export')") | ||||
|     @ApiAccessLog(operateType = EXPORT) | ||||
|     public void exportDeviceExcel(@Valid IotDevicePageReqVO pageReqVO, | ||||
|   | ||||
| @@ -1,10 +1,15 @@ | ||||
| package cn.iocoder.yudao.module.iot.controller.admin.device.vo; | ||||
|  | ||||
| import lombok.*; | ||||
| import io.swagger.v3.oas.annotations.media.Schema; | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageParam; | ||||
| import java.math.BigDecimal; | ||||
| import cn.iocoder.yudao.framework.common.validation.InEnum; | ||||
| import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStatusEnum; | ||||
| 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.math.BigDecimal; | ||||
| import java.time.LocalDateTime; | ||||
|  | ||||
| import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; | ||||
| @@ -15,28 +20,32 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_ | ||||
| @ToString(callSuper = true) | ||||
| public class IotDevicePageReqVO extends PageParam { | ||||
|  | ||||
|     @Schema(description = "设备唯一标识符,全局唯一,用于识别设备") | ||||
|     // TODO @芋艿:需要去掉一些多余的字段; | ||||
|  | ||||
|     @Schema(description = "设备唯一标识符", example = "24602") | ||||
|     private String deviceKey; | ||||
|  | ||||
|     @Schema(description = "设备名称,在产品内唯一,用于标识设备", example = "王五") | ||||
|     @Schema(description = "设备名称", example = "王五") | ||||
|     private String deviceName; | ||||
|  | ||||
|     @Schema(description = "产品 ID,关联 iot_product 表的 id", example = "26202") | ||||
|     @Schema(description = "产品编号", example = "26202") | ||||
|     private Long productId; | ||||
|  | ||||
|     @Schema(description = "产品 Key,关联 iot_product 表的 product_key") | ||||
|     @Schema(description = "产品标识") | ||||
|     private String productKey; | ||||
|  | ||||
|     @Schema(description = "设备类型:0 - 直连设备,1 - 网关子设备,2 - 网关设备", example = "1") | ||||
|     @Schema(description = "设备类型", example = "1") | ||||
|     // TODO @haohao:需要有个设备类型的枚举 | ||||
|     private Integer deviceType; | ||||
|  | ||||
|     @Schema(description = "设备备注名称,供用户自定义备注", example = "张三") | ||||
|     @Schema(description = "备注名称", example = "张三") | ||||
|     private String nickname; | ||||
|  | ||||
|     @Schema(description = "网关设备 ID,子设备需要关联的网关设备 ID", example = "16380") | ||||
|     @Schema(description = "网关设备 ID", example = "16380") | ||||
|     private Long gatewayId; | ||||
|  | ||||
|     @Schema(description = "设备状态:0 - 未激活,1 - 在线,2 - 离线,3 - 已禁用", example = "1") | ||||
|     @Schema(description = "设备状态", example = "1") | ||||
|     @InEnum(IotDeviceStatusEnum.class) | ||||
|     private Integer status; | ||||
|  | ||||
|     @Schema(description = "设备状态最后更新时间") | ||||
|   | ||||
| @@ -13,7 +13,7 @@ import java.time.LocalDateTime; | ||||
| @ExcelIgnoreUnannotated | ||||
| public class IotDeviceRespVO { | ||||
|  | ||||
|     @Schema(description = "设备 ID,主键,自增", requiredMode = Schema.RequiredMode.REQUIRED, example = "177") | ||||
|     @Schema(description = "设备编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "177") | ||||
|     private Long id; | ||||
|  | ||||
|     @Schema(description = "设备唯一标识符", requiredMode = Schema.RequiredMode.REQUIRED) | ||||
| @@ -24,11 +24,11 @@ public class IotDeviceRespVO { | ||||
|     @ExcelProperty("设备名称备") | ||||
|     private String deviceName; | ||||
|  | ||||
|     @Schema(description = "产品 ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202") | ||||
|     @Schema(description = "产品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202") | ||||
|     @ExcelProperty("产品 ID") | ||||
|     private Long productId; | ||||
|  | ||||
|     @Schema(description = "产品 Key", requiredMode = Schema.RequiredMode.REQUIRED) | ||||
|     @Schema(description = "产品标识", requiredMode = Schema.RequiredMode.REQUIRED) | ||||
|     @ExcelProperty("产品 Key") | ||||
|     private String productKey; | ||||
|  | ||||
| @@ -72,7 +72,7 @@ public class IotDeviceRespVO { | ||||
|     @ExcelProperty("设备的固件版本") | ||||
|     private String firmwareVersion; | ||||
|  | ||||
|     @Schema(description = "设备密钥,用于设备认证,需安全存储") | ||||
|     @Schema(description = "设备密钥,用于设备认证") | ||||
|     @ExcelProperty("设备密钥") | ||||
|     private String deviceSecret; | ||||
|  | ||||
| @@ -92,16 +92,17 @@ public class IotDeviceRespVO { | ||||
|     @ExcelProperty("认证类型(如一机一密、动态注册)") | ||||
|     private String authType; | ||||
|  | ||||
|     @Schema(description = "设备位置的纬度,范围 -90.000000 ~ 90.000000") | ||||
|     @ExcelProperty("设备位置的纬度,范围 -90.000000 ~ 90.000000") | ||||
|     // TODO @haohao:经纬度:可能 double 就够啦 | ||||
|     @Schema(description = "设备位置的纬度,范围") | ||||
|     @ExcelProperty("设备位置的纬度") | ||||
|     private BigDecimal latitude; | ||||
|  | ||||
|     @Schema(description = "设备位置的经度,范围 -180.000000 ~ 180.000000") | ||||
|     @ExcelProperty("设备位置的经度,范围 -180.000000 ~ 180.000000") | ||||
|     @Schema(description = "设备位置的经度") | ||||
|     @ExcelProperty("设备位置的经度") | ||||
|     private BigDecimal longitude; | ||||
|  | ||||
|     @Schema(description = "地区编码,符合国家地区编码标准,关联地区表", example = "16995") | ||||
|     @ExcelProperty("地区编码,符合国家地区编码标准,关联地区表") | ||||
|     @Schema(description = "地区编码", example = "16995") | ||||
|     @ExcelProperty("地区编码") | ||||
|     private Integer areaId; | ||||
|  | ||||
|     @Schema(description = "设备详细地址") | ||||
|   | ||||
| @@ -7,16 +7,16 @@ import lombok.Data; | ||||
| @Data | ||||
| public class IotDeviceSaveReqVO { | ||||
|  | ||||
|     @Schema(description = "设备 ID,主键,自增", requiredMode = Schema.RequiredMode.REQUIRED, example = "177") | ||||
|     @Schema(description = "设备编号", example = "177") | ||||
|     private Long id; | ||||
|  | ||||
|     @Schema(description = "设备名称,在产品内唯一,用于标识设备", requiredMode = Schema.RequiredMode.REQUIRED, example = "王五") | ||||
|     @Schema(description = "设备名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "王五") | ||||
|     private String deviceName; | ||||
|  | ||||
|     @Schema(description = "设备备注名称,供用户自定义备注", example = "张三") | ||||
|     @Schema(description = "备注名称", example = "张三") | ||||
|     private String nickname; | ||||
|  | ||||
|     @Schema(description = "产品 ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202") | ||||
|     @Schema(description = "产品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202") | ||||
|     private Long productId; | ||||
|  | ||||
| } | ||||
| @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.iot.dal.dataobject.device; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; | ||||
| import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO; | ||||
| import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStatusEnum; | ||||
| import com.baomidou.mybatisplus.annotation.KeySequence; | ||||
| import com.baomidou.mybatisplus.annotation.TableId; | ||||
| import com.baomidou.mybatisplus.annotation.TableName; | ||||
| @@ -13,7 +14,7 @@ import java.time.LocalDateTime; | ||||
| /** | ||||
|  * IoT 设备 DO | ||||
|  * | ||||
|  * @author 芋道源码 | ||||
|  * @author haohao | ||||
|  */ | ||||
| @TableName("iot_device") | ||||
| @KeySequence("iot_device_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。 | ||||
| @@ -39,33 +40,48 @@ public class IotDeviceDO extends BaseDO { | ||||
|      */ | ||||
|     private String deviceName; | ||||
|     /** | ||||
|      * 产品 ID,关联 iot_product 表的 id | ||||
|      * 设备备注名称 | ||||
|      */ | ||||
|     private String nickname; | ||||
|     /** | ||||
|      * 设备序列号 | ||||
|      */ | ||||
|     private String serialNumber; | ||||
|  | ||||
|     /** | ||||
|      * 产品编号 | ||||
|      * | ||||
|      * 关联 {@link IotProductDO#getId()} | ||||
|      */ | ||||
|     private Long productId; | ||||
|     /** | ||||
|      * 产品 Key,关联 iot_product 表的 product_key | ||||
|      * 关联 {@link IotProductDO#getProductKey()} | ||||
|      * 产品标识 | ||||
|      * | ||||
|      * 冗余 {@link IotProductDO#getProductKey()} | ||||
|      */ | ||||
|     private String productKey; | ||||
|     /** | ||||
|      * 设备类型:0 - 直连设备,1 - 网关子设备,2 - 网关设备 | ||||
|      * 关联 {@link IotProductDO#getDeviceType()} | ||||
|      * 设备类型 | ||||
|      * | ||||
|      * 冗余 {@link IotProductDO#getDeviceType()} | ||||
|      */ | ||||
|     private Integer deviceType; | ||||
|  | ||||
|     /** | ||||
|      * 设备备注名称,供用户自定义备注 | ||||
|      */ | ||||
|     private String nickname; | ||||
|     /** | ||||
|      * 网关设备 ID,子设备需要关联的网关设备 ID | ||||
|      */ | ||||
|     private Long gatewayId; | ||||
|     /** | ||||
|      * 设备状态:0 - 未激活,1 - 在线,2 - 离线,3 - 已禁用 | ||||
|      * 关联 {@link cn.iocoder.yudao.module.iot.enums.device.IotDeviceStatusEnum} | ||||
|      * 设备状态 | ||||
|      * | ||||
|      * 枚举 {@link IotDeviceStatusEnum} | ||||
|      */ | ||||
|     private Integer status; | ||||
|     /** | ||||
|      * 网关设备编号 | ||||
|      * | ||||
|      * 子设备需要关联的网关设备 ID | ||||
|      * | ||||
|      * 关联 {@link IotDeviceDO#getId()} | ||||
|      */ | ||||
|     private Long gatewayId; | ||||
|  | ||||
|     /** | ||||
|      * 设备状态最后更新时间 | ||||
|      */ | ||||
| @@ -82,6 +98,7 @@ public class IotDeviceDO extends BaseDO { | ||||
|      * 设备激活时间 | ||||
|      */ | ||||
|     private LocalDateTime activeTime; | ||||
|  | ||||
|     /** | ||||
|      * 设备的 IP 地址 | ||||
|      */ | ||||
| @@ -90,6 +107,7 @@ public class IotDeviceDO extends BaseDO { | ||||
|      * 设备的固件版本 | ||||
|      */ | ||||
|     private String firmwareVersion; | ||||
|  | ||||
|     /** | ||||
|      * 设备密钥,用于设备认证,需安全存储 | ||||
|      */ | ||||
| @@ -109,26 +127,26 @@ public class IotDeviceDO extends BaseDO { | ||||
|     /** | ||||
|      * 认证类型(如一机一密、动态注册) | ||||
|      */ | ||||
|     // TODO @haohao:是不是要枚举哈 | ||||
|     private String authType; | ||||
|  | ||||
|     /** | ||||
|      * 设备位置的纬度,范围 -90.000000 ~ 90.000000 | ||||
|      * 设备位置的纬度 | ||||
|      */ | ||||
|     private BigDecimal latitude; | ||||
|     /** | ||||
|      * 设备位置的经度,范围 -180.000000 ~ 180.000000 | ||||
|      * 设备位置的经度 | ||||
|      */ | ||||
|     private BigDecimal longitude; | ||||
|     /** | ||||
|      * 地区编码,符合国家地区编码标准,关联地区表 | ||||
|      * 地区编码 | ||||
|      * | ||||
|      * 关联 Area 的 id | ||||
|      */ | ||||
|     private Integer areaId; | ||||
|     /** | ||||
|      * 设备详细地址 | ||||
|      */ | ||||
|     private String address; | ||||
|     /** | ||||
|      * 设备序列号 | ||||
|      */ | ||||
|     private String serialNumber; | ||||
|  | ||||
| } | ||||
| @@ -53,4 +53,5 @@ public interface IotDeviceMapper extends BaseMapperX<IotDeviceDO> { | ||||
|     default long selectCountByGatewayId(Long id) { | ||||
|         return selectCount(IotDeviceDO::getGatewayId, id); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,5 +1,6 @@ | ||||
| package cn.iocoder.yudao.module.iot.service.device; | ||||
|  | ||||
| import cn.hutool.core.util.IdUtil; | ||||
| import cn.hutool.core.util.StrUtil; | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | ||||
| @@ -34,6 +35,7 @@ public class DeviceServiceImpl implements IotDeviceService { | ||||
|  | ||||
|     @Resource | ||||
|     private IotDeviceMapper deviceMapper; | ||||
|     // TODO @haohao:不直接调用 productmapper,通过 productservice;每一个模型,不直接使用对方的 | ||||
|     @Resource | ||||
|     private IotProductMapper productMapper; | ||||
|  | ||||
| @@ -46,40 +48,33 @@ public class DeviceServiceImpl implements IotDeviceService { | ||||
|     @Override | ||||
|     @Transactional(rollbackFor = Exception.class) | ||||
|     public Long createDevice(IotDeviceSaveReqVO createReqVO) { | ||||
|         // 1. 转换 VO 为 DO | ||||
|         IotDeviceDO device = BeanUtils.toBean(createReqVO, IotDeviceDO.class); | ||||
|  | ||||
|         // 2. 根据产品 ID 查询产品信息 | ||||
|         // 1.1 校验产品是否存在 | ||||
|         IotProductDO product = productMapper.selectById(createReqVO.getProductId()); | ||||
|         if (product == null) { | ||||
|             throw exception(PRODUCT_NOT_EXISTS); | ||||
|         } | ||||
|         device.setProductKey(product.getProductKey()); | ||||
|         device.setDeviceType(product.getDeviceType()); | ||||
|  | ||||
|         // 3. DeviceName 可以为空,当为空时,自动生成产品下的唯一标识符作为 DeviceName | ||||
|         if (StrUtil.isBlank(device.getDeviceName())) { | ||||
|             device.setDeviceName(generateUniqueDeviceName(createReqVO.getProductId())); | ||||
|         // 1.2 校验设备名称在同一产品下是否唯一 | ||||
|         if (StrUtil.isBlank(createReqVO.getDeviceName())) { | ||||
|             createReqVO.setDeviceName(generateUniqueDeviceName(product.getProductKey())); | ||||
|         } else { | ||||
|             validateDeviceNameUnique(product.getProductKey(), createReqVO.getDeviceName()); | ||||
|         } | ||||
|  | ||||
|         // 4. 校验设备名称在同一产品下是否唯一 | ||||
|         validateDeviceNameUnique(device.getProductKey(), device.getDeviceName()); | ||||
|  | ||||
|         // 5. 生成并设置必要的字段 | ||||
|         // 2.1 转换 VO 为 DO | ||||
|         IotDeviceDO device = BeanUtils.toBean(createReqVO, IotDeviceDO.class) | ||||
|                 .setProductKey(product.getProductKey()) | ||||
|                 .setDeviceType(product.getDeviceType()); | ||||
|         // 2.2 生成并设置必要的字段 | ||||
|         device.setDeviceKey(generateUniqueDeviceKey()); | ||||
|         device.setDeviceSecret(generateDeviceSecret()); | ||||
|         device.setMqttClientId(generateMqttClientId()); | ||||
|         device.setMqttUsername(generateMqttUsername(device.getDeviceName(), device.getProductKey())); | ||||
|         device.setMqttPassword(generateMqttPassword()); | ||||
|  | ||||
|         // 6. 设置设备状态为未激活 | ||||
|         // 2.3 设置设备状态为未激活 | ||||
|         device.setStatus(IotDeviceStatusEnum.INACTIVE.getStatus()); | ||||
|         device.setStatusLastUpdateTime(LocalDateTime.now()); | ||||
|  | ||||
|         // 7. 插入到数据库 | ||||
|         // 2.4 插入到数据库 | ||||
|         deviceMapper.insert(device); | ||||
|  | ||||
|         // 8. 返回生成的设备 ID | ||||
|         return device.getId(); | ||||
|     } | ||||
|  | ||||
| @@ -111,7 +106,7 @@ public class DeviceServiceImpl implements IotDeviceService { | ||||
|      * @return 生成的 deviceSecret | ||||
|      */ | ||||
|     private String generateDeviceSecret() { | ||||
|         // 32 位随机字符串 | ||||
|         // TODO @haohao:return IdUtil.fastSimpleUUID() | ||||
|         return UUID.randomUUID().toString().replace("-", ""); | ||||
|     } | ||||
|  | ||||
| @@ -141,39 +136,25 @@ public class DeviceServiceImpl implements IotDeviceService { | ||||
|      * @return 生成的 MQTT Password | ||||
|      */ | ||||
|     private String generateMqttPassword() { | ||||
|         // 在实际应用中,建议使用更安全的方法生成 MQTT Password,如加密或哈希 | ||||
|         // TODO @haohao:【后续优化】在实际应用中,建议使用更安全的方法生成 MQTT Password,如加密或哈希 | ||||
|         return UUID.randomUUID().toString(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 生成唯一的 DeviceName | ||||
|      * | ||||
|      * @param productId 产品 ID | ||||
|      * @param productKey 产品标识 | ||||
|      * @return 生成的唯一 DeviceName | ||||
|      */ | ||||
|     private String generateUniqueDeviceName(Long productId) { | ||||
|         // 实现逻辑以在产品下生成唯一的设备名称 | ||||
|         String deviceName; | ||||
|         String productKey = getProductKey(productId); | ||||
|         do { | ||||
|             // 20 位随机字符串 | ||||
|             deviceName = UUID.randomUUID().toString().replace("-", "").substring(0, 20); | ||||
|         } while (deviceMapper.selectByProductKeyAndDeviceName(productKey, deviceName) != null); | ||||
|         return deviceName; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 获取产品 Key | ||||
|      * | ||||
|      * @param productId 产品 ID | ||||
|      * @return 产品 Key | ||||
|      */ | ||||
|     private String getProductKey(Long productId) { | ||||
|         IotProductDO product = productMapper.selectById(productId); | ||||
|         if (product == null) { | ||||
|             throw exception(PRODUCT_NOT_EXISTS); | ||||
|     private String generateUniqueDeviceName(String productKey) { | ||||
|         // TODO @haohao:业务逻辑里,尽量避免 while true。万一 bug = =;虽然这个不会哈。我先改了下 | ||||
|         for (int i = 0; i < Short.MAX_VALUE; i++) { | ||||
|             String deviceName = IdUtil.fastSimpleUUID().substring(0, 20); | ||||
|             if (deviceMapper.selectByProductKeyAndDeviceName(productKey, deviceName) != null) { | ||||
|                 return deviceName; | ||||
|             } | ||||
|         } | ||||
|         return product.getProductKey(); | ||||
|         throw new IllegalArgumentException("生成 DeviceName 失败"); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -183,6 +164,7 @@ public class DeviceServiceImpl implements IotDeviceService { | ||||
|         IotDeviceDO existingDevice = validateDeviceExists(updateReqVO.getId()); | ||||
|  | ||||
|         // 设备名称 和 产品 ID 不能修改 | ||||
|         // TODO @haohao:这种,直接设置为 null 就不会更新了。忽略前端的传参 | ||||
|         if (updateReqVO.getDeviceName() != null && !updateReqVO.getDeviceName().equals(existingDevice.getDeviceName())) { | ||||
|             throw exception(DEVICE_NAME_CANNOT_BE_MODIFIED); | ||||
|         } | ||||
| @@ -200,18 +182,14 @@ public class DeviceServiceImpl implements IotDeviceService { | ||||
|     @Override | ||||
|     @Transactional(rollbackFor = Exception.class) | ||||
|     public void deleteDevice(Long id) { | ||||
|         // 校验存在 | ||||
|         IotDeviceDO iotDeviceDO = validateDeviceExists(id); | ||||
|  | ||||
|         // 如果是网关设备,检查是否有子设备 | ||||
|         if (iotDeviceDO.getGatewayId() != null) { | ||||
|             long childCount = deviceMapper.selectCountByGatewayId(id); | ||||
|             if (childCount > 0) { | ||||
|                 throw exception(DEVICE_HAS_CHILDREN); | ||||
|             } | ||||
|         // 1.1 校验存在 | ||||
|         IotDeviceDO device = validateDeviceExists(id); | ||||
|         // 1.2 如果是网关设备,检查是否有子设备 | ||||
|         if (device.getGatewayId() != null && deviceMapper.selectCountByGatewayId(id) > 0) { | ||||
|             throw exception(DEVICE_HAS_CHILDREN); | ||||
|         } | ||||
|  | ||||
|         // 删除设备 | ||||
|         // 2. 删除设备 | ||||
|         deviceMapper.deleteById(id); | ||||
|     } | ||||
|  | ||||
| @@ -222,11 +200,11 @@ public class DeviceServiceImpl implements IotDeviceService { | ||||
|      * @return 设备对象 | ||||
|      */ | ||||
|     private IotDeviceDO validateDeviceExists(Long id) { | ||||
|         IotDeviceDO iotDeviceDO = deviceMapper.selectById(id); | ||||
|         if (iotDeviceDO == null) { | ||||
|         IotDeviceDO device = deviceMapper.selectById(id); | ||||
|         if (device == null) { | ||||
|             throw exception(DEVICE_NOT_EXISTS); | ||||
|         } | ||||
|         return iotDeviceDO; | ||||
|         return device; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -244,21 +222,20 @@ public class DeviceServiceImpl implements IotDeviceService { | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     @Transactional(rollbackFor = Exception.class) | ||||
|     public void updateDeviceStatus(Long id, Integer status) { | ||||
|         // 校验存在 | ||||
|         validateDeviceExists(id); | ||||
|  | ||||
|         // TODO @haohao:这个可以直接用 swagger 注解哈 | ||||
|         // 校验状态是否合法 | ||||
|         if (!IotDeviceStatusEnum.isValidStatus(status)) { | ||||
|             throw exception(DEVICE_INVALID_DEVICE_STATUS); | ||||
|         } | ||||
|  | ||||
|         // 更新状态和更新时间 | ||||
|         IotDeviceDO updateObj = new IotDeviceDO() | ||||
|                 .setId(id) | ||||
|                 .setStatus(status) | ||||
|         IotDeviceDO updateObj = new IotDeviceDO().setId(id).setStatus(status) | ||||
|                 .setStatusLastUpdateTime(LocalDateTime.now()); | ||||
|         deviceMapper.updateById(updateObj); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -13,7 +13,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| public interface IotDeviceService { | ||||
|  | ||||
|     /** | ||||
|      * 创建IoT 设备 | ||||
|      * 创建设备 | ||||
|      * | ||||
|      * @param createReqVO 创建信息 | ||||
|      * @return 编号 | ||||
| @@ -21,21 +21,21 @@ public interface IotDeviceService { | ||||
|     Long createDevice(@Valid IotDeviceSaveReqVO createReqVO); | ||||
|  | ||||
|     /** | ||||
|      * 更新IoT 设备 | ||||
|      * 更新设备 | ||||
|      * | ||||
|      * @param updateReqVO 更新信息 | ||||
|      */ | ||||
|     void updateDevice(@Valid IotDeviceSaveReqVO updateReqVO); | ||||
|  | ||||
|     /** | ||||
|      * 删除IoT 设备 | ||||
|      * 删除设备 | ||||
|      * | ||||
|      * @param id 编号 | ||||
|      */ | ||||
|     void deleteDevice(Long id); | ||||
|  | ||||
|     /** | ||||
|      * 获得IoT 设备 | ||||
|      * 获得设备 | ||||
|      * | ||||
|      * @param id 编号 | ||||
|      * @return IoT 设备 | ||||
| @@ -43,7 +43,7 @@ public interface IotDeviceService { | ||||
|     IotDeviceDO getDevice(Long id); | ||||
|  | ||||
|     /** | ||||
|      * 获得IoT 设备分页 | ||||
|      * 获得设备分页 | ||||
|      * | ||||
|      * @param pageReqVO 分页查询 | ||||
|      * @return IoT 设备分页 | ||||
| @@ -51,10 +51,11 @@ public interface IotDeviceService { | ||||
|     PageResult<IotDeviceDO> getDevicePage(IotDevicePageReqVO pageReqVO); | ||||
|  | ||||
|     /** | ||||
|      * 更新IoT 设备状态 | ||||
|      * 更新设备状态 | ||||
|      * | ||||
|      * @param id 编号 | ||||
|      * @param status 状态 | ||||
|      */ | ||||
|     void updateDeviceStatus(Long id, Integer status); | ||||
|  | ||||
| } | ||||
| @@ -170,7 +170,6 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe | ||||
|  | ||||
|         // 3.1 使用 diffList 方法比较新旧列表 | ||||
|         List<List<IotThinkModelFunctionDO>> diffResult = diffList(oldFunctionList, newFunctionList, | ||||
|                 // TODO @haohao:是不是用 id 比较相同就 ok 哈。如果可以的化,下面的 update 可以更简单 | ||||
|                 // 继续使用 identifier 和 type 进行比较:这样可以准确地匹配对应的功能对象。 | ||||
|                 (oldFunc, newFunc) -> Objects.equals(oldFunc.getIdentifier(), newFunc.getIdentifier()) | ||||
|                         && Objects.equals(oldFunc.getType(), newFunc.getType())); | ||||
| @@ -191,6 +190,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe | ||||
|                     thinkModelFunctionMapper.updateById(updateFunc); | ||||
|                 } | ||||
|             } | ||||
|             // TODO @haohao:seckillProductMapper.updateBatch(diffList.get(1)); 可以直接类似这么操作哇? | ||||
|         } | ||||
|         if (CollUtil.isNotEmpty(deleteList)) { | ||||
|             Set<Long> idsToDelete = CollectionUtils.convertSet(deleteList, IotThinkModelFunctionDO::getId); | ||||
|   | ||||
| @@ -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.device.IotDeviceMapper"> | ||||
|  | ||||
|     <!-- | ||||
|         一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。 | ||||
|         无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。 | ||||
|         代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。 | ||||
|         文档可见:https://www.iocoder.cn/MyBatis/x-plugins/ | ||||
|      --> | ||||
|  | ||||
| </mapper> | ||||
| @@ -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.thinkmodelfunction.IotThinkModelFunctionMapper"> | ||||
|  | ||||
|     <!-- | ||||
|         一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。 | ||||
|         无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。 | ||||
|         代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。 | ||||
|         文档可见:https://www.iocoder.cn/MyBatis/x-plugins/ | ||||
|      --> | ||||
|  | ||||
| </mapper> | ||||
| @@ -1,219 +0,0 @@ | ||||
| package cn.iocoder.yudao.module.iot.service.device; | ||||
|  | ||||
| import org.junit.jupiter.api.Disabled; | ||||
| import org.junit.jupiter.api.Test; | ||||
|  | ||||
| import jakarta.annotation.Resource; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | ||||
|  | ||||
| import cn.iocoder.yudao.module.iot.controller.admin.device.vo.*; | ||||
| import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; | ||||
| import cn.iocoder.yudao.module.iot.dal.mysql.device.IotDeviceMapper; | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
|  | ||||
| import org.springframework.context.annotation.Import; | ||||
|  | ||||
| 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 org.junit.jupiter.api.Assertions.*; | ||||
|  | ||||
| /** | ||||
|  * {@link DeviceServiceImpl} 的单元测试类 | ||||
|  * | ||||
|  * @author 芋道源码 | ||||
|  */ | ||||
| @Import(DeviceServiceImpl.class) | ||||
| public class DeviceServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
|     @Resource | ||||
|     private DeviceServiceImpl deviceService; | ||||
|  | ||||
|     @Resource | ||||
|     private IotDeviceMapper deviceMapper; | ||||
|  | ||||
|     @Test | ||||
|     public void testCreateDevice_success() { | ||||
|         // 准备参数 | ||||
|         IotDeviceSaveReqVO createReqVO = randomPojo(IotDeviceSaveReqVO.class).setId(null); | ||||
|  | ||||
|         // 调用 | ||||
|         Long deviceId = deviceService.createDevice(createReqVO); | ||||
|         // 断言 | ||||
|         assertNotNull(deviceId); | ||||
|         // 校验记录的属性是否正确 | ||||
|         IotDeviceDO device = deviceMapper.selectById(deviceId); | ||||
|         assertPojoEquals(createReqVO, device, "id"); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testUpdateDevice_success() { | ||||
|         // mock 数据 | ||||
|         IotDeviceDO dbDevice = randomPojo(IotDeviceDO.class); | ||||
|         deviceMapper.insert(dbDevice);// @Sql: 先插入出一条存在的数据 | ||||
|         // 准备参数 | ||||
|         IotDeviceSaveReqVO updateReqVO = randomPojo(IotDeviceSaveReqVO.class, o -> { | ||||
|             o.setId(dbDevice.getId()); // 设置更新的 ID | ||||
|         }); | ||||
|  | ||||
|         // 调用 | ||||
|         deviceService.updateDevice(updateReqVO); | ||||
|         // 校验是否更新正确 | ||||
|         IotDeviceDO device = deviceMapper.selectById(updateReqVO.getId()); // 获取最新的 | ||||
|         assertPojoEquals(updateReqVO, device); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testUpdateDevice_notExists() { | ||||
|         // 准备参数 | ||||
|         IotDeviceSaveReqVO updateReqVO = randomPojo(IotDeviceSaveReqVO.class); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> deviceService.updateDevice(updateReqVO), DEVICE_NOT_EXISTS); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testDeleteDevice_success() { | ||||
|         // mock 数据 | ||||
|         IotDeviceDO dbDevice = randomPojo(IotDeviceDO.class); | ||||
|         deviceMapper.insert(dbDevice);// @Sql: 先插入出一条存在的数据 | ||||
|         // 准备参数 | ||||
|         Long id = dbDevice.getId(); | ||||
|  | ||||
|         // 调用 | ||||
|         deviceService.deleteDevice(id); | ||||
|        // 校验数据不存在了 | ||||
|        assertNull(deviceMapper.selectById(id)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testDeleteDevice_notExists() { | ||||
|         // 准备参数 | ||||
|         Long id = randomLongId(); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> deviceService.deleteDevice(id), DEVICE_NOT_EXISTS); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     @Disabled  // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解 | ||||
|     public void testGetDevicePage() { | ||||
|        // mock 数据 | ||||
|        IotDeviceDO dbDevice = randomPojo(IotDeviceDO.class, o -> { // 等会查询到 | ||||
|            o.setDeviceKey(null); | ||||
|            o.setDeviceName(null); | ||||
|            o.setProductId(null); | ||||
|            o.setProductKey(null); | ||||
|            o.setDeviceType(null); | ||||
|            o.setNickname(null); | ||||
|            o.setGatewayId(null); | ||||
|            o.setStatus(null); | ||||
|            o.setStatusLastUpdateTime(null); | ||||
|            o.setLastOnlineTime(null); | ||||
|            o.setLastOfflineTime(null); | ||||
|            o.setActiveTime(null); | ||||
|            o.setIp(null); | ||||
|            o.setFirmwareVersion(null); | ||||
|            o.setDeviceSecret(null); | ||||
|            o.setMqttClientId(null); | ||||
|            o.setMqttUsername(null); | ||||
|            o.setMqttPassword(null); | ||||
|            o.setAuthType(null); | ||||
|            o.setLatitude(null); | ||||
|            o.setLongitude(null); | ||||
|            o.setAreaId(null); | ||||
|            o.setAddress(null); | ||||
|            o.setSerialNumber(null); | ||||
|            o.setCreateTime(null); | ||||
|        }); | ||||
|        deviceMapper.insert(dbDevice); | ||||
|        // 测试 deviceKey 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setDeviceKey(null))); | ||||
|        // 测试 deviceName 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setDeviceName(null))); | ||||
|        // 测试 productId 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setProductId(null))); | ||||
|        // 测试 productKey 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setProductKey(null))); | ||||
|        // 测试 deviceType 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setDeviceType(null))); | ||||
|        // 测试 nickname 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setNickname(null))); | ||||
|        // 测试 gatewayId 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setGatewayId(null))); | ||||
|        // 测试 status 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setStatus(null))); | ||||
|        // 测试 statusLastUpdateTime 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setStatusLastUpdateTime(null))); | ||||
|        // 测试 lastOnlineTime 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setLastOnlineTime(null))); | ||||
|        // 测试 lastOfflineTime 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setLastOfflineTime(null))); | ||||
|        // 测试 activeTime 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setActiveTime(null))); | ||||
|        // 测试 ip 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setIp(null))); | ||||
|        // 测试 firmwareVersion 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setFirmwareVersion(null))); | ||||
|        // 测试 deviceSecret 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setDeviceSecret(null))); | ||||
|        // 测试 mqttClientId 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setMqttClientId(null))); | ||||
|        // 测试 mqttUsername 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setMqttUsername(null))); | ||||
|        // 测试 mqttPassword 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setMqttPassword(null))); | ||||
|        // 测试 authType 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setAuthType(null))); | ||||
|        // 测试 latitude 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setLatitude(null))); | ||||
|        // 测试 longitude 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setLongitude(null))); | ||||
|        // 测试 areaId 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setAreaId(null))); | ||||
|        // 测试 address 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setAddress(null))); | ||||
|        // 测试 serialNumber 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setSerialNumber(null))); | ||||
|        // 测试 createTime 不匹配 | ||||
|        deviceMapper.insert(cloneIgnoreId(dbDevice, o -> o.setCreateTime(null))); | ||||
|        // 准备参数 | ||||
|        IotDevicePageReqVO reqVO = new IotDevicePageReqVO(); | ||||
|        reqVO.setDeviceKey(null); | ||||
|        reqVO.setDeviceName(null); | ||||
|        reqVO.setProductId(null); | ||||
|        reqVO.setProductKey(null); | ||||
|        reqVO.setDeviceType(null); | ||||
|        reqVO.setNickname(null); | ||||
|        reqVO.setGatewayId(null); | ||||
|        reqVO.setStatus(null); | ||||
|        reqVO.setStatusLastUpdateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28)); | ||||
|        reqVO.setLastOnlineTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28)); | ||||
|        reqVO.setLastOfflineTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28)); | ||||
|        reqVO.setActiveTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28)); | ||||
|        reqVO.setIp(null); | ||||
|        reqVO.setFirmwareVersion(null); | ||||
|        reqVO.setDeviceSecret(null); | ||||
|        reqVO.setMqttClientId(null); | ||||
|        reqVO.setMqttUsername(null); | ||||
|        reqVO.setMqttPassword(null); | ||||
|        reqVO.setAuthType(null); | ||||
|        reqVO.setLatitude(null); | ||||
|        reqVO.setLongitude(null); | ||||
|        reqVO.setAreaId(null); | ||||
|        reqVO.setAddress(null); | ||||
|        reqVO.setSerialNumber(null); | ||||
|        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28)); | ||||
|  | ||||
|        // 调用 | ||||
|        PageResult<IotDeviceDO> pageResult = deviceService.getDevicePage(reqVO); | ||||
|        // 断言 | ||||
|        assertEquals(1, pageResult.getTotal()); | ||||
|        assertEquals(1, pageResult.getList().size()); | ||||
|        assertPojoEquals(dbDevice, pageResult.getList().get(0)); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,71 +0,0 @@ | ||||
| package cn.iocoder.yudao.module.iot.service.thinkmodelfunction; | ||||
|  | ||||
| import org.junit.jupiter.api.Test; | ||||
|  | ||||
| import jakarta.annotation.Resource; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | ||||
|  | ||||
| import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.*; | ||||
| import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO; | ||||
| import cn.iocoder.yudao.module.iot.dal.mysql.thinkmodelfunction.IotThinkModelFunctionMapper; | ||||
|  | ||||
| import org.springframework.context.annotation.Import; | ||||
|  | ||||
| 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 org.junit.jupiter.api.Assertions.*; | ||||
|  | ||||
| /** | ||||
|  * {@link IotThinkModelFunctionServiceImpl} 的单元测试类 | ||||
|  * | ||||
|  * @author 芋道源码 | ||||
|  */ | ||||
| @Import(IotThinkModelFunctionServiceImpl.class) | ||||
| public class IotThinkModelFunctionServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
|     @Resource | ||||
|     private IotThinkModelFunctionServiceImpl thinkModelFunctionService; | ||||
|  | ||||
|     @Resource | ||||
|     private IotThinkModelFunctionMapper thinkModelFunctionMapper; | ||||
|  | ||||
|     @Test | ||||
|     public void testCreateThinkModelFunction_success() { | ||||
|         // 准备参数 | ||||
|         IotThinkModelFunctionSaveReqVO createReqVO = randomPojo(IotThinkModelFunctionSaveReqVO.class); | ||||
|  | ||||
|         // 调用 | ||||
|         Long thinkModelFunctionId = thinkModelFunctionService.createThinkModelFunction(createReqVO); | ||||
|         // 断言 | ||||
|         assertNotNull(thinkModelFunctionId); | ||||
|         // 校验记录的属性是否正确 | ||||
|         IotThinkModelFunctionDO thinkModelFunction = thinkModelFunctionMapper.selectById(thinkModelFunctionId); | ||||
|         assertPojoEquals(createReqVO, thinkModelFunction, "id"); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testDeleteThinkModelFunction_success() { | ||||
|         // mock 数据 | ||||
|         IotThinkModelFunctionDO dbThinkModelFunction = randomPojo(IotThinkModelFunctionDO.class); | ||||
|         thinkModelFunctionMapper.insert(dbThinkModelFunction);// @Sql: 先插入出一条存在的数据 | ||||
|         // 准备参数 | ||||
|         Long id = dbThinkModelFunction.getId(); | ||||
|  | ||||
|         // 调用 | ||||
|         thinkModelFunctionService.deleteThinkModelFunction(id); | ||||
|        // 校验数据不存在了 | ||||
|        assertNull(thinkModelFunctionMapper.selectById(id)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testDeleteThinkModelFunction_notExists() { | ||||
|         // 准备参数 | ||||
|         Long id = randomLongId(); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> thinkModelFunctionService.deleteThinkModelFunction(id), THINK_MODEL_FUNCTION_NOT_EXISTS); | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV