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