blob: 8ea93b0771297ce794e0ed894600012dd9b19d59 [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;
95630366963296f62025-06-02 21:16:32 +08006import java.util.Map;
Haotian W88568d82025-05-15 16:15:21 +00007import java.util.Date;
95630366963296f62025-06-02 21:16:32 +08008import java.util.HashMap;
Haotian W88568d82025-05-15 16:15:21 +00009
10import javax.persistence.EntityManager;
11import javax.persistence.EntityManagerFactory;
12import javax.persistence.EntityTransaction;
13import javax.persistence.Persistence;
14
15import com.querydsl.jpa.impl.JPAQuery;
16import com.querydsl.core.Tuple;
rootff0769a2025-05-18 17:24:41 +000017import entity.QBegInfo;
18import entity.QUserVotes;
19import entity.QSubmitSeed;
20import entity.QProfile;
21import entity.QSeed;
22import entity.Seed;
23import entity.User;
24import entity.UserPT;
95630366963296f62025-06-02 21:16:32 +080025import entity.config;
rootff0769a2025-05-18 17:24:41 +000026import entity.Notice;
27import entity.BegInfo;
28import entity.Post;
29import entity.PostReply;
30import entity.Profile;
Haotian W88568d82025-05-15 16:15:21 +000031
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
95630366963296f62025-06-02 21:16:32 +080035public class Database2 implements DataManagerInterface {
Haotian W88568d82025-05-15 16:15:21 +000036
95630366963296f62025-06-02 21:16:32 +080037 private EntityManagerFactory emf;
38 private static final Logger logger = LoggerFactory.getLogger(Database2.class);
Haotian W88568d82025-05-15 16:15:21 +000039
95630366963296f62025-06-02 21:16:32 +080040 // 构造函数,初始化EntityManagerFactory
41 public Database2() {
42 config cfg = new config();
43 Map<String, Object> props = new HashMap<>();
44 props.put("javax.persistence.jdbc.url",
45 "jdbc:mysql://" + cfg.SqlURL + "/" + cfg.Database);
46 props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
47 props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
48 this.emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
49 }
Haotian W88568d82025-05-15 16:15:21 +000050
95630366963296f62025-06-02 21:16:32 +080051 public Database2(EntityManagerFactory emf) {
52 this.emf = emf;
53 }
root33a7d952025-05-18 17:24:41 +000054
95630366963296f62025-06-02 21:16:32 +080055 @Override
56 public int RegisterUser(User userinfo) {
57 return 0;
58 }
root33a7d952025-05-18 17:24:41 +000059
95630366963296f62025-06-02 21:16:32 +080060 @Override
61 public int UpdateInformation(User userinfo) {
62 return 0;
63 }
root33a7d952025-05-18 17:24:41 +000064
95630366963296f62025-06-02 21:16:32 +080065 @Override
66 public User GetInformation(String userid) {
67 return null;
68 }
root33a7d952025-05-18 17:24:41 +000069
95630366963296f62025-06-02 21:16:32 +080070 @Override
71 public UserPT GetInformationPT(String userid) {
72 return null;
73 }
root33a7d952025-05-18 17:24:41 +000074
95630366963296f62025-06-02 21:16:32 +080075 @Override
76 public int UpdateInformationPT(UserPT userinfo) {
77 return 0;
78 }
root33a7d952025-05-18 17:24:41 +000079
95630366963296f62025-06-02 21:16:32 +080080 @Override
81 public int RegisterUserPT(UserPT userinfo) {
82 return 0;
83 }
root33a7d952025-05-18 17:24:41 +000084
95630366963296f62025-06-02 21:16:32 +080085 @Override
86 public Seed GetSeedInformation(String seedid) {
87 return null;
88 }
root33a7d952025-05-18 17:24:41 +000089
95630366963296f62025-06-02 21:16:32 +080090 @Override
91 public int RegisterSeed(Seed seedinfo) {
92 return 0;
93 }
root33a7d952025-05-18 17:24:41 +000094
95630366963296f62025-06-02 21:16:32 +080095 @Override
96 public int UpdateSeed(Seed seedinfo) {
97 return 0;
98 }
root33a7d952025-05-18 17:24:41 +000099
95630366963296f62025-06-02 21:16:32 +0800100 @Override
101 public Seed[] SearchSeed(String userQ) {
102 return new Seed[0];
103 }
root33a7d952025-05-18 17:24:41 +0000104
95630366963296f62025-06-02 21:16:32 +0800105 @Override
106 public int AddNotice(Notice notice) {
107 return 0;
108 }
root33a7d952025-05-18 17:24:41 +0000109
95630366963296f62025-06-02 21:16:32 +0800110 @Override
111 public boolean UpdateNotice(Notice notice) {
112 return false;
113 }
root33a7d952025-05-18 17:24:41 +0000114
95630366963296f62025-06-02 21:16:32 +0800115 @Override
116 public boolean DeleteNotice(String noticeid) {
117 return false;
118 }
root33a7d952025-05-18 17:24:41 +0000119
95630366963296f62025-06-02 21:16:32 +0800120 @Override
121 public int GetUserAvailableInviteTimes(String userid) {
122 return 0;
123 }
root33a7d952025-05-18 17:24:41 +0000124
95630366963296f62025-06-02 21:16:32 +0800125 @Override
126 public int InviteUser(String inviterid, String inviteemail) {
127 return 0;
128 }
root33a7d952025-05-18 17:24:41 +0000129
95630366963296f62025-06-02 21:16:32 +0800130 @Override
131 public boolean AddCollect(String userid, String postid) {
132 return false;
133 }
root33a7d952025-05-18 17:24:41 +0000134
95630366963296f62025-06-02 21:16:32 +0800135 @Override
136 public boolean DeleteCollect(String userid, String postid) {
137 return false;
138 }
Haotian W88568d82025-05-15 16:15:21 +0000139
95630366963296f62025-06-02 21:16:32 +0800140 @Override
141 public int AddBegSeed(BegInfo info) {
142 if (info == null || info.begid == null || info.begid.isEmpty()) {
143 logger.warn("Invalid parameter: info is null or begid is empty");
144 return 2;
145 }
Haotian W88568d82025-05-15 16:15:21 +0000146
95630366963296f62025-06-02 21:16:32 +0800147 EntityManager em = null;
148 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000149
95630366963296f62025-06-02 21:16:32 +0800150 try {
151 em = emf.createEntityManager();
152 tx = em.getTransaction();
153 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000154
95630366963296f62025-06-02 21:16:32 +0800155 // 检查是否重复
156 BegInfo existingBeg = em.find(BegInfo.class, info.begid);
157 if (existingBeg != null) {
158 logger.warn("BegSeed with ID {} already exists", info.begid);
159 return 1;
160 }
Haotian W88568d82025-05-15 16:15:21 +0000161
95630366963296f62025-06-02 21:16:32 +0800162 // 设置默认值
163 if (info.endtime == null) {
164 // 设置默认14天截止期
165 Calendar calendar = Calendar.getInstance();
166 calendar.add(Calendar.DAY_OF_MONTH, 14);
167 info.endtime = calendar.getTime();
168 }
169 info.hasseed = 0;
Haotian W88568d82025-05-15 16:15:21 +0000170
95630366963296f62025-06-02 21:16:32 +0800171 // 保存新的求种信息
172 em.persist(info);
173 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000174
95630366963296f62025-06-02 21:16:32 +0800175 logger.info("Successfully added new BegSeed with ID: {}", info.begid);
176 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000177
95630366963296f62025-06-02 21:16:32 +0800178 } catch (Exception e) {
179 if (tx != null && tx.isActive()) {
180 tx.rollback();
181 }
182 logger.error("Error adding BegSeed: {}", e.getMessage());
183 return 2;
184 } finally {
185 if (em != null) {
186 em.close();
187 }
188 }
189 }
Haotian W88568d82025-05-15 16:15:21 +0000190
95630366963296f62025-06-02 21:16:32 +0800191 @Override
192 public int UpdateBegSeed(BegInfo info) {
193 if (info == null || info.begid == null || info.begid.isEmpty()) {
194 logger.warn("Invalid parameter: info is null or begid is empty");
195 return 2;
196 }
Haotian W88568d82025-05-15 16:15:21 +0000197
95630366963296f62025-06-02 21:16:32 +0800198 EntityManager em = null;
199 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000200
95630366963296f62025-06-02 21:16:32 +0800201 try {
202 em = emf.createEntityManager();
203 tx = em.getTransaction();
204 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000205
95630366963296f62025-06-02 21:16:32 +0800206 // 检查是否存在
207 BegInfo existingBeg = em.find(BegInfo.class, info.begid);
208 if (existingBeg == null) {
209 logger.warn("BegSeed with ID {} does not exist", info.begid);
210 return 1;
211 }
Haotian W88568d82025-05-15 16:15:21 +0000212
95630366963296f62025-06-02 21:16:32 +0800213 // 保持原有值不变的字段
214 info.hasseed = existingBeg.hasseed;
215 if (info.endtime == null) {
216 info.endtime = existingBeg.endtime;
217 }
Haotian W88568d82025-05-15 16:15:21 +0000218
95630366963296f62025-06-02 21:16:32 +0800219 // 更新求种信息
220 em.merge(info);
221 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000222
95630366963296f62025-06-02 21:16:32 +0800223 logger.info("Successfully updated BegSeed with ID: {}", info.begid);
224 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000225
95630366963296f62025-06-02 21:16:32 +0800226 } catch (Exception e) {
227 if (tx != null && tx.isActive()) {
228 tx.rollback();
229 }
230 logger.error("Error updating BegSeed: {}", e.getMessage());
231 return 2;
232 } finally {
233 if (em != null) {
234 em.close();
235 }
236 }
237 }
Haotian W88568d82025-05-15 16:15:21 +0000238
95630366963296f62025-06-02 21:16:32 +0800239 @Override
240 public int DeleteBegSeed(String begid) {
241 if (begid == null || begid.isEmpty()) {
242 logger.warn("Invalid parameter: begid is null or empty");
243 return 2;
244 }
Haotian W88568d82025-05-15 16:15:21 +0000245
95630366963296f62025-06-02 21:16:32 +0800246 EntityManager em = null;
247 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000248
95630366963296f62025-06-02 21:16:32 +0800249 try {
250 em = emf.createEntityManager();
251 tx = em.getTransaction();
252 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000253
95630366963296f62025-06-02 21:16:32 +0800254 // 查找要删除的求种信息
255 BegInfo begInfo = em.find(BegInfo.class, begid);
256 if (begInfo == null) {
257 logger.warn("BegSeed with ID {} does not exist", begid);
258 tx.rollback();
259 return 1;
260 }
Haotian W88568d82025-05-15 16:15:21 +0000261
95630366963296f62025-06-02 21:16:32 +0800262 // 删除求种信息
263 em.remove(begInfo);
264 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000265
95630366963296f62025-06-02 21:16:32 +0800266 logger.info("Successfully deleted BegSeed with ID: {}", begid);
267 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000268
95630366963296f62025-06-02 21:16:32 +0800269 } catch (Exception e) {
270 if (tx != null && tx.isActive()) {
271 tx.rollback();
272 }
273 logger.error("Error deleting BegSeed: {}", e.getMessage());
274 return 2;
275 } finally {
276 if (em != null) {
277 em.close();
278 }
279 }
280 }
Haotian W88568d82025-05-15 16:15:21 +0000281
95630366963296f62025-06-02 21:16:32 +0800282 @Override
283 public int VoteSeed(String begId, String seedId, String userId) {
284 if (begId == null || seedId == null || userId == null ||
285 begId.isEmpty() || seedId.isEmpty() || userId.isEmpty()) {
286 logger.warn("Invalid parameters: begId, seedId or userId is null or empty");
287 return 2;
288 }
Haotian W88568d82025-05-15 16:15:21 +0000289
95630366963296f62025-06-02 21:16:32 +0800290 EntityManager em = null;
291 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000292
95630366963296f62025-06-02 21:16:32 +0800293 try {
294 em = emf.createEntityManager();
295 tx = em.getTransaction();
296 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000297
95630366963296f62025-06-02 21:16:32 +0800298 // 检查求种信息是否存在
299 BegInfo begInfo = em.find(BegInfo.class, begId);
300 if (begInfo == null) {
301 logger.warn("BegSeed with ID {} does not exist", begId);
302 return 2;
303 }
Haotian W88568d82025-05-15 16:15:21 +0000304
95630366963296f62025-06-02 21:16:32 +0800305 // 检查用户是否已投票
306 Long voteCount = new JPAQuery<>(em)
307 .select(QUserVotes.userVotes.count())
308 .from(QUserVotes.userVotes)
309 .where(QUserVotes.userVotes.id.eq(new entity.UserVotesId(userId, begId, seedId)))
310 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000311
95630366963296f62025-06-02 21:16:32 +0800312 if (voteCount != null && voteCount > 0) {
313 logger.warn("User {} has already voted for seed {} in beg {}", userId, seedId, begId);
314 return 1;
315 }
Haotian W88568d82025-05-15 16:15:21 +0000316
95630366963296f62025-06-02 21:16:32 +0800317 // 创建新的投票记录
318 em.createNativeQuery("INSERT INTO UserVotes (user_id, beg_id, seed_id, created_at) " +
319 "VALUES (?, ?, ?, CURRENT_TIMESTAMP)")
320 .setParameter(1, userId)
321 .setParameter(2, begId)
322 .setParameter(3, seedId)
323 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000324
95630366963296f62025-06-02 21:16:32 +0800325 // 更新SubmitSeed表中的投票数
326 em.createQuery("UPDATE SubmitSeed s SET s.votes = s.votes + 1 " +
327 "WHERE s.id.begId = :begId AND s.id.seedId = :seedId")
328 .setParameter("begId", begId)
329 .setParameter("seedId", seedId)
330 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000331
95630366963296f62025-06-02 21:16:32 +0800332 tx.commit();
333 logger.info("Successfully added vote from user {} for seed {} in beg {}", userId, seedId, begId);
334 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000335
95630366963296f62025-06-02 21:16:32 +0800336 } catch (Exception e) {
337 if (tx != null && tx.isActive()) {
338 tx.rollback();
339 }
340 logger.error("Error voting for seed: {}", e.getMessage());
341 return 2;
342 } finally {
343 if (em != null) {
344 em.close();
345 }
346 }
347 }
Haotian W88568d82025-05-15 16:15:21 +0000348
95630366963296f62025-06-02 21:16:32 +0800349 @Override
350 public int SubmitSeed(String begid, Seed seed) {
351 if (begid == null || seed == null || begid.isEmpty() || seed.seedid == null) {
352 logger.warn("Invalid parameters: begid or seed is null or empty");
353 return 2;
354 }
Haotian W88568d82025-05-15 16:15:21 +0000355
95630366963296f62025-06-02 21:16:32 +0800356 EntityManager em = null;
357 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000358
95630366963296f62025-06-02 21:16:32 +0800359 try {
360 em = emf.createEntityManager();
361 tx = em.getTransaction();
362 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000363
95630366963296f62025-06-02 21:16:32 +0800364 // 检查求种信息是否存在
365 BegInfo begInfo = em.find(BegInfo.class, begid);
366 if (begInfo == null) {
367 logger.warn("BegSeed with ID {} does not exist", begid);
368 return 2;
369 }
Haotian W88568d82025-05-15 16:15:21 +0000370
95630366963296f62025-06-02 21:16:32 +0800371 // 检查种子是否已提交过
372 QSubmitSeed ss = QSubmitSeed.submitSeed;
373 Long submitCount = new JPAQuery<>(em)
374 .select(ss.count())
375 .from(ss)
376 .where(ss.begInfo.begid.eq(begid))
377 .where(ss.seed.seedid.eq(seed.seedid))
378 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000379
95630366963296f62025-06-02 21:16:32 +0800380 if (submitCount > 0) {
381 logger.warn("Seed {} has already been submitted for beg {}", seed.seedid,
382 begid);
383 return 1;
384 }
Haotian W88568d82025-05-15 16:15:21 +0000385
95630366963296f62025-06-02 21:16:32 +0800386 // 保存种子信息(如果不存在)
387 if (em.find(Seed.class, seed.seedid) == null) {
388 em.persist(seed);
389 }
Haotian W88568d82025-05-15 16:15:21 +0000390
95630366963296f62025-06-02 21:16:32 +0800391 // 创建提交记录
392 em.createNativeQuery("INSERT INTO SubmitSeed (beg_id, seed_id, votes) VALUES (?, ?, 0)")
393 .setParameter(1, begid)
394 .setParameter(2, seed.seedid)
395 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000396
95630366963296f62025-06-02 21:16:32 +0800397 tx.commit();
398 logger.info("Successfully submitted seed {} for beg {}", seed.seedid, begid);
399 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000400
95630366963296f62025-06-02 21:16:32 +0800401 } catch (Exception e) {
402 if (tx != null && tx.isActive()) {
403 tx.rollback();
404 }
405 logger.error("Error submitting seed: {}", e.getMessage());
406 return 2;
407 } finally {
408 if (em != null) {
409 em.close();
410 }
411 }
412 }
Haotian W88568d82025-05-15 16:15:21 +0000413
95630366963296f62025-06-02 21:16:32 +0800414 @Override
415 public void SettleBeg() {
416 EntityManager em = null;
417 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000418
95630366963296f62025-06-02 21:16:32 +0800419 try {
420 em = emf.createEntityManager();
421 tx = em.getTransaction();
422 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000423
95630366963296f62025-06-02 21:16:32 +0800424 // 1. 获取所有已过期且未完成的求种信息
425 QBegInfo b = QBegInfo.begInfo;
426 List<BegInfo> expiredBegs = new JPAQuery<>(em)
427 .select(b)
428 .from(b)
429 .where(b.endtime.loe(new Date())
430 .and(b.hasseed.eq(0)))
431 .fetch();
Haotian W88568d82025-05-15 16:15:21 +0000432
95630366963296f62025-06-02 21:16:32 +0800433 for (BegInfo beg : expiredBegs) {
434 // 2. 查找投票最多的提交任务
435 QSubmitSeed ss = QSubmitSeed.submitSeed;
436 Tuple topSubmission = new JPAQuery<>(em)
437 .select(ss.seed.seedid, ss.votes)
438 .from(ss)
439 .where(ss.begInfo.begid.eq(beg.begid))
440 .orderBy(ss.votes.desc())
441 .limit(1)
442 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000443
95630366963296f62025-06-02 21:16:32 +0800444 if (topSubmission != null && topSubmission.get(ss.votes) > 0) {
445 String seedId = topSubmission.get(ss.seed.seedid);
Haotian W88568d82025-05-15 16:15:21 +0000446
95630366963296f62025-06-02 21:16:32 +0800447 // 3. 获取上传者ID
448 QSeed s = QSeed.seed;
449 String ownerId = new JPAQuery<>(em)
450 .select(s.seeduserid)
451 .from(s)
452 .where(s.seedid.eq(seedId))
453 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000454
95630366963296f62025-06-02 21:16:32 +0800455 // 4. 获取上传者的PT信息并更新魔力值
456 UserPT ownerPT = em.find(UserPT.class, ownerId);
457 if (ownerPT != null) {
458 // 5. 发放奖励
459 ownerPT.magic += beg.magic;
460 em.merge(ownerPT);
Haotian W88568d82025-05-15 16:15:21 +0000461
95630366963296f62025-06-02 21:16:32 +0800462 // 6. 更新求种状态
463 beg.hasseed = 1;
464 em.merge(beg);
Haotian W88568d82025-05-15 16:15:21 +0000465
95630366963296f62025-06-02 21:16:32 +0800466 logger.info("Reward {} magic points awarded to user {} for beg {}",
467 beg.magic, ownerId, beg.begid);
468 }
469 }
470 }
Haotian W88568d82025-05-15 16:15:21 +0000471
95630366963296f62025-06-02 21:16:32 +0800472 tx.commit();
473 logger.info("Successfully settled {} expired beg requests",
474 expiredBegs.size());
Haotian W88568d82025-05-15 16:15:21 +0000475
95630366963296f62025-06-02 21:16:32 +0800476 } catch (Exception e) {
477 if (tx != null && tx.isActive()) {
478 tx.rollback();
479 }
480 logger.error("Error settling beg requests: {}", e.getMessage(), e);
481 } finally {
482 if (em != null) {
483 em.close();
484 }
485 }
486 }
Haotian W88568d82025-05-15 16:15:21 +0000487
95630366963296f62025-06-02 21:16:32 +0800488 @Override
489 public int AddPost(Post post) {
490 if (post == null || post.postid == null || post.postid.isEmpty()) {
491 logger.warn("Invalid parameter: post is null or postid is empty");
492 return 2;
493 }
Haotian W88568d82025-05-15 16:15:21 +0000494
95630366963296f62025-06-02 21:16:32 +0800495 EntityManager em = null;
496 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000497
95630366963296f62025-06-02 21:16:32 +0800498 try {
499 em = emf.createEntityManager();
500 tx = em.getTransaction();
501 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000502
95630366963296f62025-06-02 21:16:32 +0800503 // 检查是否重复
504 Post existingPost = em.find(Post.class, post.postid);
505 if (existingPost != null) {
506 logger.warn("Post with ID {} already exists", post.postid);
507 return 1;
508 }
Haotian W88568d82025-05-15 16:15:21 +0000509
95630366963296f62025-06-02 21:16:32 +0800510 // 检查用户是否存在
511 User user = em.find(User.class, post.postuserid);
512 if (user == null) {
513 logger.warn("User with ID {} does not exist", post.postuserid);
514 return 2;
515 }
Haotian W88568d82025-05-15 16:15:21 +0000516
95630366963296f62025-06-02 21:16:32 +0800517 // 设置初始值
518 if (post.posttime == null) {
519 post.posttime = new Date();
520 }
521 post.replytime = 0;
522 post.readtime = 0;
Haotian W88568d82025-05-15 16:15:21 +0000523
95630366963296f62025-06-02 21:16:32 +0800524 // 保存帖子
525 em.persist(post);
526 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000527
95630366963296f62025-06-02 21:16:32 +0800528 logger.info("Successfully added new post with ID: {}", post.postid);
529 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000530
95630366963296f62025-06-02 21:16:32 +0800531 } catch (Exception e) {
532 if (tx != null && tx.isActive()) {
533 tx.rollback();
534 }
535 logger.error("Error adding post: {}", e.getMessage());
536 return 2;
537 } finally {
538 if (em != null) {
539 em.close();
540 }
541 }
542 }
Haotian W88568d82025-05-15 16:15:21 +0000543
95630366963296f62025-06-02 21:16:32 +0800544 @Override
545 public int UpdatePost(Post post) {
546 if (post == null || post.postid == null || post.postid.isEmpty()) {
547 logger.warn("Invalid parameter: post is null or postid is empty");
548 return 2;
549 }
Haotian W88568d82025-05-15 16:15:21 +0000550
95630366963296f62025-06-02 21:16:32 +0800551 EntityManager em = null;
552 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000553
95630366963296f62025-06-02 21:16:32 +0800554 try {
555 em = emf.createEntityManager();
556 tx = em.getTransaction();
557 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000558
95630366963296f62025-06-02 21:16:32 +0800559 // 检查帖子是否存在
560 Post existingPost = em.find(Post.class, post.postid);
561 if (existingPost == null) {
562 logger.warn("Post with ID {} does not exist", post.postid);
563 return 1;
564 }
Haotian W88568d82025-05-15 16:15:21 +0000565
95630366963296f62025-06-02 21:16:32 +0800566 // 保持原有不可修改的字段
567 post.postuserid = existingPost.postuserid;
568 post.posttime = existingPost.posttime;
569 post.replytime = existingPost.replytime;
570 post.readtime = existingPost.readtime;
Haotian W88568d82025-05-15 16:15:21 +0000571
95630366963296f62025-06-02 21:16:32 +0800572 // 更新帖子
573 em.merge(post);
574 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000575
95630366963296f62025-06-02 21:16:32 +0800576 logger.info("Successfully updated post with ID: {}", post.postid);
577 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000578
95630366963296f62025-06-02 21:16:32 +0800579 } catch (Exception e) {
580 if (tx != null && tx.isActive()) {
581 tx.rollback();
582 }
583 logger.error("Error updating post: {}", e.getMessage());
584 return 2;
585 } finally {
586 if (em != null) {
587 em.close();
588 }
589 }
590 }
Haotian W88568d82025-05-15 16:15:21 +0000591
95630366963296f62025-06-02 21:16:32 +0800592 @Override
593 public int DeletePost(String postid) {
594 if (postid == null || postid.isEmpty()) {
595 logger.warn("Invalid parameter: postid is null or empty");
596 return 2;
597 }
Haotian W88568d82025-05-15 16:15:21 +0000598
95630366963296f62025-06-02 21:16:32 +0800599 EntityManager em = null;
600 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000601
95630366963296f62025-06-02 21:16:32 +0800602 try {
603 em = emf.createEntityManager();
604 tx = em.getTransaction();
605 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000606
95630366963296f62025-06-02 21:16:32 +0800607 // 查找要删除的帖子
608 Post post = em.find(Post.class, postid);
609 if (post == null) {
610 logger.warn("Post with ID {} does not exist", postid);
611 tx.rollback();
612 return 1;
613 }
Haotian W88568d82025-05-15 16:15:21 +0000614
95630366963296f62025-06-02 21:16:32 +0800615 // 删除帖子(由于设置了级联删除,相关的回复会自动删除)
616 em.remove(post);
617 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000618
95630366963296f62025-06-02 21:16:32 +0800619 logger.info("Successfully deleted post with ID: {}", postid);
620 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000621
95630366963296f62025-06-02 21:16:32 +0800622 } catch (Exception e) {
623 if (tx != null && tx.isActive()) {
624 tx.rollback();
625 }
626 logger.error("Error deleting post: {}", e.getMessage());
627 return 2;
628 } finally {
629 if (em != null) {
630 em.close();
631 }
632 }
633 }
Haotian W88568d82025-05-15 16:15:21 +0000634
95630366963296f62025-06-02 21:16:32 +0800635 @Override
636 public int AddComment(String postid, String userid, String comment) {
637 if (postid == null || postid.isEmpty() ||
638 userid == null || userid.isEmpty() ||
639 comment == null || comment.isEmpty()) {
640 logger.warn("Invalid parameters: postid, userid or comment is null or empty");
641 return 2;
642 }
Haotian W88568d82025-05-15 16:15:21 +0000643
95630366963296f62025-06-02 21:16:32 +0800644 EntityManager em = null;
645 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000646
95630366963296f62025-06-02 21:16:32 +0800647 try {
648 em = emf.createEntityManager();
649 tx = em.getTransaction();
650 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000651
95630366963296f62025-06-02 21:16:32 +0800652 // 检查帖子是否存在
653 Post post = em.find(Post.class, postid);
654 if (post == null) {
655 logger.warn("Post with ID {} does not exist", postid);
656 return 1;
657 }
Haotian W88568d82025-05-15 16:15:21 +0000658
95630366963296f62025-06-02 21:16:32 +0800659 // 检查评论用户是否存在
660 User user = em.find(User.class, userid);
661 if (user == null) {
662 logger.warn("User with ID {} does not exist", userid);
663 return 1;
664 }
Haotian W88568d82025-05-15 16:15:21 +0000665
95630366963296f62025-06-02 21:16:32 +0800666 // 创建新的回复
667 PostReply reply = new PostReply();
668 reply.replyid = UUID.randomUUID().toString();
669 reply.postid = postid;
670 reply.content = comment;
671 reply.authorid = userid;
672 reply.createdAt = new Date();
Haotian W88568d82025-05-15 16:15:21 +0000673
95630366963296f62025-06-02 21:16:32 +0800674 // 保存回复
675 em.persist(reply);
Haotian W88568d82025-05-15 16:15:21 +0000676
95630366963296f62025-06-02 21:16:32 +0800677 // 更新帖子的回复数
678 post.replytime += 1;
679 em.merge(post);
Haotian W88568d82025-05-15 16:15:21 +0000680
95630366963296f62025-06-02 21:16:32 +0800681 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000682
95630366963296f62025-06-02 21:16:32 +0800683 logger.info("Successfully added comment by user {} to post {}, reply ID: {}",
684 userid, postid, reply.replyid);
685 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000686
95630366963296f62025-06-02 21:16:32 +0800687 } catch (Exception e) {
688 if (tx != null && tx.isActive()) {
689 tx.rollback();
690 }
691 logger.error("Error adding comment: {}", e.getMessage());
692 return 2;
693 } finally {
694 if (em != null) {
695 em.close();
696 }
697 }
698 }
Haotian W88568d82025-05-15 16:15:21 +0000699
95630366963296f62025-06-02 21:16:32 +0800700 @Override
701 public int DeleteComment(String postid, String commentid) {
702 if (postid == null || postid.isEmpty() || commentid == null || commentid.isEmpty()) {
703 logger.warn("Invalid parameters: postid or commentid is null or empty");
704 return 2;
705 }
Haotian W88568d82025-05-15 16:15:21 +0000706
95630366963296f62025-06-02 21:16:32 +0800707 EntityManager em = null;
708 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000709
95630366963296f62025-06-02 21:16:32 +0800710 try {
711 em = emf.createEntityManager();
712 tx = em.getTransaction();
713 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000714
95630366963296f62025-06-02 21:16:32 +0800715 // 检查帖子是否存在
716 Post post = em.find(Post.class, postid);
717 if (post == null) {
718 logger.warn("Post with ID {} does not exist", postid);
719 return 1;
720 }
Haotian W88568d82025-05-15 16:15:21 +0000721
95630366963296f62025-06-02 21:16:32 +0800722 // 检查评论是否存在且属于该帖子
723 PostReply reply = em.find(PostReply.class, commentid);
724 if (reply == null || !reply.postid.equals(postid)) {
725 logger.warn("Comment {} does not exist or does not belong to post {}", commentid, postid);
726 return 1;
727 }
Haotian W88568d82025-05-15 16:15:21 +0000728
95630366963296f62025-06-02 21:16:32 +0800729 // 删除评论
730 em.remove(reply);
Haotian W88568d82025-05-15 16:15:21 +0000731
95630366963296f62025-06-02 21:16:32 +0800732 // 更新帖子的回复数
733 post.replytime = Math.max(0, post.replytime - 1);
734 em.merge(post);
Haotian W88568d82025-05-15 16:15:21 +0000735
95630366963296f62025-06-02 21:16:32 +0800736 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000737
95630366963296f62025-06-02 21:16:32 +0800738 logger.info("Successfully deleted comment {} from post {}", commentid, postid);
739 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000740
95630366963296f62025-06-02 21:16:32 +0800741 } catch (Exception e) {
742 if (tx != null && tx.isActive()) {
743 tx.rollback();
744 }
745 logger.error("Error deleting comment: {}", e.getMessage());
746 return 2;
747 } finally {
748 if (em != null) {
749 em.close();
750 }
751 }
752 }
Haotian W88568d82025-05-15 16:15:21 +0000753
95630366963296f62025-06-02 21:16:32 +0800754 @Override
755 public boolean ExchangeMagicToUpload(String userid, int magic) {
756 if (userid == null || userid.isEmpty() || magic <= 0) {
757 logger.warn("参数无效: userid为空或magic <= 0");
758 return false;
759 }
Haotian W88568d82025-05-15 16:15:21 +0000760
95630366963296f62025-06-02 21:16:32 +0800761 EntityManager em = null;
762 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000763
95630366963296f62025-06-02 21:16:32 +0800764 try {
765 em = emf.createEntityManager();
766 tx = em.getTransaction();
767 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000768
95630366963296f62025-06-02 21:16:32 +0800769 UserPT userPT = em.find(UserPT.class, userid);
770 if (userPT == null) {
771 logger.warn("未找到用户 {} 的PT信息", userid);
772 return false;
773 }
Haotian W88568d82025-05-15 16:15:21 +0000774
95630366963296f62025-06-02 21:16:32 +0800775 if (userPT.magic < magic) {
776 logger.warn("用户 {} 的魔力值不足", userid);
777 return false;
778 }
Haotian W88568d82025-05-15 16:15:21 +0000779
95630366963296f62025-06-02 21:16:32 +0800780 // 1:1兑换,直接加上魔力值(假设单位是MB)
781 userPT.magic -= magic;
782 userPT.upload += magic;
Haotian W88568d82025-05-15 16:15:21 +0000783
95630366963296f62025-06-02 21:16:32 +0800784 // 更新分享率
785 if (userPT.download > 0) {
786 userPT.share = (double) userPT.upload / userPT.download;
787 }
Haotian W88568d82025-05-15 16:15:21 +0000788
95630366963296f62025-06-02 21:16:32 +0800789 em.merge(userPT);
790 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000791
95630366963296f62025-06-02 21:16:32 +0800792 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 上传量", userid, magic, magic);
793 return true;
Haotian W88568d82025-05-15 16:15:21 +0000794
95630366963296f62025-06-02 21:16:32 +0800795 } catch (Exception e) {
796 if (tx != null && tx.isActive()) {
797 tx.rollback();
798 }
799 logger.error("魔力值兑换上传量时发生错误: {}", e.getMessage());
800 return false;
801 } finally {
802 if (em != null) {
803 em.close();
804 }
805 }
806 }
Haotian W88568d82025-05-15 16:15:21 +0000807
95630366963296f62025-06-02 21:16:32 +0800808 @Override
809 public boolean ExchangeMagicToDownload(String userid, int magic) {
810 if (userid == null || userid.isEmpty() || magic <= 0) {
811 logger.warn("参数无效: userid为空或magic <= 0");
812 return false;
813 }
Haotian W88568d82025-05-15 16:15:21 +0000814
95630366963296f62025-06-02 21:16:32 +0800815 EntityManager em = null;
816 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000817
95630366963296f62025-06-02 21:16:32 +0800818 try {
819 em = emf.createEntityManager();
820 tx = em.getTransaction();
821 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000822
95630366963296f62025-06-02 21:16:32 +0800823 UserPT userPT = em.find(UserPT.class, userid);
824 if (userPT == null) {
825 logger.warn("未找到用户 {} 的PT信息", userid);
826 return false;
827 }
Haotian W88568d82025-05-15 16:15:21 +0000828
95630366963296f62025-06-02 21:16:32 +0800829 if (userPT.magic < magic) {
830 logger.warn("用户 {} 的魔力值不足", userid);
831 return false;
832 }
Haotian W88568d82025-05-15 16:15:21 +0000833
95630366963296f62025-06-02 21:16:32 +0800834 // 1:1兑换,直接减去魔力值对应的下载量(假设单位是MB)
835 userPT.magic -= magic;
836 userPT.download = Math.max(0, userPT.download - magic);
Haotian W88568d82025-05-15 16:15:21 +0000837
95630366963296f62025-06-02 21:16:32 +0800838 // 更新分享率
839 if (userPT.download > 0) {
840 userPT.share = (double) userPT.upload / userPT.download;
841 }
Haotian W88568d82025-05-15 16:15:21 +0000842
95630366963296f62025-06-02 21:16:32 +0800843 em.merge(userPT);
844 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000845
95630366963296f62025-06-02 21:16:32 +0800846 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 下载量减免", userid, magic, magic);
847 return true;
Haotian W88568d82025-05-15 16:15:21 +0000848
95630366963296f62025-06-02 21:16:32 +0800849 } catch (Exception e) {
850 if (tx != null && tx.isActive()) {
851 tx.rollback();
852 }
853 logger.error("魔力值兑换下载量时发生错误: {}", e.getMessage());
854 return false;
855 } finally {
856 if (em != null) {
857 em.close();
858 }
859 }
860 }
Haotian W88568d82025-05-15 16:15:21 +0000861
95630366963296f62025-06-02 21:16:32 +0800862 @Override
863 public boolean ExchangeMagicToVip(String userid, int magic) {
864 if (userid == null || userid.isEmpty() || magic <= 0) {
865 logger.warn("参数无效: userid为空或magic <= 0");
866 return false;
867 }
Haotian W88568d82025-05-15 16:15:21 +0000868
95630366963296f62025-06-02 21:16:32 +0800869 EntityManager em = null;
870 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000871
95630366963296f62025-06-02 21:16:32 +0800872 try {
873 em = emf.createEntityManager();
874 tx = em.getTransaction();
875 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000876
95630366963296f62025-06-02 21:16:32 +0800877 UserPT userPT = em.find(UserPT.class, userid);
878 if (userPT == null) {
879 logger.warn("未找到用户 {} 的PT信息", userid);
880 return false;
881 }
Haotian W88568d82025-05-15 16:15:21 +0000882
95630366963296f62025-06-02 21:16:32 +0800883 if (userPT.magic < magic) {
884 logger.warn("用户 {} 的魔力值不足", userid);
885 return false;
886 }
Haotian W88568d82025-05-15 16:15:21 +0000887
95630366963296f62025-06-02 21:16:32 +0800888 // 1:1兑换VIP下载次数
889 userPT.magic -= magic;
890 userPT.viptime += magic;
Haotian W88568d82025-05-15 16:15:21 +0000891
95630366963296f62025-06-02 21:16:32 +0800892 em.merge(userPT);
893 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000894
95630366963296f62025-06-02 21:16:32 +0800895 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {} 次VIP下载次数", userid, magic, magic);
896 return true;
Haotian W88568d82025-05-15 16:15:21 +0000897
95630366963296f62025-06-02 21:16:32 +0800898 } catch (Exception e) {
899 if (tx != null && tx.isActive()) {
900 tx.rollback();
901 }
902 logger.error("魔力值兑换VIP下载次数时发生错误: {}", e.getMessage());
903 return false;
904 } finally {
905 if (em != null) {
906 em.close();
907 }
908 }
909 }
Haotian W88568d82025-05-15 16:15:21 +0000910
95630366963296f62025-06-02 21:16:32 +0800911 @Override
912 public boolean UploadTransmitProfile(Profile profile) {
913 if (profile == null || profile.profileurl == null || profile.profileurl.isEmpty() ||
914 profile.userid == null || profile.userid.isEmpty()) {
915 logger.warn("参数无效: profile为空或必要字段为空");
916 return false;
917 }
Haotian W88568d82025-05-15 16:15:21 +0000918
95630366963296f62025-06-02 21:16:32 +0800919 EntityManager em = null;
920 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000921
95630366963296f62025-06-02 21:16:32 +0800922 try {
923 em = emf.createEntityManager();
924 tx = em.getTransaction();
925 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000926
95630366963296f62025-06-02 21:16:32 +0800927 // 检查用户是否存在
928 User user = em.find(User.class, profile.userid);
929 if (user == null) {
930 logger.warn("用户 {} 不存在", profile.userid);
931 return false;
932 }
Haotian W88568d82025-05-15 16:15:21 +0000933
95630366963296f62025-06-02 21:16:32 +0800934 // 检查是否已存在相同的迁移申请
935 Profile existingProfile = em.find(Profile.class, profile.profileurl);
936 if (existingProfile != null) {
937 logger.warn("迁移申请 {} 已存在", profile.profileurl);
938 return false;
939 }
Haotian W88568d82025-05-15 16:15:21 +0000940
95630366963296f62025-06-02 21:16:32 +0800941 // 设置初始值
942 profile.exampass = false;
943 profile.magicgived = "0";
944 profile.uploadgived = "0";
Haotian W88568d82025-05-15 16:15:21 +0000945
95630366963296f62025-06-02 21:16:32 +0800946 // 保存迁移申请
947 em.persist(profile);
948 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000949
95630366963296f62025-06-02 21:16:32 +0800950 logger.info("成功上传迁移申请 {}", profile.profileurl);
951 return true;
Haotian W88568d82025-05-15 16:15:21 +0000952
95630366963296f62025-06-02 21:16:32 +0800953 } catch (Exception e) {
954 if (tx != null && tx.isActive()) {
955 tx.rollback();
956 }
957 logger.error("上传迁移申请时发生错误: {}", e.getMessage());
958 return false;
959 } finally {
960 if (em != null) {
961 em.close();
962 }
963 }
964 }
Haotian W88568d82025-05-15 16:15:21 +0000965
95630366963296f62025-06-02 21:16:32 +0800966 @Override
967 public Profile GetTransmitProfile(String profileid) {
968 if (profileid == null || profileid.isEmpty()) {
969 logger.warn("参数无效: profileid为空");
970 return null;
971 }
Haotian W88568d82025-05-15 16:15:21 +0000972
95630366963296f62025-06-02 21:16:32 +0800973 EntityManager em = null;
Haotian W88568d82025-05-15 16:15:21 +0000974
95630366963296f62025-06-02 21:16:32 +0800975 try {
976 em = emf.createEntityManager();
977 Profile profile = em.find(Profile.class, profileid);
978 if (profile == null) {
979 logger.warn("未找到迁移申请 {}", profileid);
980 return null;
981 }
Haotian W88568d82025-05-15 16:15:21 +0000982
95630366963296f62025-06-02 21:16:32 +0800983 logger.info("成功获取迁移申请 {}", profileid);
984 return profile;
Haotian W88568d82025-05-15 16:15:21 +0000985
95630366963296f62025-06-02 21:16:32 +0800986 } catch (Exception e) {
987 logger.error("获取迁移申请时发生错误: {}", e.getMessage());
988 return null;
989 } finally {
990 if (em != null) {
991 em.close();
992 }
993 }
994 }
Haotian W88568d82025-05-15 16:15:21 +0000995
95630366963296f62025-06-02 21:16:32 +0800996 @Override
997 public boolean ExamTransmitProfile(String profileid, boolean result) {
998 if (profileid == null || profileid.isEmpty()) {
999 logger.warn("参数无效: profileid为空");
1000 return false;
1001 }
Haotian W88568d82025-05-15 16:15:21 +00001002
95630366963296f62025-06-02 21:16:32 +08001003 EntityManager em = null;
1004 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +00001005
95630366963296f62025-06-02 21:16:32 +08001006 try {
1007 em = emf.createEntityManager();
1008 tx = em.getTransaction();
1009 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +00001010
95630366963296f62025-06-02 21:16:32 +08001011 // 查找迁移申请
1012 Profile profile = em.find(Profile.class, profileid);
1013 if (profile == null) {
1014 logger.warn("未找到迁移申请 {}", profileid);
1015 return false;
1016 }
Haotian W88568d82025-05-15 16:15:21 +00001017
95630366963296f62025-06-02 21:16:32 +08001018 // 更新审核状态
1019 profile.exampass = result;
Haotian W88568d82025-05-15 16:15:21 +00001020
95630366963296f62025-06-02 21:16:32 +08001021 if (result) {
1022 // 如果审核通过,更新用户的PT信息
1023 UserPT userPT = em.find(UserPT.class, profile.userid);
1024 if (userPT != null) {
1025 // 发放魔力值
1026 int magicToGive = Integer.parseInt(profile.magictogive);
1027 userPT.magic += magicToGive;
1028 profile.magicgived = String.valueOf(magicToGive);
Haotian W88568d82025-05-15 16:15:21 +00001029
95630366963296f62025-06-02 21:16:32 +08001030 // 发放上传量
1031 long uploadToGive = Long.parseLong(profile.uploadtogive);
1032 userPT.upload += uploadToGive;
1033 profile.uploadgived = String.valueOf(uploadToGive);
Haotian W88568d82025-05-15 16:15:21 +00001034
95630366963296f62025-06-02 21:16:32 +08001035 em.merge(userPT);
1036 }
1037 }
Haotian W88568d82025-05-15 16:15:21 +00001038
95630366963296f62025-06-02 21:16:32 +08001039 em.merge(profile);
1040 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +00001041
95630366963296f62025-06-02 21:16:32 +08001042 logger.info("成功审核迁移申请 {}, 结果: {}", profileid, result);
1043 return true;
Haotian W88568d82025-05-15 16:15:21 +00001044
95630366963296f62025-06-02 21:16:32 +08001045 } catch (Exception e) {
1046 if (tx != null && tx.isActive()) {
1047 tx.rollback();
1048 }
1049 logger.error("审核迁移申请时发生错误: {}", e.getMessage());
1050 return false;
1051 } finally {
1052 if (em != null) {
1053 em.close();
1054 }
1055 }
1056 }
Haotian W88568d82025-05-15 16:15:21 +00001057
95630366963296f62025-06-02 21:16:32 +08001058 @Override
1059 public Profile[] GetTransmitProfileList() {
1060 EntityManager em = null;
Haotian W88568d82025-05-15 16:15:21 +00001061
95630366963296f62025-06-02 21:16:32 +08001062 try {
1063 em = emf.createEntityManager();
Haotian W88568d82025-05-15 16:15:21 +00001064
95630366963296f62025-06-02 21:16:32 +08001065 // 获取所有迁移申请
1066 QProfile p = QProfile.profile;
1067 List<Profile> profiles = new JPAQuery<>(em)
1068 .select(p)
1069 .from(p)
1070 .fetch();
Haotian W88568d82025-05-15 16:15:21 +00001071
95630366963296f62025-06-02 21:16:32 +08001072 logger.info("成功获取所有迁移申请,共 {} 条", profiles.size());
1073 return profiles.toArray(new Profile[0]);
Haotian W88568d82025-05-15 16:15:21 +00001074
95630366963296f62025-06-02 21:16:32 +08001075 } catch (Exception e) {
1076 logger.error("获取迁移申请列表时发生错误: {}", e.getMessage());
1077 return new Profile[0];
1078 } finally {
1079 if (em != null) {
1080 em.close();
1081 }
1082 }
1083 }
1084}