<?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.patienttaskexecuterecord.PatientTaskExecuteRecordMapper">

    <resultMap type="PatientTaskExecuteRecord" id="PatientTaskExecuteRecordResult">
        <result property="id" column="id"/>
        <result property="patientId" column="patient_id"/>
        <result property="manageRouteId" column="manage_route_id"/>
        <result property="manageRouteNodeId" column="manage_route_node_id"/>
        <result property="patientName" column="patient_name"/>
        <result property="manageRouteName" column="manage_route_name"/>
        <result property="manageRouteNodeName" column="manage_route_node_name"/>
        <result property="taskContent" column="task_content"/>
        <result property="executeTime" column="execute_time"/>
        <result property="executePerson" column="execute_person"/>
        <result property="executeType" column="execute_type"/>
        <result property="executeRemark" column="execute_remark"/>
        <result property="visitRecordId" column="visit_record_id"/>
        <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="selectPatientTaskExecuteRecordVo">
        select id,
               patient_id,
               manage_route_id,
               manage_route_node_id,
               patient_name,
               manage_route_name,
               manage_route_node_name,
               task_content,
               execute_time,
               execute_person,
               execute_type,
               execute_remark,
               visit_record_id,
               create_by,
               create_time,
               update_by,
               update_time
        from patient_task_execute_record
    </sql>

    <select id="selectPatientTaskExecuteRecordList" parameterType="PatientTaskExecuteRecord"
            resultMap="PatientTaskExecuteRecordResult">
        <include refid="selectPatientTaskExecuteRecordVo"/>
        <where>
            <if test="patientId != null ">
                and patient_id = #{patientId}
            </if>
            <if test="manageRouteId != null ">
                and manage_route_id = #{manageRouteId}
            </if>
            <if test="manageRouteNodeId != null ">
                and manage_route_node_id = #{manageRouteNodeId}
            </if>
            <if test="visitRecordId != null ">
                and visit_record_id = #{visitRecordId}
            </if>
            <if test="patientName != null  and patientName != ''">
                and patient_name like concat('%', #{patientName}, '%')
            </if>
            <if test="manageRouteName != null  and manageRouteName != ''">
                and manage_route_name like concat('%', #{manageRouteName}, '%')
            </if>
            <if test="manageRouteNodeName != null  and manageRouteNodeName != ''">
                and manage_route_node_name like concat('%', #{manageRouteNodeName}, '%')
            </if>
            <if test="taskContent != null  and taskContent != ''">
                and task_content = #{taskContent}
            </if>
            <if test="executeTime != null ">
                and execute_time = #{executeTime}
            </if>
            <if test="executePerson != null  and executePerson != ''">
                and execute_person = #{executePerson}
            </if>
            <if test="executeType != null  and executeType != ''">
                and execute_type = #{executeType}
            </if>
            <if test="executeRemark != null  and executeRemark != ''">
                and execute_remark = #{executeRemark}
            </if>
        </where>
    </select>

    <select id="selectPatientAndExecuteRecordList"
            resultType="com.xinelu.manage.vo.patienttaskexecuterecord.PatientTaskExecuteRecordVO">
        select pter.id,
        pter.patient_id,
        pter.manage_route_id,
        pter.manage_route_node_id,
        pter.patient_name,
        pter.manage_route_name,
        pter.manage_route_node_name,
        pter.task_content,
        pter.execute_time,
        pter.execute_person,
        pter.execute_type,
        pi.patient_type,
        pi.visit_method,
        pi.hospital_agency_name,
        pi.campus_agency_name,
        pi.department_name,
        pi.ward_name,
        pi.visit_serial_number,
        pi.admission_time,
        pi.discharge_time,
        pi.in_hospital_number,
        pi.patient_phone,
        spmrn.task_type,
        spmrn.route_handle_remark,
        CASE
        WHEN spmrn.task_type = 'PHONE_OUTBOUND' THEN spmrn.phone_id
        WHEN spmrn.task_type = 'QUESTIONNAIRE_SCALE' THEN spmrn.question_info_id
        WHEN spmrn.task_type = 'ARTIFICIAL_FOLLOW_UP' THEN spmrn.follow_template_id
        WHEN spmrn.task_type = 'PROPAGANDA_ARTICLE' THEN spmrn.propaganda_info_id
        END AS 'templateId',
        IF(spmrn.task_type ='TEXT_REMIND',spmrn.text_remind_content,NULL) AS textRemindContent
        from patient_task_execute_record pter
        LEFT JOIN patient_info pi ON pi.id = pter.patient_id
        LEFT JOIN sign_patient_manage_route_node spmrn ON pter.manage_route_node_id = spmrn.id
        where pi.del_flag = 0
        <if test="patientName != null  and patientName != ''">
            and pi.patient_name = #{patientName}
        </if>
        <if test="patientPhone != null  and patientPhone != ''">
            and pi.patient_phone = #{patientPhone}
        </if>
        <if test="hospitalAgencyId != null ">
            and pi.hospital_agency_id = #{hospitalAgencyId}
        </if>
        <if test="visitSerialNumber != null  and visitSerialNumber != ''">
            and pi.visit_serial_number = #{visitSerialNumber}
        </if>
        <if test="visitMethod != null  and visitMethod != ''">
            and pi.visit_method = #{visitMethod}
        </if>
        <if test="startDate != null ">
            and pter.execute_time &gt;=#{startDate}
        </if>
        <if test="endDate != null ">
            and pter.execute_time &lt;= #{endDate}
        </if>
        <if test="manageRouteName != null  and manageRouteName != ''">
            and pter.manage_route_name like concat('%', #{manageRouteName}, '%')
        </if>
    </select>

    <select id="selectPatientTaskExecuteRecordById" parameterType="Long"
            resultMap="PatientTaskExecuteRecordResult">
        <include refid="selectPatientTaskExecuteRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertPatientTaskExecuteRecord" parameterType="PatientTaskExecuteRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into patient_task_execute_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="patientId != null">patient_id,
            </if>
            <if test="manageRouteId != null">manage_route_id,
            </if>
            <if test="manageRouteNodeId != null">manage_route_node_id,
            </if>
            <if test="patientName != null">patient_name,
            </if>
            <if test="manageRouteName != null">manage_route_name,
            </if>
            <if test="manageRouteNodeName != null">manage_route_node_name,
            </if>
            <if test="taskContent != null">task_content,
            </if>
            <if test="executeTime != null">execute_time,
            </if>
            <if test="executePerson != null">execute_person,
            </if>
            <if test="executeType != null">execute_type,
            </if>
            <if test="executeRemark != null">execute_remark,
            </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>
            <if test="visitRecordId != null">visit_record_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="patientId != null">#{patientId},
            </if>
            <if test="manageRouteId != null">#{manageRouteId},
            </if>
            <if test="manageRouteNodeId != null">#{manageRouteNodeId},
            </if>
            <if test="patientName != null">#{patientName},
            </if>
            <if test="manageRouteName != null">#{manageRouteName},
            </if>
            <if test="manageRouteNodeName != null">#{manageRouteNodeName},
            </if>
            <if test="taskContent != null">#{taskContent},
            </if>
            <if test="executeTime != null">#{executeTime},
            </if>
            <if test="executePerson != null">#{executePerson},
            </if>
            <if test="executeType != null">#{executeType},
            </if>
            <if test="executeRemark != null">#{executeRemark},
            </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>
            <if test="visitRecordId != null">#{visitRecordId},
            </if>
        </trim>
    </insert>

    <update id="updatePatientTaskExecuteRecord" parameterType="PatientTaskExecuteRecord">
        update patient_task_execute_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="patientId != null">patient_id =
                #{patientId},
            </if>
            <if test="manageRouteId != null">manage_route_id =
                #{manageRouteId},
            </if>
            <if test="manageRouteNodeId != null">manage_route_node_id =
                #{manageRouteNodeId},
            </if>
            <if test="visitRecordId != null ">
                and visit_record_id = #{visitRecordId}
            </if>
            <if test="patientName != null">patient_name =
                #{patientName},
            </if>
            <if test="manageRouteName != null">manage_route_name =
                #{manageRouteName},
            </if>
            <if test="manageRouteNodeName != null">manage_route_node_name =
                #{manageRouteNodeName},
            </if>
            <if test="taskContent != null">task_content =
                #{taskContent},
            </if>
            <if test="executeTime != null">execute_time =
                #{executeTime},
            </if>
            <if test="executePerson != null">execute_person =
                #{executePerson},
            </if>
            <if test="executeType != null">execute_type =
                #{executeType},
            </if>
            <if test="executeRemark != null">execute_remark =
                #{executeRemark},
            </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="deletePatientTaskExecuteRecordById" parameterType="Long">
        delete
        from patient_task_execute_record
        where id = #{id}
    </delete>

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

    <select id="selectVisitRecord"
            resultType="com.xinelu.manage.vo.patienttaskexecuterecord.PatientTaskExecuteRecordVO">
        select pvr.in_hospital_info,
               pvr.out_hospital_info,
               pvr.surgical_record,
               ri.patient_name,
               ri.patient_phone,
               ri.family_member_phone,
               ri.birth_date,
               ri.card_no,
               ri.sex,
               ri.address
        FROM patient_task_execute_record pter
                 LEFT JOIN patient_visit_record pvr ON pter.visit_record_id = pvr.id
                 LEFT JOIN patient_info pi ON pi.id = pter.patient_id
                 LEFT JOIN resident_info ri ON ri.id = pi.resident_id
        where pter.id = #{id}
    </select>
</mapper>