mirror of https://github.com/elunez/eladmin
				
				
				
			refactor: 移除缓存注解,优化Redis缓存使用
							parent
							
								
									fe8d271557
								
							
						
					
					
						commit
						10b43563aa
					
				| 
						 | 
				
			
			@ -103,6 +103,7 @@ public class QuartzJobServiceImpl implements QuartzJobService {
 | 
			
		|||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void updateIsPause(QuartzJob quartzJob) {
 | 
			
		||||
        // 置换暂停状态
 | 
			
		||||
        if (quartzJob.getIsPause()) {
 | 
			
		||||
            quartzManage.resumeJob(quartzJob);
 | 
			
		||||
            quartzJob.setIsPause(false);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,23 +23,23 @@ import me.zhengjie.modules.system.service.DeptService;
 | 
			
		|||
import me.zhengjie.modules.system.service.RoleService;
 | 
			
		||||
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
 | 
			
		||||
import me.zhengjie.modules.system.service.dto.UserDto;
 | 
			
		||||
import me.zhengjie.utils.CacheKey;
 | 
			
		||||
import me.zhengjie.utils.RedisUtils;
 | 
			
		||||
import me.zhengjie.utils.enums.DataScopeEnum;
 | 
			
		||||
import org.springframework.cache.annotation.CacheConfig;
 | 
			
		||||
import org.springframework.cache.annotation.Cacheable;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author Zheng Jie
 | 
			
		||||
 * @website https://eladmin.vip
 | 
			
		||||
 * @description 数据权限服务实现
 | 
			
		||||
 * @date 2020-05-07
 | 
			
		||||
 **/
 | 
			
		||||
@Service
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
@CacheConfig(cacheNames = "data", keyGenerator = "keyGenerator")
 | 
			
		||||
public class DataServiceImpl implements DataService {
 | 
			
		||||
 | 
			
		||||
    private final RedisUtils redisUtils;
 | 
			
		||||
    private final RoleService roleService;
 | 
			
		||||
    private final DeptService deptService;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -49,27 +49,32 @@ public class DataServiceImpl implements DataService {
 | 
			
		|||
     * @return /
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    @Cacheable(key = "'user:' + #p0.id")
 | 
			
		||||
    public List<Long> getDeptIds(UserDto user) {
 | 
			
		||||
        // 用于存储部门id
 | 
			
		||||
        Set<Long> deptIds = new HashSet<>();
 | 
			
		||||
        // 查询用户角色
 | 
			
		||||
        List<RoleSmallDto> roleSet = roleService.findByUsersId(user.getId());
 | 
			
		||||
        // 获取对应的部门ID
 | 
			
		||||
        for (RoleSmallDto role : roleSet) {
 | 
			
		||||
            DataScopeEnum dataScopeEnum = DataScopeEnum.find(role.getDataScope());
 | 
			
		||||
            switch (Objects.requireNonNull(dataScopeEnum)) {
 | 
			
		||||
                case THIS_LEVEL:
 | 
			
		||||
                    deptIds.add(user.getDept().getId());
 | 
			
		||||
                    break;
 | 
			
		||||
                case CUSTOMIZE:
 | 
			
		||||
                    deptIds.addAll(getCustomize(deptIds, role));
 | 
			
		||||
                    break;
 | 
			
		||||
                default:
 | 
			
		||||
                    return new ArrayList<>();
 | 
			
		||||
        String key = CacheKey.DATA_USER + user.getId();
 | 
			
		||||
        List<Long> ids = redisUtils.getList(key, Long.class);
 | 
			
		||||
        if (CollUtil.isEmpty(ids)) {
 | 
			
		||||
            // 用于存储部门id
 | 
			
		||||
            Set<Long> deptIds = new HashSet<>();
 | 
			
		||||
            // 查询用户角色
 | 
			
		||||
            List<RoleSmallDto> roleSet = roleService.findByUsersId(user.getId());
 | 
			
		||||
            // 获取对应的部门ID
 | 
			
		||||
            for (RoleSmallDto role : roleSet) {
 | 
			
		||||
                DataScopeEnum dataScopeEnum = DataScopeEnum.find(role.getDataScope());
 | 
			
		||||
                switch (Objects.requireNonNull(dataScopeEnum)) {
 | 
			
		||||
                    case THIS_LEVEL:
 | 
			
		||||
                        deptIds.add(user.getDept().getId());
 | 
			
		||||
                        break;
 | 
			
		||||
                    case CUSTOMIZE:
 | 
			
		||||
                        deptIds.addAll(getCustomize(deptIds, role));
 | 
			
		||||
                        break;
 | 
			
		||||
                    default:
 | 
			
		||||
                        return new ArrayList<>();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            ids = new ArrayList<>(deptIds);
 | 
			
		||||
            redisUtils.set(key, ids, 1, TimeUnit.DAYS);
 | 
			
		||||
        }
 | 
			
		||||
        return new ArrayList<>(deptIds);
 | 
			
		||||
        return new ArrayList<>(ids);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,6 +15,7 @@
 | 
			
		|||
 */
 | 
			
		||||
package me.zhengjie.modules.system.service.impl;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.collection.CollUtil;
 | 
			
		||||
import cn.hutool.core.collection.CollectionUtil;
 | 
			
		||||
import cn.hutool.core.util.ObjectUtil;
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
| 
						 | 
				
			
			@ -30,8 +31,6 @@ import me.zhengjie.modules.system.repository.DeptRepository;
 | 
			
		|||
import me.zhengjie.modules.system.service.DeptService;
 | 
			
		||||
import me.zhengjie.modules.system.service.mapstruct.DeptMapper;
 | 
			
		||||
import me.zhengjie.utils.enums.DataScopeEnum;
 | 
			
		||||
import org.springframework.cache.annotation.CacheConfig;
 | 
			
		||||
import org.springframework.cache.annotation.Cacheable;
 | 
			
		||||
import org.springframework.data.domain.Sort;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.springframework.transaction.annotation.Transactional;
 | 
			
		||||
| 
						 | 
				
			
			@ -39,6 +38,7 @@ import javax.servlet.http.HttpServletResponse;
 | 
			
		|||
import java.io.IOException;
 | 
			
		||||
import java.lang.reflect.Field;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			@ -47,7 +47,6 @@ import java.util.stream.Collectors;
 | 
			
		|||
*/
 | 
			
		||||
@Service
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
@CacheConfig(cacheNames = "dept", keyGenerator = "keyGenerator")
 | 
			
		||||
public class DeptServiceImpl implements DeptService {
 | 
			
		||||
 | 
			
		||||
    private final DeptRepository deptRepository;
 | 
			
		||||
| 
						 | 
				
			
			@ -88,10 +87,14 @@ public class DeptServiceImpl implements DeptService {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @Cacheable(key = "'id:' + #p0")
 | 
			
		||||
    public DeptDto findById(Long id) {
 | 
			
		||||
        Dept dept = deptRepository.findById(id).orElseGet(Dept::new);
 | 
			
		||||
        ValidationUtil.isNull(dept.getId(),"Dept","id",id);
 | 
			
		||||
        String key = CacheKey.DEPT_ID + id;
 | 
			
		||||
        Dept dept = redisUtils.get(key, Dept.class);
 | 
			
		||||
        if(dept == null){
 | 
			
		||||
            dept = deptRepository.findById(id).orElseGet(Dept::new);
 | 
			
		||||
            ValidationUtil.isNull(dept.getId(),"Dept","id",id);
 | 
			
		||||
            redisUtils.set(key, dept, 1, TimeUnit.DAYS);
 | 
			
		||||
        }
 | 
			
		||||
        return deptMapper.toDto(dept);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -166,7 +169,7 @@ public class DeptServiceImpl implements DeptService {
 | 
			
		|||
        for (Dept dept : menuList) {
 | 
			
		||||
            deptDtos.add(deptMapper.toDto(dept));
 | 
			
		||||
            List<Dept> depts = deptRepository.findByPid(dept.getId());
 | 
			
		||||
            if(depts!=null && depts.size()!=0){
 | 
			
		||||
            if(CollUtil.isNotEmpty(depts)){
 | 
			
		||||
                getDeleteDepts(depts, deptDtos);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			@ -179,7 +182,7 @@ public class DeptServiceImpl implements DeptService {
 | 
			
		|||
        deptList.forEach(dept -> {
 | 
			
		||||
                    if (dept!=null && dept.getEnabled()) {
 | 
			
		||||
                        List<Dept> depts = deptRepository.findByPid(dept.getId());
 | 
			
		||||
                        if (depts.size() != 0) {
 | 
			
		||||
                        if (CollUtil.isNotEmpty(depts)) {
 | 
			
		||||
                            list.addAll(getDeptChildren(depts));
 | 
			
		||||
                        }
 | 
			
		||||
                        list.add(dept.getId());
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,6 +15,7 @@
 | 
			
		|||
 */
 | 
			
		||||
package me.zhengjie.modules.system.service.impl;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.collection.CollUtil;
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
import me.zhengjie.utils.PageResult;
 | 
			
		||||
import me.zhengjie.modules.system.domain.Dict;
 | 
			
		||||
| 
						 | 
				
			
			@ -26,13 +27,12 @@ import me.zhengjie.modules.system.repository.DictDetailRepository;
 | 
			
		|||
import me.zhengjie.modules.system.service.DictDetailService;
 | 
			
		||||
import me.zhengjie.modules.system.service.dto.DictDetailDto;
 | 
			
		||||
import me.zhengjie.modules.system.service.mapstruct.DictDetailMapper;
 | 
			
		||||
import org.springframework.cache.annotation.CacheConfig;
 | 
			
		||||
import org.springframework.cache.annotation.Cacheable;
 | 
			
		||||
import org.springframework.data.domain.Page;
 | 
			
		||||
import org.springframework.data.domain.Pageable;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.springframework.transaction.annotation.Transactional;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @author Zheng Jie
 | 
			
		||||
| 
						 | 
				
			
			@ -40,7 +40,6 @@ import java.util.List;
 | 
			
		|||
*/
 | 
			
		||||
@Service
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
@CacheConfig(cacheNames = "dict", keyGenerator = "keyGenerator")
 | 
			
		||||
public class DictDetailServiceImpl implements DictDetailService {
 | 
			
		||||
 | 
			
		||||
    private final DictRepository dictRepository;
 | 
			
		||||
| 
						 | 
				
			
			@ -74,9 +73,14 @@ public class DictDetailServiceImpl implements DictDetailService {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @Cacheable(key = "'name:' + #p0")
 | 
			
		||||
    public List<DictDetailDto> getDictByName(String name) {
 | 
			
		||||
        return dictDetailMapper.toDto(dictDetailRepository.findByDictName(name));
 | 
			
		||||
        String key = CacheKey.DICT_NAME + name;
 | 
			
		||||
        List<DictDetail> dictDetails = redisUtils.getList(key, DictDetail.class);
 | 
			
		||||
        if(CollUtil.isEmpty(dictDetails)){
 | 
			
		||||
            dictDetails = dictDetailRepository.findByDictName(name);
 | 
			
		||||
            redisUtils.set(key, dictDetails, 1 , TimeUnit.DAYS);
 | 
			
		||||
        }
 | 
			
		||||
        return dictDetailMapper.toDto(dictDetails);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,7 +26,6 @@ import me.zhengjie.modules.system.repository.DictRepository;
 | 
			
		|||
import me.zhengjie.modules.system.service.DictService;
 | 
			
		||||
import me.zhengjie.modules.system.service.dto.DictDto;
 | 
			
		||||
import me.zhengjie.modules.system.service.mapstruct.DictMapper;
 | 
			
		||||
import org.springframework.cache.annotation.CacheConfig;
 | 
			
		||||
import org.springframework.data.domain.Page;
 | 
			
		||||
import org.springframework.data.domain.Pageable;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
| 
						 | 
				
			
			@ -41,7 +40,6 @@ import java.util.*;
 | 
			
		|||
*/
 | 
			
		||||
@Service
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
@CacheConfig(cacheNames = "dict", keyGenerator = "keyGenerator")
 | 
			
		||||
public class DictServiceImpl implements DictService {
 | 
			
		||||
 | 
			
		||||
    private final DictRepository dictRepository;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -27,9 +27,6 @@ import me.zhengjie.modules.system.repository.JobRepository;
 | 
			
		|||
import me.zhengjie.modules.system.service.JobService;
 | 
			
		||||
import me.zhengjie.modules.system.service.dto.JobDto;
 | 
			
		||||
import me.zhengjie.modules.system.service.mapstruct.JobMapper;
 | 
			
		||||
import org.springframework.cache.annotation.CacheConfig;
 | 
			
		||||
import org.springframework.cache.annotation.CacheEvict;
 | 
			
		||||
import org.springframework.cache.annotation.Cacheable;
 | 
			
		||||
import org.springframework.data.domain.Page;
 | 
			
		||||
import org.springframework.data.domain.Pageable;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
| 
						 | 
				
			
			@ -37,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
 | 
			
		|||
import javax.servlet.http.HttpServletResponse;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
* @author Zheng Jie
 | 
			
		||||
| 
						 | 
				
			
			@ -44,7 +42,6 @@ import java.util.*;
 | 
			
		|||
*/
 | 
			
		||||
@Service
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
@CacheConfig(cacheNames = "job", keyGenerator = "keyGenerator")
 | 
			
		||||
public class JobServiceImpl implements JobService {
 | 
			
		||||
 | 
			
		||||
    private final JobRepository jobRepository;
 | 
			
		||||
| 
						 | 
				
			
			@ -65,10 +62,14 @@ public class JobServiceImpl implements JobService {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @Cacheable(key = "'id:' + #p0")
 | 
			
		||||
    public JobDto findById(Long id) {
 | 
			
		||||
        Job job = jobRepository.findById(id).orElseGet(Job::new);
 | 
			
		||||
        ValidationUtil.isNull(job.getId(),"Job","id",id);
 | 
			
		||||
        String key = CacheKey.JOB_ID + id;
 | 
			
		||||
        Job job = redisUtils.get(key, Job.class);
 | 
			
		||||
        if(job == null){
 | 
			
		||||
            job = jobRepository.findById(id).orElseGet(Job::new);
 | 
			
		||||
            ValidationUtil.isNull(job.getId(),"Job","id",id);
 | 
			
		||||
            redisUtils.set(key, job, 1, TimeUnit.DAYS);
 | 
			
		||||
        }
 | 
			
		||||
        return jobMapper.toDto(job);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -83,7 +84,6 @@ public class JobServiceImpl implements JobService {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @CacheEvict(key = "'id:' + #p0.id")
 | 
			
		||||
    @Transactional(rollbackFor = Exception.class)
 | 
			
		||||
    public void update(Job resources) {
 | 
			
		||||
        Job job = jobRepository.findById(resources.getId()).orElseGet(Job::new);
 | 
			
		||||
| 
						 | 
				
			
			@ -94,6 +94,8 @@ public class JobServiceImpl implements JobService {
 | 
			
		|||
        ValidationUtil.isNull( job.getId(),"Job","id",resources.getId());
 | 
			
		||||
        resources.setId(job.getId());
 | 
			
		||||
        jobRepository.save(resources);
 | 
			
		||||
        // 删除缓存
 | 
			
		||||
        delCaches(resources.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
| 
						 | 
				
			
			@ -123,4 +125,12 @@ public class JobServiceImpl implements JobService {
 | 
			
		|||
            throw new BadRequestException("所选的岗位中存在用户关联,请解除关联再试!");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 删除缓存
 | 
			
		||||
     * @param id /
 | 
			
		||||
     */
 | 
			
		||||
    public void delCaches(Long id){
 | 
			
		||||
        redisUtils.del(CacheKey.JOB_ID + id);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,6 +15,7 @@
 | 
			
		|||
 */
 | 
			
		||||
package me.zhengjie.modules.system.service.impl;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.collection.CollUtil;
 | 
			
		||||
import cn.hutool.core.collection.CollectionUtil;
 | 
			
		||||
import cn.hutool.core.util.ObjectUtil;
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
| 
						 | 
				
			
			@ -34,8 +35,6 @@ import me.zhengjie.modules.system.service.dto.MenuQueryCriteria;
 | 
			
		|||
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
 | 
			
		||||
import me.zhengjie.modules.system.service.mapstruct.MenuMapper;
 | 
			
		||||
import me.zhengjie.utils.*;
 | 
			
		||||
import org.springframework.cache.annotation.CacheConfig;
 | 
			
		||||
import org.springframework.cache.annotation.Cacheable;
 | 
			
		||||
import org.springframework.data.domain.Sort;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.springframework.transaction.annotation.Transactional;
 | 
			
		||||
| 
						 | 
				
			
			@ -43,6 +42,7 @@ import javax.servlet.http.HttpServletResponse;
 | 
			
		|||
import java.io.IOException;
 | 
			
		||||
import java.lang.reflect.Field;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			@ -50,7 +50,6 @@ import java.util.stream.Collectors;
 | 
			
		|||
 */
 | 
			
		||||
@Service
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
@CacheConfig(cacheNames = "menu", keyGenerator = "keyGenerator")
 | 
			
		||||
public class MenuServiceImpl implements MenuService {
 | 
			
		||||
 | 
			
		||||
    private final MenuRepository menuRepository;
 | 
			
		||||
| 
						 | 
				
			
			@ -88,10 +87,14 @@ public class MenuServiceImpl implements MenuService {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @Cacheable(key = "'id:' + #p0")
 | 
			
		||||
    public MenuDto findById(long id) {
 | 
			
		||||
        Menu menu = menuRepository.findById(id).orElseGet(Menu::new);
 | 
			
		||||
        ValidationUtil.isNull(menu.getId(),"Menu","id",id);
 | 
			
		||||
        String key = CacheKey.MENU_ID + id;
 | 
			
		||||
        Menu menu = redisUtils.get(key, Menu.class);
 | 
			
		||||
        if(menu == null){
 | 
			
		||||
            menu = menuRepository.findById(id).orElseGet(Menu::new);
 | 
			
		||||
            ValidationUtil.isNull(menu.getId(),"Menu","id",id);
 | 
			
		||||
            redisUtils.set(key, menu, 1, TimeUnit.DAYS);
 | 
			
		||||
        }
 | 
			
		||||
        return menuMapper.toDto(menu);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -101,11 +104,16 @@ public class MenuServiceImpl implements MenuService {
 | 
			
		|||
     * @return /
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    @Cacheable(key = "'user:' + #p0")
 | 
			
		||||
    public List<MenuDto> findByUser(Long currentUserId) {
 | 
			
		||||
        List<RoleSmallDto> roles = roleService.findByUsersId(currentUserId);
 | 
			
		||||
        Set<Long> roleIds = roles.stream().map(RoleSmallDto::getId).collect(Collectors.toSet());
 | 
			
		||||
        LinkedHashSet<Menu> menus = menuRepository.findByRoleIdsAndTypeNot(roleIds, 2);
 | 
			
		||||
        String key = CacheKey.MENU_USER + currentUserId;
 | 
			
		||||
        List<Menu> menus = redisUtils.getList(key, Menu.class);
 | 
			
		||||
        if (CollUtil.isEmpty(menus)){
 | 
			
		||||
            List<RoleSmallDto> roles = roleService.findByUsersId(currentUserId);
 | 
			
		||||
            Set<Long> roleIds = roles.stream().map(RoleSmallDto::getId).collect(Collectors.toSet());
 | 
			
		||||
            LinkedHashSet<Menu> data = menuRepository.findByRoleIdsAndTypeNot(roleIds, 2);
 | 
			
		||||
            menus = new ArrayList<>(data);
 | 
			
		||||
            redisUtils.set(key, menus, 1, TimeUnit.DAYS);
 | 
			
		||||
        }
 | 
			
		||||
        return menus.stream().map(menuMapper::toDto).collect(Collectors.toList());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -194,7 +202,7 @@ public class MenuServiceImpl implements MenuService {
 | 
			
		|||
        for (Menu menu : menuList) {
 | 
			
		||||
            menuSet.add(menu);
 | 
			
		||||
            List<Menu> menus = menuRepository.findByPidOrderByMenuSort(menu.getId());
 | 
			
		||||
            if(menus!=null && menus.size()!=0){
 | 
			
		||||
            if(CollUtil.isNotEmpty(menus)){
 | 
			
		||||
                getChildMenus(menus, menuSet);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			@ -252,7 +260,7 @@ public class MenuServiceImpl implements MenuService {
 | 
			
		|||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if(trees.size() == 0){
 | 
			
		||||
        if(trees.isEmpty()){
 | 
			
		||||
            trees = menuDtos.stream().filter(s -> !ids.contains(s.getId())).collect(Collectors.toList());
 | 
			
		||||
        }
 | 
			
		||||
        return trees;
 | 
			
		||||
| 
						 | 
				
			
			@ -287,16 +295,7 @@ public class MenuServiceImpl implements MenuService {
 | 
			
		|||
                            menuVo.setChildren(buildMenus(menuDtoList));
 | 
			
		||||
                            // 处理是一级菜单并且没有子菜单的情况
 | 
			
		||||
                        } else if(menuDTO.getPid() == null){
 | 
			
		||||
                            MenuVo menuVo1 = new MenuVo();
 | 
			
		||||
                            menuVo1.setMeta(menuVo.getMeta());
 | 
			
		||||
                            // 非外链
 | 
			
		||||
                            if(!menuDTO.getIFrame()){
 | 
			
		||||
                                menuVo1.setPath("index");
 | 
			
		||||
                                menuVo1.setName(menuVo.getName());
 | 
			
		||||
                                menuVo1.setComponent(menuVo.getComponent());
 | 
			
		||||
                            } else {
 | 
			
		||||
                                menuVo1.setPath(menuDTO.getPath());
 | 
			
		||||
                            }
 | 
			
		||||
                            MenuVo menuVo1 = getMenuVo(menuDTO, menuVo);
 | 
			
		||||
                            menuVo.setName(null);
 | 
			
		||||
                            menuVo.setMeta(null);
 | 
			
		||||
                            menuVo.setComponent("Layout");
 | 
			
		||||
| 
						 | 
				
			
			@ -356,4 +355,24 @@ public class MenuServiceImpl implements MenuService {
 | 
			
		|||
        }});
 | 
			
		||||
        redisUtils.delByKeys(CacheKey.ROLE_ID, roles.stream().map(Role::getId).collect(Collectors.toSet()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 构建前端路由
 | 
			
		||||
     * @param menuDTO /
 | 
			
		||||
     * @param menuVo /
 | 
			
		||||
     * @return /
 | 
			
		||||
     */
 | 
			
		||||
    private static MenuVo getMenuVo(MenuDto menuDTO, MenuVo menuVo) {
 | 
			
		||||
        MenuVo menuVo1 = new MenuVo();
 | 
			
		||||
        menuVo1.setMeta(menuVo.getMeta());
 | 
			
		||||
        // 非外链
 | 
			
		||||
        if(!menuDTO.getIFrame()){
 | 
			
		||||
            menuVo1.setPath("index");
 | 
			
		||||
            menuVo1.setName(menuVo.getName());
 | 
			
		||||
            menuVo1.setComponent(menuVo.getComponent());
 | 
			
		||||
        } else {
 | 
			
		||||
            menuVo1.setPath(menuDTO.getPath());
 | 
			
		||||
        }
 | 
			
		||||
        return menuVo1;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,6 +15,7 @@
 | 
			
		|||
 */
 | 
			
		||||
package me.zhengjie.modules.system.service.impl;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.collection.CollUtil;
 | 
			
		||||
import cn.hutool.core.collection.CollectionUtil;
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
import me.zhengjie.exception.BadRequestException;
 | 
			
		||||
| 
						 | 
				
			
			@ -34,8 +35,6 @@ import me.zhengjie.modules.system.service.dto.UserDto;
 | 
			
		|||
import me.zhengjie.modules.system.service.mapstruct.RoleMapper;
 | 
			
		||||
import me.zhengjie.modules.system.service.mapstruct.RoleSmallMapper;
 | 
			
		||||
import me.zhengjie.utils.*;
 | 
			
		||||
import org.springframework.cache.annotation.CacheConfig;
 | 
			
		||||
import org.springframework.cache.annotation.Cacheable;
 | 
			
		||||
import org.springframework.data.domain.Page;
 | 
			
		||||
import org.springframework.data.domain.Pageable;
 | 
			
		||||
import org.springframework.data.domain.Sort;
 | 
			
		||||
| 
						 | 
				
			
			@ -44,6 +43,7 @@ import org.springframework.transaction.annotation.Transactional;
 | 
			
		|||
import javax.servlet.http.HttpServletResponse;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			@ -52,7 +52,6 @@ import java.util.stream.Collectors;
 | 
			
		|||
 */
 | 
			
		||||
@Service
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
@CacheConfig(cacheNames = "role", keyGenerator = "keyGenerator")
 | 
			
		||||
public class RoleServiceImpl implements RoleService {
 | 
			
		||||
 | 
			
		||||
    private final RoleRepository roleRepository;
 | 
			
		||||
| 
						 | 
				
			
			@ -80,11 +79,14 @@ public class RoleServiceImpl implements RoleService {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @Cacheable(key = "'id:' + #p0")
 | 
			
		||||
    @Transactional(rollbackFor = Exception.class)
 | 
			
		||||
    public RoleDto findById(long id) {
 | 
			
		||||
        Role role = roleRepository.findById(id).orElseGet(Role::new);
 | 
			
		||||
        ValidationUtil.isNull(role.getId(), "Role", "id", id);
 | 
			
		||||
        String key = CacheKey.ROLE_ID + id;
 | 
			
		||||
        Role role = redisUtils.get(key, Role.class);
 | 
			
		||||
        if (role == null) {
 | 
			
		||||
            role = roleRepository.findById(id).orElseGet(Role::new);
 | 
			
		||||
            ValidationUtil.isNull(role.getId(), "Role", "id", id);
 | 
			
		||||
            redisUtils.set(key, role, 1, TimeUnit.DAYS);
 | 
			
		||||
        }
 | 
			
		||||
        return roleMapper.toDto(role);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -152,7 +154,7 @@ public class RoleServiceImpl implements RoleService {
 | 
			
		|||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Integer findByRoles(Set<Role> roles) {
 | 
			
		||||
        if (roles.size() == 0) {
 | 
			
		||||
        if (roles.isEmpty()) {
 | 
			
		||||
            return Integer.MAX_VALUE;
 | 
			
		||||
        }
 | 
			
		||||
        Set<RoleDto> roleDtos = new HashSet<>();
 | 
			
		||||
| 
						 | 
				
			
			@ -163,21 +165,26 @@ public class RoleServiceImpl implements RoleService {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @Cacheable(key = "'auth:' + #p0.id")
 | 
			
		||||
    public List<AuthorityDto> buildAuthorities(UserDto user) {
 | 
			
		||||
        Set<String> permissions = new HashSet<>();
 | 
			
		||||
        // 如果是管理员直接返回
 | 
			
		||||
        if (user.getIsAdmin()) {
 | 
			
		||||
            permissions.add("admin");
 | 
			
		||||
            return permissions.stream().map(AuthorityDto::new)
 | 
			
		||||
        String key = CacheKey.ROLE_AUTH + user.getId();
 | 
			
		||||
        List<AuthorityDto> authorityDtos = redisUtils.getList(key, AuthorityDto.class);
 | 
			
		||||
        if (CollUtil.isEmpty(authorityDtos)) {
 | 
			
		||||
            Set<String> permissions = new HashSet<>();
 | 
			
		||||
            // 如果是管理员直接返回
 | 
			
		||||
            if (user.getIsAdmin()) {
 | 
			
		||||
                permissions.add("admin");
 | 
			
		||||
                return permissions.stream().map(AuthorityDto::new)
 | 
			
		||||
                        .collect(Collectors.toList());
 | 
			
		||||
            }
 | 
			
		||||
            Set<Role> roles = roleRepository.findByUserId(user.getId());
 | 
			
		||||
            permissions = roles.stream().flatMap(role -> role.getMenus().stream())
 | 
			
		||||
                    .map(Menu::getPermission)
 | 
			
		||||
                    .filter(StringUtils::isNotBlank).collect(Collectors.toSet());
 | 
			
		||||
            authorityDtos = permissions.stream().map(AuthorityDto::new)
 | 
			
		||||
                    .collect(Collectors.toList());
 | 
			
		||||
            redisUtils.set(key, authorityDtos, 1, TimeUnit.HOURS);
 | 
			
		||||
        }
 | 
			
		||||
        Set<Role> roles = roleRepository.findByUserId(user.getId());
 | 
			
		||||
        permissions = roles.stream().flatMap(role -> role.getMenus().stream())
 | 
			
		||||
                .map(Menu::getPermission)
 | 
			
		||||
                .filter(StringUtils::isNotBlank).collect(Collectors.toSet());
 | 
			
		||||
        return permissions.stream().map(AuthorityDto::new)
 | 
			
		||||
                .collect(Collectors.toList());
 | 
			
		||||
        return authorityDtos;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -30,8 +30,6 @@ import me.zhengjie.modules.system.service.dto.*;
 | 
			
		|||
import me.zhengjie.modules.system.service.mapstruct.UserLoginMapper;
 | 
			
		||||
import me.zhengjie.modules.system.service.mapstruct.UserMapper;
 | 
			
		||||
import me.zhengjie.utils.*;
 | 
			
		||||
import org.springframework.cache.annotation.CacheConfig;
 | 
			
		||||
import org.springframework.cache.annotation.Cacheable;
 | 
			
		||||
import org.springframework.data.domain.Page;
 | 
			
		||||
import org.springframework.data.domain.Pageable;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
| 
						 | 
				
			
			@ -42,6 +40,7 @@ import javax.validation.constraints.NotBlank;
 | 
			
		|||
import java.io.File;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			@ -50,7 +49,6 @@ import java.util.stream.Collectors;
 | 
			
		|||
 */
 | 
			
		||||
@Service
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
@CacheConfig(cacheNames = "user", keyGenerator = "keyGenerator")
 | 
			
		||||
public class UserServiceImpl implements UserService {
 | 
			
		||||
 | 
			
		||||
    private final UserRepository userRepository;
 | 
			
		||||
| 
						 | 
				
			
			@ -74,11 +72,15 @@ public class UserServiceImpl implements UserService {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @Cacheable(key = "'id:' + #p0")
 | 
			
		||||
    @Transactional(rollbackFor = Exception.class)
 | 
			
		||||
    public UserDto findById(long id) {
 | 
			
		||||
        User user = userRepository.findById(id).orElseGet(User::new);
 | 
			
		||||
        ValidationUtil.isNull(user.getId(), "User", "id", id);
 | 
			
		||||
        String key = CacheKey.USER_ID + id;
 | 
			
		||||
        User user = redisUtils.get(key, User.class);
 | 
			
		||||
        if (user == null) {
 | 
			
		||||
            user = userRepository.findById(id).orElseGet(User::new);
 | 
			
		||||
            ValidationUtil.isNull(user.getId(), "User", "id", id);
 | 
			
		||||
            redisUtils.set(key, user, 1, TimeUnit.DAYS);
 | 
			
		||||
        }
 | 
			
		||||
        return userMapper.toDto(user);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue