blob: 28a8ac5fbbc71f4a2ac49d8d0ccb905a743d1ed7 [file] [log] [blame]
rootcd436562025-05-08 14:09:19 +00001package database;
2
Haotian W88568d82025-05-15 16:15:21 +00003import java.util.Calendar;
4import java.util.UUID;
5import java.util.List;
6import java.util.Date;
7
8import javax.persistence.EntityManager;
9import javax.persistence.EntityManagerFactory;
10import javax.persistence.EntityTransaction;
11import javax.persistence.Persistence;
12
13import com.querydsl.jpa.impl.JPAQuery;
14import com.querydsl.core.Tuple;
rootff0769a2025-05-18 17:24:41 +000015import entity.QBegInfo;
16import entity.QUserVotes;
17import entity.QSubmitSeed;
18import entity.QProfile;
19import entity.QSeed;
20import entity.Seed;
21import entity.User;
22import entity.UserPT;
23import entity.Notice;
24import entity.BegInfo;
25import entity.Post;
26import entity.PostReply;
27import entity.Profile;
Haotian W88568d82025-05-15 16:15:21 +000028
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
rootcd436562025-05-08 14:09:19 +000032// public class Database2 implements DataManagerInterface {
Haotian W88568d82025-05-15 16:15:21 +000033
34// private EntityManagerFactory emf;
35// private static final Logger logger = LoggerFactory.getLogger(Database2.class);
36
37// // 构造函数,初始化EntityManagerFactory
38// public Database2() {
39// this.emf = Persistence.createEntityManagerFactory("myPersistenceUnit"); // 使用 persistence.xml 中定义的持久化单元名称。
40// }
41
42// @Override
root824a0ed2025-05-18 17:24:41 +000043// public int RegisterUser(User userinfo) {
44// return 0;
45// }
46
47// @Override
48// public int UpdateInformation(User userinfo) {
49// return 0;
50// }
51
52// @Override
53// public User GetInformation(String userid) {
54// return null;
55// }
56
57// @Override
58// public UserPT GetInformationPT(String userid) {
59// return null;
60// }
61
62// @Override
63// public int UpdateInformationPT(UserPT userinfo) {
64// return 0;
65// }
66
67// @Override
68// public int RegisterUserPT(UserPT userinfo) {
69// return 0;
70// }
71
72// @Override
73// public Seed GetSeedInformation(String seedid) {
74// return null;
75// }
76
77// @Override
78// public int RegisterSeed(Seed seedinfo) {
79// return 0;
80// }
81
82// @Override
83// public int UpdateSeed(Seed seedinfo) {
84// return 0;
85// }
86
87// @Override
88// public Seed[] SearchSeed(String userQ) {
89// return new Seed[0];
90// }
91
92// @Override
93// public int AddNotice(Notice notice) {
94// return 0;
95// }
96
97// @Override
98// public boolean UpdateNotice(Notice notice) {
99// return false;
100// }
101
102// @Override
103// public boolean DeleteNotice(String noticeid) {
104// return false;
105// }
106
107// @Override
108// public int GetUserAvailableInviteTimes(String userid) {
109// return 0;
110// }
111
112// @Override
113// public int InviteUser(String inviterid, String inviteemail) {
114// return 0;
115// }
116
117// @Override
118// public boolean AddCollect(String userid, String postid) {
119// return false;
120// }
121
122// @Override
123// public boolean DeleteCollect(String userid, String postid) {
124// return false;
125// }
126
127// @Override
Haotian W88568d82025-05-15 16:15:21 +0000128// public int AddBegSeed(BegInfo info) {
129// if (info == null || info.begid == null || info.begid.isEmpty()) {
130// logger.warn("Invalid parameter: info is null or begid is empty");
131// return 2;
132// }
133
134// EntityManager em = null;
135// EntityTransaction tx = null;
136
137// try {
138// em = emf.createEntityManager();
139// tx = em.getTransaction();
140// tx.begin();
141
142// // 检查是否重复
143// BegInfo existingBeg = em.find(BegInfo.class, info.begid);
144// if (existingBeg != null) {
145// logger.warn("BegSeed with ID {} already exists", info.begid);
146// return 1;
147// }
148
149// // 设置默认值
150// if (info.endtime == null) {
151// // 设置默认14天截止期
152// Calendar calendar = Calendar.getInstance();
153// calendar.add(Calendar.DAY_OF_MONTH, 14);
154// info.endtime = calendar.getTime();
155// }
156// info.hasseed = false;
157
158// // 保存新的求种信息
159// em.persist(info);
160// tx.commit();
161
162// logger.info("Successfully added new BegSeed with ID: {}", info.begid);
163// return 0;
164
165// } catch (Exception e) {
166// if (tx != null && tx.isActive()) {
167// tx.rollback();
168// }
169// logger.error("Error adding BegSeed: {}", e.getMessage());
170// return 2;
171// } finally {
172// if (em != null) {
173// em.close();
174// }
175// }
176// }
177
178// @Override
179// public int UpdateBegSeed(BegInfo info) {
180// if (info == null || info.begid == null || info.begid.isEmpty()) {
181// logger.warn("Invalid parameter: info is null or begid is empty");
182// return 2;
183// }
184
185// EntityManager em = null;
186// EntityTransaction tx = null;
187
188// try {
189// em = emf.createEntityManager();
190// tx = em.getTransaction();
191// tx.begin();
192
193// // 检查是否存在
194// BegInfo existingBeg = em.find(BegInfo.class, info.begid);
195// if (existingBeg == null) {
196// logger.warn("BegSeed with ID {} does not exist", info.begid);
197// return 1;
198// }
199
200// // 保持原有值不变的字段
201// info.hasseed = existingBeg.hasseed;
202// if (info.endtime == null) {
203// info.endtime = existingBeg.endtime;
204// }
205
206// // 更新求种信息
207// em.merge(info);
208// tx.commit();
209
210// logger.info("Successfully updated BegSeed with ID: {}", info.begid);
211// return 0;
212
213// } catch (Exception e) {
214// if (tx != null && tx.isActive()) {
215// tx.rollback();
216// }
217// logger.error("Error updating BegSeed: {}", e.getMessage());
218// return 2;
219// } finally {
220// if (em != null) {
221// em.close();
222// }
223// }
224// }
225
226// @Override
227// public int DeleteBegSeed(String begid) {
228// if (begid == null || begid.isEmpty()) {
229// logger.warn("Invalid parameter: begid is null or empty");
230// return 2;
231// }
232
233// EntityManager em = null;
234// EntityTransaction tx = null;
235
236// try {
237// em = emf.createEntityManager();
238// tx = em.getTransaction();
239// tx.begin();
240
241// // 查找要删除的求种信息
242// BegInfo begInfo = em.find(BegInfo.class, begid);
243// if (begInfo == null) {
244// logger.warn("BegSeed with ID {} does not exist", begid);
245// return 1;
246// }
247
248// // 删除求种信息
249// em.remove(begInfo);
250// tx.commit();
251
252// logger.info("Successfully deleted BegSeed with ID: {}", begid);
253// return 0;
254
255// } catch (Exception e) {
256// if (tx != null && tx.isActive()) {
257// tx.rollback();
258// }
259// logger.error("Error deleting BegSeed: {}", e.getMessage());
260// return 2;
261// } finally {
262// if (em != null) {
263// em.close();
264// }
265// }
266// }
267
268// @Override
269// public int VoteSeed(String begId, String seedId, String userId) {
270// if (begId == null || seedId == null || userId == null ||
271// begId.isEmpty() || seedId.isEmpty() || userId.isEmpty()) {
272// logger.warn("Invalid parameters: begId, seedId or userId is null or empty");
273// return 2;
274// }
275
276// EntityManager em = null;
277// EntityTransaction tx = null;
278
279// try {
280// em = emf.createEntityManager();
281// tx = em.getTransaction();
282// tx.begin();
283
284// // 检查求种信息是否存在
285// BegInfo begInfo = em.find(BegInfo.class, begId);
286// if (begInfo == null) {
287// logger.warn("BegSeed with ID {} does not exist", begId);
288// return 2;
289// }
290
291// // 检查用户是否已投票
292// QUserVotes uv = QUserVotes.userVotes;
293// Long voteCount = new JPAQuery<>(em)
294// .select(uv.count())
295// .from(uv)
root824a0ed2025-05-18 17:24:41 +0000296// .where(uv.user.userid.eq(userId))
297// .where(uv.begInfo.begid.eq(begId))
298// .where(uv.seed.seedid.eq(seedId))
Haotian W88568d82025-05-15 16:15:21 +0000299// .fetchOne();
300
301// if (voteCount > 0) {
root824a0ed2025-05-18 17:24:41 +0000302// logger.warn("User {} has already voted for seed {} in beg {}", userId,
303// seedId, begId);
Haotian W88568d82025-05-15 16:15:21 +0000304// return 1;
305// }
306
307// // 创建新的投票记录
308// em.createNativeQuery("INSERT INTO UserVotes (user_id, beg_id, seed_id, created_at) " +
309// "VALUES (?, ?, ?, CURRENT_TIMESTAMP)")
310// .setParameter(1, userId)
311// .setParameter(2, begId)
312// .setParameter(3, seedId)
313// .executeUpdate();
314
315// // 更新SubmitSeed表中的投票数
316// em.createQuery("UPDATE SubmitSeed s SET s.votes = s.votes + 1 " +
317// "WHERE s.begId = :begId AND s.seedId = :seedId")
318// .setParameter("begId", begId)
319// .setParameter("seedId", seedId)
320// .executeUpdate();
321
322// tx.commit();
323// logger.info("Successfully added vote from user {} for seed {} in beg {}",
324// userId, seedId, begId);
325// return 0;
326
327// } catch (Exception e) {
328// if (tx != null && tx.isActive()) {
329// tx.rollback();
330// }
331// logger.error("Error voting for seed: {}", e.getMessage());
332// return 2;
333// } finally {
334// if (em != null) {
335// em.close();
336// }
337// }
338// }
339
340// @Override
341// public int SubmitSeed(String begid, Seed seed) {
342// if (begid == null || seed == null || begid.isEmpty() || seed.seedid == null) {
343// logger.warn("Invalid parameters: begid or seed is null or empty");
344// return 2;
345// }
346
347// EntityManager em = null;
348// EntityTransaction tx = null;
349
350// try {
351// em = emf.createEntityManager();
352// tx = em.getTransaction();
353// tx.begin();
354
355// // 检查求种信息是否存在
356// BegInfo begInfo = em.find(BegInfo.class, begid);
357// if (begInfo == null) {
358// logger.warn("BegSeed with ID {} does not exist", begid);
359// return 2;
360// }
361
362// // 检查种子是否已提交过
363// QSubmitSeed ss = QSubmitSeed.submitSeed;
364// Long submitCount = new JPAQuery<>(em)
365// .select(ss.count())
366// .from(ss)
root824a0ed2025-05-18 17:24:41 +0000367// .where(ss.begInfo.begid.eq(begid))
368// .where(ss.seed.seedid.eq(seed.seedid))
Haotian W88568d82025-05-15 16:15:21 +0000369// .fetchOne();
370
371// if (submitCount > 0) {
root824a0ed2025-05-18 17:24:41 +0000372// logger.warn("Seed {} has already been submitted for beg {}", seed.seedid,
373// begid);
Haotian W88568d82025-05-15 16:15:21 +0000374// return 1;
375// }
376
377// // 保存种子信息(如果不存在)
378// if (em.find(Seed.class, seed.seedid) == null) {
379// em.persist(seed);
380// }
381
382// // 创建提交记录
383// em.createNativeQuery("INSERT INTO SubmitSeed (beg_id, seed_id, votes) VALUES (?, ?, 0)")
384// .setParameter(1, begid)
385// .setParameter(2, seed.seedid)
386// .executeUpdate();
387
388// tx.commit();
389// logger.info("Successfully submitted seed {} for beg {}", seed.seedid, begid);
390// return 0;
391
392// } catch (Exception e) {
393// if (tx != null && tx.isActive()) {
394// tx.rollback();
395// }
396// logger.error("Error submitting seed: {}", e.getMessage());
397// return 2;
398// } finally {
399// if (em != null) {
400// em.close();
401// }
402// }
403// }
404
405// @Override
406// public void SettleBeg() {
407// EntityManager em = null;
408// EntityTransaction tx = null;
409
410// try {
411// em = emf.createEntityManager();
412// tx = em.getTransaction();
413// tx.begin();
414
415// // 1. 获取所有已过期且未完成的求种信息
416// QBegInfo b = QBegInfo.begInfo;
417// List<BegInfo> expiredBegs = new JPAQuery<>(em)
418// .select(b)
419// .from(b)
420// .where(b.endtime.loe(new Date())
421// .and(b.hasseed.eq(false)))
422// .fetch();
423
424// for (BegInfo beg : expiredBegs) {
425// // 2. 查找投票最多的提交任务
426// QSubmitSeed ss = QSubmitSeed.submitSeed;
427// Tuple topSubmission = new JPAQuery<>(em)
root824a0ed2025-05-18 17:24:41 +0000428// .select(ss.seed.seedid, ss.votes)
Haotian W88568d82025-05-15 16:15:21 +0000429// .from(ss)
root824a0ed2025-05-18 17:24:41 +0000430// .where(ss.begInfo.begid.eq(beg.begid))
Haotian W88568d82025-05-15 16:15:21 +0000431// .orderBy(ss.votes.desc())
432// .limit(1)
433// .fetchOne();
434
435// if (topSubmission != null && topSubmission.get(ss.votes) > 0) {
root824a0ed2025-05-18 17:24:41 +0000436// String seedId = topSubmission.get(ss.seed.seedid);
Haotian W88568d82025-05-15 16:15:21 +0000437
438// // 3. 获取上传者ID
439// QSeed s = QSeed.seed;
440// String ownerId = new JPAQuery<>(em)
441// .select(s.seeduserid)
442// .from(s)
443// .where(s.seedid.eq(seedId))
444// .fetchOne();
445
446// // 4. 获取上传者的PT信息并更新魔力值
447// UserPT ownerPT = em.find(UserPT.class, ownerId);
448// if (ownerPT != null) {
449// // 5. 发放奖励
450// ownerPT.magic += beg.magic;
451// em.merge(ownerPT);
452
453// // 6. 更新求种状态
454// beg.hasseed = true;
455// em.merge(beg);
456
457// logger.info("Reward {} magic points awarded to user {} for beg {}",
458// beg.magic, ownerId, beg.begid);
459// }
460// }
461// }
462
463// tx.commit();
root824a0ed2025-05-18 17:24:41 +0000464// logger.info("Successfully settled {} expired beg requests",
465// expiredBegs.size());
Haotian W88568d82025-05-15 16:15:21 +0000466
467// } catch (Exception e) {
468// if (tx != null && tx.isActive()) {
469// tx.rollback();
470// }
471// logger.error("Error settling beg requests: {}", e.getMessage(), e);
472// } finally {
473// if (em != null) {
474// em.close();
475// }
476// }
477// }
478
479// @Override
480// public int AddPost(Post post) {
481// if (post == null || post.postid == null || post.postid.isEmpty()) {
482// logger.warn("Invalid parameter: post is null or postid is empty");
483// return 2;
484// }
485
486// EntityManager em = null;
487// EntityTransaction tx = null;
488
489// try {
490// em = emf.createEntityManager();
491// tx = em.getTransaction();
492// tx.begin();
493
494// // 检查是否重复
495// Post existingPost = em.find(Post.class, post.postid);
496// if (existingPost != null) {
497// logger.warn("Post with ID {} already exists", post.postid);
498// return 1;
499// }
500
501// // 检查用户是否存在
502// User user = em.find(User.class, post.postuserid);
503// if (user == null) {
504// logger.warn("User with ID {} does not exist", post.postuserid);
505// return 2;
506// }
507
508// // 设置初始值
509// if (post.posttime == null) {
510// post.posttime = new Date();
511// }
512// post.replytime = 0;
513// post.readtime = 0;
514
515// // 保存帖子
516// em.persist(post);
517// tx.commit();
518
519// logger.info("Successfully added new post with ID: {}", post.postid);
520// return 0;
521
522// } catch (Exception e) {
523// if (tx != null && tx.isActive()) {
524// tx.rollback();
525// }
526// logger.error("Error adding post: {}", e.getMessage());
527// return 2;
528// } finally {
529// if (em != null) {
530// em.close();
531// }
532// }
533// }
534
535// @Override
536// public int UpdatePost(Post post) {
537// if (post == null || post.postid == null || post.postid.isEmpty()) {
538// logger.warn("Invalid parameter: post is null or postid is empty");
539// return 2;
540// }
541
542// EntityManager em = null;
543// EntityTransaction tx = null;
544
545// try {
546// em = emf.createEntityManager();
547// tx = em.getTransaction();
548// tx.begin();
549
550// // 检查帖子是否存在
551// Post existingPost = em.find(Post.class, post.postid);
552// if (existingPost == null) {
553// logger.warn("Post with ID {} does not exist", post.postid);
554// return 1;
555// }
556
557// // 保持原有不可修改的字段
558// post.postuserid = existingPost.postuserid;
559// post.posttime = existingPost.posttime;
560// post.replytime = existingPost.replytime;
561// post.readtime = existingPost.readtime;
562
563// // 更新帖子
564// em.merge(post);
565// tx.commit();
566
567// logger.info("Successfully updated post with ID: {}", post.postid);
568// return 0;
569
570// } catch (Exception e) {
571// if (tx != null && tx.isActive()) {
572// tx.rollback();
573// }
574// logger.error("Error updating post: {}", e.getMessage());
575// return 2;
576// } finally {
577// if (em != null) {
578// em.close();
579// }
580// }
581// }
582
583// @Override
584// public int DeletePost(String postid) {
585// if (postid == null || postid.isEmpty()) {
586// logger.warn("Invalid parameter: postid is null or empty");
587// return 2;
588// }
589
590// EntityManager em = null;
591// EntityTransaction tx = null;
592
593// try {
594// em = emf.createEntityManager();
595// tx = em.getTransaction();
596// tx.begin();
597
598// // 查找要删除的帖子
599// Post post = em.find(Post.class, postid);
600// if (post == null) {
601// logger.warn("Post with ID {} does not exist", postid);
602// return 1;
603// }
604
605// // 删除帖子(由于设置了级联删除,相关的回复会自动删除)
606// em.remove(post);
607// tx.commit();
608
609// logger.info("Successfully deleted post with ID: {}", postid);
610// return 0;
611
612// } catch (Exception e) {
613// if (tx != null && tx.isActive()) {
614// tx.rollback();
615// }
616// logger.error("Error deleting post: {}", e.getMessage());
617// return 2;
618// } finally {
619// if (em != null) {
620// em.close();
621// }
622// }
623// }
624
625// @Override
626// public int AddComment(String postid, String userid, String comment) {
627// if (postid == null || postid.isEmpty() ||
628// userid == null || userid.isEmpty() ||
629// comment == null || comment.isEmpty()) {
630// logger.warn("Invalid parameters: postid, userid or comment is null or empty");
631// return 2;
632// }
633
634// EntityManager em = null;
635// EntityTransaction tx = null;
636
637// try {
638// em = emf.createEntityManager();
639// tx = em.getTransaction();
640// tx.begin();
641
642// // 检查帖子是否存在
643// Post post = em.find(Post.class, postid);
644// if (post == null) {
645// logger.warn("Post with ID {} does not exist", postid);
646// return 1;
647// }
648
649// // 检查评论用户是否存在
650// User user = em.find(User.class, userid);
651// if (user == null) {
652// logger.warn("User with ID {} does not exist", userid);
653// return 1;
654// }
655
656// // 创建新的回复
657// PostReply reply = new PostReply();
658// reply.replyid = UUID.randomUUID().toString();
659// reply.postid = postid;
660// reply.content = comment;
661// reply.authorid = userid;
662// reply.createdAt = new Date();
663
664// // 保存回复
665// em.persist(reply);
666
667// // 更新帖子的回复数
668// post.replytime += 1;
669// em.merge(post);
670
671// tx.commit();
672
673// logger.info("Successfully added comment by user {} to post {}, reply ID: {}",
674// userid, postid, reply.replyid);
675// return 0;
676
677// } catch (Exception e) {
678// if (tx != null && tx.isActive()) {
679// tx.rollback();
680// }
681// logger.error("Error adding comment: {}", e.getMessage());
682// return 2;
683// } finally {
684// if (em != null) {
685// em.close();
686// }
687// }
688// }
689
690// @Override
691// public int DeleteComment(String postid, String commentid) {
692// if (postid == null || postid.isEmpty() || commentid == null || commentid.isEmpty()) {
693// logger.warn("Invalid parameters: postid or commentid is null or empty");
694// return 2;
695// }
696
697// EntityManager em = null;
698// EntityTransaction tx = null;
699
700// try {
701// em = emf.createEntityManager();
702// tx = em.getTransaction();
703// tx.begin();
704
705// // 检查帖子是否存在
706// Post post = em.find(Post.class, postid);
707// if (post == null) {
708// logger.warn("Post with ID {} does not exist", postid);
709// return 1;
710// }
711
712// // 检查评论是否存在且属于该帖子
713// PostReply reply = em.find(PostReply.class, commentid);
714// if (reply == null || !reply.postid.equals(postid)) {
715// logger.warn("Comment {} does not exist or does not belong to post {}", commentid, postid);
716// return 1;
717// }
718
719// // 删除评论
720// em.remove(reply);
721
722// // 更新帖子的回复数
723// post.replytime = Math.max(0, post.replytime - 1);
724// em.merge(post);
725
726// tx.commit();
727
728// logger.info("Successfully deleted comment {} from post {}", commentid, postid);
729// return 0;
730
731// } catch (Exception e) {
732// if (tx != null && tx.isActive()) {
733// tx.rollback();
734// }
735// logger.error("Error deleting comment: {}", e.getMessage());
736// return 2;
737// } finally {
738// if (em != null) {
739// em.close();
740// }
741// }
742// }
743
744// @Override
745// public boolean ExchangeMagicToUpload(String userid, int magic) {
746// if (userid == null || userid.isEmpty() || magic <= 0) {
747// logger.warn("参数无效: userid为空或magic <= 0");
748// return false;
749// }
750
751// EntityManager em = null;
752// EntityTransaction tx = null;
753
754// try {
755// em = emf.createEntityManager();
756// tx = em.getTransaction();
757// tx.begin();
758
759// UserPT userPT = em.find(UserPT.class, userid);
760// if (userPT == null) {
761// logger.warn("未找到用户 {} 的PT信息", userid);
762// return false;
763// }
764
765// if (userPT.magic < magic) {
766// logger.warn("用户 {} 的魔力值不足", userid);
767// return false;
768// }
769
770// // 1:1兑换,直接加上魔力值(假设单位是MB)
771// userPT.magic -= magic;
772// userPT.upload += magic;
773
774// // 更新分享率
775// if (userPT.download > 0) {
776// userPT.share = (double) userPT.upload / userPT.download;
777// }
778
779// em.merge(userPT);
780// tx.commit();
781
782// logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 上传量", userid, magic, magic);
783// return true;
784
785// } catch (Exception e) {
786// if (tx != null && tx.isActive()) {
787// tx.rollback();
788// }
789// logger.error("魔力值兑换上传量时发生错误: {}", e.getMessage());
790// return false;
791// } finally {
792// if (em != null) {
793// em.close();
794// }
795// }
796// }
797
798// @Override
799// public boolean ExchangeMagicToDownload(String userid, int magic) {
800// if (userid == null || userid.isEmpty() || magic <= 0) {
801// logger.warn("参数无效: userid为空或magic <= 0");
802// return false;
803// }
804
805// EntityManager em = null;
806// EntityTransaction tx = null;
807
808// try {
809// em = emf.createEntityManager();
810// tx = em.getTransaction();
811// tx.begin();
812
813// UserPT userPT = em.find(UserPT.class, userid);
814// if (userPT == null) {
815// logger.warn("未找到用户 {} 的PT信息", userid);
816// return false;
817// }
818
819// if (userPT.magic < magic) {
820// logger.warn("用户 {} 的魔力值不足", userid);
821// return false;
822// }
823
824// // 1:1兑换,直接减去魔力值对应的下载量(假设单位是MB)
825// userPT.magic -= magic;
826// userPT.download = Math.max(0, userPT.download - magic);
827
828// // 更新分享率
829// if (userPT.download > 0) {
830// userPT.share = (double) userPT.upload / userPT.download;
831// }
832
833// em.merge(userPT);
834// tx.commit();
835
836// logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 下载量减免", userid, magic, magic);
837// return true;
838
839// } catch (Exception e) {
840// if (tx != null && tx.isActive()) {
841// tx.rollback();
842// }
843// logger.error("魔力值兑换下载量时发生错误: {}", e.getMessage());
844// return false;
845// } finally {
846// if (em != null) {
847// em.close();
848// }
849// }
850// }
851
852// @Override
853// public boolean ExchangeMagicToVip(String userid, int magic) {
854// if (userid == null || userid.isEmpty() || magic <= 0) {
855// logger.warn("参数无效: userid为空或magic <= 0");
856// return false;
857// }
858
859// EntityManager em = null;
860// EntityTransaction tx = null;
861
862// try {
863// em = emf.createEntityManager();
864// tx = em.getTransaction();
865// tx.begin();
866
867// UserPT userPT = em.find(UserPT.class, userid);
868// if (userPT == null) {
869// logger.warn("未找到用户 {} 的PT信息", userid);
870// return false;
871// }
872
873// if (userPT.magic < magic) {
874// logger.warn("用户 {} 的魔力值不足", userid);
875// return false;
876// }
877
878// // 1:1兑换VIP下载次数
879// userPT.magic -= magic;
880// userPT.viptime += magic;
881
882// em.merge(userPT);
883// tx.commit();
884
885// logger.info("用户 {} 成功将 {} 点魔力值兑换为 {} 次VIP下载次数", userid, magic, magic);
886// return true;
887
888// } catch (Exception e) {
889// if (tx != null && tx.isActive()) {
890// tx.rollback();
891// }
892// logger.error("魔力值兑换VIP下载次数时发生错误: {}", e.getMessage());
893// return false;
894// } finally {
895// if (em != null) {
896// em.close();
897// }
898// }
899// }
900
901// @Override
902// public boolean UploadTransmitProfile(Profile profile) {
903// if (profile == null || profile.profileurl == null || profile.profileurl.isEmpty() ||
904// profile.userid == null || profile.userid.isEmpty()) {
905// logger.warn("参数无效: profile为空或必要字段为空");
906// return false;
907// }
908
909// EntityManager em = null;
910// EntityTransaction tx = null;
911
912// try {
913// em = emf.createEntityManager();
914// tx = em.getTransaction();
915// tx.begin();
916
917// // 检查用户是否存在
918// User user = em.find(User.class, profile.userid);
919// if (user == null) {
920// logger.warn("用户 {} 不存在", profile.userid);
921// return false;
922// }
923
924// // 检查是否已存在相同的迁移申请
925// Profile existingProfile = em.find(Profile.class, profile.profileurl);
926// if (existingProfile != null) {
927// logger.warn("迁移申请 {} 已存在", profile.profileurl);
928// return false;
929// }
930
931// // 设置初始值
932// profile.exampass = false;
933// profile.magicgived = "0";
934// profile.uploadgived = "0";
935
936// // 保存迁移申请
937// em.persist(profile);
938// tx.commit();
939
940// logger.info("成功上传迁移申请 {}", profile.profileurl);
941// return true;
942
943// } catch (Exception e) {
944// if (tx != null && tx.isActive()) {
945// tx.rollback();
946// }
947// logger.error("上传迁移申请时发生错误: {}", e.getMessage());
948// return false;
949// } finally {
950// if (em != null) {
951// em.close();
952// }
953// }
954// }
955
956// @Override
957// public Profile GetTransmitProfile(String profileid) {
958// if (profileid == null || profileid.isEmpty()) {
959// logger.warn("参数无效: profileid为空");
960// return null;
961// }
962
963// EntityManager em = null;
964
965// try {
966// em = emf.createEntityManager();
967// Profile profile = em.find(Profile.class, profileid);
968// if (profile == null) {
969// logger.warn("未找到迁移申请 {}", profileid);
970// return null;
971// }
972
973// logger.info("成功获取迁移申请 {}", profileid);
974// return profile;
975
976// } catch (Exception e) {
977// logger.error("获取迁移申请时发生错误: {}", e.getMessage());
978// return null;
979// } finally {
980// if (em != null) {
981// em.close();
982// }
983// }
984// }
985
986// @Override
987// public boolean ExamTransmitProfile(String profileid, boolean result) {
988// if (profileid == null || profileid.isEmpty()) {
989// logger.warn("参数无效: profileid为空");
990// return false;
991// }
992
993// EntityManager em = null;
994// EntityTransaction tx = null;
995
996// try {
997// em = emf.createEntityManager();
998// tx = em.getTransaction();
999// tx.begin();
1000
1001// Profile profile = em.find(Profile.class, profileid);
1002// if (profile == null) {
1003// logger.warn("未找到迁移申请 {}", profileid);
1004// return false;
1005// }
1006
1007// if (profile.exampass != result) {
1008// profile.exampass = result;
1009
1010// if (result) {
1011// // 如果审核通过,更新用户的PT信息
1012// UserPT userPT = em.find(UserPT.class, profile.userid);
1013// if (userPT != null) {
1014// // 发放魔力值
1015// int magicToGive = Integer.parseInt(profile.magictogive);
1016// userPT.magic += magicToGive;
1017// profile.magicgived = String.valueOf(magicToGive);
1018
1019// // 发放上传量
1020// long uploadToGive = Long.parseLong(profile.uploadtogive);
1021// userPT.upload += uploadToGive;
1022// profile.uploadgived = String.valueOf(uploadToGive);
1023
1024// em.merge(userPT);
1025// }
1026// }
1027
1028// em.merge(profile);
1029// }
1030
1031// tx.commit();
1032
1033// logger.info("成功审核迁移申请 {}, 结果: {}", profileid, result);
1034// return true;
1035
1036// } catch (Exception e) {
1037// if (tx != null && tx.isActive()) {
1038// tx.rollback();
1039// }
1040// logger.error("审核迁移申请时发生错误: {}", e.getMessage());
1041// return false;
1042// } finally {
1043// if (em != null) {
1044// em.close();
1045// }
1046// }
1047// }
1048
1049// @Override
1050// public Profile[] GetTransmitProfileList() {
1051// EntityManager em = null;
1052
1053// try {
1054// em = emf.createEntityManager();
1055
1056// // 获取所有迁移申请
1057// QProfile p = QProfile.profile;
1058// List<Profile> profiles = new JPAQuery<>(em)
1059// .select(p)
1060// .from(p)
1061// .fetch();
1062
1063// logger.info("成功获取所有迁移申请,共 {} 条", profiles.size());
1064// return profiles.toArray(new Profile[0]);
1065
1066// } catch (Exception e) {
1067// logger.error("获取迁移申请列表时发生错误: {}", e.getMessage());
1068// return new Profile[0];
1069// } finally {
1070// if (em != null) {
1071// em.close();
1072// }
1073// }
1074// }
1075// }