root | cd43656 | 2025-05-08 14:09:19 +0000 | [diff] [blame] | 1 | package database; |
| 2 | |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 3 | import java.util.Calendar; |
| 4 | import java.util.UUID; |
| 5 | import java.util.List; |
| 6 | import java.util.Date; |
| 7 | |
| 8 | import javax.persistence.EntityManager; |
| 9 | import javax.persistence.EntityManagerFactory; |
| 10 | import javax.persistence.EntityTransaction; |
| 11 | import javax.persistence.Persistence; |
| 12 | |
| 13 | import com.querydsl.jpa.impl.JPAQuery; |
| 14 | import com.querydsl.core.Tuple; |
| 15 | import object.QBegInfo; |
| 16 | import object.QUserVotes; |
| 17 | import object.QSubmitSeed; |
| 18 | import object.QProfile; |
| 19 | import object.QSeed; |
| 20 | import object.Seed; |
| 21 | import object.User; |
| 22 | import object.UserPT; |
| 23 | import object.Notice; |
| 24 | import object.BegInfo; |
| 25 | import object.Post; |
| 26 | import object.PostReply; |
| 27 | import object.Profile; |
| 28 | |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
root | cd43656 | 2025-05-08 14:09:19 +0000 | [diff] [blame] | 32 | // public class Database2 implements DataManagerInterface { |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 33 | |
| 34 | // private EntityManagerFactory emf; |
| 35 | // private static final Logger logger = LoggerFactory.getLogger(Database2.class); |
| 36 | |
| 37 | // // 构造函数,初始化EntityManagerFactory |
| 38 | // public Database2() { |
| 39 | // this.emf = Persistence.createEntityManagerFactory("myPersistenceUnit"); // 使用 persistence.xml 中定义的持久化单元名称。 |
| 40 | // } |
| 41 | |
| 42 | // @Override |
| 43 | // public int AddBegSeed(BegInfo info) { |
| 44 | // if (info == null || info.begid == null || info.begid.isEmpty()) { |
| 45 | // logger.warn("Invalid parameter: info is null or begid is empty"); |
| 46 | // return 2; |
| 47 | // } |
| 48 | |
| 49 | // EntityManager em = null; |
| 50 | // EntityTransaction tx = null; |
| 51 | |
| 52 | // try { |
| 53 | // em = emf.createEntityManager(); |
| 54 | // tx = em.getTransaction(); |
| 55 | // tx.begin(); |
| 56 | |
| 57 | // // 检查是否重复 |
| 58 | // BegInfo existingBeg = em.find(BegInfo.class, info.begid); |
| 59 | // if (existingBeg != null) { |
| 60 | // logger.warn("BegSeed with ID {} already exists", info.begid); |
| 61 | // return 1; |
| 62 | // } |
| 63 | |
| 64 | // // 设置默认值 |
| 65 | // if (info.endtime == null) { |
| 66 | // // 设置默认14天截止期 |
| 67 | // Calendar calendar = Calendar.getInstance(); |
| 68 | // calendar.add(Calendar.DAY_OF_MONTH, 14); |
| 69 | // info.endtime = calendar.getTime(); |
| 70 | // } |
| 71 | // info.hasseed = false; |
| 72 | |
| 73 | // // 保存新的求种信息 |
| 74 | // em.persist(info); |
| 75 | // tx.commit(); |
| 76 | |
| 77 | // logger.info("Successfully added new BegSeed with ID: {}", info.begid); |
| 78 | // return 0; |
| 79 | |
| 80 | // } catch (Exception e) { |
| 81 | // if (tx != null && tx.isActive()) { |
| 82 | // tx.rollback(); |
| 83 | // } |
| 84 | // logger.error("Error adding BegSeed: {}", e.getMessage()); |
| 85 | // return 2; |
| 86 | // } finally { |
| 87 | // if (em != null) { |
| 88 | // em.close(); |
| 89 | // } |
| 90 | // } |
| 91 | // } |
| 92 | |
| 93 | // @Override |
| 94 | // public int UpdateBegSeed(BegInfo info) { |
| 95 | // if (info == null || info.begid == null || info.begid.isEmpty()) { |
| 96 | // logger.warn("Invalid parameter: info is null or begid is empty"); |
| 97 | // return 2; |
| 98 | // } |
| 99 | |
| 100 | // EntityManager em = null; |
| 101 | // EntityTransaction tx = null; |
| 102 | |
| 103 | // try { |
| 104 | // em = emf.createEntityManager(); |
| 105 | // tx = em.getTransaction(); |
| 106 | // tx.begin(); |
| 107 | |
| 108 | // // 检查是否存在 |
| 109 | // BegInfo existingBeg = em.find(BegInfo.class, info.begid); |
| 110 | // if (existingBeg == null) { |
| 111 | // logger.warn("BegSeed with ID {} does not exist", info.begid); |
| 112 | // return 1; |
| 113 | // } |
| 114 | |
| 115 | // // 保持原有值不变的字段 |
| 116 | // info.hasseed = existingBeg.hasseed; |
| 117 | // if (info.endtime == null) { |
| 118 | // info.endtime = existingBeg.endtime; |
| 119 | // } |
| 120 | |
| 121 | // // 更新求种信息 |
| 122 | // em.merge(info); |
| 123 | // tx.commit(); |
| 124 | |
| 125 | // logger.info("Successfully updated BegSeed with ID: {}", info.begid); |
| 126 | // return 0; |
| 127 | |
| 128 | // } catch (Exception e) { |
| 129 | // if (tx != null && tx.isActive()) { |
| 130 | // tx.rollback(); |
| 131 | // } |
| 132 | // logger.error("Error updating BegSeed: {}", e.getMessage()); |
| 133 | // return 2; |
| 134 | // } finally { |
| 135 | // if (em != null) { |
| 136 | // em.close(); |
| 137 | // } |
| 138 | // } |
| 139 | // } |
| 140 | |
| 141 | // @Override |
| 142 | // public int DeleteBegSeed(String begid) { |
| 143 | // if (begid == null || begid.isEmpty()) { |
| 144 | // logger.warn("Invalid parameter: begid is null or empty"); |
| 145 | // return 2; |
| 146 | // } |
| 147 | |
| 148 | // EntityManager em = null; |
| 149 | // EntityTransaction tx = null; |
| 150 | |
| 151 | // try { |
| 152 | // em = emf.createEntityManager(); |
| 153 | // tx = em.getTransaction(); |
| 154 | // tx.begin(); |
| 155 | |
| 156 | // // 查找要删除的求种信息 |
| 157 | // BegInfo begInfo = em.find(BegInfo.class, begid); |
| 158 | // if (begInfo == null) { |
| 159 | // logger.warn("BegSeed with ID {} does not exist", begid); |
| 160 | // return 1; |
| 161 | // } |
| 162 | |
| 163 | // // 删除求种信息 |
| 164 | // em.remove(begInfo); |
| 165 | // tx.commit(); |
| 166 | |
| 167 | // logger.info("Successfully deleted BegSeed with ID: {}", begid); |
| 168 | // return 0; |
| 169 | |
| 170 | // } catch (Exception e) { |
| 171 | // if (tx != null && tx.isActive()) { |
| 172 | // tx.rollback(); |
| 173 | // } |
| 174 | // logger.error("Error deleting BegSeed: {}", e.getMessage()); |
| 175 | // return 2; |
| 176 | // } finally { |
| 177 | // if (em != null) { |
| 178 | // em.close(); |
| 179 | // } |
| 180 | // } |
| 181 | // } |
| 182 | |
| 183 | // @Override |
| 184 | // public int VoteSeed(String begId, String seedId, String userId) { |
| 185 | // if (begId == null || seedId == null || userId == null || |
| 186 | // begId.isEmpty() || seedId.isEmpty() || userId.isEmpty()) { |
| 187 | // logger.warn("Invalid parameters: begId, seedId or userId is null or empty"); |
| 188 | // return 2; |
| 189 | // } |
| 190 | |
| 191 | // EntityManager em = null; |
| 192 | // EntityTransaction tx = null; |
| 193 | |
| 194 | // try { |
| 195 | // em = emf.createEntityManager(); |
| 196 | // tx = em.getTransaction(); |
| 197 | // tx.begin(); |
| 198 | |
| 199 | // // 检查求种信息是否存在 |
| 200 | // BegInfo begInfo = em.find(BegInfo.class, begId); |
| 201 | // if (begInfo == null) { |
| 202 | // logger.warn("BegSeed with ID {} does not exist", begId); |
| 203 | // return 2; |
| 204 | // } |
| 205 | |
| 206 | // // 检查用户是否已投票 |
| 207 | // QUserVotes uv = QUserVotes.userVotes; |
| 208 | // Long voteCount = new JPAQuery<>(em) |
| 209 | // .select(uv.count()) |
| 210 | // .from(uv) |
| 211 | // .where(uv.userId.eq(userId) |
| 212 | // .and(uv.begId.eq(begId)) |
| 213 | // .and(uv.seedId.eq(seedId))) |
| 214 | // .fetchOne(); |
| 215 | |
| 216 | // if (voteCount > 0) { |
| 217 | // logger.warn("User {} has already voted for seed {} in beg {}", userId, seedId, begId); |
| 218 | // return 1; |
| 219 | // } |
| 220 | |
| 221 | // // 创建新的投票记录 |
| 222 | // em.createNativeQuery("INSERT INTO UserVotes (user_id, beg_id, seed_id, created_at) " + |
| 223 | // "VALUES (?, ?, ?, CURRENT_TIMESTAMP)") |
| 224 | // .setParameter(1, userId) |
| 225 | // .setParameter(2, begId) |
| 226 | // .setParameter(3, seedId) |
| 227 | // .executeUpdate(); |
| 228 | |
| 229 | // // 更新SubmitSeed表中的投票数 |
| 230 | // em.createQuery("UPDATE SubmitSeed s SET s.votes = s.votes + 1 " + |
| 231 | // "WHERE s.begId = :begId AND s.seedId = :seedId") |
| 232 | // .setParameter("begId", begId) |
| 233 | // .setParameter("seedId", seedId) |
| 234 | // .executeUpdate(); |
| 235 | |
| 236 | // tx.commit(); |
| 237 | // logger.info("Successfully added vote from user {} for seed {} in beg {}", |
| 238 | // userId, seedId, begId); |
| 239 | // return 0; |
| 240 | |
| 241 | // } catch (Exception e) { |
| 242 | // if (tx != null && tx.isActive()) { |
| 243 | // tx.rollback(); |
| 244 | // } |
| 245 | // logger.error("Error voting for seed: {}", e.getMessage()); |
| 246 | // return 2; |
| 247 | // } finally { |
| 248 | // if (em != null) { |
| 249 | // em.close(); |
| 250 | // } |
| 251 | // } |
| 252 | // } |
| 253 | |
| 254 | // @Override |
| 255 | // public int SubmitSeed(String begid, Seed seed) { |
| 256 | // if (begid == null || seed == null || begid.isEmpty() || seed.seedid == null) { |
| 257 | // logger.warn("Invalid parameters: begid or seed is null or empty"); |
| 258 | // return 2; |
| 259 | // } |
| 260 | |
| 261 | // EntityManager em = null; |
| 262 | // EntityTransaction tx = null; |
| 263 | |
| 264 | // try { |
| 265 | // em = emf.createEntityManager(); |
| 266 | // tx = em.getTransaction(); |
| 267 | // tx.begin(); |
| 268 | |
| 269 | // // 检查求种信息是否存在 |
| 270 | // BegInfo begInfo = em.find(BegInfo.class, begid); |
| 271 | // if (begInfo == null) { |
| 272 | // logger.warn("BegSeed with ID {} does not exist", begid); |
| 273 | // return 2; |
| 274 | // } |
| 275 | |
| 276 | // // 检查种子是否已提交过 |
| 277 | // QSubmitSeed ss = QSubmitSeed.submitSeed; |
| 278 | // Long submitCount = new JPAQuery<>(em) |
| 279 | // .select(ss.count()) |
| 280 | // .from(ss) |
| 281 | // .where(ss.begId.eq(begid) |
| 282 | // .and(ss.seedId.eq(seed.seedid))) |
| 283 | // .fetchOne(); |
| 284 | |
| 285 | // if (submitCount > 0) { |
| 286 | // logger.warn("Seed {} has already been submitted for beg {}", seed.seedid, begid); |
| 287 | // return 1; |
| 288 | // } |
| 289 | |
| 290 | // // 保存种子信息(如果不存在) |
| 291 | // if (em.find(Seed.class, seed.seedid) == null) { |
| 292 | // em.persist(seed); |
| 293 | // } |
| 294 | |
| 295 | // // 创建提交记录 |
| 296 | // em.createNativeQuery("INSERT INTO SubmitSeed (beg_id, seed_id, votes) VALUES (?, ?, 0)") |
| 297 | // .setParameter(1, begid) |
| 298 | // .setParameter(2, seed.seedid) |
| 299 | // .executeUpdate(); |
| 300 | |
| 301 | // tx.commit(); |
| 302 | // logger.info("Successfully submitted seed {} for beg {}", seed.seedid, begid); |
| 303 | // return 0; |
| 304 | |
| 305 | // } catch (Exception e) { |
| 306 | // if (tx != null && tx.isActive()) { |
| 307 | // tx.rollback(); |
| 308 | // } |
| 309 | // logger.error("Error submitting seed: {}", e.getMessage()); |
| 310 | // return 2; |
| 311 | // } finally { |
| 312 | // if (em != null) { |
| 313 | // em.close(); |
| 314 | // } |
| 315 | // } |
| 316 | // } |
| 317 | |
| 318 | // @Override |
| 319 | // public void SettleBeg() { |
| 320 | // EntityManager em = null; |
| 321 | // EntityTransaction tx = null; |
| 322 | |
| 323 | // try { |
| 324 | // em = emf.createEntityManager(); |
| 325 | // tx = em.getTransaction(); |
| 326 | // tx.begin(); |
| 327 | |
| 328 | // // 1. 获取所有已过期且未完成的求种信息 |
| 329 | // QBegInfo b = QBegInfo.begInfo; |
| 330 | // List<BegInfo> expiredBegs = new JPAQuery<>(em) |
| 331 | // .select(b) |
| 332 | // .from(b) |
| 333 | // .where(b.endtime.loe(new Date()) |
| 334 | // .and(b.hasseed.eq(false))) |
| 335 | // .fetch(); |
| 336 | |
| 337 | // for (BegInfo beg : expiredBegs) { |
| 338 | // // 2. 查找投票最多的提交任务 |
| 339 | // QSubmitSeed ss = QSubmitSeed.submitSeed; |
| 340 | // Tuple topSubmission = new JPAQuery<>(em) |
| 341 | // .select(ss.seedId, ss.votes) |
| 342 | // .from(ss) |
| 343 | // .where(ss.begId.eq(beg.begid)) |
| 344 | // .orderBy(ss.votes.desc()) |
| 345 | // .limit(1) |
| 346 | // .fetchOne(); |
| 347 | |
| 348 | // if (topSubmission != null && topSubmission.get(ss.votes) > 0) { |
| 349 | // String seedId = topSubmission.get(ss.seedId); |
| 350 | |
| 351 | // // 3. 获取上传者ID |
| 352 | // QSeed s = QSeed.seed; |
| 353 | // String ownerId = new JPAQuery<>(em) |
| 354 | // .select(s.seeduserid) |
| 355 | // .from(s) |
| 356 | // .where(s.seedid.eq(seedId)) |
| 357 | // .fetchOne(); |
| 358 | |
| 359 | // // 4. 获取上传者的PT信息并更新魔力值 |
| 360 | // UserPT ownerPT = em.find(UserPT.class, ownerId); |
| 361 | // if (ownerPT != null) { |
| 362 | // // 5. 发放奖励 |
| 363 | // ownerPT.magic += beg.magic; |
| 364 | // em.merge(ownerPT); |
| 365 | |
| 366 | // // 6. 更新求种状态 |
| 367 | // beg.hasseed = true; |
| 368 | // em.merge(beg); |
| 369 | |
| 370 | // logger.info("Reward {} magic points awarded to user {} for beg {}", |
| 371 | // beg.magic, ownerId, beg.begid); |
| 372 | // } |
| 373 | // } |
| 374 | // } |
| 375 | |
| 376 | // tx.commit(); |
| 377 | // logger.info("Successfully settled {} expired beg requests", expiredBegs.size()); |
| 378 | |
| 379 | // } catch (Exception e) { |
| 380 | // if (tx != null && tx.isActive()) { |
| 381 | // tx.rollback(); |
| 382 | // } |
| 383 | // logger.error("Error settling beg requests: {}", e.getMessage(), e); |
| 384 | // } finally { |
| 385 | // if (em != null) { |
| 386 | // em.close(); |
| 387 | // } |
| 388 | // } |
| 389 | // } |
| 390 | |
| 391 | // @Override |
| 392 | // public int AddPost(Post post) { |
| 393 | // if (post == null || post.postid == null || post.postid.isEmpty()) { |
| 394 | // logger.warn("Invalid parameter: post is null or postid is empty"); |
| 395 | // return 2; |
| 396 | // } |
| 397 | |
| 398 | // EntityManager em = null; |
| 399 | // EntityTransaction tx = null; |
| 400 | |
| 401 | // try { |
| 402 | // em = emf.createEntityManager(); |
| 403 | // tx = em.getTransaction(); |
| 404 | // tx.begin(); |
| 405 | |
| 406 | // // 检查是否重复 |
| 407 | // Post existingPost = em.find(Post.class, post.postid); |
| 408 | // if (existingPost != null) { |
| 409 | // logger.warn("Post with ID {} already exists", post.postid); |
| 410 | // return 1; |
| 411 | // } |
| 412 | |
| 413 | // // 检查用户是否存在 |
| 414 | // User user = em.find(User.class, post.postuserid); |
| 415 | // if (user == null) { |
| 416 | // logger.warn("User with ID {} does not exist", post.postuserid); |
| 417 | // return 2; |
| 418 | // } |
| 419 | |
| 420 | // // 设置初始值 |
| 421 | // if (post.posttime == null) { |
| 422 | // post.posttime = new Date(); |
| 423 | // } |
| 424 | // post.replytime = 0; |
| 425 | // post.readtime = 0; |
| 426 | |
| 427 | // // 保存帖子 |
| 428 | // em.persist(post); |
| 429 | // tx.commit(); |
| 430 | |
| 431 | // logger.info("Successfully added new post with ID: {}", post.postid); |
| 432 | // return 0; |
| 433 | |
| 434 | // } catch (Exception e) { |
| 435 | // if (tx != null && tx.isActive()) { |
| 436 | // tx.rollback(); |
| 437 | // } |
| 438 | // logger.error("Error adding post: {}", e.getMessage()); |
| 439 | // return 2; |
| 440 | // } finally { |
| 441 | // if (em != null) { |
| 442 | // em.close(); |
| 443 | // } |
| 444 | // } |
| 445 | // } |
| 446 | |
| 447 | // @Override |
| 448 | // public int UpdatePost(Post post) { |
| 449 | // if (post == null || post.postid == null || post.postid.isEmpty()) { |
| 450 | // logger.warn("Invalid parameter: post is null or postid is empty"); |
| 451 | // return 2; |
| 452 | // } |
| 453 | |
| 454 | // EntityManager em = null; |
| 455 | // EntityTransaction tx = null; |
| 456 | |
| 457 | // try { |
| 458 | // em = emf.createEntityManager(); |
| 459 | // tx = em.getTransaction(); |
| 460 | // tx.begin(); |
| 461 | |
| 462 | // // 检查帖子是否存在 |
| 463 | // Post existingPost = em.find(Post.class, post.postid); |
| 464 | // if (existingPost == null) { |
| 465 | // logger.warn("Post with ID {} does not exist", post.postid); |
| 466 | // return 1; |
| 467 | // } |
| 468 | |
| 469 | // // 保持原有不可修改的字段 |
| 470 | // post.postuserid = existingPost.postuserid; |
| 471 | // post.posttime = existingPost.posttime; |
| 472 | // post.replytime = existingPost.replytime; |
| 473 | // post.readtime = existingPost.readtime; |
| 474 | |
| 475 | // // 更新帖子 |
| 476 | // em.merge(post); |
| 477 | // tx.commit(); |
| 478 | |
| 479 | // logger.info("Successfully updated post with ID: {}", post.postid); |
| 480 | // return 0; |
| 481 | |
| 482 | // } catch (Exception e) { |
| 483 | // if (tx != null && tx.isActive()) { |
| 484 | // tx.rollback(); |
| 485 | // } |
| 486 | // logger.error("Error updating post: {}", e.getMessage()); |
| 487 | // return 2; |
| 488 | // } finally { |
| 489 | // if (em != null) { |
| 490 | // em.close(); |
| 491 | // } |
| 492 | // } |
| 493 | // } |
| 494 | |
| 495 | // @Override |
| 496 | // public int DeletePost(String postid) { |
| 497 | // if (postid == null || postid.isEmpty()) { |
| 498 | // logger.warn("Invalid parameter: postid is null or empty"); |
| 499 | // return 2; |
| 500 | // } |
| 501 | |
| 502 | // EntityManager em = null; |
| 503 | // EntityTransaction tx = null; |
| 504 | |
| 505 | // try { |
| 506 | // em = emf.createEntityManager(); |
| 507 | // tx = em.getTransaction(); |
| 508 | // tx.begin(); |
| 509 | |
| 510 | // // 查找要删除的帖子 |
| 511 | // Post post = em.find(Post.class, postid); |
| 512 | // if (post == null) { |
| 513 | // logger.warn("Post with ID {} does not exist", postid); |
| 514 | // return 1; |
| 515 | // } |
| 516 | |
| 517 | // // 删除帖子(由于设置了级联删除,相关的回复会自动删除) |
| 518 | // em.remove(post); |
| 519 | // tx.commit(); |
| 520 | |
| 521 | // logger.info("Successfully deleted post with ID: {}", postid); |
| 522 | // return 0; |
| 523 | |
| 524 | // } catch (Exception e) { |
| 525 | // if (tx != null && tx.isActive()) { |
| 526 | // tx.rollback(); |
| 527 | // } |
| 528 | // logger.error("Error deleting post: {}", e.getMessage()); |
| 529 | // return 2; |
| 530 | // } finally { |
| 531 | // if (em != null) { |
| 532 | // em.close(); |
| 533 | // } |
| 534 | // } |
| 535 | // } |
| 536 | |
| 537 | // @Override |
| 538 | // public int AddComment(String postid, String userid, String comment) { |
| 539 | // if (postid == null || postid.isEmpty() || |
| 540 | // userid == null || userid.isEmpty() || |
| 541 | // comment == null || comment.isEmpty()) { |
| 542 | // logger.warn("Invalid parameters: postid, userid or comment is null or empty"); |
| 543 | // return 2; |
| 544 | // } |
| 545 | |
| 546 | // EntityManager em = null; |
| 547 | // EntityTransaction tx = null; |
| 548 | |
| 549 | // try { |
| 550 | // em = emf.createEntityManager(); |
| 551 | // tx = em.getTransaction(); |
| 552 | // tx.begin(); |
| 553 | |
| 554 | // // 检查帖子是否存在 |
| 555 | // Post post = em.find(Post.class, postid); |
| 556 | // if (post == null) { |
| 557 | // logger.warn("Post with ID {} does not exist", postid); |
| 558 | // return 1; |
| 559 | // } |
| 560 | |
| 561 | // // 检查评论用户是否存在 |
| 562 | // User user = em.find(User.class, userid); |
| 563 | // if (user == null) { |
| 564 | // logger.warn("User with ID {} does not exist", userid); |
| 565 | // return 1; |
| 566 | // } |
| 567 | |
| 568 | // // 创建新的回复 |
| 569 | // PostReply reply = new PostReply(); |
| 570 | // reply.replyid = UUID.randomUUID().toString(); |
| 571 | // reply.postid = postid; |
| 572 | // reply.content = comment; |
| 573 | // reply.authorid = userid; |
| 574 | // reply.createdAt = new Date(); |
| 575 | |
| 576 | // // 保存回复 |
| 577 | // em.persist(reply); |
| 578 | |
| 579 | // // 更新帖子的回复数 |
| 580 | // post.replytime += 1; |
| 581 | // em.merge(post); |
| 582 | |
| 583 | // tx.commit(); |
| 584 | |
| 585 | // logger.info("Successfully added comment by user {} to post {}, reply ID: {}", |
| 586 | // userid, postid, reply.replyid); |
| 587 | // return 0; |
| 588 | |
| 589 | // } catch (Exception e) { |
| 590 | // if (tx != null && tx.isActive()) { |
| 591 | // tx.rollback(); |
| 592 | // } |
| 593 | // logger.error("Error adding comment: {}", e.getMessage()); |
| 594 | // return 2; |
| 595 | // } finally { |
| 596 | // if (em != null) { |
| 597 | // em.close(); |
| 598 | // } |
| 599 | // } |
| 600 | // } |
| 601 | |
| 602 | // @Override |
| 603 | // public int DeleteComment(String postid, String commentid) { |
| 604 | // if (postid == null || postid.isEmpty() || commentid == null || commentid.isEmpty()) { |
| 605 | // logger.warn("Invalid parameters: postid or commentid is null or empty"); |
| 606 | // return 2; |
| 607 | // } |
| 608 | |
| 609 | // EntityManager em = null; |
| 610 | // EntityTransaction tx = null; |
| 611 | |
| 612 | // try { |
| 613 | // em = emf.createEntityManager(); |
| 614 | // tx = em.getTransaction(); |
| 615 | // tx.begin(); |
| 616 | |
| 617 | // // 检查帖子是否存在 |
| 618 | // Post post = em.find(Post.class, postid); |
| 619 | // if (post == null) { |
| 620 | // logger.warn("Post with ID {} does not exist", postid); |
| 621 | // return 1; |
| 622 | // } |
| 623 | |
| 624 | // // 检查评论是否存在且属于该帖子 |
| 625 | // PostReply reply = em.find(PostReply.class, commentid); |
| 626 | // if (reply == null || !reply.postid.equals(postid)) { |
| 627 | // logger.warn("Comment {} does not exist or does not belong to post {}", commentid, postid); |
| 628 | // return 1; |
| 629 | // } |
| 630 | |
| 631 | // // 删除评论 |
| 632 | // em.remove(reply); |
| 633 | |
| 634 | // // 更新帖子的回复数 |
| 635 | // post.replytime = Math.max(0, post.replytime - 1); |
| 636 | // em.merge(post); |
| 637 | |
| 638 | // tx.commit(); |
| 639 | |
| 640 | // logger.info("Successfully deleted comment {} from post {}", commentid, postid); |
| 641 | // return 0; |
| 642 | |
| 643 | // } catch (Exception e) { |
| 644 | // if (tx != null && tx.isActive()) { |
| 645 | // tx.rollback(); |
| 646 | // } |
| 647 | // logger.error("Error deleting comment: {}", e.getMessage()); |
| 648 | // return 2; |
| 649 | // } finally { |
| 650 | // if (em != null) { |
| 651 | // em.close(); |
| 652 | // } |
| 653 | // } |
| 654 | // } |
| 655 | |
| 656 | // @Override |
| 657 | // public boolean ExchangeMagicToUpload(String userid, int magic) { |
| 658 | // if (userid == null || userid.isEmpty() || magic <= 0) { |
| 659 | // logger.warn("参数无效: userid为空或magic <= 0"); |
| 660 | // return false; |
| 661 | // } |
| 662 | |
| 663 | // EntityManager em = null; |
| 664 | // EntityTransaction tx = null; |
| 665 | |
| 666 | // try { |
| 667 | // em = emf.createEntityManager(); |
| 668 | // tx = em.getTransaction(); |
| 669 | // tx.begin(); |
| 670 | |
| 671 | // UserPT userPT = em.find(UserPT.class, userid); |
| 672 | // if (userPT == null) { |
| 673 | // logger.warn("未找到用户 {} 的PT信息", userid); |
| 674 | // return false; |
| 675 | // } |
| 676 | |
| 677 | // if (userPT.magic < magic) { |
| 678 | // logger.warn("用户 {} 的魔力值不足", userid); |
| 679 | // return false; |
| 680 | // } |
| 681 | |
| 682 | // // 1:1兑换,直接加上魔力值(假设单位是MB) |
| 683 | // userPT.magic -= magic; |
| 684 | // userPT.upload += magic; |
| 685 | |
| 686 | // // 更新分享率 |
| 687 | // if (userPT.download > 0) { |
| 688 | // userPT.share = (double) userPT.upload / userPT.download; |
| 689 | // } |
| 690 | |
| 691 | // em.merge(userPT); |
| 692 | // tx.commit(); |
| 693 | |
| 694 | // logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 上传量", userid, magic, magic); |
| 695 | // return true; |
| 696 | |
| 697 | // } catch (Exception e) { |
| 698 | // if (tx != null && tx.isActive()) { |
| 699 | // tx.rollback(); |
| 700 | // } |
| 701 | // logger.error("魔力值兑换上传量时发生错误: {}", e.getMessage()); |
| 702 | // return false; |
| 703 | // } finally { |
| 704 | // if (em != null) { |
| 705 | // em.close(); |
| 706 | // } |
| 707 | // } |
| 708 | // } |
| 709 | |
| 710 | // @Override |
| 711 | // public boolean ExchangeMagicToDownload(String userid, int magic) { |
| 712 | // if (userid == null || userid.isEmpty() || magic <= 0) { |
| 713 | // logger.warn("参数无效: userid为空或magic <= 0"); |
| 714 | // return false; |
| 715 | // } |
| 716 | |
| 717 | // EntityManager em = null; |
| 718 | // EntityTransaction tx = null; |
| 719 | |
| 720 | // try { |
| 721 | // em = emf.createEntityManager(); |
| 722 | // tx = em.getTransaction(); |
| 723 | // tx.begin(); |
| 724 | |
| 725 | // UserPT userPT = em.find(UserPT.class, userid); |
| 726 | // if (userPT == null) { |
| 727 | // logger.warn("未找到用户 {} 的PT信息", userid); |
| 728 | // return false; |
| 729 | // } |
| 730 | |
| 731 | // if (userPT.magic < magic) { |
| 732 | // logger.warn("用户 {} 的魔力值不足", userid); |
| 733 | // return false; |
| 734 | // } |
| 735 | |
| 736 | // // 1:1兑换,直接减去魔力值对应的下载量(假设单位是MB) |
| 737 | // userPT.magic -= magic; |
| 738 | // userPT.download = Math.max(0, userPT.download - magic); |
| 739 | |
| 740 | // // 更新分享率 |
| 741 | // if (userPT.download > 0) { |
| 742 | // userPT.share = (double) userPT.upload / userPT.download; |
| 743 | // } |
| 744 | |
| 745 | // em.merge(userPT); |
| 746 | // tx.commit(); |
| 747 | |
| 748 | // logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 下载量减免", userid, magic, magic); |
| 749 | // return true; |
| 750 | |
| 751 | // } catch (Exception e) { |
| 752 | // if (tx != null && tx.isActive()) { |
| 753 | // tx.rollback(); |
| 754 | // } |
| 755 | // logger.error("魔力值兑换下载量时发生错误: {}", e.getMessage()); |
| 756 | // return false; |
| 757 | // } finally { |
| 758 | // if (em != null) { |
| 759 | // em.close(); |
| 760 | // } |
| 761 | // } |
| 762 | // } |
| 763 | |
| 764 | // @Override |
| 765 | // public boolean ExchangeMagicToVip(String userid, int magic) { |
| 766 | // if (userid == null || userid.isEmpty() || magic <= 0) { |
| 767 | // logger.warn("参数无效: userid为空或magic <= 0"); |
| 768 | // return false; |
| 769 | // } |
| 770 | |
| 771 | // EntityManager em = null; |
| 772 | // EntityTransaction tx = null; |
| 773 | |
| 774 | // try { |
| 775 | // em = emf.createEntityManager(); |
| 776 | // tx = em.getTransaction(); |
| 777 | // tx.begin(); |
| 778 | |
| 779 | // UserPT userPT = em.find(UserPT.class, userid); |
| 780 | // if (userPT == null) { |
| 781 | // logger.warn("未找到用户 {} 的PT信息", userid); |
| 782 | // return false; |
| 783 | // } |
| 784 | |
| 785 | // if (userPT.magic < magic) { |
| 786 | // logger.warn("用户 {} 的魔力值不足", userid); |
| 787 | // return false; |
| 788 | // } |
| 789 | |
| 790 | // // 1:1兑换VIP下载次数 |
| 791 | // userPT.magic -= magic; |
| 792 | // userPT.viptime += magic; |
| 793 | |
| 794 | // em.merge(userPT); |
| 795 | // tx.commit(); |
| 796 | |
| 797 | // logger.info("用户 {} 成功将 {} 点魔力值兑换为 {} 次VIP下载次数", userid, magic, magic); |
| 798 | // return true; |
| 799 | |
| 800 | // } catch (Exception e) { |
| 801 | // if (tx != null && tx.isActive()) { |
| 802 | // tx.rollback(); |
| 803 | // } |
| 804 | // logger.error("魔力值兑换VIP下载次数时发生错误: {}", e.getMessage()); |
| 805 | // return false; |
| 806 | // } finally { |
| 807 | // if (em != null) { |
| 808 | // em.close(); |
| 809 | // } |
| 810 | // } |
| 811 | // } |
| 812 | |
| 813 | // @Override |
| 814 | // public boolean UploadTransmitProfile(Profile profile) { |
| 815 | // if (profile == null || profile.profileurl == null || profile.profileurl.isEmpty() || |
| 816 | // profile.userid == null || profile.userid.isEmpty()) { |
| 817 | // logger.warn("参数无效: profile为空或必要字段为空"); |
| 818 | // return false; |
| 819 | // } |
| 820 | |
| 821 | // EntityManager em = null; |
| 822 | // EntityTransaction tx = null; |
| 823 | |
| 824 | // try { |
| 825 | // em = emf.createEntityManager(); |
| 826 | // tx = em.getTransaction(); |
| 827 | // tx.begin(); |
| 828 | |
| 829 | // // 检查用户是否存在 |
| 830 | // User user = em.find(User.class, profile.userid); |
| 831 | // if (user == null) { |
| 832 | // logger.warn("用户 {} 不存在", profile.userid); |
| 833 | // return false; |
| 834 | // } |
| 835 | |
| 836 | // // 检查是否已存在相同的迁移申请 |
| 837 | // Profile existingProfile = em.find(Profile.class, profile.profileurl); |
| 838 | // if (existingProfile != null) { |
| 839 | // logger.warn("迁移申请 {} 已存在", profile.profileurl); |
| 840 | // return false; |
| 841 | // } |
| 842 | |
| 843 | // // 设置初始值 |
| 844 | // profile.exampass = false; |
| 845 | // profile.magicgived = "0"; |
| 846 | // profile.uploadgived = "0"; |
| 847 | |
| 848 | // // 保存迁移申请 |
| 849 | // em.persist(profile); |
| 850 | // tx.commit(); |
| 851 | |
| 852 | // logger.info("成功上传迁移申请 {}", profile.profileurl); |
| 853 | // return true; |
| 854 | |
| 855 | // } catch (Exception e) { |
| 856 | // if (tx != null && tx.isActive()) { |
| 857 | // tx.rollback(); |
| 858 | // } |
| 859 | // logger.error("上传迁移申请时发生错误: {}", e.getMessage()); |
| 860 | // return false; |
| 861 | // } finally { |
| 862 | // if (em != null) { |
| 863 | // em.close(); |
| 864 | // } |
| 865 | // } |
| 866 | // } |
| 867 | |
| 868 | // @Override |
| 869 | // public Profile GetTransmitProfile(String profileid) { |
| 870 | // if (profileid == null || profileid.isEmpty()) { |
| 871 | // logger.warn("参数无效: profileid为空"); |
| 872 | // return null; |
| 873 | // } |
| 874 | |
| 875 | // EntityManager em = null; |
| 876 | |
| 877 | // try { |
| 878 | // em = emf.createEntityManager(); |
| 879 | // Profile profile = em.find(Profile.class, profileid); |
| 880 | // if (profile == null) { |
| 881 | // logger.warn("未找到迁移申请 {}", profileid); |
| 882 | // return null; |
| 883 | // } |
| 884 | |
| 885 | // logger.info("成功获取迁移申请 {}", profileid); |
| 886 | // return profile; |
| 887 | |
| 888 | // } catch (Exception e) { |
| 889 | // logger.error("获取迁移申请时发生错误: {}", e.getMessage()); |
| 890 | // return null; |
| 891 | // } finally { |
| 892 | // if (em != null) { |
| 893 | // em.close(); |
| 894 | // } |
| 895 | // } |
| 896 | // } |
| 897 | |
| 898 | // @Override |
| 899 | // public boolean ExamTransmitProfile(String profileid, boolean result) { |
| 900 | // if (profileid == null || profileid.isEmpty()) { |
| 901 | // logger.warn("参数无效: profileid为空"); |
| 902 | // return false; |
| 903 | // } |
| 904 | |
| 905 | // EntityManager em = null; |
| 906 | // EntityTransaction tx = null; |
| 907 | |
| 908 | // try { |
| 909 | // em = emf.createEntityManager(); |
| 910 | // tx = em.getTransaction(); |
| 911 | // tx.begin(); |
| 912 | |
| 913 | // Profile profile = em.find(Profile.class, profileid); |
| 914 | // if (profile == null) { |
| 915 | // logger.warn("未找到迁移申请 {}", profileid); |
| 916 | // return false; |
| 917 | // } |
| 918 | |
| 919 | // if (profile.exampass != result) { |
| 920 | // profile.exampass = result; |
| 921 | |
| 922 | // if (result) { |
| 923 | // // 如果审核通过,更新用户的PT信息 |
| 924 | // UserPT userPT = em.find(UserPT.class, profile.userid); |
| 925 | // if (userPT != null) { |
| 926 | // // 发放魔力值 |
| 927 | // int magicToGive = Integer.parseInt(profile.magictogive); |
| 928 | // userPT.magic += magicToGive; |
| 929 | // profile.magicgived = String.valueOf(magicToGive); |
| 930 | |
| 931 | // // 发放上传量 |
| 932 | // long uploadToGive = Long.parseLong(profile.uploadtogive); |
| 933 | // userPT.upload += uploadToGive; |
| 934 | // profile.uploadgived = String.valueOf(uploadToGive); |
| 935 | |
| 936 | // em.merge(userPT); |
| 937 | // } |
| 938 | // } |
| 939 | |
| 940 | // em.merge(profile); |
| 941 | // } |
| 942 | |
| 943 | // tx.commit(); |
| 944 | |
| 945 | // logger.info("成功审核迁移申请 {}, 结果: {}", profileid, result); |
| 946 | // return true; |
| 947 | |
| 948 | // } catch (Exception e) { |
| 949 | // if (tx != null && tx.isActive()) { |
| 950 | // tx.rollback(); |
| 951 | // } |
| 952 | // logger.error("审核迁移申请时发生错误: {}", e.getMessage()); |
| 953 | // return false; |
| 954 | // } finally { |
| 955 | // if (em != null) { |
| 956 | // em.close(); |
| 957 | // } |
| 958 | // } |
| 959 | // } |
| 960 | |
| 961 | // @Override |
| 962 | // public Profile[] GetTransmitProfileList() { |
| 963 | // EntityManager em = null; |
| 964 | |
| 965 | // try { |
| 966 | // em = emf.createEntityManager(); |
| 967 | |
| 968 | // // 获取所有迁移申请 |
| 969 | // QProfile p = QProfile.profile; |
| 970 | // List<Profile> profiles = new JPAQuery<>(em) |
| 971 | // .select(p) |
| 972 | // .from(p) |
| 973 | // .fetch(); |
| 974 | |
| 975 | // logger.info("成功获取所有迁移申请,共 {} 条", profiles.size()); |
| 976 | // return profiles.toArray(new Profile[0]); |
| 977 | |
| 978 | // } catch (Exception e) { |
| 979 | // logger.error("获取迁移申请列表时发生错误: {}", e.getMessage()); |
| 980 | // return new Profile[0]; |
| 981 | // } finally { |
| 982 | // if (em != null) { |
| 983 | // em.close(); |
| 984 | // } |
| 985 | // } |
| 986 | // } |
| 987 | // } |