jiangping
2025-01-06 e462144e6dcf574cf538baba510b69e477d2b057
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package com.doumee.service.system.impl;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doumee.service.business.third.model.PageData;
import com.doumee.service.business.third.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.dao.business.dao.CompanyMapper;
import com.doumee.dao.business.model.Company;
import com.doumee.dao.system.SystemDataPermissionMapper;
import com.doumee.dao.system.SystemRoleMapper;
import com.doumee.dao.system.model.SystemDataPermission;
import com.doumee.dao.system.model.SystemRole;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.dao.system.vo.SystemDataPermissionListVO;
import com.doumee.service.system.SystemDataPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 数据权限配置Service实现
 * @author Eva.Caesar Liu
 * @date 2023/03/21 14:49
 */
@Service
public class SystemDataPermissionServiceImpl implements SystemDataPermissionService {
 
    @Autowired
    private SystemDataPermissionMapper systemDataPermissionMapper;
 
    @Autowired
    private SystemRoleMapper systemRoleMapper;
    @Autowired
    private CompanyMapper companyMapper;
 
    @Override
    public Integer create(SystemDataPermission systemDataPermission) {
        systemDataPermissionMapper.insert(systemDataPermission);
        return systemDataPermission.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        SystemDataPermission dataPermission = new SystemDataPermission();
        dataPermission.setId(id);
        dataPermission.setDeleted(Boolean.TRUE);
        this.updateById(dataPermission);
    }
 
    @Override
    public void delete(SystemDataPermission systemDataPermission) {
        UpdateWrapper<SystemDataPermission> deleteWrapper = new UpdateWrapper<>(systemDataPermission);
        systemDataPermissionMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        systemDataPermissionMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(SystemDataPermission systemDataPermission) {
        systemDataPermissionMapper.updateById(systemDataPermission);
    }
 
    @Override
    public void updateByIdInBatch(List<SystemDataPermission> systemDataPermissions) {
        if (CollectionUtils.isEmpty(systemDataPermissions)) {
            return;
        }
        for (SystemDataPermission systemDataPermission: systemDataPermissions) {
            this.updateById(systemDataPermission);
        }
    }
 
    @Override
    public SystemDataPermission findById(Integer id) {
        return systemDataPermissionMapper.selectById(id);
    }
 
    @Override
    public SystemDataPermission findOne(SystemDataPermission systemDataPermission) {
        Wrapper<SystemDataPermission> wrapper = new QueryWrapper<>(systemDataPermission);
        return systemDataPermissionMapper.selectOne(wrapper);
    }
 
    @Override
    public List<SystemDataPermission> findList(SystemDataPermission systemDataPermission) {
        Wrapper<SystemDataPermission> wrapper = new QueryWrapper<>(systemDataPermission);
        return systemDataPermissionMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<SystemDataPermissionListVO> findPage(PageWrap<SystemDataPermission> pageWrap) {
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        return PageData.from(new PageInfo<>(systemDataPermissionMapper.selectManageList(pageWrap.getModel(), pageWrap.getOrderByClause())));
    }
 
    @Override
    public List<SystemDataPermission> findDataPermission(String businessCode, List<String> roles) {
        if (CollectionUtils.isEmpty(roles)) {
            return Collections.emptyList();
        }
        // 查询角色
        QueryWrapper<SystemRole> queryRoleWrapper = new QueryWrapper<>();
        queryRoleWrapper.lambda().in(SystemRole::getCode, roles).eq(SystemRole::getDeleted, Boolean.FALSE);
        List<SystemRole> systemRoles = systemRoleMapper.selectList(queryRoleWrapper);
        if (CollectionUtils.isEmpty(systemRoles)) {
            return Collections.emptyList();
        }
        List<Integer> roleIds = new ArrayList<>();
        for (SystemRole role : systemRoles) {
            roleIds.add(role.getId());
        }
        // 查询数据权限
        QueryWrapper<SystemDataPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SystemDataPermission::getBusinessCode, businessCode)
                .in(SystemDataPermission::getRoleId, roleIds)
                .eq(SystemDataPermission::getDeleted, Boolean.FALSE)
                .eq(SystemDataPermission::getDisabled, Boolean.FALSE);
        return systemDataPermissionMapper.selectList(queryWrapper);
    }
 
    @Override
    public long count(SystemDataPermission systemDataPermission) {
        Wrapper<SystemDataPermission> wrapper = new QueryWrapper<>(systemDataPermission);
        return systemDataPermissionMapper.selectCount(wrapper);
    }
    @Override
    public SystemDataPermission findByRoleId(SystemDataPermission pageWrap){
        if ( pageWrap.getRoleId() == null) {
            return null;
        }
 
        // 查询数据权限
        QueryWrapper<SystemDataPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SystemDataPermission::getBusinessCode, "DEPARTMENT")
                .eq(SystemDataPermission::getRoleId, pageWrap.getRoleId())
                .eq(SystemDataPermission::getDeleted, Boolean.FALSE)
                .eq(SystemDataPermission::getDisabled, Boolean.FALSE);
        List<SystemDataPermission> result = systemDataPermissionMapper.selectList(queryWrapper);
        if(result!=null && result.size()>0){
            return result.get(0);
        }
        return  null;
    }
 
 
    @Override
    public List<Integer> selectHighRole(SystemDataPermission model, SystemRole role, SystemUser user) {
        List<SystemDataPermissionListVO>list =   systemDataPermissionMapper.selectUserList(model,role,user.getId()) ;
        if(list !=null){
            boolean all = false;
            boolean departAndChild=false;
            boolean depart=false;
            boolean departAndLeaf=false;
            boolean custom=false;
            String c = "";
            for(SystemDataPermissionListVO d : list){
                if(!all && Constants.equalsInteger(d.getType(),Constants.DATAPERMISSION_TYPE.all)){
                    all =true;
                }
                if(!depart && Constants.equalsInteger(d.getType(),Constants.DATAPERMISSION_TYPE.depart)){
                    depart =true;
                }
                if(!departAndChild && Constants.equalsInteger(d.getType(),Constants.DATAPERMISSION_TYPE.departAndChild)){
                    departAndChild =true;
                }
                if(!departAndLeaf && Constants.equalsInteger(d.getType(),Constants.DATAPERMISSION_TYPE.departAndLeaf)){
                    departAndLeaf =true;
                }
                if(  Constants.equalsInteger(d.getType(),Constants.DATAPERMISSION_TYPE.custom)){
                    custom =true;
                    c += StringUtils.defaultString(d.getCustomData(),"");
                }
 
            }
            if(!all &&!departAndChild&& !depart && !departAndLeaf && !custom ){
                return  new ArrayList<>();
            }
            Company department = new Company();
            department.setId(user.getCompanyId());
 
            if(all){
                return  null;
            } else{
                List<Integer> dList = new ArrayList<>();
                dList.add(-1);//虚拟部门,排查空集合
                if(custom) {
                    //如果有自定义部门
                    String[] idStrs = c.split(",");
                    for(String s :idStrs){
                        try {
                            Integer si = Integer.parseInt(s);
                            if(!isExists(si,dList)){
                                dList.add(si);
                            }
                        }catch (Exception e){
                        }
                    }
                }
                if(departAndLeaf){
                    if(department!= null){
                        dList.addAll(userDataPermissonList(department,true));
                    }
                }else if(departAndChild){
                    if(department!= null) {
                        dList.addAll(userDataPermissonList(department, false));
                    }
                }else if(depart){
                    if(department!= null && !isExists(department.getId(), dList)){
                        dList.add(department.getId());
                    }
                }
                return  dList;
            }
        }
        return new ArrayList<>();
    }
 
