blob: d2d98c7b8bac50236cac94e70a3d83f2f210ceb8 [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
TRM-codingcdfe5482025-06-06 17:31:01 +080056 public String LoginUser(User userinfo){
57 return null;
58 }
59
60 @Override
95630366963296f62025-06-02 21:16:32 +080061 public int RegisterUser(User userinfo) {
62 return 0;
63 }
root33a7d952025-05-18 17:24:41 +000064
95630366963296f62025-06-02 21:16:32 +080065 @Override
66 public int UpdateInformation(User userinfo) {
67 return 0;
68 }
root33a7d952025-05-18 17:24:41 +000069
95630366963296f62025-06-02 21:16:32 +080070 @Override
71 public User GetInformation(String userid) {
72 return null;
73 }
root33a7d952025-05-18 17:24:41 +000074
95630366963296f62025-06-02 21:16:32 +080075 @Override
76 public UserPT GetInformationPT(String userid) {
77 return null;
78 }
root33a7d952025-05-18 17:24:41 +000079
95630366963296f62025-06-02 21:16:32 +080080 @Override
81 public int UpdateInformationPT(UserPT userinfo) {
82 return 0;
83 }
root33a7d952025-05-18 17:24:41 +000084
95630366963296f62025-06-02 21:16:32 +080085 @Override
86 public int RegisterUserPT(UserPT userinfo) {
87 return 0;
88 }
root33a7d952025-05-18 17:24:41 +000089
95630366963296f62025-06-02 21:16:32 +080090 @Override
91 public Seed GetSeedInformation(String seedid) {
92 return null;
93 }
root33a7d952025-05-18 17:24:41 +000094
95630366963296f62025-06-02 21:16:32 +080095 @Override
96 public int RegisterSeed(Seed seedinfo) {
97 return 0;
98 }
root33a7d952025-05-18 17:24:41 +000099
95630366963296f62025-06-02 21:16:32 +0800100 @Override
101 public int UpdateSeed(Seed seedinfo) {
102 return 0;
103 }
root33a7d952025-05-18 17:24:41 +0000104
95630366963296f62025-06-02 21:16:32 +0800105 @Override
106 public Seed[] SearchSeed(String userQ) {
107 return new Seed[0];
108 }
root33a7d952025-05-18 17:24:41 +0000109
95630366963296f62025-06-02 21:16:32 +0800110 @Override
111 public int AddNotice(Notice notice) {
112 return 0;
113 }
root33a7d952025-05-18 17:24:41 +0000114
95630366963296f62025-06-02 21:16:32 +0800115 @Override
116 public boolean UpdateNotice(Notice notice) {
117 return false;
118 }
root33a7d952025-05-18 17:24:41 +0000119
95630366963296f62025-06-02 21:16:32 +0800120 @Override
121 public boolean DeleteNotice(String noticeid) {
122 return false;
123 }
root33a7d952025-05-18 17:24:41 +0000124
95630366963296f62025-06-02 21:16:32 +0800125 @Override
126 public int GetUserAvailableInviteTimes(String userid) {
127 return 0;
128 }
root33a7d952025-05-18 17:24:41 +0000129
95630366963296f62025-06-02 21:16:32 +0800130 @Override
131 public int InviteUser(String inviterid, String inviteemail) {
132 return 0;
133 }
root33a7d952025-05-18 17:24:41 +0000134
95630366963296f62025-06-02 21:16:32 +0800135 @Override
Raveraae06122025-06-05 08:13:35 +0000136 public Seed[] GetSeedListByTag(String tag) {
137 return new Seed[0];
138 }
139
140 @Override
95630366963296f62025-06-02 21:16:32 +0800141 public boolean AddCollect(String userid, String postid) {
142 return false;
143 }
root33a7d952025-05-18 17:24:41 +0000144
95630366963296f62025-06-02 21:16:32 +0800145 @Override
146 public boolean DeleteCollect(String userid, String postid) {
147 return false;
148 }
Haotian W88568d82025-05-15 16:15:21 +0000149
95630366963296f62025-06-02 21:16:32 +0800150 @Override
151 public int AddBegSeed(BegInfo info) {
152 if (info == null || info.begid == null || info.begid.isEmpty()) {
153 logger.warn("Invalid parameter: info is null or begid is empty");
154 return 2;
155 }
Haotian W88568d82025-05-15 16:15:21 +0000156
95630366963296f62025-06-02 21:16:32 +0800157 EntityManager em = null;
158 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000159
95630366963296f62025-06-02 21:16:32 +0800160 try {
161 em = emf.createEntityManager();
162 tx = em.getTransaction();
163 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000164
95630366963296f62025-06-02 21:16:32 +0800165 // 检查是否重复
166 BegInfo existingBeg = em.find(BegInfo.class, info.begid);
167 if (existingBeg != null) {
168 logger.warn("BegSeed with ID {} already exists", info.begid);
169 return 1;
170 }
Haotian W88568d82025-05-15 16:15:21 +0000171
95630366963296f62025-06-02 21:16:32 +0800172 // 设置默认值
173 if (info.endtime == null) {
174 // 设置默认14天截止期
175 Calendar calendar = Calendar.getInstance();
176 calendar.add(Calendar.DAY_OF_MONTH, 14);
177 info.endtime = calendar.getTime();
178 }
179 info.hasseed = 0;
Haotian W88568d82025-05-15 16:15:21 +0000180
95630366963296f62025-06-02 21:16:32 +0800181 // 保存新的求种信息
182 em.persist(info);
183 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000184
95630366963296f62025-06-02 21:16:32 +0800185 logger.info("Successfully added new BegSeed with ID: {}", info.begid);
186 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000187
95630366963296f62025-06-02 21:16:32 +0800188 } catch (Exception e) {
189 if (tx != null && tx.isActive()) {
190 tx.rollback();
191 }
192 logger.error("Error adding BegSeed: {}", e.getMessage());
193 return 2;
194 } finally {
195 if (em != null) {
196 em.close();
197 }
198 }
199 }
Haotian W88568d82025-05-15 16:15:21 +0000200
95630366963296f62025-06-02 21:16:32 +0800201 @Override
202 public int UpdateBegSeed(BegInfo info) {
203 if (info == null || info.begid == null || info.begid.isEmpty()) {
204 logger.warn("Invalid parameter: info is null or begid is empty");
205 return 2;
206 }
Haotian W88568d82025-05-15 16:15:21 +0000207
95630366963296f62025-06-02 21:16:32 +0800208 EntityManager em = null;
209 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000210
95630366963296f62025-06-02 21:16:32 +0800211 try {
212 em = emf.createEntityManager();
213 tx = em.getTransaction();
214 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000215
95630366963296f62025-06-02 21:16:32 +0800216 // 检查是否存在
217 BegInfo existingBeg = em.find(BegInfo.class, info.begid);
218 if (existingBeg == null) {
219 logger.warn("BegSeed with ID {} does not exist", info.begid);
220 return 1;
221 }
Haotian W88568d82025-05-15 16:15:21 +0000222
95630366963296f62025-06-02 21:16:32 +0800223 // 保持原有值不变的字段
224 info.hasseed = existingBeg.hasseed;
225 if (info.endtime == null) {
226 info.endtime = existingBeg.endtime;
227 }
Haotian W88568d82025-05-15 16:15:21 +0000228
95630366963296f62025-06-02 21:16:32 +0800229 // 更新求种信息
230 em.merge(info);
231 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000232
95630366963296f62025-06-02 21:16:32 +0800233 logger.info("Successfully updated BegSeed with ID: {}", info.begid);
234 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000235
95630366963296f62025-06-02 21:16:32 +0800236 } catch (Exception e) {
237 if (tx != null && tx.isActive()) {
238 tx.rollback();
239 }
240 logger.error("Error updating BegSeed: {}", e.getMessage());
241 return 2;
242 } finally {
243 if (em != null) {
244 em.close();
245 }
246 }
247 }
Haotian W88568d82025-05-15 16:15:21 +0000248
95630366963296f62025-06-02 21:16:32 +0800249 @Override
250 public int DeleteBegSeed(String begid) {
251 if (begid == null || begid.isEmpty()) {
252 logger.warn("Invalid parameter: begid is null or empty");
253 return 2;
254 }
Haotian W88568d82025-05-15 16:15:21 +0000255
95630366963296f62025-06-02 21:16:32 +0800256 EntityManager em = null;
257 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000258
95630366963296f62025-06-02 21:16:32 +0800259 try {
260 em = emf.createEntityManager();
261 tx = em.getTransaction();
262 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000263
95630366963296f62025-06-02 21:16:32 +0800264 // 查找要删除的求种信息
265 BegInfo begInfo = em.find(BegInfo.class, begid);
266 if (begInfo == null) {
267 logger.warn("BegSeed with ID {} does not exist", begid);
268 tx.rollback();
269 return 1;
270 }
Haotian W88568d82025-05-15 16:15:21 +0000271
95630366963296f62025-06-02 21:16:32 +0800272 // 删除求种信息
273 em.remove(begInfo);
274 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000275
95630366963296f62025-06-02 21:16:32 +0800276 logger.info("Successfully deleted BegSeed with ID: {}", begid);
277 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000278
95630366963296f62025-06-02 21:16:32 +0800279 } catch (Exception e) {
280 if (tx != null && tx.isActive()) {
281 tx.rollback();
282 }
283 logger.error("Error deleting BegSeed: {}", e.getMessage());
284 return 2;
285 } finally {
286 if (em != null) {
287 em.close();
288 }
289 }
290 }
Haotian W88568d82025-05-15 16:15:21 +0000291
95630366963296f62025-06-02 21:16:32 +0800292 @Override
293 public int VoteSeed(String begId, String seedId, String userId) {
294 if (begId == null || seedId == null || userId == null ||
295 begId.isEmpty() || seedId.isEmpty() || userId.isEmpty()) {
296 logger.warn("Invalid parameters: begId, seedId or userId is null or empty");
297 return 2;
298 }
Haotian W88568d82025-05-15 16:15:21 +0000299
95630366963296f62025-06-02 21:16:32 +0800300 EntityManager em = null;
301 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000302
95630366963296f62025-06-02 21:16:32 +0800303 try {
304 em = emf.createEntityManager();
305 tx = em.getTransaction();
306 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000307
95630366963296f62025-06-02 21:16:32 +0800308 // 检查求种信息是否存在
309 BegInfo begInfo = em.find(BegInfo.class, begId);
310 if (begInfo == null) {
311 logger.warn("BegSeed with ID {} does not exist", begId);
312 return 2;
313 }
Haotian W88568d82025-05-15 16:15:21 +0000314
95630366963296f62025-06-02 21:16:32 +0800315 // 检查用户是否已投票
316 Long voteCount = new JPAQuery<>(em)
317 .select(QUserVotes.userVotes.count())
318 .from(QUserVotes.userVotes)
319 .where(QUserVotes.userVotes.id.eq(new entity.UserVotesId(userId, begId, seedId)))
320 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000321
95630366963296f62025-06-02 21:16:32 +0800322 if (voteCount != null && voteCount > 0) {
323 logger.warn("User {} has already voted for seed {} in beg {}", userId, seedId, begId);
324 return 1;
325 }
Haotian W88568d82025-05-15 16:15:21 +0000326
95630366963296f62025-06-02 21:16:32 +0800327 // 创建新的投票记录
328 em.createNativeQuery("INSERT INTO UserVotes (user_id, beg_id, seed_id, created_at) " +
329 "VALUES (?, ?, ?, CURRENT_TIMESTAMP)")
330 .setParameter(1, userId)
331 .setParameter(2, begId)
332 .setParameter(3, seedId)
333 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000334
95630366963296f62025-06-02 21:16:32 +0800335 // 更新SubmitSeed表中的投票数
336 em.createQuery("UPDATE SubmitSeed s SET s.votes = s.votes + 1 " +
337 "WHERE s.id.begId = :begId AND s.id.seedId = :seedId")
338 .setParameter("begId", begId)
339 .setParameter("seedId", seedId)
340 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000341
95630366963296f62025-06-02 21:16:32 +0800342 tx.commit();
343 logger.info("Successfully added vote from user {} for seed {} in beg {}", userId, seedId, begId);
344 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000345
95630366963296f62025-06-02 21:16:32 +0800346 } catch (Exception e) {
347 if (tx != null && tx.isActive()) {
348 tx.rollback();
349 }
350 logger.error("Error voting for seed: {}", e.getMessage());
351 return 2;
352 } finally {
353 if (em != null) {
354 em.close();
355 }
356 }
357 }
Haotian W88568d82025-05-15 16:15:21 +0000358
95630366963296f62025-06-02 21:16:32 +0800359 @Override
360 public int SubmitSeed(String begid, Seed seed) {
361 if (begid == null || seed == null || begid.isEmpty() || seed.seedid == null) {
362 logger.warn("Invalid parameters: begid or seed is null or empty");
363 return 2;
364 }
Haotian W88568d82025-05-15 16:15:21 +0000365
95630366963296f62025-06-02 21:16:32 +0800366 EntityManager em = null;
367 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000368
95630366963296f62025-06-02 21:16:32 +0800369 try {
370 em = emf.createEntityManager();
371 tx = em.getTransaction();
372 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000373
95630366963296f62025-06-02 21:16:32 +0800374 // 检查求种信息是否存在
375 BegInfo begInfo = em.find(BegInfo.class, begid);
376 if (begInfo == null) {
377 logger.warn("BegSeed with ID {} does not exist", begid);
378 return 2;
379 }
Haotian W88568d82025-05-15 16:15:21 +0000380
95630366963296f62025-06-02 21:16:32 +0800381 // 检查种子是否已提交过
382 QSubmitSeed ss = QSubmitSeed.submitSeed;
383 Long submitCount = new JPAQuery<>(em)
384 .select(ss.count())
385 .from(ss)
386 .where(ss.begInfo.begid.eq(begid))
387 .where(ss.seed.seedid.eq(seed.seedid))
388 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000389
95630366963296f62025-06-02 21:16:32 +0800390 if (submitCount > 0) {
391 logger.warn("Seed {} has already been submitted for beg {}", seed.seedid,
392 begid);
393 return 1;
394 }
Haotian W88568d82025-05-15 16:15:21 +0000395
95630366963296f62025-06-02 21:16:32 +0800396 // 保存种子信息(如果不存在)
397 if (em.find(Seed.class, seed.seedid) == null) {
398 em.persist(seed);
399 }
Haotian W88568d82025-05-15 16:15:21 +0000400
95630366963296f62025-06-02 21:16:32 +0800401 // 创建提交记录
402 em.createNativeQuery("INSERT INTO SubmitSeed (beg_id, seed_id, votes) VALUES (?, ?, 0)")
403 .setParameter(1, begid)
404 .setParameter(2, seed.seedid)
405 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000406
95630366963296f62025-06-02 21:16:32 +0800407 tx.commit();
408 logger.info("Successfully submitted seed {} for beg {}", seed.seedid, begid);
409 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000410
95630366963296f62025-06-02 21:16:32 +0800411 } catch (Exception e) {
412 if (tx != null && tx.isActive()) {
413 tx.rollback();
414 }
415 logger.error("Error submitting seed: {}", e.getMessage());
416 return 2;
417 } finally {
418 if (em != null) {
419 em.close();
420 }
421 }
422 }
Haotian W88568d82025-05-15 16:15:21 +0000423
95630366963296f62025-06-02 21:16:32 +0800424 @Override
425 public void SettleBeg() {
426 EntityManager em = null;
427 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000428
95630366963296f62025-06-02 21:16:32 +0800429 try {
430 em = emf.createEntityManager();
431 tx = em.getTransaction();
432 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000433
95630366963296f62025-06-02 21:16:32 +0800434 // 1. 获取所有已过期且未完成的求种信息
435 QBegInfo b = QBegInfo.begInfo;
436 List<BegInfo> expiredBegs = new JPAQuery<>(em)
437 .select(b)
438 .from(b)
439 .where(b.endtime.loe(new Date())
440 .and(b.hasseed.eq(0)))
441 .fetch();
Haotian W88568d82025-05-15 16:15:21 +0000442
95630366963296f62025-06-02 21:16:32 +0800443 for (BegInfo beg : expiredBegs) {
444 // 2. 查找投票最多的提交任务
445 QSubmitSeed ss = QSubmitSeed.submitSeed;
446 Tuple topSubmission = new JPAQuery<>(em)
447 .select(ss.seed.seedid, ss.votes)
448 .from(ss)
449 .where(ss.begInfo.begid.eq(beg.begid))
450 .orderBy(ss.votes.desc())
451 .limit(1)
452 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000453
95630366963296f62025-06-02 21:16:32 +0800454 if (topSubmission != null && topSubmission.get(ss.votes) > 0) {
455 String seedId = topSubmission.get(ss.seed.seedid);
Haotian W88568d82025-05-15 16:15:21 +0000456
95630366963296f62025-06-02 21:16:32 +0800457 // 3. 获取上传者ID
458 QSeed s = QSeed.seed;
459 String ownerId = new JPAQuery<>(em)
460 .select(s.seeduserid)
461 .from(s)
462 .where(s.seedid.eq(seedId))
463 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000464
95630366963296f62025-06-02 21:16:32 +0800465 // 4. 获取上传者的PT信息并更新魔力值
466 UserPT ownerPT = em.find(UserPT.class, ownerId);
467 if (ownerPT != null) {
468 // 5. 发放奖励
469 ownerPT.magic += beg.magic;
470 em.merge(ownerPT);
Haotian W88568d82025-05-15 16:15:21 +0000471
95630366963296f62025-06-02 21:16:32 +0800472 // 6. 更新求种状态
473 beg.hasseed = 1;
474 em.merge(beg);
Haotian W88568d82025-05-15 16:15:21 +0000475
95630366963296f62025-06-02 21:16:32 +0800476 logger.info("Reward {} magic points awarded to user {} for beg {}",
477 beg.magic, ownerId, beg.begid);
478 }
479 }
480 }
Haotian W88568d82025-05-15 16:15:21 +0000481
95630366963296f62025-06-02 21:16:32 +0800482 tx.commit();
483 logger.info("Successfully settled {} expired beg requests",
484 expiredBegs.size());
Haotian W88568d82025-05-15 16:15:21 +0000485
95630366963296f62025-06-02 21:16:32 +0800486 } catch (Exception e) {
487 if (tx != null && tx.isActive()) {
488 tx.rollback();
489 }
490 logger.error("Error settling beg requests: {}", e.getMessage(), e);
491 } finally {
492 if (em != null) {
493 em.close();
494 }
495 }
496 }
Haotian W88568d82025-05-15 16:15:21 +0000497
95630366963296f62025-06-02 21:16:32 +0800498 @Override
499 public int AddPost(Post post) {
500 if (post == null || post.postid == null || post.postid.isEmpty()) {
501 logger.warn("Invalid parameter: post is null or postid is empty");
502 return 2;
503 }
Haotian W88568d82025-05-15 16:15:21 +0000504
95630366963296f62025-06-02 21:16:32 +0800505 EntityManager em = null;
506 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000507
95630366963296f62025-06-02 21:16:32 +0800508 try {
509 em = emf.createEntityManager();
510 tx = em.getTransaction();
511 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000512
95630366963296f62025-06-02 21:16:32 +0800513 // 检查是否重复
514 Post existingPost = em.find(Post.class, post.postid);
515 if (existingPost != null) {
516 logger.warn("Post with ID {} already exists", post.postid);
517 return 1;
518 }
Haotian W88568d82025-05-15 16:15:21 +0000519
95630366963296f62025-06-02 21:16:32 +0800520 // 检查用户是否存在
521 User user = em.find(User.class, post.postuserid);
522 if (user == null) {
523 logger.warn("User with ID {} does not exist", post.postuserid);
524 return 2;
525 }
Haotian W88568d82025-05-15 16:15:21 +0000526
95630366963296f62025-06-02 21:16:32 +0800527 // 设置初始值
528 if (post.posttime == null) {
529 post.posttime = new Date();
530 }
531 post.replytime = 0;
532 post.readtime = 0;
Haotian W88568d82025-05-15 16:15:21 +0000533
95630366963296f62025-06-02 21:16:32 +0800534 // 保存帖子
535 em.persist(post);
536 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000537
95630366963296f62025-06-02 21:16:32 +0800538 logger.info("Successfully added new post with ID: {}", post.postid);
539 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000540
95630366963296f62025-06-02 21:16:32 +0800541 } catch (Exception e) {
542 if (tx != null && tx.isActive()) {
543 tx.rollback();
544 }
545 logger.error("Error adding post: {}", e.getMessage());
546 return 2;
547 } finally {
548 if (em != null) {
549 em.close();
550 }
551 }
552 }
Haotian W88568d82025-05-15 16:15:21 +0000553
95630366963296f62025-06-02 21:16:32 +0800554 @Override
555 public int UpdatePost(Post post) {
556 if (post == null || post.postid == null || post.postid.isEmpty()) {
557 logger.warn("Invalid parameter: post is null or postid is empty");
558 return 2;
559 }
Haotian W88568d82025-05-15 16:15:21 +0000560
95630366963296f62025-06-02 21:16:32 +0800561 EntityManager em = null;
562 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000563
95630366963296f62025-06-02 21:16:32 +0800564 try {
565 em = emf.createEntityManager();
566 tx = em.getTransaction();
567 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000568
95630366963296f62025-06-02 21:16:32 +0800569 // 检查帖子是否存在
570 Post existingPost = em.find(Post.class, post.postid);
571 if (existingPost == null) {
572 logger.warn("Post with ID {} does not exist", post.postid);
573 return 1;
574 }
Haotian W88568d82025-05-15 16:15:21 +0000575
95630366963296f62025-06-02 21:16:32 +0800576 // 保持原有不可修改的字段
577 post.postuserid = existingPost.postuserid;
578 post.posttime = existingPost.posttime;
579 post.replytime = existingPost.replytime;
580 post.readtime = existingPost.readtime;
Haotian W88568d82025-05-15 16:15:21 +0000581
95630366963296f62025-06-02 21:16:32 +0800582 // 更新帖子
583 em.merge(post);
584 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000585
95630366963296f62025-06-02 21:16:32 +0800586 logger.info("Successfully updated post with ID: {}", post.postid);
587 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000588
95630366963296f62025-06-02 21:16:32 +0800589 } catch (Exception e) {
590 if (tx != null && tx.isActive()) {
591 tx.rollback();
592 }
593 logger.error("Error updating post: {}", e.getMessage());
594 return 2;
595 } finally {
596 if (em != null) {
597 em.close();
598 }
599 }
600 }
Haotian W88568d82025-05-15 16:15:21 +0000601
95630366963296f62025-06-02 21:16:32 +0800602 @Override
603 public int DeletePost(String postid) {
604 if (postid == null || postid.isEmpty()) {
605 logger.warn("Invalid parameter: postid is null or empty");
606 return 2;
607 }
Haotian W88568d82025-05-15 16:15:21 +0000608
95630366963296f62025-06-02 21:16:32 +0800609 EntityManager em = null;
610 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000611
95630366963296f62025-06-02 21:16:32 +0800612 try {
613 em = emf.createEntityManager();
614 tx = em.getTransaction();
615 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000616
95630366963296f62025-06-02 21:16:32 +0800617 // 查找要删除的帖子
618 Post post = em.find(Post.class, postid);
619 if (post == null) {
620 logger.warn("Post with ID {} does not exist", postid);
621 tx.rollback();
622 return 1;
623 }
Haotian W88568d82025-05-15 16:15:21 +0000624
95630366963296f62025-06-02 21:16:32 +0800625 // 删除帖子(由于设置了级联删除,相关的回复会自动删除)
626 em.remove(post);
627 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000628
95630366963296f62025-06-02 21:16:32 +0800629 logger.info("Successfully deleted post with ID: {}", postid);
630 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000631
95630366963296f62025-06-02 21:16:32 +0800632 } catch (Exception e) {
633 if (tx != null && tx.isActive()) {
634 tx.rollback();
635 }
636 logger.error("Error deleting post: {}", e.getMessage());
637 return 2;
638 } finally {
639 if (em != null) {
640 em.close();
641 }
642 }
643 }
Haotian W88568d82025-05-15 16:15:21 +0000644
95630366963296f62025-06-02 21:16:32 +0800645 @Override
646 public int AddComment(String postid, String userid, String comment) {
647 if (postid == null || postid.isEmpty() ||
648 userid == null || userid.isEmpty() ||
649 comment == null || comment.isEmpty()) {
650 logger.warn("Invalid parameters: postid, userid or comment is null or empty");
651 return 2;
652 }
Haotian W88568d82025-05-15 16:15:21 +0000653
95630366963296f62025-06-02 21:16:32 +0800654 EntityManager em = null;
655 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000656
95630366963296f62025-06-02 21:16:32 +0800657 try {
658 em = emf.createEntityManager();
659 tx = em.getTransaction();
660 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000661
95630366963296f62025-06-02 21:16:32 +0800662 // 检查帖子是否存在
663 Post post = em.find(Post.class, postid);
664 if (post == null) {
665 logger.warn("Post with ID {} does not exist", postid);
666 return 1;
667 }
Haotian W88568d82025-05-15 16:15:21 +0000668
95630366963296f62025-06-02 21:16:32 +0800669 // 检查评论用户是否存在
670 User user = em.find(User.class, userid);
671 if (user == null) {
672 logger.warn("User with ID {} does not exist", userid);
673 return 1;
674 }
Haotian W88568d82025-05-15 16:15:21 +0000675
95630366963296f62025-06-02 21:16:32 +0800676 // 创建新的回复
677 PostReply reply = new PostReply();
678 reply.replyid = UUID.randomUUID().toString();
679 reply.postid = postid;
680 reply.content = comment;
681 reply.authorid = userid;
682 reply.createdAt = new Date();
Haotian W88568d82025-05-15 16:15:21 +0000683
95630366963296f62025-06-02 21:16:32 +0800684 // 保存回复
685 em.persist(reply);
Haotian W88568d82025-05-15 16:15:21 +0000686
95630366963296f62025-06-02 21:16:32 +0800687 // 更新帖子的回复数
688 post.replytime += 1;
689 em.merge(post);
Haotian W88568d82025-05-15 16:15:21 +0000690
95630366963296f62025-06-02 21:16:32 +0800691 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000692
95630366963296f62025-06-02 21:16:32 +0800693 logger.info("Successfully added comment by user {} to post {}, reply ID: {}",
694 userid, postid, reply.replyid);
695 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000696
95630366963296f62025-06-02 21:16:32 +0800697 } catch (Exception e) {
698 if (tx != null && tx.isActive()) {
699 tx.rollback();
700 }
701 logger.error("Error adding comment: {}", e.getMessage());
702 return 2;
703 } finally {
704 if (em != null) {
705 em.close();
706 }
707 }
708 }
Haotian W88568d82025-05-15 16:15:21 +0000709
95630366963296f62025-06-02 21:16:32 +0800710 @Override
711 public int DeleteComment(String postid, String commentid) {
712 if (postid == null || postid.isEmpty() || commentid == null || commentid.isEmpty()) {
713 logger.warn("Invalid parameters: postid or commentid is null or empty");
714 return 2;
715 }
Haotian W88568d82025-05-15 16:15:21 +0000716
95630366963296f62025-06-02 21:16:32 +0800717 EntityManager em = null;
718 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000719
95630366963296f62025-06-02 21:16:32 +0800720 try {
721 em = emf.createEntityManager();
722 tx = em.getTransaction();
723 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000724
95630366963296f62025-06-02 21:16:32 +0800725 // 检查帖子是否存在
726 Post post = em.find(Post.class, postid);
727 if (post == null) {
728 logger.warn("Post with ID {} does not exist", postid);
729 return 1;
730 }
Haotian W88568d82025-05-15 16:15:21 +0000731
95630366963296f62025-06-02 21:16:32 +0800732 // 检查评论是否存在且属于该帖子
733 PostReply reply = em.find(PostReply.class, commentid);
734 if (reply == null || !reply.postid.equals(postid)) {
735 logger.warn("Comment {} does not exist or does not belong to post {}", commentid, postid);
736 return 1;
737 }
Haotian W88568d82025-05-15 16:15:21 +0000738
95630366963296f62025-06-02 21:16:32 +0800739 // 删除评论
740 em.remove(reply);
Haotian W88568d82025-05-15 16:15:21 +0000741
95630366963296f62025-06-02 21:16:32 +0800742 // 更新帖子的回复数
743 post.replytime = Math.max(0, post.replytime - 1);
744 em.merge(post);
Haotian W88568d82025-05-15 16:15:21 +0000745
95630366963296f62025-06-02 21:16:32 +0800746 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000747
95630366963296f62025-06-02 21:16:32 +0800748 logger.info("Successfully deleted comment {} from post {}", commentid, postid);
749 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000750
95630366963296f62025-06-02 21:16:32 +0800751 } catch (Exception e) {
752 if (tx != null && tx.isActive()) {
753 tx.rollback();
754 }
755 logger.error("Error deleting comment: {}", e.getMessage());
756 return 2;
757 } finally {
758 if (em != null) {
759 em.close();
760 }
761 }
762 }
Haotian W88568d82025-05-15 16:15:21 +0000763
95630366963296f62025-06-02 21:16:32 +0800764 @Override
765 public boolean ExchangeMagicToUpload(String userid, int magic) {
766 if (userid == null || userid.isEmpty() || magic <= 0) {
767 logger.warn("参数无效: userid为空或magic <= 0");
768 return false;
769 }
Haotian W88568d82025-05-15 16:15:21 +0000770
95630366963296f62025-06-02 21:16:32 +0800771 EntityManager em = null;
772 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000773
95630366963296f62025-06-02 21:16:32 +0800774 try {
775 em = emf.createEntityManager();
776 tx = em.getTransaction();
777 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000778
95630366963296f62025-06-02 21:16:32 +0800779 UserPT userPT = em.find(UserPT.class, userid);
780 if (userPT == null) {
781 logger.warn("未找到用户 {} 的PT信息", userid);
782 return false;
783 }
Haotian W88568d82025-05-15 16:15:21 +0000784
95630366963296f62025-06-02 21:16:32 +0800785 if (userPT.magic < magic) {
786 logger.warn("用户 {} 的魔力值不足", userid);
787 return false;
788 }
Haotian W88568d82025-05-15 16:15:21 +0000789
95630366963296f62025-06-02 21:16:32 +0800790 // 1:1兑换,直接加上魔力值(假设单位是MB)
791 userPT.magic -= magic;
792 userPT.upload += magic;
Haotian W88568d82025-05-15 16:15:21 +0000793
95630366963296f62025-06-02 21:16:32 +0800794 // 更新分享率
795 if (userPT.download > 0) {
796 userPT.share = (double) userPT.upload / userPT.download;
797 }
Haotian W88568d82025-05-15 16:15:21 +0000798
95630366963296f62025-06-02 21:16:32 +0800799 em.merge(userPT);
800 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000801
95630366963296f62025-06-02 21:16:32 +0800802 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 上传量", userid, magic, magic);
803 return true;
Haotian W88568d82025-05-15 16:15:21 +0000804
95630366963296f62025-06-02 21:16:32 +0800805 } catch (Exception e) {
806 if (tx != null && tx.isActive()) {
807 tx.rollback();
808 }
809 logger.error("魔力值兑换上传量时发生错误: {}", e.getMessage());
810 return false;
811 } finally {
812 if (em != null) {
813 em.close();
814 }
815 }
816 }
Haotian W88568d82025-05-15 16:15:21 +0000817
95630366963296f62025-06-02 21:16:32 +0800818 @Override
819 public boolean ExchangeMagicToDownload(String userid, int magic) {
820 if (userid == null || userid.isEmpty() || magic <= 0) {
821 logger.warn("参数无效: userid为空或magic <= 0");
822 return false;
823 }
Haotian W88568d82025-05-15 16:15:21 +0000824
95630366963296f62025-06-02 21:16:32 +0800825 EntityManager em = null;
826 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000827
95630366963296f62025-06-02 21:16:32 +0800828 try {
829 em = emf.createEntityManager();
830 tx = em.getTransaction();
831 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000832
95630366963296f62025-06-02 21:16:32 +0800833 UserPT userPT = em.find(UserPT.class, userid);
834 if (userPT == null) {
835 logger.warn("未找到用户 {} 的PT信息", userid);
836 return false;
837 }
Haotian W88568d82025-05-15 16:15:21 +0000838
95630366963296f62025-06-02 21:16:32 +0800839 if (userPT.magic < magic) {
840 logger.warn("用户 {} 的魔力值不足", userid);
841 return false;
842 }
Haotian W88568d82025-05-15 16:15:21 +0000843
95630366963296f62025-06-02 21:16:32 +0800844 // 1:1兑换,直接减去魔力值对应的下载量(假设单位是MB)
845 userPT.magic -= magic;
846 userPT.download = Math.max(0, userPT.download - magic);
Haotian W88568d82025-05-15 16:15:21 +0000847
95630366963296f62025-06-02 21:16:32 +0800848 // 更新分享率
849 if (userPT.download > 0) {
850 userPT.share = (double) userPT.upload / userPT.download;
851 }
Haotian W88568d82025-05-15 16:15:21 +0000852
95630366963296f62025-06-02 21:16:32 +0800853 em.merge(userPT);
854 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000855
95630366963296f62025-06-02 21:16:32 +0800856 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 下载量减免", userid, magic, magic);
857 return true;
Haotian W88568d82025-05-15 16:15:21 +0000858
95630366963296f62025-06-02 21:16:32 +0800859 } catch (Exception e) {
860 if (tx != null && tx.isActive()) {
861 tx.rollback();
862 }
863 logger.error("魔力值兑换下载量时发生错误: {}", e.getMessage());
864 return false;
865 } finally {
866 if (em != null) {
867 em.close();
868 }
869 }
870 }
Haotian W88568d82025-05-15 16:15:21 +0000871
95630366963296f62025-06-02 21:16:32 +0800872 @Override
873 public boolean ExchangeMagicToVip(String userid, int magic) {
874 if (userid == null || userid.isEmpty() || magic <= 0) {
875 logger.warn("参数无效: userid为空或magic <= 0");
876 return false;
877 }
Haotian W88568d82025-05-15 16:15:21 +0000878
95630366963296f62025-06-02 21:16:32 +0800879 EntityManager em = null;
880 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000881
95630366963296f62025-06-02 21:16:32 +0800882 try {
883 em = emf.createEntityManager();
884 tx = em.getTransaction();
885 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000886
95630366963296f62025-06-02 21:16:32 +0800887 UserPT userPT = em.find(UserPT.class, userid);
888 if (userPT == null) {
889 logger.warn("未找到用户 {} 的PT信息", userid);
890 return false;
891 }
Haotian W88568d82025-05-15 16:15:21 +0000892
95630366963296f62025-06-02 21:16:32 +0800893 if (userPT.magic < magic) {
894 logger.warn("用户 {} 的魔力值不足", userid);
895 return false;
896 }
Haotian W88568d82025-05-15 16:15:21 +0000897
95630366963296f62025-06-02 21:16:32 +0800898 // 1:1兑换VIP下载次数
899 userPT.magic -= magic;
900 userPT.viptime += magic;
Haotian W88568d82025-05-15 16:15:21 +0000901
95630366963296f62025-06-02 21:16:32 +0800902 em.merge(userPT);
903 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000904
95630366963296f62025-06-02 21:16:32 +0800905 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {} 次VIP下载次数", userid, magic, magic);
906 return true;
Haotian W88568d82025-05-15 16:15:21 +0000907
95630366963296f62025-06-02 21:16:32 +0800908 } catch (Exception e) {
909 if (tx != null && tx.isActive()) {
910 tx.rollback();
911 }
912 logger.error("魔力值兑换VIP下载次数时发生错误: {}", e.getMessage());
913 return false;
914 } finally {
915 if (em != null) {
916 em.close();
917 }
918 }
919 }
Haotian W88568d82025-05-15 16:15:21 +0000920
95630366963296f62025-06-02 21:16:32 +0800921 @Override
922 public boolean UploadTransmitProfile(Profile profile) {
923 if (profile == null || profile.profileurl == null || profile.profileurl.isEmpty() ||
924 profile.userid == null || profile.userid.isEmpty()) {
925 logger.warn("参数无效: profile为空或必要字段为空");
926 return false;
927 }
Haotian W88568d82025-05-15 16:15:21 +0000928
95630366963296f62025-06-02 21:16:32 +0800929 EntityManager em = null;
930 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000931
95630366963296f62025-06-02 21:16:32 +0800932 try {
933 em = emf.createEntityManager();
934 tx = em.getTransaction();
935 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000936
95630366963296f62025-06-02 21:16:32 +0800937 // 检查用户是否存在
938 User user = em.find(User.class, profile.userid);
939 if (user == null) {
940 logger.warn("用户 {} 不存在", profile.userid);
941 return false;
942 }
Haotian W88568d82025-05-15 16:15:21 +0000943
95630366963296f62025-06-02 21:16:32 +0800944 // 检查是否已存在相同的迁移申请
945 Profile existingProfile = em.find(Profile.class, profile.profileurl);
946 if (existingProfile != null) {
947 logger.warn("迁移申请 {} 已存在", profile.profileurl);
948 return false;
949 }
Haotian W88568d82025-05-15 16:15:21 +0000950
95630366963296f62025-06-02 21:16:32 +0800951 // 设置初始值
952 profile.exampass = false;
953 profile.magicgived = "0";
954 profile.uploadgived = "0";
Haotian W88568d82025-05-15 16:15:21 +0000955
95630366963296f62025-06-02 21:16:32 +0800956 // 保存迁移申请
957 em.persist(profile);
958 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000959
95630366963296f62025-06-02 21:16:32 +0800960 logger.info("成功上传迁移申请 {}", profile.profileurl);
961 return true;
Haotian W88568d82025-05-15 16:15:21 +0000962
95630366963296f62025-06-02 21:16:32 +0800963 } catch (Exception e) {
964 if (tx != null && tx.isActive()) {
965 tx.rollback();
966 }
967 logger.error("上传迁移申请时发生错误: {}", e.getMessage());
968 return false;
969 } finally {
970 if (em != null) {
971 em.close();
972 }
973 }
974 }
Haotian W88568d82025-05-15 16:15:21 +0000975
95630366963296f62025-06-02 21:16:32 +0800976 @Override
977 public Profile GetTransmitProfile(String profileid) {
978 if (profileid == null || profileid.isEmpty()) {
979 logger.warn("参数无效: profileid为空");
980 return null;
981 }
Haotian W88568d82025-05-15 16:15:21 +0000982
95630366963296f62025-06-02 21:16:32 +0800983 EntityManager em = null;
Haotian W88568d82025-05-15 16:15:21 +0000984
95630366963296f62025-06-02 21:16:32 +0800985 try {
986 em = emf.createEntityManager();
987 Profile profile = em.find(Profile.class, profileid);
988 if (profile == null) {
989 logger.warn("未找到迁移申请 {}", profileid);
990 return null;
991 }
Haotian W88568d82025-05-15 16:15:21 +0000992
95630366963296f62025-06-02 21:16:32 +0800993 logger.info("成功获取迁移申请 {}", profileid);
994 return profile;
Haotian W88568d82025-05-15 16:15:21 +0000995
95630366963296f62025-06-02 21:16:32 +0800996 } catch (Exception e) {
997 logger.error("获取迁移申请时发生错误: {}", e.getMessage());
998 return null;
999 } finally {
1000 if (em != null) {
1001 em.close();
1002 }
1003 }
1004 }
Haotian W88568d82025-05-15 16:15:21 +00001005
95630366963296f62025-06-02 21:16:32 +08001006 @Override
1007 public boolean ExamTransmitProfile(String profileid, boolean result) {
1008 if (profileid == null || profileid.isEmpty()) {
1009 logger.warn("参数无效: profileid为空");
1010 return false;
1011 }
Haotian W88568d82025-05-15 16:15:21 +00001012
95630366963296f62025-06-02 21:16:32 +08001013 EntityManager em = null;
1014 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +00001015
95630366963296f62025-06-02 21:16:32 +08001016 try {
1017 em = emf.createEntityManager();
1018 tx = em.getTransaction();
1019 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +00001020
95630366963296f62025-06-02 21:16:32 +08001021 // 查找迁移申请
1022 Profile profile = em.find(Profile.class, profileid);
1023 if (profile == null) {
1024 logger.warn("未找到迁移申请 {}", profileid);
1025 return false;
1026 }
Haotian W88568d82025-05-15 16:15:21 +00001027
95630366963296f62025-06-02 21:16:32 +08001028 // 更新审核状态
1029 profile.exampass = result;
Haotian W88568d82025-05-15 16:15:21 +00001030
95630366963296f62025-06-02 21:16:32 +08001031 if (result) {
1032 // 如果审核通过,更新用户的PT信息
1033 UserPT userPT = em.find(UserPT.class, profile.userid);
1034 if (userPT != null) {
1035 // 发放魔力值
1036 int magicToGive = Integer.parseInt(profile.magictogive);
1037 userPT.magic += magicToGive;
1038 profile.magicgived = String.valueOf(magicToGive);
Haotian W88568d82025-05-15 16:15:21 +00001039
95630366963296f62025-06-02 21:16:32 +08001040 // 发放上传量
1041 long uploadToGive = Long.parseLong(profile.uploadtogive);
1042 userPT.upload += uploadToGive;
1043 profile.uploadgived = String.valueOf(uploadToGive);
Haotian W88568d82025-05-15 16:15:21 +00001044
95630366963296f62025-06-02 21:16:32 +08001045 em.merge(userPT);
1046 }
1047 }
Haotian W88568d82025-05-15 16:15:21 +00001048
95630366963296f62025-06-02 21:16:32 +08001049 em.merge(profile);
1050 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +00001051
95630366963296f62025-06-02 21:16:32 +08001052 logger.info("成功审核迁移申请 {}, 结果: {}", profileid, result);
1053 return true;
Haotian W88568d82025-05-15 16:15:21 +00001054
95630366963296f62025-06-02 21:16:32 +08001055 } catch (Exception e) {
1056 if (tx != null && tx.isActive()) {
1057 tx.rollback();
1058 }
1059 logger.error("审核迁移申请时发生错误: {}", e.getMessage());
1060 return false;
1061 } finally {
1062 if (em != null) {
1063 em.close();
1064 }
1065 }
1066 }
Haotian W88568d82025-05-15 16:15:21 +00001067
95630366963296f62025-06-02 21:16:32 +08001068 @Override
1069 public Profile[] GetTransmitProfileList() {
1070 EntityManager em = null;
Haotian W88568d82025-05-15 16:15:21 +00001071
95630366963296f62025-06-02 21:16:32 +08001072 try {
1073 em = emf.createEntityManager();
Haotian W88568d82025-05-15 16:15:21 +00001074
95630366963296f62025-06-02 21:16:32 +08001075 // 获取所有迁移申请
1076 QProfile p = QProfile.profile;
1077 List<Profile> profiles = new JPAQuery<>(em)
1078 .select(p)
1079 .from(p)
1080 .fetch();
Haotian W88568d82025-05-15 16:15:21 +00001081
95630366963296f62025-06-02 21:16:32 +08001082 logger.info("成功获取所有迁移申请,共 {} 条", profiles.size());
1083 return profiles.toArray(new Profile[0]);
Haotian W88568d82025-05-15 16:15:21 +00001084
95630366963296f62025-06-02 21:16:32 +08001085 } catch (Exception e) {
1086 logger.error("获取迁移申请列表时发生错误: {}", e.getMessage());
1087 return new Profile[0];
1088 } finally {
1089 if (em != null) {
1090 em.close();
1091 }
1092 }
1093 }
1094}