MenuServiceImpl.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package com.imed.costaccount.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.imed.costaccount.common.constants.Constant;
  6. import com.imed.costaccount.common.exception.CostException;
  7. import com.imed.costaccount.common.util.PageUtils;
  8. import com.imed.costaccount.mapper.MenuMapper;
  9. import com.imed.costaccount.model.Menu;
  10. import com.imed.costaccount.model.User;
  11. import com.imed.costaccount.model.dto.MenuSaveDTO;
  12. import com.imed.costaccount.model.vo.MenuVO;
  13. import com.imed.costaccount.service.MenuService;
  14. import com.imed.costaccount.utils.BeanUtil;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Propagation;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.stream.Collectors;
  21. @Service("menuService")
  22. public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
  23. /**
  24. * 保存菜单
  25. *
  26. * @param menuSaveDTO {@link MenuSaveDTO}
  27. * @param user {@linkplain User}
  28. */
  29. @Override
  30. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  31. public void saveMenu(MenuSaveDTO menuSaveDTO, User user) {
  32. // 校验
  33. this.check(menuSaveDTO);
  34. Menu menu = BeanUtil.convertObj(menuSaveDTO, Menu.class);
  35. menu.setHospId(user.getHospId())
  36. .setModifyTime(System.currentTimeMillis())
  37. .setModifyUserId(user.getId());
  38. this.save(menu);
  39. }
  40. /**
  41. * 分页查询菜单列表
  42. *
  43. * @param page 页码
  44. * @param pageSize 每页数据大小
  45. * @param user 当前登录用户
  46. * @return 菜单伪列表
  47. */
  48. @Override
  49. public PageUtils selectList(Integer page, Integer pageSize, User user) {
  50. List<Menu> list = this.list(
  51. new LambdaQueryWrapper<Menu>().eq(Menu::getHospId, user.getHospId()).orderByAsc(Menu::getOrderNum)
  52. );
  53. List<MenuVO> menuVOS = BeanUtil.convertList(list, MenuVO.class);
  54. List<MenuVO> roots = menuVOS.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());
  55. List<MenuVO> treeVOs = new ArrayList<>();
  56. for (MenuVO i : roots) {
  57. List<MenuVO> menus = this.getMenuTree(i, menuVOS);
  58. treeVOs.addAll(menus);
  59. }
  60. return new PageUtils(list,0,0,0);
  61. }
  62. /**
  63. * 递归遍历
  64. * @param menuVO
  65. * @param menuVOS
  66. * @return
  67. */
  68. private List<MenuVO> getMenuTree(MenuVO menuVO, List<MenuVO> menuVOS) {
  69. List<MenuVO> list = new ArrayList<>();
  70. list.add(menuVO);
  71. List<MenuVO> children = menuVO.getChildren();
  72. if (CollUtil.isEmpty(children)) {
  73. children = new ArrayList<>();
  74. }
  75. for (MenuVO j : menuVOS) {
  76. if (j.getParentId().equals(menuVO.getMenuId())) {
  77. children.add(j);
  78. }
  79. menuVO.setChildren(children);
  80. this.getMenuTree(j, menuVOS);
  81. }
  82. return list;
  83. }
  84. /**
  85. * 校验相关 菜单格式
  86. * @param menuSaveDTO
  87. */
  88. private void check(MenuSaveDTO menuSaveDTO) {
  89. //上级菜单类型
  90. int parentType = Constant.MenuType.CATALOG.getValue();
  91. if (menuSaveDTO.getParentId() != 0) {
  92. Menu parentMenu = this.getById(menuSaveDTO.getParentId());
  93. parentType = parentMenu.getType();
  94. }
  95. // 如果当前是 目录、菜单
  96. if (menuSaveDTO.getType() == Constant.MenuType.CATALOG.getValue() ||
  97. menuSaveDTO.getType() == Constant.MenuType.MENU.getValue()) {
  98. if (parentType != Constant.MenuType.CATALOG.getValue()) {
  99. throw new CostException("上级菜单只能为目录类型");
  100. }
  101. return;
  102. }
  103. // 如果当前是按钮
  104. if (menuSaveDTO.getType() == Constant.MenuType.BUTTON.getValue()) {
  105. if (parentType != Constant.MenuType.MENU.getValue()) {
  106. throw new CostException("上级菜单只能为菜单类型");
  107. }
  108. return;
  109. }
  110. }
  111. }