UserRepo.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.Cacheable;
  5. import org.springframework.data.jpa.repository.JpaRepository;
  6. import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
  7. import org.springframework.data.jpa.repository.Modifying;
  8. import org.springframework.data.jpa.repository.Query;
  9. import javax.transaction.Transactional;
  10. import java.util.List;
  11. import java.util.Optional;
  12. public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
  13. @Transactional
  14. @Modifying
  15. @Query("update User u set u.del = true where u.id = ?1")
  16. void softDelete(Long id);
  17. User findByUsernameAndDelFalse(String username);
  18. List<User> findAllByAuthoritiesContainsAndDelFalse(Authority authority);
  19. Optional<User> findByOpenIdAndDelFalse(String openId);
  20. Optional<User> findByPhoneAndDelFalse(String phone);
  21. Optional<User> findByIdAndDelFalse(Long userId);
  22. @Transactional
  23. @Modifying
  24. @Query("update User u set u.followers = u.followers + ?1 where u.id = ?1")
  25. void addFollow(Long userId, int num);
  26. @Query("select distinct u from User u join Follow f on u.id = f.followUserId " +
  27. "where f.userId = ?1 and u.del = false ")
  28. List<User> userFollows(Long userId);
  29. @Query("select distinct u from User u join Follow f on u.id = f.userId " +
  30. "where f.followUserId = ?1 and u.del = false ")
  31. List<User> userFollowers(Long userId);
  32. @Transactional
  33. @Modifying
  34. @Query(value = "update user set follows = (select count(*) from follow " +
  35. "where follow.user_id = ?1) where user.id = ?1", nativeQuery = true)
  36. void updateFollows(Long userId);
  37. @Transactional
  38. @Modifying
  39. @Query(value = "update user set followers = (select count(*) from follow " +
  40. "where follow.follow_user_id = ?1) where user.id = ?1", nativeQuery = true)
  41. void updateFollowers(Long userId);
  42. @Transactional
  43. @Modifying
  44. @Query(value = "update collection_info join user on collection_info.minter_id = user.id " +
  45. "set collection_info.minter = user.nickname, " +
  46. " collection_info.minter_avatar = user.avatar " +
  47. "where user.id = ?1", nativeQuery = true)
  48. void updateMinterForCollection(Long userId);
  49. @Transactional
  50. @Modifying
  51. @Query(value = "update collection_info join user on collection_info.owner_id = user.id " +
  52. "set collection_info.owner = user.nickname, " +
  53. " collection_info.owner_avatar = user.avatar " +
  54. "where user.id = ?1", nativeQuery = true)
  55. void updateOwnerForCollection(Long userId);
  56. @Transactional
  57. @Modifying
  58. @Query(value = "update order_info join user on order_info.minter_id = user.id " +
  59. "set order_info.minter = user.nickname, " +
  60. " order_info.minter_avatar = user.avatar " +
  61. "where user.id = ?1", nativeQuery = true)
  62. void updateMinterForOrder(Long userId);
  63. @Transactional
  64. @Modifying
  65. @Query(value = "update asset join user on asset.minter_id = user.id " +
  66. "set asset.minter = user.nickname, " +
  67. " asset.minter_avatar = user.avatar " +
  68. "where user.id = ?1", nativeQuery = true)
  69. void updateMinterForAsset(Long userId);
  70. }