package com.jilongda.optometrist.authority.service.impl;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.jilongda.optometrist.authority.mapper.SecResourcesMapper;
|
import com.jilongda.optometrist.authority.mapper.SecRoleMapper;
|
import com.jilongda.optometrist.authority.model.SecResources;
|
import com.jilongda.common.basic.PageInfo;
|
import com.jilongda.common.utils.SpringUtils;
|
import com.jilongda.optometrist.authority.dto.SecUserQueryDTO;
|
import com.jilongda.optometrist.authority.model.SecRole;
|
import com.jilongda.optometrist.authority.service.SecRoleService;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.jilongda.optometrist.authority.vo.SecResourceVO;
|
import com.jilongda.optometrist.authority.vo.SecUserVO;
|
import org.apache.commons.compress.utils.Lists;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import static java.util.stream.Collectors.toList;
|
|
/**
|
* <p>
|
* 服务实现类
|
* </p>
|
*
|
* @author xiaochen
|
* @since 2022-06-08
|
*/
|
@Service
|
public class SecRoleServiceImpl extends ServiceImpl<SecRoleMapper, SecRole> implements SecRoleService {
|
|
@Autowired
|
private SecRoleMapper secRoleMapper;
|
@Autowired
|
private SecResourcesMapper secResourcesMapper;
|
|
@Override
|
public SecRoleMapper getSecRoleMapper() {
|
return secRoleMapper;
|
}
|
|
@Override
|
public PageInfo<SecUserVO> getUserByRole(SecUserQueryDTO dto) {
|
PageInfo<SecUserVO> pageInfo = new PageInfo<SecUserVO>(dto.getPageNum(), dto.getPageSize());
|
List<SecUserVO> secUserVOS = secRoleMapper.getUserByRole(dto, pageInfo);
|
pageInfo.setRecords(secUserVOS);
|
// 获取列表
|
return pageInfo;
|
}
|
|
@Override
|
public List<SecResourceVO> selectRoleResourcesByRid(Long rid) {
|
List<SecResources> allResources = secResourcesMapper.selectList(Wrappers.emptyWrapper());
|
List<Long> resourceIds = secRoleMapper.selectResByRid(rid);
|
SecResourceVO item;
|
//获取根节点的集合
|
List<SecResourceVO> root = new ArrayList<>();
|
Map<Long, SecResourceVO> tempMap = new HashMap<>(3);
|
//子节点集合
|
SecResourceVO parent;
|
for (SecResources resources : allResources) {
|
item = SpringUtils.beanCopy(resources, SecResourceVO.class);
|
if (resourceIds.contains(resources.getId())) {
|
item.setFlag(true);
|
}
|
//每次循环都将集合重新循环添加到父集合中
|
tempMap.put(resources.getId(), item);
|
parent = tempMap.get(resources.getParentId());
|
if (null != parent) {
|
//父集合不为空时,获取下级集合
|
//子节点集合
|
parent.getChildren().add(item);
|
} else {
|
//根集合
|
root.add(item);
|
}
|
}
|
return root;
|
}
|
|
@Override
|
public List<SecResourceVO> getRecursion(Long rid) {
|
List<SecResourceVO> secResourceVOS = secResourcesMapper.getList(rid);
|
// 判空
|
if (CollectionUtils.isEmpty(secResourceVOS))
|
return Lists.newArrayList();
|
// 找出所有父级
|
List<SecResourceVO> parent = secResourceVOS.stream().filter(resourceVO -> resourceVO.getParentId().equals("0")).collect(toList());
|
// 通过父级递归子集
|
getChildren(secResourceVOS,parent);
|
return parent;
|
}
|
|
private void getChildren(List<SecResourceVO> secResourceVOS, List<SecResourceVO> parentList) {
|
parentList.forEach(parent -> {
|
List<SecResourceVO> childrenList = secResourceVOS.stream().filter(resourceVO -> resourceVO.getParentId().equals(parent.getId())).collect(toList());
|
parent.setChildren(childrenList);
|
if (!CollectionUtils.isEmpty(childrenList))
|
getChildren(secResourceVOS,childrenList);
|
});
|
}
|
|
}
|