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

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

import java.util.List;

public interface AddressRepository extends JpaRepository<Address, Integer> {
    @Query(value = "select * from address where user_id = ? and deleted = 0 and type = 0 order by create_time desc  ;", nativeQuery = true)
    List<Address> getAddressList(int userId);

    /**
     * 得到医生所有地址
     *
     * @param userId
     * @return
     */
    @Query(value = "select * from address where user_id = ? and deleted = 0 and type =1 order by create_time desc ;", nativeQuery = true)
    List<Address> getDoctorAddressList(int userId);

    @Query(value = "select count(1) from address where user_id = ? and deleted = 0 and type = 1 and province_id = ? and city_id = ? and area_id = ? and address = ? ", nativeQuery = true)
    int checkAddress(int doctorId, int provinceId, int cityId, int areaId, String address);

    /**
     * 判断医生是否有设置默认地址
     *
     * @param doctorId
     * @return
     */
    @Query(value = "select count(1) from address where user_id = ? and deleted = 0 and type = 1 and status = 1  ", nativeQuery = true)
    int checkDoctorDefaultAddress(int doctorId);

    /**
     * 得到医生的默认地址
     *
     * @param doctorId
     * @return
     */
    @Query(value = "select * from address where user_id = ? and deleted = 0 and type = 1 and status = 1 limit 1 ", nativeQuery = true)
    public Address getDoctorDefaultAddress(int doctorId);

    /**
     * 得到医生的最近添加的地址
     *
     * @param doctorId
     * @return
     */
    @Query(value = "select * from address where user_id = ? and deleted = 0 and type = 1 order by create_time desc limit 1 ", nativeQuery = true)
    public Address getDoctorLatestAddress(int doctorId);

    Address findFirstByProvinceAndCityAndAreaAndAddressAndContactAndAppellationAndMobileAndUserIdAndDeletedFalse(String province,
                                                                                                                 String city,
                                                                                                                 String area,
                                                                                                                 String address,
                                                                                                                 String contact,
                                                                                                                 String appellation,
                                                                                                                 String mobile,
                                                                                                                 Integer userId);

}
