| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.moral.api.entity.ManageMenu; |
| | | import com.moral.api.entity.Menu; |
| | | import com.moral.api.mapper.MenuMapper; |
| | | import com.moral.api.pojo.dto.menu.MenuDTO; |
| | | import com.moral.api.pojo.dto.menu.MenuQueryDTO; |
| | | import com.moral.api.pojo.form.menu.MenuDeleteForm; |
| | | import com.moral.api.pojo.form.menu.MenuInsertForm; |
| | | import com.moral.api.service.MenuService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.ResponseCodeEnum; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.function.Predicate; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Service |
| | | public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService { |
| | | |
| | | @Autowired |
| | | MenuMapper menuMapper; |
| | | |
| | | @Override |
| | | public MenuQueryDTO queryAllMenu() { |
| | | //创建返回对象 |
| | | MenuQueryDTO dto = new MenuQueryDTO(); |
| | | //获取所有菜单 |
| | | List<Menu> menus = menuMapper.selectList(null); |
| | | //组合menu父子结构 |
| | | Map<Integer, Menu> menuMap = new HashMap<>(); |
| | | for (Menu menu : menus) { |
| | | menuMap.put(menu.getId(), menu); |
| | | menu.setChildren(new ArrayList<>());//初始化集合 |
| | | } |
| | | for (Menu menu : menus) { |
| | | combinationParentChildrenMenus(menuMap, menu); |
| | | } |
| | | //删除非跟菜单 |
| | | menus.removeIf(new Predicate<Menu>() { |
| | | @Override |
| | | public boolean test(Menu menu) { |
| | | if (menu.getParentId().equals(0)) |
| | | return false; |
| | | return true; |
| | | } |
| | | }); |
| | | //转换菜单为DTO |
| | | List<MenuDTO> dtos = new ArrayList<>(); |
| | | for (Menu menu : menus) { |
| | | MenuDTO menuDTO = new MenuDTO(); |
| | | menuDTO.setMenu(menu); |
| | | dtos.add(menuDTO); |
| | | } |
| | | dto.setDtos(dtos); |
| | | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); |
| | | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); |
| | | return dto; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public MenuDTO insertMenu(MenuInsertForm form) { |
| | | //创建返回对象 |
| | | MenuDTO dto = new MenuDTO(); |
| | | //取参 |
| | | Menu menu = form.formConvertEntity(); |
| | | //检查名字是否重复 |
| | | Menu existMenu = new Menu(); |
| | | existMenu.setName(menu.getName()); |
| | | existMenu.setIsDelete(Constants.NOT_DELETE); |
| | | QueryWrapper existWrapper = new QueryWrapper(); |
| | | existWrapper.setEntity(existMenu); |
| | | Menu existMenuResult = menuMapper.selectOne(existWrapper); |
| | | if (!ObjectUtils.isEmpty(existMenuResult)) { |
| | | dto.setCode(ResponseCodeEnum.MENU_IS_EXIST.getCode()); |
| | | dto.setMsg(ResponseCodeEnum.MENU_IS_EXIST.getMsg()); |
| | | return dto; |
| | | } |
| | | //执行插入逻辑 |
| | | menuMapper.insert(menu); |
| | | //封装返回对象 |
| | | dto.setMenu(menu); |
| | | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); |
| | | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); |
| | | return dto; |
| | | } |
| | | |
| | | @Override |
| | | public MenuDTO deleteMenu(MenuDeleteForm form) { |
| | | //创建返回对象 |
| | | MenuDTO dto = new MenuDTO(); |
| | | //取参 |
| | | Integer id = form.getId(); |
| | | //查询要删除的菜单 |
| | | Menu existMenu = menuMapper.selectById(id); |
| | | if (ObjectUtils.isEmpty(existMenu)) { |
| | | dto.setCode(ResponseCodeEnum.MENU_IS_NULL.getCode()); |
| | | dto.setMsg(ResponseCodeEnum.MENU_IS_NULL.getMsg()); |
| | | return dto; |
| | | } |
| | | //执行逻辑删除 |
| | | existMenu.setIsDelete(Constants.DELETE); |
| | | menuMapper.updateById(existMenu); |
| | | //删除所有子菜单 |
| | | List<Integer> childrenIds = getChildrenIdsByParentId(id); |
| | | UpdateWrapper wrapper = new UpdateWrapper(); |
| | | wrapper.in("id",childrenIds); |
| | | wrapper.set("is_delete",Constants.DELETE); |
| | | menuMapper.update(null,wrapper); |
| | | //封装返回对象 |
| | | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); |
| | | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); |
| | | dto.setMenu(existMenu); |
| | | return dto; |
| | | } |
| | | |
| | | private void combinationParentChildrenMenus(Map<Integer, Menu> menuMap, Menu menu) { |
| | | Integer parentId = menu.getParentId(); |
| | | Menu parentMenu = menuMap.get(parentId); |
| | | if (!ObjectUtils.isEmpty(parentMenu)) { |
| | | parentMenu.getChildren().add(menu); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @Description: 根据菜单的id获取封装好children的menu对象 |
| | | * @Param: [id] |
| | | * @return: com.moral.api.entity.Menu |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/4/26 |
| | | */ |
| | | private Menu getMenuAndChildrenById(Integer id){ |
| | | //获取所有菜单 |
| | | List<Menu> menus = menuMapper.selectList(null); |
| | | //组合menu父子结构 |
| | | Map<Integer, Menu> menuMap = new HashMap<>(); |
| | | for (Menu menu : menus) { |
| | | menuMap.put(menu.getId(), menu); |
| | | menu.setChildren(new ArrayList<>());//初始化集合 |
| | | } |
| | | for (Menu menu : menus) { |
| | | combinationParentChildrenMenus(menuMap, menu); |
| | | } |
| | | Menu menu = menuMap.get(id); |
| | | return menu; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 获取该菜单下的所有子菜单id集合 |
| | | * @Param: [id] |
| | | * @return: java.util.List<java.lang.Integer> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/4/26 |
| | | */ |
| | | private List<Integer> getChildrenIdsByParentId(Integer id){ |
| | | Menu parentMenu = getMenuAndChildrenById(id); |
| | | List<Menu> children = parentMenu.getChildren(); |
| | | List<Integer> childrenIds = new ArrayList<>(); |
| | | for (Menu child : children) { |
| | | recursiveAccess(child,childrenIds); |
| | | } |
| | | return childrenIds; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @Description: 递归获取菜单的id放入集合中 |
| | | * @Param: [menu, ids] |
| | | * @return: void |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/4/26 |
| | | */ |
| | | private void recursiveAccess(Menu menu,List<Integer> ids){ |
| | | ids.add(menu.getId()); |
| | | List<Menu> children = menu.getChildren(); |
| | | if(!ObjectUtils.isEmpty(children)){ |
| | | for (Menu child : children) { |
| | | recursiveAccess(child,ids); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | |