weaver-hrm-salary/src/com/engine/salary/service/auth/AuthRoleServiceImpl.java

241 lines
10 KiB
Java
Raw Normal View History

2024-07-26 11:35:17 +08:00
package com.engine.salary.service.auth;
2024-08-30 10:49:46 +08:00
import cn.hutool.core.collection.CollectionUtil;
2024-07-26 11:35:17 +08:00
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
2024-09-24 10:19:06 +08:00
import com.engine.hrmelog.entity.dto.LoggerContext;
import com.engine.salary.config.SalaryElogConfig;
2024-07-26 11:35:17 +08:00
import com.engine.salary.constant.SalaryDefaultTenantConstant;
2024-08-30 17:22:10 +08:00
import com.engine.salary.entity.auth.dto.AuthRoleDTO;
2024-09-02 10:22:42 +08:00
import com.engine.salary.entity.auth.dto.AuthRoleDataDTO;
import com.engine.salary.entity.auth.dto.AuthRoleEmpDTO;
import com.engine.salary.entity.auth.param.AuthRoleListQueryParam;
2024-07-26 11:35:17 +08:00
import com.engine.salary.entity.auth.param.AuthRoleSaveParam;
2024-09-02 10:22:42 +08:00
import com.engine.salary.entity.auth.po.AuthOptPO;
2024-08-30 17:22:10 +08:00
import com.engine.salary.entity.auth.po.AuthResourcePO;
2024-07-26 11:35:17 +08:00
import com.engine.salary.entity.auth.po.AuthRolePO;
2024-08-30 17:22:10 +08:00
import com.engine.salary.entity.salarysob.po.SalarySobPO;
2024-07-26 11:35:17 +08:00
import com.engine.salary.entity.taxagent.po.TaxAgentPO;
2024-09-24 10:19:06 +08:00
import com.engine.salary.enums.OperateTypeEnum;
2024-08-30 17:22:10 +08:00
import com.engine.salary.enums.auth.ResourceTargetTypeEnum;
2024-07-26 11:35:17 +08:00
import com.engine.salary.exception.SalaryRunTimeException;
2024-08-30 17:22:10 +08:00
import com.engine.salary.mapper.auth.AuthResourceMapper;
2024-07-26 11:35:17 +08:00
import com.engine.salary.mapper.auth.AuthRoleMapper;
2024-09-24 10:19:06 +08:00
import com.engine.salary.util.SalaryI18nUtil;
2024-07-26 11:35:17 +08:00
import com.engine.salary.util.db.IdGenerator;
import com.engine.salary.util.db.MapperProxyFactory;
2024-08-30 17:22:10 +08:00
import com.engine.salary.util.page.PageInfo;
2024-09-02 10:22:42 +08:00
import com.engine.salary.util.page.SalaryPageUtil;
2024-07-26 11:35:17 +08:00
import weaver.hrm.User;
2024-09-09 19:10:40 +08:00
import java.util.ArrayList;
2024-07-26 11:35:17 +08:00
import java.util.Collections;
import java.util.Date;
2024-08-01 18:04:09 +08:00
import java.util.List;
2024-09-02 10:22:42 +08:00
import java.util.stream.Collectors;
2024-07-26 11:35:17 +08:00
public class AuthRoleServiceImpl extends Service implements AuthRoleService {
private AuthRoleMapper getAuthRoleMapper() {
return MapperProxyFactory.getProxy(AuthRoleMapper.class);
}
2024-08-30 17:22:10 +08:00
private AuthResourceMapper getAuthResourceMapper() {
return MapperProxyFactory.getProxy(AuthResourceMapper.class);
2024-07-26 11:35:17 +08:00
}
2024-08-23 09:38:54 +08:00
private AuthMemberService getAuthMemberService(User user) {
return ServiceUtil.getService(AuthMemberServiceImpl.class, user);
}
private AuthOptService getAuthOptService(User user) {
return ServiceUtil.getService(AuthOptServiceImpl.class, user);
}
private AuthDataService getAuthDataService(User user) {
return ServiceUtil.getService(AuthDataServiceImpl.class, user);
2024-07-26 11:35:17 +08:00
}
2024-08-01 18:04:09 +08:00
@Override
2024-09-02 10:22:42 +08:00
public PageInfo<AuthRoleDTO> roleList(AuthRoleListQueryParam param) {
2024-09-24 09:50:46 +08:00
List<AuthRolePO> authRolePOS = getAuthRoleMapper().list(param);
2024-09-02 10:22:42 +08:00
int total = authRolePOS.size();
List<AuthRoleDTO> collect = SalaryPageUtil.subList(param.getCurrent(), param.getPageSize(), authRolePOS)
.stream().map(po -> {
Long roleId = po.getId();
List<AuthRoleEmpDTO> authRoleEmpDTOS = getAuthMemberService(user).listRoleEmp(roleId);
List<AuthOptPO> authOptPOS = getAuthOptService(user).listOpts(roleId);
List<AuthRoleDataDTO> authRoleDataDTOS = getAuthDataService(user).listRoleData(roleId);
2024-09-09 11:13:10 +08:00
List<AuthResourcePO> authResources = getAuthResourceMapper().listSome(AuthResourcePO.builder().roleId(roleId).build());
2024-09-02 10:22:42 +08:00
return AuthRoleDTO.builder().id(roleId)
.name(po.getName())
2024-09-11 10:28:20 +08:00
.description(po.getDescription())
2024-09-02 10:22:42 +08:00
.members(authRoleEmpDTOS.size())
.opts(authOptPOS.size())
.datas(authRoleDataDTOS.size())
2024-09-09 11:13:10 +08:00
.resources(authResources.size())
2024-09-02 10:22:42 +08:00
.build();
}).collect(Collectors.toList());
PageInfo<AuthRoleDTO> pageInfo = SalaryPageUtil.buildPage(param.getCurrent(), param.getPageSize(), collect, AuthRoleDTO.class);
pageInfo.setTotal(total);
return pageInfo;
2024-08-01 18:04:09 +08:00
}
2024-07-26 11:35:17 +08:00
@Override
2024-09-09 19:10:40 +08:00
public AuthRoleDTO getRole(Long id) {
AuthRolePO po = getAuthRoleMapper().getById(id);
2024-09-12 11:50:18 +08:00
if (po == null){
throw new SalaryRunTimeException("业务线不存在");
}
2024-09-09 19:10:40 +08:00
Long roleId = po.getId();
List<AuthResourcePO> authResources = getAuthResourceMapper().listSome(AuthResourcePO.builder().roleId(roleId).build());
List<TaxAgentPO> taxAgentIds = new ArrayList<>();
List<SalarySobPO> sobIds = new ArrayList<>();
2024-09-12 16:32:19 +08:00
authResources.forEach(resource -> {
if (ResourceTargetTypeEnum.TAX_AGENT.getValue().equals(resource.getTargetType())) {
taxAgentIds.add(TaxAgentPO.builder().id(resource.getTarget()).name(resource.getTargetName()).build());
} else if (ResourceTargetTypeEnum.SOB.getValue().equals(resource.getTargetType())) {
sobIds.add(SalarySobPO.builder().id(resource.getTarget()).name(resource.getTargetName()).build());
}
});
2024-09-09 19:10:40 +08:00
return AuthRoleDTO.builder()
.id(roleId)
.name(po.getName())
.description(po.getDescription())
.taxAgentIds(taxAgentIds)
.sobIds(sobIds)
.build();
}
@Override
public Long saveRole(AuthRoleSaveParam param) {
2024-07-26 11:35:17 +08:00
Date now = new Date();
Long id = param.getId();
String name = param.getName();
2024-08-02 16:18:26 +08:00
String description = param.getDescription();
2024-07-26 11:35:17 +08:00
AuthRolePO po;
if (id == null) {
po = AuthRolePO.builder()
.id(IdGenerator.generate())
.name(name)
2024-08-02 16:18:26 +08:00
.description(description)
2024-07-26 11:35:17 +08:00
.creator((long) user.getUID())
.createTime(now)
.updateTime(now)
.deleteType(0)
.tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
.build();
getAuthRoleMapper().insertIgnoreNull(po);
2024-09-24 10:19:06 +08:00
LoggerContext<AuthRolePO> loggerContext = new LoggerContext<>();
loggerContext.setUser(user);
loggerContext.setTargetId(po.getId() + "");
loggerContext.setTargetName("业务线:" + name);
loggerContext.setOperateType(OperateTypeEnum.ADD.getValue());
loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(0, "新增业务线"));
loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(0, "新增业务线"));
loggerContext.setNewValues(po);
SalaryElogConfig.authLinkLoggerTemplate.write(loggerContext);
2024-07-26 11:35:17 +08:00
} else {
po = getAuthRoleMapper().getById(id);
if (po == null) {
2024-09-24 10:19:06 +08:00
throw new SalaryRunTimeException("业务线不存在!");
2024-07-26 11:35:17 +08:00
}
AuthRolePO newPo = AuthRolePO.builder()
.id(id)
.name(name)
2024-08-02 16:18:26 +08:00
.description(description)
2024-07-26 11:35:17 +08:00
.updateTime(now)
.build();
getAuthRoleMapper().updateIgnoreNull(newPo);
2024-09-24 10:19:06 +08:00
LoggerContext<AuthRolePO> loggerContext = new LoggerContext<>();
loggerContext.setUser(user);
loggerContext.setTargetId(po.getId() + "");
loggerContext.setTargetName("业务线:" + name);
loggerContext.setOperateType(OperateTypeEnum.UPDATE.getValue());
loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(0, "更新业务线"));
loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(0, "更新业务线"));
loggerContext.setOldValues(po);
loggerContext.setNewValues(newPo);
SalaryElogConfig.authLinkLoggerTemplate.write(loggerContext);
2024-07-26 11:35:17 +08:00
}
2024-08-30 17:22:10 +08:00
getAuthResourceMapper().deleteByRoleId(po.getId());
List<TaxAgentPO> taxAgents = param.getTaxAgentIds();
List<SalarySobPO> sobs = param.getSobIds();
2024-09-02 10:22:42 +08:00
if (CollectionUtil.isNotEmpty(taxAgents)) {
taxAgents.forEach(tax -> {
2024-08-30 17:22:10 +08:00
AuthResourcePO resourcePO = AuthResourcePO.builder()
.id(IdGenerator.generate())
.roleId(po.getId())
.target(tax.getId())
.targetType(ResourceTargetTypeEnum.TAX_AGENT.getValue())
.targetName(tax.getName())
.creator((long) user.getUID())
.createTime(now)
.updateTime(now)
.deleteType(0)
.tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
.build();
getAuthResourceMapper().insertIgnoreNull(resourcePO);
});
}
2024-09-02 10:22:42 +08:00
if (CollectionUtil.isNotEmpty(sobs)) {
sobs.forEach(sob -> {
2024-08-30 17:22:10 +08:00
AuthResourcePO resourcePO = AuthResourcePO.builder()
2024-08-30 10:49:46 +08:00
.id(IdGenerator.generate())
.roleId(po.getId())
2024-08-30 17:22:10 +08:00
.target(sob.getId())
.targetType(ResourceTargetTypeEnum.SOB.getValue())
.targetName(sob.getName())
2024-08-30 10:49:46 +08:00
.creator((long) user.getUID())
.createTime(now)
.updateTime(now)
.deleteType(0)
.tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
.build();
2024-08-30 17:22:10 +08:00
getAuthResourceMapper().insertIgnoreNull(resourcePO);
2024-08-30 10:49:46 +08:00
});
}
2024-07-26 11:35:17 +08:00
return po.getId();
}
@Override
2024-08-06 10:46:33 +08:00
public void deleteRole(List<Long> ids) {
2024-08-23 09:38:54 +08:00
ids.forEach(roleId -> {
AuthRolePO po = getAuthRoleMapper().getById(roleId);
2024-08-06 10:46:33 +08:00
if (po == null) {
2024-09-24 10:19:06 +08:00
throw new SalaryRunTimeException("业务线不存在!");
2024-08-06 10:46:33 +08:00
}
2024-07-26 11:35:17 +08:00
2024-08-30 17:22:10 +08:00
getAuthResourceMapper().deleteByRoleId(roleId);
2024-08-23 09:38:54 +08:00
getAuthMemberService(user).deleteByRoleId(roleId);
getAuthOptService(user).deleteByRoleId(roleId);
getAuthDataService(user).deleteByRoleId(roleId);
getAuthRoleMapper().deleteByIds(Collections.singleton(roleId));
2024-07-26 11:35:17 +08:00
2024-09-24 10:19:06 +08:00
LoggerContext<AuthRolePO> loggerContext = new LoggerContext<>();
loggerContext.setUser(user);
loggerContext.setTargetId(po.getId() + "");
loggerContext.setTargetName("业务线:" + po.getName());
loggerContext.setOperateType(OperateTypeEnum.DELETE.getValue());
loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(0, "删除业务线"));
loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(0, "删除业务线"));
loggerContext.setNewValues(po);
SalaryElogConfig.taxAgentLoggerTemplate.write(loggerContext);
2024-08-06 10:46:33 +08:00
});
2024-07-26 11:35:17 +08:00
}
}