155 lines
5.9 KiB
Java
155 lines
5.9 KiB
Java
|
|
package com.engine.salary.service.auth;
|
||
|
|
|
||
|
|
import cn.hutool.core.collection.CollUtil;
|
||
|
|
import com.engine.core.impl.Service;
|
||
|
|
import com.engine.salary.constant.SalaryDefaultTenantConstant;
|
||
|
|
import com.engine.salary.entity.auth.param.AuthDataSaveParam;
|
||
|
|
import com.engine.salary.entity.auth.po.*;
|
||
|
|
import com.engine.salary.enums.auth.DataLinkEnum;
|
||
|
|
import com.engine.salary.enums.auth.DataTargetTypeEnum;
|
||
|
|
import com.engine.salary.enums.auth.MemberTargetTypeEnum;
|
||
|
|
import com.engine.salary.exception.SalaryRunTimeException;
|
||
|
|
import com.engine.salary.mapper.auth.AuthDataMapper;
|
||
|
|
import com.engine.salary.mapper.auth.AuthRoleDataMapper;
|
||
|
|
import com.engine.salary.mapper.auth.AuthRoleEmpMapper;
|
||
|
|
import com.engine.salary.mapper.auth.AuthRoleMapper;
|
||
|
|
import com.engine.salary.util.SalaryEntityUtil;
|
||
|
|
import com.engine.salary.util.db.IdGenerator;
|
||
|
|
import com.engine.salary.util.db.MapperProxyFactory;
|
||
|
|
import com.google.common.collect.Lists;
|
||
|
|
|
||
|
|
import java.util.*;
|
||
|
|
import java.util.stream.Collectors;
|
||
|
|
|
||
|
|
public class AuthDataServiceImpl extends Service implements AuthDataService {
|
||
|
|
|
||
|
|
|
||
|
|
private AuthDataMapper getAuthDataMapper() {
|
||
|
|
return MapperProxyFactory.getProxy(AuthDataMapper.class);
|
||
|
|
}
|
||
|
|
|
||
|
|
private AuthRoleMapper getAuthRoleMapper() {
|
||
|
|
return MapperProxyFactory.getProxy(AuthRoleMapper.class);
|
||
|
|
}
|
||
|
|
|
||
|
|
private AuthRoleEmpMapper getAuthRoleEmpMapper() {
|
||
|
|
return MapperProxyFactory.getProxy(AuthRoleEmpMapper.class);
|
||
|
|
}
|
||
|
|
private AuthRoleDataMapper getAuthRoleDataMapper() {
|
||
|
|
return MapperProxyFactory.getProxy(AuthRoleDataMapper.class);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public List<AuthDataPO> list(Long roleId) {
|
||
|
|
AuthRolePO rolePO = getAuthRoleMapper().getById(roleId);
|
||
|
|
if (rolePO == null) {
|
||
|
|
throw new SalaryRunTimeException("角色不存在!");
|
||
|
|
}
|
||
|
|
return getAuthDataMapper().listSome(AuthDataPO.builder().roleId(roleId).build());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void save(AuthDataSaveParam param) {
|
||
|
|
Date now = new Date();
|
||
|
|
Long roleId = param.getRoleId();
|
||
|
|
AuthRolePO rolePO = getAuthRoleMapper().getById(roleId);
|
||
|
|
if (rolePO == null) {
|
||
|
|
throw new SalaryRunTimeException("角色不存在!");
|
||
|
|
}
|
||
|
|
|
||
|
|
AuthDataPO dataPO = AuthDataPO.builder()
|
||
|
|
.id(IdGenerator.generate())
|
||
|
|
.roleId(param.getRoleId())
|
||
|
|
.link(param.getLink().getValue())
|
||
|
|
.targetType(param.getTargetType().getValue())
|
||
|
|
.target(param.getTarget())
|
||
|
|
.targetName(param.getTargetName())
|
||
|
|
.sortedIndex(param.getSortedIndex())
|
||
|
|
.creator((long) user.getUID())
|
||
|
|
.createTime(now)
|
||
|
|
.updateTime(now)
|
||
|
|
.deleteType(0)
|
||
|
|
.tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
|
||
|
|
.build();
|
||
|
|
|
||
|
|
getAuthDataMapper().insertIgnoreNull(dataPO);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void sync(Long roleId) {
|
||
|
|
|
||
|
|
|
||
|
|
AuthRolePO rolePO = getAuthRoleMapper().getById(roleId);
|
||
|
|
if (rolePO == null) {
|
||
|
|
throw new SalaryRunTimeException("角色不存在!");
|
||
|
|
}
|
||
|
|
|
||
|
|
//1、获取成员
|
||
|
|
List<AuthMemberPO> members = getAuthMemberMapper().listSome(AuthMemberPO.builder().roleId(roleId).build());
|
||
|
|
Map<Integer, Set<String>> targetTypeMap = SalaryEntityUtil.group2Map(members, AuthMemberPO::getTargetType, AuthMemberPO::getTarget);
|
||
|
|
|
||
|
|
//2、获取人员
|
||
|
|
Set<Long> empIds = new HashSet<>();
|
||
|
|
for (Integer targetType : targetTypeMap.keySet()) {
|
||
|
|
Set<String> targetIds = targetTypeMap.get(targetType);
|
||
|
|
List<Long> ids = MemberTargetTypeEnum.parseByValue(targetType).getEmpIds(targetIds);
|
||
|
|
empIds.addAll(ids);
|
||
|
|
}
|
||
|
|
|
||
|
|
//3、更新成员关系
|
||
|
|
getAuthRoleEmpMapper().deleteByRoleId(roleId);
|
||
|
|
Date now = new Date();
|
||
|
|
List<AuthRoleEmpPO> roleEmpPOS = empIds.stream()
|
||
|
|
.map(empId -> AuthRoleEmpPO.builder()
|
||
|
|
.id(IdGenerator.generate())
|
||
|
|
.employeeId(empId)
|
||
|
|
.roleId(roleId)
|
||
|
|
.creator((long) user.getUID())
|
||
|
|
.createTime(now)
|
||
|
|
.updateTime(now)
|
||
|
|
.deleteType(0)
|
||
|
|
.tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
|
||
|
|
.build())
|
||
|
|
.collect(Collectors.toList());
|
||
|
|
if (CollUtil.isNotEmpty(roleEmpPOS)) {
|
||
|
|
List<List<AuthRoleEmpPO>> partition = Lists.partition(roleEmpPOS, 100);
|
||
|
|
partition.forEach(list -> getAuthRoleEmpMapper().batchInsert(list));
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
List<AuthDataPO> list = list(roleId);
|
||
|
|
|
||
|
|
Set<Long> ids = null;
|
||
|
|
for (int i = 0; i < list.size(); i++) {
|
||
|
|
AuthDataPO dataPO = list.get(i);
|
||
|
|
DataTargetTypeEnum dataTargetTypeEnum = DataTargetTypeEnum.parseByValue(dataPO.getTargetType());
|
||
|
|
DataLinkEnum dataLinkEnum = DataLinkEnum.parseByValue(dataPO.getLink());
|
||
|
|
Set<Long> empResult = dataTargetTypeEnum.getEmpIds(dataPO.getTarget());
|
||
|
|
if (i == 0) {
|
||
|
|
ids = empResult;
|
||
|
|
} else {
|
||
|
|
ids = dataLinkEnum.calculation(ids, empResult);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
getAuthDataMapper().deleteByRoleId(roleId);
|
||
|
|
|
||
|
|
List<AuthRoleDataPO> collect = ids.stream().map(empId -> AuthRoleDataPO.builder()
|
||
|
|
.id(IdGenerator.generate())
|
||
|
|
.employeeId(empId)
|
||
|
|
.roleId(roleId)
|
||
|
|
.creator((long) user.getUID())
|
||
|
|
.createTime(now)
|
||
|
|
.updateTime(now)
|
||
|
|
.deleteType(0)
|
||
|
|
.tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
|
||
|
|
.build())
|
||
|
|
.collect(Collectors.toList());
|
||
|
|
|
||
|
|
List<List<AuthRoleDataPO>> partition = Lists.partition(collect, 100);
|
||
|
|
partition.forEach(list -> getAuthRoleDataMapper().batchInsert(list));
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|