Commit 7161058b by huangjinxin

fix:构建资源树

1 parent 7809665d
...@@ -13,6 +13,11 @@ public enum RedisKeyGroup { ...@@ -13,6 +13,11 @@ public enum RedisKeyGroup {
/** /**
* 认证ak对应的认证信息 * 认证ak对应的认证信息
*/ */
appKey; appKey,
/**
* 资源信息
*/
resourceKey;
} }
...@@ -3,17 +3,21 @@ package com.dituhui.pea.user.service; ...@@ -3,17 +3,21 @@ package com.dituhui.pea.user.service;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.dituhui.pea.common.PageResult; import com.dituhui.pea.common.PageResult;
import com.dituhui.pea.enums.RedisKeyGroup;
import com.dituhui.pea.enums.StatusCodeEnum; import com.dituhui.pea.enums.StatusCodeEnum;
import com.dituhui.pea.exception.BusinessException; import com.dituhui.pea.exception.BusinessException;
import com.dituhui.pea.pojo.PageRequest; import com.dituhui.pea.pojo.PageRequest;
import com.dituhui.pea.pojo.ResourceInfo; import com.dituhui.pea.pojo.ResourceInfo;
import com.dituhui.pea.pojo.RoleInfo; import com.dituhui.pea.pojo.RoleInfo;
import com.dituhui.pea.pojo.role.RoleResourceInfo; import com.dituhui.pea.pojo.role.RoleResourceInfo;
import com.dituhui.pea.user.commom.RedisService;
import com.dituhui.pea.user.dao.*; import com.dituhui.pea.user.dao.*;
import com.dituhui.pea.user.entity.*; import com.dituhui.pea.user.entity.*;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
...@@ -49,6 +53,14 @@ public class RoleService { ...@@ -49,6 +53,14 @@ public class RoleService {
@Autowired @Autowired
RoleResourceDao roleResourceDao; RoleResourceDao roleResourceDao;
@Autowired
RedisService redisService;
/**
* 失效时间ms
*/
private static final int LIVE_TIME_MILLIS = 360000;
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public RoleInfo addRole(RoleInfo roleInfo) { public RoleInfo addRole(RoleInfo roleInfo) {
...@@ -263,7 +275,23 @@ public class RoleService { ...@@ -263,7 +275,23 @@ public class RoleService {
} }
public List<ResourceInfo> allResource() { public List<ResourceInfo> allResource() {
String redisValue = redisService.get(RedisKeyGroup.resourceKey.name());
if (StringUtils.isNotBlank(redisValue)) {
return JSONObject.parseObject(redisValue, List.class);
}
List<ResourceEntity> all = resourceDao.findAll(); List<ResourceEntity> all = resourceDao.findAll();
return all.stream().map(entity -> BeanUtil.copyProperties(entity, ResourceInfo.class)).collect(Collectors.toList());
List<ResourceInfo> collect = all.stream().filter(resourceEntity -> StringUtils.isBlank(resourceEntity.getParentId())).map(entity -> BeanUtil.copyProperties(entity, ResourceInfo.class)).collect(Collectors.toList());
for (ResourceInfo entity : collect) {
List<ResourceInfo> twoResource = all.stream().filter(resourceEntity -> entity.getParentId().equals(entity.getId())).map(e -> BeanUtil.copyProperties(e, ResourceInfo.class)).collect(Collectors.toList());
for (ResourceInfo twoEntity : twoResource) {
List<ResourceInfo> threeResource = all.stream().filter(resourceEntity -> entity.getParentId().equals(twoEntity.getId())).map(e -> BeanUtil.copyProperties(e, ResourceInfo.class)).collect(Collectors.toList());
twoEntity.setChildren(threeResource);
}
entity.setChildren(twoResource);
}
long timestamp = System.currentTimeMillis() + LIVE_TIME_MILLIS;
redisService.set(RedisKeyGroup.resourceKey.name(), JSONObject.toJSONString(collect), timestamp / 1000);
return collect;
} }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!