package org.example.mapper;

import org.apache.ibatis.annotations.*;
import org.example.entity.Patient;

import java.util.List;

@Mapper
public interface PatientMapper {
    
    /**
     * 查询所有患者
     * @return 患者列表
     */
    @Select("SELECT admission_date_time AS admissionDateTime, inpatient_no AS inpatientNo, name, sex, age, " +
            "medical_insurance_type AS medicalInsuranceType, dept_code AS deptCode, ward_code AS wardCode, room_no AS roomNo, bed_no AS bedNo, " +
            "doctor_code AS doctorCode, nurse_code AS nurseCode, inpatient_serial_no AS inpatientSerialNo, " +
            "dept_date_time AS deptDateTime, discharged_date_time AS dischargedDateTime, " +
            "inpatient_status AS inpatientStatus, diagnose, condition_status AS conditionStatus, " +
            "nurse_level AS nurseLevel, allergen, diet_type AS dietType, notice " +
            "FROM tb_hospital_patient")
    List<Patient> findAll();
    
    /**
     * 根据条件查询患者
     * @return 患者列表
     */
    @Select({
        "<script>",
        "SELECT admission_date_time AS admissionDateTime, inpatient_no AS inpatientNo, name, sex, age, " +
        "medical_insurance_type AS medicalInsuranceType, dept_code AS deptCode, ward_code AS wardCode, room_no AS roomNo, bed_no AS bedNo, " +
        "doctor_code AS doctorCode, nurse_code AS nurseCode, inpatient_serial_no AS inpatientSerialNo, " +
        "dept_date_time AS deptDateTime, discharged_date_time AS dischargedDateTime, " +
        "inpatient_status AS inpatientStatus, diagnose, condition_status AS conditionStatus, " +
        "nurse_level AS nurseLevel, allergen, diet_type AS dietType, notice " +
        "FROM tb_hospital_patient",
        "WHERE 1=1",
        "<if test='name != null and name != \"\"'> AND name LIKE CONCAT('%', #{name}, '%')</if>",
        "<if test='department != null and department != \"\"'> AND dept_code = #{department}</if>",
        "<if test='ward != null and ward != \"\"'> AND ward_code = #{ward}</if>",
        "<if test='roomNo != null and roomNo != \"\"'> AND room_no = #{roomNo}</if>",
        "<if test='bedNo != null and bedNo != \"\"'> AND bed_no = #{bedNo}</if>",
        "<if test='inpatientNo != null and inpatientNo != \"\"'> AND inpatient_no LIKE CONCAT('%', #{inpatientNo}, '%')</if>",
        "<if test='nurseLevel != null and nurseLevel != \"\"'> AND nurse_level = #{nurseLevel}</if>",
        "<if test='status != null and status != \"\"'> AND inpatient_status = #{status}</if>",
        "<if test='admissionDateTime != null and admissionDateTime != \"\"'> AND admission_date_time LIKE CONCAT('%', #{admissionDateTime}, '%')</if>",
        "</script>"
    })
    List<Patient> findWithConditions(@Param("name") String name, @Param("department") String department, 
                                  @Param("ward") String ward, @Param("roomNo") String roomNo, 
                                  @Param("bedNo") String bedNo, @Param("inpatientNo") String inpatientNo,
                                  @Param("nurseLevel") String nurseLevel, @Param("status") String status,
                                  @Param("admissionDateTime") String admissionDateTime);
    
    /**
     * 查询所有不同的护理级别
     * @return 护理级别列表
     */
    @Select("SELECT DISTINCT nurse_level FROM tb_hospital_patient WHERE nurse_level IS NOT NULL AND nurse_level != ''")
    List<String> findDistinctNurseLevels();
    
    /**
     * 查询所有不同的在院状态
     * @return 在院状态列表
     */
    @Select("SELECT DISTINCT inpatient_status FROM tb_hospital_patient WHERE inpatient_status IS NOT NULL AND inpatient_status != ''")
    List<String> findDistinctInpatientStatuses();
    
    /**
     * 根据住院号查询患者
     * @param inpatientNo 住院号
     * @return 患者信息
     */
    @Select("SELECT admission_date_time AS admissionDateTime, inpatient_no AS inpatientNo, name, sex, age, " +
            "medical_insurance_type AS medicalInsuranceType, dept_code AS deptCode, ward_code AS wardCode, room_no AS roomNo, bed_no AS bedNo, " +
            "doctor_code AS doctorCode, nurse_code AS nurseCode, inpatient_serial_no AS inpatientSerialNo, " +
            "dept_date_time AS deptDateTime, discharged_date_time AS dischargedDateTime, " +
            "inpatient_status AS inpatientStatus, diagnose, condition_status AS conditionStatus, " +
            "nurse_level AS nurseLevel, allergen, diet_type AS dietType, notice " +
            "FROM tb_hospital_patient WHERE inpatient_no = #{inpatientNo}")
    Patient findByInpatientNo(String inpatientNo);
    
    /**
     * 插入新患者
     * @param patient 患者信息
     */
    @Insert("INSERT INTO tb_hospital_patient(admission_date_time, inpatient_no, name, sex, age, " +
            "medical_insurance_type, dept_code, ward_code, room_no, bed_no, " +
            "doctor_code, nurse_code, inpatient_serial_no, dept_date_time, discharged_date_time, " +
            "inpatient_status, diagnose, condition_status, nurse_level, allergen, diet_type, notice) " +
            "VALUES(#{admissionDateTime}, #{inpatientNo}, #{name}, #{sex}, #{age}, " +
            "#{medicalInsuranceType}, #{deptCode}, #{wardCode}, #{roomNo}, #{bedNo}, " +
            "#{doctorCode}, #{nurseCode}, #{inpatientSerialNo}, #{deptDateTime}, #{dischargedDateTime}, " +
            "#{inpatientStatus}, #{diagnose}, #{conditionStatus}, #{nurseLevel}, #{allergen}, #{dietType}, #{notice})")
    void insert(Patient patient);
    
    /**
     * 更新患者信息
     * @param patient 患者信息
     */
    @Update("UPDATE tb_hospital_patient SET admission_date_time=#{admissionDateTime}, name=#{name}, sex=#{sex}, age=#{age}, " +
            "medical_insurance_type=#{medicalInsuranceType}, dept_code=#{deptCode}, ward_code=#{wardCode}, " +
            "room_no=#{roomNo}, bed_no=#{bedNo}, doctor_code=#{doctorCode}, nurse_code=#{nurseCode}, " +
            "inpatient_serial_no=#{inpatientSerialNo}, dept_date_time=#{deptDateTime}, " +
            "discharged_date_time=#{dischargedDateTime}, inpatient_status=#{inpatientStatus}, " +
            "diagnose=#{diagnose}, condition_status=#{conditionStatus}, nurse_level=#{nurseLevel}, " +
            "allergen=#{allergen}, diet_type=#{dietType}, notice=#{notice} " +
            "WHERE inpatient_no=#{inpatientNo}")
    void update(Patient patient);
    
    /**
     * 根据住院号删除患者
     * @param inpatientNo 住院号
     */
    @Delete("DELETE FROM tb_hospital_patient WHERE inpatient_no = #{inpatientNo}")
    void deleteByInpatientNo(String inpatientNo);
}