package com.rongyichuang.user.resolver; import com.rongyichuang.auth.dto.LoginResponse.EmployeeInfo; import com.rongyichuang.auth.dto.LoginResponse.JudgeInfo; import com.rongyichuang.auth.dto.LoginResponse.PlayerInfo; import com.rongyichuang.common.util.UserContextUtil; import com.rongyichuang.employee.entity.Employee; import com.rongyichuang.employee.service.EmployeeService; import com.rongyichuang.judge.entity.Judge; import com.rongyichuang.judge.service.JudgeService; import com.rongyichuang.player.entity.Player; import com.rongyichuang.player.service.PlayerService; import com.rongyichuang.user.dto.response.UserProfile; import com.rongyichuang.user.dto.response.UserStats; import com.rongyichuang.user.dto.response.UserRegistration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.graphql.data.method.annotation.Argument; import org.springframework.graphql.data.method.annotation.QueryMapping; import org.springframework.stereotype.Controller; import java.util.ArrayList; import java.util.List; /** * 用户GraphQL解析器 */ @Controller public class UserResolver { private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(UserResolver.class); @Autowired private EmployeeService employeeService; @Autowired private JudgeService judgeService; @Autowired private PlayerService playerService; @Autowired private UserContextUtil userContextUtil; /** * 获取当前用户档案 */ @QueryMapping public UserProfile userProfile() { try { Long userId = userContextUtil.getCurrentUserId(); logger.debug("进入userProfile,解析到用户ID: {}", userId); if (userId == null) { throw new RuntimeException("用户未登录"); } UserProfile profile = new UserProfile(); profile.setId(userId.toString()); List roles = new ArrayList<>(); // 检查是否是员工 logger.debug("开始查询员工信息,userId={}", userId); Employee employee = employeeService.findByUserId(userId); logger.debug("员工查询结果: {}", employee != null ? ("id=" + employee.getId() + ", name=" + employee.getName()) : "无"); if (employee != null) { profile.setName(employee.getName()); profile.setPhone(employee.getPhone()); roles.add("EMPLOYEE"); EmployeeInfo employeeInfo = new EmployeeInfo( employee.getId(), employee.getName(), employee.getRoleId(), employee.getDescription() ); profile.setEmployee(employeeInfo); } // 检查是否是评委 logger.debug("开始查询评委信息,userId={}", userId); Judge judge = judgeService.findByUserId(userId); logger.debug("评委查询结果: {}", judge != null ? ("id=" + judge.getId() + ", name=" + judge.getName()) : "无"); if (judge != null) { if (profile.getName() == null) { profile.setName(judge.getName()); profile.setPhone(judge.getPhone()); } roles.add("JUDGE"); JudgeInfo judgeInfo = new JudgeInfo( judge.getId(), judge.getName(), judge.getTitle(), judge.getCompany(), judge.getDescription() ); profile.setJudge(judgeInfo); } // 检查是否是学员 logger.debug("开始查询学员信息,userId={}", userId); Player player = playerService.findByUserId(userId); logger.debug("学员查询结果: {}", player != null ? ("id=" + player.getId() + ", name=" + player.getName()) : "无"); if (player != null) { if (profile.getName() == null) { profile.setName(player.getName()); profile.setPhone(player.getPhone()); } roles.add("PLAYER"); PlayerInfo playerInfo = new PlayerInfo( player.getId(), player.getName(), player.getPhone(), player.getDescription() ); profile.setPlayer(playerInfo); } profile.setRoles(roles); logger.debug("userProfile构建完成,roles={}, name={}, phone={}", roles, profile.getName(), profile.getPhone()); profile.setCreatedAt(java.time.LocalDateTime.now().toString()); return profile; } catch (Exception e) { logger.error("获取用户档案失败", e); throw new RuntimeException("获取用户档案失败: " + e.getMessage(), e); } } /** * 获取用户统计数据 */ @QueryMapping public UserStats userStats() { try { Long userId = userContextUtil.getCurrentUserId(); if (userId == null) { return null; } UserStats stats = new UserStats(); stats.setTotalRegistrations(0); stats.setOngoingActivities(0); stats.setCompletedActivities(0); stats.setAwards(0); return stats; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 获取我的报名记录 */ @QueryMapping public List myRegistrations(@Argument Integer limit) { try { Long userId = userContextUtil.getCurrentUserId(); if (userId == null) { return new ArrayList<>(); } // 这里应该实现真正的报名记录查询逻辑 // 暂时返回空列表 return new ArrayList<>(); } catch (Exception e) { e.printStackTrace(); return new ArrayList<>(); } } }