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.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -19,7 +20,7 @@ import java.util.stream.Collectors;
@Slf4j @Slf4j
@Component @Component
public class CacheProductFeatureResourceJob extends IJobHandler { public class CacheProductFeatureResourceJob extends IJobHandler implements InitializingBean {
@Autowired @Autowired
private ProductSaasFeatureResourceCacheService productSaasFeatureResourceCacheService; private ProductSaasFeatureResourceCacheService productSaasFeatureResourceCacheService;
@ -50,4 +51,10 @@ public class CacheProductFeatureResourceJob extends IJobHandler {
productSaasFeatureResourceCacheService.refreshCache(param); productSaasFeatureResourceCacheService.refreshCache(param);
return ReturnT.SUCCESS; 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.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -19,7 +20,7 @@ import java.util.stream.Collectors;
@Slf4j @Slf4j
@Component @Component
public class CacheProductPermissionJob extends IJobHandler { public class CacheProductPermissionJob extends IJobHandler implements InitializingBean {
@Autowired @Autowired
private ProductPermissionCacheService productPermissionCacheService; private ProductPermissionCacheService productPermissionCacheService;
@ -49,4 +50,10 @@ public class CacheProductPermissionJob extends IJobHandler {
productPermissionCacheService.refreshCache(param); productPermissionCacheService.refreshCache(param);
return ReturnT.SUCCESS; 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.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -20,7 +21,7 @@ import java.util.stream.Collectors;
@Slf4j @Slf4j
@Component @Component
public class CacheRoleFeatureResourceJob extends IJobHandler { public class CacheRoleFeatureResourceJob extends IJobHandler implements InitializingBean {
@Autowired @Autowired
private RoleService roleService; private RoleService roleService;
@ -51,4 +52,10 @@ public class CacheRoleFeatureResourceJob extends IJobHandler {
return ReturnT.SUCCESS; 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.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -20,7 +21,7 @@ import java.util.stream.Collectors;
@Slf4j @Slf4j
@Component @Component
public class CacheRolePermissionJob extends IJobHandler { public class CacheRolePermissionJob extends IJobHandler implements InitializingBean {
@Autowired @Autowired
private RoleService roleService; private RoleService roleService;
@ -53,4 +54,10 @@ public class CacheRolePermissionJob extends IJobHandler {
return ReturnT.SUCCESS; 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.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -24,7 +25,7 @@ import java.util.stream.Collectors;
@Slf4j @Slf4j
@Component @Component
public class CacheSaasFeatureJob extends IJobHandler { public class CacheSaasFeatureJob extends IJobHandler implements InitializingBean {
@Autowired @Autowired
private SaasFeatureDao saasFeatureDao; private SaasFeatureDao saasFeatureDao;
@ -85,6 +86,12 @@ public class CacheSaasFeatureJob extends IJobHandler {
saasFeatureResourceService.refreshCache(param); saasFeatureResourceService.refreshCache(param);
} }
@Override
public void afterPropertiesSet() throws Exception {
// 启动时加载做localCache
execute(null);
}
@Data @Data
@Builder @Builder
@NoArgsConstructor @NoArgsConstructor

View File

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

View File

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