blob: 7a5895361f435a69354261ac05f56185db5dcff3 [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
rhjc6a4ee02025-06-06 00:45:18 +0800141 public Seed[] GetSeedListByUser(String userid) {
142 return new Seed[0];
143 }
144
145 @Override
146 public int DeleteSeed(String seedid) {
147 return 0;
148 }
149
150 @Override
95630366963296f62025-06-02 21:16:32 +0800151 public boolean AddCollect(String userid, String postid) {
152 return false;
153 }
root33a7d952025-05-18 17:24:41 +0000154
95630366963296f62025-06-02 21:16:32 +0800155 @Override
156 public boolean DeleteCollect(String userid, String postid) {
157 return false;
158 }
Haotian W88568d82025-05-15 16:15:21 +0000159
95630366963296f62025-06-02 21:16:32 +0800160 @Override
161 public int AddBegSeed(BegInfo info) {
162 if (info == null || info.begid == null || info.begid.isEmpty()) {
163 logger.warn("Invalid parameter: info is null or begid is empty");
164 return 2;
165 }
Haotian W88568d82025-05-15 16:15:21 +0000166
95630366963296f62025-06-02 21:16:32 +0800167 EntityManager em = null;
168 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000169
95630366963296f62025-06-02 21:16:32 +0800170 try {
171 em = emf.createEntityManager();
172 tx = em.getTransaction();
173 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000174
95630366963296f62025-06-02 21:16:32 +0800175 // 检查是否重复
176 BegInfo existingBeg = em.find(BegInfo.class, info.begid);
177 if (existingBeg != null) {
178 logger.warn("BegSeed with ID {} already exists", info.begid);
179 return 1;
180 }
Haotian W88568d82025-05-15 16:15:21 +0000181
95630366963296f62025-06-02 21:16:32 +0800182 // 设置默认值
183 if (info.endtime == null) {
184 // 设置默认14天截止期
185 Calendar calendar = Calendar.getInstance();
186 calendar.add(Calendar.DAY_OF_MONTH, 14);
187 info.endtime = calendar.getTime();
188 }
189 info.hasseed = 0;
Haotian W88568d82025-05-15 16:15:21 +0000190
95630366963296f62025-06-02 21:16:32 +0800191 // 保存新的求种信息
192 em.persist(info);
193 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000194
95630366963296f62025-06-02 21:16:32 +0800195 logger.info("Successfully added new BegSeed with ID: {}", info.begid);
196 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000197
95630366963296f62025-06-02 21:16:32 +0800198 } catch (Exception e) {
199 if (tx != null && tx.isActive()) {
200 tx.rollback();
201 }
202 logger.error("Error adding BegSeed: {}", e.getMessage());
203 return 2;
204 } finally {
205 if (em != null) {
206 em.close();
207 }
208 }
209 }
Haotian W88568d82025-05-15 16:15:21 +0000210
95630366963296f62025-06-02 21:16:32 +0800211 @Override
212 public int UpdateBegSeed(BegInfo info) {
213 if (info == null || info.begid == null || info.begid.isEmpty()) {
214 logger.warn("Invalid parameter: info is null or begid is empty");
215 return 2;
216 }
Haotian W88568d82025-05-15 16:15:21 +0000217
95630366963296f62025-06-02 21:16:32 +0800218 EntityManager em = null;
219 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000220
95630366963296f62025-06-02 21:16:32 +0800221 try {
222 em = emf.createEntityManager();
223 tx = em.getTransaction();
224 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000225
95630366963296f62025-06-02 21:16:32 +0800226 // 检查是否存在
227 BegInfo existingBeg = em.find(BegInfo.class, info.begid);
228 if (existingBeg == null) {
229 logger.warn("BegSeed with ID {} does not exist", info.begid);
230 return 1;
231 }
Haotian W88568d82025-05-15 16:15:21 +0000232
95630366963296f62025-06-02 21:16:32 +0800233 // 保持原有值不变的字段
234 info.hasseed = existingBeg.hasseed;
235 if (info.endtime == null) {
236 info.endtime = existingBeg.endtime;
237 }
Haotian W88568d82025-05-15 16:15:21 +0000238
95630366963296f62025-06-02 21:16:32 +0800239 // 更新求种信息
240 em.merge(info);
241 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000242
95630366963296f62025-06-02 21:16:32 +0800243 logger.info("Successfully updated BegSeed with ID: {}", info.begid);
244 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000245
95630366963296f62025-06-02 21:16:32 +0800246 } catch (Exception e) {
247 if (tx != null && tx.isActive()) {
248 tx.rollback();
249 }
250 logger.error("Error updating BegSeed: {}", e.getMessage());
251 return 2;
252 } finally {
253 if (em != null) {
254 em.close();
255 }
256 }
257 }
Haotian W88568d82025-05-15 16:15:21 +0000258
95630366963296f62025-06-02 21:16:32 +0800259 @Override
260 public int DeleteBegSeed(String begid) {
261 if (begid == null || begid.isEmpty()) {
262 logger.warn("Invalid parameter: begid is null or empty");
263 return 2;
264 }
Haotian W88568d82025-05-15 16:15:21 +0000265
95630366963296f62025-06-02 21:16:32 +0800266 EntityManager em = null;
267 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000268
95630366963296f62025-06-02 21:16:32 +0800269 try {
270 em = emf.createEntityManager();
271 tx = em.getTransaction();
272 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000273
95630366963296f62025-06-02 21:16:32 +0800274 // 查找要删除的求种信息
275 BegInfo begInfo = em.find(BegInfo.class, begid);
276 if (begInfo == null) {
277 logger.warn("BegSeed with ID {} does not exist", begid);
278 tx.rollback();
279 return 1;
280 }
Haotian W88568d82025-05-15 16:15:21 +0000281
95630366963296f62025-06-02 21:16:32 +0800282 // 删除求种信息
283 em.remove(begInfo);
284 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000285
95630366963296f62025-06-02 21:16:32 +0800286 logger.info("Successfully deleted BegSeed with ID: {}", begid);
287 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000288
95630366963296f62025-06-02 21:16:32 +0800289 } catch (Exception e) {
290 if (tx != null && tx.isActive()) {
291 tx.rollback();
292 }
293 logger.error("Error deleting BegSeed: {}", e.getMessage());
294 return 2;
295 } finally {
296 if (em != null) {
297 em.close();
298 }
299 }
300 }
Haotian W88568d82025-05-15 16:15:21 +0000301
95630366963296f62025-06-02 21:16:32 +0800302 @Override
303 public int VoteSeed(String begId, String seedId, String userId) {
304 if (begId == null || seedId == null || userId == null ||
305 begId.isEmpty() || seedId.isEmpty() || userId.isEmpty()) {
306 logger.warn("Invalid parameters: begId, seedId or userId is null or empty");
307 return 2;
308 }
Haotian W88568d82025-05-15 16:15:21 +0000309
95630366963296f62025-06-02 21:16:32 +0800310 EntityManager em = null;
311 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000312
95630366963296f62025-06-02 21:16:32 +0800313 try {
314 em = emf.createEntityManager();
315 tx = em.getTransaction();
316 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000317
95630366963296f62025-06-02 21:16:32 +0800318 // 检查求种信息是否存在
319 BegInfo begInfo = em.find(BegInfo.class, begId);
320 if (begInfo == null) {
321 logger.warn("BegSeed with ID {} does not exist", begId);
322 return 2;
323 }
Haotian W88568d82025-05-15 16:15:21 +0000324
95630366963296f62025-06-02 21:16:32 +0800325 // 检查用户是否已投票
326 Long voteCount = new JPAQuery<>(em)
327 .select(QUserVotes.userVotes.count())
328 .from(QUserVotes.userVotes)
329 .where(QUserVotes.userVotes.id.eq(new entity.UserVotesId(userId, begId, seedId)))
330 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000331
95630366963296f62025-06-02 21:16:32 +0800332 if (voteCount != null && voteCount > 0) {
333 logger.warn("User {} has already voted for seed {} in beg {}", userId, seedId, begId);
334 return 1;
335 }
Haotian W88568d82025-05-15 16:15:21 +0000336
95630366963296f62025-06-02 21:16:32 +0800337 // 创建新的投票记录
338 em.createNativeQuery("INSERT INTO UserVotes (user_id, beg_id, seed_id, created_at) " +
339 "VALUES (?, ?, ?, CURRENT_TIMESTAMP)")
340 .setParameter(1, userId)
341 .setParameter(2, begId)
342 .setParameter(3, seedId)
343 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000344
95630366963296f62025-06-02 21:16:32 +0800345 // 更新SubmitSeed表中的投票数
346 em.createQuery("UPDATE SubmitSeed s SET s.votes = s.votes + 1 " +
347 "WHERE s.id.begId = :begId AND s.id.seedId = :seedId")
348 .setParameter("begId", begId)
349 .setParameter("seedId", seedId)
350 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000351
95630366963296f62025-06-02 21:16:32 +0800352 tx.commit();
353 logger.info("Successfully added vote from user {} for seed {} in beg {}", userId, seedId, begId);
354 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000355
95630366963296f62025-06-02 21:16:32 +0800356 } catch (Exception e) {
357 if (tx != null && tx.isActive()) {
358 tx.rollback();
359 }
360 logger.error("Error voting for seed: {}", e.getMessage());
361 return 2;
362 } finally {
363 if (em != null) {
364 em.close();
365 }
366 }
367 }
Haotian W88568d82025-05-15 16:15:21 +0000368
95630366963296f62025-06-02 21:16:32 +0800369 @Override
370 public int SubmitSeed(String begid, Seed seed) {
371 if (begid == null || seed == null || begid.isEmpty() || seed.seedid == null) {
372 logger.warn("Invalid parameters: begid or seed is null or empty");
373 return 2;
374 }
Haotian W88568d82025-05-15 16:15:21 +0000375
95630366963296f62025-06-02 21:16:32 +0800376 EntityManager em = null;
377 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000378
95630366963296f62025-06-02 21:16:32 +0800379 try {
380 em = emf.createEntityManager();
381 tx = em.getTransaction();
382 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000383
95630366963296f62025-06-02 21:16:32 +0800384 // 检查求种信息是否存在
385 BegInfo begInfo = em.find(BegInfo.class, begid);
386 if (begInfo == null) {
387 logger.warn("BegSeed with ID {} does not exist", begid);
388 return 2;
389 }
Haotian W88568d82025-05-15 16:15:21 +0000390
95630366963296f62025-06-02 21:16:32 +0800391 // 检查种子是否已提交过
392 QSubmitSeed ss = QSubmitSeed.submitSeed;
393 Long submitCount = new JPAQuery<>(em)
394 .select(ss.count())
395 .from(ss)
396 .where(ss.begInfo.begid.eq(begid))
397 .where(ss.seed.seedid.eq(seed.seedid))
398 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000399
95630366963296f62025-06-02 21:16:32 +0800400 if (submitCount > 0) {
401 logger.warn("Seed {} has already been submitted for beg {}", seed.seedid,
402 begid);
403 return 1;
404 }
Haotian W88568d82025-05-15 16:15:21 +0000405
95630366963296f62025-06-02 21:16:32 +0800406 // 保存种子信息(如果不存在)
407 if (em.find(Seed.class, seed.seedid) == null) {
408 em.persist(seed);
409 }
Haotian W88568d82025-05-15 16:15:21 +0000410
95630366963296f62025-06-02 21:16:32 +0800411 // 创建提交记录
412 em.createNativeQuery("INSERT INTO SubmitSeed (beg_id, seed_id, votes) VALUES (?, ?, 0)")
413 .setParameter(1, begid)
414 .setParameter(2, seed.seedid)
415 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000416
95630366963296f62025-06-02 21:16:32 +0800417 tx.commit();
418 logger.info("Successfully submitted seed {} for beg {}", seed.seedid, begid);
419 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000420
95630366963296f62025-06-02 21:16:32 +0800421 } catch (Exception e) {
422 if (tx != null && tx.isActive()) {
423 tx.rollback();
424 }
425 logger.error("Error submitting seed: {}", e.getMessage());
426 return 2;
427 } finally {
428 if (em != null) {
429 em.close();
430 }
431 }
432 }
Haotian W88568d82025-05-15 16:15:21 +0000433
95630366963296f62025-06-02 21:16:32 +0800434 @Override
435 public void SettleBeg() {
436 EntityManager em = null;
437 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000438
95630366963296f62025-06-02 21:16:32 +0800439 try {
440 em = emf.createEntityManager();
441 tx = em.getTransaction();
442 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000443
95630366963296f62025-06-02 21:16:32 +0800444 // 1. 获取所有已过期且未完成的求种信息
445 QBegInfo b = QBegInfo.begInfo;
446 List<BegInfo> expiredBegs = new JPAQuery<>(em)
447 .select(b)
448 .from(b)
449 .where(b.endtime.loe(new Date())
450 .and(b.hasseed.eq(0)))
451 .fetch();
Haotian W88568d82025-05-15 16:15:21 +0000452
95630366963296f62025-06-02 21:16:32 +0800453 for (BegInfo beg : expiredBegs) {
454 // 2. 查找投票最多的提交任务
455 QSubmitSeed ss = QSubmitSeed.submitSeed;
456 Tuple topSubmission = new JPAQuery<>(em)
457 .select(ss.seed.seedid, ss.votes)
458 .from(ss)
459 .where(ss.begInfo.begid.eq(beg.begid))
460 .orderBy(ss.votes.desc())
461 .limit(1)
462 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000463
95630366963296f62025-06-02 21:16:32 +0800464 if (topSubmission != null && topSubmission.get(ss.votes) > 0) {
465 String seedId = topSubmission.get(ss.seed.seedid);
Haotian W88568d82025-05-15 16:15:21 +0000466
95630366963296f62025-06-02 21:16:32 +0800467 // 3. 获取上传者ID
468 QSeed s = QSeed.seed;
469 String ownerId = new JPAQuery<>(em)
470 .select(s.seeduserid)
471 .from(s)
472 .where(s.seedid.eq(seedId))
473 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000474
95630366963296f62025-06-02 21:16:32 +0800475 // 4. 获取上传者的PT信息并更新魔力值
476 UserPT ownerPT = em.find(UserPT.class, ownerId);
477 if (ownerPT != null) {
478 // 5. 发放奖励
479 ownerPT.magic += beg.magic;
480 em.merge(ownerPT);
Haotian W88568d82025-05-15 16:15:21 +0000481
95630366963296f62025-06-02 21:16:32 +0800482 // 6. 更新求种状态
483 beg.hasseed = 1;
484 em.merge(beg);
Haotian W88568d82025-05-15 16:15:21 +0000485
95630366963296f62025-06-02 21:16:32 +0800486 logger.info("Reward {} magic points awarded to user {} for beg {}",
487 beg.magic, ownerId, beg.begid);
488 }
489 }
490 }
Haotian W88568d82025-05-15 16:15:21 +0000491
95630366963296f62025-06-02 21:16:32 +0800492 tx.commit();
493 logger.info("Successfully settled {} expired beg requests",
494 expiredBegs.size());
Haotian W88568d82025-05-15 16:15:21 +0000495
95630366963296f62025-06-02 21:16:32 +0800496 } catch (Exception e) {
497 if (tx != null && tx.isActive()) {
498 tx.rollback();
499 }
500 logger.error("Error settling beg requests: {}", e.getMessage(), e);
501 } finally {
502 if (em != null) {
503 em.close();
504 }
505 }
506 }
Haotian W88568d82025-05-15 16:15:21 +0000507
95630366963296f62025-06-02 21:16:32 +0800508 @Override
509 public int AddPost(Post post) {
510 if (post == null || post.postid == null || post.postid.isEmpty()) {
511 logger.warn("Invalid parameter: post is null or postid is empty");
512 return 2;
513 }
Haotian W88568d82025-05-15 16:15:21 +0000514
95630366963296f62025-06-02 21:16:32 +0800515 EntityManager em = null;
516 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000517
95630366963296f62025-06-02 21:16:32 +0800518 try {
519 em = emf.createEntityManager();
520 tx = em.getTransaction();
521 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000522
95630366963296f62025-06-02 21:16:32 +0800523 // 检查是否重复
524 Post existingPost = em.find(Post.class, post.postid);
525 if (existingPost != null) {
526 logger.warn("Post with ID {} already exists", post.postid);
527 return 1;
528 }
Haotian W88568d82025-05-15 16:15:21 +0000529
95630366963296f62025-06-02 21:16:32 +0800530 // 检查用户是否存在
531 User user = em.find(User.class, post.postuserid);
532 if (user == null) {
533 logger.warn("User with ID {} does not exist", post.postuserid);
534 return 2;
535 }
Haotian W88568d82025-05-15 16:15:21 +0000536
95630366963296f62025-06-02 21:16:32 +0800537 // 设置初始值
538 if (post.posttime == null) {
539 post.posttime = new Date();
540 }
541 post.replytime = 0;
542 post.readtime = 0;
Haotian W88568d82025-05-15 16:15:21 +0000543
95630366963296f62025-06-02 21:16:32 +0800544 // 保存帖子
545 em.persist(post);
546 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000547
95630366963296f62025-06-02 21:16:32 +0800548 logger.info("Successfully added new post with ID: {}", post.postid);
549 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000550
95630366963296f62025-06-02 21:16:32 +0800551 } catch (Exception e) {
552 if (tx != null && tx.isActive()) {
553 tx.rollback();
554 }
555 logger.error("Error adding post: {}", e.getMessage());
556 return 2;
557 } finally {
558 if (em != null) {
559 em.close();
560 }
561 }
562 }
Haotian W88568d82025-05-15 16:15:21 +0000563
95630366963296f62025-06-02 21:16:32 +0800564 @Override
565 public int UpdatePost(Post post) {
566 if (post == null || post.postid == null || post.postid.isEmpty()) {
567 logger.warn("Invalid parameter: post is null or postid is empty");
568 return 2;
569 }
Haotian W88568d82025-05-15 16:15:21 +0000570
95630366963296f62025-06-02 21:16:32 +0800571 EntityManager em = null;
572 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000573
95630366963296f62025-06-02 21:16:32 +0800574 try {
575 em = emf.createEntityManager();
576 tx = em.getTransaction();
577 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000578
95630366963296f62025-06-02 21:16:32 +0800579 // 检查帖子是否存在
580 Post existingPost = em.find(Post.class, post.postid);
581 if (existingPost == null) {
582 logger.warn("Post with ID {} does not exist", post.postid);
583 return 1;
584 }
Haotian W88568d82025-05-15 16:15:21 +0000585
95630366963296f62025-06-02 21:16:32 +0800586 // 保持原有不可修改的字段
587 post.postuserid = existingPost.postuserid;
588 post.posttime = existingPost.posttime;
589 post.replytime = existingPost.replytime;
590 post.readtime = existingPost.readtime;
Haotian W88568d82025-05-15 16:15:21 +0000591
95630366963296f62025-06-02 21:16:32 +0800592 // 更新帖子
593 em.merge(post);
594 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000595
95630366963296f62025-06-02 21:16:32 +0800596 logger.info("Successfully updated post with ID: {}", post.postid);
597 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000598
95630366963296f62025-06-02 21:16:32 +0800599 } catch (Exception e) {
600 if (tx != null && tx.isActive()) {
601 tx.rollback();
602 }
603 logger.error("Error updating post: {}", e.getMessage());
604 return 2;
605 } finally {
606 if (em != null) {
607 em.close();
608 }
609 }
610 }
Haotian W88568d82025-05-15 16:15:21 +0000611
95630366963296f62025-06-02 21:16:32 +0800612 @Override
613 public int DeletePost(String postid) {
614 if (postid == null || postid.isEmpty()) {
615 logger.warn("Invalid parameter: postid is null or empty");
616 return 2;
617 }
Haotian W88568d82025-05-15 16:15:21 +0000618
95630366963296f62025-06-02 21:16:32 +0800619 EntityManager em = null;
620 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000621
95630366963296f62025-06-02 21:16:32 +0800622 try {
623 em = emf.createEntityManager();
624 tx = em.getTransaction();
625 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000626
95630366963296f62025-06-02 21:16:32 +0800627 // 查找要删除的帖子
628 Post post = em.find(Post.class, postid);
629 if (post == null) {
630 logger.warn("Post with ID {} does not exist", postid);
631 tx.rollback();
632 return 1;
633 }
Haotian W88568d82025-05-15 16:15:21 +0000634
95630366963296f62025-06-02 21:16:32 +0800635 // 删除帖子(由于设置了级联删除,相关的回复会自动删除)
636 em.remove(post);
637 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000638
95630366963296f62025-06-02 21:16:32 +0800639 logger.info("Successfully deleted post with ID: {}", postid);
640 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000641
95630366963296f62025-06-02 21:16:32 +0800642 } catch (Exception e) {
643 if (tx != null && tx.isActive()) {
644 tx.rollback();
645 }
646 logger.error("Error deleting post: {}", e.getMessage());
647 return 2;
648 } finally {
649 if (em != null) {
650 em.close();
651 }
652 }
653 }
Haotian W88568d82025-05-15 16:15:21 +0000654
95630366963296f62025-06-02 21:16:32 +0800655 @Override
656 public int AddComment(String postid, String userid, String comment) {
657 if (postid == null || postid.isEmpty() ||
658 userid == null || userid.isEmpty() ||
659 comment == null || comment.isEmpty()) {
660 logger.warn("Invalid parameters: postid, userid or comment is null or empty");
661 return 2;
662 }
Haotian W88568d82025-05-15 16:15:21 +0000663
95630366963296f62025-06-02 21:16:32 +0800664 EntityManager em = null;
665 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000666
95630366963296f62025-06-02 21:16:32 +0800667 try {
668 em = emf.createEntityManager();
669 tx = em.getTransaction();
670 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000671
95630366963296f62025-06-02 21:16:32 +0800672 // 检查帖子是否存在
673 Post post = em.find(Post.class, postid);
674 if (post == null) {
675 logger.warn("Post with ID {} does not exist", postid);
676 return 1;
677 }
Haotian W88568d82025-05-15 16:15:21 +0000678
95630366963296f62025-06-02 21:16:32 +0800679 // 检查评论用户是否存在
680 User user = em.find(User.class, userid);
681 if (user == null) {
682 logger.warn("User with ID {} does not exist", userid);
683 return 1;
684 }
Haotian W88568d82025-05-15 16:15:21 +0000685
95630366963296f62025-06-02 21:16:32 +0800686 // 创建新的回复
687 PostReply reply = new PostReply();
688 reply.replyid = UUID.randomUUID().toString();
689 reply.postid = postid;
690 reply.content = comment;
691 reply.authorid = userid;
692 reply.createdAt = new Date();
Haotian W88568d82025-05-15 16:15:21 +0000693
95630366963296f62025-06-02 21:16:32 +0800694 // 保存回复
695 em.persist(reply);
Haotian W88568d82025-05-15 16:15:21 +0000696
95630366963296f62025-06-02 21:16:32 +0800697 // 更新帖子的回复数
698 post.replytime += 1;
699 em.merge(post);
Haotian W88568d82025-05-15 16:15:21 +0000700
95630366963296f62025-06-02 21:16:32 +0800701 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000702
95630366963296f62025-06-02 21:16:32 +0800703 logger.info("Successfully added comment by user {} to post {}, reply ID: {}",
704 userid, postid, reply.replyid);
705 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000706
95630366963296f62025-06-02 21:16:32 +0800707 } catch (Exception e) {
708 if (tx != null && tx.isActive()) {
709 tx.rollback();
710 }
711 logger.error("Error adding comment: {}", e.getMessage());
712 return 2;
713 } finally {
714 if (em != null) {
715 em.close();
716 }
717 }
718 }
Haotian W88568d82025-05-15 16:15:21 +0000719
95630366963296f62025-06-02 21:16:32 +0800720 @Override
721 public int DeleteComment(String postid, String commentid) {
722 if (postid == null || postid.isEmpty() || commentid == null || commentid.isEmpty()) {
723 logger.warn("Invalid parameters: postid or commentid is null or empty");
724 return 2;
725 }
Haotian W88568d82025-05-15 16:15:21 +0000726
95630366963296f62025-06-02 21:16:32 +0800727 EntityManager em = null;
728 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000729
95630366963296f62025-06-02 21:16:32 +0800730 try {
731 em = emf.createEntityManager();
732 tx = em.getTransaction();
733 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000734
95630366963296f62025-06-02 21:16:32 +0800735 // 检查帖子是否存在
736 Post post = em.find(Post.class, postid);
737 if (post == null) {
738 logger.warn("Post with ID {} does not exist", postid);
739 return 1;
740 }
Haotian W88568d82025-05-15 16:15:21 +0000741
95630366963296f62025-06-02 21:16:32 +0800742 // 检查评论是否存在且属于该帖子
743 PostReply reply = em.find(PostReply.class, commentid);
744 if (reply == null || !reply.postid.equals(postid)) {
745 logger.warn("Comment {} does not exist or does not belong to post {}", commentid, postid);
746 return 1;
747 }
Haotian W88568d82025-05-15 16:15:21 +0000748
95630366963296f62025-06-02 21:16:32 +0800749 // 删除评论
750 em.remove(reply);
Haotian W88568d82025-05-15 16:15:21 +0000751
95630366963296f62025-06-02 21:16:32 +0800752 // 更新帖子的回复数
753 post.replytime = Math.max(0, post.replytime - 1);
754 em.merge(post);
Haotian W88568d82025-05-15 16:15:21 +0000755
95630366963296f62025-06-02 21:16:32 +0800756 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000757
95630366963296f62025-06-02 21:16:32 +0800758 logger.info("Successfully deleted comment {} from post {}", commentid, postid);
759 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000760
95630366963296f62025-06-02 21:16:32 +0800761 } catch (Exception e) {
762 if (tx != null && tx.isActive()) {
763 tx.rollback();
764 }
765 logger.error("Error deleting comment: {}", e.getMessage());
766 return 2;
767 } finally {
768 if (em != null) {
769 em.close();
770 }
771 }
772 }
Haotian W88568d82025-05-15 16:15:21 +0000773
95630366963296f62025-06-02 21:16:32 +0800774 @Override
775 public boolean ExchangeMagicToUpload(String userid, int magic) {
776 if (userid == null || userid.isEmpty() || magic <= 0) {
777 logger.warn("参数无效: userid为空或magic <= 0");
778 return false;
779 }
Haotian W88568d82025-05-15 16:15:21 +0000780
95630366963296f62025-06-02 21:16:32 +0800781 EntityManager em = null;
782 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000783
95630366963296f62025-06-02 21:16:32 +0800784 try {
785 em = emf.createEntityManager();
786 tx = em.getTransaction();
787 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000788
95630366963296f62025-06-02 21:16:32 +0800789 UserPT userPT = em.find(UserPT.class, userid);
790 if (userPT == null) {
791 logger.warn("未找到用户 {} 的PT信息", userid);
792 return false;
793 }
Haotian W88568d82025-05-15 16:15:21 +0000794
95630366963296f62025-06-02 21:16:32 +0800795 if (userPT.magic < magic) {
796 logger.warn("用户 {} 的魔力值不足", userid);
797 return false;
798 }
Haotian W88568d82025-05-15 16:15:21 +0000799
95630366963296f62025-06-02 21:16:32 +0800800 // 1:1兑换,直接加上魔力值(假设单位是MB)
801 userPT.magic -= magic;
802 userPT.upload += magic;
Haotian W88568d82025-05-15 16:15:21 +0000803
95630366963296f62025-06-02 21:16:32 +0800804 // 更新分享率
805 if (userPT.download > 0) {
806 userPT.share = (double) userPT.upload / userPT.download;
807 }
Haotian W88568d82025-05-15 16:15:21 +0000808
95630366963296f62025-06-02 21:16:32 +0800809 em.merge(userPT);
810 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000811
95630366963296f62025-06-02 21:16:32 +0800812 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 上传量", userid, magic, magic);
813 return true;
Haotian W88568d82025-05-15 16:15:21 +0000814
95630366963296f62025-06-02 21:16:32 +0800815 } catch (Exception e) {
816 if (tx != null && tx.isActive()) {
817 tx.rollback();
818 }
819 logger.error("魔力值兑换上传量时发生错误: {}", e.getMessage());
820 return false;
821 } finally {
822 if (em != null) {
823 em.close();
824 }
825 }
826 }
Haotian W88568d82025-05-15 16:15:21 +0000827
95630366963296f62025-06-02 21:16:32 +0800828 @Override
829 public boolean ExchangeMagicToDownload(String userid, int magic) {
830 if (userid == null || userid.isEmpty() || magic <= 0) {
831 logger.warn("参数无效: userid为空或magic <= 0");
832 return false;
833 }
Haotian W88568d82025-05-15 16:15:21 +0000834
95630366963296f62025-06-02 21:16:32 +0800835 EntityManager em = null;
836 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000837
95630366963296f62025-06-02 21:16:32 +0800838 try {
839 em = emf.createEntityManager();
840 tx = em.getTransaction();
841 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000842
95630366963296f62025-06-02 21:16:32 +0800843 UserPT userPT = em.find(UserPT.class, userid);
844 if (userPT == null) {
845 logger.warn("未找到用户 {} 的PT信息", userid);
846 return false;
847 }
Haotian W88568d82025-05-15 16:15:21 +0000848
95630366963296f62025-06-02 21:16:32 +0800849 if (userPT.magic < magic) {
850 logger.warn("用户 {} 的魔力值不足", userid);
851 return false;
852 }
Haotian W88568d82025-05-15 16:15:21 +0000853
95630366963296f62025-06-02 21:16:32 +0800854 // 1:1兑换,直接减去魔力值对应的下载量(假设单位是MB)
855 userPT.magic -= magic;
856 userPT.download = Math.max(0, userPT.download - magic);
Haotian W88568d82025-05-15 16:15:21 +0000857
95630366963296f62025-06-02 21:16:32 +0800858 // 更新分享率
859 if (userPT.download > 0) {
860 userPT.share = (double) userPT.upload / userPT.download;
861 }
Haotian W88568d82025-05-15 16:15:21 +0000862
95630366963296f62025-06-02 21:16:32 +0800863 em.merge(userPT);
864 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000865
95630366963296f62025-06-02 21:16:32 +0800866 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 下载量减免", userid, magic, magic);
867 return true;
Haotian W88568d82025-05-15 16:15:21 +0000868
95630366963296f62025-06-02 21:16:32 +0800869 } catch (Exception e) {
870 if (tx != null && tx.isActive()) {
871 tx.rollback();
872 }
873 logger.error("魔力值兑换下载量时发生错误: {}", e.getMessage());
874 return false;
875 } finally {
876 if (em != null) {
877 em.close();
878 }
879 }
880 }
Haotian W88568d82025-05-15 16:15:21 +0000881
95630366963296f62025-06-02 21:16:32 +0800882 @Override
883 public boolean ExchangeMagicToVip(String userid, int magic) {
884 if (userid == null || userid.isEmpty() || magic <= 0) {
885 logger.warn("参数无效: userid为空或magic <= 0");
886 return false;
887 }
Haotian W88568d82025-05-15 16:15:21 +0000888
95630366963296f62025-06-02 21:16:32 +0800889 EntityManager em = null;
890 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000891
95630366963296f62025-06-02 21:16:32 +0800892 try {
893 em = emf.createEntityManager();
894 tx = em.getTransaction();
895 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000896
95630366963296f62025-06-02 21:16:32 +0800897 UserPT userPT = em.find(UserPT.class, userid);
898 if (userPT == null) {
899 logger.warn("未找到用户 {} 的PT信息", userid);
900 return false;
901 }
Haotian W88568d82025-05-15 16:15:21 +0000902
95630366963296f62025-06-02 21:16:32 +0800903 if (userPT.magic < magic) {
904 logger.warn("用户 {} 的魔力值不足", userid);
905 return false;
906 }
Haotian W88568d82025-05-15 16:15:21 +0000907
95630366963296f62025-06-02 21:16:32 +0800908 // 1:1兑换VIP下载次数
909 userPT.magic -= magic;
910 userPT.viptime += magic;
Haotian W88568d82025-05-15 16:15:21 +0000911
95630366963296f62025-06-02 21:16:32 +0800912 em.merge(userPT);
913 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000914
95630366963296f62025-06-02 21:16:32 +0800915 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {} 次VIP下载次数", userid, magic, magic);
916 return true;
Haotian W88568d82025-05-15 16:15:21 +0000917
95630366963296f62025-06-02 21:16:32 +0800918 } catch (Exception e) {
919 if (tx != null && tx.isActive()) {
920 tx.rollback();
921 }
922 logger.error("魔力值兑换VIP下载次数时发生错误: {}", e.getMessage());
923 return false;
924 } finally {
925 if (em != null) {
926 em.close();
927 }
928 }
929 }
Haotian W88568d82025-05-15 16:15:21 +0000930
95630366963296f62025-06-02 21:16:32 +0800931 @Override
932 public boolean UploadTransmitProfile(Profile profile) {
933 if (profile == null || profile.profileurl == null || profile.profileurl.isEmpty() ||
934 profile.userid == null || profile.userid.isEmpty()) {
935 logger.warn("参数无效: profile为空或必要字段为空");
936 return false;
937 }
Haotian W88568d82025-05-15 16:15:21 +0000938
95630366963296f62025-06-02 21:16:32 +0800939 EntityManager em = null;
940 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000941
95630366963296f62025-06-02 21:16:32 +0800942 try {
943 em = emf.createEntityManager();
944 tx = em.getTransaction();
945 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000946
95630366963296f62025-06-02 21:16:32 +0800947 // 检查用户是否存在
948 User user = em.find(User.class, profile.userid);
949 if (user == null) {
950 logger.warn("用户 {} 不存在", profile.userid);
951 return false;
952 }
Haotian W88568d82025-05-15 16:15:21 +0000953
95630366963296f62025-06-02 21:16:32 +0800954 // 检查是否已存在相同的迁移申请
955 Profile existingProfile = em.find(Profile.class, profile.profileurl);
956 if (existingProfile != null) {
957 logger.warn("迁移申请 {} 已存在", profile.profileurl);
958 return false;
959 }
Haotian W88568d82025-05-15 16:15:21 +0000960
95630366963296f62025-06-02 21:16:32 +0800961 // 设置初始值
962 profile.exampass = false;
963 profile.magicgived = "0";
964 profile.uploadgived = "0";
Haotian W88568d82025-05-15 16:15:21 +0000965
95630366963296f62025-06-02 21:16:32 +0800966 // 保存迁移申请
967 em.persist(profile);
968 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000969
95630366963296f62025-06-02 21:16:32 +0800970 logger.info("成功上传迁移申请 {}", profile.profileurl);
971 return true;
Haotian W88568d82025-05-15 16:15:21 +0000972
95630366963296f62025-06-02 21:16:32 +0800973 } catch (Exception e) {
974 if (tx != null && tx.isActive()) {
975 tx.rollback();
976 }
977 logger.error("上传迁移申请时发生错误: {}", e.getMessage());
978 return false;
979 } finally {
980 if (em != null) {
981 em.close();
982 }
983 }
984 }
Haotian W88568d82025-05-15 16:15:21 +0000985
95630366963296f62025-06-02 21:16:32 +0800986 @Override
987 public Profile GetTransmitProfile(String profileid) {
988 if (profileid == null || profileid.isEmpty()) {
989 logger.warn("参数无效: profileid为空");
990 return null;
991 }
Haotian W88568d82025-05-15 16:15:21 +0000992
95630366963296f62025-06-02 21:16:32 +0800993 EntityManager em = null;
Haotian W88568d82025-05-15 16:15:21 +0000994
95630366963296f62025-06-02 21:16:32 +0800995 try {
996 em = emf.createEntityManager();
997 Profile profile = em.find(Profile.class, profileid);
998 if (profile == null) {
999 logger.warn("未找到迁移申请 {}", profileid);
1000 return null;
1001 }
Haotian W88568d82025-05-15 16:15:21 +00001002
95630366963296f62025-06-02 21:16:32 +08001003 logger.info("成功获取迁移申请 {}", profileid);
1004 return profile;
Haotian W88568d82025-05-15 16:15:21 +00001005
95630366963296f62025-06-02 21:16:32 +08001006 } catch (Exception e) {
1007 logger.error("获取迁移申请时发生错误: {}", e.getMessage());
1008 return null;
1009 } finally {
1010 if (em != null) {
1011 em.close();
1012 }
1013 }
1014 }
Haotian W88568d82025-05-15 16:15:21 +00001015
95630366963296f62025-06-02 21:16:32 +08001016 @Override
1017 public boolean ExamTransmitProfile(String profileid, boolean result) {
1018 if (profileid == null || profileid.isEmpty()) {
1019 logger.warn("参数无效: profileid为空");
1020 return false;
1021 }
Haotian W88568d82025-05-15 16:15:21 +00001022
95630366963296f62025-06-02 21:16:32 +08001023 EntityManager em = null;
1024 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +00001025
95630366963296f62025-06-02 21:16:32 +08001026 try {
1027 em = emf.createEntityManager();
1028 tx = em.getTransaction();
1029 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +00001030
95630366963296f62025-06-02 21:16:32 +08001031 // 查找迁移申请
1032 Profile profile = em.find(Profile.class, profileid);
1033 if (profile == null) {
1034 logger.warn("未找到迁移申请 {}", profileid);
1035 return false;
1036 }
Haotian W88568d82025-05-15 16:15:21 +00001037
95630366963296f62025-06-02 21:16:32 +08001038 // 更新审核状态
1039 profile.exampass = result;
Haotian W88568d82025-05-15 16:15:21 +00001040
95630366963296f62025-06-02 21:16:32 +08001041 if (result) {
1042 // 如果审核通过,更新用户的PT信息
1043 UserPT userPT = em.find(UserPT.class, profile.userid);
1044 if (userPT != null) {
1045 // 发放魔力值
1046 int magicToGive = Integer.parseInt(profile.magictogive);
1047 userPT.magic += magicToGive;
1048 profile.magicgived = String.valueOf(magicToGive);
Haotian W88568d82025-05-15 16:15:21 +00001049
95630366963296f62025-06-02 21:16:32 +08001050 // 发放上传量
1051 long uploadToGive = Long.parseLong(profile.uploadtogive);
1052 userPT.upload += uploadToGive;
1053 profile.uploadgived = String.valueOf(uploadToGive);
Haotian W88568d82025-05-15 16:15:21 +00001054
95630366963296f62025-06-02 21:16:32 +08001055 em.merge(userPT);
1056 }
1057 }
Haotian W88568d82025-05-15 16:15:21 +00001058
95630366963296f62025-06-02 21:16:32 +08001059 em.merge(profile);
1060 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +00001061
95630366963296f62025-06-02 21:16:32 +08001062 logger.info("成功审核迁移申请 {}, 结果: {}", profileid, result);
1063 return true;
Haotian W88568d82025-05-15 16:15:21 +00001064
95630366963296f62025-06-02 21:16:32 +08001065 } catch (Exception e) {
1066 if (tx != null && tx.isActive()) {
1067 tx.rollback();
1068 }
1069 logger.error("审核迁移申请时发生错误: {}", e.getMessage());
1070 return false;
1071 } finally {
1072 if (em != null) {
1073 em.close();
1074 }
1075 }
1076 }
Haotian W88568d82025-05-15 16:15:21 +00001077
95630366963296f62025-06-02 21:16:32 +08001078 @Override
1079 public Profile[] GetTransmitProfileList() {
1080 EntityManager em = null;
Haotian W88568d82025-05-15 16:15:21 +00001081
95630366963296f62025-06-02 21:16:32 +08001082 try {
1083 em = emf.createEntityManager();
Haotian W88568d82025-05-15 16:15:21 +00001084
95630366963296f62025-06-02 21:16:32 +08001085 // 获取所有迁移申请
1086 QProfile p = QProfile.profile;
1087 List<Profile> profiles = new JPAQuery<>(em)
1088 .select(p)
1089 .from(p)
1090 .fetch();
Haotian W88568d82025-05-15 16:15:21 +00001091
95630366963296f62025-06-02 21:16:32 +08001092 logger.info("成功获取所有迁移申请,共 {} 条", profiles.size());
1093 return profiles.toArray(new Profile[0]);
Haotian W88568d82025-05-15 16:15:21 +00001094
95630366963296f62025-06-02 21:16:32 +08001095 } catch (Exception e) {
1096 logger.error("获取迁移申请列表时发生错误: {}", e.getMessage());
1097 return new Profile[0];
1098 } finally {
1099 if (em != null) {
1100 em.close();
1101 }
1102 }
1103 }
rhj46f62c42025-06-06 23:24:10 +08001104
1105 @Override
1106 public Post[] GetPostList(){
1107 return null;
1108 }
95630366963296f62025-06-02 21:16:32 +08001109}