blob: 3fe0c9c7d270764818efe0b498965579018f6939 [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;
rhj7912f2c2025-06-09 17:21:32 +080025import entity.SeedPromotion;
26import entity.SeedWithPromotionDTO;
rootff0769a2025-05-18 17:24:41 +000027import entity.User;
28import entity.UserPT;
95630366963296f62025-06-02 21:16:32 +080029import entity.config;
rootff0769a2025-05-18 17:24:41 +000030import entity.Notice;
31import entity.BegInfo;
rhje18c3f72025-06-08 00:27:01 +080032import entity.BegInfoDetail;
33import entity.BegSeedDetail;
rootff0769a2025-05-18 17:24:41 +000034import entity.Post;
35import entity.PostReply;
36import entity.Profile;
rhje18c3f72025-06-08 00:27:01 +080037import entity.UserStar;
38import entity.SeedWithVotes;
Haotian W88568d82025-05-15 16:15:21 +000039
40import org.slf4j.Logger;
41import org.slf4j.LoggerFactory;
42
95630366963296f62025-06-02 21:16:32 +080043public class Database2 implements DataManagerInterface {
Haotian W88568d82025-05-15 16:15:21 +000044
95630366963296f62025-06-02 21:16:32 +080045 private EntityManagerFactory emf;
46 private static final Logger logger = LoggerFactory.getLogger(Database2.class);
Haotian W88568d82025-05-15 16:15:21 +000047
95630366963296f62025-06-02 21:16:32 +080048 // 构造函数,初始化EntityManagerFactory
49 public Database2() {
50 config cfg = new config();
51 Map<String, Object> props = new HashMap<>();
52 props.put("javax.persistence.jdbc.url",
53 "jdbc:mysql://" + cfg.SqlURL + "/" + cfg.Database);
54 props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
55 props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
56 this.emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
57 }
Haotian W88568d82025-05-15 16:15:21 +000058
95630366963296f62025-06-02 21:16:32 +080059 public Database2(EntityManagerFactory emf) {
60 this.emf = emf;
61 }
root33a7d952025-05-18 17:24:41 +000062
95630366963296f62025-06-02 21:16:32 +080063 @Override
TRM-codingcdfe5482025-06-06 17:31:01 +080064 public String LoginUser(User userinfo){
65 return null;
66 }
67
68 @Override
95630366963296f62025-06-02 21:16:32 +080069 public int RegisterUser(User userinfo) {
70 return 0;
71 }
root33a7d952025-05-18 17:24:41 +000072
95630366963296f62025-06-02 21:16:32 +080073 @Override
74 public int UpdateInformation(User userinfo) {
75 return 0;
76 }
root33a7d952025-05-18 17:24:41 +000077
95630366963296f62025-06-02 21:16:32 +080078 @Override
79 public User GetInformation(String userid) {
80 return null;
81 }
root33a7d952025-05-18 17:24:41 +000082
95630366963296f62025-06-02 21:16:32 +080083 @Override
84 public UserPT GetInformationPT(String userid) {
85 return null;
86 }
root33a7d952025-05-18 17:24:41 +000087
95630366963296f62025-06-02 21:16:32 +080088 @Override
89 public int UpdateInformationPT(UserPT userinfo) {
90 return 0;
91 }
root33a7d952025-05-18 17:24:41 +000092
95630366963296f62025-06-02 21:16:32 +080093 @Override
94 public int RegisterUserPT(UserPT userinfo) {
95 return 0;
96 }
root33a7d952025-05-18 17:24:41 +000097
95630366963296f62025-06-02 21:16:32 +080098 @Override
99 public Seed GetSeedInformation(String seedid) {
100 return null;
101 }
root33a7d952025-05-18 17:24:41 +0000102
95630366963296f62025-06-02 21:16:32 +0800103 @Override
104 public int RegisterSeed(Seed seedinfo) {
105 return 0;
106 }
root33a7d952025-05-18 17:24:41 +0000107
95630366963296f62025-06-02 21:16:32 +0800108 @Override
109 public int UpdateSeed(Seed seedinfo) {
110 return 0;
111 }
root33a7d952025-05-18 17:24:41 +0000112
95630366963296f62025-06-02 21:16:32 +0800113 @Override
114 public Seed[] SearchSeed(String userQ) {
115 return new Seed[0];
116 }
root33a7d952025-05-18 17:24:41 +0000117
95630366963296f62025-06-02 21:16:32 +0800118 @Override
119 public int AddNotice(Notice notice) {
120 return 0;
121 }
root33a7d952025-05-18 17:24:41 +0000122
95630366963296f62025-06-02 21:16:32 +0800123 @Override
124 public boolean UpdateNotice(Notice notice) {
125 return false;
126 }
root33a7d952025-05-18 17:24:41 +0000127
95630366963296f62025-06-02 21:16:32 +0800128 @Override
129 public boolean DeleteNotice(String noticeid) {
130 return false;
131 }
root33a7d952025-05-18 17:24:41 +0000132
95630366963296f62025-06-02 21:16:32 +0800133 @Override
134 public int GetUserAvailableInviteTimes(String userid) {
135 return 0;
136 }
root33a7d952025-05-18 17:24:41 +0000137
95630366963296f62025-06-02 21:16:32 +0800138 @Override
139 public int InviteUser(String inviterid, String inviteemail) {
140 return 0;
141 }
root33a7d952025-05-18 17:24:41 +0000142
95630366963296f62025-06-02 21:16:32 +0800143 @Override
rhj7912f2c2025-06-09 17:21:32 +0800144 public SeedWithPromotionDTO[] GetSeedListByTag(String tag) {
145 return new SeedWithPromotionDTO[0];
Raveraae06122025-06-05 08:13:35 +0000146 }
147
148 @Override
rhjc6a4ee02025-06-06 00:45:18 +0800149 public Seed[] GetSeedListByUser(String userid) {
150 return new Seed[0];
151 }
152
153 @Override
154 public int DeleteSeed(String seedid) {
155 return 0;
156 }
157
158 @Override
95630366963296f62025-06-02 21:16:32 +0800159 public boolean AddCollect(String userid, String postid) {
160 return false;
161 }
root33a7d952025-05-18 17:24:41 +0000162
95630366963296f62025-06-02 21:16:32 +0800163 @Override
164 public boolean DeleteCollect(String userid, String postid) {
165 return false;
166 }
Haotian W88568d82025-05-15 16:15:21 +0000167
95630366963296f62025-06-02 21:16:32 +0800168 @Override
169 public int AddBegSeed(BegInfo info) {
170 if (info == null || info.begid == null || info.begid.isEmpty()) {
171 logger.warn("Invalid parameter: info is null or begid is empty");
172 return 2;
173 }
Haotian W88568d82025-05-15 16:15:21 +0000174
95630366963296f62025-06-02 21:16:32 +0800175 EntityManager em = null;
176 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000177
95630366963296f62025-06-02 21:16:32 +0800178 try {
179 em = emf.createEntityManager();
180 tx = em.getTransaction();
181 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000182
95630366963296f62025-06-02 21:16:32 +0800183 // 检查是否重复
184 BegInfo existingBeg = em.find(BegInfo.class, info.begid);
185 if (existingBeg != null) {
186 logger.warn("BegSeed with ID {} already exists", info.begid);
187 return 1;
188 }
Haotian W88568d82025-05-15 16:15:21 +0000189
95630366963296f62025-06-02 21:16:32 +0800190 // 设置默认值
191 if (info.endtime == null) {
192 // 设置默认14天截止期
193 Calendar calendar = Calendar.getInstance();
194 calendar.add(Calendar.DAY_OF_MONTH, 14);
195 info.endtime = calendar.getTime();
196 }
197 info.hasseed = 0;
Haotian W88568d82025-05-15 16:15:21 +0000198
95630366963296f62025-06-02 21:16:32 +0800199 // 保存新的求种信息
200 em.persist(info);
201 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000202
95630366963296f62025-06-02 21:16:32 +0800203 logger.info("Successfully added new BegSeed with ID: {}", info.begid);
204 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000205
95630366963296f62025-06-02 21:16:32 +0800206 } catch (Exception e) {
207 if (tx != null && tx.isActive()) {
208 tx.rollback();
209 }
210 logger.error("Error adding BegSeed: {}", e.getMessage());
211 return 2;
212 } finally {
213 if (em != null) {
214 em.close();
215 }
216 }
217 }
Haotian W88568d82025-05-15 16:15:21 +0000218
95630366963296f62025-06-02 21:16:32 +0800219 @Override
220 public int UpdateBegSeed(BegInfo info) {
221 if (info == null || info.begid == null || info.begid.isEmpty()) {
222 logger.warn("Invalid parameter: info is null or begid is empty");
223 return 2;
224 }
Haotian W88568d82025-05-15 16:15:21 +0000225
95630366963296f62025-06-02 21:16:32 +0800226 EntityManager em = null;
227 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000228
95630366963296f62025-06-02 21:16:32 +0800229 try {
230 em = emf.createEntityManager();
231 tx = em.getTransaction();
232 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000233
95630366963296f62025-06-02 21:16:32 +0800234 // 检查是否存在
235 BegInfo existingBeg = em.find(BegInfo.class, info.begid);
236 if (existingBeg == null) {
237 logger.warn("BegSeed with ID {} does not exist", info.begid);
238 return 1;
239 }
Haotian W88568d82025-05-15 16:15:21 +0000240
95630366963296f62025-06-02 21:16:32 +0800241 // 保持原有值不变的字段
242 info.hasseed = existingBeg.hasseed;
243 if (info.endtime == null) {
244 info.endtime = existingBeg.endtime;
245 }
Haotian W88568d82025-05-15 16:15:21 +0000246
95630366963296f62025-06-02 21:16:32 +0800247 // 更新求种信息
248 em.merge(info);
249 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000250
95630366963296f62025-06-02 21:16:32 +0800251 logger.info("Successfully updated BegSeed with ID: {}", info.begid);
252 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000253
95630366963296f62025-06-02 21:16:32 +0800254 } catch (Exception e) {
255 if (tx != null && tx.isActive()) {
256 tx.rollback();
257 }
258 logger.error("Error updating BegSeed: {}", e.getMessage());
259 return 2;
260 } finally {
261 if (em != null) {
262 em.close();
263 }
264 }
265 }
Haotian W88568d82025-05-15 16:15:21 +0000266
95630366963296f62025-06-02 21:16:32 +0800267 @Override
268 public int DeleteBegSeed(String begid) {
269 if (begid == null || begid.isEmpty()) {
270 logger.warn("Invalid parameter: begid is null or empty");
271 return 2;
272 }
Haotian W88568d82025-05-15 16:15:21 +0000273
95630366963296f62025-06-02 21:16:32 +0800274 EntityManager em = null;
275 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000276
95630366963296f62025-06-02 21:16:32 +0800277 try {
278 em = emf.createEntityManager();
279 tx = em.getTransaction();
280 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000281
95630366963296f62025-06-02 21:16:32 +0800282 // 查找要删除的求种信息
283 BegInfo begInfo = em.find(BegInfo.class, begid);
284 if (begInfo == null) {
285 logger.warn("BegSeed with ID {} does not exist", begid);
286 tx.rollback();
287 return 1;
288 }
Haotian W88568d82025-05-15 16:15:21 +0000289
95630366963296f62025-06-02 21:16:32 +0800290 // 删除求种信息
291 em.remove(begInfo);
292 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000293
95630366963296f62025-06-02 21:16:32 +0800294 logger.info("Successfully deleted BegSeed with ID: {}", begid);
295 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000296
95630366963296f62025-06-02 21:16:32 +0800297 } catch (Exception e) {
298 if (tx != null && tx.isActive()) {
299 tx.rollback();
300 }
301 logger.error("Error deleting BegSeed: {}", e.getMessage());
302 return 2;
303 } finally {
304 if (em != null) {
305 em.close();
306 }
307 }
308 }
Haotian W88568d82025-05-15 16:15:21 +0000309
95630366963296f62025-06-02 21:16:32 +0800310 @Override
311 public int VoteSeed(String begId, String seedId, String userId) {
312 if (begId == null || seedId == null || userId == null ||
313 begId.isEmpty() || seedId.isEmpty() || userId.isEmpty()) {
314 logger.warn("Invalid parameters: begId, seedId or userId is null or empty");
315 return 2;
316 }
Haotian W88568d82025-05-15 16:15:21 +0000317
95630366963296f62025-06-02 21:16:32 +0800318 EntityManager em = null;
319 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000320
95630366963296f62025-06-02 21:16:32 +0800321 try {
322 em = emf.createEntityManager();
323 tx = em.getTransaction();
324 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000325
95630366963296f62025-06-02 21:16:32 +0800326 // 检查求种信息是否存在
327 BegInfo begInfo = em.find(BegInfo.class, begId);
328 if (begInfo == null) {
329 logger.warn("BegSeed with ID {} does not exist", begId);
330 return 2;
331 }
Haotian W88568d82025-05-15 16:15:21 +0000332
95630366963296f62025-06-02 21:16:32 +0800333 // 检查用户是否已投票
334 Long voteCount = new JPAQuery<>(em)
335 .select(QUserVotes.userVotes.count())
336 .from(QUserVotes.userVotes)
337 .where(QUserVotes.userVotes.id.eq(new entity.UserVotesId(userId, begId, seedId)))
338 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000339
95630366963296f62025-06-02 21:16:32 +0800340 if (voteCount != null && voteCount > 0) {
341 logger.warn("User {} has already voted for seed {} in beg {}", userId, seedId, begId);
342 return 1;
343 }
Haotian W88568d82025-05-15 16:15:21 +0000344
95630366963296f62025-06-02 21:16:32 +0800345 // 创建新的投票记录
346 em.createNativeQuery("INSERT INTO UserVotes (user_id, beg_id, seed_id, created_at) " +
347 "VALUES (?, ?, ?, CURRENT_TIMESTAMP)")
348 .setParameter(1, userId)
349 .setParameter(2, begId)
350 .setParameter(3, seedId)
351 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000352
95630366963296f62025-06-02 21:16:32 +0800353 // 更新SubmitSeed表中的投票数
354 em.createQuery("UPDATE SubmitSeed s SET s.votes = s.votes + 1 " +
355 "WHERE s.id.begId = :begId AND s.id.seedId = :seedId")
356 .setParameter("begId", begId)
357 .setParameter("seedId", seedId)
358 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000359
95630366963296f62025-06-02 21:16:32 +0800360 tx.commit();
361 logger.info("Successfully added vote from user {} for seed {} in beg {}", userId, seedId, begId);
362 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000363
95630366963296f62025-06-02 21:16:32 +0800364 } catch (Exception e) {
365 if (tx != null && tx.isActive()) {
366 tx.rollback();
367 }
368 logger.error("Error voting for seed: {}", e.getMessage());
369 return 2;
370 } finally {
371 if (em != null) {
372 em.close();
373 }
374 }
375 }
Haotian W88568d82025-05-15 16:15:21 +0000376
95630366963296f62025-06-02 21:16:32 +0800377 @Override
378 public int SubmitSeed(String begid, Seed seed) {
379 if (begid == null || seed == null || begid.isEmpty() || seed.seedid == null) {
380 logger.warn("Invalid parameters: begid or seed is null or empty");
381 return 2;
382 }
Haotian W88568d82025-05-15 16:15:21 +0000383
95630366963296f62025-06-02 21:16:32 +0800384 EntityManager em = null;
385 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000386
95630366963296f62025-06-02 21:16:32 +0800387 try {
388 em = emf.createEntityManager();
389 tx = em.getTransaction();
390 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000391
95630366963296f62025-06-02 21:16:32 +0800392 // 检查求种信息是否存在
393 BegInfo begInfo = em.find(BegInfo.class, begid);
394 if (begInfo == null) {
395 logger.warn("BegSeed with ID {} does not exist", begid);
396 return 2;
397 }
Haotian W88568d82025-05-15 16:15:21 +0000398
95630366963296f62025-06-02 21:16:32 +0800399 // 检查种子是否已提交过
400 QSubmitSeed ss = QSubmitSeed.submitSeed;
401 Long submitCount = new JPAQuery<>(em)
402 .select(ss.count())
403 .from(ss)
404 .where(ss.begInfo.begid.eq(begid))
405 .where(ss.seed.seedid.eq(seed.seedid))
406 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000407
95630366963296f62025-06-02 21:16:32 +0800408 if (submitCount > 0) {
409 logger.warn("Seed {} has already been submitted for beg {}", seed.seedid,
410 begid);
411 return 1;
412 }
Haotian W88568d82025-05-15 16:15:21 +0000413
95630366963296f62025-06-02 21:16:32 +0800414 // 保存种子信息(如果不存在)
415 if (em.find(Seed.class, seed.seedid) == null) {
416 em.persist(seed);
417 }
Haotian W88568d82025-05-15 16:15:21 +0000418
95630366963296f62025-06-02 21:16:32 +0800419 // 创建提交记录
420 em.createNativeQuery("INSERT INTO SubmitSeed (beg_id, seed_id, votes) VALUES (?, ?, 0)")
421 .setParameter(1, begid)
422 .setParameter(2, seed.seedid)
423 .executeUpdate();
Haotian W88568d82025-05-15 16:15:21 +0000424
95630366963296f62025-06-02 21:16:32 +0800425 tx.commit();
426 logger.info("Successfully submitted seed {} for beg {}", seed.seedid, begid);
427 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000428
95630366963296f62025-06-02 21:16:32 +0800429 } catch (Exception e) {
430 if (tx != null && tx.isActive()) {
431 tx.rollback();
432 }
433 logger.error("Error submitting seed: {}", e.getMessage());
434 return 2;
435 } finally {
436 if (em != null) {
437 em.close();
438 }
439 }
440 }
Haotian W88568d82025-05-15 16:15:21 +0000441
95630366963296f62025-06-02 21:16:32 +0800442 @Override
443 public void SettleBeg() {
444 EntityManager em = null;
445 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000446
95630366963296f62025-06-02 21:16:32 +0800447 try {
448 em = emf.createEntityManager();
449 tx = em.getTransaction();
450 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000451
95630366963296f62025-06-02 21:16:32 +0800452 // 1. 获取所有已过期且未完成的求种信息
453 QBegInfo b = QBegInfo.begInfo;
454 List<BegInfo> expiredBegs = new JPAQuery<>(em)
455 .select(b)
456 .from(b)
457 .where(b.endtime.loe(new Date())
458 .and(b.hasseed.eq(0)))
459 .fetch();
Haotian W88568d82025-05-15 16:15:21 +0000460
95630366963296f62025-06-02 21:16:32 +0800461 for (BegInfo beg : expiredBegs) {
462 // 2. 查找投票最多的提交任务
463 QSubmitSeed ss = QSubmitSeed.submitSeed;
464 Tuple topSubmission = new JPAQuery<>(em)
465 .select(ss.seed.seedid, ss.votes)
466 .from(ss)
467 .where(ss.begInfo.begid.eq(beg.begid))
468 .orderBy(ss.votes.desc())
469 .limit(1)
470 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000471
95630366963296f62025-06-02 21:16:32 +0800472 if (topSubmission != null && topSubmission.get(ss.votes) > 0) {
473 String seedId = topSubmission.get(ss.seed.seedid);
Haotian W88568d82025-05-15 16:15:21 +0000474
95630366963296f62025-06-02 21:16:32 +0800475 // 3. 获取上传者ID
476 QSeed s = QSeed.seed;
477 String ownerId = new JPAQuery<>(em)
478 .select(s.seeduserid)
479 .from(s)
480 .where(s.seedid.eq(seedId))
481 .fetchOne();
Haotian W88568d82025-05-15 16:15:21 +0000482
95630366963296f62025-06-02 21:16:32 +0800483 // 4. 获取上传者的PT信息并更新魔力值
484 UserPT ownerPT = em.find(UserPT.class, ownerId);
485 if (ownerPT != null) {
486 // 5. 发放奖励
487 ownerPT.magic += beg.magic;
488 em.merge(ownerPT);
Haotian W88568d82025-05-15 16:15:21 +0000489
95630366963296f62025-06-02 21:16:32 +0800490 // 6. 更新求种状态
491 beg.hasseed = 1;
492 em.merge(beg);
Haotian W88568d82025-05-15 16:15:21 +0000493
95630366963296f62025-06-02 21:16:32 +0800494 logger.info("Reward {} magic points awarded to user {} for beg {}",
495 beg.magic, ownerId, beg.begid);
496 }
497 }
498 }
Haotian W88568d82025-05-15 16:15:21 +0000499
95630366963296f62025-06-02 21:16:32 +0800500 tx.commit();
501 logger.info("Successfully settled {} expired beg requests",
502 expiredBegs.size());
Haotian W88568d82025-05-15 16:15:21 +0000503
95630366963296f62025-06-02 21:16:32 +0800504 } catch (Exception e) {
505 if (tx != null && tx.isActive()) {
506 tx.rollback();
507 }
508 logger.error("Error settling beg requests: {}", e.getMessage(), e);
509 } finally {
510 if (em != null) {
511 em.close();
512 }
513 }
514 }
Haotian W88568d82025-05-15 16:15:21 +0000515
95630366963296f62025-06-02 21:16:32 +0800516 @Override
517 public int AddPost(Post post) {
518 if (post == null || post.postid == null || post.postid.isEmpty()) {
519 logger.warn("Invalid parameter: post is null or postid is empty");
520 return 2;
521 }
Haotian W88568d82025-05-15 16:15:21 +0000522
95630366963296f62025-06-02 21:16:32 +0800523 EntityManager em = null;
524 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000525
95630366963296f62025-06-02 21:16:32 +0800526 try {
527 em = emf.createEntityManager();
528 tx = em.getTransaction();
529 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000530
95630366963296f62025-06-02 21:16:32 +0800531 // 检查是否重复
532 Post existingPost = em.find(Post.class, post.postid);
533 if (existingPost != null) {
534 logger.warn("Post with ID {} already exists", post.postid);
535 return 1;
536 }
Haotian W88568d82025-05-15 16:15:21 +0000537
95630366963296f62025-06-02 21:16:32 +0800538 // 检查用户是否存在
539 User user = em.find(User.class, post.postuserid);
540 if (user == null) {
541 logger.warn("User with ID {} does not exist", post.postuserid);
542 return 2;
543 }
Haotian W88568d82025-05-15 16:15:21 +0000544
95630366963296f62025-06-02 21:16:32 +0800545 // 设置初始值
546 if (post.posttime == null) {
547 post.posttime = new Date();
548 }
549 post.replytime = 0;
550 post.readtime = 0;
Haotian W88568d82025-05-15 16:15:21 +0000551
95630366963296f62025-06-02 21:16:32 +0800552 // 保存帖子
553 em.persist(post);
554 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000555
95630366963296f62025-06-02 21:16:32 +0800556 logger.info("Successfully added new post with ID: {}", post.postid);
557 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000558
95630366963296f62025-06-02 21:16:32 +0800559 } catch (Exception e) {
560 if (tx != null && tx.isActive()) {
561 tx.rollback();
562 }
563 logger.error("Error adding post: {}", e.getMessage());
564 return 2;
565 } finally {
566 if (em != null) {
567 em.close();
568 }
569 }
570 }
Haotian W88568d82025-05-15 16:15:21 +0000571
95630366963296f62025-06-02 21:16:32 +0800572 @Override
573 public int UpdatePost(Post post) {
574 if (post == null || post.postid == null || post.postid.isEmpty()) {
575 logger.warn("Invalid parameter: post is null or postid is empty");
576 return 2;
577 }
Haotian W88568d82025-05-15 16:15:21 +0000578
95630366963296f62025-06-02 21:16:32 +0800579 EntityManager em = null;
580 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000581
95630366963296f62025-06-02 21:16:32 +0800582 try {
583 em = emf.createEntityManager();
584 tx = em.getTransaction();
585 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000586
95630366963296f62025-06-02 21:16:32 +0800587 // 检查帖子是否存在
588 Post existingPost = em.find(Post.class, post.postid);
589 if (existingPost == null) {
590 logger.warn("Post with ID {} does not exist", post.postid);
591 return 1;
592 }
Haotian W88568d82025-05-15 16:15:21 +0000593
95630366963296f62025-06-02 21:16:32 +0800594 // 保持原有不可修改的字段
595 post.postuserid = existingPost.postuserid;
596 post.posttime = existingPost.posttime;
597 post.replytime = existingPost.replytime;
598 post.readtime = existingPost.readtime;
Haotian W88568d82025-05-15 16:15:21 +0000599
95630366963296f62025-06-02 21:16:32 +0800600 // 更新帖子
601 em.merge(post);
602 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000603
95630366963296f62025-06-02 21:16:32 +0800604 logger.info("Successfully updated post with ID: {}", post.postid);
605 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000606
95630366963296f62025-06-02 21:16:32 +0800607 } catch (Exception e) {
608 if (tx != null && tx.isActive()) {
609 tx.rollback();
610 }
611 logger.error("Error updating post: {}", e.getMessage());
612 return 2;
613 } finally {
614 if (em != null) {
615 em.close();
616 }
617 }
618 }
Haotian W88568d82025-05-15 16:15:21 +0000619
95630366963296f62025-06-02 21:16:32 +0800620 @Override
621 public int DeletePost(String postid) {
622 if (postid == null || postid.isEmpty()) {
623 logger.warn("Invalid parameter: postid is null or empty");
624 return 2;
625 }
Haotian W88568d82025-05-15 16:15:21 +0000626
95630366963296f62025-06-02 21:16:32 +0800627 EntityManager em = null;
628 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000629
95630366963296f62025-06-02 21:16:32 +0800630 try {
631 em = emf.createEntityManager();
632 tx = em.getTransaction();
633 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000634
95630366963296f62025-06-02 21:16:32 +0800635 // 查找要删除的帖子
636 Post post = em.find(Post.class, postid);
637 if (post == null) {
638 logger.warn("Post with ID {} does not exist", postid);
639 tx.rollback();
640 return 1;
641 }
Haotian W88568d82025-05-15 16:15:21 +0000642
95630366963296f62025-06-02 21:16:32 +0800643 // 删除帖子(由于设置了级联删除,相关的回复会自动删除)
644 em.remove(post);
645 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000646
95630366963296f62025-06-02 21:16:32 +0800647 logger.info("Successfully deleted post with ID: {}", postid);
648 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000649
95630366963296f62025-06-02 21:16:32 +0800650 } catch (Exception e) {
651 if (tx != null && tx.isActive()) {
652 tx.rollback();
653 }
654 logger.error("Error deleting post: {}", e.getMessage());
655 return 2;
656 } finally {
657 if (em != null) {
658 em.close();
659 }
660 }
661 }
Haotian W88568d82025-05-15 16:15:21 +0000662
95630366963296f62025-06-02 21:16:32 +0800663 @Override
664 public int AddComment(String postid, String userid, String comment) {
665 if (postid == null || postid.isEmpty() ||
666 userid == null || userid.isEmpty() ||
667 comment == null || comment.isEmpty()) {
668 logger.warn("Invalid parameters: postid, userid or comment is null or empty");
669 return 2;
670 }
Haotian W88568d82025-05-15 16:15:21 +0000671
95630366963296f62025-06-02 21:16:32 +0800672 EntityManager em = null;
673 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000674
95630366963296f62025-06-02 21:16:32 +0800675 try {
676 em = emf.createEntityManager();
677 tx = em.getTransaction();
678 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000679
95630366963296f62025-06-02 21:16:32 +0800680 // 检查帖子是否存在
681 Post post = em.find(Post.class, postid);
682 if (post == null) {
683 logger.warn("Post with ID {} does not exist", postid);
684 return 1;
685 }
Haotian W88568d82025-05-15 16:15:21 +0000686
95630366963296f62025-06-02 21:16:32 +0800687 // 检查评论用户是否存在
688 User user = em.find(User.class, userid);
689 if (user == null) {
690 logger.warn("User with ID {} does not exist", userid);
691 return 1;
692 }
Haotian W88568d82025-05-15 16:15:21 +0000693
95630366963296f62025-06-02 21:16:32 +0800694 // 创建新的回复
695 PostReply reply = new PostReply();
696 reply.replyid = UUID.randomUUID().toString();
697 reply.postid = postid;
698 reply.content = comment;
699 reply.authorid = userid;
700 reply.createdAt = new Date();
Haotian W88568d82025-05-15 16:15:21 +0000701
95630366963296f62025-06-02 21:16:32 +0800702 // 保存回复
703 em.persist(reply);
Haotian W88568d82025-05-15 16:15:21 +0000704
95630366963296f62025-06-02 21:16:32 +0800705 // 更新帖子的回复数
706 post.replytime += 1;
707 em.merge(post);
Haotian W88568d82025-05-15 16:15:21 +0000708
95630366963296f62025-06-02 21:16:32 +0800709 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000710
95630366963296f62025-06-02 21:16:32 +0800711 logger.info("Successfully added comment by user {} to post {}, reply ID: {}",
712 userid, postid, reply.replyid);
713 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000714
95630366963296f62025-06-02 21:16:32 +0800715 } catch (Exception e) {
716 if (tx != null && tx.isActive()) {
717 tx.rollback();
718 }
719 logger.error("Error adding comment: {}", e.getMessage());
720 return 2;
721 } finally {
722 if (em != null) {
723 em.close();
724 }
725 }
726 }
Haotian W88568d82025-05-15 16:15:21 +0000727
95630366963296f62025-06-02 21:16:32 +0800728 @Override
729 public int DeleteComment(String postid, String commentid) {
730 if (postid == null || postid.isEmpty() || commentid == null || commentid.isEmpty()) {
731 logger.warn("Invalid parameters: postid or commentid is null or empty");
732 return 2;
733 }
Haotian W88568d82025-05-15 16:15:21 +0000734
95630366963296f62025-06-02 21:16:32 +0800735 EntityManager em = null;
736 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000737
95630366963296f62025-06-02 21:16:32 +0800738 try {
739 em = emf.createEntityManager();
740 tx = em.getTransaction();
741 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000742
95630366963296f62025-06-02 21:16:32 +0800743 // 检查帖子是否存在
744 Post post = em.find(Post.class, postid);
745 if (post == null) {
746 logger.warn("Post with ID {} does not exist", postid);
747 return 1;
748 }
Haotian W88568d82025-05-15 16:15:21 +0000749
95630366963296f62025-06-02 21:16:32 +0800750 // 检查评论是否存在且属于该帖子
751 PostReply reply = em.find(PostReply.class, commentid);
752 if (reply == null || !reply.postid.equals(postid)) {
753 logger.warn("Comment {} does not exist or does not belong to post {}", commentid, postid);
754 return 1;
755 }
Haotian W88568d82025-05-15 16:15:21 +0000756
95630366963296f62025-06-02 21:16:32 +0800757 // 删除评论
758 em.remove(reply);
Haotian W88568d82025-05-15 16:15:21 +0000759
95630366963296f62025-06-02 21:16:32 +0800760 // 更新帖子的回复数
761 post.replytime = Math.max(0, post.replytime - 1);
762 em.merge(post);
Haotian W88568d82025-05-15 16:15:21 +0000763
95630366963296f62025-06-02 21:16:32 +0800764 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000765
95630366963296f62025-06-02 21:16:32 +0800766 logger.info("Successfully deleted comment {} from post {}", commentid, postid);
767 return 0;
Haotian W88568d82025-05-15 16:15:21 +0000768
95630366963296f62025-06-02 21:16:32 +0800769 } catch (Exception e) {
770 if (tx != null && tx.isActive()) {
771 tx.rollback();
772 }
773 logger.error("Error deleting comment: {}", e.getMessage());
774 return 2;
775 } finally {
776 if (em != null) {
777 em.close();
778 }
779 }
780 }
Haotian W88568d82025-05-15 16:15:21 +0000781
95630366963296f62025-06-02 21:16:32 +0800782 @Override
783 public boolean ExchangeMagicToUpload(String userid, int magic) {
784 if (userid == null || userid.isEmpty() || magic <= 0) {
785 logger.warn("参数无效: userid为空或magic <= 0");
786 return false;
787 }
Haotian W88568d82025-05-15 16:15:21 +0000788
95630366963296f62025-06-02 21:16:32 +0800789 EntityManager em = null;
790 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000791
95630366963296f62025-06-02 21:16:32 +0800792 try {
793 em = emf.createEntityManager();
794 tx = em.getTransaction();
795 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000796
95630366963296f62025-06-02 21:16:32 +0800797 UserPT userPT = em.find(UserPT.class, userid);
798 if (userPT == null) {
799 logger.warn("未找到用户 {} 的PT信息", userid);
800 return false;
801 }
Haotian W88568d82025-05-15 16:15:21 +0000802
95630366963296f62025-06-02 21:16:32 +0800803 if (userPT.magic < magic) {
804 logger.warn("用户 {} 的魔力值不足", userid);
805 return false;
806 }
Haotian W88568d82025-05-15 16:15:21 +0000807
95630366963296f62025-06-02 21:16:32 +0800808 // 1:1兑换,直接加上魔力值(假设单位是MB)
809 userPT.magic -= magic;
810 userPT.upload += magic;
Haotian W88568d82025-05-15 16:15:21 +0000811
95630366963296f62025-06-02 21:16:32 +0800812 // 更新分享率
813 if (userPT.download > 0) {
814 userPT.share = (double) userPT.upload / userPT.download;
815 }
Haotian W88568d82025-05-15 16:15:21 +0000816
95630366963296f62025-06-02 21:16:32 +0800817 em.merge(userPT);
818 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000819
95630366963296f62025-06-02 21:16:32 +0800820 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 上传量", userid, magic, magic);
821 return true;
Haotian W88568d82025-05-15 16:15:21 +0000822
95630366963296f62025-06-02 21:16:32 +0800823 } catch (Exception e) {
824 if (tx != null && tx.isActive()) {
825 tx.rollback();
826 }
827 logger.error("魔力值兑换上传量时发生错误: {}", e.getMessage());
828 return false;
829 } finally {
830 if (em != null) {
831 em.close();
832 }
833 }
834 }
Haotian W88568d82025-05-15 16:15:21 +0000835
95630366963296f62025-06-02 21:16:32 +0800836 @Override
837 public boolean ExchangeMagicToDownload(String userid, int magic) {
838 if (userid == null || userid.isEmpty() || magic <= 0) {
839 logger.warn("参数无效: userid为空或magic <= 0");
840 return false;
841 }
Haotian W88568d82025-05-15 16:15:21 +0000842
95630366963296f62025-06-02 21:16:32 +0800843 EntityManager em = null;
844 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000845
95630366963296f62025-06-02 21:16:32 +0800846 try {
847 em = emf.createEntityManager();
848 tx = em.getTransaction();
849 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000850
95630366963296f62025-06-02 21:16:32 +0800851 UserPT userPT = em.find(UserPT.class, userid);
852 if (userPT == null) {
853 logger.warn("未找到用户 {} 的PT信息", userid);
854 return false;
855 }
Haotian W88568d82025-05-15 16:15:21 +0000856
95630366963296f62025-06-02 21:16:32 +0800857 if (userPT.magic < magic) {
858 logger.warn("用户 {} 的魔力值不足", userid);
859 return false;
860 }
Haotian W88568d82025-05-15 16:15:21 +0000861
95630366963296f62025-06-02 21:16:32 +0800862 // 1:1兑换,直接减去魔力值对应的下载量(假设单位是MB)
863 userPT.magic -= magic;
864 userPT.download = Math.max(0, userPT.download - magic);
Haotian W88568d82025-05-15 16:15:21 +0000865
95630366963296f62025-06-02 21:16:32 +0800866 // 更新分享率
867 if (userPT.download > 0) {
868 userPT.share = (double) userPT.upload / userPT.download;
869 }
Haotian W88568d82025-05-15 16:15:21 +0000870
95630366963296f62025-06-02 21:16:32 +0800871 em.merge(userPT);
872 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000873
95630366963296f62025-06-02 21:16:32 +0800874 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 下载量减免", userid, magic, magic);
875 return true;
Haotian W88568d82025-05-15 16:15:21 +0000876
95630366963296f62025-06-02 21:16:32 +0800877 } catch (Exception e) {
878 if (tx != null && tx.isActive()) {
879 tx.rollback();
880 }
881 logger.error("魔力值兑换下载量时发生错误: {}", e.getMessage());
882 return false;
883 } finally {
884 if (em != null) {
885 em.close();
886 }
887 }
888 }
Haotian W88568d82025-05-15 16:15:21 +0000889
95630366963296f62025-06-02 21:16:32 +0800890 @Override
891 public boolean ExchangeMagicToVip(String userid, int magic) {
892 if (userid == null || userid.isEmpty() || magic <= 0) {
893 logger.warn("参数无效: userid为空或magic <= 0");
894 return false;
895 }
Haotian W88568d82025-05-15 16:15:21 +0000896
95630366963296f62025-06-02 21:16:32 +0800897 EntityManager em = null;
898 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000899
95630366963296f62025-06-02 21:16:32 +0800900 try {
901 em = emf.createEntityManager();
902 tx = em.getTransaction();
903 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000904
95630366963296f62025-06-02 21:16:32 +0800905 UserPT userPT = em.find(UserPT.class, userid);
906 if (userPT == null) {
907 logger.warn("未找到用户 {} 的PT信息", userid);
908 return false;
909 }
Haotian W88568d82025-05-15 16:15:21 +0000910
95630366963296f62025-06-02 21:16:32 +0800911 if (userPT.magic < magic) {
912 logger.warn("用户 {} 的魔力值不足", userid);
913 return false;
914 }
Haotian W88568d82025-05-15 16:15:21 +0000915
95630366963296f62025-06-02 21:16:32 +0800916 // 1:1兑换VIP下载次数
917 userPT.magic -= magic;
918 userPT.viptime += magic;
Haotian W88568d82025-05-15 16:15:21 +0000919
95630366963296f62025-06-02 21:16:32 +0800920 em.merge(userPT);
921 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000922
95630366963296f62025-06-02 21:16:32 +0800923 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {} 次VIP下载次数", userid, magic, magic);
924 return true;
Haotian W88568d82025-05-15 16:15:21 +0000925
95630366963296f62025-06-02 21:16:32 +0800926 } catch (Exception e) {
927 if (tx != null && tx.isActive()) {
928 tx.rollback();
929 }
930 logger.error("魔力值兑换VIP下载次数时发生错误: {}", e.getMessage());
931 return false;
932 } finally {
933 if (em != null) {
934 em.close();
935 }
936 }
937 }
Haotian W88568d82025-05-15 16:15:21 +0000938
95630366963296f62025-06-02 21:16:32 +0800939 @Override
940 public boolean UploadTransmitProfile(Profile profile) {
941 if (profile == null || profile.profileurl == null || profile.profileurl.isEmpty() ||
942 profile.userid == null || profile.userid.isEmpty()) {
943 logger.warn("参数无效: profile为空或必要字段为空");
944 return false;
945 }
Haotian W88568d82025-05-15 16:15:21 +0000946
95630366963296f62025-06-02 21:16:32 +0800947 EntityManager em = null;
948 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +0000949
95630366963296f62025-06-02 21:16:32 +0800950 try {
951 em = emf.createEntityManager();
952 tx = em.getTransaction();
953 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +0000954
95630366963296f62025-06-02 21:16:32 +0800955 // 检查用户是否存在
956 User user = em.find(User.class, profile.userid);
957 if (user == null) {
958 logger.warn("用户 {} 不存在", profile.userid);
959 return false;
960 }
Haotian W88568d82025-05-15 16:15:21 +0000961
95630366963296f62025-06-02 21:16:32 +0800962 // 检查是否已存在相同的迁移申请
963 Profile existingProfile = em.find(Profile.class, profile.profileurl);
964 if (existingProfile != null) {
965 logger.warn("迁移申请 {} 已存在", profile.profileurl);
966 return false;
967 }
Haotian W88568d82025-05-15 16:15:21 +0000968
95630366963296f62025-06-02 21:16:32 +0800969 // 设置初始值
970 profile.exampass = false;
971 profile.magicgived = "0";
972 profile.uploadgived = "0";
Haotian W88568d82025-05-15 16:15:21 +0000973
95630366963296f62025-06-02 21:16:32 +0800974 // 保存迁移申请
975 em.persist(profile);
976 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +0000977
95630366963296f62025-06-02 21:16:32 +0800978 logger.info("成功上传迁移申请 {}", profile.profileurl);
979 return true;
Haotian W88568d82025-05-15 16:15:21 +0000980
95630366963296f62025-06-02 21:16:32 +0800981 } catch (Exception e) {
982 if (tx != null && tx.isActive()) {
983 tx.rollback();
984 }
985 logger.error("上传迁移申请时发生错误: {}", e.getMessage());
986 return false;
987 } finally {
988 if (em != null) {
989 em.close();
990 }
991 }
992 }
Haotian W88568d82025-05-15 16:15:21 +0000993
95630366963296f62025-06-02 21:16:32 +0800994 @Override
995 public Profile GetTransmitProfile(String profileid) {
996 if (profileid == null || profileid.isEmpty()) {
997 logger.warn("参数无效: profileid为空");
998 return null;
999 }
Haotian W88568d82025-05-15 16:15:21 +00001000
95630366963296f62025-06-02 21:16:32 +08001001 EntityManager em = null;
Haotian W88568d82025-05-15 16:15:21 +00001002
95630366963296f62025-06-02 21:16:32 +08001003 try {
1004 em = emf.createEntityManager();
1005 Profile profile = em.find(Profile.class, profileid);
1006 if (profile == null) {
1007 logger.warn("未找到迁移申请 {}", profileid);
1008 return null;
1009 }
Haotian W88568d82025-05-15 16:15:21 +00001010
95630366963296f62025-06-02 21:16:32 +08001011 logger.info("成功获取迁移申请 {}", profileid);
1012 return profile;
Haotian W88568d82025-05-15 16:15:21 +00001013
95630366963296f62025-06-02 21:16:32 +08001014 } catch (Exception e) {
1015 logger.error("获取迁移申请时发生错误: {}", e.getMessage());
1016 return null;
1017 } finally {
1018 if (em != null) {
1019 em.close();
1020 }
1021 }
1022 }
Haotian W88568d82025-05-15 16:15:21 +00001023
95630366963296f62025-06-02 21:16:32 +08001024 @Override
rhj7912f2c2025-06-09 17:21:32 +08001025 public boolean ExamTransmitProfile(String profileid, boolean result, Integer grantedUpload) {
95630366963296f62025-06-02 21:16:32 +08001026 if (profileid == null || profileid.isEmpty()) {
1027 logger.warn("参数无效: profileid为空");
1028 return false;
1029 }
Haotian W88568d82025-05-15 16:15:21 +00001030
95630366963296f62025-06-02 21:16:32 +08001031 EntityManager em = null;
1032 EntityTransaction tx = null;
Haotian W88568d82025-05-15 16:15:21 +00001033
95630366963296f62025-06-02 21:16:32 +08001034 try {
1035 em = emf.createEntityManager();
1036 tx = em.getTransaction();
1037 tx.begin();
Haotian W88568d82025-05-15 16:15:21 +00001038
95630366963296f62025-06-02 21:16:32 +08001039 // 查找迁移申请
1040 Profile profile = em.find(Profile.class, profileid);
1041 if (profile == null) {
1042 logger.warn("未找到迁移申请 {}", profileid);
1043 return false;
1044 }
Haotian W88568d82025-05-15 16:15:21 +00001045
95630366963296f62025-06-02 21:16:32 +08001046 // 更新审核状态
1047 profile.exampass = result;
Haotian W88568d82025-05-15 16:15:21 +00001048
95630366963296f62025-06-02 21:16:32 +08001049 if (result) {
1050 // 如果审核通过,更新用户的PT信息
1051 UserPT userPT = em.find(UserPT.class, profile.userid);
1052 if (userPT != null) {
1053 // 发放魔力值
1054 int magicToGive = Integer.parseInt(profile.magictogive);
1055 userPT.magic += magicToGive;
1056 profile.magicgived = String.valueOf(magicToGive);
Haotian W88568d82025-05-15 16:15:21 +00001057
95630366963296f62025-06-02 21:16:32 +08001058 // 发放上传量
1059 long uploadToGive = Long.parseLong(profile.uploadtogive);
1060 userPT.upload += uploadToGive;
1061 profile.uploadgived = String.valueOf(uploadToGive);
Haotian W88568d82025-05-15 16:15:21 +00001062
95630366963296f62025-06-02 21:16:32 +08001063 em.merge(userPT);
1064 }
1065 }
Haotian W88568d82025-05-15 16:15:21 +00001066
95630366963296f62025-06-02 21:16:32 +08001067 em.merge(profile);
1068 tx.commit();
Haotian W88568d82025-05-15 16:15:21 +00001069
95630366963296f62025-06-02 21:16:32 +08001070 logger.info("成功审核迁移申请 {}, 结果: {}", profileid, result);
1071 return true;
Haotian W88568d82025-05-15 16:15:21 +00001072
95630366963296f62025-06-02 21:16:32 +08001073 } catch (Exception e) {
1074 if (tx != null && tx.isActive()) {
1075 tx.rollback();
1076 }
1077 logger.error("审核迁移申请时发生错误: {}", e.getMessage());
1078 return false;
1079 } finally {
1080 if (em != null) {
1081 em.close();
1082 }
1083 }
1084 }
Haotian W88568d82025-05-15 16:15:21 +00001085
95630366963296f62025-06-02 21:16:32 +08001086 @Override
1087 public Profile[] GetTransmitProfileList() {
1088 EntityManager em = null;
Haotian W88568d82025-05-15 16:15:21 +00001089
95630366963296f62025-06-02 21:16:32 +08001090 try {
1091 em = emf.createEntityManager();
Haotian W88568d82025-05-15 16:15:21 +00001092
95630366963296f62025-06-02 21:16:32 +08001093 // 获取所有迁移申请
1094 QProfile p = QProfile.profile;
1095 List<Profile> profiles = new JPAQuery<>(em)
1096 .select(p)
1097 .from(p)
1098 .fetch();
Haotian W88568d82025-05-15 16:15:21 +00001099
95630366963296f62025-06-02 21:16:32 +08001100 logger.info("成功获取所有迁移申请,共 {} 条", profiles.size());
1101 return profiles.toArray(new Profile[0]);
Haotian W88568d82025-05-15 16:15:21 +00001102
95630366963296f62025-06-02 21:16:32 +08001103 } catch (Exception e) {
1104 logger.error("获取迁移申请列表时发生错误: {}", e.getMessage());
1105 return new Profile[0];
1106 } finally {
1107 if (em != null) {
1108 em.close();
1109 }
1110 }
1111 }
rhj46f62c42025-06-06 23:24:10 +08001112
1113 @Override
1114 public Post[] GetPostList(){
1115 return null;
1116 }
rhj5b69b7e2025-06-07 01:28:08 +08001117
1118 @Override
1119 public Post GetPost(String postid) {
1120 return null;
1121 }
1122
1123 @Override
1124 public PostReply[] GetPostReplyList(String postid) {
1125 return null;
1126 }
rhj5ebd93c2025-06-07 15:57:28 +08001127
1128 @Override
1129 public Post[] SearchPost(String userQ) {
1130 return null;
1131 }
rhje18c3f72025-06-08 00:27:01 +08001132
1133 @Override
1134 public int CheckAdmin(String userid) {
1135 return 0;
1136 }
1137
1138 @Override
1139 public int InviteNewUser(String inviterid, String invitedemail) {
1140 return 0;
1141 }
1142
1143 @Override
1144 public UserStar[] GetUserStarList(String userid) {
1145 return new UserStar[0];
1146 }
1147
1148 @Override
rhj7912f2c2025-06-09 17:21:32 +08001149 public int UploadMigration(String userid, File file, String uploadtogive){
rhje18c3f72025-06-08 00:27:01 +08001150 return 0;
1151 }
1152
1153 @Override
1154 public BegSeedDetail[] GetBegList() {
1155 EntityManager em = null;
1156 try {
1157 em = emf.createEntityManager();
1158
1159 JPAQuery<BegInfo> query = new JPAQuery<>(em);
1160 QBegInfo b = QBegInfo.begInfo;
1161 List<BegInfo> begList = query.select(b).from(b).fetch();
1162
1163 List<BegSeedDetail> begSeedDetailList = new ArrayList<>();
1164 for (BegInfo begInfo : begList) {
1165 // 查询对应的BegInfo表的Info字段
1166 BegInfoDetail begInfoDetail = em.find(BegInfoDetail.class, begInfo.begid);
1167 // 构造BegSeedDetail对象
1168 BegSeedDetail begSeedDetail = new BegSeedDetail(begInfo, begInfoDetail);
1169 begSeedDetailList.add(begSeedDetail);
1170 }
1171
1172 return begSeedDetailList.toArray(new BegSeedDetail[0]);
1173 } catch (Exception e) {
1174 logger.error("Error getting BegList: {}", e.getMessage(), e);
1175 return new BegSeedDetail[0];
1176 } finally {
1177 if (em != null) {
1178 em.close();
1179 }
1180 }
1181 }
1182
1183 @Override
1184 public BegInfo GetBegDetail(String begid) {
1185 return null;
1186 }
1187
1188 @Override
1189 public BegSeedDetail GetBegSeedDetail(String begid) {
1190 if (begid == null || begid.isEmpty()) {
1191 return null;
1192 }
1193
1194 EntityManager em = null;
1195 try {
1196 em = emf.createEntityManager();
1197
1198 // 查询 BegSeed 表数据
1199 JPAQuery<BegInfo> begQuery = new JPAQuery<>(em);
1200 QBegInfo b = QBegInfo.begInfo;
1201 BegInfo begInfo = begQuery.select(b).from(b).where(b.begid.eq(begid)).fetchOne();
1202
1203 if (begInfo == null) {
1204 return null;
1205 }
1206
1207 // 查询 BegInfo 表的 Info 字段
1208 BegInfoDetail begInfoDetail = em.find(BegInfoDetail.class, begid);
1209
1210 // 构造返回对象
1211 return new BegSeedDetail(begInfo, begInfoDetail);
1212 } catch (Exception e) {
1213 logger.error("Error getting BegSeedDetail: {}", e.getMessage(), e);
1214 return null;
1215 } finally {
1216 if (em != null) {
1217 em.close();
1218 }
1219 }
1220 }
1221
1222 @Override
1223 public SeedWithVotes[] GetBegSeedListWithVotes(String begid) {
1224 return new SeedWithVotes[0];
1225 }
1226
1227 @Override
1228 public int SubmitBegSeed(String begid, String seedid, String userid) {
1229 return 0;
1230 }
1231
1232 @Override
1233 public int createBagSeed(BegInfo begInfo, String userid, String info) {
1234 return 0;
1235 }
rhj7912f2c2025-06-09 17:21:32 +08001236
1237 @Override
1238 public Seed[] getAllSeeds() {
1239 return null;
1240 }
1241
1242 @Override
1243 public SeedPromotion[] getAllSeedPromotions() {
1244 return null;
1245 }
1246
1247 @Override
1248 public int createSeedPromotion(String seedid, Date startTime, Date endTime, Integer discount) {
1249 return 0;
1250 }
95630366963296f62025-06-02 21:16:32 +08001251}