| package com.xzx.gc.role.service;  | 
|   | 
| import com.xzx.gc.entity.CoreMenu;  | 
| import com.xzx.gc.role.mapper.CoreRoleMenuMapper;  | 
| import com.xzx.gc.role.mapper.MenuConsoleMapper;  | 
| import com.xzx.gc.role.rbac.tree.MenuItem;  | 
| import org.springframework.beans.factory.annotation.Autowired;  | 
| import org.springframework.stereotype.Service;  | 
| import org.springframework.transaction.annotation.Transactional;  | 
|   | 
| import java.util.ArrayList;  | 
| import java.util.List;  | 
|   | 
| @Service  | 
| @Transactional  | 
| public class MenuConsoleService {  | 
|     @Autowired  | 
|     MenuConsoleMapper menuDao;  | 
|     @Autowired  | 
|     CoreRoleMenuMapper roleMenuDao;  | 
|   | 
|     @Autowired  | 
|     CorePlatformService platformService;  | 
|     public Long saveMenu(CoreMenu menu) {  | 
|         CoreMenu query = new CoreMenu();  | 
|         query.setCode(menu.getCode());  | 
|         long queryCount = menuDao.selectCount(query);  | 
|         if (queryCount > 0) {  | 
|             //throw new PlatformException("菜单编码已存在");  | 
|             return -1L;  | 
|         }  | 
|         menuDao.insertUseGeneratedKeys(menu);  | 
|         platformService.clearMenuCache();  | 
|         return menu.getId();  | 
|     }  | 
|   | 
|     public void deleteMenu(Long menuId) {  | 
|         deleteMenuId(menuId);  | 
|     }  | 
|   | 
|     public void batchDeleteMenuId(List<Long> menuIds) {  | 
|         for (Long id : menuIds) {  | 
|             deleteMenuId(id);  | 
|         }  | 
|         platformService.clearMenuCache();  | 
|     }  | 
|   | 
|     public void updateMenu(CoreMenu menu) {  | 
|         menuDao.updateByPrimaryKey(menu);  | 
|         platformService.clearMenuCache();  | 
|     }  | 
|   | 
|     public CoreMenu getMenu(Long menuId) {  | 
|         CoreMenu menu = menuDao.selectByPrimaryKey(menuId);  | 
|         platformService.clearMenuCache();  | 
|         return menu;  | 
|     }  | 
|   | 
|   | 
|     private void deleteMenuId(Long menuId) {  | 
|         MenuItem root = platformService.buildMenu();  | 
|         MenuItem fun = root.findChild(menuId);  | 
|         if(fun!=null){  | 
|             List<MenuItem> all = fun.findAllItem();  | 
|             //也删除自身  | 
|             all.add(fun);  | 
|             realDeleteMenu(all);  | 
|         }else{  | 
|             this.menuDao.deleteByPrimaryKey(menuId);  | 
|         }  | 
|   | 
|   | 
|     }  | 
|   | 
|     private void realDeleteMenu(List<MenuItem> all) {  | 
|         List<Long> ids = new ArrayList<>(all.size());  | 
|         for (MenuItem item : all) {  | 
|             ids.add(item.getId());  | 
|             this.menuDao.deleteByPrimaryKey(item.getId());  | 
|         }  | 
|         //删除角色和菜单的关系  | 
|         roleMenuDao.deleteRoleMenu(ids);  | 
|   | 
|     }  | 
|   | 
|     public CoreMenu queryById(Long id) {  | 
|         return menuDao.selectByPrimaryKey(id);  | 
|     }  | 
| }  |