UserRepo.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package com.izouma.nineth.repo;
  2. import com.izouma.nineth.domain.User;
  3. import com.izouma.nineth.enums.AuthStatus;
  4. import com.izouma.nineth.security.Authority;
  5. import org.springframework.cache.annotation.CacheEvict;
  6. import org.springframework.cache.annotation.Cacheable;
  7. import org.springframework.data.jpa.repository.JpaRepository;
  8. import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
  9. import org.springframework.data.jpa.repository.Modifying;
  10. import org.springframework.data.jpa.repository.Query;
  11. import javax.transaction.Transactional;
  12. import java.time.LocalDateTime;
  13. import java.util.Collection;
  14. import java.util.List;
  15. import java.util.Optional;
  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. Optional<User> findByUsernameAndDelFalse(String username);
  22. List<User> findAllByPhoneIn(Collection<String> phones);
  23. List<User> findAllByAuthoritiesContainsAndDelFalse(Authority authority);
  24. Optional<User> findByOpenIdAndDelFalse(String openId);
  25. Optional<User> findByPhoneAndDelFalse(String phone);
  26. @Cacheable(value = "user", key = "#userId")
  27. Optional<User> findByIdAndDelFalse(Long userId);
  28. @Transactional
  29. @Modifying
  30. @Query("update User u set u.followers = u.followers + ?1 where u.id = ?1")
  31. void addFollow(Long userId, int num);
  32. @Query("select distinct u from User u join Follow f on u.id = f.followUserId " +
  33. "where f.userId = ?1 and u.del = false ")
  34. List<User> userFollows(Long userId);
  35. @Query("select distinct u from User u join Follow f on u.id = f.userId " +
  36. "where f.followUserId = ?1 and u.del = false ")
  37. List<User> userFollowers(Long userId);
  38. @Transactional
  39. @Modifying
  40. @Query(value = "update user set follows = (select count(*) from follow " +
  41. "where follow.user_id = ?1) where user.id = ?1", nativeQuery = true)
  42. @CacheEvict(value = {"myUserInfo","user"} ,allEntries = true)
  43. void updateFollows(Long userId);
  44. @Transactional
  45. @Modifying
  46. @Query(value = "update user set followers = (select count(*) from follow " +
  47. "where follow.follow_user_id = ?1) where user.id = ?1", nativeQuery = true)
  48. @CacheEvict(value = {"myUserInfo","user"} ,allEntries = 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> findAllByCreatedAtIsAfterAndAuthoritiesContains(LocalDateTime createdAt, Authority authorities);
  144. List<User> findBySettleAccountIdIsNotNull();
  145. @Transactional
  146. @Modifying
  147. @Query("update User set vipPoint = vipPoint + ?2 where id = ?1")
  148. void addVipPoint(Long id, int num);
  149. @Transactional
  150. @Modifying
  151. @Query("update User set vipPoint = ?2 where id = ?1")
  152. void updateVipPoint(Long id, int num);
  153. @Query("update User u set u.authStatus = ?2, u.authId = ?3 where u.id = ?1")
  154. @Transactional
  155. @Modifying
  156. void setAuthStatus(Long id, AuthStatus status, Long authId);
  157. @Query(nativeQuery = true, value = "select collection_invitor" +
  158. " from user" +
  159. " where collection_id = ?1" +
  160. " group by collection_invitor" +
  161. " having count(id) >= ?2")
  162. List<Long> findInvitorByCollectionId(Long collectionId, int size);
  163. @Transactional
  164. @Modifying
  165. @Query("update User set vipPoint = 1 where vipPoint = 0 and id in ?1")
  166. void updateAllByInvitor(Collection<Long> ids);
  167. List<User> findAllByCollectionId(Long collectionId);
  168. @Query(value = "update showroom join user on showroom.user_id = user.id " +
  169. "set showroom.nickname = user.nickname " +
  170. "where user.id = ?1 ", nativeQuery = true)
  171. @Modifying
  172. @Transactional
  173. void updateShowroomToUser(Long userId);
  174. }