package com.jilongda.manage.authority.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.jilongda.manage.authority.mapper.SecResourcesMapper;
import com.jilongda.manage.authority.model.SecResources;
import com.jilongda.manage.authority.service.SecResourcesService;
import com.jilongda.common.basic.ApiResult;
import com.jilongda.common.utils.SpringUtils;
import com.jilongda.manage.authority.dto.Btn;
import com.jilongda.manage.authority.dto.SysSourcesDTO;
import com.jilongda.manage.authority.dto.SysSourcesListDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jilongda.manage.authority.vo.SecResourceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.*;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
/**
*
* 资源表 服务实现类
*
*
* @author xiaochen
* @since 2022-06-08
*/
@Service
public class SecResourcesServiceImpl extends ServiceImpl implements SecResourcesService {
@Autowired
private SecResourcesMapper secResourcesMapper;
@Override
public SecResourcesMapper getSecResourcesMapper() {
return secResourcesMapper;
}
@Override
public ApiResult importAllSources(SysSourcesListDTO dto) {
try {
secResourcesMapper.delete(Wrappers.lambdaQuery(SecResources.class).eq(SecResources::getIsDelete, false));
List menus = dto.getMenus();
for (SysSourcesDTO sources : menus) {
importMenu(sources, 0L);
}
return ApiResult.success();
} catch (Exception e) {
return ApiResult.failed(e.getMessage());
}
}
@Override
public List selectResources() {
List allResources = secResourcesMapper.selectList(Wrappers.lambdaQuery(SecResources.class)
.ne(SecResources::getHidden,1));
SecResourceVO item;
//获取根节点的集合
List root = new ArrayList<>();
Map tempMap = new HashMap<>(3);
//子节点集合
SecResourceVO parent;
for (SecResources resources : allResources) {
item = SpringUtils.beanCopy(resources, SecResourceVO.class);
//每次循环都将集合重新循环添加到父集合中
tempMap.put(resources.getId(), item);
parent = tempMap.get(resources.getParentId());
if (Objects.nonNull(parent)) {
//父集合不为空时,获取下级集合
//子节点集合
parent.getChildren().add(item);
// 对子集排序,升序
parent.getChildren().sort(comparing(SecResourceVO::getSort));
} else {
//根集合
root.add(item);
// 对根集合排序,升序
root.sort(comparing(SecResourceVO::getSort));
}
}
return root;
}
@Override
public List selectResourcesRecursion() {
List resourceVOS = secResourcesMapper.allList();
// 判空
if (CollectionUtils.isEmpty(resourceVOS))
return Lists.newArrayList();
// 找出所有父级
List parent = resourceVOS.stream().filter(resource -> resource.getParentId()==0).collect(toList());
// 通过父级递归子集
getChildren(resourceVOS,parent);
return parent;
}
private void getChildren(List resourceVOS, List parentList) {
parentList.forEach(parent -> {
List childrenList = resourceVOS.stream().filter(region -> region.getParentId().equals(parent.getId())).collect(toList());
parent.setChildren(childrenList);
if (!CollectionUtils.isEmpty(childrenList))
getChildren(resourceVOS,childrenList);
});
}
private void importMenu(SysSourcesDTO sources, Long parentId) {
//装填本体
SecResources resources = new SecResources();
if ("0".equals(parentId)) {
resources.setType(1);
} else {
resources.setType(2);
}
if (Objects.nonNull(sources.getMeta())) {
resources.setMenu(sources.getMeta().getParent());
resources.setTitle(sources.getMeta().getTitle());
}
resources.setParentId(parentId);
resources.setIcon(sources.getIcon());
resources.setPath(sources.getPath());
resources.setName(sources.getName());
resources.setComponent(sources.getComponent());
if (Objects.nonNull(sources.getHidden())) {
resources.setHidden(sources.getHidden());
}
//存储本体
secResourcesMapper.insert(resources);
//判断是否有btn按钮,有则存储
if (!CollectionUtils.isEmpty(sources.getBtn())) {
setButtons(sources.getBtn(), resources.getId());
}
//判断是否存在子菜单,存在则递归存储
if (!CollectionUtils.isEmpty(sources.getChildren())) {
for (SysSourcesDTO nextSources : sources.getChildren()) {
importMenu(nextSources, resources.getId());
}
}
}
private void setButtons(List buttons, Long parentId) {
for (Btn button : buttons) {
SecResources btn = new SecResources();
btn.setType(3);
btn.setTitle(button.getTitle());
btn.setName(button.getName());
btn.setParentId(parentId);
secResourcesMapper.insert(btn);
}
}
}