package com.izouma.wenlvju.repo;

import com.izouma.wenlvju.domain.User;
import com.izouma.wenlvju.security.Authority;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
    @Transactional
    @Modifying
    @Query("update User u set u.del = true where u.id = ?1")
    void softDelete(Long id);

    User findByUsernameAndDelFalse(String username);

    List<User> findAllByAuthoritiesContainsAndDelFalse(Authority authority);

    User findByOpenIdAndDelFalse(String openId);

    User findByPhoneAndDelFalse(String phone);

    User findByPhoneAndDelFalseAndIdIsNot(String phone, Long id);

    List<User> findAllByDistrictAndAuthoritiesContainsAndDelFalse(String district, Authority authorities);

    Optional<User> findByIdAndDelFalse(Long id);

    List<User> findAllByIdInAndDelFalse(Collection<Long> id);
}
