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 args = new ArrayList<>(Collections.singletonList(mapperId)); List 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 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 args = new ArrayList<>(Collections.singletonList(code)); List 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 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 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 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; } }