UserRepo.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.izouma.nineth.repo;
  2. import com.izouma.nineth.domain.User;
  3. import com.izouma.nineth.security.Authority;
  4. import org.springframework.cache.annotation.CachePut;
  5. import org.springframework.cache.annotation.Cacheable;
  6. import org.springframework.data.jpa.repository.JpaRepository;
  7. import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
  8. import org.springframework.data.jpa.repository.Modifying;
  9. import org.springframework.data.jpa.repository.Query;
  10. import org.springframework.lang.NonNull;
  11. import javax.transaction.Transactional;
  12. import java.time.LocalDateTime;
  13. import java.util.List;
  14. import java.util.Optional;
  15. import java.util.Set;
  16. public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
  17. @Transactional
  18. @Modifying
  19. @Query("update User u set u.del = true where u.id = ?1")
  20. void softDelete(Long id);
  21. @NonNull
  22. @CachePut(value = "userInfo", key = "#user.id")
  23. User save(@NonNull User user);
  24. @Cacheable("user")
  25. User findByUsernameAndDelFalse(String username);
  26. List<User> findAllByAuthoritiesContainsAndDelFalse(Authority authority);
  27. Optional<User> findByOpenIdAndDelFalse(String openId);
  28. Optional<User> findByPhoneAndDelFalse(String phone);
  29. Optional<User> findByIdAndDelFalse(Long userId);
  30. @Transactional
  31. @Modifying
  32. @Query("update User u set u.followers = u.followers + ?1 where u.id = ?1")
  33. void addFollow(Long userId, int num);
  34. @Query("select distinct u from User u join Follow f on u.id = f.followUserId " +
  35. "where f.userId = ?1 and u.del = false ")
  36. List<User> userFollows(Long userId);
  37. @Query("select distinct u from User u join Follow f on u.id = f.userId " +
  38. "where f.followUserId = ?1 and u.del = false ")
  39. List<User> userFollowers(Long userId);
  40. @Transactional
  41. @Modifying
  42. @Query(value = "update user set follows = (select count(*) from follow " +
  43. "where follow.user_id = ?1) where user.id = ?1", nativeQuery = true)
  44. void updateFollows(Long userId);
  45. @Transactional
  46. @Modifying
  47. @Query(value = "update user set followers = (select count(*) from follow " +
  48. "where follow.follow_user_id = ?1) where user.id = ?1", nativeQuery = true)
  49. void updateFollowers(Long userId);
  50. @Transactional
  51. @Modifying
  52. @Query(value = "update collection_info join user on collection_info.minter_id = user.id " +
  53. "set collection_info.minter = user.nickname, " +
  54. " collection_info.minter_avatar = user.avatar " +
  55. "where user.id = ?1", nativeQuery = true)
  56. void updateMinterForCollection(Long userId);
  57. @Transactional
  58. @Modifying
  59. @Query(value = "update collection_info join user on collection_info.owner_id = user.id " +
  60. "set collection_info.owner = user.nickname, " +
  61. " collection_info.owner_avatar = user.avatar " +
  62. "where user.id = ?1", nativeQuery = true)
  63. void updateOwnerForCollection(Long userId);
  64. @Transactional
  65. @Modifying
  66. @Query(value = "update order_info join user on order_info.minter_id = user.id " +
  67. "set order_info.minter = user.nickname, " +
  68. " order_info.minter_avatar = user.avatar " +
  69. "where user.id = ?1", nativeQuery = true)
  70. void updateMinterForOrder(Long userId);
  71. @Transactional
  72. @Modifying
  73. @Query(value = "update asset join user on asset.minter_id = user.id " +
  74. "set asset.minter = user.nickname, " +
  75. " asset.minter_avatar = user.avatar " +
  76. "where user.id = ?1", nativeQuery = true)
  77. void updateMinterForAsset(Long userId);
  78. List<User> findByPhoneInAndDelFalse(Iterable<String> phone);
  79. List<User> findByIdInAndDelFalse(Iterable<Long> userId);
  80. @Query(value = "update asset join user on asset.minter_id = user.id " +
  81. "set asset.minter = user.nickname, " +
  82. " asset.minter_avatar = user.avatar " +
  83. "where user.id = ?1 ", nativeQuery = true)
  84. @Modifying
  85. @Transactional
  86. void updateAssetMinter(Long userId);
  87. @Query(value = "update asset join user on asset.owner_id = user.id " +
  88. "set asset.owner = user.nickname, " +
  89. " asset.owner_avatar = user.avatar " +
  90. "where user.id = ?1 ", nativeQuery = true)
  91. @Modifying
  92. @Transactional
  93. void updateAssetOwner(Long userId);
  94. @Query(value = "update collection_info join user on collection_info.minter_id = user.id " +
  95. "set collection_info.minter = user.nickname, " +
  96. " collection_info.minter_avatar = user.avatar " +
  97. "where user.id = ?1 ", nativeQuery = true)
  98. @Modifying
  99. @Transactional
  100. void updateCollectionMinter(Long userId);
  101. @Query(value = "update collection_info join user on collection_info.owner_id = user.id " +
  102. "set collection_info.owner = user.nickname, " +
  103. " collection_info.owner_avatar = user.avatar " +
  104. "where user.id = ?1 ", nativeQuery = true)
  105. @Modifying
  106. @Transactional
  107. void updateCollectionOwner(Long userId);
  108. @Query(value = "update order_info join user on order_info.minter_id = user.id " +
  109. "set order_info.minter = user.nickname, " +
  110. " order_info.minter_avatar = user.avatar " +
  111. "where user.id = ?1 ", nativeQuery = true)
  112. @Modifying
  113. @Transactional
  114. void updateOrderMinter(Long userId);
  115. @Query(value = "update token_history join user on token_history.from_user_id = user.id " +
  116. "set token_history.from_user = user.nickname, " +
  117. " token_history.from_avatar = user.avatar " +
  118. "where user.id = ?1 ", nativeQuery = true)
  119. @Modifying
  120. @Transactional
  121. void updateHistoryFromUser(Long userId);
  122. @Query(value = "update token_history join user on token_history.to_user_id = user.id " +
  123. "set token_history.to_user = user.nickname, " +
  124. " token_history.to_avatar = user.avatar " +
  125. "where user.id = ?1 ", nativeQuery = true)
  126. @Modifying
  127. @Transactional
  128. void updateHistoryToUser(Long userId);
  129. @Transactional
  130. @Modifying
  131. @Query("update User u set u.sales = ?2 where u.id = ?1")
  132. void setSales(Long userId, int sales);
  133. List<User> findByAuthoritiesContains(Authority authority);
  134. @Transactional
  135. @Modifying
  136. @Query("update User u set u.sales = COALESCE(u.sales, 0) + ?2 where u.id = ?1")
  137. public void increaseSales(Long id, int num);
  138. @Query("select phone from User where id = ?1 and del = false")
  139. String findPhoneById(Long id);
  140. List<User> findAllByCollectionIdAndCollectionInvitor(Long collectionId, Long collectionInvitor);
  141. int countAllByCollectionIdAndCollectionInvitor(Long collectionId, Long collectionInvitor);
  142. long countAllByAuthoritiesContainsAndDelFalse(Authority authority);
  143. List<User> findAllByCreatedAtBetweenAndAuthoritiesContains(LocalDateTime start, LocalDateTime end, Authority authorities);
  144. List<User> findAllByCreatedAtIsAfterAndAuthoritiesContains(LocalDateTime createdAt, Authority authorities);
  145. List<User> findBySettleAccountIdIsNotNull();
  146. @Transactional
  147. @Modifying
  148. @Query("update User set vipPoint = vipPoint + ?2 where id = ?1")
  149. void updateVipPoint(Long id, int num);
  150. }