| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.moral.api.config.mybatis.wrapper.NullFilterWrapper; |
| | | import com.moral.api.entity.Group; |
| | | import com.moral.api.entity.Menu; |
| | | import com.moral.api.mapper.GroupMenuMapper; |
| | | import com.moral.api.mapper.MenuMapper; |
| | | import com.moral.api.pojo.dto.menu.MenuDTO; |
| | | import com.moral.api.pojo.dto.menu.MenuQueryDTO; |
| | |
| | | import com.moral.api.pojo.form.menu.MenuInsertForm; |
| | | import com.moral.api.pojo.form.menu.MenuQueryNamesForm; |
| | | import com.moral.api.pojo.form.menu.MenuUpdateForm; |
| | | import com.moral.api.service.GroupService; |
| | | import com.moral.api.service.MenuService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.constant.Constants; |
| | |
| | | |
| | | @Autowired |
| | | MenuMapper menuMapper; |
| | | @Autowired |
| | | GroupService groupService; |
| | | @Autowired |
| | | GroupMenuMapper groupMenuMapper; |
| | | |
| | | @Override |
| | | public MenuQueryDTO queryAllMenu() { |
| | | public MenuQueryDTO queryAllMenus() { |
| | | //创建返回对象 |
| | | MenuQueryDTO dto = new MenuQueryDTO(); |
| | | //获取所有菜单 |
| | | QueryWrapper wrapper = new QueryWrapper(); |
| | | wrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | List<Menu> menus = menuMapper.selectList(wrapper); |
| | | //组合menu父子结构 |
| | | Map<Integer, Menu> menuMap = new HashMap<>(); |
| | | for (Menu menu : menus) { |
| | | menuMap.put(menu.getId(), menu); |
| | | menu.setChildren(new ArrayList<>());//初始化集合 |
| | | } |
| | | for (Menu menu : menus) { |
| | | combinationParentChildrenMenus(menuMap, menu); |
| | | } |
| | | //删除非根菜单 |
| | | menus.removeIf(new Predicate<Menu>() { |
| | | @Override |
| | | public boolean test(Menu menu) { |
| | | if (menu.getParentId().equals(0)) |
| | | return false; |
| | | return true; |
| | | } |
| | | }); |
| | | //组装成父子结构 |
| | | combinationParentChildrenMenus(menus); |
| | | //转换菜单为DTO |
| | | List<MenuDTO> dtos = new ArrayList<>(); |
| | | for (Menu menu : menus) { |
| | | MenuDTO menuDTO = new MenuDTO(); |
| | | menuDTO.setMenu(menu); |
| | | dtos.add(menuDTO); |
| | | } |
| | | dto.setDtos(dtos); |
| | | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); |
| | | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); |
| | | return dto; |
| | | } |
| | | |
| | | @Override |
| | | public MenuQueryDTO queryAdminGroupMenusByOrganizationId(Integer organizationId) { |
| | | //创建返回对象 |
| | | MenuQueryDTO dto = new MenuQueryDTO(); |
| | | //查询组织admin角色 |
| | | Group group = groupService.queryAdminGroupByOrganizationId(organizationId); |
| | | if (ObjectUtils.isEmpty(group)) {//如果没有角色则证明组织还没有账号 |
| | | dto.setCode(ResponseCodeEnum.ORGANIZATION_USER_NOT_EXIST.getCode()); |
| | | dto.setMsg(ResponseCodeEnum.ORGANIZATION_USER_NOT_EXIST.getMsg()); |
| | | return dto; |
| | | } |
| | | //根据角色查询对应的所有菜单 |
| | | List<Menu> allMenus = menuMapper.getMenusByGroupId(group.getId()); |
| | | //将菜单转换为父子结构 |
| | | combinationParentChildrenMenus(allMenus); |
| | | //删除所有菜单只保留最底层菜单 |
| | | List<Menu> menus = new ArrayList<>(); |
| | | for (Menu menu : allMenus) { |
| | | getNotParentMenu(menu, menus); |
| | | } |
| | | //封装返回结果 |
| | | List<MenuDTO> dtos = new ArrayList<>(); |
| | | for (Menu menu : menus) { |
| | | MenuDTO menuDTO = new MenuDTO(); |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public MenuDTO deleteMenu(MenuDeleteForm form) { |
| | | //创建返回对象 |
| | | MenuDTO dto = new MenuDTO(); |
| | |
| | | wrapper.set("is_delete", Constants.DELETE); |
| | | menuMapper.update(null, wrapper); |
| | | } |
| | | //删除根菜单 |
| | | //删除菜单 |
| | | existMenu.setIsDelete(Constants.DELETE); |
| | | menuMapper.updateById(existMenu); |
| | | |
| | | //删除角色菜单关系表中对应的菜单 |
| | | UpdateWrapper deleteGroupMenuWrapper = new UpdateWrapper(); |
| | | List<Integer> allMenuIds = new ArrayList<>(); |
| | | allMenuIds.add(id); |
| | | allMenuIds.addAll(childrenIds); |
| | | deleteGroupMenuWrapper.in("menu_id",allMenuIds); |
| | | deleteGroupMenuWrapper.set("is_delete",Constants.DELETE); |
| | | groupMenuMapper.update(null,deleteGroupMenuWrapper); |
| | | //封装返回对象 |
| | | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); |
| | | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); |
| | |
| | | //查询结果 |
| | | NullFilterWrapper<Menu> wrapper = new NullFilterWrapper<>(); |
| | | wrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | wrapper.like("name",name); |
| | | wrapper.like("name", name); |
| | | List<Menu> menus = menuMapper.selectList(wrapper); |
| | | //过滤该id对应的所有子菜单 |
| | | List<Integer> childrenIds = getChildrenIdsByParentId(id);//获取所有子菜单id结合 |
| | |
| | | return dto; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @Description: 将传来的菜单集合封装成父子结构的集合 |
| | | * @Param: [menus] |
| | | * @return: java.util.List<com.moral.api.entity.Menu> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/5/13 |
| | | */ |
| | | public void combinationParentChildrenMenus(List<Menu> menus){ |
| | | //组合menu父子结构 |
| | | Map<Integer, Menu> menuMap = new HashMap<>(); |
| | | for (Menu menu : menus) { |
| | | menuMap.put(menu.getId(), menu); |
| | | menu.setChildren(new ArrayList<>());//初始化集合 |
| | | } |
| | | for (Menu menu : menus) { |
| | | putMenuToParentMenu(menuMap, menu); |
| | | } |
| | | //删除非根菜单 |
| | | menus.removeIf(new Predicate<Menu>() { |
| | | @Override |
| | | public boolean test(Menu menu) { |
| | | if (menu.getParentId().equals(0)) |
| | | return false; |
| | | return true; |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * @Description: 把传来的menu放到menuMap对应的父菜单中 |
| | | * @Param: [menuMap, menu] |
| | |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/5/6 |
| | | */ |
| | | private void combinationParentChildrenMenus(Map<Integer, Menu> menuMap, Menu menu) { |
| | | private void putMenuToParentMenu(Map<Integer, Menu> menuMap, Menu menu) { |
| | | Integer parentId = menu.getParentId(); |
| | | Menu parentMenu = menuMap.get(parentId); |
| | | if (!ObjectUtils.isEmpty(parentMenu)) { |
| | |
| | | menu.setChildren(new ArrayList<>());//初始化集合 |
| | | } |
| | | for (Menu menu : menus) { |
| | | combinationParentChildrenMenus(menuMap, menu); |
| | | putMenuToParentMenu(menuMap, menu); |
| | | } |
| | | Menu menu = menuMap.get(id); |
| | | return menu; |
| | |
| | | |
| | | |
| | | /** |
| | | * @Description: 递归获取菜单的id放入集合中 |
| | | * @Description: 递归获取菜单以及children的id放入集合中 |
| | | * @Param: [menu, ids] |
| | | * @return: void |
| | | * @Author: 陈凯裕 |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @Description: 获取menu菜单中的最底层菜单添加到menus中 |
| | | * @Param: [menu, menus] |
| | | * @return: void |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/5/13 |
| | | */ |
| | | private void getNotParentMenu(Menu menu, List<Menu> menus) { |
| | | if (ObjectUtils.isEmpty(menu.getChildren())) { |
| | | menus.add(menu); |
| | | } else { |
| | | List<Menu> children = menu.getChildren(); |
| | | for (Menu child : children) { |
| | | getNotParentMenu(child, menus); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |