123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- 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<MenuMapper, Menu> 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<Menu> list = this.list(
- new LambdaQueryWrapper<Menu>().eq(Menu::getHospId, user.getHospId()).orderByAsc(Menu::getOrderNum)
- );
- List<MenuVO> menuVOS = BeanUtil.convertList(list, MenuVO.class);
- List<MenuVO> roots = menuVOS.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());
- List<MenuVO> treeVOs = new ArrayList<>();
- for (MenuVO i : roots) {
- List<MenuVO> menus = this.getMenuTree(i, menuVOS);
- treeVOs.addAll(menus);
- }
- return new PageUtils(list,0,0,0);
- }
- /**
- * 递归遍历
- * @param menuVO
- * @param menuVOS
- * @return
- */
- private List<MenuVO> getMenuTree(MenuVO menuVO, List<MenuVO> menuVOS) {
- List<MenuVO> list = new ArrayList<>();
- list.add(menuVO);
- List<MenuVO> 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;
- }
- }
- }
|