UserRepo.java 3.3 KB

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