TokenHistoryRepo.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. import java.util.Optional;
  16. public interface TokenHistoryRepo extends JpaRepository<TokenHistory, Long>, JpaSpecificationExecutor<TokenHistory> {
  17. List<TokenHistory> findByTokenIdOrderByCreatedAtDesc(String tokenId);
  18. @Query("select t from TokenHistory t where t.toUserId = ?1 or t.fromUserId = ?1 order by t.createdAt desc")
  19. Page<TokenHistory> userHistory(Long userId, Pageable pageable);
  20. @Query("select t from TokenHistory t where t.toUserId = ?2 or t.fromUserId = ?1 order by t.createdAt desc")
  21. Page<TokenHistory> userHistoryTo(Long userId, Long toUserId, Pageable pageable);
  22. @Query("select t from TokenHistory t where t.toUserId = ?1 or t.fromUserId = ?2 order by t.createdAt desc")
  23. Page<TokenHistory> userHistoryFrom(Long userId, Long fromUserId, Pageable pageable);
  24. @Query("select t from TokenHistory t where t.toUserId = ?1 or t.fromUserId = ?1 order by t.createdAt desc")
  25. List<TokenHistory> userHistory(Long userId);
  26. @Query("select t from TokenHistory t where t.toUserId in ?1 and t.price is not null")
  27. List<TokenHistory> userBuy(Collection<Long> userId);
  28. @Query("select sum(t.price) from TokenHistory t where t.toUserId = ?1 and t.price is not null")
  29. BigDecimal userBuy(Long userId);
  30. @Query("select sum(t.price) from TokenHistory t where t.fromUserId = ?1 and t.price is not null")
  31. BigDecimal userSale(Long userId);
  32. @Transactional
  33. @Modifying
  34. int deleteByTokenId(String tokenId);
  35. @Query("select t from TokenHistory t where t.fromUserId = t.toUserId")
  36. List<TokenHistory> findError();
  37. List<TokenHistory> findByOperationAndPriceNull(String oper);
  38. @Query(nativeQuery = true, value = "select to_user_id , to_user, to_avatar, sum(price) total from token_history " +
  39. "where created_at between ?1 and ?2 group by to_user_id order by sum(price) desc limit ?3")
  40. List<Map<String, Object>> sumPrice(LocalDateTime start, LocalDateTime end, int size);
  41. Optional<TokenHistory> findByToUserIdAndTokenId(Long toUserId, String tokenId);
  42. List<TokenHistory> findAllByPriceIsNotNull();
  43. }