UserRepo.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.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 javax.transaction.Transactional;
  11. import java.time.LocalDateTime;
  12. import java.util.Collection;
  13. import java.util.List;
  14. import java.util.Optional;
  15. public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
  16. @Transactional
  17. @Modifying
  18. @Query("update User u set u.del = true where u.id = ?1")
  19. void softDelete(Long id);
  20. Optional<User> findByUsernameAndDelFalse(String username);
  21. List<User> findAllByAuthoritiesContainsAndDelFalse(Authority authority);
  22. Optional<User> findByOpenIdAndDelFalse(String openId);
  23. Optional<User> findByPhoneAndDelFalse(String phone);
  24. @Cacheable(value = "user", key = "#userId")
  25. Optional<User> findByIdAndDelFalse(Long userId);
  26. @Transactional
  27. @Modifying
  28. @Query("update User u set u.followers = u.followers + ?1 where u.id = ?1")
  29. void addFollow(Long userId, int num);
  30. @Query("select distinct u from User u join Follow f on u.id = f.followUserId " +
  31. "where f.userId = ?1 and u.del = false ")
  32. List<User> userFollows(Long userId);
  33. @Query("select distinct u from User u join Follow f on u.id = f.userId " +
  34. "where f.followUserId = ?1 and u.del = false ")
  35. List<User> userFollowers(Long userId);
  36. @Transactional
  37. @Modifying
  38. @Query(value = "update user set follows = (select count(*) from follow " +
  39. "where follow.user_id = ?1) where user.id = ?1", nativeQuery = true)
  40. void updateFollows(Long userId);
  41. @Transactional
  42. @Modifying
  43. @Query(value = "update user set followers = (select count(*) from follow " +
  44. "where follow.follow_user_id = ?1) where user.id = ?1", nativeQuery = true)
  45. void updateFollowers(Long userId);
  46. @Transactional
  47. @Modifying
  48. @Query(value = "update collection_info join user on collection_info.minter_id = user.id " +
  49. "set collection_info.minter = user.nickname, " +
  50. " collection_info.minter_avatar = user.avatar " +
  51. "where user.id = ?1", nativeQuery = true)
  52. void updateMinterForCollection(Long userId);
  53. @Transactional
  54. @Modifying
  55. @Query(value = "update collection_info join user on collection_info.owner_id = user.id " +
  56. "set collection_info.owner = user.nickname, " +
  57. " collection_info.owner_avatar = user.avatar " +
  58. "where user.id = ?1", nativeQuery = true)
  59. void updateOwnerForCollection(Long userId);
  60. @Transactional
  61. @Modifying
  62. @Query(value = "update order_info join user on order_info.minter_id = user.id " +
  63. "set order_info.minter = user.nickname, " +
  64. " order_info.minter_avatar = user.avatar " +
  65. "where user.id = ?1", nativeQuery = true)
  66. void updateMinterForOrder(Long userId);
  67. @Transactional
  68. @Modifying
  69. @Query(value = "update asset join user on asset.minter_id = user.id " +
  70. "set asset.minter = user.nickname, " +
  71. " asset.minter_avatar = user.avatar " +
  72. "where user.id = ?1", nativeQuery = true)
  73. void updateMinterForAsset(Long userId);
  74. List<User> findByPhoneInAndDelFalse(Iterable<String> phone);
  75. List<User> findByIdInAndDelFalse(Iterable<Long> userId);
  76. @Query(value = "update asset join user on asset.minter_id = user.id " +
  77. "set asset.minter = user.nickname, " +
  78. " asset.minter_avatar = user.avatar " +
  79. "where user.id = ?1 ", nativeQuery = true)
  80. @Modifying
  81. @Transactional
  82. void updateAssetMinter(Long userId);
  83. @Query(value = "update asset join user on asset.owner_id = user.id " +
  84. "set asset.owner = user.nickname, " +
  85. " asset.owner_avatar = user.avatar " +
  86. "where user.id = ?1 ", nativeQuery = true)
  87. @Modifying
  88. @Transactional
  89. void updateAssetOwner(Long userId);
  90. @Query(value = "update collection_info join user on collection_info.minter_id = user.id " +
  91. "set collection_info.minter = user.nickname, " +
  92. " collection_info.minter_avatar = user.avatar " +
  93. "where user.id = ?1 ", nativeQuery = true)
  94. @Modifying
  95. @Transactional
  96. void updateCollectionMinter(Long userId);
  97. @Query(value = "update collection_info join user on collection_info.owner_id = user.id " +
  98. "set collection_info.owner = user.nickname, " +
  99. " collection_info.owner_avatar = user.avatar " +
  100. "where user.id = ?1 ", nativeQuery = true)
  101. @Modifying
  102. @Transactional
  103. void updateCollectionOwner(Long userId);
  104. @Query(value = "update order_info join user on order_info.minter_id = user.id " +
  105. "set order_info.minter = user.nickname, " +
  106. " order_info.minter_avatar = user.avatar " +
  107. "where user.id = ?1 ", nativeQuery = true)
  108. @Modifying
  109. @Transactional
  110. void updateOrderMinter(Long userId);
  111. @Query(value = "update token_history join user on token_history.from_user_id = user.id " +
  112. "set token_history.from_user = user.nickname, " +
  113. " token_history.from_avatar = user.avatar " +
  114. "where user.id = ?1 ", nativeQuery = true)
  115. @Modifying
  116. @Transactional
  117. void updateHistoryFromUser(Long userId);
  118. @Query(value = "update token_history join user on token_history.to_user_id = user.id " +
  119. "set token_history.to_user = user.nickname, " +
  120. " token_history.to_avatar = user.avatar " +
  121. "where user.id = ?1 ", nativeQuery = true)
  122. @Modifying
  123. @Transactional
  124. void updateHistoryToUser(Long userId);
  125. @Transactional
  126. @Modifying
  127. @Query("update User u set u.sales = ?2 where u.id = ?1")
  128. void setSales(Long userId, int sales);
  129. List<User> findByAuthoritiesContains(Authority authority);
  130. @Transactional
  131. @Modifying
  132. @Query("update User u set u.sales = COALESCE(u.sales, 0) + ?2 where u.id = ?1")
  133. public void increaseSales(Long id, int num);
  134. @Query("select phone from User where id = ?1 and del = false")
  135. String findPhoneById(Long id);
  136. List<User> findAllByCollectionIdAndCollectionInvitor(Long collectionId, Long collectionInvitor);
  137. int countAllByCollectionIdAndCollectionInvitor(Long collectionId, Long collectionInvitor);
  138. long countAllByAuthoritiesContainsAndDelFalse(Authority authority);
  139. List<User> findAllByCreatedAtIsAfterAndAuthoritiesContains(LocalDateTime createdAt, Authority authorities);
  140. List<User> findBySettleAccountIdIsNotNull();
  141. @Transactional
  142. @Modifying
  143. @Query("update User set vipPoint = vipPoint + ?2 where id = ?1")
  144. void addVipPoint(Long id, int num);
  145. @Transactional
  146. @Modifying
  147. @Query("update User set vipPoint = ?2 where id = ?1")
  148. void updateVipPoint(Long id, int num);
  149. @Query("update User u set u.authStatus = ?2, u.authId = ?3 where u.id = ?1")
  150. @Transactional
  151. @Modifying
  152. void setAuthStatus(Long id, AuthStatus status, Long authId);
  153. @Query(nativeQuery = true, value = "select collection_invitor" +
  154. " from user" +
  155. " where collection_id = ?1" +
  156. " group by collection_invitor" +
  157. " having count(id) >= ?2")
  158. List<Long> findInvitorByCollectionId(Long collectionId, int size);
  159. @Transactional
  160. @Modifying
  161. @Query("update User set vipPoint = 1 where vipPoint = 0 and id in ?1")
  162. void updateAllByInvitor(Collection<Long> ids);
  163. List<User> findAllByCollectionId(Long collectionId);
  164. @Query(value = "update showroom join user on showroom.user_id = user.id " +
  165. "set showroom.nickname = user.nickname " +
  166. "where user.id = ?1 ", nativeQuery = true)
  167. @Modifying
  168. @Transactional
  169. void updateShowroomToUser(Long userId);
  170. }