feat: (feature/REQ-2595) 服务器启动时加载权限缓存,处理一个权限点一个权限集有多条记录的情况

This commit is contained in:
lilong 2024-11-07 09:49:07 +08:00
parent 181a7becdd
commit a90cc28aee
7 changed files with 64 additions and 17 deletions

View File

@ -9,6 +9,7 @@ import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@ -19,7 +20,7 @@ import java.util.stream.Collectors;
@Slf4j
@Component
public class CacheProductFeatureResourceJob extends IJobHandler {
public class CacheProductFeatureResourceJob extends IJobHandler implements InitializingBean {
@Autowired
private ProductSaasFeatureResourceCacheService productSaasFeatureResourceCacheService;
@ -50,4 +51,10 @@ public class CacheProductFeatureResourceJob extends IJobHandler {
productSaasFeatureResourceCacheService.refreshCache(param);
return ReturnT.SUCCESS;
}
@Override
public void afterPropertiesSet() throws Exception {
// 启动时加载做localCache
execute(null);
}
}

View File

@ -9,6 +9,7 @@ import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@ -19,7 +20,7 @@ import java.util.stream.Collectors;
@Slf4j
@Component
public class CacheProductPermissionJob extends IJobHandler {
public class CacheProductPermissionJob extends IJobHandler implements InitializingBean {
@Autowired
private ProductPermissionCacheService productPermissionCacheService;
@ -49,4 +50,10 @@ public class CacheProductPermissionJob extends IJobHandler {
productPermissionCacheService.refreshCache(param);
return ReturnT.SUCCESS;
}
@Override
public void afterPropertiesSet() throws Exception {
// 启动时加载做localCache
execute(null);
}
}

View File

@ -10,6 +10,7 @@ import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@ -20,7 +21,7 @@ import java.util.stream.Collectors;
@Slf4j
@Component
public class CacheRoleFeatureResourceJob extends IJobHandler {
public class CacheRoleFeatureResourceJob extends IJobHandler implements InitializingBean {
@Autowired
private RoleService roleService;
@ -51,4 +52,10 @@ public class CacheRoleFeatureResourceJob extends IJobHandler {
return ReturnT.SUCCESS;
}
@Override
public void afterPropertiesSet() throws Exception {
// 启动时加载做localCache
execute(null);
}
}

View File

@ -10,6 +10,7 @@ import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@ -20,7 +21,7 @@ import java.util.stream.Collectors;
@Slf4j
@Component
public class CacheRolePermissionJob extends IJobHandler {
public class CacheRolePermissionJob extends IJobHandler implements InitializingBean {
@Autowired
private RoleService roleService;
@ -53,4 +54,10 @@ public class CacheRolePermissionJob extends IJobHandler {
return ReturnT.SUCCESS;
}
@Override
public void afterPropertiesSet() throws Exception {
// 启动时加载做localCache
execute(null);
}
}

View File

@ -15,6 +15,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -24,7 +25,7 @@ import java.util.stream.Collectors;
@Slf4j
@Component
public class CacheSaasFeatureJob extends IJobHandler {
public class CacheSaasFeatureJob extends IJobHandler implements InitializingBean {
@Autowired
private SaasFeatureDao saasFeatureDao;
@ -85,6 +86,12 @@ public class CacheSaasFeatureJob extends IJobHandler {
saasFeatureResourceService.refreshCache(param);
}
@Override
public void afterPropertiesSet() throws Exception {
// 启动时加载做localCache
execute(null);
}
@Data
@Builder
@NoArgsConstructor

View File

@ -1737,19 +1737,25 @@ public class RoleServiceImpl extends ServiceImpl<SaasRoleMapper, SaasRole>
Map<Long, FeatureRoleRelationReq.Role> roles = item.getRoles().stream()
.collect(Collectors.toMap(FeatureRoleRelationReq.Role::getRoleId, Function.identity(), (f, s) -> f));
Map<Long, SaasPgroupPermissionRelation> existFeatureGroups = saasPgroupPermissionRelations.stream()
.collect(Collectors.toMap(SaasPgroupPermissionRelation::getGroupId, Function.identity()));
// 现在接口没有收口也没有找到为什么一个featureIdgroupId会有多条记录
Map<Long, List<SaasPgroupPermissionRelation>> existFeatureGroups = saasPgroupPermissionRelations.stream()
.collect(Collectors.groupingBy(SaasPgroupPermissionRelation::getGroupId));
List<SaasPgroupRoleRelation> update = saasPgroupRoleRelations.stream()
.map(e -> {
SaasPgroupPermissionRelation old = existFeatureGroups.get(e.getGroupId());
if (Objects.isNull(old)) {
List<SaasPgroupPermissionRelation> olds = existFeatureGroups.get(e.getGroupId());
if (CollectionUtils.isEmpty(olds)) {
return null;
}
return olds.stream()
.map(old -> {
e.setId(old.getId());
return e;
})
.collect(Collectors.toList());
})
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.collect(Collectors.toList());
// 记录操作日志

View File

@ -74,19 +74,25 @@ public class SaasPgroupPermissionRelationServiceImpl
saasPgroupPermissionRelationDao.removeByIds(deleteList.stream().map(SaasPgroupPermissionRelation::getId).sorted().collect(Collectors.toList()));
}
Map<String, SaasPgroupPermissionRelation> existFeatureGroups = exists.stream()
.collect(Collectors.toMap(e -> e.getFeatureId() + "_" + e.getGroupId(), Function.identity()));
// 现在接口没有收口也没有找到为什么一个featureIdgroupId会有多条记录
Map<String, List<SaasPgroupPermissionRelation>> existFeatureGroups = exists.stream()
.collect(Collectors.groupingBy(e -> e.getFeatureId() + "_" + e.getGroupId()));
List<SaasPgroupPermissionRelation> updates = relations.stream()
.map(e -> {
SaasPgroupPermissionRelation old = existFeatureGroups.get(e.getFeatureId() + "_" + e.getGroupId());
if (Objects.isNull(old)) {
List<SaasPgroupPermissionRelation> olds = existFeatureGroups.get(e.getFeatureId() + "_" + e.getGroupId());
if (CollectionUtils.isEmpty(olds)) {
return null;
}
return olds.stream()
.map(old -> {
e.setId(old.getId());
return e;
})
.collect(Collectors.toList());
})
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(updates)) {
saasPgroupPermissionRelationDao.updateBatchById(updates);