TokenHistoryRepo.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package com.izouma.nineth.repo;
  2. import com.izouma.nineth.TokenHistory;
  3. import org.springframework.data.domain.Page;
  4. import org.springframework.data.domain.Pageable;
  5. import org.springframework.data.jpa.repository.JpaRepository;
  6. import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
  7. import org.springframework.data.jpa.repository.Modifying;
  8. import org.springframework.data.jpa.repository.Query;
  9. import javax.transaction.Transactional;
  10. import java.math.BigDecimal;
  11. import java.time.LocalDateTime;
  12. import java.util.Collection;
  13. import java.util.List;
  14. import java.util.Map;
  15. public interface TokenHistoryRepo extends JpaRepository<TokenHistory, Long>, JpaSpecificationExecutor<TokenHistory> {
  16. List<TokenHistory> findByTokenIdOrderByCreatedAtDesc(String tokenId);
  17. @Query("select t from TokenHistory t where t.toUserId = ?1 or t.fromUserId = ?1 order by t.createdAt desc")
  18. Page<TokenHistory> userHistory(Long userId, Pageable pageable);
  19. @Query("select t from TokenHistory t where t.toUserId = ?2 or t.fromUserId = ?1 order by t.createdAt desc")
  20. Page<TokenHistory> userHistoryTo(Long userId, Long toUserId, Pageable pageable);
  21. @Query("select t from TokenHistory t where t.toUserId = ?1 or t.fromUserId = ?2 order by t.createdAt desc")
  22. Page<TokenHistory> userHistoryFrom(Long userId, Long fromUserId, Pageable pageable);
  23. @Query("select t from TokenHistory t where t.toUserId = ?1 or t.fromUserId = ?1 order by t.createdAt desc")
  24. List<TokenHistory> userHistory(Long userId);
  25. @Query("select t from TokenHistory t where t.toUserId in ?1 and t.price is not null")
  26. List<TokenHistory> userBuy(Collection<Long> userId);
  27. @Query("select sum(t.price) from TokenHistory t where t.toUserId = ?1 and t.price is not null")
  28. BigDecimal userBuy(Long userId);
  29. @Transactional
  30. @Modifying
  31. int deleteByTokenId(String tokenId);
  32. @Query("select t from TokenHistory t where t.fromUserId = t.toUserId")
  33. List<TokenHistory> findError();
  34. List<TokenHistory> findByOperationAndPriceNull(String oper);
  35. @Query(nativeQuery = true, value = "select to_user_id , to_user, to_avatar, sum(price) total from token_history " +
  36. "where created_at between ?1 and ?2 group by to_user_id order by sum(price) desc limit ?3")
  37. List<Map<String, Object>> sumPrice(LocalDateTime start, LocalDateTime end, int size);
  38. }