MenuServiceImpl.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.imed.costaccount.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.imed.costaccount.common.constants.Constant;
  7. import com.imed.costaccount.common.exception.CostException;
  8. import com.imed.costaccount.common.util.PageUtils;
  9. import com.imed.costaccount.mapper.MenuMapper;
  10. import com.imed.costaccount.model.Menu;
  11. import com.imed.costaccount.model.User;
  12. import com.imed.costaccount.model.dto.MenuEditDTO;
  13. import com.imed.costaccount.model.dto.MenuSaveDTO;
  14. import com.imed.costaccount.model.vo.MenuVO;
  15. import com.imed.costaccount.service.MenuService;
  16. import com.imed.costaccount.service.UserService;
  17. import com.imed.costaccount.utils.BeanUtil;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Propagation;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Objects;
  25. import java.util.stream.Collectors;
  26. @Slf4j
  27. @Service("menuService")
  28. public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
  29. private final UserService userService;
  30. public MenuServiceImpl(UserService userService) {
  31. this.userService = userService;
  32. }
  33. /**
  34. * 保存菜单
  35. *
  36. * @param menuSaveDTO {@link MenuSaveDTO}
  37. * @param user {@linkplain User}
  38. */
  39. @Override
  40. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  41. public void saveMenu(MenuSaveDTO menuSaveDTO, User user) {
  42. // 校验
  43. this.check(menuSaveDTO.getType(), menuSaveDTO.getParentId());
  44. Menu menu = BeanUtil.convertObj(menuSaveDTO, Menu.class);
  45. menu.setHospId(user.getHospId())
  46. .setModifyTime(System.currentTimeMillis())
  47. .setModifyUserId(user.getId());
  48. this.save(menu);
  49. }
  50. /**
  51. * 分页查询菜单列表
  52. *
  53. * @param page 页码
  54. * @param pageSize 每页数据大小
  55. * @param user 当前登录用户
  56. * @return 菜单伪列表
  57. */
  58. @Override
  59. public PageUtils selectList(Integer page, Integer pageSize, User user) {
  60. List<Menu> list = this.list(
  61. new LambdaQueryWrapper<Menu>().eq(Menu::getHospId, user.getHospId()).orderByAsc(Menu::getOrderNum)
  62. );
  63. List<MenuVO> menuVOS = list.stream().map(i -> {
  64. MenuVO menuVO = BeanUtil.convertObj(i, MenuVO.class);
  65. menuVO.setModifyTime(DateUtil.formatDateTime(DateUtil.date(i.getModifyTime())));
  66. menuVO.setModifyUserName(userService.getUsernameByIdAndHospId(user.getId(), user.getHospId()));
  67. return menuVO;
  68. }).collect(Collectors.toList());
  69. List<MenuVO> roots = menuVOS.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());
  70. List<MenuVO> treeVOs = new ArrayList<>();
  71. for (MenuVO i : roots) {
  72. List<MenuVO> menus = this.getMenuTree(i, menuVOS);
  73. treeVOs.addAll(menus);
  74. }
  75. return new PageUtils(treeVOs, 0, 0, 0);
  76. }
  77. /**
  78. * 编辑某个菜单
  79. *
  80. * @param menuDTO {@link MenuEditDTO}
  81. * @param user {@link User}
  82. */
  83. @Override
  84. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  85. public void updateMenu(MenuEditDTO menuDTO, User user) {
  86. this.check(menuDTO.getType(), menuDTO.getParentId());
  87. Menu byId = this.getById(menuDTO.getId());
  88. if (Objects.isNull(byId)) {
  89. throw new CostException(500, "选择的菜单已被移除");
  90. }
  91. BeanUtil.convertObj(menuDTO, byId);
  92. log.info("byId:{}", byId);
  93. this.updateById(byId);
  94. }
  95. /**
  96. * 递归遍历
  97. *
  98. * @param menuVO
  99. * @param menuVOS
  100. * @return
  101. */
  102. private List<MenuVO> getMenuTree(MenuVO menuVO, List<MenuVO> menuVOS) {
  103. List<MenuVO> list = new ArrayList<>();
  104. list.add(menuVO);
  105. for (MenuVO vo : menuVOS) {
  106. if (vo.getParentId().equals(menuVO.getMenuId())) {
  107. List<MenuVO> children = menuVO.getChildren();
  108. if (CollUtil.isEmpty(children)) {
  109. children = new ArrayList<>();
  110. }
  111. children.add(vo);
  112. menuVO.setChildren(children);
  113. this.getMenuTree(vo, menuVOS);
  114. }
  115. }
  116. return list;
  117. }
  118. /**
  119. * 校验相关 菜单格式
  120. *
  121. * @param parentId 父级id
  122. * @param type 菜单类型
  123. */
  124. private void check(Integer type, Long parentId) {
  125. //上级菜单类型
  126. int parentType = Constant.MenuType.CATALOG.getValue();
  127. if (parentId != 0L) {
  128. Menu parentMenu = this.getById(parentId);
  129. parentType = parentMenu.getType();
  130. }
  131. // 如果当前是 目录、菜单
  132. if (type == Constant.MenuType.CATALOG.getValue() ||
  133. type == Constant.MenuType.MENU.getValue()) {
  134. if (parentType != Constant.MenuType.CATALOG.getValue()) {
  135. throw new CostException("上级菜单只能为目录类型");
  136. }
  137. return;
  138. }
  139. // 如果当前是按钮
  140. if (type == Constant.MenuType.BUTTON.getValue()) {
  141. if (parentType != Constant.MenuType.MENU.getValue()) {
  142. throw new CostException("上级菜单只能为菜单类型");
  143. }
  144. }
  145. }
  146. }