<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mapper.PatientMapper">

    <resultMap id="PatientResultMap" type="org.example.entity.Patient">
        <result property="admissionDateTime" column="admission_date_time"/>
        <result property="inpatientNo" column="inpatient_no"/>
        <result property="name" column="name"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
        <result property="medicalInsuranceType" column="medical_insurance_type"/>
        <result property="deptCode" column="dept_code"/>
        <result property="wardCode" column="ward_code"/>
        <result property="roomNo" column="room_no"/>
        <result property="bedNo" column="bed_no"/>
        <result property="doctorCode" column="doctor_code"/>
        <result property="nurseCode" column="nurse_code"/>
        <result property="inpatientSerialNo" column="inpatient_serial_no"/>
        <result property="deptDateTime" column="dept_date_time"/>
        <result property="dischargedDateTime" column="discharged_date_time"/>
        <result property="inpatientStatus" column="inpatient_status"/>
        <result property="diagnose" column="diagnose"/>
        <result property="conditionStatus" column="condition_status"/>
        <result property="nurseLevel" column="nurse_level"/>
        <result property="allergen" column="allergen"/>
        <result property="dietType" column="diet_type"/>
        <result property="notice" column="notice"/>
    </resultMap>

    <!-- 查询所有患者 -->
    <select id="findAll" resultMap="PatientResultMap">
        SELECT 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
        FROM tb_hospital_patient
    </select>

    <!-- 根据条件查询患者 -->
    <select id="findWithConditions" resultMap="PatientResultMap">
        SELECT 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
        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>
    </select>

    <!-- 查询所有不同的护理级别 -->
    <select id="findDistinctNurseLevels" resultType="string">
        SELECT DISTINCT nurse_level
        FROM tb_hospital_patient
        WHERE nurse_level IS NOT NULL AND nurse_level != ''
    </select>

    <!-- 查询所有不同的在院状态 -->
    <select id="findDistinctInpatientStatuses" resultType="string">
        SELECT DISTINCT inpatient_status
        FROM tb_hospital_patient
        WHERE inpatient_status IS NOT NULL AND inpatient_status != ''
    </select>

    <!-- 根据住院号查询患者 -->
    <select id="findByInpatientNo" parameterType="string" resultMap="PatientResultMap">
        SELECT 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
        FROM tb_hospital_patient
        WHERE inpatient_no = #{inpatientNo}
    </select>

    <!-- 插入新患者 -->
    <insert id="insert" parameterType="org.example.entity.Patient">
        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})
    </insert>

    <!-- 更新患者信息 -->
    <update id="update" parameterType="org.example.entity.Patient">
        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}
    </update>

    <!-- 根据住院号删除患者 -->
    <delete id="deleteByInpatientNo" parameterType="string">
        DELETE FROM tb_hospital_patient WHERE inpatient_no = #{inpatientNo}
    </delete>

</mapper>
