package com.aurora.service.impl; import com.aurora.model.dto.LabelOptionDTO; import com.aurora.model.dto.MenuDTO; import com.aurora.model.dto.UserMenuDTO; import com.aurora.entity.Menu; import com.aurora.entity.RoleMenu; import com.aurora.exception.BizException; import com.aurora.mapper.MenuMapper; import com.aurora.mapper.RoleMenuMapper; import com.aurora.service.MenuService; import com.aurora.util.BeanCopyUtil; import com.aurora.util.UserUtil; import com.aurora.model.vo.ConditionVO; import com.aurora.model.vo.IsHiddenVO; import com.aurora.model.vo.MenuVO; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; import java.util.stream.Collectors; import static com.aurora.constant.CommonConstant.COMPONENT; import static com.aurora.constant.CommonConstant.TRUE; @Service public class MenuServiceImpl extends ServiceImpl implements MenuService { @Autowired private MenuMapper menuMapper; @Autowired private RoleMenuMapper roleMenuMapper; @Override public List listMenus(ConditionVO conditionVO) { List menus = menuMapper.selectList(new LambdaQueryWrapper() .like(StringUtils.isNotBlank(conditionVO.getKeywords()), Menu::getName, conditionVO.getKeywords())); List catalogs = listCatalogs(menus); Map> childrenMap = getMenuMap(menus); List menuDTOs = catalogs.stream().map(item -> { MenuDTO menuDTO = BeanCopyUtil.copyObject(item, MenuDTO.class); List list = BeanCopyUtil.copyList(childrenMap.get(item.getId()), MenuDTO.class).stream() .sorted(Comparator.comparing(MenuDTO::getOrderNum)) .collect(Collectors.toList()); menuDTO.setChildren(list); childrenMap.remove(item.getId()); return menuDTO; }).sorted(Comparator.comparing(MenuDTO::getOrderNum)).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(childrenMap)) { List childrenList = new ArrayList<>(); childrenMap.values().forEach(childrenList::addAll); List childrenDTOList = childrenList.stream() .map(item -> BeanCopyUtil.copyObject(item, MenuDTO.class)) .sorted(Comparator.comparing(MenuDTO::getOrderNum)) .collect(Collectors.toList()); menuDTOs.addAll(childrenDTOList); } return menuDTOs; } @Transactional(rollbackFor = Exception.class) @Override public void saveOrUpdateMenu(MenuVO menuVO) { Menu menu = BeanCopyUtil.copyObject(menuVO, Menu.class); this.saveOrUpdate(menu); } @Override public void updateMenuIsHidden(IsHiddenVO isHiddenVO) { Menu menu = BeanCopyUtil.copyObject(isHiddenVO, Menu.class); menuMapper.updateById(menu); } @Override public void deleteMenu(Integer menuId) { Integer count = roleMenuMapper.selectCount(new LambdaQueryWrapper() .eq(RoleMenu::getMenuId, menuId)); if (count > 0) { throw new BizException("菜单下有角色关联"); } List menuIds = menuMapper.selectList(new LambdaQueryWrapper() .select(Menu::getId) .eq(Menu::getParentId, menuId)) .stream() .map(Menu::getId) .collect(Collectors.toList()); menuIds.add(menuId); menuMapper.deleteBatchIds(menuIds); } @Override public List listMenuOptions() { List menus = menuMapper.selectList(new LambdaQueryWrapper() .select(Menu::getId, Menu::getName, Menu::getParentId, Menu::getOrderNum)); List catalogs = listCatalogs(menus); Map> childrenMap = getMenuMap(menus); return catalogs.stream().map(item -> { List list = new ArrayList<>(); List children = childrenMap.get(item.getId()); if (CollectionUtils.isNotEmpty(children)) { list = children.stream() .sorted(Comparator.comparing(Menu::getOrderNum)) .map(menu -> LabelOptionDTO.builder() .id(menu.getId()) .label(menu.getName()) .build()) .collect(Collectors.toList()); } return LabelOptionDTO.builder() .id(item.getId()) .label(item.getName()) .children(list) .build(); }).collect(Collectors.toList()); } @Override public List listUserMenus() { List menus = menuMapper.listMenusByUserInfoId(UserUtil.getUserDetailsDTO().getUserInfoId()); List catalogs = listCatalogs(menus); Map> childrenMap = getMenuMap(menus); return convertUserMenuList(catalogs, childrenMap); } private List listCatalogs(List menus) { return menus.stream() .filter(item -> Objects.isNull(item.getParentId())) .sorted(Comparator.comparing(Menu::getOrderNum)) .collect(Collectors.toList()); } private Map> getMenuMap(List menus) { return menus.stream() .filter(item -> Objects.nonNull(item.getParentId())) .collect(Collectors.groupingBy(Menu::getParentId)); } private List convertUserMenuList(List catalogList, Map> childrenMap) { return catalogList.stream().map(item -> { UserMenuDTO userMenuDTO = new UserMenuDTO(); List list = new ArrayList<>(); List children = childrenMap.get(item.getId()); if (CollectionUtils.isNotEmpty(children)) { userMenuDTO = BeanCopyUtil.copyObject(item, UserMenuDTO.class); list = children.stream() .sorted(Comparator.comparing(Menu::getOrderNum)) .map(menu -> { UserMenuDTO dto = BeanCopyUtil.copyObject(menu, UserMenuDTO.class); dto.setHidden(menu.getIsHidden().equals(TRUE)); return dto; }) .collect(Collectors.toList()); } else { userMenuDTO.setPath(item.getPath()); userMenuDTO.setComponent(COMPONENT); list.add(UserMenuDTO.builder() .path("") .name(item.getName()) .icon(item.getIcon()) .component(item.getComponent()) .build()); } userMenuDTO.setHidden(item.getIsHidden().equals(TRUE)); userMenuDTO.setChildren(list); return userMenuDTO; }).collect(Collectors.toList()); } }