添加角色清洗逻辑

This commit is contained in:
陈维伟 2023-09-11 16:12:37 +08:00
parent d4d18740e0
commit 8409db6023
3 changed files with 173 additions and 0 deletions

View File

@ -70,6 +70,10 @@
<groupId>cn.axzo.basics</groupId>
<artifactId>basics-common</artifactId>
</dependency>
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -37,6 +37,42 @@ public class SaasRole extends BaseEntity<SaasRole> {
*/
private String roleType;
private Long workspaceId;
private Integer workspaceType;
private Long ownerOuId;
/**
* 废弃 20230911
*/
private Integer fitOuTypeBit;
/**
* 废弃 20230911
*/
private Integer fitOuNodeTypeBit;
/**
* 废弃 20230911
*/
private Long positionTemplateId;
/**
* 废弃 20230911
*/
private Long projectTeamManageRoleResourceId;
/**
* 废弃 20230911
*/
private Long fromPreRoleId;
/**
* 废弃 20230911
*/
private String jobCode;
/**
* 创建者
*/

View File

@ -0,0 +1,133 @@
package job;
import java.util.Date;
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.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.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
/**
* 成都重新推送job
*
* @author cn
* @version 1.0
* @description
* @date 2021/9/13 11:31
*/
@Component
@AllArgsConstructor
@Slf4j
public class OMSRoleJobHandler extends IJobHandler {
@Autowired
SaasRoleGroupDao roleGroupDao;
@Autowired
SaasRoleDao roleDao;
@Autowired
SaasPermissionGroupDao saasPermissionGroupDao;
@Autowired
SaasFeatureDao featureDao;
@Autowired
SaasRoleGroupRelationDao roleGroupRelationDao;
@Autowired
SaasRoleUserRelationDao roleUserRelationDao;
@Autowired
SaasPgroupRoleRelationDao pgroupRoleRelationDao;
@Autowired
SaasPgroupPermissionRelationDao pgroupPermissionRelationDao;
/**
* 清洗OMS角色相关数据(先通过SQL检查和清除脏数据要不然无法保证各个实体的关联关系)
*
* @param s
* @return
* @throws Exception
*/
@Transactional // 在一个事务里面做一起提交
@Override
@XxlJob("chengDuSubwayLogRetry")
public ReturnT<String> execute(String s) throws Exception {
// 创建角色分组
SaasRoleGroup roleGroup = new SaasRoleGroup();
roleGroup.setWorkspaceTypeCode("6");
roleGroup.setName("管理员");
roleGroupDao.save(roleGroup);
// 查询OMS的角色 workspaceType=6 OMS的角色
List<SaasRole> oldRole = roleDao.lambdaQuery().eq(SaasRole::getWorkspaceType, 6).list();
// 重置老角色多余的字段
oldRole.forEach(e -> {
e.setWorkspaceId(-1l);
e.setOwnerOuId(-1l);
e.setWorkspaceType(-1);
e.setFitOuTypeBit(-1);
e.setFitOuNodeTypeBit(-1);
e.setPositionTemplateId(-1l);
e.setProjectTeamManageRoleResourceId(-1l);
e.setFromPreRoleId(-1l);
e.setJobCode("");
});
roleDao.updateBatchById(oldRole);
// 保存角色分组关联关系
List<SaasRoleGroupRelation> roleGroupRelation = oldRole.stream().map(e -> {
SaasRoleGroupRelation saasRoleGroupRelation = new SaasRoleGroupRelation();
saasRoleGroupRelation.setRoleId(e.getId());
saasRoleGroupRelation.setSaasRoleGroupId(roleGroup.getId());
return saasRoleGroupRelation;
}).collect(Collectors.toList());
roleGroupRelationDao.saveBatch(roleGroupRelation);
// 查询角色关联的角色打包成新的权限集
oldRole.forEach(role -> {
List<SaasPgroupRoleRelation> pgroupRoleRelation = pgroupRoleRelationDao.lambdaQuery().eq(SaasPgroupRoleRelation::getRoleId, role.getId()).list();
List<SaasPermissionGroup> permissionGroup = saasPermissionGroupDao.lambdaQuery().in(BaseEntity::getId, pgroupRoleRelation.stream().map(SaasPgroupRoleRelation::getGroupId).collect(Collectors.toList())).list();
List<SaasPgroupPermissionRelation> pgroupPermissionRelation = pgroupPermissionRelationDao.lambdaQuery().in(SaasPgroupPermissionRelation::getGroupId, permissionGroup.stream().map(BaseEntity::getId).collect(Collectors.toList())).list();
List<SaasFeature> feature = featureDao.lambdaQuery().in(BaseEntity::getId, pgroupPermissionRelation.stream().map(SaasPgroupPermissionRelation::getFeatureId).collect(Collectors.toList())).list();
// 创建新的权限集
SaasPermissionGroup saasPermissionGroup = new SaasPermissionGroup();
saasPermissionGroup.setName("通用权限");
saasPermissionGroup.setDescription("");
saasPermissionGroup.setCreateBy(-1L);
saasPermissionGroup.setUpdateBy(-1L);
saasPermissionGroup.setType("feature");
saasPermissionGroup.setIsCommon(1);
saasPermissionGroupDao.save(saasPermissionGroup);
// 创建新的角色权限集关联关系
SaasPgroupRoleRelation saasPgroupRoleRelation = new SaasPgroupRoleRelation();
saasPgroupRoleRelation.setRoleId(role.getId());
saasPgroupRoleRelation.setGroupId(saasPermissionGroup.getId());
saasPgroupRoleRelation.setCreateBy(-1L);
saasPgroupRoleRelation.setUpdateBy(-1L);
pgroupRoleRelationDao.save(saasPgroupRoleRelation);
// 创建新的权限集权限关联关系
feature.forEach(e -> {
SaasPgroupPermissionRelation saasPgroupPermissionRelation = new SaasPgroupPermissionRelation();
saasPgroupPermissionRelation.setGroupId(saasPermissionGroup.getId());
saasPgroupPermissionRelation.setFeatureId(e.getId());
saasPgroupPermissionRelation.setCreateBy(-1L);
saasPgroupPermissionRelation.setUpdateBy(-1L);
pgroupPermissionRelationDao.save(saasPgroupPermissionRelation);
});
// 删除老的权限集权限关联关系
pgroupPermissionRelation.forEach(e -> e.setIsDelete(1l));
pgroupPermissionRelationDao.updateBatchById(pgroupPermissionRelation);
// 删除老的角色权限集关联关系
pgroupRoleRelation.forEach(e -> e.setIsDelete(1l));
pgroupRoleRelationDao.updateBatchById(pgroupRoleRelation);
});
return ReturnT.SUCCESS;
}
}