| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.moral.api.entity.ManageMenu; |
| | | import com.moral.api.entity.ManageRole; |
| | | import com.moral.api.mapper.ManageMenuMapper; |
| | | import com.moral.api.mapper.ManageRoleMenuMapper; |
| | | import com.moral.api.service.ManageMenuService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | 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 javax.annotation.Resource; |
| | | 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 ManageMenuServiceImpl extends ServiceImpl<ManageMenuMapper, ManageMenu> implements ManageMenuService { |
| | | |
| | | @Autowired(required = false) |
| | | private ManageMenuMapper manageMenuMapper; |
| | | |
| | | @Autowired(required = false) |
| | | private ManageRoleMenuMapper manageRoleMenuMapper; |
| | | |
| | | @Override |
| | | @Transactional |
| | | public Map<String, Object> insertManageMenu(ManageMenu manageMenu) { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | if (manageMenu.getName() == null) { |
| | | resultMap.put("code", ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode()); |
| | | resultMap.put("msg", ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); |
| | | return resultMap; |
| | | } |
| | | if (manageMenuMapper.getManageMenuByName(manageMenu.getName()) != null) { |
| | | resultMap.put("code", ResponseCodeEnum.MENU_IS_EXPIRE.getCode()); |
| | | resultMap.put("msg", ResponseCodeEnum.MENU_IS_EXPIRE.getMsg()); |
| | | } else { |
| | | manageMenuMapper.insertOne(manageMenu); |
| | | resultMap.put("code", ResponseCodeEnum.SUCCESS.getCode()); |
| | | resultMap.put("msg", ResponseCodeEnum.SUCCESS.getMsg()); |
| | | } |
| | | return resultMap; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public Map<String, Object> updateManageMenu(Map map) { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | if (!map.containsKey("id")) { |
| | | resultMap.put("code", ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode()); |
| | | resultMap.put("msg", ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); |
| | | return resultMap; |
| | | } |
| | | if (manageMenuMapper.getManageMenuById(Integer.parseInt(map.get("id").toString())) == null) { |
| | | resultMap.put("code", ResponseCodeEnum.MENU_IS_NULL.getCode()); |
| | | resultMap.put("msg", ResponseCodeEnum.MENU_IS_NULL.getMsg()); |
| | | } else { |
| | | manageMenuMapper.updateManageMenuById(map); |
| | | resultMap.put("code", ResponseCodeEnum.SUCCESS.getCode()); |
| | | resultMap.put("msg", ResponseCodeEnum.SUCCESS.getMsg()); |
| | | } |
| | | return resultMap; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public Map<String, Object> deleteManageMenu(Map map) { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | if (!map.containsKey("id")) { |
| | | resultMap.put("code", ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode()); |
| | | resultMap.put("msg", ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); |
| | | return resultMap; |
| | | } |
| | | if (manageMenuMapper.getManageMenuById(Integer.parseInt(map.get("id").toString())) == null) { |
| | | resultMap.put("code", ResponseCodeEnum.MENU_IS_NULL.getCode()); |
| | | resultMap.put("msg", ResponseCodeEnum.MENU_IS_NULL.getMsg()); |
| | | } else { |
| | | Map deleteMap = new HashMap(); |
| | | int id = Integer.parseInt(map.get("id").toString()); |
| | | deleteMap.put("id", id); |
| | | deleteMap.put("is_delete", 1); |
| | | manageMenuMapper.updateManageMenuById(deleteMap); |
| | | Map role_menuDeleteMap = new HashMap(); |
| | | role_menuDeleteMap.put("menu_id", id); |
| | | manageRoleMenuMapper.updateDeleteStateByMenu_id(role_menuDeleteMap); |
| | | resultMap.put("code", ResponseCodeEnum.SUCCESS.getCode()); |
| | | resultMap.put("msg", ResponseCodeEnum.SUCCESS.getMsg()); |
| | | } |
| | | return resultMap; |
| | | } |
| | | |
| | | @Override |
| | | public List<ManageMenu> getAllWithPagingQuery(Map map) { |
| | | Map limitMap = new HashMap(); |
| | | limitMap.put("start", (Integer.parseInt(map.get("current").toString()) - 1) * Integer.parseInt(map.get("size").toString())); |
| | | limitMap.put("number", Integer.parseInt(map.get("size").toString())); |
| | | List<ManageMenu> manageMenus = manageMenuMapper.getDataWithPage(limitMap); |
| | | return manageMenus; |
| | | } |
| | | |
| | | @Override |
| | | public List<ManageMenu> getAllMenus() { |
| | | List<ManageMenu> allMenus = manageMenuMapper.getAll(); |
| | | if(ObjectUtils.isEmpty(allMenus)) |
| | | return null; |
| | | Map<Integer, ManageMenu> menusMap = new HashMap<>(); |
| | | for (ManageMenu menu : allMenus) { |
| | | menu.setChildren(new ArrayList<>());//初始化 |
| | | menusMap.put(menu.getId(), menu); |
| | | } |
| | | for (ManageMenu menu : allMenus) { |
| | | combinationParentChildrenMenus(menusMap, menu); |
| | | } |
| | | |
| | | //已经封装好父子菜单,去除parentId不是0的菜单,去除重复 |
| | | allMenus.removeIf(new Predicate<ManageMenu>() { |
| | | @Override |
| | | public boolean test(ManageMenu manageMenu) { |
| | | if(manageMenu.getParentId().equals(0)) |
| | | return false; |
| | | return true; |
| | | } |
| | | }); |
| | | return allMenus; |
| | | } |
| | | |
| | | @Override |
| | | public List<ManageMenu> getManageMenuByNameFuzzy(Map map) { |
| | | Map limitMap = new HashMap(); |
| | | limitMap.put("name", map.get("name")); |
| | | limitMap.put("start", (Integer.parseInt(map.get("current").toString()) - 1) * Integer.parseInt(map.get("size").toString())); |
| | | limitMap.put("number", Integer.parseInt(map.get("size").toString())); |
| | | List<ManageMenu> manageMenus = manageMenuMapper.getManageRoleByNameFuzzy(limitMap); |
| | | return manageMenus; |
| | | } |
| | | |
| | | @Override |
| | | public List<ManageMenu> getParentChildrenMenusByRoles(List<ManageRole> roles) { |
| | | List<ManageMenu> allMenus = manageMenuMapper.getAllMenusByRoles(roles); |
| | | if(ObjectUtils.isEmpty(allMenus)) |
| | | return null; |
| | | Map<Integer, ManageMenu> menusMap = new HashMap<>(); |
| | | for (ManageMenu menu : allMenus) { |
| | | menu.setChildren(new ArrayList<>());//初始化 |
| | | menusMap.put(menu.getId(), menu); |
| | | } |
| | | for (ManageMenu menu : allMenus) { |
| | | combinationParentChildrenMenus(menusMap, menu); |
| | | } |
| | | |
| | | //已经封装好父子菜单,去除parentId不是0的菜单,去除重复 |
| | | allMenus.removeIf(new Predicate<ManageMenu>() { |
| | | @Override |
| | | public boolean test(ManageMenu manageMenu) { |
| | | if(manageMenu.getParentId().equals(0)) |
| | | return false; |
| | | return true; |
| | | } |
| | | }); |
| | | return allMenus; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 组合多级菜单 |
| | | * @Param: [menuMap, menu]menuMap:key为menuid,value为menu |
| | | * @return: void |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/4/7 |
| | | */ |
| | | private void combinationParentChildrenMenus(Map<Integer, ManageMenu> menuMap, ManageMenu menu) { |
| | | Integer parentId = menu.getParentId(); |
| | | ManageMenu parentMenu = menuMap.get(parentId); |
| | | if (!ObjectUtils.isEmpty(parentMenu)) { |
| | | parentMenu.getChildren().add(menu); |
| | | } |
| | | } |
| | | } |