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 35  
36 36 // ========== 设备分组 1-050-005-000 ==========
37 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 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 import lombok.Data;
7 7  
8 8 import java.time.LocalDateTime;
  9 +import java.util.Set;
9 10  
10 11 @Schema(description = "管理后台 - IoT 设备 Response VO")
11 12 @Data
... ... @@ -23,6 +24,9 @@ public class IotDeviceRespVO {
23 24 @ExcelProperty("设备名称备")
24 25 private String deviceName;
25 26  
  27 + @Schema(description = "设备分组编号数组", example = "1,2")
  28 + private Set<Long> groupIds;
  29 +
26 30 @Schema(description = "产品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202")
27 31 @ExcelProperty("产品编号")
28 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 3 import io.swagger.v3.oas.annotations.media.Schema;
4 4 import lombok.Data;
5 5  
  6 +import java.util.Set;
  7 +
6 8 @Schema(description = "管理后台 - IoT 设备新增/修改 Request VO")
7 9 @Data
8 10 public class IotDeviceSaveReqVO {
... ... @@ -25,6 +27,9 @@ public class IotDeviceSaveReqVO {
25 27 @Schema(description = "设备图片", example = "https://iocoder.cn/1.png")
26 28 private String picUrl;
27 29  
  30 + @Schema(description = "设备分组编号数组", example = "1,2")
  31 + private Set<Long> groupIds;
  32 +
28 33 @Schema(description = "产品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "26202")
29 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 24 @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
25 25 private LocalDateTime createTime;
26 26  
  27 + @Schema(description = "设备数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
  28 + private Long deviceCount;
  29 +
27 30 }
28 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 57 /**
58 58 * 设备分组编号集合
59 59 *
60   - * 关联 TODO 芋艿:
  60 + * 关联 {@link IotDeviceGroupDO#getId()}
61 61 */
62 62 @TableField(typeHandler = LongSetTypeHandler.class)
63 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 7 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceGroupDO;
8 8 import org.apache.ibatis.annotations.Mapper;
9 9  
  10 +import java.util.List;
  11 +
10 12 /**
11 13 * IoT 设备分组 Mapper
12 14 *
... ... @@ -22,4 +24,8 @@ public interface IotDeviceGroupMapper extends BaseMapperX&lt;IotDeviceGroupDO&gt; {
22 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 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 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 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 1 package cn.iocoder.yudao.module.iot.service.device;
2 2  
  3 +import cn.hutool.core.collection.CollUtil;
3 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 5 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.group.IotDeviceGroupPageReqVO;
5 6 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.group.IotDeviceGroupSaveReqVO;
6 7 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceGroupDO;
7 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 16 * IoT 设备分组 Service 接口
11 17 *
... ... @@ -14,7 +20,7 @@ import jakarta.validation.Valid;
14 20 public interface IotDeviceGroupService {
15 21  
16 22 /**
17   - * 创建IoT 设备分组
  23 + * 创建设备分组
18 24 *
19 25 * @param createReqVO 创建信息
20 26 * @return 编号
... ... @@ -22,33 +28,61 @@ public interface IotDeviceGroupService {
22 28 Long createDeviceGroup(@Valid IotDeviceGroupSaveReqVO createReqVO);
23 29  
24 30 /**
25   - * 更新IoT 设备分组
  31 + * 更新设备分组
26 32 *
27 33 * @param updateReqVO 更新信息
28 34 */
29 35 void updateDeviceGroup(@Valid IotDeviceGroupSaveReqVO updateReqVO);
30 36  
31 37 /**
32   - * 删除IoT 设备分组
  38 + * 删除设备分组
33 39 *
34 40 * @param id 编号
35 41 */
36 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 67 * @param id 编号
42   - * @return IoT 设备分组
  68 + * @return 设备分组
43 69 */
44 70 IotDeviceGroupDO getDeviceGroup(Long id);
45 71  
46 72 /**
47   - * 获得IoT 设备分组分页
  73 + * 获得设备分组分页
48 74 *
49 75 * @param pageReqVO 分页查询
50   - * @return IoT 设备分组分页
  76 + * @return 设备分组分页
51 77 */
52 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 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 10 import org.springframework.stereotype.Service;
11 11 import org.springframework.validation.annotation.Validated;
12 12  
  13 +import java.util.List;
  14 +
13 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 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 28 @Resource
26 29 private IotDeviceGroupMapper deviceGroupMapper;
27 30  
  31 + @Resource
  32 + private IotDeviceService deviceService;
  33 +
28 34 @Override
29 35 public Long createDeviceGroup(IotDeviceGroupSaveReqVO createReqVO) {
30 36 // 插入
... ... @@ -45,16 +51,24 @@ public class IotDeviceGroupServiceImpl implements IotDeviceGroupService {
45 51  
46 52 @Override
47 53 public void deleteDeviceGroup(Long id) {
48   - // 校验存在
  54 + // 1.1 校验存在
49 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 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 69 throw exception(DEVICE_GROUP_NOT_EXISTS);
57 70 }
  71 + return group;
58 72 }
59 73  
60 74 @Override
... ... @@ -67,4 +81,9 @@ public class IotDeviceGroupServiceImpl implements IotDeviceGroupService {
67 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 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 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 90 * 根据产品 key 和设备名称,获得设备信息
83 91 *
84 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 16 import cn.iocoder.yudao.module.iot.service.product.IotProductService;
17 17 import jakarta.annotation.Resource;
18 18 import lombok.extern.slf4j.Slf4j;
  19 +import org.springframework.context.annotation.Lazy;
19 20 import org.springframework.stereotype.Service;
20 21 import org.springframework.validation.annotation.Validated;
21 22  
... ... @@ -42,6 +43,9 @@ public class IotDeviceServiceImpl implements IotDeviceService {
42 43  
43 44 @Resource
44 45 private IotProductService productService;
  46 + @Resource
  47 + @Lazy // 延迟加载,解决循环依赖
  48 + private IotDeviceGroupService deviceGroupService;
45 49  
46 50 @Override
47 51 public Long createDevice(IotDeviceSaveReqVO createReqVO) {
... ... @@ -63,6 +67,8 @@ public class IotDeviceServiceImpl implements IotDeviceService {
63 67 && createReqVO.getGatewayId() != null) {
64 68 validateGatewayDeviceExists(createReqVO.getGatewayId());
65 69 }
  70 + // 1.5 校验分组存在
  71 + deviceGroupService.validateDeviceGroupExists(createReqVO.getGroupIds());
66 72  
67 73 // 2.1 转换 VO 为 DO
68 74 IotDeviceDO device = BeanUtils.toBean(createReqVO, IotDeviceDO.class, o -> {
... ... @@ -90,6 +96,8 @@ public class IotDeviceServiceImpl implements IotDeviceService {
90 96 && updateReqVO.getGatewayId() != null) {
91 97 validateGatewayDeviceExists(updateReqVO.getGatewayId());
92 98 }
  99 + // 1.3 校验分组存在
  100 + deviceGroupService.validateDeviceGroupExists(updateReqVO.getGroupIds());
93 101  
94 102 // 2. 更新到数据库
95 103 IotDeviceDO updateObj = BeanUtils.toBean(updateReqVO, IotDeviceDO.class);
... ... @@ -183,6 +191,11 @@ public class IotDeviceServiceImpl implements IotDeviceService {
183 191 }
184 192  
185 193 @Override
  194 + public Long getDeviceCountByGroupId(Long groupId) {
  195 + return deviceMapper.selectCountByGroupId(groupId);
  196 + }
  197 +
  198 + @Override
186 199 @TenantIgnore
187 200 public IotDeviceDO getDeviceByProductKeyAndDeviceName(String productKey, String deviceName) {
188 201 return deviceMapper.selectByProductKeyAndDeviceName(productKey, deviceName);
... ...