package com.qianwen.core.datascope.handler; import java.util.ArrayList; import java.util.List; import java.util.Objects; import com.qianwen.core.datascope.enums.DataScopeEnum; import com.qianwen.core.datascope.model.DataScopeModel; import com.qianwen.core.secure.BladeUser; import com.qianwen.core.tool.utils.BeanUtil; import com.qianwen.core.tool.utils.Func; import com.qianwen.core.tool.utils.PlaceholderUtil; import com.qianwen.core.tool.utils.StringUtil; /* loaded from: blade-starter-datascope-9.3.0.1-SNAPSHOT.jar:org/springblade/core/datascope/handler/BladeDataScopeHandler.class */ public class BladeDataScopeHandler implements DataScopeHandler { private final ScopeModelHandler scopeModelHandler; public BladeDataScopeHandler(final ScopeModelHandler scopeModelHandler) { this.scopeModelHandler = scopeModelHandler; } @Override // org.springblade.core.datascope.handler.DataScopeHandler public String sqlCondition(String mapperId, DataScopeModel dataScope, BladeUser bladeUser, String originalSql) { String code = dataScope.getResourceCode(); DataScopeModel dataScopeDb = this.scopeModelHandler.getDataScopeByMapper(mapperId, bladeUser.getDeptId()); if (dataScopeDb == null && StringUtil.isNotBlank(code) && StringUtil.isNotBlank(bladeUser.getDeptId())) { dataScopeDb = this.scopeModelHandler.getDataScopeByCodeAndDept(code, bladeUser.getDeptId()); } DataScopeModel dataScope2 = dataScopeDb != null ? dataScopeDb : dataScope; Integer scopeRule = ((DataScopeModel) Objects.requireNonNull(dataScope2)).getScopeType(); DataScopeEnum scopeTypeEnum = DataScopeEnum.of(scopeRule); List ids = new ArrayList<>(); String whereSql = "where scope.{} in ({}) or scope.{} is null"; if (DataScopeEnum.ALL == scopeTypeEnum || StringUtil.containsAny(bladeUser.getRoleName(), new CharSequence[]{"administrator"})) { return null; } if (DataScopeEnum.CUSTOM == scopeTypeEnum) { whereSql = PlaceholderUtil.getDefaultResolver().resolveByMap(dataScope2.getScopeValue(), BeanUtil.toMap(bladeUser)); } else if (DataScopeEnum.OWN == scopeTypeEnum) { ids.add(bladeUser.getUserId()); } else if (DataScopeEnum.OWN_DEPT == scopeTypeEnum) { ids.addAll(Func.toLongList(bladeUser.getDeptId())); } else if (DataScopeEnum.OWN_DEPT_CHILD == scopeTypeEnum) { List deptIds = Func.toLongList(bladeUser.getDeptId()); ids.addAll(deptIds); deptIds.forEach(deptId -> { List deptIdList = this.scopeModelHandler.getDeptAncestors(deptId); ids.addAll(deptIdList); }); } return StringUtil.format(" select {} from ({}) scope " + whereSql, new Object[]{Func.toStr(dataScope2.getScopeField(), "*"), originalSql, dataScope2.getScopeColumn(), StringUtil.join(ids), dataScope2.getScopeColumn()}); } }