package com.imed.costaccount.service.impl; import cn.hutool.core.collection.CollUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.imed.costaccount.common.constants.Constant; import com.imed.costaccount.common.exception.CostException; import com.imed.costaccount.common.util.PageUtils; import com.imed.costaccount.mapper.MenuMapper; import com.imed.costaccount.model.Menu; import com.imed.costaccount.model.User; import com.imed.costaccount.model.dto.MenuSaveDTO; import com.imed.costaccount.model.vo.MenuVO; import com.imed.costaccount.service.MenuService; import com.imed.costaccount.utils.BeanUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @Service("menuService") public class MenuServiceImpl extends ServiceImpl implements MenuService { /** * 保存菜单 * * @param menuSaveDTO {@link MenuSaveDTO} * @param user {@linkplain User} */ @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public void saveMenu(MenuSaveDTO menuSaveDTO, User user) { // 校验 this.check(menuSaveDTO); Menu menu = BeanUtil.convertObj(menuSaveDTO, Menu.class); menu.setHospId(user.getHospId()) .setModifyTime(System.currentTimeMillis()) .setModifyUserId(user.getId()); this.save(menu); } /** * 分页查询菜单列表 * * @param page 页码 * @param pageSize 每页数据大小 * @param user 当前登录用户 * @return 菜单伪列表 */ @Override public PageUtils selectList(Integer page, Integer pageSize, User user) { List list = this.list( new LambdaQueryWrapper().eq(Menu::getHospId, user.getHospId()).orderByAsc(Menu::getOrderNum) ); List menuVOS = BeanUtil.convertList(list, MenuVO.class); List roots = menuVOS.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList()); List treeVOs = new ArrayList<>(); for (MenuVO i : roots) { List menus = this.getMenuTree(i, menuVOS); treeVOs.addAll(menus); } return new PageUtils(list,0,0,0); } /** * 递归遍历 * @param menuVO * @param menuVOS * @return */ private List getMenuTree(MenuVO menuVO, List menuVOS) { List list = new ArrayList<>(); list.add(menuVO); List children = menuVO.getChildren(); if (CollUtil.isEmpty(children)) { children = new ArrayList<>(); } for (MenuVO j : menuVOS) { if (j.getParentId().equals(menuVO.getMenuId())) { children.add(j); } menuVO.setChildren(children); this.getMenuTree(j, menuVOS); } return list; } /** * 校验相关 菜单格式 * @param menuSaveDTO */ private void check(MenuSaveDTO menuSaveDTO) { //上级菜单类型 int parentType = Constant.MenuType.CATALOG.getValue(); if (menuSaveDTO.getParentId() != 0) { Menu parentMenu = this.getById(menuSaveDTO.getParentId()); parentType = parentMenu.getType(); } // 如果当前是 目录、菜单 if (menuSaveDTO.getType() == Constant.MenuType.CATALOG.getValue() || menuSaveDTO.getType() == Constant.MenuType.MENU.getValue()) { if (parentType != Constant.MenuType.CATALOG.getValue()) { throw new CostException("上级菜单只能为目录类型"); } return; } // 如果当前是按钮 if (menuSaveDTO.getType() == Constant.MenuType.BUTTON.getValue()) { if (parentType != Constant.MenuType.MENU.getValue()) { throw new CostException("上级菜单只能为菜单类型"); } return; } } }