UserRepo.java 8.1 KB

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