blob: 6334ebb79b74957adfda9ae611be81dd1b4d12ae [file] [log] [blame]
rootcd436562025-05-08 14:09:19 +00001package tracker;
2
root0d8b11f2025-05-15 14:10:43 +00003import java.io.File;
rootff0769a2025-05-18 17:24:41 +00004import java.util.HashMap;
5import java.util.Map;
6
root0d8b11f2025-05-15 14:10:43 +00007import javax.persistence.EntityManager;
8import javax.persistence.EntityManagerFactory;
9import javax.persistence.EntityTransaction;
10import javax.persistence.Persistence;
rootff0769a2025-05-18 17:24:41 +000011
root0d8b11f2025-05-15 14:10:43 +000012import com.querydsl.jpa.impl.JPAUpdateClause;
rootff0769a2025-05-18 17:24:41 +000013
rootf35409f2025-05-19 04:41:57 +000014import entity.QUserPT;
rootff0769a2025-05-18 17:24:41 +000015import entity.TTorent;
16import entity.TransRecord;
17import entity.config;
root0d8b11f2025-05-15 14:10:43 +000018
19public class Tracker implements TrackerInterface {
rootff0769a2025-05-18 17:24:41 +000020 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 }
root0d8b11f2025-05-15 14:10:43 +000037
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();
rootff0769a2025-05-18 17:24:41 +000050 // 成功时 updated>0 返回 false,失败时返回 true
51 return updated <= 0;
root0d8b11f2025-05-15 14:10:43 +000052 } catch(Exception e) {
53 if (tx.isActive()) tx.rollback();
rootff0769a2025-05-18 17:24:41 +000054 return true;
root0d8b11f2025-05-15 14:10:43 +000055 } finally {
56 em.close();
57 }
58 }
59
60 @Override
rootff0769a2025-05-18 17:24:41 +000061 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 }
root0d8b11f2025-05-15 14:10:43 +000081
82 @Override
rootf35409f2025-05-19 04:41:57 +000083 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 }
root0d8b11f2025-05-15 14:10:43 +0000102
103 @Override
rootf35409f2025-05-19 04:41:57 +0000104 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 }
root0d8b11f2025-05-15 14:10:43 +0000123
124 @Override
rootf35409f2025-05-19 04:41:57 +0000125 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 }
root0d8b11f2025-05-15 14:10:43 +0000144
145 @Override
rootf35409f2025-05-19 04:41:57 +0000146 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
root0d8b11f2025-05-15 14:10:43 +0000166
167 @Override
rootf35409f2025-05-19 04:41:57 +0000168 public int SaveTorrent(File TTorent){
root0d8b11f2025-05-15 14:10:43 +0000169 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
rootf35409f2025-05-19 04:41:57 +0000179 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 }
root0d8b11f2025-05-15 14:10:43 +0000195}