| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- package modules.address.token;
- import com.jfinal.aop.Inject;
- import com.jfinal.kit.Kv;
- import com.jfinal.plugin.activerecord.Db;
- import com.jfinal.plugin.activerecord.Record;
- import common.model.Address;
- import common.model.Token;
- import common.utils.http.MyPaginate;
- import common.utils.model.ConstantUtil;
- import common.utils.model.TableUtil;
- import common.utils.util.ThreadQueryUtil;
- import modules.address.AddressService;
- import java.util.List;
- public class TokenService {
- @Inject
- private AddressService addressService;
- public boolean append(Token token, Address address) {
- boolean isAddressExist = addressService.isAddressExist(address);
- token.setId(token.getChainId()+token.getHash());
- return Db.tx(() -> {
- if (!isAddressExist) addressService.append(address);
- token.save();
- return true;
- });
- }
- public boolean update(Token token) {
- token.setId(token.getChainId()+token.getHash());
- return token.update();
- }
- public boolean isTokenExists(Token token) {
- return Db.template("hasRecord", TableUtil.getTableName(Token.class), token.getChainId(), token.getHash()).queryInt() == 1;
- }
- public Record findByChainIdAndHash(int chainId, String hash) {
- return Db.template("address.findByChainIdAndHash", TableUtil.getTableName(Token.class), chainId, hash).findFirst();
- }
- public Record findByChainIdAndHashSingle(int chainId, String hash) {
- return Db.template("address.findByChainIdAndHashSingle", TableUtil.getTableName(Token.class), chainId, hash).findFirst();
- }
- public List<Record> findByChainId(int chainId, MyPaginate p) {
- // 查询条件构建
- Kv query = Kv.of("chainId", chainId).set("tableName", "t_token");
- if(p != null){
- query.set("limit0", (p.getPageNumber() - 1) * p.getPageSize())
- .set("limit1", p.getPageSize());
- if(p.getPageSize() > ConstantUtil.THREAD_QUERY_THRESHOLD){
- return ThreadQueryUtil.findByChainIdThread(chainId, query);
- }
- } else {
- query.set("limit0", 0)
- .set("limit1", ConstantUtil.DEFAULT_ROWS_NUM);
- }
- return Db.template("address.findByChainId", query).find();
- }
-
- public Record findTokenByChainIdAndHash(int chainId, String hash) {
- return Db.template("token.findTokenByChainIdAndHash", chainId, hash).findFirst();
- }
- public int countByChainId(int chainId) {
- return addressService.countByChainId("t_token", chainId);
- }
- public int deleteByHashAndChainId(int chainId, String hash){
- return addressService.deleteByHashAndChainId("t_token", chainId, hash);
- }
- }
|