package com.ywt.outpatient.domain.entity.center;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.Optional;

/**
 * Created by huangguoping.
 */
public interface UserRepository extends PagingAndSortingRepository<User, Integer> {

    @Query(value = "Select * From user Where mobile = ? and status = 0 and is_deleted = 0 limit 1;", nativeQuery = true)
    User findByMobile(String mobile);

//    @Query(value = "Select * From user Where id = ? and is_deleted = 0 limit 1;", nativeQuery = true)
//    User findById(Integer id);

    @Query(value = "select * from user where mobile = ? and user_type = 2 and status = 0 and is_deleted = 0 limit 1;", nativeQuery = true)
    User getDoctorByMobile(String mobile);

    @Query(value = "select * from user where mobile = ? and status = 0 and is_deleted = 0 limit 1;", nativeQuery = true)
    User getUserByMobile(String mobile);

    /**
     * 通过用户名和密码得到用户对象
     *
     * @param username
     * @param password
     * @return
     */
    @Query(value = "select * from user where mobile = ? and password = ? and user_type =2 and status = 0 and is_deleted = 0 limit 1;", nativeQuery = true)
    User getUserByUsernameAndPsd(String username, String password);

    /**
     * 根据手机号得到用户信息
     *
     * @param mobile
     * @return
     */
    @Query(value = "select * from user where mobile = ?  and is_deleted = 0  limit 1;", nativeQuery = true)
    User findUserByMobile(String mobile);

    @Query(value = "select * from user where id = ?  and is_deleted = 0 and status = 0  limit 1;", nativeQuery = true)
    User getNormalUserById(int id);

    @Query(value = "select * from user where union_id = ?  and is_deleted = 0 and status = 0  limit 1;", nativeQuery = true)
    User getByUnionId(String unionId);

    @Query(value = "select * from user where union_id = ?  and is_deleted = 0 limit 1;", nativeQuery = true)
    User getByUnionIdAndIsDeletedFalse(String unionId);

    User findFirstByUnionIdAndIsDeletedFalse(String unionId);

    /**
     * 返回 User 对象
     * @return
     */
    User getById(int id);

}
