forked from dyf/fys-Multi-tenant
查询设备分组列表
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
package com.fuyuanshen.equipment.service;
|
||||
|
||||
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
|
||||
import com.fuyuanshen.equipment.domain.vo.DeviceGroupVo;
|
||||
import com.fuyuanshen.equipment.domain.bo.DeviceGroupBo;
|
||||
|
||||
|
||||
@ -1,10 +1,16 @@
|
||||
package com.fuyuanshen.equipment.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.fuyuanshen.common.core.domain.R;
|
||||
import com.fuyuanshen.common.core.utils.MapstructUtils;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
|
||||
import com.fuyuanshen.common.satoken.utils.LoginHelper;
|
||||
import com.fuyuanshen.equipment.domain.Device;
|
||||
import com.fuyuanshen.equipment.domain.DeviceTypeGrants;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -15,9 +21,11 @@ import com.fuyuanshen.equipment.domain.DeviceGroup;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceGroupMapper;
|
||||
import com.fuyuanshen.equipment.service.IDeviceGroupService;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 设备分组Service业务层处理
|
||||
@ -32,6 +40,7 @@ public class DeviceGroupServiceImpl implements IDeviceGroupService {
|
||||
|
||||
private final DeviceGroupMapper baseMapper;
|
||||
|
||||
|
||||
/**
|
||||
* 查询设备分组
|
||||
*
|
||||
@ -52,8 +61,34 @@ public class DeviceGroupServiceImpl implements IDeviceGroupService {
|
||||
*/
|
||||
@Override
|
||||
public List<DeviceGroupVo> queryList(DeviceGroupBo bo) {
|
||||
LambdaQueryWrapper<DeviceGroup> lqw = buildQueryWrapper(bo);
|
||||
return baseMapper.selectVoList(lqw);
|
||||
Page<Device> page = new Page<>(bo.getPageNum(), bo.getPageSize());
|
||||
// 1. 查询顶级分组(parent_id为null)
|
||||
IPage<DeviceGroup> rootGroups = baseMapper.selectRootGroups(bo, page);
|
||||
List<DeviceGroup> records = rootGroups.getRecords();
|
||||
|
||||
// 2. 递归构建树形结构
|
||||
return records.stream()
|
||||
.map(this::buildGroupTree)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private DeviceGroupVo buildGroupTree(DeviceGroup group) {
|
||||
DeviceGroupVo vo = convertToVO(group);
|
||||
// 递归查询子分组
|
||||
List<DeviceGroup> children = baseMapper.selectByParentId(group.getId());
|
||||
vo.setChildren(children.stream()
|
||||
.map(this::buildGroupTree)
|
||||
.collect(Collectors.toList()));
|
||||
return vo;
|
||||
}
|
||||
|
||||
private DeviceGroupVo convertToVO(DeviceGroup group) {
|
||||
DeviceGroupVo vo = new DeviceGroupVo();
|
||||
vo.setId(group.getId());
|
||||
vo.setGroupName(group.getGroupName());
|
||||
vo.setStatus(group.getStatus() == 1 ? "正常" : "禁用");
|
||||
vo.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(group.getCreateTime()));
|
||||
return vo;
|
||||
}
|
||||
|
||||
private LambdaQueryWrapper<DeviceGroup> buildQueryWrapper(DeviceGroupBo bo) {
|
||||
@ -81,13 +116,13 @@ public class DeviceGroupServiceImpl implements IDeviceGroupService {
|
||||
// 验证分组名称唯一性
|
||||
DeviceGroup deviceGroup = baseMapper.selectOne(new QueryWrapper<DeviceGroup>().eq("group_name", bo.getGroupName()));
|
||||
if (deviceGroup != null) {
|
||||
throw new RuntimeException("分组名称已存在,请勿重复添加");
|
||||
throw new RuntimeException("分组名称已存在,请勿重复添加!!!");
|
||||
}
|
||||
|
||||
// 验证父分组是否存在(如果提供了parentId)
|
||||
DeviceGroup pDeviceGroup = baseMapper.selectById(bo.getParentId());
|
||||
if (bo.getParentId() != null && pDeviceGroup == null) {
|
||||
throw new RuntimeException("父分组不存在");
|
||||
throw new RuntimeException("父分组不存在!!!");
|
||||
}
|
||||
|
||||
DeviceGroup add = MapstructUtils.convert(bo, DeviceGroup.class);
|
||||
|
||||
Reference in New Issue
Block a user