| | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.extra.spring.SpringUtil; |
| | | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleImportExcelVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleImportReqVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleImportRespVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RolePageReqVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleSaveReqVO; |
| | | import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; |
| | |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; |
| | | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| | | import static cn.iocoder.yudao.module.system.enums.LogRecordConstants.*; |
| | | |
| | |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public RoleImportRespVO importRoleList(List<RoleImportExcelVO> importRoles, RoleImportReqVO importReqVO) { |
| | | importRoles = filterList(importRoles, item -> StrUtil.isNotBlank(item.getName())); |
| | | if (CollUtil.isEmpty(importRoles)) { |
| | | throw exception(ROLE_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | RoleImportRespVO respVO = RoleImportRespVO.builder() |
| | | .createRoleNames(new ArrayList<>()) |
| | | .updateRoleNames(new ArrayList<>()) |
| | | .failureRoleNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importRoles.forEach(importRole -> { |
| | | try { |
| | | // 1. 判断是否存在(按名称或标识) |
| | | RoleDO existRole = roleMapper.selectByName(importRole.getName()); |
| | | if (existRole == null && StrUtil.isNotBlank(importRole.getCode())) { |
| | | existRole = roleMapper.selectByCode(importRole.getCode()); |
| | | } |
| | | if (existRole == null) { |
| | | // 2.1 新增 |
| | | validateRoleDuplicate(importRole.getName(), importRole.getCode(), null); |
| | | RoleDO role = BeanUtils.toBean(importRole, RoleDO.class) |
| | | .setType(RoleTypeEnum.CUSTOM.getType()) |
| | | .setStatus(ObjUtil.defaultIfNull(importRole.getStatus(), CommonStatusEnum.ENABLE.getStatus())) |
| | | .setDataScope(DataScopeEnum.ALL.getScope()); |
| | | if (role.getSort() == null) { |
| | | role.setSort(0); |
| | | } |
| | | roleMapper.insert(role); |
| | | respVO.getCreateRoleNames().add(importRole.getName()); |
| | | return; |
| | | } |
| | | // 2.2 已存在,判断是否允许更新 |
| | | if (!importReqVO.getUpdateSupport()) { |
| | | respVO.getFailureRoleNames().put(importRole.getName(), |
| | | StrUtil.format(ROLE_NAME_DUPLICATE.getMsg(), importRole.getName())); |
| | | return; |
| | | } |
| | | RoleDO updateRole = BeanUtils.toBean(importRole, RoleDO.class) |
| | | .setId(existRole.getId()); |
| | | roleMapper.updateById(updateRole); |
| | | respVO.getUpdateRoleNames().add(importRole.getName()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureRoleNames().put(importRole.getName(), ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | /** |
| | | * 获得自身的代理对象,解决 AOP 生效问题 |
| | | * |