blob: 2c37f8b57fdb2616b69e30bb3ef8bd12b7158979 [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;
11
Raverf79fdb62025-06-03 06:02:49 +000012import org.springframework.core.io.FileSystemResource;
13import org.springframework.core.io.Resource;
14import org.springframework.http.HttpHeaders;
Raveraae06122025-06-05 08:13:35 +000015import org.springframework.http.HttpStatus;
Raverf79fdb62025-06-03 06:02:49 +000016import org.springframework.http.MediaType;
17import org.springframework.http.ResponseEntity;
18import org.springframework.web.bind.annotation.RequestParam;
19import org.springframework.web.bind.annotation.RestController;
20import org.springframework.web.multipart.MultipartFile;
Raveraae06122025-06-05 08:13:35 +000021import org.springframework.web.bind.annotation.CrossOrigin;
rhjc6a4ee02025-06-06 00:45:18 +080022import org.springframework.web.bind.annotation.RequestBody;
Raveraae06122025-06-05 08:13:35 +000023
24import com.fasterxml.jackson.core.JsonProcessingException;
25import com.fasterxml.jackson.databind.ObjectMapper;
Raverf79fdb62025-06-03 06:02:49 +000026
27import database.Database1;
Raverf79fdb62025-06-03 06:02:49 +000028import tracker.Tracker;
rhje18c3f72025-06-08 00:27:01 +080029import cheat.Cheat;
Raverf79fdb62025-06-03 06:02:49 +000030
rhjc6a4ee02025-06-06 00:45:18 +080031import entity.Seed;
32import entity.User;
rhj46f62c42025-06-06 23:24:10 +080033import entity.Post;
rhje18c3f72025-06-08 00:27:01 +080034import entity.BegSeedDetail;
rhj5b69b7e2025-06-07 01:28:08 +080035import entity.PostReply;
rhje18c3f72025-06-08 00:27:01 +080036import entity.Profile;
rhj5ebd93c2025-06-07 15:57:28 +080037import entity.UserPT;
rhje18c3f72025-06-08 00:27:01 +080038import entity.config;
39import entity.Appeal;
40import entity.BegInfo;
41import entity.BegInfoDetail;
42import entity.UserStar;
43import entity.SeedWithVotes;
rhjc6a4ee02025-06-06 00:45:18 +080044
45import java.util.UUID;
Raveraae06122025-06-05 08:13:35 +000046
Raverf79fdb62025-06-03 06:02:49 +000047@RestController
48public class ApiController implements ApiInterface {
49
Raveraae06122025-06-05 08:13:35 +000050 private static Database1 db1;
Raverf79fdb62025-06-03 06:02:49 +000051 private static Tracker tracker;
rhje18c3f72025-06-08 00:27:01 +080052 private static Cheat cheat;
Raveraae06122025-06-05 08:13:35 +000053 private static ObjectMapper mapper;
rhjc6a4ee02025-06-06 00:45:18 +080054 private static HttpHeaders headers;
55 private static HttpHeaders errorHeaders;
Raveraae06122025-06-05 08:13:35 +000056
57 @PostConstruct
58 public void init() {
rhje18c3f72025-06-08 00:27:01 +080059 cheat = new Cheat();
Raveraae06122025-06-05 08:13:35 +000060 db1 = new Database1();
61 tracker = new Tracker();
62 mapper = new ObjectMapper();
63 mapper.configure(com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
rhjc6a4ee02025-06-06 00:45:18 +080064 headers = new HttpHeaders();
65 headers.add("Access-Control-Allow-Origin", "*");
66 headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
67 headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
68 errorHeaders = new HttpHeaders();
69 errorHeaders.add("Access-Control-Allow-Origin", "*");
70 errorHeaders.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
71 errorHeaders.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
Raveraae06122025-06-05 08:13:35 +000072 }
Raverf79fdb62025-06-03 06:02:49 +000073
74 @Override
75 public ResponseEntity<Integer> saveTorrent(
76 @RequestParam("userid") String userid,
77 @RequestParam("title") String title,
78 @RequestParam("tag") String tag,
79 @RequestParam("file") MultipartFile file
80 ) {
81 try {
82 Seed seed = new Seed();
rhjc6a4ee02025-06-06 00:45:18 +080083 seed.seedid = UUID.randomUUID().toString(); // 生成唯一的种子ID
84 seed.seeduserid = userid;
85 seed.title = title;
86 seed.seedsize = "1GB";
87 seed.seedtag = tag;
Raverf79fdb62025-06-03 06:02:49 +000088 seed.url = "http://example.com/torrent"; // 示例URL
rhjc6a4ee02025-06-06 00:45:18 +080089 System.out.println("seed is null? " + (seed == null));
Raveraae06122025-06-05 08:13:35 +000090 int ret = db1.RegisterSeed(seed);
rhjc6a4ee02025-06-06 00:45:18 +080091 if (ret != 0) {
92 // 如果注册种子失败,返回错误状态
93 return new ResponseEntity<>(ret, headers, HttpStatus.INTERNAL_SERVER_ERROR);
94 }
rhje18c3f72025-06-08 00:27:01 +080095 File tempFile = File.createTempFile(userid, file.getOriginalFilename());
Raverf79fdb62025-06-03 06:02:49 +000096 file.transferTo(tempFile);
97 tracker.SaveTorrent(seed.seedid, tempFile);
Raveraae06122025-06-05 08:13:35 +000098 return new ResponseEntity<>(0, headers, HttpStatus.OK); // 返回 0 表示成功
Raverf79fdb62025-06-03 06:02:49 +000099 } catch (Exception e) {
100 e.printStackTrace();
Raveraae06122025-06-05 08:13:35 +0000101 return new ResponseEntity<>(1, errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示失败
Raverf79fdb62025-06-03 06:02:49 +0000102 }
103 }
104
105 @Override
Raveraae06122025-06-05 08:13:35 +0000106 @CrossOrigin(origins = "*", allowedHeaders = "*") // 允许所有来源和头部
Raverf79fdb62025-06-03 06:02:49 +0000107 public ResponseEntity<Resource> getTorrent(
rhjc6a4ee02025-06-06 00:45:18 +0800108 @RequestParam("torrentId") String seedid,
109 @RequestParam("userId") String userid
Raverf79fdb62025-06-03 06:02:49 +0000110 ) {
Raveraae06122025-06-05 08:13:35 +0000111 File file = tracker.GetTTorent(seedid, userid);
Raverf79fdb62025-06-03 06:02:49 +0000112 if (file != null) {
113 FileSystemResource resource = new FileSystemResource(file);
114 return ResponseEntity.ok()
115 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"")
Raveraae06122025-06-05 08:13:35 +0000116 .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) // 关键:允许前端访问Content-Disposition
Raverf79fdb62025-06-03 06:02:49 +0000117 .contentType(MediaType.APPLICATION_OCTET_STREAM)
118 .body(resource);
119 } else {
Raveraae06122025-06-05 08:13:35 +0000120 return ResponseEntity.notFound().build();
121 }
122 }
123
Raveraae06122025-06-05 08:13:35 +0000124 @Override
125 public ResponseEntity<String> getSeedListByTag(
126 @RequestParam("tag") String tag
127 ) {
128 try {
129 Seed[] seeds = db1.GetSeedListByTag(tag);
rhje18c3f72025-06-08 00:27:01 +0800130 if (seeds == null) {
rhjc6a4ee02025-06-06 00:45:18 +0800131 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 404 表示未找到种子
132 }
Raveraae06122025-06-05 08:13:35 +0000133 String json = mapper.writeValueAsString(seeds);
Raveraae06122025-06-05 08:13:35 +0000134 return new ResponseEntity<>(json, headers, HttpStatus.OK);
135 } catch (JsonProcessingException e) {
136 e.printStackTrace();
Raveraae06122025-06-05 08:13:35 +0000137 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
138 }
139 }
140
141 @Override
rhjc6a4ee02025-06-06 00:45:18 +0800142 public ResponseEntity<String> getUserProfile(
143 @RequestParam("userid") String userid
144 ) {
145 try {
146 User user = db1.GetInformation(userid);
147 if (user == null) {
148 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
149 }
150 String json = mapper.writeValueAsString(user);
151 return new ResponseEntity<>(json, headers, HttpStatus.OK);
152 } catch (JsonProcessingException e) {
153 e.printStackTrace();
154 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
155 }
156 }
157
158 @Override
159 @CrossOrigin(origins = "*", allowedHeaders = "*")
160 public ResponseEntity<Integer> changeProfile(
161 @RequestBody String requestBody
162 ) {
163 try {
164 // 解析 JSON 数据
165 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
166
167 // 安全地获取 userid 字段
168 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
169 if (useridNode == null) {
170 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
171 }
172 String userid = useridNode.asText();
173
174 // 添加参数验证
175 if (userid == null || userid.trim().isEmpty()) {
176 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
177 }
178
179 // 手动映射前端字段到 User 对象,处理类型转换
180 User user = new User();
181 user.userid = userid;
182
183 // 安全地获取其他字段并进行类型转换
184 if (jsonNode.has("username") && !jsonNode.get("username").isNull()) {
185 user.username = jsonNode.get("username").asText();
186 }
187 if (jsonNode.has("school") && !jsonNode.get("school").isNull()) {
188 user.school = jsonNode.get("school").asText();
189 }
190 if (jsonNode.has("gender") && !jsonNode.get("gender").isNull()) {
191 user.sex = jsonNode.get("gender").asText();
192 }
193 if (jsonNode.has("avatar_url") && !jsonNode.get("avatar_url").isNull()) {
194 user.pictureurl = jsonNode.get("avatar_url").asText();
195 }
196
197 // 处理 account_status 的类型转换(字符串/数字 -> 布尔值)
198 if (jsonNode.has("account_status") && !jsonNode.get("account_status").isNull()) {
199 com.fasterxml.jackson.databind.JsonNode statusNode = jsonNode.get("account_status");
200 if (statusNode.isTextual()) {
201 String statusStr = statusNode.asText();
202 user.accountstate = "1".equals(statusStr) || "封禁".equals(statusStr);
203 } else if (statusNode.isNumber()) {
204 user.accountstate = statusNode.asInt() == 1;
205 } else if (statusNode.isBoolean()) {
206 user.accountstate = statusNode.asBoolean();
207 }
208 }
209
210 // 处理 invite_left 的类型转换(字符串 -> 整数)
211 if (jsonNode.has("invite_left") && !jsonNode.get("invite_left").isNull()) {
212 com.fasterxml.jackson.databind.JsonNode inviteNode = jsonNode.get("invite_left");
213 if (inviteNode.isTextual()) {
214 try {
215 user.invitetimes = Integer.parseInt(inviteNode.asText());
216 } catch (NumberFormatException e) {
217 user.invitetimes = 0; // 默认值
218 }
219 } else if (inviteNode.isNumber()) {
220 user.invitetimes = inviteNode.asInt();
221 }
222 }
223
224 int ret = db1.UpdateInformation(user);
225 if (ret == 0) {
226 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
227 } else {
228 return new ResponseEntity<>(ret, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
229 }
230 } catch (JsonProcessingException e) {
231 e.printStackTrace();
232 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
233 } catch (Exception e) {
234 e.printStackTrace();
235 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
236 }
237 }
238
239 @Override
240 public ResponseEntity<String> getUserSeeds(
241 @RequestParam("userid") String userid
242 ) {
243 try {
244 Seed[] seeds = db1.GetSeedListByUser(userid);
rhje18c3f72025-06-08 00:27:01 +0800245 if (seeds == null) {
rhjc6a4ee02025-06-06 00:45:18 +0800246 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 404 表示未找到种子
247 }
248 String json = mapper.writeValueAsString(seeds);
249 return new ResponseEntity<>(json, headers, HttpStatus.OK);
250 } catch (JsonProcessingException e) {
251 e.printStackTrace();
252 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
253 }
254 }
255
256 @Override
257 @CrossOrigin(origins = "*", allowedHeaders = "*")
258 public ResponseEntity<Integer> deleteSeed(
259 @RequestBody String requestBody
260 ) {
261 try {
262 // 解析 JSON 数据
263 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
264 com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seedid");
265 if (seedidNode == null) {
266 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
267 }
268 String seedid = seedidNode.asText();
269
270 // 添加参数验证
271 if (seedid == null || seedid.trim().isEmpty()) {
272 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
273 }
274
275 int ret = db1.DeleteSeed(seedid);
276 if (ret == 0) {
277 return new ResponseEntity<>(0, HttpStatus.OK);
278 } else {
279 return new ResponseEntity<>(ret, HttpStatus.INTERNAL_SERVER_ERROR);
280 }
281 } catch (Exception e) {
282 e.printStackTrace();
283 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
284 }
285 }
286
287 @Override
288 public ResponseEntity<String> getUserStat(
289 @RequestParam("userid") String userid
290 ) {
291 try {
292 User user = db1.GetInformation(userid);
293 if (user == null) {
294 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
295 }
296 String json = mapper.writeValueAsString(user);
297 return new ResponseEntity<>(json, headers, HttpStatus.OK);
298 } catch (JsonProcessingException e) {
299 e.printStackTrace();
300 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
301 }
302 }
303
304 @Override
Raveraae06122025-06-05 08:13:35 +0000305 public ResponseEntity<String> getTorrentDetail(
306 @RequestParam("id") String seedid
307 ) {
308 try {
309 Seed seed = db1.GetSeedInformation(seedid);
310 if (seed != null) {
311 String json = mapper.writeValueAsString(seed);
312 HttpHeaders headers = new HttpHeaders();
313 headers.add("Access-Control-Allow-Origin", "*");
314 headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
315 headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
316 return new ResponseEntity<>(json, headers, HttpStatus.OK);
317 } else {
318 return ResponseEntity.notFound().build(); // 返回 404 表示种子未找到
319 }
320 } catch (JsonProcessingException e) {
321 e.printStackTrace();
322 HttpHeaders errorHeaders = new HttpHeaders();
323 errorHeaders.add("Access-Control-Allow-Origin", "*");
324 errorHeaders.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
325 errorHeaders.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
326 return new ResponseEntity<>("{}", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
Raverf79fdb62025-06-03 06:02:49 +0000327 }
328 }
rhj46f62c42025-06-06 23:24:10 +0800329
330 @Override
331 @CrossOrigin(origins = "*", allowedHeaders = "*")
332 public ResponseEntity<String> loginUser(
333 @RequestBody String requestBody
334 ) {
335 try {
336 // 解析前端发送的JSON数据 {email: xxx, password: xxx}
337 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
338
339 // 获取email和password字段
340 com.fasterxml.jackson.databind.JsonNode emailNode = jsonNode.get("email");
341 com.fasterxml.jackson.databind.JsonNode passwordNode = jsonNode.get("password");
342
343 if (emailNode == null || passwordNode == null) {
344 com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
345 errorJson.put("message", "缺少必要参数");
346 String jsonError = mapper.writeValueAsString(errorJson);
347 return new ResponseEntity<>(jsonError, HttpStatus.BAD_REQUEST);
348 }
349
350 String email = emailNode.asText();
351 String password = passwordNode.asText();
352
353 // 参数验证
354 if (email == null || email.trim().isEmpty() ||
355 password == null || password.trim().isEmpty()) {
356 com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
357 errorJson.put("message", "邮箱和密码不能为空");
358 String jsonError = mapper.writeValueAsString(errorJson);
359 return new ResponseEntity<>(jsonError, HttpStatus.BAD_REQUEST);
360 }
361
362 // 创建User对象进行登录验证
363 User user = new User();
364 user.email = email.trim();
365 user.password = password;
366
367 String userid = db1.LoginUser(user);
368 if (userid != null) {
369 com.fasterxml.jackson.databind.node.ObjectNode responseJson = mapper.createObjectNode();
370 responseJson.put("userId", userid);
371 responseJson.put("userid", userid);
372 responseJson.put("message", "登录成功");
373 String jsonResponse = mapper.writeValueAsString(responseJson);
374 return new ResponseEntity<>(jsonResponse, HttpStatus.OK);
375 } else {
376 // 返回JSON格式的错误信息
377 com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
378 errorJson.put("message", "登录失败,请检查账号密码");
379 String jsonError = mapper.writeValueAsString(errorJson);
380 return new ResponseEntity<>(jsonError, HttpStatus.UNAUTHORIZED);
381 }
382 } catch (JsonProcessingException e) {
383 e.printStackTrace();
384 com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
385 try {
386 errorJson.put("message", "服务器内部错误");
387 String jsonError = mapper.writeValueAsString(errorJson);
388 return new ResponseEntity<>(jsonError, HttpStatus.INTERNAL_SERVER_ERROR);
389 } catch (JsonProcessingException ex) {
390 return new ResponseEntity<>("{\"message\":\"服务器内部错误\"}", HttpStatus.INTERNAL_SERVER_ERROR);
391 }
392 } catch (Exception e) {
393 e.printStackTrace();
394 com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
395 try {
396 errorJson.put("message", "服务器内部错误");
397 String jsonError = mapper.writeValueAsString(errorJson);
398 return new ResponseEntity<>(jsonError, HttpStatus.INTERNAL_SERVER_ERROR);
399 } catch (JsonProcessingException ex) {
400 return new ResponseEntity<>("{\"message\":\"服务器内部错误\"}", HttpStatus.INTERNAL_SERVER_ERROR);
401 }
402 }
403 }
404
405 @Override
406 @CrossOrigin(origins = "*", allowedHeaders = "*")
407 public ResponseEntity<Integer> registerUser(
408 @RequestBody String requestBody
409 ) {
410 try {
411 System.out.println("Register request body: " + requestBody);
412 // 解析 JSON 数据
413 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
414
415 // 安全地获取字段
416 com.fasterxml.jackson.databind.JsonNode usernameNode = jsonNode.get("username");
417 com.fasterxml.jackson.databind.JsonNode passwordNode = jsonNode.get("password");
418 com.fasterxml.jackson.databind.JsonNode inviteEmailNode = jsonNode.get("invite_email");
419
420 if (usernameNode == null || passwordNode == null || inviteEmailNode == null) {
421 return new ResponseEntity<>(2, HttpStatus.BAD_REQUEST); // 参数不完整
422 }
423
424 String username = usernameNode.asText();
425 String password = passwordNode.asText();
426 String inviteEmail = inviteEmailNode.asText();
427
428 // 参数验证
429 if (username == null || username.trim().isEmpty() ||
430 password == null || password.trim().isEmpty() ||
431 inviteEmail == null || inviteEmail.trim().isEmpty()) {
432 return new ResponseEntity<>(2, HttpStatus.BAD_REQUEST);
433 }
434
435 // 创建 User 对象
436 User user = new User();
437 user.userid = java.util.UUID.randomUUID().toString(); // 生成唯一用户ID
438 user.username = username.trim();
439 user.password = password;
440 user.email = inviteEmail.trim(); // 使用邀请邮箱作为用户邮箱
441
442 // 设置默认值
443 user.sex = "m"; // 默认性别
444 user.school = ""; // 默认学校
445 user.pictureurl = ""; // 默认头像URL
446 user.profile = ""; // 默认个人简介
447 user.accountstate = false; // 默认账号状态为正常
448 user.invitetimes = 5; // 默认邀请次数
449
450 // 设置时间字段
451 user.lastDetectedTime = new java.util.Date();
452 user.fakeLastDetectedTime = new java.util.Date();
453
454 // 调用数据库注册方法
455 int ret = db1.RegisterUser(user);
456 System.out.println("Register result: " + ret);
457
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");
996 if (migrationidNode == null) {
997 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
998 }
999 String migrationid = migrationidNode.asText();
1000 System.out.println("Migration ID: " + migrationid);
1001
1002 // 添加参数验证
1003 if (migrationid == null || migrationid.trim().isEmpty()) {
1004 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1005 }
1006
1007 boolean ret = db1.ExamTransmitProfile(migrationid, true);
1008 System.out.println("Migration approval result: " + ret);
1009 if (ret) {
1010 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1011 } else {
1012 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1013 }
1014 } catch (JsonProcessingException e) {
1015 e.printStackTrace();
1016 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1017 } catch (Exception e) {
1018 e.printStackTrace();
1019 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1020 }
1021 }
1022
1023 @Override
1024 @CrossOrigin(origins = "*", allowedHeaders = "*")
1025 public ResponseEntity<Integer> rejectMigration(
1026 @RequestBody String requestBody
1027 ) {
1028 try {
1029 // 解析 JSON 数据
1030 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1031 com.fasterxml.jackson.databind.JsonNode migrationidNode = jsonNode.get("migration_id");
1032 if (migrationidNode == null) {
1033 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1034 }
1035 String migrationid = migrationidNode.asText();
1036
1037 // 添加参数验证
1038 if (migrationid == null || migrationid.trim().isEmpty()) {
1039 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1040 }
1041
1042 boolean ret = db1.ExamTransmitProfile(migrationid, false);
1043 if (ret) {
1044 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1045 } else {
1046 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1047 }
1048 } catch (JsonProcessingException e) {
1049 e.printStackTrace();
1050 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1051 } catch (Exception e) {
1052 e.printStackTrace();
1053 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1054 }
1055 }
1056
1057 @Override
1058 @CrossOrigin(origins = "*", allowedHeaders = "*")
1059 public ResponseEntity<Integer> inviteUser(
1060 @RequestBody String requestBody
1061 ) {
1062 try {
1063 // 解析 JSON 数据
1064 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1065 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1066 com.fasterxml.jackson.databind.JsonNode emailNode = jsonNode.get("invite_email");
1067 if (useridNode == null || emailNode == null) {
1068 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1069 }
1070 String userid = useridNode.asText();
1071 String email = emailNode.asText();
1072
1073 // 添加参数验证
1074 if (email == null || email.trim().isEmpty()) {
1075 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1076 }
1077
1078 int ret = db1.InviteNewUser(userid, email);
1079 if (ret == 0) {
1080 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1081 } else if (ret == 1) {
1082 return new ResponseEntity<>(1, HttpStatus.CONFLICT); // 返回 1 表示邀请已存在
1083 } else {
1084 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1085 }
1086 } catch (JsonProcessingException e) {
1087 e.printStackTrace();
1088 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1089 } catch (Exception e) {
1090 e.printStackTrace();
1091 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1092 }
1093 }
1094
1095 @Override
1096 @CrossOrigin(origins = "*", allowedHeaders = "*")
1097 public ResponseEntity<Integer> submitAppeal(
1098 @RequestParam("userid") String userid,
1099 @RequestParam("content") String content,
1100 @RequestParam("file") MultipartFile file
1101 ) {
1102 try {
1103 System.out.println("Submit appeal request: userid=" + userid + ", content=" + content);
1104 // 先进行参数验证
1105 if (userid == null || userid.trim().isEmpty() ||
1106 content == null || content.trim().isEmpty() ||
1107 file == null || file.isEmpty()) {
1108 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1109 }
1110
1111 if (file.getSize() > 100 * 1024 * 1024) {
1112 System.out.println("File size exceeds limit: " + file.getSize() + " bytes");
1113 return new ResponseEntity<>(3, HttpStatus.PAYLOAD_TOO_LARGE); // 返回 3 表示文件过大
1114 }
1115
1116 System.out.println("Submit appeal request: userid=" + userid + ", content=" + content);
1117 File tempFile = File.createTempFile(userid, file.getOriginalFilename());
1118 file.transferTo(tempFile);
1119
1120 int ret = cheat.SubmitAppeal(userid, content, tempFile);
1121 System.out.println("Submit appeal result: " + ret);
1122 if (ret == 0) {
1123 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1124 } else if (ret == 1) {
1125 return new ResponseEntity<>(1, HttpStatus.CONFLICT); // 返回 1 表示用户未被封禁或其他错误
1126 } else {
1127 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1128 }
1129 } catch (org.springframework.web.multipart.MaxUploadSizeExceededException e) {
1130 System.out.println("File upload size exceeded: " + e.getMessage());
1131 return new ResponseEntity<>(3, HttpStatus.PAYLOAD_TOO_LARGE); // 返回 3 表示文件过大
1132 } catch (JsonProcessingException e) {
1133 e.printStackTrace();
1134 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1135 } catch (Exception e) {
1136 e.printStackTrace();
1137 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1138 }
1139 }
1140
1141 @Override
1142 public ResponseEntity<String> getUserStats(
1143 @RequestParam("userid") String userid
1144 ) {
1145 try {
1146 if (userid == null || userid.trim().isEmpty()) {
1147 return new ResponseEntity<>("", errorHeaders, HttpStatus.BAD_REQUEST); // 返回 400 表示参数不完整
1148 }
1149
1150 UserPT userPT = db1.GetInformationPT(userid);
1151 if (userPT == null) {
1152 return new ResponseEntity<>("", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到用户PT信息
1153 }
1154
1155 String json = mapper.writeValueAsString(userPT);
1156 return new ResponseEntity<>(json, headers, HttpStatus.OK);
1157 } catch (JsonProcessingException e) {
1158 e.printStackTrace();
1159 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
1160 } catch (Exception e) {
1161 e.printStackTrace();
1162 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
1163 }
1164 }
1165
1166 @Override
1167 @CrossOrigin(origins = "*", allowedHeaders = "*")
1168 public ResponseEntity<Integer> magicExchange(
1169 @RequestBody String requestBody
1170 ) {
1171 try {
1172 // 解析 JSON 数据
1173 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1174 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1175 com.fasterxml.jackson.databind.JsonNode magicNode = jsonNode.get("magic");
1176 com.fasterxml.jackson.databind.JsonNode typeNode = jsonNode.get("exchangeType");
1177
1178 if (useridNode == null || magicNode == null || typeNode == null) {
1179 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1180 }
1181 String userid = useridNode.asText();
1182 int magic = magicNode.asInt();
1183 String exchangeType = typeNode.asText();
1184
1185 boolean ret = false;
1186 if( "uploaded".equals(exchangeType) ) {
1187 ret = db1.ExchangeMagicToUpload(userid, magic);
1188 } else if( "downloaded".equals(exchangeType) ) {
1189 ret = db1.ExchangeMagicToDownload(userid, magic);
1190 } else if( "vip_downloads".equals(exchangeType) ) {
1191 ret = db1.ExchangeMagicToVip(userid, magic);
1192 } else {
1193 System.out.println("Invalid exchange type: " + exchangeType);
1194 return new ResponseEntity<>(2, HttpStatus.BAD_REQUEST); // 返回 2 表示交换类型错误
1195 }
1196 if (!ret) {
1197 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1198 }
1199 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1200 } catch (JsonProcessingException e) {
1201 e.printStackTrace();
1202 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1203 } catch (Exception e) {
1204 e.printStackTrace();
1205 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1206 }
1207 }
1208
1209 @Override
1210 public ResponseEntity<String> getUserFavorites(
1211 @RequestParam("userid") String userid
1212 ) {
1213 try {
1214 if (userid == null || userid.trim().isEmpty()) {
1215 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.BAD_REQUEST); // 返回 400 表示参数不完整
1216 }
1217
1218 UserStar[] favorites = db1.GetUserStarList(userid);
1219 if (favorites == null) {
1220 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到用户收藏
1221 }
1222
1223 String json = mapper.writeValueAsString(favorites);
1224 return new ResponseEntity<>(json, headers, HttpStatus.OK);
1225 } catch (JsonProcessingException e) {
1226 e.printStackTrace();
1227 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
1228 } catch (Exception e) {
1229 e.printStackTrace();
1230 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
1231 }
1232 }
1233
1234 @Override
1235 @CrossOrigin(origins = "*", allowedHeaders = "*")
1236 public ResponseEntity<Integer> removeFavorite(
1237 @RequestBody String requestBody
1238 ) {
1239 try {
1240 // 解析 JSON 数据
1241 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1242 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1243 com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seedid");
1244 if (useridNode == null || seedidNode == null) {
1245 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1246 }
1247
1248 String userid = useridNode.asText();
1249 String seedid = seedidNode.asText();
1250
1251 // 添加参数验证
1252 if (userid == null || userid.trim().isEmpty() ||
1253 seedid == null || seedid.trim().isEmpty()) {
1254 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1255 }
1256
1257 boolean ret = db1.DeleteCollect(userid, seedid);
1258 if (ret) {
1259 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1260 } else {
1261 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1262 }
1263 } catch (JsonProcessingException e) {
1264 e.printStackTrace();
1265 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1266 } catch (Exception e) {
1267 e.printStackTrace();
1268 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1269 }
1270 }
1271
1272 @Override
1273 @CrossOrigin(origins = "*", allowedHeaders = "*")
1274 public ResponseEntity<Integer> addFavorite(
1275 @RequestBody String requestBody
1276 ) {
1277 try {
1278 // 解析 JSON 数据
1279 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1280 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1281 com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seedid");
1282 if (useridNode == null || seedidNode == null) {
1283 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1284 }
1285
1286 String userid = useridNode.asText();
1287 String seedid = seedidNode.asText();
1288
1289 // 添加参数验证
1290 if (userid == null || userid.trim().isEmpty() ||
1291 seedid == null || seedid.trim().isEmpty()) {
1292 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1293 }
1294
1295 boolean ret = db1.AddCollect(userid, seedid);
1296 if (ret) {
1297 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1298 } else {
1299 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1300 }
1301 } catch (JsonProcessingException e) {
1302 e.printStackTrace();
1303 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1304 } catch (Exception e) {
1305 e.printStackTrace();
1306 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1307 }
1308 }
1309
1310 @Override
1311 @CrossOrigin(origins = "*", allowedHeaders = "*")
1312 public ResponseEntity<Integer> migrateAccount(
1313 @RequestParam("userid") String userid,
1314 @RequestParam("file") MultipartFile file
1315 ) {
1316 try {
1317 // 先进行参数验证
1318 if (userid == null || userid.trim().isEmpty() ||
1319 file == null || file.isEmpty()) {
1320 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
1321 }
1322
1323 if (file.getSize() > 100 * 1024 * 1024) {
1324 System.out.println("File size exceeds limit: " + file.getSize() + " bytes");
1325 return new ResponseEntity<>(3, HttpStatus.PAYLOAD_TOO_LARGE); // 返回 3 表示文件过大
1326 }
1327
1328 File tempFile = File.createTempFile(userid, file.getOriginalFilename());
1329 file.transferTo(tempFile);
1330
1331 int ret = db1.UploadMigration(userid, tempFile);
1332 if (ret == 0) {
1333 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1334 } else if (ret == 1) {
1335 return new ResponseEntity<>(1, HttpStatus.CONFLICT); // 返回 1 表示用户已存在或其他错误
1336 } else {
1337 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1338 }
1339 } catch (JsonProcessingException e) {
1340 e.printStackTrace();
1341 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1342 } catch (Exception e) {
1343 e.printStackTrace();
1344 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
1345 }
1346 }
1347
1348 @Override
1349 public ResponseEntity<String> getBegSeedList() {
1350 try {
1351 BegSeedDetail[] begSeedList = db1.GetBegList();
1352 if (begSeedList == null) {
1353 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到种子列表
1354 }
1355 String json = mapper.writeValueAsString(begSeedList);
1356 return new ResponseEntity<>(json, headers, HttpStatus.OK);
1357 } catch (Exception e) {
1358 e.printStackTrace();
1359 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
1360 }
1361 }
1362
1363 @Override
1364 public ResponseEntity<String> getBegSeedDetail(
1365 @RequestParam("begid") String begid
1366 ) {
1367 try {
1368 System.out.println("getBegSeedDetail called with begid: " + begid);
1369 if (begid == null || begid.trim().isEmpty()) {
1370 return new ResponseEntity<>("", errorHeaders, HttpStatus.BAD_REQUEST); // 返回 400 表示参数不完整
1371 }
1372
1373 BegSeedDetail begSeedDetail = db1.GetBegSeedDetail(begid);
1374 if (begSeedDetail == null) {
1375 return new ResponseEntity<>("", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到种子详情
1376 }
1377 String json = mapper.writeValueAsString(begSeedDetail);
1378 return new ResponseEntity<>(json, headers, HttpStatus.OK);
1379 } catch (JsonProcessingException e) {
1380 e.printStackTrace();
1381 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
1382 } catch (Exception e) {
1383 e.printStackTrace();
1384 return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
1385 }
1386 }
1387
1388 @Override
1389 public ResponseEntity<String> getBegSeedSubmissions(
1390 @RequestParam("begid") String begid
1391 ) {
1392 try {
1393 if (begid == null || begid.trim().isEmpty()) {
1394 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.BAD_REQUEST); // 返回 400 表示参数不完整
1395 }
1396
1397 SeedWithVotes[] submissionsWithVotes = db1.GetBegSeedListWithVotes(begid);
1398 if (submissionsWithVotes == null) {
1399 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到提交记录
1400 }
1401
1402 String json = mapper.writeValueAsString(submissionsWithVotes);
1403 return new ResponseEntity<>(json, headers, HttpStatus.OK);
1404 } catch (JsonProcessingException e) {
1405 e.printStackTrace();
1406 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
1407 } catch (Exception e) {
1408 e.printStackTrace();
1409 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
1410 }
1411 }
1412
1413 @Override
1414 @CrossOrigin(origins = "*", allowedHeaders = "*")
1415 public ResponseEntity<Integer> submitSeed(
1416 @RequestBody String requestBody
1417 ) {
1418 try {
1419 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1420 com.fasterxml.jackson.databind.JsonNode begidNode = jsonNode.get("begid");
1421 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1422 com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seedid");
1423
1424 if (begidNode == null || useridNode == null || seedidNode == null) {
1425 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1426 }
1427
1428 String begid = begidNode.asText();
1429 String userid = useridNode.asText();
1430 String seedid = seedidNode.asText();
1431
1432 if (begid == null || begid.trim().isEmpty() ||
1433 userid == null || userid.trim().isEmpty() ||
1434 seedid == null || seedid.trim().isEmpty()) {
1435 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1436 }
1437
1438 int ret = db1.SubmitBegSeed(begid, seedid, userid);
1439 if (ret == 0) {
1440 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1441 } else if (ret == 1) {
1442 return new ResponseEntity<>(1, HttpStatus.CONFLICT); // 返回 1 表示提交失败或其他错误
1443 } else {
1444 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1445 }
1446 } catch (Exception e) {
1447 e.printStackTrace();
1448 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1449 }
1450 }
1451
1452 @Override
1453 @CrossOrigin(origins = "*", allowedHeaders = "*")
1454 public ResponseEntity<Integer> voteSeed(
1455 @RequestBody String requestBody
1456 ) {
1457 try {
1458 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1459 com.fasterxml.jackson.databind.JsonNode begidNode = jsonNode.get("begid");
1460 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1461 com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seedid");
1462
1463 if (begidNode == null || useridNode == null || seedidNode == null) {
1464 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1465 }
1466
1467 String begid = begidNode.asText();
1468 String userid = useridNode.asText();
1469 String seedid = seedidNode.asText();
1470
1471 if (begid == null || begid.trim().isEmpty() ||
1472 userid == null || userid.trim().isEmpty() ||
1473 seedid == null || seedid.trim().isEmpty()) {
1474 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1475 }
1476
1477 int ret = db1.VoteSeed(begid, seedid, userid);
1478 if (ret == 0) {
1479 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1480 } else if (ret == 1) {
1481 return new ResponseEntity<>(3, HttpStatus.CONFLICT); // 返回 1 表示投票失败或其他错误
1482 } else {
1483 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1484 }
1485 } catch (Exception e) {
1486 e.printStackTrace();
1487 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1488 }
1489 }
1490
1491 @Override
1492 @CrossOrigin(origins = "*", allowedHeaders = "*")
1493 public ResponseEntity<Integer> createBegSeed(
1494 @RequestBody String requestBody
1495 ) {
1496 try {
1497 com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
1498 com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
1499 com.fasterxml.jackson.databind.JsonNode infoNode = jsonNode.get("info");
1500 com.fasterxml.jackson.databind.JsonNode rewardNode = jsonNode.get("reward_magic");
1501 com.fasterxml.jackson.databind.JsonNode dedlineNode = jsonNode.get("deadline");
1502
1503 if (useridNode == null || infoNode == null || rewardNode == null || dedlineNode == null) {
1504 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1505 }
1506
1507 String userid = useridNode.asText();
1508 String info = infoNode.asText();
1509
1510 if (userid == null || userid.trim().isEmpty() ||
1511 info == null || info.trim().isEmpty() ||
1512 rewardNode.asInt() <= 0 ){
1513 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
1514 }
1515
1516 // 解析日期字符串
1517 Date endTime;
1518 try {
1519 String deadlineStr = dedlineNode.asText();
1520 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
1521 endTime = dateFormat.parse(deadlineStr);
1522 } catch (ParseException e) {
1523 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 日期格式错误
1524 }
1525
1526 BegInfo beg = new BegInfo();
1527 beg.begid = java.util.UUID.randomUUID().toString();
1528 beg.begnumbers = 0; // 初始提交数为0
1529 beg.magic = rewardNode.asInt();
1530 beg.endtime = endTime;
1531 beg.hasseed = 0; // 初始状态为未开始
1532
1533 // int ret = db1.AddBegSeed(beg);
1534 int ret = db1.createBagSeed(beg, userid, info);
1535 if (ret == 0) {
1536 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
1537 } else if (ret == 1) {
1538 return new ResponseEntity<>(2, HttpStatus.CONFLICT); // 返回 1 表示创建失败或其他错误
1539 } else {
1540 return new ResponseEntity<>(3, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
1541 }
1542 } catch (Exception e) {
1543 e.printStackTrace();
1544 return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
1545 }
1546 }
Raverf79fdb62025-06-03 06:02:49 +00001547}