<?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="com.ruoyi.framework.system.mapper.PatientsMapper">
    
    <resultMap type="Patients" id="PatientsResult">
        <result property="PatientID"    column="PatientID"    />
        <result property="Name"    column="Name"    />
        <result property="Gender"    column="Gender"    />
        <result property="BirthDate"    column="BirthDate"    />
        <result property="ContactInfo"    column="ContactInfo"    />
        <result property="Address"    column="Address"    />
        <result property="Classification"    column="Classification"    />
        <result property="EmergencyContact"    column="EmergencyContact"    />
        <result property="EmergencyContactPhone"    column="EmergencyContactPhone"    />
        <result property="InsuranceProvider"    column="InsuranceProvider"    />
        <result property="InsurancePolicyNumber"    column="InsurancePolicyNumber"    />
        <result property="Allergies"    column="Allergies"    />
        <result property="MedicalHistory"    column="MedicalHistory"    />
        <result property="LastVisitDate"    column="LastVisitDate"    />
        <result property="NextFollowUpDate"    column="NextFollowUpDate"    />
        <result property="CreatedAt"    column="CreatedAt"    />
        <result property="UpdatedAt"    column="UpdatedAt"    />
        <result property="DockerId"    column="DockerId"    />
    </resultMap>

    <sql id="selectPatientsVo">
        select PatientID, Name, Gender, BirthDate, ContactInfo, Address, Classification, EmergencyContact, EmergencyContactPhone, InsuranceProvider, InsurancePolicyNumber, Allergies, MedicalHistory, LastVisitDate, NextFollowUpDate, CreatedAt, UpdatedAt, DockerId from Patients
    </sql>

    <select id="selectPatientsList" parameterType="Patients" resultMap="PatientsResult">
        <include refid="selectPatientsVo"/>
        <where>  
            <if test="Name != null  and Name != ''"> and Name like concat('%', #{Name}, '%')</if>
            <if test="Gender != null  and Gender != ''"> and Gender = #{Gender}</if>
            <if test="BirthDate != null "> and BirthDate = #{BirthDate}</if>
            <if test="ContactInfo != null  and ContactInfo != ''"> and ContactInfo = #{ContactInfo}</if>
            <if test="Address != null  and Address != ''"> and Address = #{Address}</if>
            <if test="Classification != null  and Classification != ''"> and Classification = #{Classification}</if>
            <if test="EmergencyContact != null  and EmergencyContact != ''"> and EmergencyContact = #{EmergencyContact}</if>
            <if test="EmergencyContactPhone != null  and EmergencyContactPhone != ''"> and EmergencyContactPhone = #{EmergencyContactPhone}</if>
            <if test="InsuranceProvider != null  and InsuranceProvider != ''"> and InsuranceProvider = #{InsuranceProvider}</if>
            <if test="InsurancePolicyNumber != null  and InsurancePolicyNumber != ''"> and InsurancePolicyNumber = #{InsurancePolicyNumber}</if>
            <if test="Allergies != null  and Allergies != ''"> and Allergies = #{Allergies}</if>
            <if test="MedicalHistory != null  and MedicalHistory != ''"> and MedicalHistory = #{MedicalHistory}</if>
            <if test="LastVisitDate != null "> and LastVisitDate = #{LastVisitDate}</if>
            <if test="NextFollowUpDate != null "> and NextFollowUpDate = #{NextFollowUpDate}</if>
            <if test="CreatedAt != null "> and CreatedAt = #{CreatedAt}</if>
            <if test="UpdatedAt != null "> and UpdatedAt = #{UpdatedAt}</if>
            <if test="DockerId != null "> and DockerId = #{DockerId}</if>
        </where>
    </select>
    
    <select id="selectPatientsByPatientID" parameterType="Long" resultMap="PatientsResult">
        <include refid="selectPatientsVo"/>
        where PatientID = #{PatientID}
    </select>

    <insert id="insertPatients" parameterType="Patients" useGeneratedKeys="true" keyProperty="PatientID">
        insert into Patients
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="Name != null and Name != ''">Name,</if>
            <if test="Gender != null and Gender != ''">Gender,</if>
            <if test="BirthDate != null">BirthDate,</if>
            <if test="ContactInfo != null">ContactInfo,</if>
            <if test="Address != null">Address,</if>
            <if test="Classification != null">Classification,</if>
            <if test="EmergencyContact != null">EmergencyContact,</if>
            <if test="EmergencyContactPhone != null">EmergencyContactPhone,</if>
            <if test="InsuranceProvider != null">InsuranceProvider,</if>
            <if test="InsurancePolicyNumber != null">InsurancePolicyNumber,</if>
            <if test="Allergies != null">Allergies,</if>
            <if test="MedicalHistory != null">MedicalHistory,</if>
            <if test="LastVisitDate != null">LastVisitDate,</if>
            <if test="NextFollowUpDate != null">NextFollowUpDate,</if>
            <if test="CreatedAt != null">CreatedAt,</if>
            <if test="UpdatedAt != null">UpdatedAt,</if>
            <if test="DockerId != null">DockerId,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="Name != null and Name != ''">#{Name},</if>
            <if test="Gender != null and Gender != ''">#{Gender},</if>
            <if test="BirthDate != null">#{BirthDate},</if>
            <if test="ContactInfo != null">#{ContactInfo},</if>
            <if test="Address != null">#{Address},</if>
            <if test="Classification != null">#{Classification},</if>
            <if test="EmergencyContact != null">#{EmergencyContact},</if>
            <if test="EmergencyContactPhone != null">#{EmergencyContactPhone},</if>
            <if test="InsuranceProvider != null">#{InsuranceProvider},</if>
            <if test="InsurancePolicyNumber != null">#{InsurancePolicyNumber},</if>
            <if test="Allergies != null">#{Allergies},</if>
            <if test="MedicalHistory != null">#{MedicalHistory},</if>
            <if test="LastVisitDate != null">#{LastVisitDate},</if>
            <if test="NextFollowUpDate != null">#{NextFollowUpDate},</if>
            <if test="CreatedAt != null">#{CreatedAt},</if>
            <if test="UpdatedAt != null">#{UpdatedAt},</if>
            <if test="DockerId != null">#{DockerId},</if>
         </trim>
    </insert>

    <update id="updatePatients" parameterType="Patients">
        update Patients
        <trim prefix="SET" suffixOverrides=",">
            <if test="Name != null and Name != ''">Name = #{Name},</if>
            <if test="Gender != null and Gender != ''">Gender = #{Gender},</if>
            <if test="BirthDate != null">BirthDate = #{BirthDate},</if>
            <if test="ContactInfo != null">ContactInfo = #{ContactInfo},</if>
            <if test="Address != null">Address = #{Address},</if>
            <if test="Classification != null">Classification = #{Classification},</if>
            <if test="EmergencyContact != null">EmergencyContact = #{EmergencyContact},</if>
            <if test="EmergencyContactPhone != null">EmergencyContactPhone = #{EmergencyContactPhone},</if>
            <if test="InsuranceProvider != null">InsuranceProvider = #{InsuranceProvider},</if>
            <if test="InsurancePolicyNumber != null">InsurancePolicyNumber = #{InsurancePolicyNumber},</if>
            <if test="Allergies != null">Allergies = #{Allergies},</if>
            <if test="MedicalHistory != null">MedicalHistory = #{MedicalHistory},</if>
            <if test="LastVisitDate != null">LastVisitDate = #{LastVisitDate},</if>
            <if test="NextFollowUpDate != null">NextFollowUpDate = #{NextFollowUpDate},</if>
            <if test="CreatedAt != null">CreatedAt = #{CreatedAt},</if>
            <if test="UpdatedAt != null">UpdatedAt = #{UpdatedAt},</if>
            <if test="DockerId != null">DockerId = #{DockerId},</if>
        </trim>
        where PatientID = #{PatientID}
    </update>

    <delete id="deletePatientsByPatientID" parameterType="Long">
        delete from Patients where PatientID = #{PatientID}
    </delete>

    <delete id="deletePatientsByPatientIDs" parameterType="String">
        delete from Patients where PatientID in 
        <foreach item="PatientID" collection="array" open="(" separator="," close=")">
            #{PatientID}
        </foreach>
    </delete>

    <select id="queryStatistics" resultType="com.ruoyi.framework.system.domain.vo.PatientsVo">
        select count(1) as total, Classification
        from Patients
        <where>
            <if test="vo.classification != null and vo.classification != ''">
                Classification = #{vo.classification}
            </if>
            <if test="vo.LastVisitDate != null and vo.LastVisitDate != '' and vo.NextFollowUpDate != null">
               and  LastVisitDate >= #{vo.lastVisitDate} and LastVisitDate &lt;= #{vo.nextFollowUpDate}
            </if>
        </where>
        group by Classification
    </select>
</mapper>