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 | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 4 | import java.util.HashMap; |
| 5 | import java.util.Map; |
| 6 | |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 7 | import javax.persistence.EntityManager; |
| 8 | import javax.persistence.EntityManagerFactory; |
| 9 | import javax.persistence.EntityTransaction; |
| 10 | import javax.persistence.Persistence; |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 11 | |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 12 | import com.querydsl.jpa.impl.JPAUpdateClause; |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 13 | |
root | f35409f | 2025-05-19 04:41:57 +0000 | [diff] [blame^] | 14 | import entity.QUserPT; |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 15 | import entity.TTorent; |
| 16 | import entity.TransRecord; |
| 17 | import entity.config; |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 18 | |
| 19 | public class Tracker implements TrackerInterface { |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 20 | private final EntityManagerFactory emf; |
| 21 | |
| 22 | // 默认构造:产线数据库 |
| 23 | public Tracker() { |
| 24 | config cfg = new config(); |
| 25 | Map<String,Object> props = new HashMap<>(); |
| 26 | props.put("javax.persistence.jdbc.url", |
| 27 | "jdbc:mysql://" + cfg.SqlURL + "/" + cfg.Database); |
| 28 | props.put("javax.persistence.jdbc.user", cfg.SqlUsername); |
| 29 | props.put("javax.persistence.jdbc.password", cfg.SqlPassword); |
| 30 | this.emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props); |
| 31 | } |
| 32 | |
| 33 | // 测试传入:测试库 |
| 34 | public Tracker(EntityManagerFactory emf) { |
| 35 | this.emf = emf; |
| 36 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 37 | |
| 38 | @Override |
| 39 | public boolean AddUpLoad(String userid, int upload) { |
| 40 | EntityManager em = emf.createEntityManager(); |
| 41 | EntityTransaction tx = em.getTransaction(); |
| 42 | try { |
| 43 | tx.begin(); |
| 44 | QUserPT q = QUserPT.userPT; |
| 45 | long updated = new JPAUpdateClause(em, q) |
| 46 | .where(q.userid.eq(userid)) |
| 47 | .set(q.upload, q.upload.add(upload)) |
| 48 | .execute(); |
| 49 | tx.commit(); |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 50 | // 成功时 updated>0 返回 false,失败时返回 true |
| 51 | return updated <= 0; |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 52 | } catch(Exception e) { |
| 53 | if (tx.isActive()) tx.rollback(); |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 54 | return true; |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 55 | } finally { |
| 56 | em.close(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @Override |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 61 | public boolean ReduceUpLoad(String userid, int upload){ |
| 62 | EntityManager em = emf.createEntityManager(); |
| 63 | EntityTransaction tx = em.getTransaction(); |
| 64 | try { |
| 65 | tx.begin(); |
| 66 | QUserPT q = QUserPT.userPT; |
| 67 | long updated = new JPAUpdateClause(em, q) |
| 68 | .where(q.userid.eq(userid)) |
| 69 | .set(q.upload, q.upload.subtract(upload)) |
| 70 | .execute(); |
| 71 | tx.commit(); |
| 72 | // 成功时 updated>0 返回 false,失败时返回 true |
| 73 | return updated <= 0; |
| 74 | } catch(Exception e) { |
| 75 | if (tx.isActive()) tx.rollback(); |
| 76 | return true; |
| 77 | } finally { |
| 78 | em.close(); |
| 79 | } |
| 80 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 81 | |
| 82 | @Override |
root | f35409f | 2025-05-19 04:41:57 +0000 | [diff] [blame^] | 83 | public boolean AddDownload(String userid, int download) { |
| 84 | EntityManager em = emf.createEntityManager(); |
| 85 | EntityTransaction tx = em.getTransaction(); |
| 86 | try { |
| 87 | tx.begin(); |
| 88 | QUserPT q = QUserPT.userPT; |
| 89 | long updated = new JPAUpdateClause(em, q) |
| 90 | .where(q.userid.eq(userid)) |
| 91 | .set(q.download, q.download.add(download)) |
| 92 | .execute(); |
| 93 | tx.commit(); |
| 94 | return updated <= 0; |
| 95 | } catch(Exception e) { |
| 96 | if (tx.isActive()) tx.rollback(); |
| 97 | return true; |
| 98 | } finally { |
| 99 | em.close(); |
| 100 | } |
| 101 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 102 | |
| 103 | @Override |
root | f35409f | 2025-05-19 04:41:57 +0000 | [diff] [blame^] | 104 | public boolean ReduceDownload(String userid, int download) { |
| 105 | EntityManager em = emf.createEntityManager(); |
| 106 | EntityTransaction tx = em.getTransaction(); |
| 107 | try { |
| 108 | tx.begin(); |
| 109 | QUserPT q = QUserPT.userPT; |
| 110 | long updated = new JPAUpdateClause(em, q) |
| 111 | .where(q.userid.eq(userid)) |
| 112 | .set(q.download, q.download.subtract(download)) |
| 113 | .execute(); |
| 114 | tx.commit(); |
| 115 | return updated <= 0; |
| 116 | } catch(Exception e) { |
| 117 | if (tx.isActive()) tx.rollback(); |
| 118 | return true; |
| 119 | } finally { |
| 120 | em.close(); |
| 121 | } |
| 122 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 123 | |
| 124 | @Override |
root | f35409f | 2025-05-19 04:41:57 +0000 | [diff] [blame^] | 125 | public boolean AddMagic(String userid, int magic) { |
| 126 | EntityManager em = emf.createEntityManager(); |
| 127 | EntityTransaction tx = em.getTransaction(); |
| 128 | try { |
| 129 | tx.begin(); |
| 130 | QUserPT q = QUserPT.userPT; |
| 131 | long updated = new JPAUpdateClause(em, q) |
| 132 | .where(q.userid.eq(userid)) |
| 133 | .set(q.magic, q.magic.add(magic)) |
| 134 | .execute(); |
| 135 | tx.commit(); |
| 136 | return updated <= 0; |
| 137 | } catch(Exception e) { |
| 138 | if (tx.isActive()) tx.rollback(); |
| 139 | return true; |
| 140 | } finally { |
| 141 | em.close(); |
| 142 | } |
| 143 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 144 | |
| 145 | @Override |
root | f35409f | 2025-05-19 04:41:57 +0000 | [diff] [blame^] | 146 | public boolean ReduceMagic(String userid, int magic) { |
| 147 | EntityManager em = emf.createEntityManager(); |
| 148 | EntityTransaction tx = em.getTransaction(); |
| 149 | try { |
| 150 | tx.begin(); |
| 151 | QUserPT q = QUserPT.userPT; |
| 152 | long updated = new JPAUpdateClause(em, q) |
| 153 | .where(q.userid.eq(userid)) |
| 154 | .set(q.magic, q.magic.subtract(magic)) |
| 155 | .execute(); |
| 156 | tx.commit(); |
| 157 | return updated <= 0; |
| 158 | } catch(Exception e) { |
| 159 | if (tx.isActive()) tx.rollback(); |
| 160 | return true; |
| 161 | } finally { |
| 162 | em.close(); |
| 163 | } |
| 164 | } |
| 165 | |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 166 | |
| 167 | @Override |
root | f35409f | 2025-05-19 04:41:57 +0000 | [diff] [blame^] | 168 | public int SaveTorrent(File TTorent){ |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 169 | return 0; |
| 170 | };//保存seedid对应的ttorent信息 |
| 171 | |
| 172 | @Override |
| 173 | public File GetTTorent(String seedid,String userid){ |
| 174 | return null; |
| 175 | };//根据种子id获得ttorent信息然后构建Ttorent文件并返回,同时记录用户的下载行为 |
| 176 | |
| 177 | |
| 178 | @Override |
root | f35409f | 2025-05-19 04:41:57 +0000 | [diff] [blame^] | 179 | public int AddRecord(TransRecord rd){ |
| 180 | EntityManager em = emf.createEntityManager(); |
| 181 | EntityTransaction tx = em.getTransaction(); |
| 182 | try { |
| 183 | tx.begin(); |
| 184 | em.persist(rd); |
| 185 | tx.commit(); |
| 186 | // 持久化成功,返回1表示插入成功 |
| 187 | return 1; |
| 188 | } catch (Exception e) { |
| 189 | if (tx.isActive()) tx.rollback(); |
| 190 | return -1; |
| 191 | } finally { |
| 192 | em.close(); |
| 193 | } |
| 194 | } |
root | 0d8b11f | 2025-05-15 14:10:43 +0000 | [diff] [blame] | 195 | } |