blob: 6334ebb79b74957adfda9ae611be81dd1b4d12ae [file] [log] [blame]
package tracker;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import com.querydsl.jpa.impl.JPAUpdateClause;
import entity.QUserPT;
import entity.TTorent;
import entity.TransRecord;
import entity.config;
public class Tracker implements TrackerInterface {
private final EntityManagerFactory emf;
// 默认构造:产线数据库
public Tracker() {
config cfg = new config();
Map<String,Object> props = new HashMap<>();
props.put("javax.persistence.jdbc.url",
"jdbc:mysql://" + cfg.SqlURL + "/" + cfg.Database);
props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
this.emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
}
// 测试传入:测试库
public Tracker(EntityManagerFactory emf) {
this.emf = emf;
}
@Override
public boolean AddUpLoad(String userid, int upload) {
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
QUserPT q = QUserPT.userPT;
long updated = new JPAUpdateClause(em, q)
.where(q.userid.eq(userid))
.set(q.upload, q.upload.add(upload))
.execute();
tx.commit();
// 成功时 updated>0 返回 false,失败时返回 true
return updated <= 0;
} catch(Exception e) {
if (tx.isActive()) tx.rollback();
return true;
} finally {
em.close();
}
}
@Override
public boolean ReduceUpLoad(String userid, int upload){
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
QUserPT q = QUserPT.userPT;
long updated = new JPAUpdateClause(em, q)
.where(q.userid.eq(userid))
.set(q.upload, q.upload.subtract(upload))
.execute();
tx.commit();
// 成功时 updated>0 返回 false,失败时返回 true
return updated <= 0;
} catch(Exception e) {
if (tx.isActive()) tx.rollback();
return true;
} finally {
em.close();
}
}
@Override
public boolean AddDownload(String userid, int download) {
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
QUserPT q = QUserPT.userPT;
long updated = new JPAUpdateClause(em, q)
.where(q.userid.eq(userid))
.set(q.download, q.download.add(download))
.execute();
tx.commit();
return updated <= 0;
} catch(Exception e) {
if (tx.isActive()) tx.rollback();
return true;
} finally {
em.close();
}
}
@Override
public boolean ReduceDownload(String userid, int download) {
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
QUserPT q = QUserPT.userPT;
long updated = new JPAUpdateClause(em, q)
.where(q.userid.eq(userid))
.set(q.download, q.download.subtract(download))
.execute();
tx.commit();
return updated <= 0;
} catch(Exception e) {
if (tx.isActive()) tx.rollback();
return true;
} finally {
em.close();
}
}
@Override
public boolean AddMagic(String userid, int magic) {
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
QUserPT q = QUserPT.userPT;
long updated = new JPAUpdateClause(em, q)
.where(q.userid.eq(userid))
.set(q.magic, q.magic.add(magic))
.execute();
tx.commit();
return updated <= 0;
} catch(Exception e) {
if (tx.isActive()) tx.rollback();
return true;
} finally {
em.close();
}
}
@Override
public boolean ReduceMagic(String userid, int magic) {
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
QUserPT q = QUserPT.userPT;
long updated = new JPAUpdateClause(em, q)
.where(q.userid.eq(userid))
.set(q.magic, q.magic.subtract(magic))
.execute();
tx.commit();
return updated <= 0;
} catch(Exception e) {
if (tx.isActive()) tx.rollback();
return true;
} finally {
em.close();
}
}
@Override
public int SaveTorrent(File TTorent){
return 0;
};//保存seedid对应的ttorent信息
@Override
public File GetTTorent(String seedid,String userid){
return null;
};//根据种子id获得ttorent信息然后构建Ttorent文件并返回,同时记录用户的下载行为
@Override
public int AddRecord(TransRecord rd){
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
em.persist(rd);
tx.commit();
// 持久化成功,返回1表示插入成功
return 1;
} catch (Exception e) {
if (tx.isActive()) tx.rollback();
return -1;
} finally {
em.close();
}
}
}