验证角色数据清洗

This commit is contained in:
陈维伟 2023-09-11 17:52:47 +08:00
parent 3766422f3b
commit 25b309a1a6
2 changed files with 61 additions and 26 deletions

View File

@ -1,11 +1,12 @@
package cn.axzo.tyr.server; package cn.axzo.tyr.server;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import cn.axzo.tyr.server.job.OMSRoleJobHandler;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
@ -21,22 +22,33 @@ public class TyrApplication {
ConfigurableApplicationContext run = SpringApplication.run(TyrApplication.class, args); ConfigurableApplicationContext run = SpringApplication.run(TyrApplication.class, args);
Environment env = run.getEnvironment(); Environment env = run.getEnvironment();
log.info( log.info(
"--------------------------------------------------------------------------------------------------------------------\n" + "--------------------------------------------------------------------------------------------------------------------\n" +
"Application 【{}】 is running on 【{}】 environment!\n" + "Application 【{}】 is running on 【{}】 environment!\n" +
"Api Local: \thttp://127.0.0.1:{}\n" + "Api Local: \thttp://127.0.0.1:{}\n" +
"Mysql: \t{}\t username:{}\n" + "Mysql: \t{}\t username:{}\n" +
"Redis: \t{}:{}\t database:{}\n" + "Redis: \t{}:{}\t database:{}\n" +
"RabbitMQ: \t{}\t username:{}", "RabbitMQ: \t{}\t username:{}",
env.getProperty("spring.application.name"), env.getProperty("spring.application.name"),
env.getProperty("spring.profiles.active"), env.getProperty("spring.profiles.active"),
env.getProperty("server.port"), env.getProperty("server.port"),
env.getProperty("spring.datasource.url"), env.getProperty("spring.datasource.url"),
env.getProperty("spring.datasource.username"), env.getProperty("spring.datasource.username"),
env.getProperty("spring.redis.host"), env.getProperty("spring.redis.host"),
env.getProperty("spring.redis.port"), env.getProperty("spring.redis.port"),
env.getProperty("spring.redis.database"), env.getProperty("spring.redis.database"),
env.getProperty("spring.rabbitmq.addresses"), env.getProperty("spring.rabbitmq.addresses"),
env.getProperty("spring.rabbitmq.username") + env.getProperty("spring.rabbitmq.username") +
"\n----------------------------------------------------------"); "\n----------------------------------------------------------");
// try {
// test();
// } catch (Exception e) {
// e.printStackTrace();
// }
} }
// public static void test() throws Exception {
// OMSRoleJobHandler executor = SpringUtil.getBean(OMSRoleJobHandler.class);
// executor.execute(null);
// }
} }

View File

