|  |  |  | 
|---|
|  |  |  | import com.moral.api.config.mybatis.wrapper.NullFilterWrapper; | 
|---|
|  |  |  | import com.moral.api.entity.Group; | 
|---|
|  |  |  | import com.moral.api.entity.Menu; | 
|---|
|  |  |  | import com.moral.api.mapper.GroupMenuMapper; | 
|---|
|  |  |  | 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.service.GroupService; | 
|---|
|  |  |  | import com.moral.api.service.MenuService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.moral.api.util.CompareFieldUtils; | 
|---|
|  |  |  | import com.moral.api.util.LogUtils; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.constant.ResponseCodeEnum; | 
|---|
|  |  |  | import com.moral.pojo.CompareFieldResult; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | 
|---|
|  |  |  | MenuMapper menuMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | GroupService groupService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | GroupMenuMapper groupMenuMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public MenuQueryDTO queryAllMenus() { | 
|---|
|  |  |  | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.ORGANIZATION_USER_NOT_EXIST.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //根据角色查询对应的所有菜单 | 
|---|
|  |  |  | List<Menu> allMenus = menuMapper.getMenusByGroupId(group.getId()); | 
|---|
|  |  |  | //将菜单转换为父子结构 | 
|---|
|  |  |  | combinationParentChildrenMenus(allMenus); | 
|---|
|  |  |  | //删除所有菜单只保留最底层菜单 | 
|---|
|  |  |  | List<Menu> menus = new ArrayList<>(); | 
|---|
|  |  |  | for (Menu menu : allMenus) { | 
|---|
|  |  |  | getNotParentMenu(menu, menus); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //根据角色查询拥有的所有菜单 | 
|---|
|  |  |  | List<Menu> ownMenus = menuMapper.getMenusByGroupId(group.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /*判断每个菜单是否有子菜单,如果有则不传递 | 
|---|
|  |  |  | * 前端无法解决父菜单选中,子菜单没有全部选中回显问题。由后端处理的代码。*/ | 
|---|
|  |  |  | removeMenuWithChildren(ownMenus); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //封装返回结果 | 
|---|
|  |  |  | List<MenuDTO> dtos = new ArrayList<>(); | 
|---|
|  |  |  | for (Menu menu : menus) { | 
|---|
|  |  |  | for (Menu menu : ownMenus) { | 
|---|
|  |  |  | MenuDTO menuDTO = new MenuDTO(); | 
|---|
|  |  |  | menuDTO.setMenu(menu); | 
|---|
|  |  |  | dtos.add(menuDTO); | 
|---|
|  |  |  | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.MENU_IS_EXIST.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //执行插入逻辑 | 
|---|
|  |  |  | menuMapper.insert(menu); | 
|---|
|  |  |  | //插入日志 | 
|---|
|  |  |  | insertLog(menu); | 
|---|
|  |  |  | //封装返回对象 | 
|---|
|  |  |  | dto.setMenu(menu); | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public MenuDTO deleteMenu(MenuDeleteForm form) { | 
|---|
|  |  |  | //创建返回对象 | 
|---|
|  |  |  | MenuDTO dto = new MenuDTO(); | 
|---|
|  |  |  | 
|---|
|  |  |  | //删除菜单 | 
|---|
|  |  |  | existMenu.setIsDelete(Constants.DELETE); | 
|---|
|  |  |  | menuMapper.updateById(existMenu); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //删除角色菜单关系表中对应的菜单 | 
|---|
|  |  |  | UpdateWrapper deleteGroupMenuWrapper = new UpdateWrapper(); | 
|---|
|  |  |  | List<Integer> allMenuIds = new ArrayList<>(); | 
|---|
|  |  |  | allMenuIds.add(id); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(childrenIds)) { | 
|---|
|  |  |  | allMenuIds.addAll(childrenIds); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | deleteGroupMenuWrapper.in("menu_id", allMenuIds); | 
|---|
|  |  |  | deleteGroupMenuWrapper.set("is_delete", Constants.DELETE); | 
|---|
|  |  |  | groupMenuMapper.update(null, deleteGroupMenuWrapper); | 
|---|
|  |  |  | //插入日志 | 
|---|
|  |  |  | StringBuilder content = new StringBuilder(); | 
|---|
|  |  |  | if (existMenu.getParentId().equals(0)) | 
|---|
|  |  |  | content.append("删除了父菜单:" + existMenu.getName() + ";"); | 
|---|
|  |  |  | else | 
|---|
|  |  |  | content.append("删除了子菜单:" + existMenu.getName() + ";"); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(childrenIds)) { | 
|---|
|  |  |  | for (Integer childrenId : childrenIds) { | 
|---|
|  |  |  | content.append("及其子菜单:" + menuMapper.selectById(childrenId).getName() + ";"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | LogUtils.saveOperationForManage(content.toString(), Constants.DELETE_OPERATE_TYPE); | 
|---|
|  |  |  | //封装返回对象 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | 
|---|
|  |  |  | menuMapper.updateById(menu); | 
|---|
|  |  |  | //获取更新后的对象 | 
|---|
|  |  |  | menu = menuMapper.selectById(menu.getId()); | 
|---|
|  |  |  | //插入日志 | 
|---|
|  |  |  | updateLog(oldMenu, menu); | 
|---|
|  |  |  | //封装返回对象 | 
|---|
|  |  |  | dto.setMenu(menu); | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 将传来的菜单集合封装成父子结构的集合 | 
|---|
|  |  |  | * @Param: [menus] | 
|---|
|  |  |  | * @return: java.util.List<com.moral.api.entity.Menu> | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/5/13 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void combinationParentChildrenMenus(List<Menu> menus){ | 
|---|
|  |  |  | * @Description: 将传来的菜单集合封装成父子结构的集合 | 
|---|
|  |  |  | * @Param: [menus] | 
|---|
|  |  |  | * @return: java.util.List<com.moral.api.entity.Menu> | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/5/13 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void combinationParentChildrenMenus(List<Menu> menus) { | 
|---|
|  |  |  | //组合menu父子结构 | 
|---|
|  |  |  | Map<Integer, Menu> menuMap = new HashMap<>(); | 
|---|
|  |  |  | for (Menu menu : menus) { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 获取menu菜单中的最底层菜单添加到menus中 | 
|---|
|  |  |  | * @Param: [menu, menus] | 
|---|
|  |  |  | * @Description: 移除含有子菜单的菜单 | 
|---|
|  |  |  | * @Param: [menus] | 
|---|
|  |  |  | * @return: java.util.List<com.moral.api.entity.Menu> | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/5/28 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void removeMenuWithChildren(List<Menu> menus) { | 
|---|
|  |  |  | //查询所有菜单 | 
|---|
|  |  |  | QueryWrapper wrapper = new QueryWrapper(); | 
|---|
|  |  |  | wrapper.eq("is_delete", Constants.NOT_DELETE); | 
|---|
|  |  |  | List<Menu> allMenus = menuMapper.selectList(wrapper); | 
|---|
|  |  |  | //组合menu父子结构 | 
|---|
|  |  |  | Map<Integer, Menu> menuMap = new HashMap<>(); | 
|---|
|  |  |  | for (Menu menu : allMenus) { | 
|---|
|  |  |  | menuMap.put(menu.getId(), menu); | 
|---|
|  |  |  | menu.setChildren(new ArrayList<>());//初始化集合 | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (Menu menu : allMenus) { | 
|---|
|  |  |  | putMenuToParentMenu(menuMap, menu); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //移除含有子菜单的菜单 | 
|---|
|  |  |  | menus.removeIf(new Predicate<Menu>() { | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public boolean test(Menu menu) { | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(menuMap.get(menu.getId()).getChildren())) | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 更新操作插入日志 | 
|---|
|  |  |  | * @Param: [oldMenu, newMenu] | 
|---|
|  |  |  | * @return: void | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/5/13 | 
|---|
|  |  |  | * @Date: 2021/8/24 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void getNotParentMenu(Menu menu, List<Menu> menus) { | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(menu.getChildren())) { | 
|---|
|  |  |  | menus.add(menu); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | List<Menu> children = menu.getChildren(); | 
|---|
|  |  |  | for (Menu child : children) { | 
|---|
|  |  |  | getNotParentMenu(child, menus); | 
|---|
|  |  |  | private void updateLog(Menu oldMenu, Menu newMenu) { | 
|---|
|  |  |  | List<CompareFieldResult> compareResult = CompareFieldUtils.compare(Menu.class, oldMenu, newMenu); | 
|---|
|  |  |  | for (CompareFieldResult result : compareResult) { | 
|---|
|  |  |  | //parentId做特殊处理 | 
|---|
|  |  |  | if (result.getFieldName().equals("parentId")) { | 
|---|
|  |  |  | //父菜单id转为菜单名称 | 
|---|
|  |  |  | String oldData = result.getOldData(); | 
|---|
|  |  |  | String newData = result.getNewData(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!oldData.equals("0")) | 
|---|
|  |  |  | oldData = menuMapper.selectById(Integer.parseInt(oldData)).getName(); | 
|---|
|  |  |  | else | 
|---|
|  |  |  | oldData = "null"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!newData.equals("0")) | 
|---|
|  |  |  | newData = menuMapper.selectById(Integer.parseInt(newData)).getName(); | 
|---|
|  |  |  | else | 
|---|
|  |  |  | newData = "null"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | result.setNewData(newData); | 
|---|
|  |  |  | result.setOldData(oldData); | 
|---|
|  |  |  | result.setFieldAnnoName("父菜单"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String content = CompareFieldUtils.resultsConvertContent(compareResult, "修改了前台菜单"); | 
|---|
|  |  |  | LogUtils.saveOperationForManage(content, Constants.UPDATE_OPERATE_TYPE); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 插入操作插入日志 | 
|---|
|  |  |  | * @Param: [menu] | 
|---|
|  |  |  | * @return: void | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/8/24 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void insertLog(Menu menu) { | 
|---|
|  |  |  | StringBuilder content = new StringBuilder(); | 
|---|
|  |  |  | //判断插入的是子菜单还是父菜单 | 
|---|
|  |  |  | if (menu.getParentId().equals(0)) { | 
|---|
|  |  |  | content.append("添加了前台父菜单;"); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | content.append("添加了前台子菜单;"); | 
|---|
|  |  |  | //根据父菜单id查询菜单名称 | 
|---|
|  |  |  | Menu parentMenu = menuMapper.selectById(menu.getParentId()); | 
|---|
|  |  |  | content.append("父菜单:" + parentMenu.getName() + ";"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | content.append("名称:" + menu.getName() + ";"); | 
|---|
|  |  |  | if (menu.getUrl() != null) | 
|---|
|  |  |  | content.append("url:" + menu.getUrl() + ";"); | 
|---|
|  |  |  | content.append("顺序:" + menu.getOrder() + ";"); | 
|---|
|  |  |  | LogUtils.saveOperationForManage(content.toString(), Constants.INSERT_OPERATE_TYPE); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|