diff --git a/backend/src/main/java/com/yfd/platform/qgc_base/domain/vo/CurrentUserDataScopeVo.java b/backend/src/main/java/com/yfd/platform/qgc_base/domain/vo/CurrentUserDataScopeVo.java new file mode 100644 index 00000000..7f62e247 --- /dev/null +++ b/backend/src/main/java/com/yfd/platform/qgc_base/domain/vo/CurrentUserDataScopeVo.java @@ -0,0 +1,31 @@ +package com.yfd.platform.qgc_base.domain.vo; + +import lombok.Data; + +import java.io.Serializable; +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * 当前登录用户的数据权限范围 + */ +@Data +public class CurrentUserDataScopeVo implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 是否为统一管理的超级管理员 + */ + private boolean managedAdmin; + + /** + * 当前用户有权限的电站编码 + */ + private Set stationCodes = new LinkedHashSet<>(); + + /** + * 当前用户有权限的流域编码 + */ + private Set rvcdCodes = new LinkedHashSet<>(); +} diff --git a/backend/src/main/java/com/yfd/platform/qgc_base/service/ICurrentUserDataScopeService.java b/backend/src/main/java/com/yfd/platform/qgc_base/service/ICurrentUserDataScopeService.java new file mode 100644 index 00000000..ff1ce1e2 --- /dev/null +++ b/backend/src/main/java/com/yfd/platform/qgc_base/service/ICurrentUserDataScopeService.java @@ -0,0 +1,26 @@ +package com.yfd.platform.qgc_base.service; + +import com.yfd.platform.qgc_base.domain.vo.CurrentUserDataScopeVo; + +import java.util.Set; + +/** + * 当前登录用户数据权限公共服务 + */ +public interface ICurrentUserDataScopeService { + + /** + * 获取当前用户完整的数据权限范围 + */ + CurrentUserDataScopeVo getCurrentUserDataScope(); + + /** + * 获取当前用户有权限的电站编码 + */ + Set getCurrentUserAuthorizedStationCodes(); + + /** + * 获取当前用户有权限的流域编码 + */ + Set getCurrentUserAuthorizedRvcdCodes(); +} diff --git a/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/CurrentUserDataScopeServiceImpl.java b/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/CurrentUserDataScopeServiceImpl.java new file mode 100644 index 00000000..95d399a8 --- /dev/null +++ b/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/CurrentUserDataScopeServiceImpl.java @@ -0,0 +1,161 @@ +package com.yfd.platform.qgc_base.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.yfd.platform.qgc_base.domain.SdEngInfoBH; +import com.yfd.platform.qgc_base.domain.vo.CurrentUserDataScopeVo; +import com.yfd.platform.qgc_base.mapper.SdEngInfoBHMapper; +import com.yfd.platform.qgc_base.service.ICurrentUserDataScopeService; +import com.yfd.platform.qgc_data.domain.SysUserDataScope; +import com.yfd.platform.qgc_data.mapper.SysUserDataScopeMapper; +import com.yfd.platform.system.service.IAdminAuthService; +import com.yfd.platform.utils.SecurityUtils; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + +/** + * 当前登录用户数据权限公共服务实现 + */ +@Service +public class CurrentUserDataScopeServiceImpl implements ICurrentUserDataScopeService { + + private static final int QUERY_BATCH_SIZE = 500; + + @Resource + private SysUserDataScopeMapper sysUserDataScopeMapper; + + @Resource + private SdEngInfoBHMapper sdEngInfoBHMapper; + + @Resource + private IAdminAuthService adminAuthService; + + @Override + public CurrentUserDataScopeVo getCurrentUserDataScope() { + CurrentUserDataScopeVo scopeVo = new CurrentUserDataScopeVo(); + if (adminAuthService.isCurrentManagedAdmin()) { + scopeVo.setManagedAdmin(true); + return scopeVo; + } + + String userId = SecurityUtils.getUserId(); + if (!StringUtils.hasText(userId)) { + return scopeVo; + } + + List validPermissions = sysUserDataScopeMapper.selectValidPermissions(userId); + if (validPermissions == null || validPermissions.isEmpty()) { + return scopeVo; + } + + Set directStationCodes = collectOrgIdsByType(validPermissions, "STATION"); + Set directRvcdCodes = collectOrgIdsByType(validPermissions, "RVCD"); + Set stationCodes = new LinkedHashSet<>(); + Set rvcdCodes = new LinkedHashSet<>(); + + if (!directStationCodes.isEmpty()) { + List stationList = listStationsByStcds(directStationCodes); + stationCodes.addAll(extractStationCodes(stationList)); + rvcdCodes.addAll(extractLegacyBasinCodes(stationList)); + } + + if (!directRvcdCodes.isEmpty()) { + List stationList = listStationsByReachcds(directRvcdCodes); + stationCodes.addAll(extractStationCodes(stationList)); + rvcdCodes.addAll(extractLegacyBasinCodes(stationList)); + } + + scopeVo.setStationCodes(stationCodes); + scopeVo.setRvcdCodes(rvcdCodes); + return scopeVo; + } + + @Override + public Set getCurrentUserAuthorizedStationCodes() { + return getCurrentUserDataScope().getStationCodes(); + } + + @Override + public Set getCurrentUserAuthorizedRvcdCodes() { + return getCurrentUserDataScope().getRvcdCodes(); + } + + private Set collectOrgIdsByType(List scopes, String orgType) { + Set orgIds = new LinkedHashSet<>(); + for (SysUserDataScope scope : scopes) { + if (scope == null || !orgType.equalsIgnoreCase(scope.getOrgType())) { + continue; + } + if (StringUtils.hasText(scope.getOrgId())) { + orgIds.add(scope.getOrgId()); + } + } + return orgIds; + } + + private List listStationsByStcds(Set stationCodes) { + return listStationsByColumn(stationCodes, QueryDimension.STATION); + } + + private List listStationsByReachcds(Set rvcdCodes) { + return listStationsByColumn(rvcdCodes, QueryDimension.REACH); + } + + private List listStationsByColumn(Set codes, QueryDimension queryDimension) { + List result = new ArrayList<>(); + if (codes == null || codes.isEmpty()) { + return result; + } + + List codeList = new ArrayList<>(codes); + for (int i = 0; i < codeList.size(); i += QUERY_BATCH_SIZE) { + int end = Math.min(i + QUERY_BATCH_SIZE, codeList.size()); + List batchCodes = codeList.subList(i, end); + LambdaQueryWrapper wrapper = new LambdaQueryWrapper() + .select(SdEngInfoBH::getStcd, SdEngInfoBH::getReachcd, SdEngInfoBH::getEnnm); + if (queryDimension == QueryDimension.STATION) { + wrapper.in(SdEngInfoBH::getStcd, batchCodes); + } else { + wrapper.in(SdEngInfoBH::getReachcd, batchCodes); + } + result.addAll(sdEngInfoBHMapper.selectList(wrapper)); + } + return result; + } + + private Set extractStationCodes(List stationList) { + Set stationCodes = new LinkedHashSet<>(); + if (stationList == null || stationList.isEmpty()) { + return stationCodes; + } + for (SdEngInfoBH station : stationList) { + if (station != null && StringUtils.hasText(station.getStcd())) { + stationCodes.add(station.getStcd()); + } + } + return stationCodes; + } + + private Set extractLegacyBasinCodes(List stationList) { + Set rvcdCodes = new LinkedHashSet<>(); + if (stationList == null || stationList.isEmpty()) { + return rvcdCodes; + } + for (SdEngInfoBH station : stationList) { + if (station != null && StringUtils.hasText(station.getEnnm()) && StringUtils.hasText(station.getReachcd())) { + rvcdCodes.add(station.getReachcd()); + } + } + return rvcdCodes; + } + + private enum QueryDimension { + STATION, + REACH + } +} diff --git a/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/SdEngInfoBHServiceImpl.java b/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/SdEngInfoBHServiceImpl.java index 2d144fa7..837cb471 100644 --- a/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/SdEngInfoBHServiceImpl.java +++ b/backend/src/main/java/com/yfd/platform/qgc_base/service/impl/SdEngInfoBHServiceImpl.java @@ -128,8 +128,26 @@ public class SdEngInfoBHServiceImpl extends ServiceImpl selectForDropdown(SdEngInfoBHRequest sdEngInfoBHRequest) { + List allData = this.selectForDropdownCached(sdEngInfoBHRequest); + + if (adminAuthService.isCurrentManagedAdmin()) { + return allData; + } + Set authorizedStations = getUserAuthorizedStationCodes(); + if (authorizedStations != null && !authorizedStations.isEmpty()) { + return allData.stream() + .filter(item -> authorizedStations.contains(item.getStcd())) + .collect(Collectors.toList()); + } + return new ArrayList<>(); + } + + /** + * 纯数据库查询(可缓存),不包含权限过滤逻辑 + */ + @Cacheable(cacheNames = "engInfoCache#3600", keyGenerator = "cacheKeyGenerator") + public List selectForDropdownCached(SdEngInfoBHRequest sdEngInfoBHRequest) { String baseId = sdEngInfoBHRequest.getBaseId(); String hbrvcd = sdEngInfoBHRequest.getHbrvcd(); String ennm = sdEngInfoBHRequest.getEnnm(); @@ -140,27 +158,14 @@ public class SdEngInfoBHServiceImpl extends ServiceImpl wrapper = new LambdaQueryWrapper<>(); wrapper.eq(StringUtils.hasText(baseId), SdEngInfoBH::getBaseId, baseId) .eq(StringUtils.hasText(hbrvcd), SdEngInfoBH::getHbrvcd, hbrvcd) - .eq(StrUtil.isNotBlank(reachcd), SdEngInfoBH::getRvcd, reachcd) - .eq(StrUtil.isNotBlank(rvcd), SdEngInfoBH::getReachcd, rvcd) - .eq(StrUtil.isNotBlank(rvcd), SdEngInfoBH::getReachcd, rvcd) + .eq(StrUtil.isNotBlank(reachcd), SdEngInfoBH::getReachcd, reachcd) + .eq(StrUtil.isNotBlank(rvcd), SdEngInfoBH::getRvcd, rvcd) .in(rvcds != null && !rvcds.isEmpty(), SdEngInfoBH::getReachcd, rvcds) .in(hbrvcds != null && !hbrvcds.isEmpty(), SdEngInfoBH::getHbrvcd, hbrvcds) .like(StringUtils.hasText(ennm), SdEngInfoBH::getEnnm, ennm) .select(SdEngInfoBH::getStcd, SdEngInfoBH::getEnnm, SdEngInfoBH::getReachcdName, SdEngInfoBH::getYrgeb, SdEngInfoBH::getBaseId) - .orderByAsc(SdEngInfoBH::getBaseId,SdEngInfoBH::getOrderIndex); - - if(adminAuthService.isCurrentManagedAdmin()){ - return this.list(wrapper); - } - Set authorizedStations = getUserAuthorizedStationCodes(); - if (authorizedStations != null && !authorizedStations.isEmpty()) { - List list = this.list(wrapper); - return list.stream() - .filter(item -> authorizedStations.contains(item.getStcd())) - .collect(Collectors.toList()); - }else{ - return new ArrayList<>(); - } + .orderByAsc(SdEngInfoBH::getBaseId, SdEngInfoBH::getOrderIndex); + return this.list(wrapper); } @Override public Set getUserAuthorizedStationCodes() {