xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 1 | package com.g9.g9backend.controller; |
| 2 | |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 3 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 4 | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| 5 | import com.g9.g9backend.pojo.*; |
| 6 | import com.g9.g9backend.pojo.DTO.*; |
| 7 | import com.g9.g9backend.service.*; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 8 | import org.slf4j.Logger; |
| 9 | import org.slf4j.LoggerFactory; |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 10 | import org.springframework.http.ResponseEntity; |
| 11 | import org.springframework.web.bind.annotation.*; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * UserController 用户控制器类,处理与用户相关的请求 |
| 15 | * |
| 16 | * @author hcy |
| 17 | */ |
| 18 | @RestController |
| 19 | @RequestMapping("/user") |
| 20 | public class UserController { |
| 21 | |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 22 | private final UserService userService; |
| 23 | |
| 24 | private final InvitationService invitationService; |
| 25 | |
| 26 | private final SubscriptionService subscriptionService; |
| 27 | |
| 28 | public UserController(UserService userService, InvitationService invitationService, SubscriptionService subscriptionService) { |
| 29 | this.userService = userService; |
| 30 | this.invitationService = invitationService; |
| 31 | this.subscriptionService = subscriptionService; |
| 32 | } |
| 33 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 34 | private final Logger logger = LoggerFactory.getLogger(UserController.class); |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 35 | |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 36 | /** |
| 37 | * 用户注册 |
| 38 | * |
| 39 | * @param registerDTO 用户注册 |
| 40 | * @return 注册结果 |
| 41 | */ |
| 42 | @PostMapping("/register") |
| 43 | public ResponseEntity<String> register(@RequestBody RegisterDTO registerDTO) { |
| 44 | String username = registerDTO.getUsername(); |
| 45 | String password = registerDTO.getPassword(); |
| 46 | String invitationCode = registerDTO.getInvitationCode(); |
| 47 | logger.info("Register request received for account: {}", username); |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 48 | |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 49 | // 根据用户名查询该用户名是否已存在 |
| 50 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 51 | userQuery.eq("username", username); |
| 52 | User userCheck = userService.getOne(userQuery); |
| 53 | |
| 54 | if (userCheck != null) { |
| 55 | // 用户名重复 |
| 56 | logger.warn("Registration attempt failed. Account already exists: {}", username); |
| 57 | return ResponseEntity.status(407).body(""); |
| 58 | } |
| 59 | |
| 60 | // 查询邀请码是否存在 |
| 61 | QueryWrapper<Invitation> invitationQuery = new QueryWrapper<>(); |
| 62 | invitationQuery.eq("invitation_code", invitationCode); |
| 63 | Invitation invitation = invitationService.getOne(invitationQuery); |
| 64 | |
| 65 | if (invitation == null) { |
| 66 | // 邀请码不存在 |
| 67 | logger.info("The invitation code does not exist: {}", invitationCode); |
| 68 | return ResponseEntity.status(409).body(""); |
| 69 | } else if (invitation.getInviteeId() != 0) { |
| 70 | // 邀请码已被使用 |
| 71 | logger.info("The invitation code has been used: {}", invitationCode); |
| 72 | return ResponseEntity.status(410).body(""); |
| 73 | } |
| 74 | // 注册 |
| 75 | // 添加新用户 |
| 76 | User user = new User(); |
| 77 | user.setUsername(username); |
| 78 | user.setPassword(password); |
| 79 | userService.save(user); |
| 80 | |
| 81 | // 设置该邀请码已被使用 |
| 82 | User userGetId = userService.getOne(userQuery); |
| 83 | int newUserId = userGetId.getUserId(); |
| 84 | |
| 85 | UpdateWrapper<Invitation> updateWrapper = new UpdateWrapper<>(); |
| 86 | updateWrapper.eq("invitation_code", invitationCode).set("invitee_id", newUserId); |
| 87 | invitationService.update(updateWrapper); |
| 88 | |
| 89 | // 生成五个邀请码并分配给新用户 |
| 90 | String[] invitationCodes = invitationService.generateInvitationCode(); |
| 91 | |
| 92 | for (String code : invitationCodes) { |
| 93 | Invitation newInvitation = new Invitation(); |
| 94 | newInvitation.setInvitationCode(code); |
| 95 | newInvitation.setUserId(newUserId); |
| 96 | invitationService.save(newInvitation); |
| 97 | } |
| 98 | |
| 99 | logger.info("User registered successfully: {}", username); |
| 100 | return ResponseEntity.ok(""); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * 用户登录 |
| 105 | * |
| 106 | * @param user 登录信息 |
| 107 | * @return 登录结果 |
| 108 | */ |
| 109 | @PostMapping("/login") |
| 110 | public ResponseEntity<String> login(@RequestBody User user) { |
| 111 | String username = user.getUsername(); |
| 112 | String password = user.getPassword(); |
| 113 | logger.info("Login attempt for account: {}", username); |
| 114 | |
| 115 | // 根据用户名查询该用户名是否已存在 |
| 116 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 117 | userQuery.eq("username", username); |
| 118 | User userCheck = userService.getOne(userQuery); |
| 119 | |
| 120 | if (userCheck == null) { |
| 121 | // 用户名不存在 |
| 122 | logger.warn("Login failed. User not found: {}", username); |
| 123 | return ResponseEntity.status(406).body(""); |
| 124 | } else { |
| 125 | if (userCheck.getPassword().equals(password)) { |
| 126 | return ResponseEntity.ok(""); |
| 127 | } else { |
| 128 | // 密码错误 |
| 129 | logger.warn("Login failed. Incorrect password for account: {}", username); |
| 130 | return ResponseEntity.status(408).body(""); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * 关注 |
| 137 | * |
| 138 | * @param subscription 关注信息 |
| 139 | * @return 关注结果 |
| 140 | */ |
| 141 | @PostMapping("/subscription") |
| 142 | public ResponseEntity<String> subscription(@RequestBody Subscription subscription) { |
| 143 | subscriptionService.save(subscription); |
| 144 | return ResponseEntity.ok(""); |
| 145 | } |
| 146 | } |