<?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.docus.server.report.mapper.AfReportRecordMapper">

    <insert id="saveRecord">
        INSERT INTO `docus_archivefile`.`af_report_record`(`task_id`, `inpatient_no`, `jzh`, `admiss_times`, `down_url`,
                                                           `down_type`, `state`, `serialnum`, `sysflag`, `zd_assort_id`,
                                                           `create_time`, `update_time`, `file_name`, `patient_id`)
        VALUES (#{reportRecord.taskId}, #{reportRecord.inpatientNo}, #{reportRecord.jzh}, #{reportRecord.admissTimes},
                #{reportRecord.downUrl}, #{reportRecord.downType}, 0, #{reportRecord.serialnum},
                #{reportRecord.sysflag}, #{reportRecord.zdAssortId}, now(), now(), #{reportRecord.fileName},
                #{reportRecord.patientId});
    </insert>
    <update id="updateRecordByTaskId">
        update `docus_archivefile`.`af_report_record`
        set `down_url`=#{reportRecord.downUrl},
            `down_type`=#{reportRecord.downType},
            `state`=0,
            `file_name`=#{reportRecord.fileName},
            `patient_id`=#{reportRecord.patientId},
            `update_time`=now()
        where `task_id` = #{reportRecord.taskId}
    </update>
    <update id="updateStateByTaskId">
        update `docus_archivefile`.`af_report_record`
        set `state`= 1,
            `update_time`=now()
        where `task_id` = #{taskId}
    </update>
    <update id="updateReportRecordPatientId">
        update `docus_archivefile`.`af_report_record`
        set `patient_id`= #{patientId}
        where `task_id` = #{taskId}
    </update>
    <select id="getRecordBySerialnumAndInpatientNoAndSysFlag"
            resultType="com.docus.server.report.entity.AfReportRecord">
        select *
        from `docus_archivefile`.`af_report_record`
        where `inpatient_no` = #{inpatientNo}
          and `serialnum` = #{serialnum}
          and `sysflag` = #{sysFlag}
          and admiss_times = #{admisstimes}
    </select>
    <select id="getReportRecordInfoByTaskId" resultType="com.docus.server.report.entity.AfReportRecord">
        select *
        from `docus_archivefile`.`af_report_record`
        where `task_id` = #{taskId}
    </select>
    <select id="getHasPatientIdTaskIdsByTaskIds" resultType="java.lang.Long">
        SELECT `task_id`
        FROM `docus_archivefile`.`af_report_record`
        WHERE `patient_id` IS NOT NULL
        AND `patient_id` != ''
        AND `task_id` IN
        <foreach collection="taskIds" separator="," open="(" close=")" item="taskId">
            #{taskId}
        </foreach>
    </select>
    <select id="getStartStateReportRecord" resultType="com.docus.server.report.entity.AfReportRecord">
        SELECT `task_id`, `inpatient_no`, `admiss_times`, `patient_id`
        FROM `docus_archivefile`.`af_report_record`
        WHERE `state` = 0
            LIMIT ${(page-1)*pageSize}
            , ${pageSize}
    </select>
</mapper>
