<?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.patienttaskstatistics.PatientTaskStatisticsMapper">

    <resultMap type="PatientTaskStatistics" id="PatientTaskStatisticsResult">
	        <result property="patientId" column="patient_id"/>
            <result property="signPatientRecordId" column="sign_patient_record_id"/>
            <result property="serviceStatus" column="service_status"/>
            <result property="routeCheckStatus" column="route_check_status"/>
            <result property="taskNum" column="task_num"/>
            <result property="taskExecuteNum" column="task_execute_num"/>
            <result property="taskFinishNum" column="task_finish_num"/>
		    <result property="nextTaskName" column="next_task_name"/>
		    <result property="nextTaskTime" column="next_task_time"/>
    </resultMap>

    <sql id="selectPatientTaskStatisticsVo">
        select patient_id, sign_patient_record_id, service_status, route_check_status, task_num, task_execute_num, task_finish_num,next_task_name,next_task_time from patient_task_statistics
    </sql>

    <select id="selectPatientTaskStatisticsList" parameterType="PatientTaskStatistics" resultMap="PatientTaskStatisticsResult">
        <include refid="selectPatientTaskStatisticsVo"/>
        <where>
            <if test="patientId != null ">
                and patient_id = #{patientId}
            </if>
	        <if test="signPatientRecordId != null ">
		        and sign_patient_record_id = #{signPatientRecordId}
	        </if>
            <if test="serviceStatus != null  and serviceStatus != ''">
                and service_status = #{serviceStatus}
            </if>
            <if test="routeCheckStatus != null  and routeCheckStatus != ''">
                and route_check_status = #{routeCheckStatus}
            </if>
            <if test="taskNum != null ">
                and task_num = #{taskNum}
            </if>
            <if test="taskExecuteNum != null ">
                and task_execute_num = #{taskExecuteNum}
            </if>
            <if test="taskFinishNum != null ">
                and task_finish_num = #{taskFinishNum}
            </if>
        </where>
    </select>

    <select id="selectPatientTaskStatisticsByPatientId" parameterType="Long"
            resultMap="PatientTaskStatisticsResult">
            <include refid="selectPatientTaskStatisticsVo"/>
            where patient_id = #{patientId}
    </select>

    <insert id="insertPatientTaskStatistics" parameterType="PatientTaskStatistics">
        insert into patient_task_statistics
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="signPatientRecordId != null">sign_patient_record_id,
            </if>
            <if test="patientId != null">patient_id,
            </if>
            <if test="serviceStatus != null">service_status,
            </if>
            <if test="routeCheckStatus != null">route_check_status,
            </if>
            <if test="taskNum != null">task_num,
            </if>
            <if test="taskExecuteNum != null">task_execute_num,
            </if>
            <if test="taskFinishNum != null">task_finish_num,
            </if>
	        <if test="nextTaskName != null">next_task_name,
	        </if>
	        <if test="nextTaskTime != null">next_task_time,
	        </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="signPatientRecordId != null">#{signPatientRecordId},
            </if>
            <if test="patientId != null">#{patientId},
            </if>
            <if test="serviceStatus != null">#{serviceStatus},
            </if>
            <if test="routeCheckStatus != null">#{routeCheckStatus},
            </if>
            <if test="taskNum != null">#{taskNum},
            </if>
            <if test="taskExecuteNum != null">#{taskExecuteNum},
            </if>
            <if test="taskFinishNum != null">#{taskFinishNum},
            </if>
	        <if test="nextTaskName != null">#{nextTaskName},
	        </if>
	        <if test="nextTaskTime != null">#{nextTaskTime},
	        </if>
        </trim>
    </insert>

    <update id="updatePatientTaskStatistics" parameterType="PatientTaskStatistics">
        update patient_task_statistics
        <trim prefix="SET" suffixOverrides=",">
            <if test="signPatientRecordId != null">sign_patient_record_id =
                #{signPatientRecordId},
            </if>
            <if test="serviceStatus != null">service_status =
                #{serviceStatus},
            </if>
            <if test="routeCheckStatus != null">route_check_status =
                #{routeCheckStatus},
            </if>
            <if test="taskNum != null">task_num =
                #{taskNum},
            </if>
            <if test="taskExecuteNum != null">task_execute_num =
                #{taskExecuteNum},
            </if>
            <if test="taskFinishNum != null">task_finish_num =
                #{taskFinishNum},
            </if>
	        <if test="nextTaskName != null">next_task_name =
		        #{nextTaskName},
	        </if>
	        <if test="nextTaskTime != null">next_task_time =
		        #{nextTaskTime},
	        </if>
        </trim>
        where patient_id = #{patientId}
    </update>

	<select id="selectNum" parameterType="Long" resultType="com.xinelu.manage.domain.patienttaskstatistics.PatientTaskStatistics">
		select id as patientId, sign_patient_record_id as signPatientRecordId,
		       sum(task_execute_status) as taskExecuteNum, sum(task_finish_status) as taskFinishNum
		from patient_task_statistics_view
		where id = #{patientId}
	</select>

	<select id="getPatientNextTask" parameterType="Long" resultType="com.xinelu.manage.vo.patientinfo.PatientNextTaskVo">
		select p.task_name, p.execute_date_time from patient_task_statistics_view p
		where p.id = #{patientId} and p.node_execute_status = 'UNEXECUTED'
		order by p.execute_date_time limit 1
	</select>

    <delete id="deletePatientTaskStatisticsByPatientId" parameterType="Long">
        delete from patient_task_statistics where patient_id = #{patientId}
    </delete>

    <delete id="deletePatientTaskStatisticsByPatientIds" parameterType="String">
        delete from patient_task_statistics where patient_id in
        <foreach item="patientId" collection="array" open="(" separator="," close=")">
            #{patientId}
        </foreach>
    </delete>
</mapper>