lrj
昨天 dc643ba44fd2a426263015491268a0f0d6b4671d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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<String> 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<UserRegistration> 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<>();
        }
    }
}