package com.gkhy.safePlatform.account.service.impl;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.gkhy.safePlatform.account.model.dto.req.MenuMetaReqDTO;
|
import com.gkhy.safePlatform.account.model.dto.req.MenuModReqDTO;
|
import com.gkhy.safePlatform.account.entity.user.*;
|
import com.gkhy.safePlatform.account.enums.MenuStatusEnum;
|
|
import java.util.ArrayList;
|
import java.util.Date;
|
|
import com.gkhy.safePlatform.account.model.dto.req.MenuAddReqDTO;
|
import com.gkhy.safePlatform.account.service.MenuService;
|
import com.gkhy.safePlatform.account.service.RedisService;
|
import com.gkhy.safePlatform.account.service.baseService.*;
|
import com.gkhy.safePlatform.account.utils.RedisUtils;
|
import com.gkhy.safePlatform.account.utils.TokenUtil;
|
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
|
import com.gkhy.safePlatform.commons.enums.E;
|
import com.gkhy.safePlatform.commons.enums.RedisKeyEnum;
|
import com.gkhy.safePlatform.commons.enums.ResultCodes;
|
import com.gkhy.safePlatform.commons.exception.AusinessException;
|
import com.gkhy.safePlatform.commons.exception.BusinessException;
|
import com.gkhy.safePlatform.commons.utils.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.List;
|
import java.util.concurrent.TimeUnit;
|
|
@Service("menuService")
|
public class MenuServiceImpl implements MenuService {
|
|
|
@Autowired
|
private MenuInfoService menuInfoService;
|
@Autowired
|
private ProjectInfoService projectInfoService;
|
@Autowired
|
private RoleInfoService roleInfoService;
|
@Autowired
|
private ProjectMenuInfoService projectMenuInfoService;
|
@Autowired
|
private RoleProjectInfoService roleProjectInfoService;
|
@Autowired
|
private RoleMenuInfoService roleMenuInfoService;
|
@Autowired
|
private TokenUtil tokenConfig;
|
@Autowired
|
private RedisService redisService;
|
|
@Override
|
@Transactional
|
public void addMenu(MenuAddReqDTO menuAddDto, ContextCacheUser currentUser) {
|
|
MenuMetaReqDTO meta = menuAddDto.getMeta();
|
if (meta == null) {
|
throw new AusinessException(E.DATA_PARAM_NULL,"属性为空");
|
}
|
if (StringUtils.isBlank(meta.getTitle())) {
|
throw new AusinessException(E.DATA_PARAM_NULL, "菜单名称不能为空");
|
}
|
if (StringUtils.isBlank(menuAddDto.getName())) {
|
throw new AusinessException(E.DATA_PARAM_NULL, "路由名称不能为空");
|
}
|
if (menuAddDto.getPriority() == null) {
|
throw new AusinessException(E.DATA_PARAM_NULL, "优先级不能为空");
|
}
|
if (StringUtils.isBlank(menuAddDto.getPath())) {
|
throw new AusinessException(E.DATA_PARAM_NULL, "路由不能为空");
|
}
|
if (StringUtils.isBlank(menuAddDto.getComponent())) {
|
throw new AusinessException(E.DATA_PARAM_NULL, "组件不能为空");
|
}
|
MenuInfo menuInfo;
|
// name 路由判断
|
String name = menuAddDto.getName().trim();
|
menuInfo = menuInfoService.getMenuInfoByName(name);
|
if (menuInfo != null) {
|
throw new AusinessException(E.DATA_DATABASE_EXIST, "路由已存在");
|
}
|
// path 路由地址判断
|
String path = menuAddDto.getPath().trim();
|
menuInfo = menuInfoService.getMenuInfoByPath(path);
|
if (menuInfo != null) {
|
throw new AusinessException(E.DATA_DATABASE_EXIST, "路由地址已存在");
|
}
|
// component 组件判断(组件根菜单可以重复,子菜单不可以重复)
|
String component = menuAddDto.getComponent().trim();
|
menuInfo = menuInfoService.getMenuInfoByComponent(component);
|
if (menuInfo != null) {
|
throw new AusinessException(E.DATA_DATABASE_EXIST, "组件已存在");
|
}
|
|
List<String> roleCodes = meta.getRoles();
|
// 校验角色是否存在
|
List<RoleInfo> roleInfos = roleInfoService.getRoleInfoByCodes(roleCodes);
|
|
Long projectId = menuAddDto.getProjectId();
|
ProjectInfo project = projectInfoService.getProjectById(projectId);
|
if (project == null) {
|
throw new BusinessException(ResultCodes.CLIENT_PROJECT_NOT_EXIST);
|
}
|
|
// 角色和项目是否绑定
|
for (RoleInfo role : roleInfos) {
|
RoleProjectInfo roleProjectInfo = roleProjectInfoService.getRoleProjectInfo(role.getId(), project.getId());
|
if (roleProjectInfo == null) {
|
throw new BusinessException(ResultCodes.CLIENT_ROLE_HAS_NO_PROJECT);
|
}
|
}
|
// 1.菜单插入
|
MenuInfo menuEntity = new MenuInfo();
|
menuEntity.setName(name);
|
menuEntity.setRedirect(menuAddDto.getRedirect());
|
menuEntity.setComponent(component);
|
menuEntity.setPath(path);
|
menuEntity.setPriority(menuAddDto.getPriority());
|
menuEntity.setDescription(menuAddDto.getDescription());
|
menuEntity.setTitle(meta.getTitle());
|
menuEntity.setLinkEnable(meta.getIsLink());
|
menuEntity.setHideEnable(meta.getIsHide());
|
menuEntity.setKeepAliveEnable(meta.getIsKeepAlive());
|
menuEntity.setAffixEnable(meta.getIsAffix());
|
menuEntity.setIframeEnable(meta.getIsIframe());
|
menuEntity.setIcon(meta.getIcon());
|
menuEntity.setStatus(MenuStatusEnum.ENABLED.getCode());
|
|
// 判断是否有父菜单
|
if (menuAddDto.getParentId() != null) {
|
MenuInfo parentMenu = menuInfoService.getMenuById(menuAddDto.getParentId());
|
if (parentMenu == null) {
|
throw new BusinessException(ResultCodes.CLIENT_MENU_PARENT_NOT_EXIST);
|
}
|
}
|
menuEntity.setParentId(menuAddDto.getParentId());
|
menuEntity.setPublicEnable(Boolean.FALSE);
|
menuEntity.setGmtCreate(new Date());
|
menuInfoService.saveMenu(menuEntity);
|
|
// 2.项目和菜单
|
ProjectMenuInfo projectMenuInfo = new ProjectMenuInfo();
|
projectMenuInfo.setProjectId(projectId);
|
projectMenuInfo.setMenuId(menuEntity.getId());
|
projectMenuInfoService.save(projectMenuInfo);
|
|
// 3.角色和菜单
|
List<RoleMenuInfo> roleMenus = new ArrayList<>();
|
RoleMenuInfo roleMenuInfo;
|
for (RoleInfo roleInfo : roleInfos) {
|
roleMenuInfo = new RoleMenuInfo();
|
roleMenuInfo.setRoleId(roleInfo.getId());
|
roleMenuInfo.setMenuId(menuEntity.getId());
|
roleMenus.add(roleMenuInfo);
|
}
|
roleMenuInfoService.saveRoleMenus(roleMenus);
|
|
// 4.redis更新所有菜单
|
List<MenuInfoDO> allMenu = menuInfoService.getAllMenu();
|
redisService.setCacheMenuAndExpireTime(RedisKeyEnum.AUTH_MENU.getKey(), allMenu, tokenConfig.getExpiration());
|
}
|
|
|
|
@Override
|
@Transactional
|
public void modMenu(MenuModReqDTO menuModDto, ContextCacheUser currentUser) {
|
|
if (menuModDto.getId() == null) {
|
throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
|
}
|
MenuInfo menu = menuInfoService.getMenuById(menuModDto.getId());
|
if (menu == null) {
|
throw new BusinessException(ResultCodes.CLIENT_MENU_NOT_EXIST);
|
}
|
|
MenuMetaReqDTO meta = menuModDto.getMeta();
|
if (meta == null) {
|
throw new AusinessException(E.DATA_PARAM_NULL,"属性为空");
|
}
|
if (StringUtils.isBlank(meta.getTitle())) {
|
throw new AusinessException(E.DATA_PARAM_NULL, "菜单名称不能为空");
|
}
|
if (menuModDto.getPriority() == null) {
|
throw new AusinessException(E.DATA_PARAM_NULL, "优先级不能为空");
|
}
|
if (StringUtils.isBlank(menuModDto.getName())) {
|
throw new AusinessException(E.DATA_PARAM_NULL, "路由名称不能为空");
|
}
|
if (StringUtils.isBlank(menuModDto.getPath())) {
|
throw new AusinessException(E.DATA_PARAM_NULL, "路由不能为空");
|
}
|
if (StringUtils.isBlank(menuModDto.getComponent())) {
|
throw new AusinessException(E.DATA_PARAM_NULL, "组件不能为空");
|
}
|
|
MenuInfo menuInfo;
|
// name 路由判断
|
String name = menuModDto.getName().trim();
|
menuInfo = menuInfoService.getMenuInfoByName(name);
|
if (menuInfo != null && !menuInfo.getId().equals(menu.getId())) {
|
throw new AusinessException(E.DATA_DATABASE_EXIST, "路由已存在");
|
}
|
// path 路由地址判断
|
String path = menuModDto.getPath().trim();
|
menuInfo = menuInfoService.getMenuInfoByPath(path);
|
if (menuInfo != null && !menuInfo.getId().equals(menu.getId())) {
|
throw new AusinessException(E.DATA_DATABASE_EXIST, "路由地址已存在");
|
}
|
// component 组件判断(组件根菜单可以重复,子菜单不可以重复)
|
String component = menuModDto.getComponent().trim();
|
menuInfo = menuInfoService.getMenuInfoByComponent(component);
|
if (menuInfo != null && !menuInfo.getId().equals(menu.getId())) {
|
throw new AusinessException(E.DATA_DATABASE_EXIST, "组件已存在");
|
}
|
|
|
// 判断是否有父菜单
|
if (menuModDto.getParentId() != null) {
|
MenuInfo parentMenu = menuInfoService.getMenuById(menuModDto.getParentId());
|
if (parentMenu == null) {
|
throw new BusinessException(ResultCodes.CLIENT_MENU_PARENT_NOT_EXIST);
|
}
|
}
|
List<String> roleCodes = meta.getRoles();
|
// 校验角色是否存在
|
List<RoleInfo> roleInfos = roleInfoService.getRoleInfoByCodes(roleCodes);
|
|
Long projectId = menuModDto.getProjectId();
|
ProjectInfo project = projectInfoService.getProjectById(projectId);
|
if (project == null) {
|
throw new BusinessException(ResultCodes.CLIENT_PROJECT_NOT_EXIST);
|
}
|
// 角色和项目是否绑定
|
for (RoleInfo role : roleInfos) {
|
RoleProjectInfo roleProjectInfo = roleProjectInfoService.getRoleProjectInfo(role.getId(), project.getId());
|
if (roleProjectInfo == null) {
|
throw new BusinessException(ResultCodes.CLIENT_ROLE_HAS_NO_PROJECT);
|
}
|
}
|
// 1.更新菜单
|
MenuInfo menuEntity = new MenuInfo();
|
menuEntity.setId(menu.getId());
|
menuEntity.setName(menuModDto.getName());
|
menuEntity.setRedirect(menuModDto.getRedirect());
|
menuEntity.setComponent(menuModDto.getComponent());
|
menuEntity.setPriority(menuModDto.getPriority());
|
menuEntity.setDescription(menuModDto.getDescription());
|
// meta
|
menuEntity.setTitle(meta.getTitle());
|
menuEntity.setLinkEnable(meta.getIsLink());
|
menuEntity.setHideEnable(meta.getIsHide());
|
menuEntity.setKeepAliveEnable(meta.getIsKeepAlive());
|
menuEntity.setAffixEnable(meta.getIsAffix());
|
menuEntity.setIframeEnable(meta.getIsIframe());
|
menuEntity.setIcon(meta.getIcon());
|
menuEntity.setPath(menuModDto.getPath());
|
menuEntity.setParentId(menuModDto.getParentId());
|
menuEntity.setGmtModified(new Date());
|
menuInfoService.updateMenu(menuEntity);
|
|
// 2.菜单项目
|
// 2.1解绑该菜单的所有项目
|
projectMenuInfoService.unbindProjectByMenuId(menu.getId());
|
// 2.2绑定当前菜单的所有角色
|
projectMenuInfoService.bindProjectWithMenu(menu.getId(), projectId);
|
|
// 3.角色菜单
|
// 3.1解绑菜单的角色
|
roleMenuInfoService.unbindRoleByMenuId(menu.getId());
|
// 3.2绑定菜单和角色
|
List<RoleMenuInfo> roleMenus = new ArrayList<>();
|
RoleMenuInfo roleMenuInfo;
|
for (RoleInfo roleInfo : roleInfos) {
|
roleMenuInfo = new RoleMenuInfo();
|
roleMenuInfo.setRoleId(roleInfo.getId());
|
roleMenuInfo.setMenuId(menu.getId());
|
roleMenus.add(roleMenuInfo);
|
}
|
roleMenuInfoService.saveRoleMenus(roleMenus);
|
|
// 4.redis更新所有菜单
|
List<MenuInfoDO> allMenu = menuInfoService.getAllMenu();
|
redisService.setCacheMenuAndExpireTime(RedisKeyEnum.AUTH_MENU.getKey(), allMenu, tokenConfig.getExpiration());
|
}
|
|
@Override
|
@Transactional
|
public void delMenu(Long id,ContextCacheUser currentUser) {
|
;
|
if (id == null) {
|
throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
|
}
|
MenuInfo menu = menuInfoService.getMenuById(id);
|
if (menu == null) {
|
throw new BusinessException(ResultCodes.CLIENT_MENU_NOT_EXIST);
|
}
|
assert menu.getStatus() != null;
|
MenuStatusEnum status = MenuStatusEnum.parse(menu.getStatus());
|
if (status != MenuStatusEnum.ENABLED) {
|
throw new BusinessException(ResultCodes.CLIENT_MENU_NOT_EXIST);
|
}
|
// 判断是否有子菜单,有不能删除,否则删除
|
Long subMenuCount = menuInfoService.countByParentId(menu.getId());
|
if (subMenuCount > 0) {
|
throw new AusinessException(E.NOT_DELETE, "当前菜单存在子菜单,请先删除子菜单");
|
}
|
// 1.删除菜单
|
MenuInfo menuEntity = new MenuInfo();
|
menuEntity.setId(id);
|
menuEntity.setStatus(MenuStatusEnum.ABANDONED.getCode());
|
menuEntity.setGmtModified(new Date());
|
menuInfoService.updateById(menuEntity);
|
// 2.项目和菜单解绑
|
projectMenuInfoService.unbindProjectByMenuId(id);
|
// 3.角色和菜单解绑
|
roleMenuInfoService.unbindRoleByMenuId(id);
|
// 4.redis更新所有菜单
|
List<MenuInfoDO> allMenu = menuInfoService.getAllMenu();
|
redisService.setCacheMenuAndExpireTime(RedisKeyEnum.AUTH_MENU.getKey(), allMenu, tokenConfig.getExpiration());
|
}
|
|
|
}
|