root | cd43656 | 2025-05-08 14:09:19 +0000 | [diff] [blame] | 1 | package tracker; |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 2 | import java.io.File; |
root | d4959a8 | 2025-05-27 07:07:37 +0000 | [diff] [blame] | 3 | import java.nio.file.Files; |
| 4 | import java.nio.file.Path; |
| 5 | import java.nio.file.Paths; |
| 6 | import java.nio.file.StandardCopyOption; |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 7 | import java.util.HashMap; |
| 8 | import java.util.Map; |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 9 | import javax.persistence.EntityManager; |
| 10 | import javax.persistence.EntityManagerFactory; |
| 11 | import javax.persistence.EntityTransaction; |
| 12 | import javax.persistence.Persistence; |
| 13 | import com.querydsl.jpa.impl.JPAUpdateClause; |
root | f35409f | 2025-05-19 04:41:57 +0000 | [diff] [blame] | 14 | import entity.QUserPT; |
root | d4959a8 | 2025-05-27 07:07:37 +0000 | [diff] [blame] | 15 | import entity.Seed; |
| 16 | import entity.SeedDownload; |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 17 | import entity.TransRecord; |
| 18 | import entity.config; |
root | d4959a8 | 2025-05-27 07:07:37 +0000 | [diff] [blame] | 19 | import entity.TTorent; |
| 20 | import java.time.LocalDateTime; |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 21 | public class Tracker implements TrackerInterface { |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 22 | private final EntityManagerFactory emf; |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 23 | // 默认构造:产线数据库 |
| 24 | public Tracker() { |
| 25 | config cfg = new config(); |
| 26 | Map<String,Object> props = new HashMap<>(); |
| 27 | props.put("javax.persistence.jdbc.url", |
| 28 | "jdbc:mysql://" + cfg.SqlURL + "/" + cfg.Database); |
| 29 | props.put("javax.persistence.jdbc.user", cfg.SqlUsername); |
| 30 | props.put("javax.persistence.jdbc.password", cfg.SqlPassword); |
| 31 | this.emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props); |
| 32 | } |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 33 | // 测试传入:测试库 |
| 34 | public Tracker(EntityManagerFactory emf) { |
| 35 | this.emf = emf; |
| 36 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 37 | @Override |
| 38 | public boolean AddUpLoad(String userid, int upload) { |
| 39 | EntityManager em = emf.createEntityManager(); |
| 40 | EntityTransaction tx = em.getTransaction(); |
| 41 | try { |
| 42 | tx.begin(); |
| 43 | QUserPT q = QUserPT.userPT; |
| 44 | long updated = new JPAUpdateClause(em, q) |
| 45 | .where(q.userid.eq(userid)) |
| 46 | .set(q.upload, q.upload.add(upload)) |
| 47 | .execute(); |
| 48 | tx.commit(); |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 49 | // 成功时 updated>0 返回 false,失败时返回 true |
| 50 | return updated <= 0; |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 51 | } catch(Exception e) { |
| 52 | if (tx.isActive()) tx.rollback(); |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 53 | return true; |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 54 | } finally { |
| 55 | em.close(); |
| 56 | } |
| 57 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 58 | @Override |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 59 | public boolean ReduceUpLoad(String userid, int upload){ |
| 60 | EntityManager em = emf.createEntityManager(); |
| 61 | EntityTransaction tx = em.getTransaction(); |
| 62 | try { |
| 63 | tx.begin(); |
| 64 | QUserPT q = QUserPT.userPT; |
| 65 | long updated = new JPAUpdateClause(em, q) |
| 66 | .where(q.userid.eq(userid)) |
| 67 | .set(q.upload, q.upload.subtract(upload)) |
| 68 | .execute(); |
| 69 | tx.commit(); |
| 70 | // 成功时 updated>0 返回 false,失败时返回 true |
| 71 | return updated <= 0; |
| 72 | } catch(Exception e) { |
| 73 | if (tx.isActive()) tx.rollback(); |
| 74 | return true; |
| 75 | } finally { |
| 76 | em.close(); |
| 77 | } |
| 78 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 79 | @Override |
root | f35409f | 2025-05-19 04:41:57 +0000 | [diff] [blame] | 80 | public boolean AddDownload(String userid, int download) { |
| 81 | EntityManager em = emf.createEntityManager(); |
| 82 | EntityTransaction tx = em.getTransaction(); |
| 83 | try { |
| 84 | tx.begin(); |
| 85 | QUserPT q = QUserPT.userPT; |
| 86 | long updated = new JPAUpdateClause(em, q) |
| 87 | .where(q.userid.eq(userid)) |
| 88 | .set(q.download, q.download.add(download)) |
| 89 | .execute(); |
| 90 | tx.commit(); |
| 91 | return updated <= 0; |
| 92 | } catch(Exception e) { |
| 93 | if (tx.isActive()) tx.rollback(); |
| 94 | return true; |
| 95 | } finally { |
| 96 | em.close(); |
| 97 | } |
| 98 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 99 | @Override |
root | f35409f | 2025-05-19 04:41:57 +0000 | [diff] [blame] | 100 | public boolean ReduceDownload(String userid, int download) { |
| 101 | EntityManager em = emf.createEntityManager(); |
| 102 | EntityTransaction tx = em.getTransaction(); |
| 103 | try { |
| 104 | tx.begin(); |
| 105 | QUserPT q = QUserPT.userPT; |
| 106 | long updated = new JPAUpdateClause(em, q) |
| 107 | .where(q.userid.eq(userid)) |
| 108 | .set(q.download, q.download.subtract(download)) |
| 109 | .execute(); |
| 110 | tx.commit(); |
| 111 | return updated <= 0; |
| 112 | } catch(Exception e) { |
| 113 | if (tx.isActive()) tx.rollback(); |
| 114 | return true; |
| 115 | } finally { |
| 116 | em.close(); |
| 117 | } |
| 118 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 119 | @Override |
root | f35409f | 2025-05-19 04:41:57 +0000 | [diff] [blame] | 120 | public boolean AddMagic(String userid, int magic) { |
| 121 | EntityManager em = emf.createEntityManager(); |
| 122 | EntityTransaction tx = em.getTransaction(); |
| 123 | try { |
| 124 | tx.begin(); |
| 125 | QUserPT q = QUserPT.userPT; |
| 126 | long updated = new JPAUpdateClause(em, q) |
| 127 | .where(q.userid.eq(userid)) |
| 128 | .set(q.magic, q.magic.add(magic)) |
| 129 | .execute(); |
| 130 | tx.commit(); |
| 131 | return updated <= 0; |
| 132 | } catch(Exception e) { |
| 133 | if (tx.isActive()) tx.rollback(); |
| 134 | return true; |
| 135 | } finally { |
| 136 | em.close(); |
| 137 | } |
| 138 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 139 | @Override |
root | f35409f | 2025-05-19 04:41:57 +0000 | [diff] [blame] | 140 | public boolean ReduceMagic(String userid, int magic) { |
| 141 | EntityManager em = emf.createEntityManager(); |
| 142 | EntityTransaction tx = em.getTransaction(); |
| 143 | try { |
| 144 | tx.begin(); |
| 145 | QUserPT q = QUserPT.userPT; |
| 146 | long updated = new JPAUpdateClause(em, q) |
| 147 | .where(q.userid.eq(userid)) |
| 148 | .set(q.magic, q.magic.subtract(magic)) |
| 149 | .execute(); |
| 150 | tx.commit(); |
| 151 | return updated <= 0; |
| 152 | } catch(Exception e) { |
| 153 | if (tx.isActive()) tx.rollback(); |
| 154 | return true; |
| 155 | } finally { |
| 156 | em.close(); |
| 157 | } |
| 158 | } |
root | d4959a8 | 2025-05-27 07:07:37 +0000 | [diff] [blame] | 159 | @Override |
| 160 | public int SaveTorrent(String seedid, File TTorent){ |
| 161 | try { |
| 162 | Path storageDir = Paths.get(config.TORRENT_STORAGE_DIR); |
| 163 | if (!Files.exists(storageDir)) { |
| 164 | Files.createDirectories(storageDir); |
| 165 | } |
| 166 | String filename = TTorent.getName(); |
| 167 | Path target = storageDir.resolve(seedid + "_" + filename); |
| 168 | Files.copy(TTorent.toPath(), target, StandardCopyOption.REPLACE_EXISTING); |
root | d4959a8 | 2025-05-27 07:07:37 +0000 | [diff] [blame] | 169 | EntityManager em = emf.createEntityManager(); |
| 170 | EntityTransaction tx = em.getTransaction(); |
| 171 | try { |
| 172 | tx.begin(); |
| 173 | Seed seed = em.find(Seed.class, seedid); |
| 174 | seed.url = target.toString(); |
| 175 | em.merge(seed); |
| 176 | tx.commit(); |
| 177 | return 0; |
| 178 | } catch (Exception e) { |
| 179 | if (tx.isActive()) tx.rollback(); |
| 180 | return 1; |
| 181 | } finally { |
| 182 | em.close(); |
| 183 | } |
| 184 | } catch (Exception e) { |
| 185 | return 1; |
| 186 | } |
| 187 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 188 | @Override |
root | d4959a8 | 2025-05-27 07:07:37 +0000 | [diff] [blame] | 189 | public File GetTTorent(String seedid, String userid, String ip) { |
| 190 | EntityManager em = emf.createEntityManager(); |
| 191 | EntityTransaction tx = em.getTransaction(); |
| 192 | File file = null; |
| 193 | try { |
| 194 | Seed seed = em.find(Seed.class, seedid); |
| 195 | if (seed == null || seed.url == null) { |
| 196 | return null; |
| 197 | } |
| 198 | file = new File(seed.url); |
| 199 | if (!file.exists()) { |
| 200 | return null; |
| 201 | } |
| 202 | tx.begin(); |
| 203 | SeedDownload sd = new SeedDownload(); |
| 204 | sd.seedId = seedid; |
| 205 | sd.userId = userid; |
| 206 | sd.clientIp = ip; |
| 207 | LocalDateTime now = LocalDateTime.now(); |
| 208 | sd.downloadStart = now; |
| 209 | sd.downloadEnd = now; |
| 210 | em.persist(sd); |
| 211 | tx.commit(); |
| 212 | } catch (Exception e) { |
| 213 | if (tx.isActive()) tx.rollback(); |
| 214 | // ignore persistence errors and still return the file |
| 215 | } finally { |
| 216 | em.close(); |
| 217 | } |
| 218 | return file; |
| 219 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 220 | @Override |
root | f35409f | 2025-05-19 04:41:57 +0000 | [diff] [blame] | 221 | public int AddRecord(TransRecord rd){ |
| 222 | EntityManager em = emf.createEntityManager(); |
| 223 | EntityTransaction tx = em.getTransaction(); |
| 224 | try { |
| 225 | tx.begin(); |
| 226 | em.persist(rd); |
| 227 | tx.commit(); |
| 228 | // 持久化成功,返回1表示插入成功 |
| 229 | return 1; |
| 230 | } catch (Exception e) { |
| 231 | if (tx.isActive()) tx.rollback(); |
| 232 | return -1; |
| 233 | } finally { |
| 234 | em.close(); |
| 235 | } |
| 236 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 237 | } |