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 DepartmentRepository extends JpaRepository<Department, Integer> {
    @Query(value = "Select * From department Where hospital_id = ? and is_deleted = 0 order by weight desc;", nativeQuery = true)
    List<Department> findByHospitalId(Integer hospitalId);

    List<Department> findAllByHospitalId(Integer hospitalId);

    @Query(value = "select * from department where yida_code = ? and hospital_id = 1 and is_deleted = 0;", nativeQuery = true)
    Department getDepartmentByYiDaCode(String yiDaCode);

    @Query(value = "select * from department where id = ? and is_deleted = 0;", nativeQuery = true)
    Department getById(int id);

    @Query(value = "Select * From department Where hospital_id = ? and sub_hospital_id = ? and is_deleted = 0 order by weight desc;", nativeQuery = true)
    List<Department> findByHospitalIdAndSubHospitalId(Integer hospitalId, Integer subHospitalId);

    @Query(value = "Select * From department Where hospital_id = ?  order by weight desc;", nativeQuery = true)
    List<Department> findByAllHospitalId(Integer hospitalId);

    @Query(value = "Select * From department Where (hospital_id = ? or sub_hospital_id = ?) and name like ? and is_deleted = 0 order by weight desc;", nativeQuery = true)
    List<Department> findByHospitalIdAndDeptName(int hospitalId, int subHospitalId, String deptName);

    @Query(value = "Select * From department Where (hospital_id = ? or sub_hospital_id = ?) and is_deleted = 0 order by weight desc;", nativeQuery = true)
    List<Department> findByHospitalId(Integer hospitalId, Integer subHospitalId);

    @Override
    @Query(value = "Select * From department Where is_deleted = 0 order by weight desc;", nativeQuery = true)
    List<Department> findAll();

    @Query(value = "SELECT * FROM department WHERE code = ? and hospital_id = ? limit 1;",nativeQuery = true)
    Department getDepartment(String code, int hospitalId);
}
