blob: 5152e478e2dd5f62b02e8818b927c070dac89458 [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
Raveraae06122025-06-05 08:13:35 +0000131 public Seed[] GetSeedListByTag(String tag) {
132 return new Seed[0];
133 }
134
135 @Override
95630366963296f62025-06-02 21:16:32 +0800136 public boolean AddCollect(String userid, String postid) {
137 return false;
138 }
root33a7d952025-05-18 17:24:41 +0000139
95630366963296f62025-06-02 21:16:32 +0800140 @Override
141 public boolean DeleteCollect(String userid, String postid) {
142 return false;
143 }
Haotian W88568d82025-05-15 16:15:21 +0000144
95630366963296f62025-06-02 21:16:32 +0800145 @Override
146 public int AddBegSeed(BegInfo info) {
147 if (info == null || info.begid == null || info.begid.isEmpty()) {
148 logger.warn("Invalid parameter: info is null or begid is empty");
149 return 2;
150 }
Haotian W88568d82025-05-15 16:15:21 +0000151
95630366963296f62025-06-02 21:16:32 +0800152 EntityManager em = null;
153 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000154
95630366963296f62025-06-02 21:16:32 +0800155 try {
156 em = emf.createEntityManager();
157 tx = em.getTransaction();
158 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000159
95630366963296f62025-06-02 21:16:32 +0800160 // 检查是否重复
161 BegInfo existingBeg = em.find(BegInfo.class, info.begid);
162 if (existingBeg != null) {
163 logger.warn("BegSeed with ID {} already exists", info.begid);
164 return 1;
165 }
Haotian W88568d82025-05-15 16:15:21 +0000166
95630366963296f62025-06-02 21:16:32 +0800167 // 设置默认值
168 if (info.endtime == null) {
169 // 设置默认14天截止期
170 Calendar calendar = Calendar.getInstance();
171 calendar.add(Calendar.DAY_OF_MONTH, 14);
172 info.endtime = calendar.getTime();
173 }
174 info.hasseed = 0;
Haotian W88568d82025-05-15 16:15:21 +0000175
95630366963296f62025-06-02 21:16:32 +0800176 // 保存新的求种信息
177 em.persist(info);
178 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000179
95630366963296f62025-06-02 21:16:32 +0800180 logger.info("Successfully added new BegSeed with ID: {}", info.begid);
181 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000182
95630366963296f62025-06-02 21:16:32 +0800183 } catch (Exception e) {
184 if (tx != null && tx.isActive()) {
185 tx.rollback();
186 }
187 logger.error("Error adding BegSeed: {}", e.getMessage());
188 return 2;
189 } finally {
190 if (em != null) {
191 em.close();
192 }
193 }
194 }
Haotian W88568d82025-05-15 16:15:21 +0000195
95630366963296f62025-06-02 21:16:32 +0800196 @Override
197 public int UpdateBegSeed(BegInfo info) {
198 if (info == null || info.begid == null || info.begid.isEmpty()) {
199 logger.warn("Invalid parameter: info is null or begid is empty");
200 return 2;
201 }
Haotian W88568d82025-05-15 16:15:21 +0000202
95630366963296f62025-06-02 21:16:32 +0800203 EntityManager em = null;
204 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000205
95630366963296f62025-06-02 21:16:32 +0800206 try {
207 em = emf.createEntityManager();
208 tx = em.getTransaction();
209 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000210
95630366963296f62025-06-02 21:16:32 +0800211 // 检查是否存在
212 BegInfo existingBeg = em.find(BegInfo.class, info.begid);
213 if (existingBeg == null) {
214 logger.warn("BegSeed with ID {} does not exist", info.begid);
215 return 1;
216 }
Haotian W88568d82025-05-15 16:15:21 +0000217
95630366963296f62025-06-02 21:16:32 +0800218 // 保持原有值不变的字段
219 info.hasseed = existingBeg.hasseed;
220 if (info.endtime == null) {
221 info.endtime = existingBeg.endtime;
222 }
Haotian W88568d82025-05-15 16:15:21 +0000223
95630366963296f62025-06-02 21:16:32 +0800224 // 更新求种信息
225 em.merge(info);
226 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000227
95630366963296f62025-06-02 21:16:32 +0800228 logger.info("Successfully updated BegSeed with ID: {}", info.begid);
229 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000230
95630366963296f62025-06-02 21:16:32 +0800231 } catch (Exception e) {
232 if (tx != null && tx.isActive()) {
233 tx.rollback();
234 }
235 logger.error("Error updating BegSeed: {}", e.getMessage());
236 return 2;
237 } finally {
238 if (em != null) {
239 em.close();
240 }
241 }
242 }
Haotian W88568d82025-05-15 16:15:21 +0000243
95630366963296f62025-06-02 21:16:32 +0800244 @Override
245 public int DeleteBegSeed(String begid) {
246 if (begid == null || begid.isEmpty()) {
247 logger.warn("Invalid parameter: begid is null or empty");
248 return 2;
249 }
Haotian W88568d82025-05-15 16:15:21 +0000250
95630366963296f62025-06-02 21:16:32 +0800251 EntityManager em = null;
252 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000253
95630366963296f62025-06-02 21:16:32 +0800254 try {
255 em = emf.createEntityManager();
256 tx = em.getTransaction();
257 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000258
95630366963296f62025-06-02 21:16:32 +0800259 // 查找要删除的求种信息
260 BegInfo begInfo = em.find(BegInfo.class, begid);
261 if (begInfo == null) {
262 logger.warn("BegSeed with ID {} does not exist", begid);
263 tx.rollback();
264 return 1;
265 }
Haotian W88568d82025-05-15 16:15:21 +0000266
95630366963296f62025-06-02 21:16:32 +0800267 // 删除求种信息
268 em.remove(begInfo);
269 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000270
95630366963296f62025-06-02 21:16:32 +0800271 logger.info("Successfully deleted BegSeed with ID: {}", begid);
272 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000273
95630366963296f62025-06-02 21:16:32 +0800274 } catch (Exception e) {
275 if (tx != null && tx.isActive()) {
276 tx.rollback();
277 }
278 logger.error("Error deleting BegSeed: {}", e.getMessage());
279 return 2;
280 } finally {
281 if (em != null) {
282 em.close();
283 }
284 }
285 }
Haotian W88568d82025-05-15 16:15:21 +0000286
95630366963296f62025-06-02 21:16:32 +0800287 @Override
288 public int VoteSeed(String begId, String seedId, String userId) {
289 if (begId == null || seedId == null || userId == null ||
290 begId.isEmpty() || seedId.isEmpty() || userId.isEmpty()) {
291 logger.warn("Invalid parameters: begId, seedId or userId is null or empty");
292 return 2;
293 }
Haotian W88568d82025-05-15 16:15:21 +0000294
95630366963296f62025-06-02 21:16:32 +0800295 EntityManager em = null;
296 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000297
95630366963296f62025-06-02 21:16:32 +0800298 try {
299 em = emf.createEntityManager();
300 tx = em.getTransaction();
301 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000302
95630366963296f62025-06-02 21:16:32 +0800303 // 检查求种信息是否存在
304 BegInfo begInfo = em.find(BegInfo.class, begId);
305 if (begInfo == null) {
306 logger.warn("BegSeed with ID {} does not exist", begId);
307 return 2;
308 }
Haotian W88568d82025-05-15 16:15:21 +0000309
95630366963296f62025-06-02 21:16:32 +0800310 // 检查用户是否已投票
311 Long voteCount = new JPAQuery<>(em)
312 .select(QUserVotes.userVotes.count())
313 .from(QUserVotes.userVotes)
314 .where(QUserVotes.userVotes.id.eq(new entity.UserVotesId(userId, begId, seedId)))
315 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000316
95630366963296f62025-06-02 21:16:32 +0800317 if (voteCount != null && voteCount > 0) {
318 logger.warn("User {} has already voted for seed {} in beg {}", userId, seedId, begId);
319 return 1;
320 }
Haotian W88568d82025-05-15 16:15:21 +0000321
95630366963296f62025-06-02 21:16:32 +0800322 // 创建新的投票记录
323 em.createNativeQuery("INSERT INTO UserVotes (user_id, beg_id, seed_id, created_at) " +
324 "VALUES (?, ?, ?, CURRENT_TIMESTAMP)")
325 .setParameter(1, userId)
326 .setParameter(2, begId)
327 .setParameter(3, seedId)
328 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000329
95630366963296f62025-06-02 21:16:32 +0800330 // 更新SubmitSeed表中的投票数
331 em.createQuery("UPDATE SubmitSeed s SET s.votes = s.votes + 1 " +
332 "WHERE s.id.begId = :begId AND s.id.seedId = :seedId")
333 .setParameter("begId", begId)
334 .setParameter("seedId", seedId)
335 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000336
95630366963296f62025-06-02 21:16:32 +0800337 tx.commit();
338 logger.info("Successfully added vote from user {} for seed {} in beg {}", userId, seedId, begId);
339 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000340
95630366963296f62025-06-02 21:16:32 +0800341 } catch (Exception e) {
342 if (tx != null && tx.isActive()) {
343 tx.rollback();
344 }
345 logger.error("Error voting for seed: {}", e.getMessage());
346 return 2;
347 } finally {
348 if (em != null) {
349 em.close();
350 }
351 }
352 }
Haotian W88568d82025-05-15 16:15:21 +0000353
95630366963296f62025-06-02 21:16:32 +0800354 @Override
355 public int SubmitSeed(String begid, Seed seed) {
356 if (begid == null || seed == null || begid.isEmpty() || seed.seedid == null) {
357 logger.warn("Invalid parameters: begid or seed is null or empty");
358 return 2;
359 }
Haotian W88568d82025-05-15 16:15:21 +0000360
95630366963296f62025-06-02 21:16:32 +0800361 EntityManager em = null;
362 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000363
95630366963296f62025-06-02 21:16:32 +0800364 try {
365 em = emf.createEntityManager();
366 tx = em.getTransaction();
367 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000368
95630366963296f62025-06-02 21:16:32 +0800369 // 检查求种信息是否存在
370 BegInfo begInfo = em.find(BegInfo.class, begid);
371 if (begInfo == null) {
372 logger.warn("BegSeed with ID {} does not exist", begid);
373 return 2;
374 }
Haotian W88568d82025-05-15 16:15:21 +0000375
95630366963296f62025-06-02 21:16:32 +0800376 // 检查种子是否已提交过
377 QSubmitSeed ss = QSubmitSeed.submitSeed;
378 Long submitCount = new JPAQuery<>(em)
379 .select(ss.count())
380 .from(ss)
381 .where(ss.begInfo.begid.eq(begid))
382 .where(ss.seed.seedid.eq(seed.seedid))
383 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000384
95630366963296f62025-06-02 21:16:32 +0800385 if (submitCount > 0) {
386 logger.warn("Seed {} has already been submitted for beg {}", seed.seedid,
387 begid);
388 return 1;
389 }
Haotian W88568d82025-05-15 16:15:21 +0000390
95630366963296f62025-06-02 21:16:32 +0800391 // 保存种子信息(如果不存在)
392 if (em.find(Seed.class, seed.seedid) == null) {
393 em.persist(seed);
394 }
Haotian W88568d82025-05-15 16:15:21 +0000395
95630366963296f62025-06-02 21:16:32 +0800396 // 创建提交记录
397 em.createNativeQuery("INSERT INTO SubmitSeed (beg_id, seed_id, votes) VALUES (?, ?, 0)")
398 .setParameter(1, begid)
399 .setParameter(2, seed.seedid)
400 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000401
95630366963296f62025-06-02 21:16:32 +0800402 tx.commit();
403 logger.info("Successfully submitted seed {} for beg {}", seed.seedid, begid);
404 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000405
95630366963296f62025-06-02 21:16:32 +0800406 } catch (Exception e) {
407 if (tx != null && tx.isActive()) {
408 tx.rollback();
409 }
410 logger.error("Error submitting seed: {}", e.getMessage());
411 return 2;
412 } finally {
413 if (em != null) {
414 em.close();
415 }
416 }
417 }
Haotian W88568d82025-05-15 16:15:21 +0000418
95630366963296f62025-06-02 21:16:32 +0800419 @Override
420 public void SettleBeg() {
421 EntityManager em = null;
422 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000423
95630366963296f62025-06-02 21:16:32 +0800424 try {
425 em = emf.createEntityManager();
426 tx = em.getTransaction();
427 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000428
95630366963296f62025-06-02 21:16:32 +0800429 // 1. 获取所有已过期且未完成的求种信息
430 QBegInfo b = QBegInfo.begInfo;
431 List<BegInfo> expiredBegs = new JPAQuery<>(em)
432 .select(b)
433 .from(b)
434 .where(b.endtime.loe(new Date())
435 .and(b.hasseed.eq(0)))
436 .fetch();
Haotian W88568d82025-05-15 16:15:21 +0000437
95630366963296f62025-06-02 21:16:32 +0800438 for (BegInfo beg : expiredBegs) {
439 // 2. 查找投票最多的提交任务
440 QSubmitSeed ss = QSubmitSeed.submitSeed;
441 Tuple topSubmission = new JPAQuery<>(em)
442 .select(ss.seed.seedid, ss.votes)
443 .from(ss)
444 .where(ss.begInfo.begid.eq(beg.begid))
445 .orderBy(ss.votes.desc())
446 .limit(1)
447 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000448
95630366963296f62025-06-02 21:16:32 +0800449 if (topSubmission != null && topSubmission.get(ss.votes) > 0) {
450 String seedId = topSubmission.get(ss.seed.seedid);
Haotian W88568d82025-05-15 16:15:21 +0000451
95630366963296f62025-06-02 21:16:32 +0800452 // 3. 获取上传者ID
453 QSeed s = QSeed.seed;
454 String ownerId = new JPAQuery<>(em)
455 .select(s.seeduserid)
456 .from(s)
457 .where(s.seedid.eq(seedId))
458 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000459
95630366963296f62025-06-02 21:16:32 +0800460 // 4. 获取上传者的PT信息并更新魔力值
461 UserPT ownerPT = em.find(UserPT.class, ownerId);
462 if (ownerPT != null) {
463 // 5. 发放奖励
464 ownerPT.magic += beg.magic;
465 em.merge(ownerPT);
Haotian W88568d82025-05-15 16:15:21 +0000466
95630366963296f62025-06-02 21:16:32 +0800467 // 6. 更新求种状态
468 beg.hasseed = 1;
469 em.merge(beg);
Haotian W88568d82025-05-15 16:15:21 +0000470
95630366963296f62025-06-02 21:16:32 +0800471 logger.info("Reward {} magic points awarded to user {} for beg {}",
472 beg.magic, ownerId, beg.begid);
473 }
474 }
475 }
Haotian W88568d82025-05-15 16:15:21 +0000476
95630366963296f62025-06-02 21:16:32 +0800477 tx.commit();
478 logger.info("Successfully settled {} expired beg requests",
479 expiredBegs.size());
Haotian W88568d82025-05-15 16:15:21 +0000480
95630366963296f62025-06-02 21:16:32 +0800481 } catch (Exception e) {
482 if (tx != null && tx.isActive()) {
483 tx.rollback();
484 }
485 logger.error("Error settling beg requests: {}", e.getMessage(), e);
486 } finally {
487 if (em != null) {
488 em.close();
489 }
490 }
491 }
Haotian W88568d82025-05-15 16:15:21 +0000492
95630366963296f62025-06-02 21:16:32 +0800493 @Override
494 public int AddPost(Post post) {
495 if (post == null || post.postid == null || post.postid.isEmpty()) {
496 logger.warn("Invalid parameter: post is null or postid is empty");
497 return 2;
498 }
Haotian W88568d82025-05-15 16:15:21 +0000499
95630366963296f62025-06-02 21:16:32 +0800500 EntityManager em = null;
501 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000502
95630366963296f62025-06-02 21:16:32 +0800503 try {
504 em = emf.createEntityManager();
505 tx = em.getTransaction();
506 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000507
95630366963296f62025-06-02 21:16:32 +0800508 // 检查是否重复
509 Post existingPost = em.find(Post.class, post.postid);
510 if (existingPost != null) {
511 logger.warn("Post with ID {} already exists", post.postid);
512 return 1;
513 }
Haotian W88568d82025-05-15 16:15:21 +0000514
95630366963296f62025-06-02 21:16:32 +0800515 // 检查用户是否存在
516 User user = em.find(User.class, post.postuserid);
517 if (user == null) {
518 logger.warn("User with ID {} does not exist", post.postuserid);
519 return 2;
520 }
Haotian W88568d82025-05-15 16:15:21 +0000521
95630366963296f62025-06-02 21:16:32 +0800522 // 设置初始值
523 if (post.posttime == null) {
524 post.posttime = new Date();
525 }
526 post.replytime = 0;
527 post.readtime = 0;
Haotian W88568d82025-05-15 16:15:21 +0000528
95630366963296f62025-06-02 21:16:32 +0800529 // 保存帖子
530 em.persist(post);
531 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000532
95630366963296f62025-06-02 21:16:32 +0800533 logger.info("Successfully added new post with ID: {}", post.postid);
534 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000535
95630366963296f62025-06-02 21:16:32 +0800536 } catch (Exception e) {
537 if (tx != null && tx.isActive()) {
538 tx.rollback();
539 }
540 logger.error("Error adding post: {}", e.getMessage());
541 return 2;
542 } finally {
543 if (em != null) {
544 em.close();
545 }
546 }
547 }
Haotian W88568d82025-05-15 16:15:21 +0000548
95630366963296f62025-06-02 21:16:32 +0800549 @Override
550 public int UpdatePost(Post post) {
551 if (post == null || post.postid == null || post.postid.isEmpty()) {
552 logger.warn("Invalid parameter: post is null or postid is empty");
553 return 2;
554 }
Haotian W88568d82025-05-15 16:15:21 +0000555
95630366963296f62025-06-02 21:16:32 +0800556 EntityManager em = null;
557 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000558
95630366963296f62025-06-02 21:16:32 +0800559 try {
560 em = emf.createEntityManager();
561 tx = em.getTransaction();
562 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000563
95630366963296f62025-06-02 21:16:32 +0800564 // 检查帖子是否存在
565 Post existingPost = em.find(Post.class, post.postid);
566 if (existingPost == null) {
567 logger.warn("Post with ID {} does not exist", post.postid);
568 return 1;
569 }
Haotian W88568d82025-05-15 16:15:21 +0000570
95630366963296f62025-06-02 21:16:32 +0800571 // 保持原有不可修改的字段
572 post.postuserid = existingPost.postuserid;
573 post.posttime = existingPost.posttime;
574 post.replytime = existingPost.replytime;
575 post.readtime = existingPost.readtime;
Haotian W88568d82025-05-15 16:15:21 +0000576
95630366963296f62025-06-02 21:16:32 +0800577 // 更新帖子
578 em.merge(post);
579 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000580
95630366963296f62025-06-02 21:16:32 +0800581 logger.info("Successfully updated post with ID: {}", post.postid);
582 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000583
95630366963296f62025-06-02 21:16:32 +0800584 } catch (Exception e) {
585 if (tx != null && tx.isActive()) {
586 tx.rollback();
587 }
588 logger.error("Error updating post: {}", e.getMessage());
589 return 2;
590 } finally {
591 if (em != null) {
592 em.close();
593 }
594 }
595 }
Haotian W88568d82025-05-15 16:15:21 +0000596
95630366963296f62025-06-02 21:16:32 +0800597 @Override
598 public int DeletePost(String postid) {
599 if (postid == null || postid.isEmpty()) {
600 logger.warn("Invalid parameter: postid is null or empty");
601 return 2;
602 }
Haotian W88568d82025-05-15 16:15:21 +0000603
95630366963296f62025-06-02 21:16:32 +0800604 EntityManager em = null;
605 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000606
95630366963296f62025-06-02 21:16:32 +0800607 try {
608 em = emf.createEntityManager();
609 tx = em.getTransaction();
610 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000611
95630366963296f62025-06-02 21:16:32 +0800612 // 查找要删除的帖子
613 Post post = em.find(Post.class, postid);
614 if (post == null) {
615 logger.warn("Post with ID {} does not exist", postid);
616 tx.rollback();
617 return 1;
618 }
Haotian W88568d82025-05-15 16:15:21 +0000619
95630366963296f62025-06-02 21:16:32 +0800620 // 删除帖子(由于设置了级联删除,相关的回复会自动删除)
621 em.remove(post);
622 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000623
95630366963296f62025-06-02 21:16:32 +0800624 logger.info("Successfully deleted post with ID: {}", postid);
625 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000626
95630366963296f62025-06-02 21:16:32 +0800627 } catch (Exception e) {
628 if (tx != null && tx.isActive()) {
629 tx.rollback();
630 }
631 logger.error("Error deleting post: {}", e.getMessage());
632 return 2;
633 } finally {
634 if (em != null) {
635 em.close();
636 }
637 }
638 }
Haotian W88568d82025-05-15 16:15:21 +0000639
95630366963296f62025-06-02 21:16:32 +0800640 @Override
641 public int AddComment(String postid, String userid, String comment) {
642 if (postid == null || postid.isEmpty() ||
643 userid == null || userid.isEmpty() ||
644 comment == null || comment.isEmpty()) {
645 logger.warn("Invalid parameters: postid, userid or comment is null or empty");
646 return 2;
647 }
Haotian W88568d82025-05-15 16:15:21 +0000648
95630366963296f62025-06-02 21:16:32 +0800649 EntityManager em = null;
650 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000651
95630366963296f62025-06-02 21:16:32 +0800652 try {
653 em = emf.createEntityManager();
654 tx = em.getTransaction();
655 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000656
95630366963296f62025-06-02 21:16:32 +0800657 // 检查帖子是否存在
658 Post post = em.find(Post.class, postid);
659 if (post == null) {
660 logger.warn("Post with ID {} does not exist", postid);
661 return 1;
662 }
Haotian W88568d82025-05-15 16:15:21 +0000663
95630366963296f62025-06-02 21:16:32 +0800664 // 检查评论用户是否存在
665 User user = em.find(User.class, userid);
666 if (user == null) {
667 logger.warn("User with ID {} does not exist", userid);
668 return 1;
669 }
Haotian W88568d82025-05-15 16:15:21 +0000670
95630366963296f62025-06-02 21:16:32 +0800671 // 创建新的回复
672 PostReply reply = new PostReply();
673 reply.replyid = UUID.randomUUID().toString();
674 reply.postid = postid;
675 reply.content = comment;
676 reply.authorid = userid;
677 reply.createdAt = new Date();
Haotian W88568d82025-05-15 16:15:21 +0000678
95630366963296f62025-06-02 21:16:32 +0800679 // 保存回复
680 em.persist(reply);
Haotian W88568d82025-05-15 16:15:21 +0000681
95630366963296f62025-06-02 21:16:32 +0800682 // 更新帖子的回复数
683 post.replytime += 1;
684 em.merge(post);
Haotian W88568d82025-05-15 16:15:21 +0000685
95630366963296f62025-06-02 21:16:32 +0800686 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000687
95630366963296f62025-06-02 21:16:32 +0800688 logger.info("Successfully added comment by user {} to post {}, reply ID: {}",
689 userid, postid, reply.replyid);
690 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000691
95630366963296f62025-06-02 21:16:32 +0800692 } catch (Exception e) {
693 if (tx != null && tx.isActive()) {
694 tx.rollback();
695 }
696 logger.error("Error adding comment: {}", e.getMessage());
697 return 2;
698 } finally {
699 if (em != null) {
700 em.close();
701 }
702 }
703 }
Haotian W88568d82025-05-15 16:15:21 +0000704
95630366963296f62025-06-02 21:16:32 +0800705 @Override
706 public int DeleteComment(String postid, String commentid) {
707 if (postid == null || postid.isEmpty() || commentid == null || commentid.isEmpty()) {
708 logger.warn("Invalid parameters: postid or commentid is null or empty");
709 return 2;
710 }
Haotian W88568d82025-05-15 16:15:21 +0000711
95630366963296f62025-06-02 21:16:32 +0800712 EntityManager em = null;
713 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000714
95630366963296f62025-06-02 21:16:32 +0800715 try {
716 em = emf.createEntityManager();
717 tx = em.getTransaction();
718 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000719
95630366963296f62025-06-02 21:16:32 +0800720 // 检查帖子是否存在
721 Post post = em.find(Post.class, postid);
722 if (post == null) {
723 logger.warn("Post with ID {} does not exist", postid);
724 return 1;
725 }
Haotian W88568d82025-05-15 16:15:21 +0000726
95630366963296f62025-06-02 21:16:32 +0800727 // 检查评论是否存在且属于该帖子
728 PostReply reply = em.find(PostReply.class, commentid);
729 if (reply == null || !reply.postid.equals(postid)) {
730 logger.warn("Comment {} does not exist or does not belong to post {}", commentid, postid);
731 return 1;
732 }
Haotian W88568d82025-05-15 16:15:21 +0000733
95630366963296f62025-06-02 21:16:32 +0800734 // 删除评论
735 em.remove(reply);
Haotian W88568d82025-05-15 16:15:21 +0000736
95630366963296f62025-06-02 21:16:32 +0800737 // 更新帖子的回复数
738 post.replytime = Math.max(0, post.replytime - 1);
739 em.merge(post);
Haotian W88568d82025-05-15 16:15:21 +0000740
95630366963296f62025-06-02 21:16:32 +0800741 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000742
95630366963296f62025-06-02 21:16:32 +0800743 logger.info("Successfully deleted comment {} from post {}", commentid, postid);
744 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000745
95630366963296f62025-06-02 21:16:32 +0800746 } catch (Exception e) {
747 if (tx != null && tx.isActive()) {
748 tx.rollback();
749 }
750 logger.error("Error deleting comment: {}", e.getMessage());
751 return 2;
752 } finally {
753 if (em != null) {
754 em.close();
755 }
756 }
757 }
Haotian W88568d82025-05-15 16:15:21 +0000758
95630366963296f62025-06-02 21:16:32 +0800759 @Override
760 public boolean ExchangeMagicToUpload(String userid, int magic) {
761 if (userid == null || userid.isEmpty() || magic <= 0) {
762 logger.warn("参数无效: userid为空或magic <= 0");
763 return false;
764 }
Haotian W88568d82025-05-15 16:15:21 +0000765
95630366963296f62025-06-02 21:16:32 +0800766 EntityManager em = null;
767 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000768
95630366963296f62025-06-02 21:16:32 +0800769 try {
770 em = emf.createEntityManager();
771 tx = em.getTransaction();
772 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000773
95630366963296f62025-06-02 21:16:32 +0800774 UserPT userPT = em.find(UserPT.class, userid);
775 if (userPT == null) {
776 logger.warn("未找到用户 {} 的PT信息", userid);
777 return false;
778 }
Haotian W88568d82025-05-15 16:15:21 +0000779
95630366963296f62025-06-02 21:16:32 +0800780 if (userPT.magic < magic) {
781 logger.warn("用户 {} 的魔力值不足", userid);
782 return false;
783 }
Haotian W88568d82025-05-15 16:15:21 +0000784
95630366963296f62025-06-02 21:16:32 +0800785 // 1:1兑换,直接加上魔力值(假设单位是MB)
786 userPT.magic -= magic;
787 userPT.upload += magic;
Haotian W88568d82025-05-15 16:15:21 +0000788
95630366963296f62025-06-02 21:16:32 +0800789 // 更新分享率
790 if (userPT.download > 0) {
791 userPT.share = (double) userPT.upload / userPT.download;
792 }
Haotian W88568d82025-05-15 16:15:21 +0000793
95630366963296f62025-06-02 21:16:32 +0800794 em.merge(userPT);
795 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000796
95630366963296f62025-06-02 21:16:32 +0800797 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 上传量", userid, magic, magic);
798 return true;
Haotian W88568d82025-05-15 16:15:21 +0000799
95630366963296f62025-06-02 21:16:32 +0800800 } catch (Exception e) {
801 if (tx != null && tx.isActive()) {
802 tx.rollback();
803 }
804 logger.error("魔力值兑换上传量时发生错误: {}", e.getMessage());
805 return false;
806 } finally {
807 if (em != null) {
808 em.close();
809 }
810 }
811 }
Haotian W88568d82025-05-15 16:15:21 +0000812
95630366963296f62025-06-02 21:16:32 +0800813 @Override
814 public boolean ExchangeMagicToDownload(String userid, int magic) {
815 if (userid == null || userid.isEmpty() || magic <= 0) {
816 logger.warn("参数无效: userid为空或magic <= 0");
817 return false;
818 }
Haotian W88568d82025-05-15 16:15:21 +0000819
95630366963296f62025-06-02 21:16:32 +0800820 EntityManager em = null;
821 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000822
95630366963296f62025-06-02 21:16:32 +0800823 try {
824 em = emf.createEntityManager();
825 tx = em.getTransaction();
826 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000827
95630366963296f62025-06-02 21:16:32 +0800828 UserPT userPT = em.find(UserPT.class, userid);
829 if (userPT == null) {
830 logger.warn("未找到用户 {} 的PT信息", userid);
831 return false;
832 }
Haotian W88568d82025-05-15 16:15:21 +0000833
95630366963296f62025-06-02 21:16:32 +0800834 if (userPT.magic < magic) {
835 logger.warn("用户 {} 的魔力值不足", userid);
836 return false;
837 }
Haotian W88568d82025-05-15 16:15:21 +0000838
95630366963296f62025-06-02 21:16:32 +0800839 // 1:1兑换,直接减去魔力值对应的下载量(假设单位是MB)
840 userPT.magic -= magic;
841 userPT.download = Math.max(0, userPT.download - magic);
Haotian W88568d82025-05-15 16:15:21 +0000842
95630366963296f62025-06-02 21:16:32 +0800843 // 更新分享率
844 if (userPT.download > 0) {
845 userPT.share = (double) userPT.upload / userPT.download;
846 }
Haotian W88568d82025-05-15 16:15:21 +0000847
95630366963296f62025-06-02 21:16:32 +0800848 em.merge(userPT);
849 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000850
95630366963296f62025-06-02 21:16:32 +0800851 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 下载量减免", userid, magic, magic);
852 return true;
Haotian W88568d82025-05-15 16:15:21 +0000853
95630366963296f62025-06-02 21:16:32 +0800854 } catch (Exception e) {
855 if (tx != null && tx.isActive()) {
856 tx.rollback();
857 }
858 logger.error("魔力值兑换下载量时发生错误: {}", e.getMessage());
859 return false;
860 } finally {
861 if (em != null) {
862 em.close();
863 }
864 }
865 }
Haotian W88568d82025-05-15 16:15:21 +0000866
95630366963296f62025-06-02 21:16:32 +0800867 @Override
868 public boolean ExchangeMagicToVip(String userid, int magic) {
869 if (userid == null || userid.isEmpty() || magic <= 0) {
870 logger.warn("参数无效: userid为空或magic <= 0");
871 return false;
872 }
Haotian W88568d82025-05-15 16:15:21 +0000873
95630366963296f62025-06-02 21:16:32 +0800874 EntityManager em = null;
875 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000876
95630366963296f62025-06-02 21:16:32 +0800877 try {
878 em = emf.createEntityManager();
879 tx = em.getTransaction();
880 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000881
95630366963296f62025-06-02 21:16:32 +0800882 UserPT userPT = em.find(UserPT.class, userid);
883 if (userPT == null) {
884 logger.warn("未找到用户 {} 的PT信息", userid);
885 return false;
886 }
Haotian W88568d82025-05-15 16:15:21 +0000887
95630366963296f62025-06-02 21:16:32 +0800888 if (userPT.magic < magic) {
889 logger.warn("用户 {} 的魔力值不足", userid);
890 return false;
891 }
Haotian W88568d82025-05-15 16:15:21 +0000892
95630366963296f62025-06-02 21:16:32 +0800893 // 1:1兑换VIP下载次数
894 userPT.magic -= magic;
895 userPT.viptime += magic;
Haotian W88568d82025-05-15 16:15:21 +0000896
95630366963296f62025-06-02 21:16:32 +0800897 em.merge(userPT);
898 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000899
95630366963296f62025-06-02 21:16:32 +0800900 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {} 次VIP下载次数", userid, magic, magic);
901 return true;
Haotian W88568d82025-05-15 16:15:21 +0000902
95630366963296f62025-06-02 21:16:32 +0800903 } catch (Exception e) {
904 if (tx != null && tx.isActive()) {
905 tx.rollback();
906 }
907 logger.error("魔力值兑换VIP下载次数时发生错误: {}", e.getMessage());
908 return false;
909 } finally {
910 if (em != null) {
911 em.close();
912 }
913 }
914 }
Haotian W88568d82025-05-15 16:15:21 +0000915
95630366963296f62025-06-02 21:16:32 +0800916 @Override
917 public boolean UploadTransmitProfile(Profile profile) {
918 if (profile == null || profile.profileurl == null || profile.profileurl.isEmpty() ||
919 profile.userid == null || profile.userid.isEmpty()) {
920 logger.warn("参数无效: profile为空或必要字段为空");
921 return false;
922 }
Haotian W88568d82025-05-15 16:15:21 +0000923
95630366963296f62025-06-02 21:16:32 +0800924 EntityManager em = null;
925 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000926
95630366963296f62025-06-02 21:16:32 +0800927 try {
928 em = emf.createEntityManager();
929 tx = em.getTransaction();
930 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000931
95630366963296f62025-06-02 21:16:32 +0800932 // 检查用户是否存在
933 User user = em.find(User.class, profile.userid);
934 if (user == null) {
935 logger.warn("用户 {} 不存在", profile.userid);
936 return false;
937 }
Haotian W88568d82025-05-15 16:15:21 +0000938
95630366963296f62025-06-02 21:16:32 +0800939 // 检查是否已存在相同的迁移申请
940 Profile existingProfile = em.find(Profile.class, profile.profileurl);
941 if (existingProfile != null) {
942 logger.warn("迁移申请 {} 已存在", profile.profileurl);
943 return false;
944 }
Haotian W88568d82025-05-15 16:15:21 +0000945
95630366963296f62025-06-02 21:16:32 +0800946 // 设置初始值
947 profile.exampass = false;
948 profile.magicgived = "0";
949 profile.uploadgived = "0";
Haotian W88568d82025-05-15 16:15:21 +0000950
95630366963296f62025-06-02 21:16:32 +0800951 // 保存迁移申请
952 em.persist(profile);
953 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000954
95630366963296f62025-06-02 21:16:32 +0800955 logger.info("成功上传迁移申请 {}", profile.profileurl);
956 return true;
Haotian W88568d82025-05-15 16:15:21 +0000957
95630366963296f62025-06-02 21:16:32 +0800958 } catch (Exception e) {
959 if (tx != null && tx.isActive()) {
960 tx.rollback();
961 }
962 logger.error("上传迁移申请时发生错误: {}", e.getMessage());
963 return false;
964 } finally {
965 if (em != null) {
966 em.close();
967 }
968 }
969 }
Haotian W88568d82025-05-15 16:15:21 +0000970
95630366963296f62025-06-02 21:16:32 +0800971 @Override
972 public Profile GetTransmitProfile(String profileid) {
973 if (profileid == null || profileid.isEmpty()) {
974 logger.warn("参数无效: profileid为空");
975 return null;
976 }
Haotian W88568d82025-05-15 16:15:21 +0000977
95630366963296f62025-06-02 21:16:32 +0800978 EntityManager em = null;
Haotian W88568d82025-05-15 16:15:21 +0000979
95630366963296f62025-06-02 21:16:32 +0800980 try {
981 em = emf.createEntityManager();
982 Profile profile = em.find(Profile.class, profileid);
983 if (profile == null) {
984 logger.warn("未找到迁移申请 {}", profileid);
985 return null;
986 }
Haotian W88568d82025-05-15 16:15:21 +0000987
95630366963296f62025-06-02 21:16:32 +0800988 logger.info("成功获取迁移申请 {}", profileid);
989 return profile;
Haotian W88568d82025-05-15 16:15:21 +0000990
95630366963296f62025-06-02 21:16:32 +0800991 } catch (Exception e) {
992 logger.error("获取迁移申请时发生错误: {}", e.getMessage());
993 return null;
994 } finally {
995 if (em != null) {
996 em.close();
997 }
998 }
999 }
Haotian W88568d82025-05-15 16:15:21 +00001000
95630366963296f62025-06-02 21:16:32 +08001001 @Override
1002 public boolean ExamTransmitProfile(String profileid, boolean result) {
1003 if (profileid == null || profileid.isEmpty()) {
1004 logger.warn("参数无效: profileid为空");
1005 return false;
1006 }
Haotian W88568d82025-05-15 16:15:21 +00001007
95630366963296f62025-06-02 21:16:32 +08001008 EntityManager em = null;
1009 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +00001010
95630366963296f62025-06-02 21:16:32 +08001011 try {
1012 em = emf.createEntityManager();
1013 tx = em.getTransaction();
1014 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +00001015
95630366963296f62025-06-02 21:16:32 +08001016 // 查找迁移申请
1017 Profile profile = em.find(Profile.class, profileid);
1018 if (profile == null) {
1019 logger.warn("未找到迁移申请 {}", profileid);
1020 return false;
1021 }
Haotian W88568d82025-05-15 16:15:21 +00001022
95630366963296f62025-06-02 21:16:32 +08001023 // 更新审核状态
1024 profile.exampass = result;
Haotian W88568d82025-05-15 16:15:21 +00001025
95630366963296f62025-06-02 21:16:32 +08001026 if (result) {
1027 // 如果审核通过,更新用户的PT信息
1028 UserPT userPT = em.find(UserPT.class, profile.userid);
1029 if (userPT != null) {
1030 // 发放魔力值
1031 int magicToGive = Integer.parseInt(profile.magictogive);
1032 userPT.magic += magicToGive;
1033 profile.magicgived = String.valueOf(magicToGive);
Haotian W88568d82025-05-15 16:15:21 +00001034
95630366963296f62025-06-02 21:16:32 +08001035 // 发放上传量
1036 long uploadToGive = Long.parseLong(profile.uploadtogive);
1037 userPT.upload += uploadToGive;
1038 profile.uploadgived = String.valueOf(uploadToGive);
Haotian W88568d82025-05-15 16:15:21 +00001039
95630366963296f62025-06-02 21:16:32 +08001040 em.merge(userPT);
1041 }
1042 }
Haotian W88568d82025-05-15 16:15:21 +00001043
95630366963296f62025-06-02 21:16:32 +08001044 em.merge(profile);
1045 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +00001046
95630366963296f62025-06-02 21:16:32 +08001047 logger.info("成功审核迁移申请 {}, 结果: {}", profileid, result);
1048 return true;
Haotian W88568d82025-05-15 16:15:21 +00001049
95630366963296f62025-06-02 21:16:32 +08001050 } catch (Exception e) {
1051 if (tx != null && tx.isActive()) {
1052 tx.rollback();
1053 }
1054 logger.error("审核迁移申请时发生错误: {}", e.getMessage());
1055 return false;
1056 } finally {
1057 if (em != null) {
1058 em.close();
1059 }
1060 }
1061 }
Haotian W88568d82025-05-15 16:15:21 +00001062
95630366963296f62025-06-02 21:16:32 +08001063 @Override
1064 public Profile[] GetTransmitProfileList() {
1065 EntityManager em = null;
Haotian W88568d82025-05-15 16:15:21 +00001066
95630366963296f62025-06-02 21:16:32 +08001067 try {
1068 em = emf.createEntityManager();
Haotian W88568d82025-05-15 16:15:21 +00001069
95630366963296f62025-06-02 21:16:32 +08001070 // 获取所有迁移申请
1071 QProfile p = QProfile.profile;
1072 List<Profile> profiles = new JPAQuery<>(em)
1073 .select(p)
1074 .from(p)
1075 .fetch();
Haotian W88568d82025-05-15 16:15:21 +00001076
95630366963296f62025-06-02 21:16:32 +08001077 logger.info("成功获取所有迁移申请,共 {} 条", profiles.size());
1078 return profiles.toArray(new Profile[0]);
Haotian W88568d82025-05-15 16:15:21 +00001079
95630366963296f62025-06-02 21:16:32 +08001080 } catch (Exception e) {
1081 logger.error("获取迁移申请列表时发生错误: {}", e.getMessage());
1082 return new Profile[0];
1083 } finally {
1084 if (em != null) {
1085 em.close();
1086 }
1087 }
1088 }
1089}