Commit b5ac5261394db19b535fdbe18ee1aac2a892baae

Authored by YunaiV
1 parent b143bc17

【功能新增】IoT:设备管理界面增加设备分组选择功能

Showing 12 changed files with 198 additions and 12 deletions
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java
@@ -35,5 +35,5 @@ public interface ErrorCodeConstants { @@ -35,5 +35,5 @@ public interface ErrorCodeConstants {
35 35
36 // ========== 设备分组 1-050-005-000 ========== 36 // ========== 设备分组 1-050-005-000 ==========
37 ErrorCode DEVICE_GROUP_NOT_EXISTS = new ErrorCode(1_050_005_000, "设备分组不存在"); 37 ErrorCode DEVICE_GROUP_NOT_EXISTS = new ErrorCode(1_050_005_000, "设备分组不存在");
38 - 38 + ErrorCode DEVICE_GROUP_DELETE_FAIL_DEVICE_EXISTS = new ErrorCode(1_050_005_001, "设备分组下存在设备,不允许删除");
39 } 39 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceGroupController.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.controller.admin.device;
  2 +
  3 +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
  4 +import cn.iocoder.yudao.framework.common.pojo.CommonResult;
  5 +import cn.iocoder.yudao.framework.common.pojo.PageResult;
  6 +import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
  7 +import cn.iocoder.yudao.module.iot.controller.admin.device.vo.group.IotDeviceGroupPageReqVO;
  8 +import cn.iocoder.yudao.module.iot.controller.admin.device.vo.group.IotDeviceGroupRespVO;
  9 +import cn.iocoder.yudao.module.iot.controller.admin.device.vo.group.IotDeviceGroupSaveReqVO;
  10 +import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceGroupDO;
  11 +import cn.iocoder.yudao.module.iot.service.device.IotDeviceGroupService;
  12 +import cn.iocoder.yudao.module.iot.service.device.IotDeviceService;
  13 +import io.swagger.v3.oas.annotations.Operation;
  14 +import io.swagger.v3.oas.annotations.Parameter;
  15 +import io.swagger.v3.oas.annotations.tags.Tag;
  16 +import jakarta.annotation.Resource;
  17 +import jakarta.validation.Valid;
  18 +import org.springframework.security.access.prepost.PreAuthorize;
  19 +import org.springframework.validation.annotation.Validated;
  20 +import org.springframework.web.bind.annotation.*;
  21 +
  22 +import java.util.List;
  23 +
  24 +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
  25 +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
  26 +
  27 +@Tag(name = "管理后台 - IoT 设备分组")
  28 +@RestController
  29 +@RequestMapping("/iot/device-group")
  30 +@Validated
  31 +public class IotDeviceGroupController {
  32 +
  33 + @Resource
  34 + private IotDeviceGroupService deviceGroupService;
  35 + @Resource
  36 + private IotDeviceService deviceService;
  37 +
  38 + @PostMapping("/create")
  39 + @Operation(summary = "创建设备分组")
  40 + @PreAuthorize("@ss.hasPermission('iot:device-group:create')")
  41 + public CommonResult<Long> createDeviceGroup(@Valid @RequestBody IotDeviceGroupSaveReqVO createReqVO) {
  42 + return success(deviceGroupService.createDeviceGroup(createReqVO));
  43 + }
  44 +
  45 + @PutMapping("/update")
  46 + @Operation(summary = "更新设备分组")
  47 + @PreAuthorize("@ss.hasPermission('iot:device-group:update')")
  48 + public CommonResult<Boolean> updateDeviceGroup(@Valid @RequestBody IotDeviceGroupSaveReqVO updateReqVO) {
  49 + deviceGroupService.updateDeviceGroup(updateReqVO);
  50 + return success(true);
  51 + }
  52 +
  53 + @DeleteMapping("/delete")
  54 + @Operation(summary = "删除设备分组")
  55 + @Parameter(name = "id", description = "编号", required = true)
  56 + @PreAuthorize("@ss.hasPermission('iot:device-group:delete')")
  57 + public CommonResult<Boolean> deleteDeviceGroup(@RequestParam("id") Long id) {
  58 + deviceGroupService.deleteDeviceGroup(id);
  59 + return success(true);
  60 + }
  61 +
  62 + @GetMapping("/get")
  63 + @Operation(summary = "获得设备分组")
  64 + @Parameter(name = "id", description = "编号", required = true, example = "1024")
  65 + @PreAuthorize("@ss.hasPermission('iot:device-group:query')")
  66 + public CommonResult<IotDeviceGroupRespVO> getDeviceGroup(@RequestParam("id") Long id) {
  67 + IotDeviceGroupDO deviceGroup = deviceGroupService.getDeviceGroup(id);
  68 + return success(BeanUtils.toBean(deviceGroup, IotDeviceGroupRespVO.class));
  69 + }
  70 +
  71 + @GetMapping("/page")
  72 + @Operation(summary = "获得设备分组分页")
  73 + @PreAuthorize("@ss.hasPermission('iot:device-group:query')")
  74 + public CommonResult<PageResult<IotDeviceGroupRespVO>> getDeviceGroupPage(@Valid IotDeviceGroupPageReqVO pageReqVO) {
  75 + PageResult<IotDeviceGroupDO> pageResult = deviceGroupService.getDeviceGroupPage(pageReqVO);
  76 + return success(BeanUtils.toBean(pageResult, IotDeviceGroupRespVO.class,
  77 + group -> group.setDeviceCount(deviceService.getDeviceCountByGroupId(group.getId()))));
  78 + }
  79 +
  80 + @GetMapping("/simple-list")
  81 + @Operation(summary = "获取设备分组的精简信息列表", description = "只包含被开启的分组,主要用于前端的下拉选项")
  82 + public CommonResult<List<IotDeviceGroupRespVO>> getSimpleDeviceGroupList() {
  83 + List<IotDeviceGroupDO> list = deviceGroupService.getDeviceGroupListByStatus(CommonStatusEnum.ENABLE.getStatus());
  84 + return success(convertList(list, group -> // 只返回 id、name 字段
  85 + new IotDeviceGroupRespVO().setId(group.getId()).setName(group.getName())));
  86 + }
  87 +
  88 +}
0 \ No newline at end of file 89 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/vo/device/IotDeviceRespVO.java
@@ -6,6 +6,7 @@ import io.swagger.v3.oas.annotations.media.Schema; @@ -6,6 +6,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
6 import lombok.Data; 6 import lombok.Data;
7 7
8 import java.time.LocalDateTime; 8 import java.time.LocalDateTime;
  9 +import java.util.Set;
9 10
10 @Schema(description = "管理后台 - IoT 设备 Response VO") 11 @Schema(description = "管理后台 - IoT 设备 Response VO")
11 @Data 12 @Data
@@ -23,6 +24,9 @@ public class IotDeviceRespVO { @@ -23,6 +24,9 @@ public class IotDeviceRespVO {
23 @ExcelProperty("设备名称备") 24 @ExcelProperty("设备名称备")
24 private String deviceName; 25 private String deviceName;
25 26
  27 + @Schema(description = "设备分组编号数组", example = "1,2")
  28 + private Set<Long> groupIds;
  29 +
26 @Schema(description = "产品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202") 30 @Schema(description = "产品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202")
27 @ExcelProperty("产品编号") 31 @ExcelProperty("产品编号")
28 private Long productId; 32 private Long productId;
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/vo/device/IotDeviceSaveReqVO.java
@@ -3,6 +3,8 @@ package cn.iocoder.yudao.module.iot.controller.admin.device.vo.device; @@ -3,6 +3,8 @@ package cn.iocoder.yudao.module.iot.controller.admin.device.vo.device;
3 import io.swagger.v3.oas.annotations.media.Schema; 3 import io.swagger.v3.oas.annotations.media.Schema;
4 import lombok.Data; 4 import lombok.Data;
5 5
  6 +import java.util.Set;
  7 +
6 @Schema(description = "管理后台 - IoT 设备新增/修改 Request VO") 8 @Schema(description = "管理后台 - IoT 设备新增/修改 Request VO")
7 @Data 9 @Data
8 public class IotDeviceSaveReqVO { 10 public class IotDeviceSaveReqVO {
@@ -25,6 +27,9 @@ public class IotDeviceSaveReqVO { @@ -25,6 +27,9 @@ public class IotDeviceSaveReqVO {
25 @Schema(description = "设备图片", example = "https://iocoder.cn/1.png") 27 @Schema(description = "设备图片", example = "https://iocoder.cn/1.png")
26 private String picUrl; 28 private String picUrl;
27 29
  30 + @Schema(description = "设备分组编号数组", example = "1,2")
  31 + private Set<Long> groupIds;
  32 +
28 @Schema(description = "产品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202") 33 @Schema(description = "产品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202")
29 private Long productId; 34 private Long productId;
30 35
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/vo/group/IotDeviceGroupRespVO.java
@@ -24,4 +24,7 @@ public class IotDeviceGroupRespVO { @@ -24,4 +24,7 @@ public class IotDeviceGroupRespVO {
24 @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) 24 @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
25 private LocalDateTime createTime; 25 private LocalDateTime createTime;
26 26
  27 + @Schema(description = "设备数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
  28 + private Long deviceCount;
  29 +
27 } 30 }
28 \ No newline at end of file 31 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/device/IotDeviceDO.java
@@ -57,7 +57,7 @@ public class IotDeviceDO extends BaseDO { @@ -57,7 +57,7 @@ public class IotDeviceDO extends BaseDO {
57 /** 57 /**
58 * 设备分组编号集合 58 * 设备分组编号集合
59 * 59 *
60 - * 关联 TODO 芋艿: 60 + * 关联 {@link IotDeviceGroupDO#getId()}
61 */ 61 */
62 @TableField(typeHandler = LongSetTypeHandler.class) 62 @TableField(typeHandler = LongSetTypeHandler.class)
63 private Set<Long> groupIds; 63 private Set<Long> groupIds;
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/device/IotDeviceGroupMapper.java
@@ -7,6 +7,8 @@ import cn.iocoder.yudao.module.iot.controller.admin.device.vo.group.IotDeviceGro @@ -7,6 +7,8 @@ import cn.iocoder.yudao.module.iot.controller.admin.device.vo.group.IotDeviceGro
7 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceGroupDO; 7 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceGroupDO;
8 import org.apache.ibatis.annotations.Mapper; 8 import org.apache.ibatis.annotations.Mapper;
9 9
  10 +import java.util.List;
  11 +
10 /** 12 /**
11 * IoT 设备分组 Mapper 13 * IoT 设备分组 Mapper
12 * 14 *
@@ -22,4 +24,8 @@ public interface IotDeviceGroupMapper extends BaseMapperX&lt;IotDeviceGroupDO&gt; { @@ -22,4 +24,8 @@ public interface IotDeviceGroupMapper extends BaseMapperX&lt;IotDeviceGroupDO&gt; {
22 .orderByDesc(IotDeviceGroupDO::getId)); 24 .orderByDesc(IotDeviceGroupDO::getId));
23 } 25 }
24 26
  27 + default List<IotDeviceGroupDO> selectListByStatus(Integer status) {
  28 + return selectList(IotDeviceGroupDO::getStatus, status);
  29 + }
  30 +
25 } 31 }
26 \ No newline at end of file 32 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/device/IotDeviceMapper.java
@@ -50,4 +50,10 @@ public interface IotDeviceMapper extends BaseMapperX&lt;IotDeviceDO&gt; { @@ -50,4 +50,10 @@ public interface IotDeviceMapper extends BaseMapperX&lt;IotDeviceDO&gt; {
50 return selectList(IotDeviceDO::getDeviceType, deviceType); 50 return selectList(IotDeviceDO::getDeviceType, deviceType);
51 } 51 }
52 52
  53 + default Long selectCountByGroupId(Long groupId) {
  54 + return selectCount(new LambdaQueryWrapperX<IotDeviceDO>()
  55 + .apply("FIND_IN_SET(" + groupId + ",group_ids) > 0")
  56 + .orderByDesc(IotDeviceDO::getId));
  57 + }
  58 +
53 } 59 }
54 \ No newline at end of file 60 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceGroupService.java
1 package cn.iocoder.yudao.module.iot.service.device; 1 package cn.iocoder.yudao.module.iot.service.device;
2 2
  3 +import cn.hutool.core.collection.CollUtil;
3 import cn.iocoder.yudao.framework.common.pojo.PageResult; 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.group.IotDeviceGroupPageReqVO; 5 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.group.IotDeviceGroupPageReqVO;
5 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.group.IotDeviceGroupSaveReqVO; 6 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.group.IotDeviceGroupSaveReqVO;
6 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceGroupDO; 7 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceGroupDO;
7 import jakarta.validation.Valid; 8 import jakarta.validation.Valid;
8 9
  10 +import java.util.Collection;
  11 +import java.util.List;
  12 +
  13 +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
  14 +
9 /** 15 /**
10 * IoT 设备分组 Service 接口 16 * IoT 设备分组 Service 接口
11 * 17 *
@@ -14,7 +20,7 @@ import jakarta.validation.Valid; @@ -14,7 +20,7 @@ import jakarta.validation.Valid;
14 public interface IotDeviceGroupService { 20 public interface IotDeviceGroupService {
15 21
16 /** 22 /**
17 - * 创建IoT 设备分组 23 + * 创建设备分组
18 * 24 *
19 * @param createReqVO 创建信息 25 * @param createReqVO 创建信息
20 * @return 编号 26 * @return 编号
@@ -22,33 +28,61 @@ public interface IotDeviceGroupService { @@ -22,33 +28,61 @@ public interface IotDeviceGroupService {
22 Long createDeviceGroup(@Valid IotDeviceGroupSaveReqVO createReqVO); 28 Long createDeviceGroup(@Valid IotDeviceGroupSaveReqVO createReqVO);
23 29
24 /** 30 /**
25 - * 更新IoT 设备分组 31 + * 更新设备分组
26 * 32 *
27 * @param updateReqVO 更新信息 33 * @param updateReqVO 更新信息
28 */ 34 */
29 void updateDeviceGroup(@Valid IotDeviceGroupSaveReqVO updateReqVO); 35 void updateDeviceGroup(@Valid IotDeviceGroupSaveReqVO updateReqVO);
30 36
31 /** 37 /**
32 - * 删除IoT 设备分组 38 + * 删除设备分组
33 * 39 *
34 * @param id 编号 40 * @param id 编号
35 */ 41 */
36 void deleteDeviceGroup(Long id); 42 void deleteDeviceGroup(Long id);
37 43
38 /** 44 /**
39 - * 获得IoT 设备分组 45 + * 校验设备分组是否存在
  46 + *
  47 + * @param ids 设备分组 ID 数组
  48 + */
  49 + default List<IotDeviceGroupDO> validateDeviceGroupExists(Collection<Long> ids) {
  50 + if (CollUtil.isEmpty(ids)) {
  51 + return List.of();
  52 + }
  53 + return convertList(ids, this::validateDeviceGroupExists);
  54 + }
  55 +
  56 + /**
  57 + * 校验设备分组是否存在
  58 + *
  59 + * @param id 设备分组 ID
  60 + * @return 设备分组
  61 + */
  62 + IotDeviceGroupDO validateDeviceGroupExists(Long id);
  63 +
  64 + /**
  65 + * 获得设备分组
40 * 66 *
41 * @param id 编号 67 * @param id 编号
42 - * @return IoT 设备分组 68 + * @return 设备分组
43 */ 69 */
44 IotDeviceGroupDO getDeviceGroup(Long id); 70 IotDeviceGroupDO getDeviceGroup(Long id);
45 71
46 /** 72 /**
47 - * 获得IoT 设备分组分页 73 + * 获得设备分组分页
48 * 74 *
49 * @param pageReqVO 分页查询 75 * @param pageReqVO 分页查询
50 - * @return IoT 设备分组分页 76 + * @return 设备分组分页
51 */ 77 */
52 PageResult<IotDeviceGroupDO> getDeviceGroupPage(IotDeviceGroupPageReqVO pageReqVO); 78 PageResult<IotDeviceGroupDO> getDeviceGroupPage(IotDeviceGroupPageReqVO pageReqVO);
53 79
  80 + /**
  81 + * 获得设备分组列表
  82 + *
  83 + * @param status 状态
  84 + * @return 设备分组列表
  85 + */
  86 + List<IotDeviceGroupDO> getDeviceGroupListByStatus(Integer status);
  87 +
54 } 88 }
55 \ No newline at end of file 89 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceGroupServiceImpl.java
@@ -10,7 +10,10 @@ import jakarta.annotation.Resource; @@ -10,7 +10,10 @@ import jakarta.annotation.Resource;
10 import org.springframework.stereotype.Service; 10 import org.springframework.stereotype.Service;
11 import org.springframework.validation.annotation.Validated; 11 import org.springframework.validation.annotation.Validated;
12 12
  13 +import java.util.List;
  14 +
13 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; 15 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  16 +import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_GROUP_DELETE_FAIL_DEVICE_EXISTS;
14 import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_GROUP_NOT_EXISTS; 17 import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_GROUP_NOT_EXISTS;
15 18
16 /** 19 /**
@@ -25,6 +28,9 @@ public class IotDeviceGroupServiceImpl implements IotDeviceGroupService { @@ -25,6 +28,9 @@ public class IotDeviceGroupServiceImpl implements IotDeviceGroupService {
25 @Resource 28 @Resource
26 private IotDeviceGroupMapper deviceGroupMapper; 29 private IotDeviceGroupMapper deviceGroupMapper;
27 30
  31 + @Resource
  32 + private IotDeviceService deviceService;
  33 +
28 @Override 34 @Override
29 public Long createDeviceGroup(IotDeviceGroupSaveReqVO createReqVO) { 35 public Long createDeviceGroup(IotDeviceGroupSaveReqVO createReqVO) {
30 // 插入 36 // 插入
@@ -45,16 +51,24 @@ public class IotDeviceGroupServiceImpl implements IotDeviceGroupService { @@ -45,16 +51,24 @@ public class IotDeviceGroupServiceImpl implements IotDeviceGroupService {
45 51
46 @Override 52 @Override
47 public void deleteDeviceGroup(Long id) { 53 public void deleteDeviceGroup(Long id) {
48 - // 校验存在 54 + // 1.1 校验存在
49 validateDeviceGroupExists(id); 55 validateDeviceGroupExists(id);
  56 + // 1.2 校验是否存在设备
  57 + if (deviceService.getDeviceCountByGroupId(id) > 0) {
  58 + throw exception(DEVICE_GROUP_DELETE_FAIL_DEVICE_EXISTS);
  59 + }
  60 +
50 // 删除 61 // 删除
51 deviceGroupMapper.deleteById(id); 62 deviceGroupMapper.deleteById(id);
52 } 63 }
53 64
54 - private void validateDeviceGroupExists(Long id) {  
55 - if (deviceGroupMapper.selectById(id) == null) { 65 + @Override
  66 + public IotDeviceGroupDO validateDeviceGroupExists(Long id) {
  67 + IotDeviceGroupDO group = deviceGroupMapper.selectById(id);
  68 + if (group == null) {
56 throw exception(DEVICE_GROUP_NOT_EXISTS); 69 throw exception(DEVICE_GROUP_NOT_EXISTS);
57 } 70 }
  71 + return group;
58 } 72 }
59 73
60 @Override 74 @Override
@@ -67,4 +81,9 @@ public class IotDeviceGroupServiceImpl implements IotDeviceGroupService { @@ -67,4 +81,9 @@ public class IotDeviceGroupServiceImpl implements IotDeviceGroupService {
67 return deviceGroupMapper.selectPage(pageReqVO); 81 return deviceGroupMapper.selectPage(pageReqVO);
68 } 82 }
69 83
  84 + @Override
  85 + public List<IotDeviceGroupDO> getDeviceGroupListByStatus(Integer status) {
  86 + return deviceGroupMapper.selectListByStatus(status);
  87 + }
  88 +
70 } 89 }
71 \ No newline at end of file 90 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceService.java
@@ -79,6 +79,14 @@ public interface IotDeviceService { @@ -79,6 +79,14 @@ public interface IotDeviceService {
79 Long getDeviceCountByProductId(Long productId); 79 Long getDeviceCountByProductId(Long productId);
80 80
81 /** 81 /**
  82 + * 获得设备数量
  83 + *
  84 + * @param groupId 分组编号
  85 + * @return 设备数量
  86 + */
  87 + Long getDeviceCountByGroupId(Long groupId);
  88 +
  89 + /**
82 * 根据产品 key 和设备名称,获得设备信息 90 * 根据产品 key 和设备名称,获得设备信息
83 * 91 *
84 * @param productKey 产品 key 92 * @param productKey 产品 key
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java
@@ -16,6 +16,7 @@ import cn.iocoder.yudao.module.iot.enums.product.IotProductDeviceTypeEnum; @@ -16,6 +16,7 @@ import cn.iocoder.yudao.module.iot.enums.product.IotProductDeviceTypeEnum;
16 import cn.iocoder.yudao.module.iot.service.product.IotProductService; 16 import cn.iocoder.yudao.module.iot.service.product.IotProductService;
17 import jakarta.annotation.Resource; 17 import jakarta.annotation.Resource;
18 import lombok.extern.slf4j.Slf4j; 18 import lombok.extern.slf4j.Slf4j;
  19 +import org.springframework.context.annotation.Lazy;
19 import org.springframework.stereotype.Service; 20 import org.springframework.stereotype.Service;
20 import org.springframework.validation.annotation.Validated; 21 import org.springframework.validation.annotation.Validated;
21 22
@@ -42,6 +43,9 @@ public class IotDeviceServiceImpl implements IotDeviceService { @@ -42,6 +43,9 @@ public class IotDeviceServiceImpl implements IotDeviceService {
42 43
43 @Resource 44 @Resource
44 private IotProductService productService; 45 private IotProductService productService;
  46 + @Resource
  47 + @Lazy // 延迟加载,解决循环依赖
  48 + private IotDeviceGroupService deviceGroupService;
45 49
46 @Override 50 @Override
47 public Long createDevice(IotDeviceSaveReqVO createReqVO) { 51 public Long createDevice(IotDeviceSaveReqVO createReqVO) {
@@ -63,6 +67,8 @@ public class IotDeviceServiceImpl implements IotDeviceService { @@ -63,6 +67,8 @@ public class IotDeviceServiceImpl implements IotDeviceService {
63 && createReqVO.getGatewayId() != null) { 67 && createReqVO.getGatewayId() != null) {
64 validateGatewayDeviceExists(createReqVO.getGatewayId()); 68 validateGatewayDeviceExists(createReqVO.getGatewayId());
65 } 69 }
  70 + // 1.5 校验分组存在
  71 + deviceGroupService.validateDeviceGroupExists(createReqVO.getGroupIds());
66 72
67 // 2.1 转换 VO 为 DO 73 // 2.1 转换 VO 为 DO
68 IotDeviceDO device = BeanUtils.toBean(createReqVO, IotDeviceDO.class, o -> { 74 IotDeviceDO device = BeanUtils.toBean(createReqVO, IotDeviceDO.class, o -> {
@@ -90,6 +96,8 @@ public class IotDeviceServiceImpl implements IotDeviceService { @@ -90,6 +96,8 @@ public class IotDeviceServiceImpl implements IotDeviceService {
90 && updateReqVO.getGatewayId() != null) { 96 && updateReqVO.getGatewayId() != null) {
91 validateGatewayDeviceExists(updateReqVO.getGatewayId()); 97 validateGatewayDeviceExists(updateReqVO.getGatewayId());
92 } 98 }
  99 + // 1.3 校验分组存在
  100 + deviceGroupService.validateDeviceGroupExists(updateReqVO.getGroupIds());
93 101
94 // 2. 更新到数据库 102 // 2. 更新到数据库
95 IotDeviceDO updateObj = BeanUtils.toBean(updateReqVO, IotDeviceDO.class); 103 IotDeviceDO updateObj = BeanUtils.toBean(updateReqVO, IotDeviceDO.class);
@@ -183,6 +191,11 @@ public class IotDeviceServiceImpl implements IotDeviceService { @@ -183,6 +191,11 @@ public class IotDeviceServiceImpl implements IotDeviceService {
183 } 191 }
184 192
185 @Override 193 @Override
  194 + public Long getDeviceCountByGroupId(Long groupId) {
  195 + return deviceMapper.selectCountByGroupId(groupId);
  196 + }
  197 +
  198 + @Override
186 @TenantIgnore 199 @TenantIgnore
187 public IotDeviceDO getDeviceByProductKeyAndDeviceName(String productKey, String deviceName) { 200 public IotDeviceDO getDeviceByProductKeyAndDeviceName(String productKey, String deviceName) {
188 return deviceMapper.selectByProductKeyAndDeviceName(productKey, deviceName); 201 return deviceMapper.selectByProductKeyAndDeviceName(productKey, deviceName);