Commit af509b2b authored by licc's avatar licc

新增获取直连树结构

parent d910a637
......@@ -75,12 +75,18 @@ public interface UsersMapper extends BaseMapper<Users> {
* 获取用户 基本数据
*
* @param startNumber 开始编号
* @param endNo 结束编号
* @param endNo 结束编号
* @return 列表
*/
List<UserDto> getAllUserData(@Param("startNo") Integer startNumber, @Param("endNo") Integer endNo);
/**
* 获取用户 基本数据
*
* @param list 用户ids
* @return 用户 基本数据列表
*/
List<UserVo> getUserData(@Param("list") List<Long> list);
/*************** chenqi****************/
......
......@@ -243,4 +243,15 @@ GROUP BY user_id
limit #{startNo},#{endNo}
</select>
<select id="getUserData" resultType="cn.wisenergy.model.vo.UserVo">
select u.id as userId,u.parent_id as parentId,a.real_name as realName
from users u left join actives a
on u.id=a.user_id
where u.id IN
<foreach collection="list" index="index" item="id" separator="," open="(" close=")">
#{id}
</foreach>
order by u.id
</select>
</mapper>
package cn.wisenergy.model.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* @author 86187
*/
......@@ -12,16 +15,22 @@ public class UserVo {
/**
* 用户id
*/
@ApiModelProperty(value = "用户id", name = "userId")
private Long userId;
/**
* 父id
*/
@ApiModelProperty(value = "父id", name = "parentId")
private Integer parentId;
/**
* 用户真实名字
*/
@ApiModelProperty(value = "用户真实名字", name = "realName")
private String realName;
@ApiModelProperty(value = "子节点数据", name = "childrens")
List<UserVo> children;
}
......@@ -41,5 +41,12 @@ public interface UserService {
*/
R<UsersInfoDto> getByUserInfoById(Long userId);
/**
* 获取用户直线树结构
* @param userId 用户id
* @return 用户直线树结构
*/
R<List<UserVo>> getLinkTree(Long userId);
}
package cn.wisenergy.service.app.impl;
import cn.wisenergy.common.utils.R;
import cn.wisenergy.common.utils.StringUtil;
import cn.wisenergy.mapper.*;
import cn.wisenergy.model.app.*;
import cn.wisenergy.model.dto.UsersInfoDto;
......@@ -8,6 +9,8 @@ import cn.wisenergy.model.vo.UserVo;
import cn.wisenergy.service.app.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
......@@ -94,4 +97,66 @@ public class UserServiceImpl extends ServiceImpl<UsersMapper, Users> implements
return R.ok(userDto);
}
@Override
public R<List<UserVo>> getLinkTree(Long userId) {
if (null == userId) {
return R.error("入参不能为空!");
}
List<UserVo> userVos = new ArrayList<>();
//获取用户信息
Users user = usersMapper.getById(userId);
if (null != user && StringUtils.isNotBlank(user.getPath())) {
String path = user.getPath();
List<Long> userIds = StringUtil.strToLongArray(path);
//获取用户信息列表
List<UserVo> list = usersMapper.getUserData(userIds);
//递归生成树结构
if (CollectionUtils.isNotEmpty(list)) {
userVos = getChildren(list);
}
}
return R.ok(userVos);
}
private List<UserVo> getChildren(List<UserVo> list) {
List<UserVo> rootList = new ArrayList<>();
List<UserVo> childrenList = new ArrayList<>();
for (UserVo userVo : list) {
if (userVo.getParentId() == 0) {
rootList.add(userVo);
} else {
childrenList.add(userVo);
}
}
//遍历子数组,设置子树
for (UserVo userVo : rootList) {
List<UserVo> children = setChildren(userVo.getUserId(), childrenList);
userVo.setChildren(children);
}
return rootList;
}
private List<UserVo> setChildren(Long userId, List<UserVo> list) {
List<UserVo> children = new ArrayList<>();
for (UserVo userVo : list) {
Long parentId = userVo.getParentId().longValue();
if (userId.equals(parentId)) {
children.add(userVo);
}
}
for (int i = 0; i < children.size(); i++) {
Long autoId = children.get(i).getUserId();
List<UserVo> childrenList = setChildren(autoId, list);
children.get(i).setChildren(childrenList);
}
return children;
}
}
......@@ -75,4 +75,11 @@ public class UserController extends BaseController {
public R<UsersInfoDto> getUserInfo(Long userId) {
return userService.getByUserInfoById(userId);
}
@ApiOperation(value = "获取用户直连树结构", notes = "获取用户直连树结构", httpMethod = "GET")
@ApiImplicitParam(name = "userId", value = "用户id", dataType = "int")
@GetMapping("/user/getLinkTree")
public R<List<UserVo>> getLinkTree(Long userId) {
return userService.getLinkTree(userId);
}
}
......@@ -17,7 +17,7 @@ spring:
allow-bean-definition-overriding: true
# 环境 dev|test|prod
profiles:
active: dev
active: prod
# jackson时间格式化
jackson:
time-zone: GMT+8
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment