package com.izouma.dingdong.repo;

import com.izouma.dingdong.domain.User;
import com.izouma.dingdong.enums.Identity;
import com.izouma.dingdong.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> {
    User findByUsername(String username);

    List<User> findAllByAuthoritiesNotContains(Authority authority);

    User findByOpenId(String openId);

    User findByPhone(String phone);

    User findByPhoneAndIdentity(String phone, Identity identity);

    @Query("update User t set t.enabled = false where t.id = ?1")
    @Modifying
    @Transactional
    void deleteById(Long id);

    List<User> findAllByIdentityAndAuthoritiesContains(Identity identity, Authority authority);

    List<User> findAllByIdentity(Identity identity);

    /**
     * 按身份查找，且不在黑名单
     *
     * @param identity 身份
     * @return 用户列表
     */
    List<User> findAllByIdentityAndBlacklistFalse(Identity identity);


}
