package com.gkhy.labRiskManage.application.sysAdmin.service.impl; import com.gkhy.labRiskManage.application.sysAdmin.convert.MenuItemAppConvert; import com.gkhy.labRiskManage.application.sysAdmin.model.dto.req.*; import com.gkhy.labRiskManage.application.sysAdmin.model.dto.resp.MenuItemAppDTO; import com.gkhy.labRiskManage.application.sysAdmin.service.MenuAppService; import com.gkhy.labRiskManage.commons.domain.Result; import com.gkhy.labRiskManage.commons.domain.SearchResult; import com.gkhy.labRiskManage.commons.enums.ResultCode; import com.gkhy.labRiskManage.commons.exception.BusinessException; import com.gkhy.labRiskManage.domain.account.model.bo.MenuItemBindRoleBO; import com.gkhy.labRiskManage.domain.account.model.bo.RoleBindMenuItemBO; import com.gkhy.labRiskManage.domain.account.service.RoleDomainService; import com.gkhy.labRiskManage.domain.account.service.RoleMenuDomainService; import com.gkhy.labRiskManage.domain.sysAdmin.model.bo.CreateNewMenuItemBO; import com.gkhy.labRiskManage.domain.sysAdmin.model.bo.ModifyMenuItemBO; import com.gkhy.labRiskManage.domain.sysAdmin.model.dto.MenuItemDomainDTO; import com.gkhy.labRiskManage.domain.sysAdmin.service.MenuDomainService; import org.springframework.beans.BeanUtils; 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.HashSet; import java.util.List; import java.util.Set; @Service public class MenuAppServiceImpl implements MenuAppService { @Autowired private MenuDomainService menuDomainService; @Autowired private RoleMenuDomainService roleMenuDomainService; @Autowired private MenuItemAppConvert menuItemAppConvert; @Autowired private RoleDomainService roleDomainService; @Override @Transactional public Result addMenuItem(NewMenuItemAppDTO dto) { Result result = new Result<>(); if(dto == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); if(dto.getName() == null || dto.getName().isEmpty()) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"菜单参数不能为空"); CreateNewMenuItemBO createBO = new CreateNewMenuItemBO(); if(dto.getParentId() != null){ MenuItemDomainDTO parant = menuDomainService.getMenuItemById(dto.getParentId()); if(parant == null) throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR_OBJECT_NOT_EXIST.getCode(), "上一级菜单不存在"); createBO.setParentId(dto.getParentId()); } createBO.setName(dto.getName()); createBO.setTitle(dto.getTitle()); createBO.setPath(dto.getPath()); createBO.setDescInfo(dto.getDescInfo()); createBO.setVisiable(dto.isVisiable()); createBO.setRedirect(dto.getRedirect()); createBO.setIcon(dto.getIcon()); createBO.setLink(dto.getLink()); if(createBO.getLink() == null) createBO.setLink(""); // createBO.setPriority(dto.getPriority()); createBO.setComponent(dto.getComponent()); createBO.setAliveable(dto.getAliveable()); createBO.setAffixable(dto.getAffixable()); createBO.setIframeable(dto.getIframeable()); createBO.setPublicable(dto.getPublicable()); createBO.setRoles(dto.getRoles()); MenuItemDomainDTO createResultDTO = menuDomainService.createNewMenuItem(createBO); if(createResultDTO == null) throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR.getCode(),"创建菜单项出错"); result.setSuccess(); return result; } @Override @Transactional public Result modifyMenuItem(AddAndUpdateMenuItemAppDTO dto) { Result result = new Result<>(); if(dto == null || dto.getMenuItemId() == null || dto.getMenuItemId() < 1) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); MenuItemDomainDTO orign = menuDomainService.getMenuItemById(dto.getMenuItemId()); if(orign == null) throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_OBJECT_NOT_EXIST.getCode(),"菜单项不存在"); List orignRoles = roleMenuDomainService.getBindRolesByMenuItemId(dto.getMenuItemId()); //检查是否发生变更 if(!checkMenuItemHasModified(orign,dto) && !checkBindRolesHashModified(orignRoles,dto.getRoles())){ throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR.getCode(),"菜单项配置未发生变化"); } //包装修改业务对象 ModifyMenuItemBO modifyBO = new ModifyMenuItemBO(); BeanUtils.copyProperties(dto,modifyBO); modifyBO.setRoles(dto.getRoles()); if(menuDomainService.modifyMenuItem(modifyBO) == false){ throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR.getCode(),"变更菜单项配置出错"); } result.setSuccess(); return result; } @Override @Transactional public Result deleteMenuItem(Long menuItemId) { if(menuItemId == null || menuItemId < 1) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); Result result = new Result<>(); if(menuDomainService.deleteMenuItem(menuItemId)) result.setSuccess(); else { result.setCode(ResultCode.BUSINESS_ERROR.getCode()); result.setMsg("删除菜单项出错"); } return result; } @Override public SearchResult getAllMenuItems() { SearchResult result = new SearchResult<>(); result.setSuccess(); List menuItemDomainDTOList = menuDomainService.getAllActiveMenuItems(); if(menuItemDomainDTOList == null || menuItemDomainDTOList.isEmpty()) result.setCount(0); else { //数据转换 List appDTOList = convertToMenuItemAppDtos(menuItemDomainDTOList); result.setData(appDTOList); result.setCount(appDTOList.size()); } return result; } private List convertToMenuItemAppDtos(List orignList){ if(orignList == null || orignList.isEmpty()) return null; List targetList = new ArrayList<>(); orignList.forEach(o -> { MenuItemAppDTO appDTO = menuItemAppConvert.toAppDto(o); List bindRoles = roleMenuDomainService.getBindRolesByMenuItemId(o.getId()); appDTO.setRoles(bindRoles); if(o.getSubMenuItemList() != null && !o.getSubMenuItemList().isEmpty()){ appDTO.setSubMenuItemList(convertToMenuItemAppDtos(o.getSubMenuItemList())); } targetList.add(appDTO); }); return targetList; } @Override @Transactional public Result roleBindMenu(RoleBindMenuAppDTO bindDTO) { Result result = new Result<>(); if(bindDTO == null || bindDTO.getRoleId() == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); RoleBindMenuItemBO bindBO = new RoleBindMenuItemBO(); BeanUtils.copyProperties(bindDTO,bindBO); boolean bindResult = roleMenuDomainService.roleBindMenu(bindBO); if(bindResult == true){ result.setSuccess(); }else { result.setCode(ResultCode.BUSINESS_ERROR.getCode()); result.setMsg("角色绑定菜单项出错"); } return result; } @Override public Result menuBindRole(MenuItemBindRoleAppDTO bindDTO) { Result result = new Result<>(); if(bindDTO == null || bindDTO.getMenuItemId() == null){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"参数不能为空"); } MenuItemBindRoleBO bindBO = new MenuItemBindRoleBO(); BeanUtils.copyProperties(bindDTO,bindBO); boolean bindResult = roleMenuDomainService.menuBindRole(bindBO); if(bindResult == true){ result.setSuccess(); }else { result.setCode(ResultCode.BUSINESS_ERROR.getCode()); result.setMsg("菜单项绑定角色出错"); } return result; } /** * 检查菜单项绑定的角色是否发生变化 * @param orign * @param now * @return */ private boolean checkBindRolesHashModified(List orign,List now){ if(orign == null && now == null) { return false; } if(orign == null && now != null && !now.isEmpty()) { return true; } if(orign == null && orign != null && now.size() == 0) { return false; } if(now == null && orign != null && !orign.isEmpty()) { return true; } if(now == null && orign != null && orign.size() == 0){ return false; } // if((orign == null || orign.isEmpty()) && now != null && !now.isEmpty()) { // return true; // } // if(orign != null && !orign.isEmpty() && (now == null || now.isEmpty())) { // return true; // } if(orign != null && now != null && orign.size() != now.size()) { return true; } Set set = new HashSet<>(); orign.forEach(id -> { set.add(id); }); for(int i=0;i< now.size();i++){ if(!set.contains(now.get(i))){ return true; } } return false; } /** * 检查菜单项是否发生变更 * @param orign * @param dto * @return */ private boolean checkMenuItemHasModified(MenuItemDomainDTO orign, AddAndUpdateMenuItemAppDTO dto){ if(checkStringHasChanged(orign.getName(),dto.getName())) return true; if(checkStringHasChanged(orign.getTitle(),dto.getTitle())) return true; if(checkStringHasChanged(orign.getDescInfo(),dto.getDescInfo())) return true; if(checkStringHasChanged(orign.getIcon(),dto.getIcon())) return true; if(checkStringHasChanged(orign.getPath(),dto.getPath())) return true; if(checkStringHasChanged(orign.getRedirect(),dto.getRedirect())) return true; if(checkStringHasChanged(orign.getLink(),dto.getLink())) return true; if(orign.getParentId() == null && dto.getParentId() != null) return true; if(orign.getParentId() != null && dto.getParentId() == null) return true; if(orign.getParentId() != null && dto.getParentId() != null && !orign.getParentId().equals(dto.getParentId())) return true; if(orign.getVisiable() != dto.isVisiable()) return true; if(orign.getPriority() != dto.getPriority()) return true; if(checkStringHasChanged(orign.getComponent(), dto.getComponent())) return true; if(orign.getAliveable() != dto.getAliveable()) return true; if(orign.getAffixable() != dto.getAffixable()) return true; if(orign.getIframeable() != dto.getIframeable()) return true; if(orign.getPublicable() != dto.getPublicable()) return true; return false; } /** * 检查对象字符串是否发生变化 * @param orign * @param now * @return */ private boolean checkStringHasChanged(String orign,String now){ if(orign == null && now == null) return false; if(orign == null && !now.isEmpty()) return true; if(orign != null && now == null) return true; if(orign != null && orign.isEmpty() && now != null && now.isEmpty()) return false; if(orign.equals(now)) return false; return true; } }