无关风月
2024-12-09 2053b8fe0e98d4b4449bc756a93ced78f42277c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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;
 
/**
 * <p>
 * 资源表 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2022-06-08
 */
@Service
public class SecResourcesServiceImpl extends ServiceImpl<SecResourcesMapper, SecResources> implements SecResourcesService {
 
    @Autowired
    private SecResourcesMapper secResourcesMapper;
 
    @Override
    public SecResourcesMapper getSecResourcesMapper() {
        return secResourcesMapper;
    }
 
    @Override
    public ApiResult<String> importAllSources(SysSourcesListDTO dto) {
        try {
            secResourcesMapper.delete(Wrappers.lambdaQuery(SecResources.class).eq(SecResources::getIsDelete, false));
 
            List<SysSourcesDTO> menus = dto.getMenus();
            for (SysSourcesDTO sources : menus) {
                importMenu(sources, 0L);
            }
            return ApiResult.success();
        } catch (Exception e) {
            return ApiResult.failed(e.getMessage());
        }
 
    }
 
    @Override
    public List<SecResourceVO> selectResources() {
        List<SecResources> allResources = secResourcesMapper.selectList(Wrappers.lambdaQuery(SecResources.class)
                .ne(SecResources::getHidden,1));
        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);
            //每次循环都将集合重新循环添加到父集合中
            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<SecResourceVO> selectResourcesRecursion() {
        List<SecResourceVO> resourceVOS = secResourcesMapper.allList();
        // 判空
        if (CollectionUtils.isEmpty(resourceVOS))
            return Lists.newArrayList();
 
        // 找出所有父级
        List<SecResourceVO> parent = resourceVOS.stream().filter(resource -> resource.getParentId()==0).collect(toList());
        // 通过父级递归子集
        getChildren(resourceVOS,parent);
 
        return parent;
    }
 
    private void getChildren(List<SecResourceVO> resourceVOS, List<SecResourceVO> parentList) {
        parentList.forEach(parent -> {
            List<SecResourceVO> 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<Btn> 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);
        }
    }
 
}