|
@@ -1,6 +1,7 @@
|
|
|
package com.imed.costaccount.service.impl;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.imed.costaccount.common.constants.Constant;
|
|
@@ -9,22 +10,31 @@ 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.MenuEditDTO;
|
|
|
import com.imed.costaccount.model.dto.MenuSaveDTO;
|
|
|
import com.imed.costaccount.model.vo.MenuVO;
|
|
|
import com.imed.costaccount.service.MenuService;
|
|
|
+import com.imed.costaccount.service.UserService;
|
|
|
import com.imed.costaccount.utils.BeanUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
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.Objects;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
-
|
|
|
+@Slf4j
|
|
|
@Service("menuService")
|
|
|
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
|
|
|
|
|
|
+ private final UserService userService;
|
|
|
+
|
|
|
+ public MenuServiceImpl(UserService userService) {
|
|
|
+ this.userService = userService;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 保存菜单
|
|
@@ -36,7 +46,7 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
|
|
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
|
|
|
public void saveMenu(MenuSaveDTO menuSaveDTO, User user) {
|
|
|
// 校验
|
|
|
- this.check(menuSaveDTO);
|
|
|
+ this.check(menuSaveDTO.getType(), menuSaveDTO.getParentId());
|
|
|
|
|
|
Menu menu = BeanUtil.convertObj(menuSaveDTO, Menu.class);
|
|
|
menu.setHospId(user.getHospId())
|
|
@@ -60,7 +70,14 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
|
|
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> menuVOS = list.stream().map(i -> {
|
|
|
+ MenuVO menuVO = BeanUtil.convertObj(i, MenuVO.class);
|
|
|
+ menuVO.setModifyTime(DateUtil.formatDateTime(DateUtil.date(i.getModifyTime())));
|
|
|
+ menuVO.setModifyUserName(userService.getUsernameByIdAndHospId(user.getId(), user.getHospId()));
|
|
|
+ return menuVO;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
List<MenuVO> roots = menuVOS.stream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());
|
|
|
List<MenuVO> treeVOs = new ArrayList<>();
|
|
|
for (MenuVO i : roots) {
|
|
@@ -68,11 +85,32 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
|
|
treeVOs.addAll(menus);
|
|
|
|
|
|
}
|
|
|
- return new PageUtils(list,0,0,0);
|
|
|
+ return new PageUtils(treeVOs, 0, 0, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑某个菜单
|
|
|
+ *
|
|
|
+ * @param menuDTO {@link MenuEditDTO}
|
|
|
+ * @param user {@link User}
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
|
|
|
+ public void updateMenu(MenuEditDTO menuDTO, User user) {
|
|
|
+ this.check(menuDTO.getType(), menuDTO.getParentId());
|
|
|
+
|
|
|
+ Menu byId = this.getById(menuDTO.getId());
|
|
|
+ if (Objects.isNull(byId)) {
|
|
|
+ throw new CostException(500, "选择的菜单已被移除");
|
|
|
+ }
|
|
|
+ BeanUtil.convertObj(menuDTO, byId);
|
|
|
+ log.info("byId:{}", byId);
|
|
|
+ this.updateById(byId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 递归遍历
|
|
|
+ *
|
|
|
* @param menuVO
|
|
|
* @param menuVOS
|
|
|
* @return
|
|
@@ -80,35 +118,38 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
|
|
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);
|
|
|
+ for (MenuVO vo : menuVOS) {
|
|
|
+ if (vo.getParentId().equals(menuVO.getMenuId())) {
|
|
|
+ List<MenuVO> children = menuVO.getChildren();
|
|
|
+ if (CollUtil.isEmpty(children)) {
|
|
|
+ children = new ArrayList<>();
|
|
|
+ }
|
|
|
+ children.add(vo);
|
|
|
+ menuVO.setChildren(children);
|
|
|
+ this.getMenuTree(vo, menuVOS);
|
|
|
}
|
|
|
- menuVO.setChildren(children);
|
|
|
- this.getMenuTree(j, menuVOS);
|
|
|
}
|
|
|
+
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 校验相关 菜单格式
|
|
|
- * @param menuSaveDTO
|
|
|
+ *
|
|
|
+ * @param parentId 父级id
|
|
|
+ * @param type 菜单类型
|
|
|
*/
|
|
|
- private void check(MenuSaveDTO menuSaveDTO) {
|
|
|
+ private void check(Integer type, Long parentId) {
|
|
|
//上级菜单类型
|
|
|
int parentType = Constant.MenuType.CATALOG.getValue();
|
|
|
- if (menuSaveDTO.getParentId() != 0) {
|
|
|
- Menu parentMenu = this.getById(menuSaveDTO.getParentId());
|
|
|
+ if (parentId != 0L) {
|
|
|
+ Menu parentMenu = this.getById(parentId);
|
|
|
parentType = parentMenu.getType();
|
|
|
}
|
|
|
|
|
|
// 如果当前是 目录、菜单
|
|
|
- if (menuSaveDTO.getType() == Constant.MenuType.CATALOG.getValue() ||
|
|
|
- menuSaveDTO.getType() == Constant.MenuType.MENU.getValue()) {
|
|
|
+ if (type == Constant.MenuType.CATALOG.getValue() ||
|
|
|
+ type == Constant.MenuType.MENU.getValue()) {
|
|
|
if (parentType != Constant.MenuType.CATALOG.getValue()) {
|
|
|
throw new CostException("上级菜单只能为目录类型");
|
|
|
}
|
|
@@ -116,11 +157,10 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
|
|
}
|
|
|
|
|
|
// 如果当前是按钮
|
|
|
- if (menuSaveDTO.getType() == Constant.MenuType.BUTTON.getValue()) {
|
|
|
+ if (type == Constant.MenuType.BUTTON.getValue()) {
|
|
|
if (parentType != Constant.MenuType.MENU.getValue()) {
|
|
|
throw new CostException("上级菜单只能为菜单类型");
|
|
|
}
|
|
|
- return;
|
|
|
}
|
|
|
}
|
|
|
}
|