| | |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuImportExcelVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuImportReqVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuImportRespVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuSaveVO; |
| | | import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; |
| | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| | | 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.dal.dataobject.permission.MenuDO.ID_ROOT; |
| | | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| | | |
| | |
| | | return menuMapper.selectByIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @CacheEvict(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, allEntries = true) |
| | | public MenuImportRespVO importMenuList(List<MenuImportExcelVO> importMenus, MenuImportReqVO importReqVO) { |
| | | importMenus = filterList(importMenus, item -> StrUtil.isNotBlank(item.getName())); |
| | | if (CollUtil.isEmpty(importMenus)) { |
| | | throw exception(MENU_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | MenuImportRespVO respVO = MenuImportRespVO.builder() |
| | | .createMenuNames(new ArrayList<>()) |
| | | .updateMenuNames(new ArrayList<>()) |
| | | .failureMenuNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importMenus.forEach(importMenu -> { |
| | | try { |
| | | // 1. 解析父菜单 |
| | | Long parentId = ID_ROOT; |
| | | if (StrUtil.isNotBlank(importMenu.getParentName())) { |
| | | MenuDO parent = menuMapper.selectByName(importMenu.getParentName()); |
| | | if (parent == null) { |
| | | throw exception(MENU_PARENT_NOT_EXISTS); |
| | | } |
| | | parentId = parent.getId(); |
| | | } |
| | | // 2. 校验父菜单合法(存在 + 目录/菜单) |
| | | validateParentMenu(parentId, null); |
| | | // 3. 菜单类型必填 |
| | | if (importMenu.getType() == null) { |
| | | throw exception(MENU_TYPE_NOT_EXISTS); |
| | | } |
| | | // 4. 判断是否存在(相同父菜单 + 名称) |
| | | MenuDO existMenu = menuMapper.selectByParentIdAndName(parentId, importMenu.getName()); |
| | | if (existMenu == null) { |
| | | // 4.1 新增 |
| | | validateMenuComponentName(importMenu.getComponentName(), null); |
| | | MenuDO menu = BeanUtils.toBean(importMenu, MenuDO.class).setParentId(parentId); |
| | | initMenuProperty(menu); |
| | | menuMapper.insert(menu); |
| | | respVO.getCreateMenuNames().add(importMenu.getName()); |
| | | return; |
| | | } |
| | | // 4.2 已存在,判断是否允许更新 |
| | | if (!importReqVO.getUpdateSupport()) { |
| | | respVO.getFailureMenuNames().put(importMenu.getName(), |
| | | StrUtil.format(MENU_NAME_DUPLICATE.getMsg(), importMenu.getName())); |
| | | return; |
| | | } |
| | | validateMenuComponentName(importMenu.getComponentName(), existMenu.getId()); |
| | | MenuDO updateMenu = BeanUtils.toBean(importMenu, MenuDO.class) |
| | | .setId(existMenu.getId()).setParentId(parentId); |
| | | initMenuProperty(updateMenu); |
| | | menuMapper.updateById(updateMenu); |
| | | respVO.getUpdateMenuNames().add(importMenu.getName()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureMenuNames().put(importMenu.getName(), ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | /** |
| | | * 校验父菜单是否合法 |
| | | * <p> |