TokenService.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package modules.address.token;
  2. import com.jfinal.aop.Inject;
  3. import com.jfinal.kit.Kv;
  4. import com.jfinal.plugin.activerecord.Db;
  5. import com.jfinal.plugin.activerecord.Record;
  6. import common.model.Address;
  7. import common.model.Token;
  8. import common.utils.http.MyPaginate;
  9. import common.utils.model.ConstantUtil;
  10. import common.utils.model.TableUtil;
  11. import common.utils.util.ThreadQueryUtil;
  12. import modules.address.AddressService;
  13. import java.util.List;
  14. public class TokenService {
  15. @Inject
  16. private AddressService addressService;
  17. public boolean append(Token token, Address address) {
  18. boolean isAddressExist = addressService.isAddressExist(address);
  19. token.setId(token.getChainId()+token.getHash());
  20. return Db.tx(() -> {
  21. if (!isAddressExist) addressService.append(address);
  22. token.save();
  23. return true;
  24. });
  25. }
  26. public boolean update(Token token) {
  27. token.setId(token.getChainId()+token.getHash());
  28. return token.update();
  29. }
  30. public boolean isTokenExists(Token token) {
  31. return Db.template("hasRecord", TableUtil.getTableName(Token.class), token.getChainId(), token.getHash()).queryInt() == 1;
  32. }
  33. public Record findByChainIdAndHash(int chainId, String hash) {
  34. return Db.template("address.findByChainIdAndHash", TableUtil.getTableName(Token.class), chainId, hash).findFirst();
  35. }
  36. public Record findByChainIdAndHashSingle(int chainId, String hash) {
  37. return Db.template("address.findByChainIdAndHashSingle", TableUtil.getTableName(Token.class), chainId, hash).findFirst();
  38. }
  39. public List<Record> findByChainId(int chainId, MyPaginate p) {
  40. // 查询条件构建
  41. Kv query = Kv.of("chainId", chainId).set("tableName", "t_token");
  42. if(p != null){
  43. query.set("limit0", (p.getPageNumber() - 1) * p.getPageSize())
  44. .set("limit1", p.getPageSize());
  45. if(p.getPageSize() > ConstantUtil.THREAD_QUERY_THRESHOLD){
  46. return ThreadQueryUtil.findByChainIdThread(chainId, query);
  47. }
  48. } else {
  49. query.set("limit0", 0)
  50. .set("limit1", ConstantUtil.DEFAULT_ROWS_NUM);
  51. }
  52. return Db.template("address.findByChainId", query).find();
  53. }
  54. public Record findTokenByChainIdAndHash(int chainId, String hash) {
  55. return Db.template("token.findTokenByChainIdAndHash", chainId, hash).findFirst();
  56. }
  57. public int countByChainId(int chainId) {
  58. return addressService.countByChainId("t_token", chainId);
  59. }
  60. public int deleteByHashAndChainId(int chainId, String hash){
  61. return addressService.deleteByHashAndChainId("t_token", chainId, hash);
  62. }
  63. }