<?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.xinelu.manage.mapper.patientdiseaseinfo.PatientDiseaseInfoMapper">

    <resultMap type="PatientDiseaseInfo" id="PatientDiseaseInfoResult">
        <result property="id" column="id"/>
        <result property="patientId" column="patient_id"/>
        <result property="diseaseId" column="disease_id"/>
        <result property="userId" column="user_id"/>
        <result property="diseaseName" column="disease_name"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectPatientDiseaseInfoVo">
        select id,
               patient_id,
               disease_id,
               user_id,
               disease_name,
               create_by,
               create_time,
               update_by,
               update_time
        from patient_disease_info
    </sql>

    <select id="selectPatientDiseaseInfoList" parameterType="PatientDiseaseInfo" resultMap="PatientDiseaseInfoResult">
        <include refid="selectPatientDiseaseInfoVo"/>
        <where>
            <if test="patientId != null ">
                and patient_id = #{patientId}
            </if>
            <if test="diseaseId != null ">
                and disease_id = #{diseaseId}
            </if>
            <if test="userId != null ">
                and user_id = #{userId}
            </if>
            <if test="diseaseName != null ">
                and disease_name = #{diseaseName}
            </if>
        </where>
    </select>


    <select id="selectPatientDiseasePatientId" parameterType="PatientDiseaseInfo" resultMap="PatientDiseaseInfoResult">
        <include refid="selectPatientDiseaseInfoVo"/>
        <where>
            <if test="patientId != null ">
                and patient_id = #{patientId}
            </if>
            <if test="diseaseId != null ">
                and disease_id = #{diseaseId}
            </if>
            <if test="userId != null ">
                and user_id = #{userId}
            </if>
            <if test="diseaseName != null ">
                and disease_name = #{diseaseName}
            </if>
        </where>
    </select>

    <select id="selectPatientDiseaseInfoById" parameterType="Long"
            resultMap="PatientDiseaseInfoResult">
        <include refid="selectPatientDiseaseInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertPatientDiseaseInfo" parameterType="PatientDiseaseInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into patient_disease_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="patientId != null">patient_id,
            </if>
            <if test="diseaseId != null">disease_id,
            </if>
            <if test="userId != null">user_id,
            </if>
            <if test="diseaseName != null ">disease_name,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="patientId != null">#{patientId},
            </if>
            <if test="diseaseId != null">#{diseaseId},
            </if>
            <if test="userId != null">#{userId},
            </if>
            <if test="diseaseName != null ">#{diseaseName},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updatePatientDiseaseInfo" parameterType="PatientDiseaseInfo">
        update patient_disease_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="patientId != null">patient_id =
                #{patientId},
            </if>
            <if test="diseaseId != null">disease_id =
                #{diseaseId},
            </if>
            <if test="userId != null">user_id =
                #{userId},
            </if>
            <if test="diseaseName != null ">disease_name =
                #{diseaseName},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePatientDiseaseInfoById" parameterType="Long">
        delete
        from patient_disease_info
        where id = #{id}
    </delete>

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

    <insert id="insertBatchPatientDiseaseInfo">
        insert into patient_disease_info(
        patient_id,
        disease_id,
        disease_name,
        create_time
        ) values
        <foreach item="patientDisease" index="index" collection="list" separator=",">
            (
            #{patientDisease.patientId},
            #{patientDisease.diseaseId},
            #{patientDisease.diseaseName},
            #{patientDisease.createTime}
            )
        </foreach>
    </insert>

    <select id="selectPatientDiseaseInfoByOrderNo"
            resultType="com.xinelu.manage.domain.patientdiseaseinfo.PatientDiseaseInfo">
        SELECT ao.patient_id,
               pdi.disease_name
        FROM appointment_order ao
                 LEFT JOIN patient_info pi ON ao.patient_id = pi.id
                 LEFT JOIN patient_disease_info pdi ON pdi.patient_id = pi.id
        WHERE ao.order_no = #{orderNo}
    </select>
</mapper>
