jiangping
2025-04-07 01f988407c7cca7080b564bd20b59da78d92ff9d
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.doumee.service.system.impl;
 
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doumee.dao.system.SystemPermissionMapper;
import com.doumee.dao.system.dto.DeleteSystemPermissionDTO;
import com.doumee.dao.system.model.SystemPermission;
import com.doumee.dao.system.vo.SystemPermissionListVO;
import com.doumee.service.system.SystemPermissionService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 系统权限Service实现
 *
 * @author  dm
 * @since 2025/03/31 16:44
 */
@Service
public class SystemPermissionServiceImpl implements SystemPermissionService {
 
    @Autowired
    private SystemPermissionMapper systemPermissionMapper;
 
    @Override
    public Integer create(SystemPermission systemPermission) {
        systemPermissionMapper.insert(systemPermission);
        return systemPermission.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        SystemPermission systemPermission = new SystemPermission();
        systemPermission.setId(id);
        systemPermission.setDeleted(Boolean.TRUE);
        this.updateById(systemPermission);
    }
 
    @Override
    @Transactional
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) return;
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }
 
    @Override
    public void delete(DeleteSystemPermissionDTO dto) {
        UpdateWrapper<SystemPermission> deleteWrapper = new UpdateWrapper<>();
        deleteWrapper.lambda()
                .set(SystemPermission::getDeleted, Boolean.TRUE)
                .eq(dto.getId() != null, SystemPermission::getId, dto.getId())
                .likeLeft(dto.getModulePrefix() != null, SystemPermission::getModule, dto.getModulePrefix());
        systemPermissionMapper.update(null, deleteWrapper);
    }
 
    @Override
    public void deleteInBatch(List<DeleteSystemPermissionDTO> dtos) {
        if (CollectionUtils.isEmpty(dtos)) {
            return;
        }
        for (DeleteSystemPermissionDTO dto : dtos) {
            this.delete(dto);
        }
    }
 
    @Override
    public void updateById(SystemPermission systemPermission) {
        systemPermissionMapper.updateById(systemPermission);
    }
 
    @Override
    @Transactional
    public void updateByIdInBatch(List<SystemPermission> systemPermissions) {
        if (CollectionUtils.isEmpty(systemPermissions)) return;
        for (SystemPermission systemPermission : systemPermissions) {
            this.updateById(systemPermission);
        }
    }
 
    @Override
    public SystemPermission findById(Integer id) {
        return systemPermissionMapper.selectById(id);
    }
 
    @Override
    public List<SystemPermission> findByUserId(Integer userId) {
        return systemPermissionMapper.selectByUserId(userId);
    }
 
    @Override
    public List<SystemPermission> findByRoleId(Integer roleId) {
        return systemPermissionMapper.selectByRoleId(roleId);
    }
 
    @Override
    public SystemPermission findOne(SystemPermission systemPermission) {
        Wrapper<SystemPermission> wrapper = new QueryWrapper<>(systemPermission);
        return systemPermissionMapper.selectOne(wrapper);
    }
 
    @Override
    public List<SystemPermission> findList(SystemPermission systemPermission) {
        Wrapper<SystemPermission> wrapper = new QueryWrapper<>(systemPermission);
        return systemPermissionMapper.selectList(wrapper);
    }
 
    @Override
    public List<SystemPermissionListVO> findTree() {
        List<SystemPermissionListVO> permissionList = systemPermissionMapper.selectManageList();
        List<SystemPermissionListVO> tree = new ArrayList<>();
        for (SystemPermissionListVO permission : permissionList) {
            // 没有模块的权限默认归属为"其它"模块
            if (StringUtils.isBlank(permission.getModule())) {
                permission.setModule("其它");
            }
            // 分解模块
            String[] modulePaths = permission.getModule().split("/");
            SystemPermissionListVO target = null;
            SystemPermissionListVO parent = null;
            List<String> paths = new ArrayList<>();
            for (String path : modulePaths) {
                paths.add(path);
                // 查找模块
                target = this.getTargetNode(tree, paths);
                // 如果模块不存在,则添加一个子模块
                if (target == null) {
                    target = new SystemPermissionListVO();
                    target.setId(Integer.valueOf(RandomStringUtils.randomNumeric(6)) * -1);
                    target.setLevel(paths.size() - 1);
                    target.setType("module");
                    target.setName(path);
                    target.setModulePath(StringUtils.join(paths, "/"));
                    target.setChildren(new ArrayList<>());
                    if (parent != null) {
                        parent.getChildren().add(target);
                    } else {
                        tree.add(target);
                    }
                }
                parent = target;
            }
            // 将权限添加进模块中
            if (target != null) {
                target.getChildren().add(permission);
            }
        }
        return tree;
    }
 
    @Override
    public long count(SystemPermission systemPermission) {
        Wrapper<SystemPermission> wrapper = new QueryWrapper<>(systemPermission);
        return systemPermissionMapper.selectCount(wrapper);
    }
 
    /**
     * 查找目标节点
     * @param tree 节点树
     * @param paths 模块路径
     * @return
     */
    private SystemPermissionListVO getTargetNode(List<SystemPermissionListVO> tree, List<String> paths) {
        List<SystemPermissionListVO> list = tree;
        List<SystemPermissionListVO> nodeStack = new ArrayList<>();
        for (String path : paths) {
            if (list == null) {
                break;
            }
            for (SystemPermissionListVO permission : list) {
                if (path.equals(permission.getName()) && "module".equals(permission.getType())) {
                    nodeStack.add(permission);
                    list = permission.getChildren();
                    break;
                }
            }
        }
        return nodeStack.size() == paths.size() ? nodeStack.get(nodeStack.size() - 1) : null;
    }
}