package com.gkhy.safePlatform.account.rpc.test; import com.gkhy.safePlatform.account.model.dto.resp.DepRespDTO; import com.gkhy.safePlatform.account.service.DepartmentService; import com.gkhy.safePlatform.commons.co.ContextCacheUser; import com.gkhy.safePlatform.commons.exception.AusinessException; import com.gkhy.safePlatform.commons.exception.BusinessException; import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.MenuMetaRPCRespDTO; import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.ProjectRPCRespDTO; import com.gkhy.safePlatform.account.model.dto.req.LoginReqDTO; import com.gkhy.safePlatform.account.model.dto.resp.MenuRespDTO; import com.gkhy.safePlatform.account.model.dto.resp.UserLoginRespDTO; import com.gkhy.safePlatform.account.entity.user.PermissionInfo; import com.gkhy.safePlatform.account.entity.user.RoleInfo; import com.gkhy.safePlatform.account.entity.user.UserInfo; import com.gkhy.safePlatform.account.rpc.apimodel.AccountAuthService; import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.*; import com.gkhy.safePlatform.account.service.AuthService; import com.gkhy.safePlatform.commons.enums.ResultCodes; import com.gkhy.safePlatform.commons.vo.ResultVO; import org.apache.dubbo.config.annotation.DubboService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @DubboService public class AccountAuthServiceProvider implements AccountAuthService { @Autowired private AuthService authService; @Autowired private DepartmentService departmentService; @Override public String sayName(String id) { return "rpc call "+id+" test"; } @Override public ResultVO> getUserPermissionByUserId (Long userId) { try { List codes = new ArrayList<>(); List permissionInfos = authService.getUserPermissionByUserId(userId); if (permissionInfos.size() > 0) { codes = permissionInfos.stream().map(PermissionInfo::getCode).collect(Collectors.toList()); } return new ResultVO<>(ResultCodes.OK, codes); } catch (AusinessException e) { return new ResultVO<>(e.getCode(), e.getMessage()); } catch (BusinessException e) { return new ResultVO<>(e.getCode(), e.getMessage()); } catch (Exception e) { ResultVO resultVO = new ResultVO<>(); resultVO.setCode(ResultCodes.SERVER_ERROR); return resultVO; } } @Override public ResultVO getUserRoleCodeByUserId(Long userId) { try { RoleInfo roleInfo = authService.getUserRoleByUserId(userId); return new ResultVO<>(ResultCodes.OK, roleInfo.getCode()); } catch (AusinessException e) { return new ResultVO<>(e.getCode(), e.getMessage()); } catch (BusinessException e) { return new ResultVO<>(e.getCode(), e.getMessage()); } catch (Exception e) { ResultVO resultVO = new ResultVO<>(); resultVO.setCode(ResultCodes.SERVER_ERROR); return resultVO; } } @Override public ResultVO getUserById(Long userId) { try { // 用户 UserInfo userInfo = authService.getUserByUserId(userId); UserRPCRespDTO userReqDTO = new UserRPCRespDTO(); BeanUtils.copyProperties(userInfo, userReqDTO); // 用户部门 if (userInfo.getDepId() != null) { DepRespDTO dep = departmentService.getEnableDepartmentInfoByDepId(userId, userInfo.getDepId()); if (dep != null) { UserDepRPCRespDTO userDepRPCRespDTO = new UserDepRPCRespDTO(); BeanUtils.copyProperties(dep, userDepRPCRespDTO); userReqDTO.setDepartment(userDepRPCRespDTO); } } userReqDTO.setEnterprise(new UserEnterpriseRPCRespDTO()); return new ResultVO<>(ResultCodes.OK, userReqDTO); } catch (AusinessException e) { return new ResultVO<>(e.getCode(), e.getMessage()); } catch (BusinessException e) { return new ResultVO<>(e.getCode(), e.getMessage()); } catch (Exception e) { ResultVO resultVO = new ResultVO<>(); resultVO.setCode(ResultCodes.SERVER_ERROR); return resultVO; } } @Override public ResultVO authLogin(String username, String password) { ResultVO result = new ResultVO<>(ResultCodes.OK); LoginReqDTO loginReqDTO = new LoginReqDTO(); loginReqDTO.setUsername(username); loginReqDTO.setPassword(password); try { UserLoginRespDTO userOnlineRespDTO = authService.authLogin(loginReqDTO); UserLoginRPCRespDTO userLoginRespDTO = new UserLoginRPCRespDTO(); BeanUtils.copyProperties(userOnlineRespDTO,userLoginRespDTO); result.setData(userLoginRespDTO); } catch (AusinessException e) { result.setCode(e.getCode()); result.setMsg(e.getMessage()); } catch (BusinessException e) { result.setCode(e.getCode()); result.setMsg(e.getMessage()); } catch (Exception e) { result.setCode(ResultCodes.SERVER_ERROR); } return result; } @Override public ResultVO> getMenu(ContextCacheUser currentUser, Long projectId) { ResultVO> result = new ResultVO<>(ResultCodes.OK); try { List sources = authService.getUserMenuTreeByUserIdAndProjectId(currentUser, projectId); // 重新赋值一遍 result.setData(this.convertTORPCModel(sources)); } catch (AusinessException e) { result.setCode(e.getCode()); result.setMsg(e.getMessage()); } catch (BusinessException e) { result.setCode(e.getCode()); result.setMsg(e.getMessage()); } catch (Exception e) { result.setCode(ResultCodes.SERVER_ERROR.getCode()); result.setMsg(ResultCodes.SERVER_ERROR.getDesc()); } return result; } private List convertTORPCModel(List sources) { List targets = new ArrayList<>(); for (MenuRespDTO source : sources) { MenuRPCRespDTO target = new MenuRPCRespDTO(); target.setId(source.getId()); target.setName(source.getName()); target.setPath(source.getPath()); target.setPriority(source.getPriority()); target.setDescription(source.getDescription()); target.setParentId(source.getParentId()); target.setRedirect(source.getRedirect()); target.setComponent(source.getComponent()); target.setStatus(source.getStatus()); target.setChildren(new ArrayList<>()); target.setPublic(source.getPublic()); target.setMenuSuperior(source.getMenuSuperior()); // meta MenuMetaRPCRespDTO meta; if (source.getMeta() != null) { meta = new MenuMetaRPCRespDTO(); meta.setIsAffix(source.getMeta().getIsAffix()); meta.setIsHide(source.getMeta().getIsHide()); meta.setIcon(source.getMeta().getIcon()); meta.setIsIframe(source.getMeta().getIsIframe()); meta.setIsKeepAlive(source.getMeta().getIsKeepAlive()); meta.setIsLink(source.getMeta().getIsLink()); meta.setRoles(source.getMeta().getRoles()); meta.setTitle(source.getMeta().getTitle()); target.setMeta(meta); } // project if (source.getProject() != null) { ProjectRPCRespDTO project = new ProjectRPCRespDTO(); project.setProjectCode(source.getProject().getProjectCode()); project.setProjectId(source.getProject().getProjectId()); project.setProjectName(source.getProject().getProjectName()); project.setStatus(source.getProject().getStatus()); target.setProject(project); target.setProjectId(source.getProject().getProjectId()); } if (source.getChildren() != null && source.getChildren().size() > 0) { List children = this.convertTORPCModel(source.getChildren()); target.setChildren(children); } targets.add(target); } return targets; } }