UserRepo.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.CacheEvict;
  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.util.List;
  13. import java.util.Optional;
  14. public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
  15. @Transactional
  16. @Modifying
  17. @Query("update User u set u.del = true where u.id = ?1")
  18. void softDelete(Long id);
  19. @CacheEvict(value = "user", key = "#user.username", allEntries = true)
  20. @NonNull
  21. User save(@NonNull User user);
  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. }