package com.izouma.jiashanxia.repo;

import com.izouma.jiashanxia.domain.User;
import com.izouma.jiashanxia.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.List;

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);

    List<User> findAllByParentAndDelFalse(Long parent);

    List<User> findAllByParentInAndDelFalse(Iterable<Long> parent);

    List<User> findAllByCompanyIdAndDelFalse(Long companyId);

    long countByParentAndDelFalse(Long parentId);

    List<User> findAllByCompanyIdIsNullAndDelFalse();
}
