blob: 42bc7e3ec83e609e7ed308588b970629e867f82e [file] [log] [blame]
Raverf79fdb62025-06-03 06:02:49 +00001package api;
2
3import java.io.File;
rhje18c3f72025-06-08 00:27:01 +08004import java.util.HashMap;
5import java.util.Map;
6import java.util.Date;
7import java.text.SimpleDateFormat;
8import java.text.ParseException;
Raverf79fdb62025-06-03 06:02:49 +00009
Raveraae06122025-06-05 08:13:35 +000010import javax.annotation.PostConstruct;
rhj7912f2c2025-06-09 17:21:32 +080011import javax.persistence.criteria.CriteriaBuilder.In;
Raveraae06122025-06-05 08:13:35 +000012
Raverf79fdb62025-06-03 06:02:49 +000013import org.springframework.core.io.FileSystemResource;
14import org.springframework.core.io.Resource;
15import org.springframework.http.HttpHeaders;
Raveraae06122025-06-05 08:13:35 +000016import org.springframework.http.HttpStatus;
Raverf79fdb62025-06-03 06:02:49 +000017import org.springframework.http.MediaType;
18import org.springframework.http.ResponseEntity;
19import org.springframework.web.bind.annotation.RequestParam;
20import org.springframework.web.bind.annotation.RestController;
21import org.springframework.web.multipart.MultipartFile;
Raveraae06122025-06-05 08:13:35 +000022import org.springframework.web.bind.annotation.CrossOrigin;
rhjc6a4ee02025-06-06 00:45:18 +080023import org.springframework.web.bind.annotation.RequestBody;
Raveraae06122025-06-05 08:13:35 +000024
25import com.fasterxml.jackson.core.JsonProcessingException;
26import com.fasterxml.jackson.databind.ObjectMapper;
Raverf79fdb62025-06-03 06:02:49 +000027
28import database.Database1;
Raverf79fdb62025-06-03 06:02:49 +000029import tracker.Tracker;
rhje18c3f72025-06-08 00:27:01 +080030import cheat.Cheat;
Raverf79fdb62025-06-03 06:02:49 +000031
rhjc6a4ee02025-06-06 00:45:18 +080032import entity.Seed;
33import entity.User;
rhj46f62c42025-06-06 23:24:10 +080034import entity.Post;
rhje18c3f72025-06-08 00:27:01 +080035import entity.BegSeedDetail;
rhj5b69b7e2025-06-07 01:28:08 +080036import entity.PostReply;
rhje18c3f72025-06-08 00:27:01 +080037import entity.Profile;
rhj5ebd93c2025-06-07 15:57:28 +080038import entity.UserPT;
rhje18c3f72025-06-08 00:27:01 +080039import entity.config;
40import entity.Appeal;
41import entity.BegInfo;
42import entity.BegInfoDetail;
43import entity.UserStar;
44import entity.SeedWithVotes;
rhj7912f2c2025-06-09 17:21:32 +080045import entity.SeedPromotion;
46import entity.SeedWithPromotionDTO;
rhjc6a4ee02025-06-06 00:45:18 +080047
48import java.util.UUID;
Raveraae06122025-06-05 08:13:35 +000049
Raverf79fdb62025-06-03 06:02:49 +000050@RestController
51public class ApiController implements ApiInterface {
52
Raveraae06122025-06-05 08:13:35 +000053 private static Database1 db1;
Raverf79fdb62025-06-03 06:02:49 +000054 private static Tracker tracker;
rhje18c3f72025-06-08 00:27:01 +080055 private static Cheat cheat;
Raveraae06122025-06-05 08:13:35 +000056 private static ObjectMapper mapper;
rhjc6a4ee02025-06-06 00:45:18 +080057 private static HttpHeaders headers;
58 private static HttpHeaders errorHeaders;
Raveraae06122025-06-05 08:13:35 +000059
60 @PostConstruct
61 public void init() {
rhje18c3f72025-06-08 00:27:01 +080062 cheat = new Cheat();
Raveraae06122025-06-05 08:13:35 +000063 db1 = new Database1();
64 tracker = new Tracker();
65 mapper = new ObjectMapper();
66 mapper.configure(com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
rhjc6a4ee02025-06-06 00:45:18 +080067 headers = new HttpHeaders();
68 headers.add("Access-Control-Allow-Origin", "*");
69 headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
70 headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
71 errorHeaders = new HttpHeaders();
72 errorHeaders.add("Access-Control-Allow-Origin", "*");
73 errorHeaders.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
74 errorHeaders.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
Raveraae06122025-06-05 08:13:35 +000075 }
Raverf79fdb62025-06-03 06:02:49 +000076
77 @Override
78 public ResponseEntity<Integer> saveTorrent(
79 @RequestParam("userid") String userid,
80 @RequestParam("title") String title,
81 @RequestParam("tag") String tag,
82 @RequestParam("file") MultipartFile file
83 ) {
84 try {
85 Seed seed = new Seed();
rhjc6a4ee02025-06-06 00:45:18 +080086 seed.seedid = UUID.randomUUID().toString(); // 生成唯一的种子ID
87 seed.seeduserid = userid;
88 seed.title = title;
89 seed.seedsize = "1GB";
90 seed.seedtag = tag;
Raverf79fdb62025-06-03 06:02:49 +000091 seed.url = "http://example.com/torrent"; // 示例URL
Raveraae06122025-06-05 08:13:35 +000092 int ret = db1.RegisterSeed(seed);
rhjc6a4ee02025-06-06 00:45:18 +080093 if (ret != 0) {
94 // 如果注册种子失败,返回错误状态
95 return new ResponseEntity<>(ret, headers, HttpStatus.INTERNAL_SERVER_ERROR);
96 }
rhje18c3f72025-06-08 00:27:01 +080097 File tempFile = File.createTempFile(userid, file.getOriginalFilename());
Raverf79fdb62025-06-03 06:02:49 +000098 file.transferTo(tempFile);
99 tracker.SaveTorrent(seed.seedid, tempFile);
Raveraae06122025-06-05 08:13:35 +0000100 return new ResponseEntity<>(0, headers, HttpStatus.OK); // 返回 0 表示成功
Raverf79fdb62025-06-03 06:02:49 +0000101 } catch (Exception e) {
102 e.printStackTrace();
Raveraae06122025-06-05 08:13:35 +0000103 return new ResponseEntity<>(1, errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示失败
Raverf79fdb62025-06-03 06:02:49 +0000104 }
105 }
106
107 @Override
Raveraae06122025-06-05 08:13:35 +0000108 @CrossOrigin(origins = "*", allowedHeaders = "*") // 允许所有来源和头部
Raverf79fdb62025-06-03 06:02:49 +0000109 public ResponseEntity<Resource> getTorrent(
rhjc6a4ee02025-06-06 00:45:18 +0800110 @RequestParam("torrentId") String seedid,
111 @RequestParam("userId") String userid
Raverf79fdb62025-06-03 06:02:49 +0000112 ) {
Raveraae06122025-06-05 08:13:35 +0000113 File file = tracker.GetTTorent(seedid, userid);
Raverf79fdb62025-06-03 06:02:49 +0000114 if (file != null) {
115 FileSystemResource resource = new FileSystemResource(file);
116 return ResponseEntity.ok()
117 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"")
Raveraae06122025-06-05 08:13:35 +0000118 .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) // 关键:允许前端访问Content-Disposition
Raverf79fdb62025-06-03 06:02:49 +0000119 .contentType(MediaType.APPLICATION_OCTET_STREAM)
120 .body(resource);
121 } else {
Raveraae06122025-06-05 08:13:35 +0000122 return ResponseEntity.notFound().build();
123 }
124 }
125
Raveraae06122025-06-05 08:13:35 +0000126 @Override
127 public ResponseEntity<String> getSeedListByTag(
128 @RequestParam("tag") String tag
129 ) {
130 try {
rhj7912f2c2025-06-09 17:21:32 +0800131 SeedWithPromotionDTO[] seeds = db1.GetSeedListByTag(tag);
rhje18c3f72025-06-08 00:27:01 +0800132 if (seeds == null) {
rhjc6a4ee02025-06-06 00:45:18 +0800133 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 404 表示未找到种子
134 }
Raveraae06122025-06-05 08:13:35 +0000135 String json = mapper.writeValueAsString(seeds);
Raveraae06122025-06-05 08:13:35 +0000136 return new ResponseEntity<>(json, headers, HttpStatus.OK);
137 } catch (JsonProcessingException e) {
138 e.printStackTrace();
Raveraae06122025-06-05 08:13:35 +0000139 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
140 }
141 }
142
143 @Override
rhjc6a4ee02025-06-06 00:45:18 +0800144 public ResponseEntity<String> getUserProfile(
145 @RequestParam("userid") String userid
146 ) {
147 try {
148 User user = db1.GetInformation(userid);
149 if (user == null) {
150 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
151 }
152 String json = mapper.writeValueAsString(user);
153 return new ResponseEntity<>(json, headers, HttpStatus.OK);
154 } catch (JsonProcessingException e) {
155 e.printStackTrace();
156 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
157 }
158 }
159
160 @Override
161 @CrossOrigin(origins = "*", allowedHeaders = "*")
162 public ResponseEntity<Integer> changeProfile(
163 @RequestBody String requestBody
164 ) {
165 try {
166 // 解析 JSON 数据
167 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
168
169 // 安全地获取 userid 字段
170 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
171 if (useridNode == null) {
172 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
173 }
174 String userid = useridNode.asText();
175
176 // 添加参数验证
177 if (userid == null || userid.trim().isEmpty()) {
178 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
179 }
180
181 // 手动映射前端字段到 User 对象,处理类型转换
182 User user = new User();
183 user.userid = userid;
184
185 // 安全地获取其他字段并进行类型转换
186 if (jsonNode.has("username") && !jsonNode.get("username").isNull()) {
187 user.username = jsonNode.get("username").asText();
188 }
189 if (jsonNode.has("school") && !jsonNode.get("school").isNull()) {
190 user.school = jsonNode.get("school").asText();
191 }
192 if (jsonNode.has("gender") && !jsonNode.get("gender").isNull()) {
193 user.sex = jsonNode.get("gender").asText();
194 }
195 if (jsonNode.has("avatar_url") && !jsonNode.get("avatar_url").isNull()) {
196 user.pictureurl = jsonNode.get("avatar_url").asText();
197 }
198
199 // 处理 account_status 的类型转换(字符串/数字 -> 布尔值)
200 if (jsonNode.has("account_status") && !jsonNode.get("account_status").isNull()) {
201 com.fasterxml.jackson.databind.JsonNode statusNode = jsonNode.get("account_status");
202 if (statusNode.isTextual()) {
203 String statusStr = statusNode.asText();
204 user.accountstate = "1".equals(statusStr) || "封禁".equals(statusStr);
205 } else if (statusNode.isNumber()) {
206 user.accountstate = statusNode.asInt() == 1;
207 } else if (statusNode.isBoolean()) {
208 user.accountstate = statusNode.asBoolean();
209 }
210 }
211
212 // 处理 invite_left 的类型转换(字符串 -> 整数)
213 if (jsonNode.has("invite_left") && !jsonNode.get("invite_left").isNull()) {
214 com.fasterxml.jackson.databind.JsonNode inviteNode = jsonNode.get("invite_left");
215 if (inviteNode.isTextual()) {
216 try {
217 user.invitetimes = Integer.parseInt(inviteNode.asText());
218 } catch (NumberFormatException e) {
219 user.invitetimes = 0; // 默认值
220 }
221 } else if (inviteNode.isNumber()) {
222 user.invitetimes = inviteNode.asInt();
223 }
224 }
225
226 int ret = db1.UpdateInformation(user);
227 if (ret == 0) {
228 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
229 } else {
230 return new ResponseEntity<>(ret, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
231 }
232 } catch (JsonProcessingException e) {
233 e.printStackTrace();
234 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
235 } catch (Exception e) {
236 e.printStackTrace();
237 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
238 }
239 }
240
241 @Override
242 public ResponseEntity<String> getUserSeeds(
243 @RequestParam("userid") String userid
244 ) {
245 try {
246 Seed[] seeds = db1.GetSeedListByUser(userid);
rhje18c3f72025-06-08 00:27:01 +0800247 if (seeds == null) {
rhjc6a4ee02025-06-06 00:45:18 +0800248 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 404 表示未找到种子
249 }
250 String json = mapper.writeValueAsString(seeds);
251 return new ResponseEntity<>(json, headers, HttpStatus.OK);
252 } catch (JsonProcessingException e) {
253 e.printStackTrace();
254 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
255 }
256 }
257
258 @Override
259 @CrossOrigin(origins = "*", allowedHeaders = "*")
260 public ResponseEntity<Integer> deleteSeed(
261 @RequestBody String requestBody
262 ) {
263 try {
264 // 解析 JSON 数据
265 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
266 com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seedid");
267 if (seedidNode == null) {
268 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
269 }
270 String seedid = seedidNode.asText();
271
272 // 添加参数验证
273 if (seedid == null || seedid.trim().isEmpty()) {
274 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
275 }
276
277 int ret = db1.DeleteSeed(seedid);
278 if (ret == 0) {
279 return new ResponseEntity<>(0, HttpStatus.OK);
280 } else {
281 return new ResponseEntity<>(ret, HttpStatus.INTERNAL_SERVER_ERROR);
282 }
283 } catch (Exception e) {
284 e.printStackTrace();
285 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
286 }
287 }
288
289 @Override
290 public ResponseEntity<String> getUserStat(
291 @RequestParam("userid") String userid
292 ) {
293 try {
294 User user = db1.GetInformation(userid);
295 if (user == null) {
296 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
297 }
298 String json = mapper.writeValueAsString(user);
299 return new ResponseEntity<>(json, headers, HttpStatus.OK);
300 } catch (JsonProcessingException e) {
301 e.printStackTrace();
302 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
303 }
304 }
305
306 @Override
Raveraae06122025-06-05 08:13:35 +0000307 public ResponseEntity<String> getTorrentDetail(
308 @RequestParam("id") String seedid
309 ) {
310 try {
311 Seed seed = db1.GetSeedInformation(seedid);
312 if (seed != null) {
313 String json = mapper.writeValueAsString(seed);
314 HttpHeaders headers = new HttpHeaders();
315 headers.add("Access-Control-Allow-Origin", "*");
316 headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
317 headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
318 return new ResponseEntity<>(json, headers, HttpStatus.OK);
319 } else {
320 return ResponseEntity.notFound().build(); // 返回 404 表示种子未找到
321 }
322 } catch (JsonProcessingException e) {
323 e.printStackTrace();
324 HttpHeaders errorHeaders = new HttpHeaders();
325 errorHeaders.add("Access-Control-Allow-Origin", "*");
326 errorHeaders.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
327 errorHeaders.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
328 return new ResponseEntity<>("{}", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
Raverf79fdb62025-06-03 06:02:49 +0000329 }
330 }
rhj46f62c42025-06-06 23:24:10 +0800331
332 @Override
333 @CrossOrigin(origins = "*", allowedHeaders = "*")
334 public ResponseEntity<String> loginUser(
335 @RequestBody String requestBody
336 ) {
337 try {
338 // 解析前端发送的JSON数据 {email: xxx, password: xxx}
339 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
340
341 // 获取email和password字段
342 com.fasterxml.jackson.databind.JsonNode emailNode = jsonNode.get("email");
343 com.fasterxml.jackson.databind.JsonNode passwordNode = jsonNode.get("password");
344
345 if (emailNode == null || passwordNode == null) {
346 com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
347 errorJson.put("message", "缺少必要参数");
348 String jsonError = mapper.writeValueAsString(errorJson);
349 return new ResponseEntity<>(jsonError, HttpStatus.BAD_REQUEST);
350 }
351
352 String email = emailNode.asText();
353 String password = passwordNode.asText();
354
355 // 参数验证
356 if (email == null || email.trim().isEmpty() ||
357 password == null || password.trim().isEmpty()) {
358 com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
359 errorJson.put("message", "邮箱和密码不能为空");
360 String jsonError = mapper.writeValueAsString(errorJson);
361 return new ResponseEntity<>(jsonError, HttpStatus.BAD_REQUEST);
362 }
363
364 // 创建User对象进行登录验证
365 User user = new User();
366 user.email = email.trim();
367 user.password = password;
368
369 String userid = db1.LoginUser(user);
370 if (userid != null) {
371 com.fasterxml.jackson.databind.node.ObjectNode responseJson = mapper.createObjectNode();
372 responseJson.put("userId", userid);
373 responseJson.put("userid", userid);
374 responseJson.put("message", "登录成功");
375 String jsonResponse = mapper.writeValueAsString(responseJson);
376 return new ResponseEntity<>(jsonResponse, HttpStatus.OK);
377 } else {
378 // 返回JSON格式的错误信息
379 com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
380 errorJson.put("message", "登录失败,请检查账号密码");
381 String jsonError = mapper.writeValueAsString(errorJson);
382 return new ResponseEntity<>(jsonError, HttpStatus.UNAUTHORIZED);
383 }
384 } catch (JsonProcessingException e) {
385 e.printStackTrace();
386 com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
387 try {
388 errorJson.put("message", "服务器内部错误");
389 String jsonError = mapper.writeValueAsString(errorJson);
390 return new ResponseEntity<>(jsonError, HttpStatus.INTERNAL_SERVER_ERROR);
391 } catch (JsonProcessingException ex) {
392 return new ResponseEntity<>("{\"message\":\"服务器内部错误\"}", HttpStatus.INTERNAL_SERVER_ERROR);
393 }
394 } catch (Exception e) {
395 e.printStackTrace();
396 com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
397 try {
398 errorJson.put("message", "服务器内部错误");
399 String jsonError = mapper.writeValueAsString(errorJson);
400 return new ResponseEntity<>(jsonError, HttpStatus.INTERNAL_SERVER_ERROR);
401 } catch (JsonProcessingException ex) {
402 return new ResponseEntity<>("{\"message\":\"服务器内部错误\"}", HttpStatus.INTERNAL_SERVER_ERROR);
403 }
404 }
405 }
406
407 @Override
408 @CrossOrigin(origins = "*", allowedHeaders = "*")
409 public ResponseEntity<Integer> registerUser(
410 @RequestBody String requestBody
411 ) {
412 try {
rhj46f62c42025-06-06 23:24:10 +0800413 // 解析 JSON 数据
414 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
415
416 // 安全地获取字段
417 com.fasterxml.jackson.databind.JsonNode usernameNode = jsonNode.get("username");
418 com.fasterxml.jackson.databind.JsonNode passwordNode = jsonNode.get("password");
419 com.fasterxml.jackson.databind.JsonNode inviteEmailNode = jsonNode.get("invite_email");
420
421 if (usernameNode == null || passwordNode == null || inviteEmailNode == null) {
422 return new ResponseEntity<>(2, HttpStatus.BAD_REQUEST); // 参数不完整
423 }
424
425 String username = usernameNode.asText();
426 String password = passwordNode.asText();
427 String inviteEmail = inviteEmailNode.asText();
428
429 // 参数验证
430 if (username == null || username.trim().isEmpty() ||
431 password == null || password.trim().isEmpty() ||
432 inviteEmail == null || inviteEmail.trim().isEmpty()) {
433 return new ResponseEntity<>(2, HttpStatus.BAD_REQUEST);
434 }
435
436 // 创建 User 对象
437 User user = new User();
438 user.userid = java.util.UUID.randomUUID().toString(); // 生成唯一用户ID
439 user.username = username.trim();
440 user.password = password;
441 user.email = inviteEmail.trim(); // 使用邀请邮箱作为用户邮箱
442
443 // 设置默认值
444 user.sex = "m"; // 默认性别
445 user.school = ""; // 默认学校
446 user.pictureurl = ""; // 默认头像URL
447 user.profile = ""; // 默认个人简介
448 user.accountstate = false; // 默认账号状态为正常
449 user.invitetimes = 5; // 默认邀请次数
450
451 // 设置时间字段
452 user.lastDetectedTime = new java.util.Date();
453 user.fakeLastDetectedTime = new java.util.Date();
454
455 // 调用数据库注册方法
456 int ret = db1.RegisterUser(user);
rhj46f62c42025-06-06 23:24:10 +0800457
458 // // 如果注册成功,还需要创建对应的 UserPT 记录
459 // if (ret == 0) {
460 // try {
461 // entity.UserPT userPT = new entity.UserPT();
462 // userPT.userid = user.userid;
463 // userPT.magic = 100; // 初始魔力值
464 // userPT.upload = 0L; // 初始上传量
465 // userPT.download = 0L; // 初始下载量
466 // userPT.share = 0.0; // 初始分享率
467 // userPT.farmurl = ""; // 默认做种路径
468 // userPT.viptime = 0; // 初始VIP次数
469 // userPT.user = user; // 设置关联
470
471 // int ptRet = db1.RegisterUserPT(userPT);
472 // if (ptRet != 0) {
473 // // 如果 UserPT 创建失败,记录日志但不影响主要注册流程
474 // System.err.println("Warning: Failed to create UserPT for user " + user.userid);
475 // }
476 // } catch (Exception e) {
477 // System.err.println("Warning: Exception creating UserPT: " + e.getMessage());
478 // }
479 // }
480
481 if (ret == 0) {
482 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示注册成功
483 } else if (ret == 1) {
484 return new ResponseEntity<>(1, HttpStatus.CONFLICT); // 返回 1 表示邮箱重复
485 } else {
486 return new ResponseEntity<>(ret, HttpStatus.BAD_REQUEST); // 返回 2 表示未被邀请或其他错误
487 }
488 } catch (JsonProcessingException e) {
489 e.printStackTrace();
490 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 2 表示处理失败
491 } catch (Exception e) {
492 e.printStackTrace();
493 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR);
494 }
495 }
496
497 @Override
rhj5b69b7e2025-06-07 01:28:08 +0800498 @CrossOrigin(origins = "*", allowedHeaders = "*")
rhj46f62c42025-06-06 23:24:10 +0800499 public ResponseEntity<String> getForum() {
500 try {
501 Post[] posts = db1.GetPostList();
rhje18c3f72025-06-08 00:27:01 +0800502 if (posts == null) {
rhj46f62c42025-06-06 23:24:10 +0800503 return new ResponseEntity<>("[]", HttpStatus.OK); // 返回空数组表示没有帖子
504 }
505 String json = mapper.writeValueAsString(posts);
506 return new ResponseEntity<>(json, HttpStatus.OK);
507 } catch (JsonProcessingException e) {
508 e.printStackTrace();
509 return new ResponseEntity<>("[]", HttpStatus.INTERNAL_SERVER_ERROR);
510 } catch (Exception e) {
511 e.printStackTrace();
512 return new ResponseEntity<>("[]", HttpStatus.INTERNAL_SERVER_ERROR);
513 }
514 }
rhj5b69b7e2025-06-07 01:28:08 +0800515
516 @Override
517 @CrossOrigin(origins = "*", allowedHeaders = "*")
518 public ResponseEntity<String> getPostById(
519 @RequestParam("postid") String postid
520 ) {
521 try {
522 Post post = db1.GetPost(postid);
523 PostReply[] replies = db1.GetPostReplyList(postid);
524 if (post == null) {
525 return new ResponseEntity<>("", HttpStatus.NOT_FOUND); // 返回 404 表示帖子未找到
526 }
527
528 // 创建合并的 JSON 对象
529 com.fasterxml.jackson.databind.node.ObjectNode resultJson = mapper.createObjectNode();
530
531 // 添加 post 的所有字段
532 resultJson.put("postid", post.postid);
533 resultJson.put("posttitle", post.posttitle);
534 resultJson.put("postcontent", post.postcontent);
535 resultJson.put("postuserid", post.postuserid);
536 resultJson.put("replytime", post.replytime);
537 resultJson.put("readtime", post.readtime);
538
539 if (post.posttime != null) {
540 resultJson.put("posttime", post.posttime.getTime());
541 }
542
543 // 添加作者信息
544 if (post.author != null) {
545 com.fasterxml.jackson.databind.node.ObjectNode authorJson = mapper.createObjectNode();
546 authorJson.put("userid", post.author.userid);
547 authorJson.put("username", post.author.username);
548 authorJson.put("email", post.author.email);
549 authorJson.put("sex", post.author.sex);
550 authorJson.put("school", post.author.school);
551 authorJson.put("pictureurl", post.author.pictureurl);
552 authorJson.put("profile", post.author.profile);
553 authorJson.put("accountstate", post.author.accountstate);
554 authorJson.put("invitetimes", post.author.invitetimes);
555 if (post.author.lastDetectedTime != null) {
556 authorJson.put("lastDetectedTime", post.author.lastDetectedTime.getTime());
557 }
558 if (post.author.fakeLastDetectedTime != null) {
559 authorJson.put("fakeLastDetectedTime", post.author.fakeLastDetectedTime.getTime());
560 }
561 resultJson.set("author", authorJson);
562 }
563
564 // 添加 replies 数组
565 com.fasterxml.jackson.databind.node.ArrayNode repliesArray = mapper.createArrayNode();
566 if (replies != null) {
rhj5b69b7e2025-06-07 01:28:08 +0800567 for (PostReply reply : replies) {
568 com.fasterxml.jackson.databind.node.ObjectNode replyJson = mapper.createObjectNode();
569 replyJson.put("replyid", reply.replyid);
570 replyJson.put("postid", reply.postid);
571 replyJson.put("authorid", reply.authorid);
572 replyJson.put("content", reply.content);
573 if (reply.createdAt != null) {
574 replyJson.put("createdAt", reply.createdAt.getTime());
575 }
576
577 // 添加回复作者信息
578 if (reply.author != null) {
579 com.fasterxml.jackson.databind.node.ObjectNode replyAuthorJson = mapper.createObjectNode();
580 replyAuthorJson.put("userid", reply.author.userid);
581 replyAuthorJson.put("username", reply.author.username);
582 replyAuthorJson.put("email", reply.author.email);
583 replyAuthorJson.put("sex", reply.author.sex);
584 replyAuthorJson.put("school", reply.author.school);
585 replyAuthorJson.put("pictureurl", reply.author.pictureurl);
586 replyAuthorJson.put("profile", reply.author.profile);
587 replyAuthorJson.put("accountstate", reply.author.accountstate);
588 replyAuthorJson.put("invitetimes", reply.author.invitetimes);
589 if (reply.author.lastDetectedTime != null) {
590 replyAuthorJson.put("lastDetectedTime", reply.author.lastDetectedTime.getTime());
591 }
592 if (reply.author.fakeLastDetectedTime != null) {
593 replyAuthorJson.put("fakeLastDetectedTime", reply.author.fakeLastDetectedTime.getTime());
594 }
595 replyJson.set("author", replyAuthorJson);
596 }
597
598 repliesArray.add(replyJson);
599 }
600 }
601 resultJson.set("replies", repliesArray);
602
603 String json = mapper.writeValueAsString(resultJson);
604 return new ResponseEntity<>(json, HttpStatus.OK);
605 } catch (JsonProcessingException e) {
606 // e.printStackTrace();
607 return new ResponseEntity<>("", HttpStatus.INTERNAL_SERVER_ERROR);
608 } catch (Exception e) {
609 // e.printStackTrace();
610 return new ResponseEntity<>("", HttpStatus.INTERNAL_SERVER_ERROR);
611 }
612 }
613
614 @Override
615 @CrossOrigin(origins = "*", allowedHeaders = "*")
616 public ResponseEntity<Integer> addPostReply(
617 @RequestBody String requestBody
618 ) {
619 try {
rhj5b69b7e2025-06-07 01:28:08 +0800620 // 解析 JSON 数据
621 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
622 com.fasterxml.jackson.databind.JsonNode postidNode = jsonNode.get("postid");
623 com.fasterxml.jackson.databind.JsonNode authoridNode = jsonNode.get("replyuserid");
624 com.fasterxml.jackson.databind.JsonNode contentNode = jsonNode.get("replycontent");
625
626 if (postidNode == null || authoridNode == null || contentNode == null) {
627 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
628 }
629
630 String postid = postidNode.asText();
631 String authorid = authoridNode.asText();
632 String content = contentNode.asText();
633
rhj5b69b7e2025-06-07 01:28:08 +0800634 // 参数验证
635 if (postid == null || postid.trim().isEmpty() ||
636 authorid == null || authorid.trim().isEmpty() ||
637 content == null || content.trim().isEmpty()) {
638 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
639 }
640
641 int ret = db1.AddComment(postid, authorid, content);
rhj5b69b7e2025-06-07 01:28:08 +0800642 if (ret == 0) {
643 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
644 } else {
645 return new ResponseEntity<>(ret, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
646 }
647 } catch (JsonProcessingException e) {
648 e.printStackTrace();
649 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
650 } catch (Exception e) {
651 e.printStackTrace();
652 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
653 }
654 }
655
656 @Override
657 public ResponseEntity<String> searchSeeds(
658 @RequestParam("tag") String tag,
659 @RequestParam("keyword") String query
660 ) {
rhj5ebd93c2025-06-07 15:57:28 +0800661 try {
662 Seed[] seeds = db1.SearchSeed(query);
rhje18c3f72025-06-08 00:27:01 +0800663 if (seeds == null) {
rhj5ebd93c2025-06-07 15:57:28 +0800664 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到种子
665 }
666
667 // 过滤掉与前端要求tag不同的种子
668 java.util.List<Seed> filteredSeeds = new java.util.ArrayList<>();
669 for (Seed seed : seeds) {
670 if (seed.seedtag != null && seed.seedtag.equals(tag)) {
671 filteredSeeds.add(seed);
672 }
673 }
674
675 // 如果过滤后没有匹配的种子,返回空数组
676 if (filteredSeeds.isEmpty()) {
677 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND);
678 }
679
680 Seed[] filteredSeedsArray = filteredSeeds.toArray(new Seed[0]);
681 String json = mapper.writeValueAsString(filteredSeedsArray);
682 return new ResponseEntity<>(json, headers, HttpStatus.OK);
683 } catch (JsonProcessingException e) {
684 e.printStackTrace();
685 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
686 } catch (Exception e) {
687 e.printStackTrace();
688 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
689 }
690 }
691
692 @Override
693 public ResponseEntity<String> searchPosts(
694 @RequestParam("keyword") String query
695 ) {
696 try {
697 Post[] posts = db1.SearchPost(query);
rhje18c3f72025-06-08 00:27:01 +0800698 if (posts == null) {
rhj5ebd93c2025-06-07 15:57:28 +0800699 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到帖子
700 }
701 String json = mapper.writeValueAsString(posts);
702 return new ResponseEntity<>(json, headers, HttpStatus.OK);
703 } catch (JsonProcessingException e) {
704 e.printStackTrace();
705 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
706 } catch (Exception e) {
707 e.printStackTrace();
708 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
709 }
710 }
711
712 @Override
713 public ResponseEntity<String> getUserPT(
714 @RequestParam("userid") String userid
715 ) {
716 try {
717 UserPT userPT = db1.GetInformationPT(userid);
718 if (userPT == null) {
719 return new ResponseEntity<>("", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到用户PT信息
720 }
721 String json = mapper.writeValueAsString(userPT);
722 return new ResponseEntity<>(json, headers, HttpStatus.OK);
723 } catch (JsonProcessingException e) {
724 e.printStackTrace();
725 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
726 } catch (Exception e) {
727 e.printStackTrace();
728 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
729 }
rhj5b69b7e2025-06-07 01:28:08 +0800730 }
rhje18c3f72025-06-08 00:27:01 +0800731
732 @Override
733 public ResponseEntity<String> getConfig(
734 @RequestParam("userid") String userid
735 ) {
736 try {
737 int ret = db1.CheckAdmin(userid);
738 if (ret != 0) {
739 return new ResponseEntity<>("", errorHeaders, HttpStatus.UNAUTHORIZED); // 返回 401 表示未授权
740 }
741 // 创建配置信息的 JSON 对象
742 Map<String, Object> configData = new HashMap<>();
743 configData.put("FarmNumber", config.getFarmNumber());
744 configData.put("FakeTime", config.getFakeTime());
745 configData.put("BegVote", config.getBegVote());
746 configData.put("CheatTime", config.getCheatTime());
747
748 String json = mapper.writeValueAsString(configData);
749 return new ResponseEntity<>(json, headers, HttpStatus.OK);
750 } catch (JsonProcessingException e) {
751 e.printStackTrace();
752 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
753 } catch (Exception e) {
754 e.printStackTrace();
755 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
756 }
757 }
758
759 @Override
760 public ResponseEntity<String> getCheatUsers(
761 @RequestParam("userid") String userid
762 ) {
763 try {
764 int ret = db1.CheckAdmin(userid);
765 if (ret != 0) {
766 return new ResponseEntity<>("", errorHeaders, HttpStatus.UNAUTHORIZED); // 返回 401 表示未授权
767 }
768 User[] cheatUsers = cheat.GetCheatUsers();
769 if (cheatUsers == null) {
770 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到作弊用户
771 }
772 String json = mapper.writeValueAsString(cheatUsers);
773 return new ResponseEntity<>(json, headers, HttpStatus.OK);
774 } catch (JsonProcessingException e) {
775 e.printStackTrace();
776 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
777 } catch (Exception e) {
778 e.printStackTrace();
779 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
780 }
781 }
782
783 @Override
784 public ResponseEntity<String> getSuspiciousUsers(
785 @RequestParam("userid") String userid
786 ) {
787 try {
788 int ret = db1.CheckAdmin(userid);
789 if (ret != 0) {
790 return new ResponseEntity<>("", errorHeaders, HttpStatus.UNAUTHORIZED); // 返回 401 表示未授权
791 }
792 User[] suspiciousUsers = cheat.GetSuspiciousUsers();
793 if (suspiciousUsers == null) {
794 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到可疑用户
795 }
796 String json = mapper.writeValueAsString(suspiciousUsers);
797 return new ResponseEntity<>(json, headers, HttpStatus.OK);
798 } catch (JsonProcessingException e) {
799 e.printStackTrace();
800 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
801 } catch (Exception e) {
802 e.printStackTrace();
803 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
804 }
805 }
806
807 @Override
808 @CrossOrigin(origins = "*", allowedHeaders = "*")
809 public ResponseEntity<Integer> unbanUser(
810 @RequestBody String requestBody
811 ) {
812 try {
813 // 解析 JSON 数据
814 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
815 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
816 if (useridNode == null) {
817 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
818 }
819 String userid = useridNode.asText();
820
821 // 添加参数验证
822 if (userid == null || userid.trim().isEmpty()) {
823 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
824 }
825
826 int ret = cheat.UnbanUser(userid);
827 if (ret == 0) {
828 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
829 } else if (ret == 1) {
830 return new ResponseEntity<>(1, HttpStatus.NOT_FOUND); // 返回 1 表示用户不存在
831 } else if (ret == 2) {
832 return new ResponseEntity<>(2, HttpStatus.CONFLICT); // 返回 2 表示用户未被封禁
833 } else {
834 return new ResponseEntity<>(3, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
835 }
836 } catch (JsonProcessingException e) {
837 e.printStackTrace();
838 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
839 } catch (Exception e) {
840 e.printStackTrace();
841 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
842 }
843 }
844
845 @Override
846 @CrossOrigin(origins = "*", allowedHeaders = "*")
847 public ResponseEntity<Integer> banUser(
848 @RequestBody String requestBody
849 ) {
850 try {
851 // 解析 JSON 数据
852 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
853 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
854 if (useridNode == null) {
855 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
856 }
857 String userid = useridNode.asText();
858
859 // 添加参数验证
860 if (userid == null || userid.trim().isEmpty()) {
861 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
862 }
863
864 int ret = cheat.BanUser(userid);
865 if (ret == 0) {
866 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
867 } else if (ret == 1) {
868 return new ResponseEntity<>(1, HttpStatus.NOT_FOUND); // 返回 1 表示用户不存在
869 } else if (ret == 2) {
870 return new ResponseEntity<>(2, HttpStatus.CONFLICT); // 返回 2 表示用户已被封禁
871 } else {
872 return new ResponseEntity<>(3, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
873 }
874 } catch (JsonProcessingException e) {
875 e.printStackTrace();
876 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
877 } catch (Exception e) {
878 e.printStackTrace();
879 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
880 }
881 }
882
883 @Override
884 public ResponseEntity<String> getAppeals() {
885 try {
886 Appeal[] appeals = cheat.GetAppealList();
887 if (appeals == null) {
888 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到申诉
889 }
890 String json = mapper.writeValueAsString(appeals);
891 return new ResponseEntity<>(json, headers, HttpStatus.OK);
892 } catch (JsonProcessingException e) {
893 e.printStackTrace();
894 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
895 } catch (Exception e) {
896 e.printStackTrace();
897 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
898 }
899 }
900
901 @Override
902 public ResponseEntity<String> getMigrations() {
903 try {
904 Profile[] migrations = db1.GetTransmitProfileList();
905 if (migrations == null) {
906 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到迁移记录
907 }
908 String json = mapper.writeValueAsString(migrations);
909 return new ResponseEntity<>(json, headers, HttpStatus.OK);
910 } catch (JsonProcessingException e) {
911 e.printStackTrace();
912 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
913 } catch (Exception e) {
914 e.printStackTrace();
915 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
916 }
917 }
918
919 @Override
920 @CrossOrigin(origins = "*", allowedHeaders = "*")
921 public ResponseEntity<Integer> approveAppeal(
922 @RequestBody String requestBody
923 ) {
924 try {
925 // 解析 JSON 数据
926 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
927 com.fasterxml.jackson.databind.JsonNode appealidNode = jsonNode.get("appealid");
928 if (appealidNode == null) {
929 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
930 }
931 String appealid = appealidNode.asText();
932
933 // 添加参数验证
934 if (appealid == null || appealid.trim().isEmpty()) {
935 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
936 }
937
938 boolean ret = cheat.HandleAppeal(appealid, 1);
939 if (ret) {
940 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
941 } else {
942 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
943 }
944 } catch (JsonProcessingException e) {
945 e.printStackTrace();
946 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
947 } catch (Exception e) {
948 e.printStackTrace();
949 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
950 }
951 }
952
953 @Override
954 @CrossOrigin(origins = "*", allowedHeaders = "*")
955 public ResponseEntity<Integer> rejectAppeal(
956 @RequestBody String requestBody
957 ) {
958 try {
959 // 解析 JSON 数据
960 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
961 com.fasterxml.jackson.databind.JsonNode appealidNode = jsonNode.get("appealid");
962 if (appealidNode == null) {
963 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
964 }
965 String appealid = appealidNode.asText();
966
967 // 添加参数验证
968 if (appealid == null || appealid.trim().isEmpty()) {
969 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
970 }
971
972 boolean ret = cheat.HandleAppeal(appealid, 2);
973 if (ret) {
974 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
975 } else {
976 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
977 }
978 } catch (JsonProcessingException e) {
979 e.printStackTrace();
980 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
981 } catch (Exception e) {
982 e.printStackTrace();
983 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
984 }
985 }
986
987 @Override
988 @CrossOrigin(origins = "*", allowedHeaders = "*")
989 public ResponseEntity<Integer> approveMigration(
990 @RequestBody String requestBody
991 ) {
992 try {
993 // 解析 JSON 数据
994 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
995 com.fasterxml.jackson.databind.JsonNode migrationidNode = jsonNode.get("migration_id");
rhj7912f2c2025-06-09 17:21:32 +0800996 com.fasterxml.jackson.databind.JsonNode grantedUploadedNode = jsonNode.get("granted_uploaded");
997 if (migrationidNode == null || grantedUploadedNode == null) {
rhje18c3f72025-06-08 00:27:01 +0800998 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
999 }
1000 String migrationid = migrationidNode.asText();
rhj7912f2c2025-06-09 17:21:32 +08001001 Integer grantedUploaded = grantedUploadedNode != null ? grantedUploadedNode.asInt() : null;
1002 if (grantedUploaded == null || grantedUploaded < 0) {
1003 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 上传量必须为非负整数
1004 }
rhje18c3f72025-06-08 00:27:01 +08001005
1006 // 添加参数验证
1007 if (migrationid == null || migrationid.trim().isEmpty()) {
1008 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1009 }
1010
rhj7912f2c2025-06-09 17:21:32 +08001011 boolean ret = db1.ExamTransmitProfile(migrationid, true, grantedUploaded);
rhje18c3f72025-06-08 00:27:01 +08001012 if (ret) {
1013 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1014 } else {
1015 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1016 }
1017 } catch (JsonProcessingException e) {
1018 e.printStackTrace();
1019 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1020 } catch (Exception e) {
1021 e.printStackTrace();
1022 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1023 }
1024 }
1025
1026 @Override
1027 @CrossOrigin(origins = "*", allowedHeaders = "*")
1028 public ResponseEntity<Integer> rejectMigration(
1029 @RequestBody String requestBody
1030 ) {
1031 try {
1032 // 解析 JSON 数据
1033 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1034 com.fasterxml.jackson.databind.JsonNode migrationidNode = jsonNode.get("migration_id");
1035 if (migrationidNode == null) {
1036 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1037 }
1038 String migrationid = migrationidNode.asText();
1039
1040 // 添加参数验证
1041 if (migrationid == null || migrationid.trim().isEmpty()) {
1042 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1043 }
1044
rhj7912f2c2025-06-09 17:21:32 +08001045 boolean ret = db1.ExamTransmitProfile(migrationid, false, 0);
rhje18c3f72025-06-08 00:27:01 +08001046 if (ret) {
1047 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1048 } else {
1049 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1050 }
1051 } catch (JsonProcessingException e) {
1052 e.printStackTrace();
1053 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1054 } catch (Exception e) {
1055 e.printStackTrace();
1056 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1057 }
1058 }
1059
1060 @Override
1061 @CrossOrigin(origins = "*", allowedHeaders = "*")
1062 public ResponseEntity<Integer> inviteUser(
1063 @RequestBody String requestBody
1064 ) {
1065 try {
1066 // 解析 JSON 数据
1067 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1068 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1069 com.fasterxml.jackson.databind.JsonNode emailNode = jsonNode.get("invite_email");
1070 if (useridNode == null || emailNode == null) {
1071 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1072 }
1073 String userid = useridNode.asText();
1074 String email = emailNode.asText();
1075
1076 // 添加参数验证
1077 if (email == null || email.trim().isEmpty()) {
1078 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1079 }
1080
1081 int ret = db1.InviteNewUser(userid, email);
1082 if (ret == 0) {
1083 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1084 } else if (ret == 1) {
1085 return new ResponseEntity<>(1, HttpStatus.CONFLICT); // 返回 1 表示邀请已存在
1086 } else {
1087 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1088 }
1089 } catch (JsonProcessingException e) {
1090 e.printStackTrace();
1091 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1092 } catch (Exception e) {
1093 e.printStackTrace();
1094 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1095 }
1096 }
1097
1098 @Override
1099 @CrossOrigin(origins = "*", allowedHeaders = "*")
1100 public ResponseEntity<Integer> submitAppeal(
1101 @RequestParam("userid") String userid,
1102 @RequestParam("content") String content,
1103 @RequestParam("file") MultipartFile file
1104 ) {
1105 try {
rhje18c3f72025-06-08 00:27:01 +08001106 // 先进行参数验证
1107 if (userid == null || userid.trim().isEmpty() ||
1108 content == null || content.trim().isEmpty() ||
1109 file == null || file.isEmpty()) {
1110 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1111 }
1112
1113 if (file.getSize() > 100 * 1024 * 1024) {
rhje18c3f72025-06-08 00:27:01 +08001114 return new ResponseEntity<>(3, HttpStatus.PAYLOAD_TOO_LARGE); // 返回 3 表示文件过大
1115 }
1116
rhje18c3f72025-06-08 00:27:01 +08001117 File tempFile = File.createTempFile(userid, file.getOriginalFilename());
1118 file.transferTo(tempFile);
1119
1120 int ret = cheat.SubmitAppeal(userid, content, tempFile);
rhje18c3f72025-06-08 00:27:01 +08001121 if (ret == 0) {
1122 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1123 } else if (ret == 1) {
1124 return new ResponseEntity<>(1, HttpStatus.CONFLICT); // 返回 1 表示用户未被封禁或其他错误
1125 } else {
1126 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1127 }
1128 } catch (org.springframework.web.multipart.MaxUploadSizeExceededException e) {
rhje18c3f72025-06-08 00:27:01 +08001129 return new ResponseEntity<>(3, HttpStatus.PAYLOAD_TOO_LARGE); // 返回 3 表示文件过大
1130 } catch (JsonProcessingException e) {
1131 e.printStackTrace();
1132 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1133 } catch (Exception e) {
1134 e.printStackTrace();
1135 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1136 }
1137 }
1138
1139 @Override
1140 public ResponseEntity<String> getUserStats(
1141 @RequestParam("userid") String userid
1142 ) {
1143 try {
1144 if (userid == null || userid.trim().isEmpty()) {
1145 return new ResponseEntity<>("", errorHeaders, HttpStatus.BAD_REQUEST); // 返回 400 表示参数不完整
1146 }
1147
1148 UserPT userPT = db1.GetInformationPT(userid);
1149 if (userPT == null) {
1150 return new ResponseEntity<>("", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到用户PT信息
1151 }
1152
1153 String json = mapper.writeValueAsString(userPT);
1154 return new ResponseEntity<>(json, headers, HttpStatus.OK);
1155 } catch (JsonProcessingException e) {
1156 e.printStackTrace();
1157 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
1158 } catch (Exception e) {
1159 e.printStackTrace();
1160 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
1161 }
1162 }
1163
1164 @Override
1165 @CrossOrigin(origins = "*", allowedHeaders = "*")
1166 public ResponseEntity<Integer> magicExchange(
1167 @RequestBody String requestBody
1168 ) {
1169 try {
1170 // 解析 JSON 数据
1171 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1172 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1173 com.fasterxml.jackson.databind.JsonNode magicNode = jsonNode.get("magic");
1174 com.fasterxml.jackson.databind.JsonNode typeNode = jsonNode.get("exchangeType");
1175
1176 if (useridNode == null || magicNode == null || typeNode == null) {
1177 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1178 }
1179 String userid = useridNode.asText();
1180 int magic = magicNode.asInt();
1181 String exchangeType = typeNode.asText();
1182
1183 boolean ret = false;
1184 if( "uploaded".equals(exchangeType) ) {
1185 ret = db1.ExchangeMagicToUpload(userid, magic);
1186 } else if( "downloaded".equals(exchangeType) ) {
1187 ret = db1.ExchangeMagicToDownload(userid, magic);
1188 } else if( "vip_downloads".equals(exchangeType) ) {
1189 ret = db1.ExchangeMagicToVip(userid, magic);
1190 } else {
rhje18c3f72025-06-08 00:27:01 +08001191 return new ResponseEntity<>(2, HttpStatus.BAD_REQUEST); // 返回 2 表示交换类型错误
1192 }
1193 if (!ret) {
1194 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1195 }
1196 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1197 } catch (JsonProcessingException e) {
1198 e.printStackTrace();
1199 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1200 } catch (Exception e) {
1201 e.printStackTrace();
1202 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1203 }
1204 }
1205
1206 @Override
1207 public ResponseEntity<String> getUserFavorites(
1208 @RequestParam("userid") String userid
1209 ) {
1210 try {
1211 if (userid == null || userid.trim().isEmpty()) {
1212 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.BAD_REQUEST); // 返回 400 表示参数不完整
1213 }
1214
1215 UserStar[] favorites = db1.GetUserStarList(userid);
1216 if (favorites == null) {
1217 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到用户收藏
1218 }
1219
1220 String json = mapper.writeValueAsString(favorites);
1221 return new ResponseEntity<>(json, headers, HttpStatus.OK);
1222 } catch (JsonProcessingException e) {
1223 e.printStackTrace();
1224 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
1225 } catch (Exception e) {
1226 e.printStackTrace();
1227 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
1228 }
1229 }
1230
1231 @Override
1232 @CrossOrigin(origins = "*", allowedHeaders = "*")
1233 public ResponseEntity<Integer> removeFavorite(
1234 @RequestBody String requestBody
1235 ) {
1236 try {
1237 // 解析 JSON 数据
1238 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1239 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1240 com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seedid");
1241 if (useridNode == null || seedidNode == null) {
1242 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1243 }
1244
1245 String userid = useridNode.asText();
1246 String seedid = seedidNode.asText();
1247
1248 // 添加参数验证
1249 if (userid == null || userid.trim().isEmpty() ||
1250 seedid == null || seedid.trim().isEmpty()) {
1251 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1252 }
1253
1254 boolean ret = db1.DeleteCollect(userid, seedid);
1255 if (ret) {
1256 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1257 } else {
1258 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1259 }
1260 } catch (JsonProcessingException e) {
1261 e.printStackTrace();
1262 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1263 } catch (Exception e) {
1264 e.printStackTrace();
1265 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1266 }
1267 }
1268
1269 @Override
1270 @CrossOrigin(origins = "*", allowedHeaders = "*")
1271 public ResponseEntity<Integer> addFavorite(
1272 @RequestBody String requestBody
1273 ) {
1274 try {
1275 // 解析 JSON 数据
1276 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1277 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1278 com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seedid");
1279 if (useridNode == null || seedidNode == null) {
1280 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1281 }
1282
1283 String userid = useridNode.asText();
1284 String seedid = seedidNode.asText();
1285
1286 // 添加参数验证
1287 if (userid == null || userid.trim().isEmpty() ||
1288 seedid == null || seedid.trim().isEmpty()) {
1289 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1290 }
1291
1292 boolean ret = db1.AddCollect(userid, seedid);
1293 if (ret) {
1294 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1295 } else {
1296 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1297 }
1298 } catch (JsonProcessingException e) {
1299 e.printStackTrace();
1300 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1301 } catch (Exception e) {
1302 e.printStackTrace();
1303 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1304 }
1305 }
1306
1307 @Override
1308 @CrossOrigin(origins = "*", allowedHeaders = "*")
1309 public ResponseEntity<Integer> migrateAccount(
1310 @RequestParam("userid") String userid,
rhj7912f2c2025-06-09 17:21:32 +08001311 @RequestParam("file") MultipartFile file,
1312 @RequestParam("uploadtogive") String uploadtogive
rhje18c3f72025-06-08 00:27:01 +08001313 ) {
1314 try {
1315 // 先进行参数验证
1316 if (userid == null || userid.trim().isEmpty() ||
rhj7912f2c2025-06-09 17:21:32 +08001317 file == null || file.isEmpty() ||
1318 uploadtogive == null || uploadtogive.trim().isEmpty()) {
rhje18c3f72025-06-08 00:27:01 +08001319 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1320 }
1321
1322 if (file.getSize() > 100 * 1024 * 1024) {
rhje18c3f72025-06-08 00:27:01 +08001323 return new ResponseEntity<>(3, HttpStatus.PAYLOAD_TOO_LARGE); // 返回 3 表示文件过大
1324 }
1325
1326 File tempFile = File.createTempFile(userid, file.getOriginalFilename());
1327 file.transferTo(tempFile);
1328
rhj7912f2c2025-06-09 17:21:32 +08001329 int ret = db1.UploadMigration(userid, tempFile, uploadtogive);
rhje18c3f72025-06-08 00:27:01 +08001330 if (ret == 0) {
1331 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1332 } else if (ret == 1) {
1333 return new ResponseEntity<>(1, HttpStatus.CONFLICT); // 返回 1 表示用户已存在或其他错误
1334 } else {
1335 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1336 }
1337 } catch (JsonProcessingException e) {
1338 e.printStackTrace();
1339 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1340 } catch (Exception e) {
1341 e.printStackTrace();
1342 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1343 }
1344 }
1345
1346 @Override
1347 public ResponseEntity<String> getBegSeedList() {
1348 try {
1349 BegSeedDetail[] begSeedList = db1.GetBegList();
1350 if (begSeedList == null) {
1351 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到种子列表
1352 }
1353 String json = mapper.writeValueAsString(begSeedList);
1354 return new ResponseEntity<>(json, headers, HttpStatus.OK);
1355 } catch (Exception e) {
1356 e.printStackTrace();
1357 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
1358 }
1359 }
1360
1361 @Override
1362 public ResponseEntity<String> getBegSeedDetail(
1363 @RequestParam("begid") String begid
1364 ) {
1365 try {
rhje18c3f72025-06-08 00:27:01 +08001366 if (begid == null || begid.trim().isEmpty()) {
1367 return new ResponseEntity<>("", errorHeaders, HttpStatus.BAD_REQUEST); // 返回 400 表示参数不完整
1368 }
1369
1370 BegSeedDetail begSeedDetail = db1.GetBegSeedDetail(begid);
1371 if (begSeedDetail == null) {
1372 return new ResponseEntity<>("", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到种子详情
1373 }
1374 String json = mapper.writeValueAsString(begSeedDetail);
1375 return new ResponseEntity<>(json, headers, HttpStatus.OK);
1376 } catch (JsonProcessingException e) {
1377 e.printStackTrace();
1378 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
1379 } catch (Exception e) {
1380 e.printStackTrace();
1381 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
1382 }
1383 }
1384
1385 @Override
1386 public ResponseEntity<String> getBegSeedSubmissions(
1387 @RequestParam("begid") String begid
1388 ) {
1389 try {
1390 if (begid == null || begid.trim().isEmpty()) {
1391 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.BAD_REQUEST); // 返回 400 表示参数不完整
1392 }
1393
1394 SeedWithVotes[] submissionsWithVotes = db1.GetBegSeedListWithVotes(begid);
1395 if (submissionsWithVotes == null) {
1396 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到提交记录
1397 }
1398
1399 String json = mapper.writeValueAsString(submissionsWithVotes);
1400 return new ResponseEntity<>(json, headers, HttpStatus.OK);
1401 } catch (JsonProcessingException e) {
1402 e.printStackTrace();
1403 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
1404 } catch (Exception e) {
1405 e.printStackTrace();
1406 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
1407 }
1408 }
1409
1410 @Override
1411 @CrossOrigin(origins = "*", allowedHeaders = "*")
1412 public ResponseEntity<Integer> submitSeed(
1413 @RequestBody String requestBody
1414 ) {
1415 try {
1416 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1417 com.fasterxml.jackson.databind.JsonNode begidNode = jsonNode.get("begid");
1418 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1419 com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seedid");
1420
1421 if (begidNode == null || useridNode == null || seedidNode == null) {
1422 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1423 }
1424
1425 String begid = begidNode.asText();
1426 String userid = useridNode.asText();
1427 String seedid = seedidNode.asText();
1428
1429 if (begid == null || begid.trim().isEmpty() ||
1430 userid == null || userid.trim().isEmpty() ||
1431 seedid == null || seedid.trim().isEmpty()) {
1432 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1433 }
1434
1435 int ret = db1.SubmitBegSeed(begid, seedid, userid);
1436 if (ret == 0) {
1437 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1438 } else if (ret == 1) {
1439 return new ResponseEntity<>(1, HttpStatus.CONFLICT); // 返回 1 表示提交失败或其他错误
1440 } else {
1441 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1442 }
1443 } catch (Exception e) {
1444 e.printStackTrace();
1445 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1446 }
1447 }
1448
1449 @Override
1450 @CrossOrigin(origins = "*", allowedHeaders = "*")
1451 public ResponseEntity<Integer> voteSeed(
1452 @RequestBody String requestBody
1453 ) {
1454 try {
1455 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1456 com.fasterxml.jackson.databind.JsonNode begidNode = jsonNode.get("begid");
1457 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1458 com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seedid");
1459
1460 if (begidNode == null || useridNode == null || seedidNode == null) {
1461 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1462 }
1463
1464 String begid = begidNode.asText();
1465 String userid = useridNode.asText();
1466 String seedid = seedidNode.asText();
1467
1468 if (begid == null || begid.trim().isEmpty() ||
1469 userid == null || userid.trim().isEmpty() ||
1470 seedid == null || seedid.trim().isEmpty()) {
1471 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1472 }
1473
1474 int ret = db1.VoteSeed(begid, seedid, userid);
1475 if (ret == 0) {
1476 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1477 } else if (ret == 1) {
1478 return new ResponseEntity<>(3, HttpStatus.CONFLICT); // 返回 1 表示投票失败或其他错误
1479 } else {
1480 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1481 }
1482 } catch (Exception e) {
1483 e.printStackTrace();
1484 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1485 }
1486 }
1487
1488 @Override
1489 @CrossOrigin(origins = "*", allowedHeaders = "*")
1490 public ResponseEntity<Integer> createBegSeed(
1491 @RequestBody String requestBody
1492 ) {
1493 try {
1494 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1495 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1496 com.fasterxml.jackson.databind.JsonNode infoNode = jsonNode.get("info");
1497 com.fasterxml.jackson.databind.JsonNode rewardNode = jsonNode.get("reward_magic");
1498 com.fasterxml.jackson.databind.JsonNode dedlineNode = jsonNode.get("deadline");
1499
1500 if (useridNode == null || infoNode == null || rewardNode == null || dedlineNode == null) {
1501 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1502 }
1503
1504 String userid = useridNode.asText();
1505 String info = infoNode.asText();
1506
1507 if (userid == null || userid.trim().isEmpty() ||
1508 info == null || info.trim().isEmpty() ||
1509 rewardNode.asInt() <= 0 ){
1510 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1511 }
1512
1513 // 解析日期字符串
1514 Date endTime;
1515 try {
1516 String deadlineStr = dedlineNode.asText();
1517 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
1518 endTime = dateFormat.parse(deadlineStr);
1519 } catch (ParseException e) {
1520 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 日期格式错误
1521 }
1522
1523 BegInfo beg = new BegInfo();
1524 beg.begid = java.util.UUID.randomUUID().toString();
1525 beg.begnumbers = 0; // 初始提交数为0
1526 beg.magic = rewardNode.asInt();
1527 beg.endtime = endTime;
1528 beg.hasseed = 0; // 初始状态为未开始
1529
1530 // int ret = db1.AddBegSeed(beg);
1531 int ret = db1.createBagSeed(beg, userid, info);
1532 if (ret == 0) {
1533 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1534 } else if (ret == 1) {
1535 return new ResponseEntity<>(2, HttpStatus.CONFLICT); // 返回 1 表示创建失败或其他错误
1536 } else {
1537 return new ResponseEntity<>(3, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1538 }
1539 } catch (Exception e) {
1540 e.printStackTrace();
1541 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1542 }
1543 }
rhj7912f2c2025-06-09 17:21:32 +08001544
1545 @Override
1546 public ResponseEntity<String> getAllSeeds(){
1547 try {
1548 Seed[] allSeeds = db1.getAllSeeds();
1549 if (allSeeds == null) {
1550 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到种子列表
1551 }
1552 String json = mapper.writeValueAsString(allSeeds);
1553 return new ResponseEntity<>(json, headers, HttpStatus.OK);
1554 } catch (JsonProcessingException e) {
1555 e.printStackTrace();
1556 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
1557 } catch (Exception e) {
1558 e.printStackTrace();
1559 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
1560 }
1561 }
1562
1563 @Override
1564 public ResponseEntity<String> getAllSeedPromotions() {
1565 try {
1566 SeedPromotion[] promotions = db1.getAllSeedPromotions();
1567 if (promotions == null) {
1568 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到种子推广
1569 }
1570 String json = mapper.writeValueAsString(promotions);
1571 return new ResponseEntity<>(json, headers, HttpStatus.OK);
1572 } catch (JsonProcessingException e) {
1573 e.printStackTrace();
1574 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
1575 } catch (Exception e) {
1576 e.printStackTrace();
1577 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
1578 }
1579 }
1580
1581 @Override
1582 @CrossOrigin(origins = "*", allowedHeaders = "*")
1583 public ResponseEntity<Integer> createSeedPromotion(
1584 @RequestBody String requestBody
1585 ) {
1586 try {
1587 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1588 com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seed_id");
1589 com.fasterxml.jackson.databind.JsonNode startTimeNode = jsonNode.get("start_time");
1590 com.fasterxml.jackson.databind.JsonNode endTimeNode = jsonNode.get("end_time");
1591 com.fasterxml.jackson.databind.JsonNode discountNode = jsonNode.get("discount");
1592
1593 if (seedidNode == null || startTimeNode == null || endTimeNode == null || discountNode == null) {
1594 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1595 }
1596
1597 String seedid = seedidNode.asText();
1598 Date startTime;
1599 Date endTime;
1600 Integer discount = discountNode.asInt();
1601 if (discount == null || discount < 0) {
1602 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 折扣参数错误
1603 }
1604 try {
1605 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
1606 String startTimeStr = startTimeNode.asText();
1607 startTime = dateFormat.parse(startTimeStr);
1608 String endTimeStr = endTimeNode.asText();
1609 endTime = dateFormat.parse(endTimeStr);
1610 } catch (ParseException e) {
1611 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 日期格式错误
1612 }
1613
1614 if (seedid == null || seedid.trim().isEmpty() ||
1615 startTime == null || endTime == null) {
1616 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1617 }
1618
1619 int ret = db1.createSeedPromotion(seedid, startTime, endTime, discount);
1620 if (ret == 0) {
1621 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1622 } else if (ret == 1) {
1623 return new ResponseEntity<>(2, HttpStatus.CONFLICT); // 返回 1 表示创建失败或其他错误
1624 } else {
1625 return new ResponseEntity<>(3, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1626 }
1627 } catch (JsonProcessingException e) {
1628 e.printStackTrace();
1629 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1630 } catch (Exception e) {
1631 e.printStackTrace();
1632 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1633 }
1634 }
Raverf79fdb62025-06-03 06:02:49 +00001635}