package com.ywt.mg.domain.entities;

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

import java.util.List;

public interface HospitalRepository extends JpaRepository<Hospital, Integer> {

    /**
     * 判断修改后的医院是否会重复已有医院
     *
     * @param hospitalName
     * @param hospitalId
     * @return
     */
    @Query(value = "SELECT COUNT(1) FROM  hospital WHERE  name = ? AND  id != ? and is_deleted = 0 ", nativeQuery = true)
    public int checkHospitalNameById(String hospitalName, int hospitalId);


    /**
     * 添加医院是否会重复已有医院
     *
     * @param hospitalName
     * @return
     */
    @Query(value = "SELECT COUNT(1) FROM  hospital WHERE  name = ? and is_deleted = 0 ", nativeQuery = true)
    public int checkHospitalNameById(String hospitalName);

    /**
     * 检查这个地区ID是否被引用
     *
     * @param areaIds
     * @return
     */
    @Query(value = "SELECT count(1) FROM hospital where is_deleted = 0 and area_ids like ? and `status` = 0", nativeQuery = true)
    public int checkUseAreaIds(String areaIds);

    /**
     * 得到所有未删除的医院
     *
     * @return
     */
    @Query(value = "SELECT * FROM hospital where is_deleted = 0 ", nativeQuery = true)
    public List<Hospital> findAllHospital();

    /**
     * 根据医院名称得到对象
     *
     * @param hospitalName
     * @return
     */
    @Query(value = "select * from hospital where name = ? and is_deleted = 0 limit 1;", nativeQuery = true)
    public Hospital findHospitalByName(String hospitalName);
}
