Commit eff3afe918b64fd84ccb7e1b1b9787d53c86daf3
1 parent
f163e3e5
用户设备tree
Showing
7 changed files
with
227 additions
and
6 deletions
urbanops-module-garden/src/main/java/com/zteits/urbanops/module/garden/controller/admin/deviceuser/DeviceUserController.java
| @@ -102,4 +102,14 @@ public class DeviceUserController { | @@ -102,4 +102,14 @@ public class DeviceUserController { | ||
| 102 | BeanUtils.toBean(list, DeviceUserRespVO.class)); | 102 | BeanUtils.toBean(list, DeviceUserRespVO.class)); |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | + @PostMapping("/findDeviceDeptTree") | ||
| 106 | + @Operation(summary = "获取IOT设备tree") | ||
| 107 | + //@PreAuthorize("@ss.hasPermission('garden:device-user:query')") | ||
| 108 | + public CommonResult<DeviceDeptTreeRespVO> findDeviceDeptTree(@Valid @RequestBody DeviceDeptTreeReqVO reqVO) { | ||
| 109 | + DeviceDeptTreeRespVO deviceUser = deviceUserService.findDeviceDeptTree(reqVO); | ||
| 110 | + return success(deviceUser); | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + | ||
| 114 | + | ||
| 105 | } | 115 | } |
| 106 | \ No newline at end of file | 116 | \ No newline at end of file |
urbanops-module-garden/src/main/java/com/zteits/urbanops/module/garden/controller/admin/deviceuser/vo/DeviceDeptTreeReqVO.java
0 → 100644
| 1 | +package com.zteits.urbanops.module.garden.controller.admin.deviceuser.vo; | ||
| 2 | + | ||
| 3 | +import io.swagger.v3.oas.annotations.media.Schema; | ||
| 4 | +import lombok.Data; | ||
| 5 | + | ||
| 6 | +@Schema(description = "管理后台 - IOT设备") | ||
| 7 | +@Data | ||
| 8 | +public class DeviceDeptTreeReqVO { | ||
| 9 | + | ||
| 10 | + /** | ||
| 11 | + * 设备编码 精确查询 | ||
| 12 | + */ | ||
| 13 | + private String deviceCode; | ||
| 14 | + | ||
| 15 | + /** | ||
| 16 | + * 设备名称 模糊查询 | ||
| 17 | + */ | ||
| 18 | + private String deviceName; | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * 设备归属部门名称 模糊查询 | ||
| 22 | + */ | ||
| 23 | + private String deviceDeptName; | ||
| 24 | +} |
urbanops-module-garden/src/main/java/com/zteits/urbanops/module/garden/controller/admin/deviceuser/vo/DeviceDeptTreeRespVO.java
0 → 100644
| 1 | +package com.zteits.urbanops.module.garden.controller.admin.deviceuser.vo; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +import lombok.Data; | ||
| 5 | +import java.util.Date; | ||
| 6 | +import java.util.List; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * 设备部门树响应数据 | ||
| 10 | + * | ||
| 11 | + * @author your-name | ||
| 12 | + * @since 2023-xx-xx | ||
| 13 | + */ | ||
| 14 | +@Data | ||
| 15 | +public class DeviceDeptTreeRespVO { | ||
| 16 | + | ||
| 17 | + /** | ||
| 18 | + * 编码 | ||
| 19 | + */ | ||
| 20 | + private String code; | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * 名称 | ||
| 24 | + */ | ||
| 25 | + private String name; | ||
| 26 | + /** | ||
| 27 | + * 父编码 | ||
| 28 | + */ | ||
| 29 | + private String parentCode; | ||
| 30 | + /** | ||
| 31 | + * 显示顺序 | ||
| 32 | + */ | ||
| 33 | + private Integer orderNum; | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * 部门状态(0正常 1停用) | ||
| 37 | + */ | ||
| 38 | + private String status; | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * 设备数量 | ||
| 42 | + */ | ||
| 43 | + private Integer deviceCount; | ||
| 44 | + | ||
| 45 | + /** | ||
| 46 | + * 创建时间 | ||
| 47 | + */ | ||
| 48 | + private Date createTime; | ||
| 49 | + /** | ||
| 50 | + * 节点类型:1部门 3设备 | ||
| 51 | + */ | ||
| 52 | + private Integer nodeType; | ||
| 53 | + /** | ||
| 54 | + * 子节点,和当前一样的结构 | ||
| 55 | + */ | ||
| 56 | + private List<DeviceDeptTreeRespVO> children; | ||
| 57 | +} |
urbanops-module-garden/src/main/java/com/zteits/urbanops/module/garden/service/deviceuser/DeviceUserService.java
| @@ -7,6 +7,7 @@ import com.zteits.urbanops.module.garden.dal.dataobject.deviceuser.DeviceUserDO; | @@ -7,6 +7,7 @@ import com.zteits.urbanops.module.garden.dal.dataobject.deviceuser.DeviceUserDO; | ||
| 7 | import com.zteits.urbanops.framework.common.pojo.PageResult; | 7 | import com.zteits.urbanops.framework.common.pojo.PageResult; |
| 8 | import com.zteits.urbanops.framework.common.pojo.PageParam; | 8 | import com.zteits.urbanops.framework.common.pojo.PageParam; |
| 9 | import jakarta.validation.constraints.NotNull; | 9 | import jakarta.validation.constraints.NotNull; |
| 10 | +import org.springframework.web.bind.annotation.RequestBody; | ||
| 10 | import org.springframework.web.bind.annotation.RequestParam; | 11 | import org.springframework.web.bind.annotation.RequestParam; |
| 11 | 12 | ||
| 12 | /** | 13 | /** |
| @@ -69,4 +70,12 @@ public interface DeviceUserService { | @@ -69,4 +70,12 @@ public interface DeviceUserService { | ||
| 69 | */ | 70 | */ |
| 70 | Integer deleteDeviceUser(Long userId, String deviceNo); | 71 | Integer deleteDeviceUser(Long userId, String deviceNo); |
| 71 | 72 | ||
| 73 | + /** | ||
| 74 | + * 获得设备用户关系树-从iot | ||
| 75 | + * | ||
| 76 | + * @param reqVO 树查询 | ||
| 77 | + * @return 设备用户关系树 | ||
| 78 | + */ | ||
| 79 | + DeviceDeptTreeRespVO findDeviceDeptTree(DeviceDeptTreeReqVO reqVO); | ||
| 80 | + | ||
| 72 | } | 81 | } |
| 73 | \ No newline at end of file | 82 | \ No newline at end of file |
urbanops-module-garden/src/main/java/com/zteits/urbanops/module/garden/service/deviceuser/DeviceUserServiceImpl.java
| 1 | package com.zteits.urbanops.module.garden.service.deviceuser; | 1 | package com.zteits.urbanops.module.garden.service.deviceuser; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.collection.CollUtil; | 3 | +import cn.hutool.core.collection.CollectionUtil; |
| 4 | +import com.alibaba.fastjson.JSONArray; | ||
| 5 | +import com.alibaba.fastjson.JSONObject; | ||
| 4 | import com.zteits.urbanops.framework.security.core.util.SecurityFrameworkUtils; | 6 | import com.zteits.urbanops.framework.security.core.util.SecurityFrameworkUtils; |
| 7 | +import com.zteits.urbanops.module.garden.util.DeviceDeptTreeUtils; | ||
| 8 | +import com.zteits.urbanops.module.garden.util.http.OkHttpClientUtil; | ||
| 9 | +import org.springframework.beans.factory.annotation.Value; | ||
| 5 | import org.springframework.stereotype.Service; | 10 | import org.springframework.stereotype.Service; |
| 6 | import jakarta.annotation.Resource; | 11 | import jakarta.annotation.Resource; |
| 7 | import org.springframework.validation.annotation.Validated; | 12 | import org.springframework.validation.annotation.Validated; |
| @@ -9,18 +14,15 @@ import org.springframework.transaction.annotation.Transactional; | @@ -9,18 +14,15 @@ import org.springframework.transaction.annotation.Transactional; | ||
| 9 | 14 | ||
| 10 | import java.time.LocalDateTime; | 15 | import java.time.LocalDateTime; |
| 11 | import java.util.*; | 16 | import java.util.*; |
| 17 | +import java.util.stream.Collectors; | ||
| 18 | + | ||
| 12 | import com.zteits.urbanops.module.garden.controller.admin.deviceuser.vo.*; | 19 | import com.zteits.urbanops.module.garden.controller.admin.deviceuser.vo.*; |
| 13 | import com.zteits.urbanops.module.garden.dal.dataobject.deviceuser.DeviceUserDO; | 20 | import com.zteits.urbanops.module.garden.dal.dataobject.deviceuser.DeviceUserDO; |
| 14 | import com.zteits.urbanops.framework.common.pojo.PageResult; | 21 | import com.zteits.urbanops.framework.common.pojo.PageResult; |
| 15 | -import com.zteits.urbanops.framework.common.pojo.PageParam; | ||
| 16 | import com.zteits.urbanops.framework.common.util.object.BeanUtils; | 22 | import com.zteits.urbanops.framework.common.util.object.BeanUtils; |
| 17 | 23 | ||
| 18 | import com.zteits.urbanops.module.garden.dal.mysql.deviceuser.DeviceUserMapper; | 24 | import com.zteits.urbanops.module.garden.dal.mysql.deviceuser.DeviceUserMapper; |
| 19 | 25 | ||
| 20 | -import static com.zteits.urbanops.framework.common.exception.util.ServiceExceptionUtil.exception; | ||
| 21 | -import static com.zteits.urbanops.framework.common.util.collection.CollectionUtils.convertList; | ||
| 22 | -import static com.zteits.urbanops.framework.common.util.collection.CollectionUtils.diffList; | ||
| 23 | -import static com.zteits.urbanops.module.garden.enums.ErrorCodeConstants.*; | ||
| 24 | 26 | ||
| 25 | /** | 27 | /** |
| 26 | * 设备用户关系 Service 实现类 | 28 | * 设备用户关系 Service 实现类 |
| @@ -31,6 +33,9 @@ import static com.zteits.urbanops.module.garden.enums.ErrorCodeConstants.*; | @@ -31,6 +33,9 @@ import static com.zteits.urbanops.module.garden.enums.ErrorCodeConstants.*; | ||
| 31 | @Validated | 33 | @Validated |
| 32 | public class DeviceUserServiceImpl implements DeviceUserService { | 34 | public class DeviceUserServiceImpl implements DeviceUserService { |
| 33 | 35 | ||
| 36 | + @Value("${urbanops.iot.device.host}") | ||
| 37 | + private String iotHost; | ||
| 38 | + | ||
| 34 | @Resource | 39 | @Resource |
| 35 | private DeviceUserMapper deviceUserMapper; | 40 | private DeviceUserMapper deviceUserMapper; |
| 36 | 41 | ||
| @@ -106,4 +111,31 @@ public class DeviceUserServiceImpl implements DeviceUserService { | @@ -106,4 +111,31 @@ public class DeviceUserServiceImpl implements DeviceUserService { | ||
| 106 | return deviceUserMapper.deleteDeviceUser(userId, deviceNo); | 111 | return deviceUserMapper.deleteDeviceUser(userId, deviceNo); |
| 107 | } | 112 | } |
| 108 | 113 | ||
| 114 | + @Override | ||
| 115 | + public DeviceDeptTreeRespVO findDeviceDeptTree(DeviceDeptTreeReqVO reqVO) { | ||
| 116 | + Map<String, String> params = new HashMap<>(); | ||
| 117 | + params.put("deviceCode", reqVO.getDeviceCode()); | ||
| 118 | + params.put("deviceName", reqVO.getDeviceName()); | ||
| 119 | + params.put("deviceDeptName", reqVO.getDeviceDeptName()); | ||
| 120 | + String json = OkHttpClientUtil.doPostJson(iotHost + "/deviceApi/findDeviceDeptTree", params); | ||
| 121 | + JSONObject jsonObject =JSONObject.parseObject(json); | ||
| 122 | + if(!jsonObject.getInteger("code").equals(200)){ | ||
| 123 | + return null; | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + List<DeviceDeptTreeRespVO> deviceDeptTreeList = JSONArray.parseArray(jsonObject.getString("data"), DeviceDeptTreeRespVO.class); | ||
| 127 | + if(CollectionUtil.isEmpty(deviceDeptTreeList)){ | ||
| 128 | + return null; | ||
| 129 | + } | ||
| 130 | + //获取所有已绑定设备列表 | ||
| 131 | + List<DeviceUserDO> list = deviceUserMapper.selectList(); | ||
| 132 | + if(CollectionUtil.isEmpty(list)){ | ||
| 133 | + return deviceDeptTreeList.get(0); | ||
| 134 | + } | ||
| 135 | + List<String> deviceNos = list.stream().map(deviceUserDO -> deviceUserDO.getDeviceNo()).collect(Collectors.toList()); | ||
| 136 | + // | ||
| 137 | + DeviceDeptTreeUtils.removeDevicesByCodes(deviceDeptTreeList, deviceNos); | ||
| 138 | + return deviceDeptTreeList.get(0); | ||
| 139 | + } | ||
| 140 | + | ||
| 109 | } | 141 | } |
| 110 | \ No newline at end of file | 142 | \ No newline at end of file |
urbanops-module-garden/src/main/java/com/zteits/urbanops/module/garden/util/DeviceDeptTreeUtils.java
0 → 100644
| 1 | +package com.zteits.urbanops.module.garden.util; | ||
| 2 | + | ||
| 3 | +import com.zteits.urbanops.module.garden.controller.admin.deviceuser.vo.DeviceDeptTreeRespVO; | ||
| 4 | + | ||
| 5 | +import java.util.*; | ||
| 6 | + | ||
| 7 | +public class DeviceDeptTreeUtils { | ||
| 8 | + | ||
| 9 | + | ||
| 10 | + /** | ||
| 11 | + * 批量删除指定codes的设备节点 | ||
| 12 | + * | ||
| 13 | + * @param treeNodes 树节点列表 | ||
| 14 | + * @param codes 待删除的设备编码列表 | ||
| 15 | + * @return 删除成功的设备数量 | ||
| 16 | + */ | ||
| 17 | + public static int removeDevicesByCodes(List<DeviceDeptTreeRespVO> treeNodes, List<String> codes) { | ||
| 18 | + if (treeNodes == null || treeNodes.isEmpty() || codes == null || codes.isEmpty()) { | ||
| 19 | + return 0; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + Set<String> codeSet = new HashSet<>(codes); // 转换为Set提高查找效率 | ||
| 23 | + return removeDevicesInternal(treeNodes, codeSet); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + /** | ||
| 27 | + * 内部递归删除方法 | ||
| 28 | + */ | ||
| 29 | + private static int removeDevicesInternal(List<DeviceDeptTreeRespVO> treeNodes, Set<String> codeSet) { | ||
| 30 | + if (treeNodes == null || treeNodes.isEmpty()) { | ||
| 31 | + return 0; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + int removedCount = 0; | ||
| 35 | + Iterator<DeviceDeptTreeRespVO> iterator = treeNodes.iterator(); | ||
| 36 | + while (iterator.hasNext()) { | ||
| 37 | + DeviceDeptTreeRespVO node = iterator.next(); | ||
| 38 | + | ||
| 39 | + // 检查当前节点是否需要删除(是设备节点且code在待删除列表中) | ||
| 40 | + if (node.getNodeType() != null && node.getNodeType() == 3 && | ||
| 41 | + codeSet.contains(node.getCode())) { | ||
| 42 | + iterator.remove(); | ||
| 43 | + removedCount++; | ||
| 44 | + } else { | ||
| 45 | + // 递归处理子节点 | ||
| 46 | + if (node.getChildren() != null) { | ||
| 47 | + removedCount += removeDevicesInternal(node.getChildren(), codeSet); | ||
| 48 | + } | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + return removedCount; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + /** | ||
| 56 | + * 获取所有设备节点 | ||
| 57 | + */ | ||
| 58 | + public static List<DeviceDeptTreeRespVO> getAllDevices(List<DeviceDeptTreeRespVO> treeNodes) { | ||
| 59 | + List<DeviceDeptTreeRespVO> devices = new ArrayList<>(); | ||
| 60 | + collectDevices(treeNodes, devices); | ||
| 61 | + return devices; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + /** | ||
| 65 | + * 递归收集所有设备节点 | ||
| 66 | + */ | ||
| 67 | + private static void collectDevices(List<DeviceDeptTreeRespVO> nodes, List<DeviceDeptTreeRespVO> devices) { | ||
| 68 | + if (nodes == null || nodes.isEmpty()) { | ||
| 69 | + return; | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + for (DeviceDeptTreeRespVO node : nodes) { | ||
| 73 | + // 如果是设备节点,添加到结果列表 | ||
| 74 | + if (node.getNodeType() != null && node.getNodeType() == 3) { | ||
| 75 | + devices.add(node); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + // 递归处理子节点 | ||
| 79 | + if (node.getChildren() != null && !node.getChildren().isEmpty()) { | ||
| 80 | + collectDevices(node.getChildren(), devices); | ||
| 81 | + } | ||
| 82 | + } | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | +} | ||
| 86 | + |
urbanops-server/src/main/resources/application.yaml