blob: 91dc8ff44e544a85d7fd996d545af82ab781ce42 [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;
rhje18c3f72025-06-08 00:27:01 +08006import java.util.ArrayList;
95630366963296f62025-06-02 21:16:32 +08007import java.util.Map;
Haotian W88568d82025-05-15 16:15:21 +00008import java.util.Date;
95630366963296f62025-06-02 21:16:32 +08009import java.util.HashMap;
rhje18c3f72025-06-08 00:27:01 +080010import java.io.File;
Haotian W88568d82025-05-15 16:15:21 +000011
12import javax.persistence.EntityManager;
13import javax.persistence.EntityManagerFactory;
14import javax.persistence.EntityTransaction;
15import javax.persistence.Persistence;
16
17import com.querydsl.jpa.impl.JPAQuery;
18import com.querydsl.core.Tuple;
rootff0769a2025-05-18 17:24:41 +000019import entity.QBegInfo;
20import entity.QUserVotes;
21import entity.QSubmitSeed;
22import entity.QProfile;
23import entity.QSeed;
24import entity.Seed;
25import entity.User;
26import entity.UserPT;
95630366963296f62025-06-02 21:16:32 +080027import entity.config;
rootff0769a2025-05-18 17:24:41 +000028import entity.Notice;
29import entity.BegInfo;
rhje18c3f72025-06-08 00:27:01 +080030import entity.BegInfoDetail;
31import entity.BegSeedDetail;
rootff0769a2025-05-18 17:24:41 +000032import entity.Post;
33import entity.PostReply;
34import entity.Profile;
rhje18c3f72025-06-08 00:27:01 +080035import entity.UserStar;
36import entity.SeedWithVotes;
Haotian W88568d82025-05-15 16:15:21 +000037
38import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
95630366963296f62025-06-02 21:16:32 +080041public class Database2 implements DataManagerInterface {
Haotian W88568d82025-05-15 16:15:21 +000042
95630366963296f62025-06-02 21:16:32 +080043 private EntityManagerFactory emf;
44 private static final Logger logger = LoggerFactory.getLogger(Database2.class);
Haotian W88568d82025-05-15 16:15:21 +000045
95630366963296f62025-06-02 21:16:32 +080046 // 构造函数,初始化EntityManagerFactory
47 public Database2() {
48 config cfg = new config();
49 Map<String, Object> props = new HashMap<>();
50 props.put("javax.persistence.jdbc.url",
51 "jdbc:mysql://" + cfg.SqlURL + "/" + cfg.Database);
52 props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
53 props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
54 this.emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
55 }
Haotian W88568d82025-05-15 16:15:21 +000056
95630366963296f62025-06-02 21:16:32 +080057 public Database2(EntityManagerFactory emf) {
58 this.emf = emf;
59 }
root33a7d952025-05-18 17:24:41 +000060
95630366963296f62025-06-02 21:16:32 +080061 @Override
TRM-codingcdfe5482025-06-06 17:31:01 +080062 public String LoginUser(User userinfo){
63 return null;
64 }
65
66 @Override
95630366963296f62025-06-02 21:16:32 +080067 public int RegisterUser(User userinfo) {
68 return 0;
69 }
root33a7d952025-05-18 17:24:41 +000070
95630366963296f62025-06-02 21:16:32 +080071 @Override
72 public int UpdateInformation(User userinfo) {
73 return 0;
74 }
root33a7d952025-05-18 17:24:41 +000075
95630366963296f62025-06-02 21:16:32 +080076 @Override
77 public User GetInformation(String userid) {
78 return null;
79 }
root33a7d952025-05-18 17:24:41 +000080
95630366963296f62025-06-02 21:16:32 +080081 @Override
82 public UserPT GetInformationPT(String userid) {
83 return null;
84 }
root33a7d952025-05-18 17:24:41 +000085
95630366963296f62025-06-02 21:16:32 +080086 @Override
87 public int UpdateInformationPT(UserPT userinfo) {
88 return 0;
89 }
root33a7d952025-05-18 17:24:41 +000090
95630366963296f62025-06-02 21:16:32 +080091 @Override
92 public int RegisterUserPT(UserPT userinfo) {
93 return 0;
94 }
root33a7d952025-05-18 17:24:41 +000095
95630366963296f62025-06-02 21:16:32 +080096 @Override
97 public Seed GetSeedInformation(String seedid) {
98 return null;
99 }
root33a7d952025-05-18 17:24:41 +0000100
95630366963296f62025-06-02 21:16:32 +0800101 @Override
102 public int RegisterSeed(Seed seedinfo) {
103 return 0;
104 }
root33a7d952025-05-18 17:24:41 +0000105
95630366963296f62025-06-02 21:16:32 +0800106 @Override
107 public int UpdateSeed(Seed seedinfo) {
108 return 0;
109 }
root33a7d952025-05-18 17:24:41 +0000110
95630366963296f62025-06-02 21:16:32 +0800111 @Override
112 public Seed[] SearchSeed(String userQ) {
113 return new Seed[0];
114 }
root33a7d952025-05-18 17:24:41 +0000115
95630366963296f62025-06-02 21:16:32 +0800116 @Override
117 public int AddNotice(Notice notice) {
118 return 0;
119 }
root33a7d952025-05-18 17:24:41 +0000120
95630366963296f62025-06-02 21:16:32 +0800121 @Override
122 public boolean UpdateNotice(Notice notice) {
123 return false;
124 }
root33a7d952025-05-18 17:24:41 +0000125
95630366963296f62025-06-02 21:16:32 +0800126 @Override
127 public boolean DeleteNotice(String noticeid) {
128 return false;
129 }
root33a7d952025-05-18 17:24:41 +0000130
95630366963296f62025-06-02 21:16:32 +0800131 @Override
132 public int GetUserAvailableInviteTimes(String userid) {
133 return 0;
134 }
root33a7d952025-05-18 17:24:41 +0000135
95630366963296f62025-06-02 21:16:32 +0800136 @Override
137 public int InviteUser(String inviterid, String inviteemail) {
138 return 0;
139 }
root33a7d952025-05-18 17:24:41 +0000140
95630366963296f62025-06-02 21:16:32 +0800141 @Override
Raveraae06122025-06-05 08:13:35 +0000142 public Seed[] GetSeedListByTag(String tag) {
143 return new Seed[0];
144 }
145
146 @Override
rhjc6a4ee02025-06-06 00:45:18 +0800147 public Seed[] GetSeedListByUser(String userid) {
148 return new Seed[0];
149 }
150
151 @Override
152 public int DeleteSeed(String seedid) {
153 return 0;
154 }
155
156 @Override
95630366963296f62025-06-02 21:16:32 +0800157 public boolean AddCollect(String userid, String postid) {
158 return false;
159 }
root33a7d952025-05-18 17:24:41 +0000160
95630366963296f62025-06-02 21:16:32 +0800161 @Override
162 public boolean DeleteCollect(String userid, String postid) {
163 return false;
164 }
Haotian W88568d82025-05-15 16:15:21 +0000165
95630366963296f62025-06-02 21:16:32 +0800166 @Override
167 public int AddBegSeed(BegInfo info) {
168 if (info == null || info.begid == null || info.begid.isEmpty()) {
169 logger.warn("Invalid parameter: info is null or begid is empty");
170 return 2;
171 }
Haotian W88568d82025-05-15 16:15:21 +0000172
95630366963296f62025-06-02 21:16:32 +0800173 EntityManager em = null;
174 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000175
95630366963296f62025-06-02 21:16:32 +0800176 try {
177 em = emf.createEntityManager();
178 tx = em.getTransaction();
179 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000180
95630366963296f62025-06-02 21:16:32 +0800181 // 检查是否重复
182 BegInfo existingBeg = em.find(BegInfo.class, info.begid);
183 if (existingBeg != null) {
184 logger.warn("BegSeed with ID {} already exists", info.begid);
185 return 1;
186 }
Haotian W88568d82025-05-15 16:15:21 +0000187
95630366963296f62025-06-02 21:16:32 +0800188 // 设置默认值
189 if (info.endtime == null) {
190 // 设置默认14天截止期
191 Calendar calendar = Calendar.getInstance();
192 calendar.add(Calendar.DAY_OF_MONTH, 14);
193 info.endtime = calendar.getTime();
194 }
195 info.hasseed = 0;
Haotian W88568d82025-05-15 16:15:21 +0000196
95630366963296f62025-06-02 21:16:32 +0800197 // 保存新的求种信息
198 em.persist(info);
199 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000200
95630366963296f62025-06-02 21:16:32 +0800201 logger.info("Successfully added new BegSeed with ID: {}", info.begid);
202 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000203
95630366963296f62025-06-02 21:16:32 +0800204 } catch (Exception e) {
205 if (tx != null && tx.isActive()) {
206 tx.rollback();
207 }
208 logger.error("Error adding BegSeed: {}", e.getMessage());
209 return 2;
210 } finally {
211 if (em != null) {
212 em.close();
213 }
214 }
215 }
Haotian W88568d82025-05-15 16:15:21 +0000216
95630366963296f62025-06-02 21:16:32 +0800217 @Override
218 public int UpdateBegSeed(BegInfo info) {
219 if (info == null || info.begid == null || info.begid.isEmpty()) {
220 logger.warn("Invalid parameter: info is null or begid is empty");
221 return 2;
222 }
Haotian W88568d82025-05-15 16:15:21 +0000223
95630366963296f62025-06-02 21:16:32 +0800224 EntityManager em = null;
225 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000226
95630366963296f62025-06-02 21:16:32 +0800227 try {
228 em = emf.createEntityManager();
229 tx = em.getTransaction();
230 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000231
95630366963296f62025-06-02 21:16:32 +0800232 // 检查是否存在
233 BegInfo existingBeg = em.find(BegInfo.class, info.begid);
234 if (existingBeg == null) {
235 logger.warn("BegSeed with ID {} does not exist", info.begid);
236 return 1;
237 }
Haotian W88568d82025-05-15 16:15:21 +0000238
95630366963296f62025-06-02 21:16:32 +0800239 // 保持原有值不变的字段
240 info.hasseed = existingBeg.hasseed;
241 if (info.endtime == null) {
242 info.endtime = existingBeg.endtime;
243 }
Haotian W88568d82025-05-15 16:15:21 +0000244
95630366963296f62025-06-02 21:16:32 +0800245 // 更新求种信息
246 em.merge(info);
247 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000248
95630366963296f62025-06-02 21:16:32 +0800249 logger.info("Successfully updated BegSeed with ID: {}", info.begid);
250 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000251
95630366963296f62025-06-02 21:16:32 +0800252 } catch (Exception e) {
253 if (tx != null && tx.isActive()) {
254 tx.rollback();
255 }
256 logger.error("Error updating BegSeed: {}", e.getMessage());
257 return 2;
258 } finally {
259 if (em != null) {
260 em.close();
261 }
262 }
263 }
Haotian W88568d82025-05-15 16:15:21 +0000264
95630366963296f62025-06-02 21:16:32 +0800265 @Override
266 public int DeleteBegSeed(String begid) {
267 if (begid == null || begid.isEmpty()) {
268 logger.warn("Invalid parameter: begid is null or empty");
269 return 2;
270 }
Haotian W88568d82025-05-15 16:15:21 +0000271
95630366963296f62025-06-02 21:16:32 +0800272 EntityManager em = null;
273 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000274
95630366963296f62025-06-02 21:16:32 +0800275 try {
276 em = emf.createEntityManager();
277 tx = em.getTransaction();
278 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000279
95630366963296f62025-06-02 21:16:32 +0800280 // 查找要删除的求种信息
281 BegInfo begInfo = em.find(BegInfo.class, begid);
282 if (begInfo == null) {
283 logger.warn("BegSeed with ID {} does not exist", begid);
284 tx.rollback();
285 return 1;
286 }
Haotian W88568d82025-05-15 16:15:21 +0000287
95630366963296f62025-06-02 21:16:32 +0800288 // 删除求种信息
289 em.remove(begInfo);
290 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000291
95630366963296f62025-06-02 21:16:32 +0800292 logger.info("Successfully deleted BegSeed with ID: {}", begid);
293 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000294
95630366963296f62025-06-02 21:16:32 +0800295 } catch (Exception e) {
296 if (tx != null && tx.isActive()) {
297 tx.rollback();
298 }
299 logger.error("Error deleting BegSeed: {}", e.getMessage());
300 return 2;
301 } finally {
302 if (em != null) {
303 em.close();
304 }
305 }
306 }
Haotian W88568d82025-05-15 16:15:21 +0000307
95630366963296f62025-06-02 21:16:32 +0800308 @Override
309 public int VoteSeed(String begId, String seedId, String userId) {
310 if (begId == null || seedId == null || userId == null ||
311 begId.isEmpty() || seedId.isEmpty() || userId.isEmpty()) {
312 logger.warn("Invalid parameters: begId, seedId or userId is null or empty");
313 return 2;
314 }
Haotian W88568d82025-05-15 16:15:21 +0000315
95630366963296f62025-06-02 21:16:32 +0800316 EntityManager em = null;
317 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000318
95630366963296f62025-06-02 21:16:32 +0800319 try {
320 em = emf.createEntityManager();
321 tx = em.getTransaction();
322 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000323
95630366963296f62025-06-02 21:16:32 +0800324 // 检查求种信息是否存在
325 BegInfo begInfo = em.find(BegInfo.class, begId);
326 if (begInfo == null) {
327 logger.warn("BegSeed with ID {} does not exist", begId);
328 return 2;
329 }
Haotian W88568d82025-05-15 16:15:21 +0000330
95630366963296f62025-06-02 21:16:32 +0800331 // 检查用户是否已投票
332 Long voteCount = new JPAQuery<>(em)
333 .select(QUserVotes.userVotes.count())
334 .from(QUserVotes.userVotes)
335 .where(QUserVotes.userVotes.id.eq(new entity.UserVotesId(userId, begId, seedId)))
336 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000337
95630366963296f62025-06-02 21:16:32 +0800338 if (voteCount != null && voteCount > 0) {
339 logger.warn("User {} has already voted for seed {} in beg {}", userId, seedId, begId);
340 return 1;
341 }
Haotian W88568d82025-05-15 16:15:21 +0000342
95630366963296f62025-06-02 21:16:32 +0800343 // 创建新的投票记录
344 em.createNativeQuery("INSERT INTO UserVotes (user_id, beg_id, seed_id, created_at) " +
345 "VALUES (?, ?, ?, CURRENT_TIMESTAMP)")
346 .setParameter(1, userId)
347 .setParameter(2, begId)
348 .setParameter(3, seedId)
349 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000350
95630366963296f62025-06-02 21:16:32 +0800351 // 更新SubmitSeed表中的投票数
352 em.createQuery("UPDATE SubmitSeed s SET s.votes = s.votes + 1 " +
353 "WHERE s.id.begId = :begId AND s.id.seedId = :seedId")
354 .setParameter("begId", begId)
355 .setParameter("seedId", seedId)
356 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000357
95630366963296f62025-06-02 21:16:32 +0800358 tx.commit();
359 logger.info("Successfully added vote from user {} for seed {} in beg {}", userId, seedId, begId);
360 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000361
95630366963296f62025-06-02 21:16:32 +0800362 } catch (Exception e) {
363 if (tx != null && tx.isActive()) {
364 tx.rollback();
365 }
366 logger.error("Error voting for seed: {}", e.getMessage());
367 return 2;
368 } finally {
369 if (em != null) {
370 em.close();
371 }
372 }
373 }
Haotian W88568d82025-05-15 16:15:21 +0000374
95630366963296f62025-06-02 21:16:32 +0800375 @Override
376 public int SubmitSeed(String begid, Seed seed) {
377 if (begid == null || seed == null || begid.isEmpty() || seed.seedid == null) {
378 logger.warn("Invalid parameters: begid or seed is null or empty");
379 return 2;
380 }
Haotian W88568d82025-05-15 16:15:21 +0000381
95630366963296f62025-06-02 21:16:32 +0800382 EntityManager em = null;
383 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000384
95630366963296f62025-06-02 21:16:32 +0800385 try {
386 em = emf.createEntityManager();
387 tx = em.getTransaction();
388 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000389
95630366963296f62025-06-02 21:16:32 +0800390 // 检查求种信息是否存在
391 BegInfo begInfo = em.find(BegInfo.class, begid);
392 if (begInfo == null) {
393 logger.warn("BegSeed with ID {} does not exist", begid);
394 return 2;
395 }
Haotian W88568d82025-05-15 16:15:21 +0000396
95630366963296f62025-06-02 21:16:32 +0800397 // 检查种子是否已提交过
398 QSubmitSeed ss = QSubmitSeed.submitSeed;
399 Long submitCount = new JPAQuery<>(em)
400 .select(ss.count())
401 .from(ss)
402 .where(ss.begInfo.begid.eq(begid))
403 .where(ss.seed.seedid.eq(seed.seedid))
404 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000405
95630366963296f62025-06-02 21:16:32 +0800406 if (submitCount > 0) {
407 logger.warn("Seed {} has already been submitted for beg {}", seed.seedid,
408 begid);
409 return 1;
410 }
Haotian W88568d82025-05-15 16:15:21 +0000411
95630366963296f62025-06-02 21:16:32 +0800412 // 保存种子信息(如果不存在)
413 if (em.find(Seed.class, seed.seedid) == null) {
414 em.persist(seed);
415 }
Haotian W88568d82025-05-15 16:15:21 +0000416
95630366963296f62025-06-02 21:16:32 +0800417 // 创建提交记录
418 em.createNativeQuery("INSERT INTO SubmitSeed (beg_id, seed_id, votes) VALUES (?, ?, 0)")
419 .setParameter(1, begid)
420 .setParameter(2, seed.seedid)
421 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000422
95630366963296f62025-06-02 21:16:32 +0800423 tx.commit();
424 logger.info("Successfully submitted seed {} for beg {}", seed.seedid, begid);
425 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000426
95630366963296f62025-06-02 21:16:32 +0800427 } catch (Exception e) {
428 if (tx != null && tx.isActive()) {
429 tx.rollback();
430 }
431 logger.error("Error submitting seed: {}", e.getMessage());
432 return 2;
433 } finally {
434 if (em != null) {
435 em.close();
436 }
437 }
438 }
Haotian W88568d82025-05-15 16:15:21 +0000439
95630366963296f62025-06-02 21:16:32 +0800440 @Override
441 public void SettleBeg() {
442 EntityManager em = null;
443 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000444
95630366963296f62025-06-02 21:16:32 +0800445 try {
446 em = emf.createEntityManager();
447 tx = em.getTransaction();
448 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000449
95630366963296f62025-06-02 21:16:32 +0800450 // 1. 获取所有已过期且未完成的求种信息
451 QBegInfo b = QBegInfo.begInfo;
452 List<BegInfo> expiredBegs = new JPAQuery<>(em)
453 .select(b)
454 .from(b)
455 .where(b.endtime.loe(new Date())
456 .and(b.hasseed.eq(0)))
457 .fetch();
Haotian W88568d82025-05-15 16:15:21 +0000458
95630366963296f62025-06-02 21:16:32 +0800459 for (BegInfo beg : expiredBegs) {
460 // 2. 查找投票最多的提交任务
461 QSubmitSeed ss = QSubmitSeed.submitSeed;
462 Tuple topSubmission = new JPAQuery<>(em)
463 .select(ss.seed.seedid, ss.votes)
464 .from(ss)
465 .where(ss.begInfo.begid.eq(beg.begid))
466 .orderBy(ss.votes.desc())
467 .limit(1)
468 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000469
95630366963296f62025-06-02 21:16:32 +0800470 if (topSubmission != null && topSubmission.get(ss.votes) > 0) {
471 String seedId = topSubmission.get(ss.seed.seedid);
Haotian W88568d82025-05-15 16:15:21 +0000472
95630366963296f62025-06-02 21:16:32 +0800473 // 3. 获取上传者ID
474 QSeed s = QSeed.seed;
475 String ownerId = new JPAQuery<>(em)
476 .select(s.seeduserid)
477 .from(s)
478 .where(s.seedid.eq(seedId))
479 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000480
95630366963296f62025-06-02 21:16:32 +0800481 // 4. 获取上传者的PT信息并更新魔力值
482 UserPT ownerPT = em.find(UserPT.class, ownerId);
483 if (ownerPT != null) {
484 // 5. 发放奖励
485 ownerPT.magic += beg.magic;
486 em.merge(ownerPT);
Haotian W88568d82025-05-15 16:15:21 +0000487
95630366963296f62025-06-02 21:16:32 +0800488 // 6. 更新求种状态
489 beg.hasseed = 1;
490 em.merge(beg);
Haotian W88568d82025-05-15 16:15:21 +0000491
95630366963296f62025-06-02 21:16:32 +0800492 logger.info("Reward {} magic points awarded to user {} for beg {}",
493 beg.magic, ownerId, beg.begid);
494 }
495 }
496 }
Haotian W88568d82025-05-15 16:15:21 +0000497
95630366963296f62025-06-02 21:16:32 +0800498 tx.commit();
499 logger.info("Successfully settled {} expired beg requests",
500 expiredBegs.size());
Haotian W88568d82025-05-15 16:15:21 +0000501
95630366963296f62025-06-02 21:16:32 +0800502 } catch (Exception e) {
503 if (tx != null && tx.isActive()) {
504 tx.rollback();
505 }
506 logger.error("Error settling beg requests: {}", e.getMessage(), e);
507 } finally {
508 if (em != null) {
509 em.close();
510 }
511 }
512 }
Haotian W88568d82025-05-15 16:15:21 +0000513
95630366963296f62025-06-02 21:16:32 +0800514 @Override
515 public int AddPost(Post post) {
516 if (post == null || post.postid == null || post.postid.isEmpty()) {
517 logger.warn("Invalid parameter: post is null or postid is empty");
518 return 2;
519 }
Haotian W88568d82025-05-15 16:15:21 +0000520
95630366963296f62025-06-02 21:16:32 +0800521 EntityManager em = null;
522 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000523
95630366963296f62025-06-02 21:16:32 +0800524 try {
525 em = emf.createEntityManager();
526 tx = em.getTransaction();
527 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000528
95630366963296f62025-06-02 21:16:32 +0800529 // 检查是否重复
530 Post existingPost = em.find(Post.class, post.postid);
531 if (existingPost != null) {
532 logger.warn("Post with ID {} already exists", post.postid);
533 return 1;
534 }
Haotian W88568d82025-05-15 16:15:21 +0000535
95630366963296f62025-06-02 21:16:32 +0800536 // 检查用户是否存在
537 User user = em.find(User.class, post.postuserid);
538 if (user == null) {
539 logger.warn("User with ID {} does not exist", post.postuserid);
540 return 2;
541 }
Haotian W88568d82025-05-15 16:15:21 +0000542
95630366963296f62025-06-02 21:16:32 +0800543 // 设置初始值
544 if (post.posttime == null) {
545 post.posttime = new Date();
546 }
547 post.replytime = 0;
548 post.readtime = 0;
Haotian W88568d82025-05-15 16:15:21 +0000549
95630366963296f62025-06-02 21:16:32 +0800550 // 保存帖子
551 em.persist(post);
552 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000553
95630366963296f62025-06-02 21:16:32 +0800554 logger.info("Successfully added new post with ID: {}", post.postid);
555 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000556
95630366963296f62025-06-02 21:16:32 +0800557 } catch (Exception e) {
558 if (tx != null && tx.isActive()) {
559 tx.rollback();
560 }
561 logger.error("Error adding post: {}", e.getMessage());
562 return 2;
563 } finally {
564 if (em != null) {
565 em.close();
566 }
567 }
568 }
Haotian W88568d82025-05-15 16:15:21 +0000569
95630366963296f62025-06-02 21:16:32 +0800570 @Override
571 public int UpdatePost(Post post) {
572 if (post == null || post.postid == null || post.postid.isEmpty()) {
573 logger.warn("Invalid parameter: post is null or postid is empty");
574 return 2;
575 }
Haotian W88568d82025-05-15 16:15:21 +0000576
95630366963296f62025-06-02 21:16:32 +0800577 EntityManager em = null;
578 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000579
95630366963296f62025-06-02 21:16:32 +0800580 try {
581 em = emf.createEntityManager();
582 tx = em.getTransaction();
583 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000584
95630366963296f62025-06-02 21:16:32 +0800585 // 检查帖子是否存在
586 Post existingPost = em.find(Post.class, post.postid);
587 if (existingPost == null) {
588 logger.warn("Post with ID {} does not exist", post.postid);
589 return 1;
590 }
Haotian W88568d82025-05-15 16:15:21 +0000591
95630366963296f62025-06-02 21:16:32 +0800592 // 保持原有不可修改的字段
593 post.postuserid = existingPost.postuserid;
594 post.posttime = existingPost.posttime;
595 post.replytime = existingPost.replytime;
596 post.readtime = existingPost.readtime;
Haotian W88568d82025-05-15 16:15:21 +0000597
95630366963296f62025-06-02 21:16:32 +0800598 // 更新帖子
599 em.merge(post);
600 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000601
95630366963296f62025-06-02 21:16:32 +0800602 logger.info("Successfully updated post with ID: {}", post.postid);
603 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000604
95630366963296f62025-06-02 21:16:32 +0800605 } catch (Exception e) {
606 if (tx != null && tx.isActive()) {
607 tx.rollback();
608 }
609 logger.error("Error updating post: {}", e.getMessage());
610 return 2;
611 } finally {
612 if (em != null) {
613 em.close();
614 }
615 }
616 }
Haotian W88568d82025-05-15 16:15:21 +0000617
95630366963296f62025-06-02 21:16:32 +0800618 @Override
619 public int DeletePost(String postid) {
620 if (postid == null || postid.isEmpty()) {
621 logger.warn("Invalid parameter: postid is null or empty");
622 return 2;
623 }
Haotian W88568d82025-05-15 16:15:21 +0000624
95630366963296f62025-06-02 21:16:32 +0800625 EntityManager em = null;
626 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000627
95630366963296f62025-06-02 21:16:32 +0800628 try {
629 em = emf.createEntityManager();
630 tx = em.getTransaction();
631 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000632
95630366963296f62025-06-02 21:16:32 +0800633 // 查找要删除的帖子
634 Post post = em.find(Post.class, postid);
635 if (post == null) {
636 logger.warn("Post with ID {} does not exist", postid);
637 tx.rollback();
638 return 1;
639 }
Haotian W88568d82025-05-15 16:15:21 +0000640
95630366963296f62025-06-02 21:16:32 +0800641 // 删除帖子(由于设置了级联删除,相关的回复会自动删除)
642 em.remove(post);
643 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000644
95630366963296f62025-06-02 21:16:32 +0800645 logger.info("Successfully deleted post with ID: {}", postid);
646 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000647
95630366963296f62025-06-02 21:16:32 +0800648 } catch (Exception e) {
649 if (tx != null && tx.isActive()) {
650 tx.rollback();
651 }
652 logger.error("Error deleting post: {}", e.getMessage());
653 return 2;
654 } finally {
655 if (em != null) {
656 em.close();
657 }
658 }
659 }
Haotian W88568d82025-05-15 16:15:21 +0000660
95630366963296f62025-06-02 21:16:32 +0800661 @Override
662 public int AddComment(String postid, String userid, String comment) {
663 if (postid == null || postid.isEmpty() ||
664 userid == null || userid.isEmpty() ||
665 comment == null || comment.isEmpty()) {
666 logger.warn("Invalid parameters: postid, userid or comment is null or empty");
667 return 2;
668 }
Haotian W88568d82025-05-15 16:15:21 +0000669
95630366963296f62025-06-02 21:16:32 +0800670 EntityManager em = null;
671 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000672
95630366963296f62025-06-02 21:16:32 +0800673 try {
674 em = emf.createEntityManager();
675 tx = em.getTransaction();
676 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000677
95630366963296f62025-06-02 21:16:32 +0800678 // 检查帖子是否存在
679 Post post = em.find(Post.class, postid);
680 if (post == null) {
681 logger.warn("Post with ID {} does not exist", postid);
682 return 1;
683 }
Haotian W88568d82025-05-15 16:15:21 +0000684
95630366963296f62025-06-02 21:16:32 +0800685 // 检查评论用户是否存在
686 User user = em.find(User.class, userid);
687 if (user == null) {
688 logger.warn("User with ID {} does not exist", userid);
689 return 1;
690 }
Haotian W88568d82025-05-15 16:15:21 +0000691
95630366963296f62025-06-02 21:16:32 +0800692 // 创建新的回复
693 PostReply reply = new PostReply();
694 reply.replyid = UUID.randomUUID().toString();
695 reply.postid = postid;
696 reply.content = comment;
697 reply.authorid = userid;
698 reply.createdAt = new Date();
Haotian W88568d82025-05-15 16:15:21 +0000699
95630366963296f62025-06-02 21:16:32 +0800700 // 保存回复
701 em.persist(reply);
Haotian W88568d82025-05-15 16:15:21 +0000702
95630366963296f62025-06-02 21:16:32 +0800703 // 更新帖子的回复数
704 post.replytime += 1;
705 em.merge(post);
Haotian W88568d82025-05-15 16:15:21 +0000706
95630366963296f62025-06-02 21:16:32 +0800707 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000708
95630366963296f62025-06-02 21:16:32 +0800709 logger.info("Successfully added comment by user {} to post {}, reply ID: {}",
710 userid, postid, reply.replyid);
711 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000712
95630366963296f62025-06-02 21:16:32 +0800713 } catch (Exception e) {
714 if (tx != null && tx.isActive()) {
715 tx.rollback();
716 }
717 logger.error("Error adding comment: {}", e.getMessage());
718 return 2;
719 } finally {
720 if (em != null) {
721 em.close();
722 }
723 }
724 }
Haotian W88568d82025-05-15 16:15:21 +0000725
95630366963296f62025-06-02 21:16:32 +0800726 @Override
727 public int DeleteComment(String postid, String commentid) {
728 if (postid == null || postid.isEmpty() || commentid == null || commentid.isEmpty()) {
729 logger.warn("Invalid parameters: postid or commentid is null or empty");
730 return 2;
731 }
Haotian W88568d82025-05-15 16:15:21 +0000732
95630366963296f62025-06-02 21:16:32 +0800733 EntityManager em = null;
734 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000735
95630366963296f62025-06-02 21:16:32 +0800736 try {
737 em = emf.createEntityManager();
738 tx = em.getTransaction();
739 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000740
95630366963296f62025-06-02 21:16:32 +0800741 // 检查帖子是否存在
742 Post post = em.find(Post.class, postid);
743 if (post == null) {
744 logger.warn("Post with ID {} does not exist", postid);
745 return 1;
746 }
Haotian W88568d82025-05-15 16:15:21 +0000747
95630366963296f62025-06-02 21:16:32 +0800748 // 检查评论是否存在且属于该帖子
749 PostReply reply = em.find(PostReply.class, commentid);
750 if (reply == null || !reply.postid.equals(postid)) {
751 logger.warn("Comment {} does not exist or does not belong to post {}", commentid, postid);
752 return 1;
753 }
Haotian W88568d82025-05-15 16:15:21 +0000754
95630366963296f62025-06-02 21:16:32 +0800755 // 删除评论
756 em.remove(reply);
Haotian W88568d82025-05-15 16:15:21 +0000757
95630366963296f62025-06-02 21:16:32 +0800758 // 更新帖子的回复数
759 post.replytime = Math.max(0, post.replytime - 1);
760 em.merge(post);
Haotian W88568d82025-05-15 16:15:21 +0000761
95630366963296f62025-06-02 21:16:32 +0800762 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000763
95630366963296f62025-06-02 21:16:32 +0800764 logger.info("Successfully deleted comment {} from post {}", commentid, postid);
765 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000766
95630366963296f62025-06-02 21:16:32 +0800767 } catch (Exception e) {
768 if (tx != null && tx.isActive()) {
769 tx.rollback();
770 }
771 logger.error("Error deleting comment: {}", e.getMessage());
772 return 2;
773 } finally {
774 if (em != null) {
775 em.close();
776 }
777 }
778 }
Haotian W88568d82025-05-15 16:15:21 +0000779
95630366963296f62025-06-02 21:16:32 +0800780 @Override
781 public boolean ExchangeMagicToUpload(String userid, int magic) {
782 if (userid == null || userid.isEmpty() || magic <= 0) {
783 logger.warn("参数无效: userid为空或magic <= 0");
784 return false;
785 }
Haotian W88568d82025-05-15 16:15:21 +0000786
95630366963296f62025-06-02 21:16:32 +0800787 EntityManager em = null;
788 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000789
95630366963296f62025-06-02 21:16:32 +0800790 try {
791 em = emf.createEntityManager();
792 tx = em.getTransaction();
793 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000794
95630366963296f62025-06-02 21:16:32 +0800795 UserPT userPT = em.find(UserPT.class, userid);
796 if (userPT == null) {
797 logger.warn("未找到用户 {} 的PT信息", userid);
798 return false;
799 }
Haotian W88568d82025-05-15 16:15:21 +0000800
95630366963296f62025-06-02 21:16:32 +0800801 if (userPT.magic < magic) {
802 logger.warn("用户 {} 的魔力值不足", userid);
803 return false;
804 }
Haotian W88568d82025-05-15 16:15:21 +0000805
95630366963296f62025-06-02 21:16:32 +0800806 // 1:1兑换,直接加上魔力值(假设单位是MB)
807 userPT.magic -= magic;
808 userPT.upload += magic;
Haotian W88568d82025-05-15 16:15:21 +0000809
95630366963296f62025-06-02 21:16:32 +0800810 // 更新分享率
811 if (userPT.download > 0) {
812 userPT.share = (double) userPT.upload / userPT.download;
813 }
Haotian W88568d82025-05-15 16:15:21 +0000814
95630366963296f62025-06-02 21:16:32 +0800815 em.merge(userPT);
816 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000817
95630366963296f62025-06-02 21:16:32 +0800818 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 上传量", userid, magic, magic);
819 return true;
Haotian W88568d82025-05-15 16:15:21 +0000820
95630366963296f62025-06-02 21:16:32 +0800821 } catch (Exception e) {
822 if (tx != null && tx.isActive()) {
823 tx.rollback();
824 }
825 logger.error("魔力值兑换上传量时发生错误: {}", e.getMessage());
826 return false;
827 } finally {
828 if (em != null) {
829 em.close();
830 }
831 }
832 }
Haotian W88568d82025-05-15 16:15:21 +0000833
95630366963296f62025-06-02 21:16:32 +0800834 @Override
835 public boolean ExchangeMagicToDownload(String userid, int magic) {
836 if (userid == null || userid.isEmpty() || magic <= 0) {
837 logger.warn("参数无效: userid为空或magic <= 0");
838 return false;
839 }
Haotian W88568d82025-05-15 16:15:21 +0000840
95630366963296f62025-06-02 21:16:32 +0800841 EntityManager em = null;
842 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000843
95630366963296f62025-06-02 21:16:32 +0800844 try {
845 em = emf.createEntityManager();
846 tx = em.getTransaction();
847 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000848
95630366963296f62025-06-02 21:16:32 +0800849 UserPT userPT = em.find(UserPT.class, userid);
850 if (userPT == null) {
851 logger.warn("未找到用户 {} 的PT信息", userid);
852 return false;
853 }
Haotian W88568d82025-05-15 16:15:21 +0000854
95630366963296f62025-06-02 21:16:32 +0800855 if (userPT.magic < magic) {
856 logger.warn("用户 {} 的魔力值不足", userid);
857 return false;
858 }
Haotian W88568d82025-05-15 16:15:21 +0000859
95630366963296f62025-06-02 21:16:32 +0800860 // 1:1兑换,直接减去魔力值对应的下载量(假设单位是MB)
861 userPT.magic -= magic;
862 userPT.download = Math.max(0, userPT.download - magic);
Haotian W88568d82025-05-15 16:15:21 +0000863
95630366963296f62025-06-02 21:16:32 +0800864 // 更新分享率
865 if (userPT.download > 0) {
866 userPT.share = (double) userPT.upload / userPT.download;
867 }
Haotian W88568d82025-05-15 16:15:21 +0000868
95630366963296f62025-06-02 21:16:32 +0800869 em.merge(userPT);
870 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000871
95630366963296f62025-06-02 21:16:32 +0800872 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 下载量减免", userid, magic, magic);
873 return true;
Haotian W88568d82025-05-15 16:15:21 +0000874
95630366963296f62025-06-02 21:16:32 +0800875 } catch (Exception e) {
876 if (tx != null && tx.isActive()) {
877 tx.rollback();
878 }
879 logger.error("魔力值兑换下载量时发生错误: {}", e.getMessage());
880 return false;
881 } finally {
882 if (em != null) {
883 em.close();
884 }
885 }
886 }
Haotian W88568d82025-05-15 16:15:21 +0000887
95630366963296f62025-06-02 21:16:32 +0800888 @Override
889 public boolean ExchangeMagicToVip(String userid, int magic) {
890 if (userid == null || userid.isEmpty() || magic <= 0) {
891 logger.warn("参数无效: userid为空或magic <= 0");
892 return false;
893 }
Haotian W88568d82025-05-15 16:15:21 +0000894
95630366963296f62025-06-02 21:16:32 +0800895 EntityManager em = null;
896 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000897
95630366963296f62025-06-02 21:16:32 +0800898 try {
899 em = emf.createEntityManager();
900 tx = em.getTransaction();
901 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000902
95630366963296f62025-06-02 21:16:32 +0800903 UserPT userPT = em.find(UserPT.class, userid);
904 if (userPT == null) {
905 logger.warn("未找到用户 {} 的PT信息", userid);
906 return false;
907 }
Haotian W88568d82025-05-15 16:15:21 +0000908
95630366963296f62025-06-02 21:16:32 +0800909 if (userPT.magic < magic) {
910 logger.warn("用户 {} 的魔力值不足", userid);
911 return false;
912 }
Haotian W88568d82025-05-15 16:15:21 +0000913
95630366963296f62025-06-02 21:16:32 +0800914 // 1:1兑换VIP下载次数
915 userPT.magic -= magic;
916 userPT.viptime += magic;
Haotian W88568d82025-05-15 16:15:21 +0000917
95630366963296f62025-06-02 21:16:32 +0800918 em.merge(userPT);
919 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000920
95630366963296f62025-06-02 21:16:32 +0800921 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {} 次VIP下载次数", userid, magic, magic);
922 return true;
Haotian W88568d82025-05-15 16:15:21 +0000923
95630366963296f62025-06-02 21:16:32 +0800924 } catch (Exception e) {
925 if (tx != null && tx.isActive()) {
926 tx.rollback();
927 }
928 logger.error("魔力值兑换VIP下载次数时发生错误: {}", e.getMessage());
929 return false;
930 } finally {
931 if (em != null) {
932 em.close();
933 }
934 }
935 }
Haotian W88568d82025-05-15 16:15:21 +0000936
95630366963296f62025-06-02 21:16:32 +0800937 @Override
938 public boolean UploadTransmitProfile(Profile profile) {
939 if (profile == null || profile.profileurl == null || profile.profileurl.isEmpty() ||
940 profile.userid == null || profile.userid.isEmpty()) {
941 logger.warn("参数无效: profile为空或必要字段为空");
942 return false;
943 }
Haotian W88568d82025-05-15 16:15:21 +0000944
95630366963296f62025-06-02 21:16:32 +0800945 EntityManager em = null;
946 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000947
95630366963296f62025-06-02 21:16:32 +0800948 try {
949 em = emf.createEntityManager();
950 tx = em.getTransaction();
951 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000952
95630366963296f62025-06-02 21:16:32 +0800953 // 检查用户是否存在
954 User user = em.find(User.class, profile.userid);
955 if (user == null) {
956 logger.warn("用户 {} 不存在", profile.userid);
957 return false;
958 }
Haotian W88568d82025-05-15 16:15:21 +0000959
95630366963296f62025-06-02 21:16:32 +0800960 // 检查是否已存在相同的迁移申请
961 Profile existingProfile = em.find(Profile.class, profile.profileurl);
962 if (existingProfile != null) {
963 logger.warn("迁移申请 {} 已存在", profile.profileurl);
964 return false;
965 }
Haotian W88568d82025-05-15 16:15:21 +0000966
95630366963296f62025-06-02 21:16:32 +0800967 // 设置初始值
968 profile.exampass = false;
969 profile.magicgived = "0";
970 profile.uploadgived = "0";
Haotian W88568d82025-05-15 16:15:21 +0000971
95630366963296f62025-06-02 21:16:32 +0800972 // 保存迁移申请
973 em.persist(profile);
974 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000975
95630366963296f62025-06-02 21:16:32 +0800976 logger.info("成功上传迁移申请 {}", profile.profileurl);
977 return true;
Haotian W88568d82025-05-15 16:15:21 +0000978
95630366963296f62025-06-02 21:16:32 +0800979 } catch (Exception e) {
980 if (tx != null && tx.isActive()) {
981 tx.rollback();
982 }
983 logger.error("上传迁移申请时发生错误: {}", e.getMessage());
984 return false;
985 } finally {
986 if (em != null) {
987 em.close();
988 }
989 }
990 }
Haotian W88568d82025-05-15 16:15:21 +0000991
95630366963296f62025-06-02 21:16:32 +0800992 @Override
993 public Profile GetTransmitProfile(String profileid) {
994 if (profileid == null || profileid.isEmpty()) {
995 logger.warn("参数无效: profileid为空");
996 return null;
997 }
Haotian W88568d82025-05-15 16:15:21 +0000998
95630366963296f62025-06-02 21:16:32 +0800999 EntityManager em = null;
Haotian W88568d82025-05-15 16:15:21 +00001000
95630366963296f62025-06-02 21:16:32 +08001001 try {
1002 em = emf.createEntityManager();
1003 Profile profile = em.find(Profile.class, profileid);
1004 if (profile == null) {
1005 logger.warn("未找到迁移申请 {}", profileid);
1006 return null;
1007 }
Haotian W88568d82025-05-15 16:15:21 +00001008
95630366963296f62025-06-02 21:16:32 +08001009 logger.info("成功获取迁移申请 {}", profileid);
1010 return profile;
Haotian W88568d82025-05-15 16:15:21 +00001011
95630366963296f62025-06-02 21:16:32 +08001012 } catch (Exception e) {
1013 logger.error("获取迁移申请时发生错误: {}", e.getMessage());
1014 return null;
1015 } finally {
1016 if (em != null) {
1017 em.close();
1018 }
1019 }
1020 }
Haotian W88568d82025-05-15 16:15:21 +00001021
95630366963296f62025-06-02 21:16:32 +08001022 @Override
1023 public boolean ExamTransmitProfile(String profileid, boolean result) {
1024 if (profileid == null || profileid.isEmpty()) {
1025 logger.warn("参数无效: profileid为空");
1026 return false;
1027 }
Haotian W88568d82025-05-15 16:15:21 +00001028
95630366963296f62025-06-02 21:16:32 +08001029 EntityManager em = null;
1030 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +00001031
95630366963296f62025-06-02 21:16:32 +08001032 try {
1033 em = emf.createEntityManager();
1034 tx = em.getTransaction();
1035 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +00001036
95630366963296f62025-06-02 21:16:32 +08001037 // 查找迁移申请
1038 Profile profile = em.find(Profile.class, profileid);
1039 if (profile == null) {
1040 logger.warn("未找到迁移申请 {}", profileid);
1041 return false;
1042 }
Haotian W88568d82025-05-15 16:15:21 +00001043
95630366963296f62025-06-02 21:16:32 +08001044 // 更新审核状态
1045 profile.exampass = result;
Haotian W88568d82025-05-15 16:15:21 +00001046
95630366963296f62025-06-02 21:16:32 +08001047 if (result) {
1048 // 如果审核通过,更新用户的PT信息
1049 UserPT userPT = em.find(UserPT.class, profile.userid);
1050 if (userPT != null) {
1051 // 发放魔力值
1052 int magicToGive = Integer.parseInt(profile.magictogive);
1053 userPT.magic += magicToGive;
1054 profile.magicgived = String.valueOf(magicToGive);
Haotian W88568d82025-05-15 16:15:21 +00001055
95630366963296f62025-06-02 21:16:32 +08001056 // 发放上传量
1057 long uploadToGive = Long.parseLong(profile.uploadtogive);
1058 userPT.upload += uploadToGive;
1059 profile.uploadgived = String.valueOf(uploadToGive);
Haotian W88568d82025-05-15 16:15:21 +00001060
95630366963296f62025-06-02 21:16:32 +08001061 em.merge(userPT);
1062 }
1063 }
Haotian W88568d82025-05-15 16:15:21 +00001064
95630366963296f62025-06-02 21:16:32 +08001065 em.merge(profile);
1066 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +00001067
95630366963296f62025-06-02 21:16:32 +08001068 logger.info("成功审核迁移申请 {}, 结果: {}", profileid, result);
1069 return true;
Haotian W88568d82025-05-15 16:15:21 +00001070
95630366963296f62025-06-02 21:16:32 +08001071 } catch (Exception e) {
1072 if (tx != null && tx.isActive()) {
1073 tx.rollback();
1074 }
1075 logger.error("审核迁移申请时发生错误: {}", e.getMessage());
1076 return false;
1077 } finally {
1078 if (em != null) {
1079 em.close();
1080 }
1081 }
1082 }
Haotian W88568d82025-05-15 16:15:21 +00001083
95630366963296f62025-06-02 21:16:32 +08001084 @Override
1085 public Profile[] GetTransmitProfileList() {
1086 EntityManager em = null;
Haotian W88568d82025-05-15 16:15:21 +00001087
95630366963296f62025-06-02 21:16:32 +08001088 try {
1089 em = emf.createEntityManager();
Haotian W88568d82025-05-15 16:15:21 +00001090
95630366963296f62025-06-02 21:16:32 +08001091 // 获取所有迁移申请
1092 QProfile p = QProfile.profile;
1093 List<Profile> profiles = new JPAQuery<>(em)
1094 .select(p)
1095 .from(p)
1096 .fetch();
Haotian W88568d82025-05-15 16:15:21 +00001097
95630366963296f62025-06-02 21:16:32 +08001098 logger.info("成功获取所有迁移申请,共 {} 条", profiles.size());
1099 return profiles.toArray(new Profile[0]);
Haotian W88568d82025-05-15 16:15:21 +00001100
95630366963296f62025-06-02 21:16:32 +08001101 } catch (Exception e) {
1102 logger.error("获取迁移申请列表时发生错误: {}", e.getMessage());
1103 return new Profile[0];
1104 } finally {
1105 if (em != null) {
1106 em.close();
1107 }
1108 }
1109 }
rhj46f62c42025-06-06 23:24:10 +08001110
1111 @Override
1112 public Post[] GetPostList(){
1113 return null;
1114 }
rhj5b69b7e2025-06-07 01:28:08 +08001115
1116 @Override
1117 public Post GetPost(String postid) {
1118 return null;
1119 }
1120
1121 @Override
1122 public PostReply[] GetPostReplyList(String postid) {
1123 return null;
1124 }
rhj5ebd93c2025-06-07 15:57:28 +08001125
1126 @Override
1127 public Post[] SearchPost(String userQ) {
1128 return null;
1129 }
rhje18c3f72025-06-08 00:27:01 +08001130
1131 @Override
1132 public int CheckAdmin(String userid) {
1133 return 0;
1134 }
1135
1136 @Override
1137 public int InviteNewUser(String inviterid, String invitedemail) {
1138 return 0;
1139 }
1140
1141 @Override
1142 public UserStar[] GetUserStarList(String userid) {
1143 return new UserStar[0];
1144 }
1145
1146 @Override
1147 public int UploadMigration(String userid, File file){
1148 return 0;
1149 }
1150
1151 @Override
1152 public BegSeedDetail[] GetBegList() {
1153 EntityManager em = null;
1154 try {
1155 em = emf.createEntityManager();
1156
1157 JPAQuery<BegInfo> query = new JPAQuery<>(em);
1158 QBegInfo b = QBegInfo.begInfo;
1159 List<BegInfo> begList = query.select(b).from(b).fetch();
1160
1161 List<BegSeedDetail> begSeedDetailList = new ArrayList<>();
1162 for (BegInfo begInfo : begList) {
1163 // 查询对应的BegInfo表的Info字段
1164 BegInfoDetail begInfoDetail = em.find(BegInfoDetail.class, begInfo.begid);
1165 // 构造BegSeedDetail对象
1166 BegSeedDetail begSeedDetail = new BegSeedDetail(begInfo, begInfoDetail);
1167 begSeedDetailList.add(begSeedDetail);
1168 }
1169
1170 return begSeedDetailList.toArray(new BegSeedDetail[0]);
1171 } catch (Exception e) {
1172 logger.error("Error getting BegList: {}", e.getMessage(), e);
1173 return new BegSeedDetail[0];
1174 } finally {
1175 if (em != null) {
1176 em.close();
1177 }
1178 }
1179 }
1180
1181 @Override
1182 public BegInfo GetBegDetail(String begid) {
1183 return null;
1184 }
1185
1186 @Override
1187 public BegSeedDetail GetBegSeedDetail(String begid) {
1188 if (begid == null || begid.isEmpty()) {
1189 return null;
1190 }
1191
1192 EntityManager em = null;
1193 try {
1194 em = emf.createEntityManager();
1195
1196 // 查询 BegSeed 表数据
1197 JPAQuery<BegInfo> begQuery = new JPAQuery<>(em);
1198 QBegInfo b = QBegInfo.begInfo;
1199 BegInfo begInfo = begQuery.select(b).from(b).where(b.begid.eq(begid)).fetchOne();
1200
1201 if (begInfo == null) {
1202 return null;
1203 }
1204
1205 // 查询 BegInfo 表的 Info 字段
1206 BegInfoDetail begInfoDetail = em.find(BegInfoDetail.class, begid);
1207
1208 // 构造返回对象
1209 return new BegSeedDetail(begInfo, begInfoDetail);
1210 } catch (Exception e) {
1211 logger.error("Error getting BegSeedDetail: {}", e.getMessage(), e);
1212 return null;
1213 } finally {
1214 if (em != null) {
1215 em.close();
1216 }
1217 }
1218 }
1219
1220 @Override
1221 public SeedWithVotes[] GetBegSeedListWithVotes(String begid) {
1222 return new SeedWithVotes[0];
1223 }
1224
1225 @Override
1226 public int SubmitBegSeed(String begid, String seedid, String userid) {
1227 return 0;
1228 }
1229
1230 @Override
1231 public int createBagSeed(BegInfo begInfo, String userid, String info) {
1232 return 0;
1233 }
95630366963296f62025-06-02 21:16:32 +08001234}