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);
|
}
|
}
|