UserRepo.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. @Transactional
  29. @Modifying
  30. @Query(value = "update user set follows = (select count(*) from follow " +
  31. "where follow.user_id = ?1) where user.id = ?1", nativeQuery = true)
  32. void updateFollows(Long userId);
  33. @Transactional
  34. @Modifying
  35. @Query(value = "update user set followers = (select count(*) from follow " +
  36. "where follow.follow_user_id = ?1) where user.id = ?1", nativeQuery = true)
  37. void updateFollowers(Long userId);
  38. }