@ -1,19 +1,16 @@
package job; package cn.axzo.tyr.server.job;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import cn.axzo.pokonyan.config.mybatisplus.BaseEntity; import cn.axzo.pokonyan.config.mybatisplus.BaseEntity;
import cn.axzo.tyr.client.model.DictTypeFiledEnum;
import cn.axzo.tyr.server.repository.entity.*; import cn.axzo.tyr.server.repository.entity.*;
import cn.axzo.tyr.server.repository.service.*; import cn.axzo.tyr.server.repository.service.*;
import cn.axzo.tyr.server.service.RoleService;
import cn.axzo.tyr.server.service.SaasRoleGroupService;
import com.xxl.job.core.biz.model.ReturnT; 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.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
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.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -60,7 +57,7 @@ public class OMSRoleJobHandler extends IJobHandler {
*/ */
@Transactional // 在一个事务里面做一起提交 @Transactional // 在一个事务里面做一起提交
@Override @Override
@XxlJob("chengDuSubwayLogRetry") @XxlJob("OMSRoleJobHandler")
public ReturnT<String> execute(String s) throws Exception { public ReturnT<String> execute(String s) throws Exception {
// 创建角色分组 // 创建角色分组
SaasRoleGroup roleGroup = new SaasRoleGroup(); SaasRoleGroup roleGroup = new SaasRoleGroup();
@ -68,7 +65,10 @@ public class OMSRoleJobHandler extends IJobHandler {
roleGroup.setName("管理员"); roleGroup.setName("管理员");
roleGroupDao.save(roleGroup); roleGroupDao.save(roleGroup);
// 查询OMS的角色 workspaceType=6 OMS的角色 // 查询OMS的角色 workspaceType=6 OMS的角色
List<SaasRole> oldRole = roleDao.lambdaQuery().eq(SaasRole::getWorkspaceType, 6).list(); List<SaasRole> oldRole = roleDao.lambdaQuery()
.eq(SaasRole::getWorkspaceType, 6)
.notIn(SaasRole::getRoleType,"super_admin")
.list();
// 重置老角色多余的字段 // 重置老角色多余的字段
oldRole.forEach(e -> { oldRole.forEach(e -> {
e.setWorkspaceId(-1l); e.setWorkspaceId(-1l);
@ -90,14 +90,37 @@ public class OMSRoleJobHandler extends IJobHandler {
return saasRoleGroupRelation; return saasRoleGroupRelation;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
roleGroupRelationDao.saveBatch(roleGroupRelation); roleGroupRelationDao.saveBatch(roleGroupRelation);
// 查询角色关联的角色打包成新的权限集 // 查询角色关联的角色打包成新的权限集
ArrayList<SaasPermissionGroup> deletePgroup = new ArrayList<>(); ArrayList<SaasPermissionGroup> deletePgroup = new ArrayList<>();
oldRole.forEach(role -> { oldRole.forEach(role -> {
List<SaasPgroupRoleRelation> pgroupRoleRelation = pgroupRoleRelationDao.lambdaQuery().eq(SaasPgroupRoleRelation::getRoleId, role.getId()).list(); List<SaasPgroupRoleRelation> pgroupRoleRelation = pgroupRoleRelationDao.lambdaQuery().eq(SaasPgroupRoleRelation::getRoleId, role.getId()).list();
if (CollectionUtils.isEmpty(pgroupRoleRelation)) {
return;
}
List<SaasPermissionGroup> permissionGroup = saasPermissionGroupDao.lambdaQuery().in(BaseEntity::getId, pgroupRoleRelation.stream().map(SaasPgroupRoleRelation::getGroupId).collect(Collectors.toList())).list(); List<SaasPermissionGroup> permissionGroup = saasPermissionGroupDao.lambdaQuery().in(BaseEntity::getId, pgroupRoleRelation.stream().map(SaasPgroupRoleRelation::getGroupId).collect(Collectors.toList())).list();
if (CollectionUtils.isEmpty(permissionGroup)) {
return;
}
List<SaasPgroupPermissionRelation> pgroupPermissionRelation = pgroupPermissionRelationDao.lambdaQuery().in(SaasPgroupPermissionRelation::getGroupId, permissionGroup.stream().map(BaseEntity::getId).collect(Collectors.toList())).list(); List<SaasPgroupPermissionRelation> pgroupPermissionRelation = pgroupPermissionRelationDao.lambdaQuery().in(SaasPgroupPermissionRelation::getGroupId, permissionGroup.stream().map(BaseEntity::getId).collect(Collectors.toList())).list();
if (CollectionUtils.isEmpty(pgroupPermissionRelation)) {
// 如果没查到权限则表示当前权限集是无意义的删掉
permissionGroup.forEach(e -> e.setIsDelete(e.getId()));
saasPermissionGroupDao.updateBatchById(permissionGroup);
// 删除角色权限集关联关系
pgroupRoleRelation.forEach(e -> e.setIsDelete(e.getId()));
pgroupRoleRelationDao.updateBatchById(pgroupRoleRelation);
return;
}
List<SaasFeature> feature = featureDao.lambdaQuery().in(BaseEntity::getId, pgroupPermissionRelation.stream().map(SaasPgroupPermissionRelation::getFeatureId).collect(Collectors.toList())).list(); List<SaasFeature> feature = featureDao.lambdaQuery().in(BaseEntity::getId, pgroupPermissionRelation.stream().map(SaasPgroupPermissionRelation::getFeatureId).collect(Collectors.toList())).list();
if (CollectionUtils.isEmpty(feature)) {
// 如果没查到权限则表示当前权限集是无意义的删掉
permissionGroup.forEach(e -> e.setIsDelete(e.getId()));
saasPermissionGroupDao.updateBatchById(permissionGroup);
// 删除角色权限集关联关系
pgroupRoleRelation.forEach(e -> e.setIsDelete(e.getId()));
pgroupRoleRelationDao.updateBatchById(pgroupRoleRelation);
return;
}
// 创建新的权限集 // 创建新的权限集
SaasPermissionGroup saasPermissionGroup = new SaasPermissionGroup(); SaasPermissionGroup saasPermissionGroup = new SaasPermissionGroup();
saasPermissionGroup.setName("通用权限"); saasPermissionGroup.setName("通用权限");