yangys
2024-03-27 e48aa2ac8dea1be5db11c63edf0b912c4ad5ce65
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
package com.qianwen.core.datascope.handler;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.qianwen.core.cache.utils.CacheUtil;
import com.qianwen.core.datascope.constant.DataScopeConstant;
import com.qianwen.core.datascope.model.DataScopeModel;
import com.qianwen.core.tool.utils.CollectionUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.core.tool.utils.StringUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
 
/* loaded from: blade-starter-datascope-9.3.0.1-SNAPSHOT.jar:org/springblade/core/datascope/handler/BladeScopeModelHandler.class */
public class BladeScopeModelHandler 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 = new DataScopeModel(Boolean.TRUE);
    private final JdbcTemplate jdbcTemplate;
 
    public BladeScopeModelHandler(final JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
    @Override // org.springblade.core.datascope.handler.ScopeModelHandler
    public DataScopeModel getDataScopeByMapper(String mapperId, String deptId) {
        List<Object> args = new ArrayList<>(Collections.singletonList(mapperId));
        List<Long> deptIds = Func.toLongList(deptId);
        args.addAll(deptIds);
        DataScopeModel dataScope = (DataScopeModel) CacheUtil.get("blade:sys", SCOPE_CACHE_CLASS, mapperId + ":" + deptId, DataScopeModel.class, Boolean.FALSE);
        if (dataScope == null || !dataScope.getSearched().booleanValue()) {
            List<DataScopeModel> list = this.jdbcTemplate.query(DataScopeConstant.dataByMapper(deptIds.size()), args.toArray(), new BeanPropertyRowMapper(DataScopeModel.class));
            if (CollectionUtil.isNotEmpty(list)) {
                dataScope = list.iterator().next();
                dataScope.setSearched(Boolean.TRUE);
            } else {
                dataScope = SEARCHED_DATA_SCOPE_MODEL;
            }
            CacheUtil.put("blade:sys", SCOPE_CACHE_CLASS, mapperId + ":" + deptId, dataScope, Boolean.FALSE);
        }
        if (StringUtil.isNotBlank(dataScope.getResourceCode())) {
            return dataScope;
        }
        return null;
    }
 
    @Override // org.springblade.core.datascope.handler.ScopeModelHandler
    public DataScopeModel getDataScopeByCodeAndDept(String code, String deptId) {
        List<Object> args = new ArrayList<>(Collections.singletonList(code));
        List<Long> deptIds = Func.toLongList(deptId);
        args.addAll(deptIds);
        DataScopeModel dataScope = (DataScopeModel) CacheUtil.get("blade:sys", SCOPE_CACHE_CLASS, code + ":" + deptId, DataScopeModel.class, Boolean.FALSE);
        if (dataScope == null || !dataScope.getSearched().booleanValue()) {
            List<DataScopeModel> list = this.jdbcTemplate.query(DataScopeConstant.dataByCode(deptIds.size()), args.toArray(), new BeanPropertyRowMapper(DataScopeModel.class));
            if (CollectionUtil.isNotEmpty(list)) {
                dataScope = list.iterator().next();
                dataScope.setSearched(Boolean.TRUE);
            } else {
                dataScope = SEARCHED_DATA_SCOPE_MODEL;
            }
            CacheUtil.put("blade:sys", SCOPE_CACHE_CLASS, code + ":" + deptId, dataScope, Boolean.FALSE);
        }
        if (StringUtil.isNotBlank(dataScope.getResourceCode())) {
            return dataScope;
        }
        return null;
    }
 
    @Override // org.springblade.core.datascope.handler.ScopeModelHandler
    public DataScopeModel getDataScopeByCode(String code) {
        DataScopeModel dataScope = (DataScopeModel) CacheUtil.get("blade:sys", SCOPE_CACHE_CODE, code, DataScopeModel.class, Boolean.FALSE);
        if (dataScope == null || !dataScope.getSearched().booleanValue()) {
            List<DataScopeModel> list = this.jdbcTemplate.query(DataScopeConstant.DATA_BY_CODE, new Object[]{code}, new BeanPropertyRowMapper(DataScopeModel.class));
            if (CollectionUtil.isNotEmpty(list)) {
                dataScope = list.iterator().next();
                dataScope.setSearched(Boolean.TRUE);
            } else {
                dataScope = SEARCHED_DATA_SCOPE_MODEL;
            }
            CacheUtil.put("blade:sys", SCOPE_CACHE_CODE, code, dataScope, Boolean.FALSE);
        }
        if (StringUtil.isNotBlank(dataScope.getResourceCode())) {
            return dataScope;
        }
        return null;
    }
 
    @Override // org.springblade.core.datascope.handler.ScopeModelHandler
    public List<Long> getDeptAncestors(Long deptId) {
        List ancestors = (List) CacheUtil.get("blade:sys", DEPT_CACHE_ANCESTORS, deptId, List.class, Boolean.FALSE);
        if (CollectionUtil.isEmpty(ancestors)) {
            ancestors = this.jdbcTemplate.queryForList(DataScopeConstant.DATA_BY_DEPT, new Object[]{deptId}, Long.class);
            CacheUtil.put("blade:sys", DEPT_CACHE_ANCESTORS, deptId, ancestors, Boolean.FALSE);
        }
        return ancestors;
    }
}