blob: c4c701a50d66590679d74b7e5024b18159b020ce [file] [log] [blame]
wuchimedesa1bf2782025-03-27 15:08:54 +08001package com.example.g8backend.controller;
2
夜雨声烦f995a442025-05-13 18:43:29 +08003import com.example.g8backend.dto.ApiResponse;
夜雨声烦19976162025-04-22 01:29:11 +08004import com.example.g8backend.entity.Message;
wuchimedesa1bf2782025-03-27 15:08:54 +08005import com.example.g8backend.entity.User;
夜雨声烦45c25dd2025-05-20 11:59:03 +08006import com.example.g8backend.entity.UserSignin;
wuchimedes0ef96ab2025-06-08 19:26:56 +08007import com.example.g8backend.entity.UserStats;
wuchimedesa1bf2782025-03-27 15:08:54 +08008import com.example.g8backend.service.IUserService;
9import org.springframework.beans.factory.annotation.Autowired;
夜雨声烦45c25dd2025-05-20 11:59:03 +080010import org.springframework.format.annotation.DateTimeFormat;
wuchimedes223bfab2025-04-04 17:16:05 +080011import org.springframework.security.core.Authentication;
12import org.springframework.security.core.context.SecurityContextHolder;
wuchimedesa4c6c2b2025-06-07 22:42:11 +080013import org.springframework.security.core.parameters.P;
wuchimedesa1bf2782025-03-27 15:08:54 +080014import org.springframework.web.bind.annotation.*;
夜雨声烦35c9da92025-05-20 00:12:48 +080015import com.example.g8backend.service.ISigningService;
wuchimedesa1bf2782025-03-27 15:08:54 +080016
夜雨声烦45c25dd2025-05-20 11:59:03 +080017import java.time.LocalDate;
夜雨声烦19976162025-04-22 01:29:11 +080018import java.util.List;
夜雨声烦7e6eb382025-04-22 01:18:00 +080019import java.util.Map;
20
wuchimedesa1bf2782025-03-27 15:08:54 +080021@RestController
wuchimedes223bfab2025-04-04 17:16:05 +080022@RequestMapping("/user")
wuchimedesa1bf2782025-03-27 15:08:54 +080023public class UserController {
24
25 @Autowired
26 private IUserService userService;
27
wuchimedesa1bf2782025-03-27 15:08:54 +080028 @GetMapping
wuchimedesa4c6c2b2025-06-07 22:42:11 +080029 public ApiResponse<Map<String, Object>> getCurrentUserInfo() {
30 Long userId = getCurrentUserId();
31 return getUserInfo(userId);
32 }
33
34 @GetMapping("/{userId}")
35 public ApiResponse<Map<String, Object>> getOtherUserInfo(@PathVariable(required = false) Long userId){
36 return getUserInfo(userId);
37 }
38
39 @GetMapping("/{userId}/follow-status")
40 public ApiResponse<Map<String, Boolean>> getFollowStatus(@PathVariable Long userId) {
41 Long currentUserId = getCurrentUserId();
42 boolean isFollowing = userService.isFollowing(currentUserId, userId);
43 return ApiResponse.success(Map.of("isFollowing", isFollowing));
44 }
45
46 private ApiResponse<Map<String, Object>> getUserInfo(Long userId) {
wuchimedes223bfab2025-04-04 17:16:05 +080047 User user = userService.getById(userId);
wuchimedesa4c6c2b2025-06-07 22:42:11 +080048 if (user == null) {
49 return ApiResponse.error(404, "用户不存在");
50 }
51 user.setPassword(null);
52 user.setPasskey(null);
53
54 int followingCount = userService.getFollowingsCount(userId);
55 int followersCount = userService.getFollowersCount(userId);
56
wuchimedes0ef96ab2025-06-08 19:26:56 +080057 UserStats userStats = userService.getUserStats(userId);
58 double totalUpload = userStats != null ? userStats.getTotal_upload() : 0.0;
59 double totalDownload = userStats != null ? userStats.getTotal_download() : 0.0;
60
wuchimedesa4c6c2b2025-06-07 22:42:11 +080061 return ApiResponse.success(Map.of(
62 "userInfo", user,
63 "statistics", Map.of(
64 "followingCount", followingCount,
wuchimedes0ef96ab2025-06-08 19:26:56 +080065 "followersCount", followersCount,
66 "totalUpload", totalUpload,
67 "totalDownload", totalDownload
wuchimedesa4c6c2b2025-06-07 22:42:11 +080068 )
69 ));
wuchimedesa1bf2782025-03-27 15:08:54 +080070 }
夜雨声烦f995a442025-05-13 18:43:29 +080071
夜雨声烦19976162025-04-22 01:29:11 +080072 // ==================== 关注功能 ====================
夜雨声烦f995a442025-05-13 18:43:29 +080073
夜雨声烦7e6eb382025-04-22 01:18:00 +080074 @PostMapping("/follow/{userId}")
夜雨声烦f995a442025-05-13 18:43:29 +080075 public ApiResponse<Map<String, Boolean>> followUser(@PathVariable Long userId) {
夜雨声烦19976162025-04-22 01:29:11 +080076 Long currentUserId = getCurrentUserId();
77 boolean success = userService.followUser(currentUserId, userId);
夜雨声烦f995a442025-05-13 18:43:29 +080078 return ApiResponse.success(Map.of("success", success));
夜雨声烦7e6eb382025-04-22 01:18:00 +080079 }
80
夜雨声烦19976162025-04-22 01:29:11 +080081 @DeleteMapping("/follow/{userId}")
夜雨声烦f995a442025-05-13 18:43:29 +080082 public ApiResponse<Map<String, Boolean>> unfollowUser(@PathVariable Long userId) {
夜雨声烦19976162025-04-22 01:29:11 +080083 Long currentUserId = getCurrentUserId();
84 boolean success = userService.unfollowUser(currentUserId, userId);
夜雨声烦f995a442025-05-13 18:43:29 +080085 return ApiResponse.success(Map.of("success", success));
夜雨声烦19976162025-04-22 01:29:11 +080086 }
87
88 @GetMapping("/followings")
夜雨声烦f995a442025-05-13 18:43:29 +080089 public ApiResponse<List<User>> getFollowings() {
夜雨声烦19976162025-04-22 01:29:11 +080090 Long currentUserId = getCurrentUserId();
91 List<User> followings = userService.getFollowings(currentUserId);
夜雨声烦f995a442025-05-13 18:43:29 +080092 return ApiResponse.success(followings);
夜雨声烦19976162025-04-22 01:29:11 +080093 }
94
95 @GetMapping("/followers")
夜雨声烦f995a442025-05-13 18:43:29 +080096 public ApiResponse<List<User>> getFollowers() {
夜雨声烦19976162025-04-22 01:29:11 +080097 Long currentUserId = getCurrentUserId();
98 List<User> followers = userService.getFollowers(currentUserId);
夜雨声烦f995a442025-05-13 18:43:29 +080099 return ApiResponse.success(followers);
夜雨声烦19976162025-04-22 01:29:11 +0800100 }
101
102 // ==================== 私信功能 ====================
夜雨声烦f995a442025-05-13 18:43:29 +0800103
夜雨声烦7e6eb382025-04-22 01:18:00 +0800104 @PostMapping("/message/{receiverId}")
夜雨声烦f995a442025-05-13 18:43:29 +0800105 public ApiResponse<Map<String, Long>> sendMessage(
夜雨声烦7e6eb382025-04-22 01:18:00 +0800106 @PathVariable Long receiverId,
107 @RequestBody String content
108 ) {
夜雨声烦19976162025-04-22 01:29:11 +0800109 Long senderId = getCurrentUserId();
夜雨声烦7e6eb382025-04-22 01:18:00 +0800110 Long messageId = userService.sendMessage(senderId, receiverId, content);
夜雨声烦f995a442025-05-13 18:43:29 +0800111 return ApiResponse.success(Map.of("messageId", messageId));
夜雨声烦7e6eb382025-04-22 01:18:00 +0800112 }
夜雨声烦19976162025-04-22 01:29:11 +0800113
114 @GetMapping("/messages/{otherUserId}")
夜雨声烦f995a442025-05-13 18:43:29 +0800115 public ApiResponse<List<Message>> getMessages(@PathVariable Long otherUserId) {
夜雨声烦19976162025-04-22 01:29:11 +0800116 Long currentUserId = getCurrentUserId();
117 List<Message> messages = userService.getMessages(currentUserId, otherUserId);
夜雨声烦f995a442025-05-13 18:43:29 +0800118 return ApiResponse.success(messages);
夜雨声烦19976162025-04-22 01:29:11 +0800119 }
120
121 @GetMapping("/messages/history")
夜雨声烦f995a442025-05-13 18:43:29 +0800122 public ApiResponse<List<Message>> getMessageHistory() {
夜雨声烦19976162025-04-22 01:29:11 +0800123 Long currentUserId = getCurrentUserId();
124 List<Message> messages = userService.getMessageHistory(currentUserId);
夜雨声烦f995a442025-05-13 18:43:29 +0800125 return ApiResponse.success(messages);
夜雨声烦19976162025-04-22 01:29:11 +0800126 }
127
128 // ==================== 工具方法 ====================
129 private Long getCurrentUserId() {
130 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
131 return (Long) authentication.getPrincipal();
132 }
夜雨声烦35c9da92025-05-20 00:12:48 +0800133
134 @Autowired
135 private ISigningService signingService; // 修改接口名称
136
137 @PostMapping("/signin")
夜雨声烦45c25dd2025-05-20 11:59:03 +0800138 public ApiResponse<String> signIn() { // 修改返回类型为 ApiResponse
139 Long userId = getCurrentUserId();
夜雨声烦35c9da92025-05-20 00:12:48 +0800140 boolean success = signingService.signIn(userId);
夜雨声烦45c25dd2025-05-20 11:59:03 +0800141 String message = success ? "签到成功" : "今日已签到";
142 return ApiResponse.success(message);
143 }
144
145 // 新增获取时间段内的签到记录接口
146 @GetMapping("/signins")
147 public ApiResponse<List<UserSignin>> getSignins(
148 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
149 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
150 Long userId = getCurrentUserId();
151 List<UserSignin> signins = signingService.getSigninsByDateRange(userId, startDate, endDate);
152 return ApiResponse.success(signins);
夜雨声烦35c9da92025-05-20 00:12:48 +0800153 }
wuchimedesa1bf2782025-03-27 15:08:54 +0800154}