linwe
2024-08-09 8b7258c9427882bb1798f1502eaa35184c6e374e
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
package org.springblade.common.handle;
 
import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.RequiredArgsConstructor;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.datascope.constant.DataScopeConstant;
import org.springblade.core.datascope.handler.ScopeModelHandler;
import org.springblade.core.datascope.model.DataScopeModel;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
@DS("master")
@RequiredArgsConstructor
public class BladeScopeModelHandlerMaster implements ScopeModelHandler {
    private static final String SCOPE_CACHE_CODE = "dataScope:code:";
    private static final String SCOPE_CACHE_CLASS = "dataScope:class:";
    private static final String DEPT_CACHE_ANCESTORS = "dept:ancestors:";
    private static final DataScopeModel SEARCHED_DATA_SCOPE_MODEL;
    private final JdbcTemplate jdbcTemplate;
 
    public DataScopeModel getDataScopeByMapper(String mapperId, String roleId) {
        List<Object> args = new ArrayList(Collections.singletonList(mapperId));
        List<Long> roleIds = Func.toLongList(roleId);
        args.addAll(roleIds);
        DataScopeModel dataScope = (DataScopeModel) CacheUtil.get("blade:sys", "dataScope:class:", mapperId + ":" + roleId, DataScopeModel.class, Boolean.FALSE);
        if (dataScope == null || !dataScope.getSearched()) {
            List<DataScopeModel> list = this.jdbcTemplate.query(DataScopeConstant.dataByMapper(roleIds.size()), args.toArray(), new BeanPropertyRowMapper(DataScopeModel.class));
            if (CollectionUtil.isNotEmpty(list)) {
                dataScope = (DataScopeModel)list.iterator().next();
                dataScope.setSearched(Boolean.TRUE);
            } else {
                dataScope = SEARCHED_DATA_SCOPE_MODEL;
            }
 
            CacheUtil.put("blade:sys", "dataScope:class:", mapperId + ":" + roleId, dataScope, Boolean.FALSE);
        }
 
        return StringUtil.isNotBlank(dataScope.getResourceCode()) ? dataScope : null;
    }
 
    public DataScopeModel getDataScopeByCode(String code) {
        DataScopeModel dataScope = (DataScopeModel)CacheUtil.get("blade:sys", "dataScope:code:", code, DataScopeModel.class, Boolean.FALSE);
        if (dataScope == null || !dataScope.getSearched()) {
            List<DataScopeModel> list = this.jdbcTemplate.query("select resource_code, scope_column, scope_field, scope_type, scope_value from blade_scope_data where resource_code = ?", new Object[]{code}, new BeanPropertyRowMapper(DataScopeModel.class));
            if (CollectionUtil.isNotEmpty(list)) {
                dataScope = (DataScopeModel)list.iterator().next();
                dataScope.setSearched(Boolean.TRUE);
            } else {
                dataScope = SEARCHED_DATA_SCOPE_MODEL;
            }
 
            CacheUtil.put("blade:sys", "dataScope:code:", code, dataScope, Boolean.FALSE);
        }
 
        return StringUtil.isNotBlank(dataScope.getResourceCode()) ? dataScope : null;
    }
 
    public List<Long> getDeptAncestors(Long deptId) {
        List ancestors = (List)CacheUtil.get("blade:sys", "dept:ancestors:", deptId, List.class);
        if (CollectionUtil.isEmpty(ancestors)) {
            ancestors = this.jdbcTemplate.queryForList("select id from blade_dept where ancestors like concat(concat('%', ?),'%') and is_deleted = 0", new Object[]{deptId}, Long.class);
            CacheUtil.put("blade:sys", "dept:ancestors:", deptId, ancestors);
        }
 
        return ancestors;
    }
 
    static {
        SEARCHED_DATA_SCOPE_MODEL = new DataScopeModel(Boolean.TRUE);
    }
}