    public List<Integer> userDataPermissonList(Company depart ,boolean isleaf) {
        List<Integer> pool = new ArrayList<>();
        List<Company> departList = companyMapper.selectList(new QueryWrapper<Company>().lambda()
                .eq(Company::getIsdeleted,Constants.ZERO ) );
        pool.add(depart.getId());
        this.fillDepartChildren(pool, Arrays.asList(depart.getId()),isleaf,departList);
        return pool;
    }
 
    private void fillDepartChildren(List<Integer> pool, List<Integer> asList, boolean isleaf, List<Company> departList) {
        List<Company> departments = getDepartListByParentIds(asList,departList);
        List<Integer> ids = departments.stream().map(Company::getId).collect(Collectors.toList());
        if(isleaf){
            if (ids.size() > 0) {
                pool.addAll(ids);
                this.fillDepartChildren(pool, ids,isleaf,departList);
            }
        }else{
            pool.addAll(ids);
        }
 
    }
 
    private List<Company> getDepartListByParentIds(List<Integer> asList, List<Company> departList) {
        List<Company> list = new ArrayList<>();
        if(asList.size()>0){
            for(Integer id : asList){
                list.addAll(getDepartListByParentId(id,departList));
            }
        }
        return list;
    }
 
 
    private List<Company> getDepartListByParentId(Integer pId, List<Company> departList) {
        List<Company> list = new ArrayList<>();
        if(departList!= null){
            for(Company d :departList){
                if(Constants.equalsInteger(d.getParentId(),pId)){
                    list.add(d);
                }
            }
        }
        return  list;
    }
    private boolean isExists(Integer s, List<Integer> dList) {
        if(dList!=null){
            for(Integer t : dList){
                if(Constants.equalsInteger(s,t)){
                    return true;
                }
            }
        }
        return false;
    }
}