UserRepo.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. }