blob: a41e4d781a74316661c1dba8be806b279fbf0c74 [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;
wuchimedesa1bf2782025-03-27 15:08:54 +08007import com.example.g8backend.service.IUserService;
8import org.springframework.beans.factory.annotation.Autowired;
夜雨声烦45c25dd2025-05-20 11:59:03 +08009import org.springframework.format.annotation.DateTimeFormat;
wuchimedes223bfab2025-04-04 17:16:05 +080010import org.springframework.security.core.Authentication;
11import org.springframework.security.core.context.SecurityContextHolder;
wuchimedesa4c6c2b2025-06-07 22:42:11 +080012import org.springframework.security.core.parameters.P;
wuchimedesa1bf2782025-03-27 15:08:54 +080013import org.springframework.web.bind.annotation.*;
夜雨声烦35c9da92025-05-20 00:12:48 +080014import com.example.g8backend.service.ISigningService;
wuchimedesa1bf2782025-03-27 15:08:54 +080015
夜雨声烦45c25dd2025-05-20 11:59:03 +080016import java.time.LocalDate;
夜雨声烦19976162025-04-22 01:29:11 +080017import java.util.List;
夜雨声烦7e6eb382025-04-22 01:18:00 +080018import java.util.Map;
19
wuchimedesa1bf2782025-03-27 15:08:54 +080020@RestController
wuchimedes223bfab2025-04-04 17:16:05 +080021@RequestMapping("/user")
wuchimedesa1bf2782025-03-27 15:08:54 +080022public class UserController {
23
24 @Autowired
25 private IUserService userService;
26
wuchimedesa1bf2782025-03-27 15:08:54 +080027 @GetMapping
wuchimedesa4c6c2b2025-06-07 22:42:11 +080028 public ApiResponse<Map<String, Object>> getCurrentUserInfo() {
29 Long userId = getCurrentUserId();
30 return getUserInfo(userId);
31 }
32
33 @GetMapping("/{userId}")
34 public ApiResponse<Map<String, Object>> getOtherUserInfo(@PathVariable(required = false) Long userId){
35 return getUserInfo(userId);
36 }
37
38 @GetMapping("/{userId}/follow-status")
39 public ApiResponse<Map<String, Boolean>> getFollowStatus(@PathVariable Long userId) {
40 Long currentUserId = getCurrentUserId();
41 boolean isFollowing = userService.isFollowing(currentUserId, userId);
42 return ApiResponse.success(Map.of("isFollowing", isFollowing));
43 }
44
45 private ApiResponse<Map<String, Object>> getUserInfo(Long userId) {
wuchimedes223bfab2025-04-04 17:16:05 +080046 User user = userService.getById(userId);
wuchimedesa4c6c2b2025-06-07 22:42:11 +080047 if (user == null) {
48 return ApiResponse.error(404, "用户不存在");
49 }
50 user.setPassword(null);
51 user.setPasskey(null);
52
53 int followingCount = userService.getFollowingsCount(userId);
54 int followersCount = userService.getFollowersCount(userId);
55
56 return ApiResponse.success(Map.of(
57 "userInfo", user,
58 "statistics", Map.of(
59 "followingCount", followingCount,
60 "followersCount", followersCount
61 )
62 ));
wuchimedesa1bf2782025-03-27 15:08:54 +080063 }
夜雨声烦f995a442025-05-13 18:43:29 +080064
夜雨声烦19976162025-04-22 01:29:11 +080065 // ==================== 关注功能 ====================
夜雨声烦f995a442025-05-13 18:43:29 +080066
夜雨声烦7e6eb382025-04-22 01:18:00 +080067 @PostMapping("/follow/{userId}")
夜雨声烦f995a442025-05-13 18:43:29 +080068 public ApiResponse<Map<String, Boolean>> followUser(@PathVariable Long userId) {
夜雨声烦19976162025-04-22 01:29:11 +080069 Long currentUserId = getCurrentUserId();
70 boolean success = userService.followUser(currentUserId, userId);
夜雨声烦f995a442025-05-13 18:43:29 +080071 return ApiResponse.success(Map.of("success", success));
夜雨声烦7e6eb382025-04-22 01:18:00 +080072 }
73
夜雨声烦19976162025-04-22 01:29:11 +080074 @DeleteMapping("/follow/{userId}")
夜雨声烦f995a442025-05-13 18:43:29 +080075 public ApiResponse<Map<String, Boolean>> unfollowUser(@PathVariable Long userId) {
夜雨声烦19976162025-04-22 01:29:11 +080076 Long currentUserId = getCurrentUserId();
77 boolean success = userService.unfollowUser(currentUserId, userId);
夜雨声烦f995a442025-05-13 18:43:29 +080078 return ApiResponse.success(Map.of("success", success));
夜雨声烦19976162025-04-22 01:29:11 +080079 }
80
81 @GetMapping("/followings")
夜雨声烦f995a442025-05-13 18:43:29 +080082 public ApiResponse<List<User>> getFollowings() {
夜雨声烦19976162025-04-22 01:29:11 +080083 Long currentUserId = getCurrentUserId();
84 List<User> followings = userService.getFollowings(currentUserId);
夜雨声烦f995a442025-05-13 18:43:29 +080085 return ApiResponse.success(followings);
夜雨声烦19976162025-04-22 01:29:11 +080086 }
87
88 @GetMapping("/followers")
夜雨声烦f995a442025-05-13 18:43:29 +080089 public ApiResponse<List<User>> getFollowers() {
夜雨声烦19976162025-04-22 01:29:11 +080090 Long currentUserId = getCurrentUserId();
91 List<User> followers = userService.getFollowers(currentUserId);
夜雨声烦f995a442025-05-13 18:43:29 +080092 return ApiResponse.success(followers);
夜雨声烦19976162025-04-22 01:29:11 +080093 }
94
95 // ==================== 私信功能 ====================
夜雨声烦f995a442025-05-13 18:43:29 +080096
夜雨声烦7e6eb382025-04-22 01:18:00 +080097 @PostMapping("/message/{receiverId}")
夜雨声烦f995a442025-05-13 18:43:29 +080098 public ApiResponse<Map<String, Long>> sendMessage(
夜雨声烦7e6eb382025-04-22 01:18:00 +080099 @PathVariable Long receiverId,
100 @RequestBody String content
101 ) {
夜雨声烦19976162025-04-22 01:29:11 +0800102 Long senderId = getCurrentUserId();
夜雨声烦7e6eb382025-04-22 01:18:00 +0800103 Long messageId = userService.sendMessage(senderId, receiverId, content);
夜雨声烦f995a442025-05-13 18:43:29 +0800104 return ApiResponse.success(Map.of("messageId", messageId));
夜雨声烦7e6eb382025-04-22 01:18:00 +0800105 }
夜雨声烦19976162025-04-22 01:29:11 +0800106
107 @GetMapping("/messages/{otherUserId}")
夜雨声烦f995a442025-05-13 18:43:29 +0800108 public ApiResponse<List<Message>> getMessages(@PathVariable Long otherUserId) {
夜雨声烦19976162025-04-22 01:29:11 +0800109 Long currentUserId = getCurrentUserId();
110 List<Message> messages = userService.getMessages(currentUserId, otherUserId);
夜雨声烦f995a442025-05-13 18:43:29 +0800111 return ApiResponse.success(messages);
夜雨声烦19976162025-04-22 01:29:11 +0800112 }
113
114 @GetMapping("/messages/history")
夜雨声烦f995a442025-05-13 18:43:29 +0800115 public ApiResponse<List<Message>> getMessageHistory() {
夜雨声烦19976162025-04-22 01:29:11 +0800116 Long currentUserId = getCurrentUserId();
117 List<Message> messages = userService.getMessageHistory(currentUserId);
夜雨声烦f995a442025-05-13 18:43:29 +0800118 return ApiResponse.success(messages);
夜雨声烦19976162025-04-22 01:29:11 +0800119 }
120
121 // ==================== 工具方法 ====================
122 private Long getCurrentUserId() {
123 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
124 return (Long) authentication.getPrincipal();
125 }
夜雨声烦35c9da92025-05-20 00:12:48 +0800126
127 @Autowired
128 private ISigningService signingService; // 修改接口名称
129
130 @PostMapping("/signin")
夜雨声烦45c25dd2025-05-20 11:59:03 +0800131 public ApiResponse<String> signIn() { // 修改返回类型为 ApiResponse
132 Long userId = getCurrentUserId();
夜雨声烦35c9da92025-05-20 00:12:48 +0800133 boolean success = signingService.signIn(userId);
夜雨声烦45c25dd2025-05-20 11:59:03 +0800134 String message = success ? "签到成功" : "今日已签到";
135 return ApiResponse.success(message);
136 }
137
138 // 新增获取时间段内的签到记录接口
139 @GetMapping("/signins")
140 public ApiResponse<List<UserSignin>> getSignins(
141 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
142 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
143 Long userId = getCurrentUserId();
144 List<UserSignin> signins = signingService.getSigninsByDateRange(userId, startDate, endDate);
145 return ApiResponse.success(signins);
夜雨声烦35c9da92025-05-20 00:12:48 +0800146 }
wuchimedesa1bf2782025-03-27 15:08:54 +0800147}