<?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.archive.mapper.TBasicMapper">
    <insert id="insertCollectTask">
        INSERT INTO docus_archivefile.af_collect_task (id, patient_id, af_archive_detail_id, sysflag, state,
                                                       sync_time, C1, C2, C3, C4, C5, C6, C7, T1, T2, T3, create_time)
        VALUES(#{task.id},#{task.patientId},#{task.afArchiveDetailId},#{task.sysflag},#{task.state},
               #{task.syncTime},#{task.c1},#{task.c2},#{task.c3},#{task.c4},
               #{task.c5},#{task.c6},#{task.c7},#{task.t1},#{task.t2},#{task.t3},#{task.createTime})
    </insert>
    <insert id="saveOrUpdateCollectLog">
        INSERT INTO docus_archivefile.af_collect_patient_log
            (patient_id, collect_id, last_collect_time, file_count)
        VALUES
        (#{cpLog.patientId}, #{cpLog.collectId}, #{cpLog.lastCollectTime}, #{cpLog.fileCount})
        ON DUPLICATE KEY UPDATE
        last_collect_time = VALUES(last_collect_time),
        file_count = VALUES(file_count)
    </insert>
    <update id="updateTaskById">
        update docus_archivefile.af_collect_task set sync_time=#{task.syncTime}, C2=#{task.c2}, C3=#{task.c3}, C4=#{task.c4}, C5=#{task.c5},
                                                     C6=#{task.c6}, C7=#{task.c7},T1=#{task.t1}, T2=#{task.t2}, T3=#{task.t3}
                                                     where id=#{task.id}
    </update>

    <update id="invalidFileBySource">
        update docus_archivefile.t_scan_assort set is_del=1
        where patient_id =#{patientId} and source= #{collectorId}
    </update>
    <update id="flushPatTaskEndTime">
        update docus_archivefile.af_collect_task set end_time=#{dateTime} where patient_id=#{patientId}
    </update>

    <update id="mergeYzyFile">
        update docus_archivefile.t_scan_assort set patient_id=#{zyPatientId},assort_id = 'Wiw213woq412awqe42'   where  patient_id=#{yzhPatientId}
    </update>

    <delete id="deleteTaskByPatAndSource">
        delete from  docus_archivefile.af_collect_task where patient_id=#{patientId} and sysflag=#{collectorId}
    </delete>
    <delete id="delTaskById">
        delete from  docus_archivefile.af_collect_task where id=#{id}
    </delete>
    <update id="cancelTask">
        update docus_archivefile.af_collect_task set state='4',end_time=now() where patient_id=#{patientId} and sysflag=#{sysflag}
    </update>
    <update id="updateTaskRemarkById">
        UPDATE docus_archivefile.af_collect_task
        SET remark = #{remark},end_time=now()
        WHERE id = #{id}
    </update>

    <select id="getTbasicByReportTimeAndInpNoOrIdCard" resultType="com.docus.server.archive.entity.TBasic">
        select
            patient_id as patientId,
            inpatient_no as   inpatientNo,
            name,
            admiss_date as admissDate,
            dis_date as  disDate,
            id_card as   idCard,
            jzh
        from docus_medicalrecord.t_basic
        where
        file_source = 1
        and   DATE_FORMAT(admiss_date, '%Y-%m-%d')  <![CDATA[ <= ]]>  #{sendDateStr}
        and (dis_date >=  #{sendDateStr} or dis_date is null)
        and (id_card=#{idCard} or inpatient_no=#{inpatientNo})
    </select>
    <select id="getCollectTaskByPatAndSource" resultType="com.docus.server.archive.entity.AfCollectTask">
       select *  from  docus_archivefile.af_collect_task where patient_id=#{patientId} and sysflag=#{collectorId}
    </select>
    <select id="getTbasicByPatientIds" resultType="com.docus.server.archive.entity.TBasic">
        select
            patient_id as patientId,
            inpatient_no as   inpatientNo,
            admiss_times as admissTimes,
            name,
            admiss_date as admissDate,
            dis_date as  disDate,
            id_card as   idCard,
            jzh,
            emp_id as empId
        from docus_medicalrecord.t_basic
        where patient_id in
            <foreach collection="patientIds" open="(" close=")" separator="," item="patientId">
                #{patientId}
            </foreach>
    </select>
    <select id="findFailedTaskPatId" resultType="java.lang.String">
        select patient_id
        from (
            select patient_id
            from docus_archivefile.af_collect_task
            where create_time >= #{startDate}
                and sysflag = #{collectorId}
                and state not in ('3', '4')

              union

            select patient_id
            from docus_archivefile.af_collect_task
            where sync_time >= #{startDate}
              and sysflag = #{collectorId}
              and state not in ('3', '4')
             ) t1
        group by patient_id
    </select>
    <select id="getZyPatientIdsByYzyJzh" resultType="com.docus.server.archive.entity.BasicExtend">
        select patient_id,pre_jzh
        from docus_medicalrecord.t_basic_extend
        where pre_jzh in
        <foreach collection="preJzhs" open="(" close=")" separator="," item="preJzh">
            #{preJzh}
        </foreach>
    </select>
    <select id="getYzyPatientIdFromScanAssort" resultType="java.lang.String">
        select distinct patient_id
        from docus_archivefile.t_scan_assort
        where patient_id in (
            select patient_id
            from docus_medicalrecord.t_basic
            where jzh in (
                select pre_jzh
                from docus_medicalrecord.t_basic_extend
                where pre_jzh is not null
                )
            )
    </select>
    <select id="getNoTaskPatientByDisDate" resultType="com.docus.server.archive.entity.TBasic">
        select patient_id   as patientId,
               inpatient_no as inpatientNo,
               name,
               admiss_date  as admissDate,
               dis_date     as disDate,
               id_card      as idCard,
               jzh
        from docus_medicalrecord.t_basic
        where file_source = 1
          and dis_date >= #{startDateTime}
          and id_card is not null
          and id_card != ''
and patient_id not in(
	select patient_id
	from docus_archivefile.af_collect_task act
	where  sysflag = #{collectorId}
            )
    </select>
    <select id="getPatientState" resultType="com.docus.server.archive.dto.PatientState">
        select tb.patient_id as patientId,
               tb.is_print   as isPrint,
               tb.is_archive as isArchive,
               state.is_seal as isSeal,
               state.is_lock as isLock
        from docus_medicalrecord.t_basic tb
             left join docus_medicalrecord.sm_storage_state state on state.patient_id = tb.patient_id
        where tb.patient_id = #{patientId}
    </select>
    <select id="getCollectLog" resultType="com.docus.server.archive.entity.AfCollectPatientLog">
        select patient_id, collect_id, last_collect_time, file_count
        from docus_archivefile.af_collect_patient_log
        where patient_id = #{patientId}
        and collect_id = #{collectorId}
        <if test="offsetTime != null and offsetTime != ''">
        and  last_collect_time &lt; #{offsetTime}
        </if>
    </select>
    <select id="getPatientIdsByDisDate" resultType="java.lang.String">
        select tb.patient_id
        from docus_medicalrecord.t_basic tb
        where dis_date between #{disDateStart} and #{disDateEnd}
        and file_source = 1
    </select>
    <select id="getNoCollectPatientIds" resultType="java.lang.String">
        select  patient_id
        from docus_medicalrecord.t_basic tb
        where
            dis_date > '2020-01-01 00:00:00'
          and file_source = 1
          and not exists (
            SELECT patient_id,collect_id
            FROM docus_archivefile.af_collect_patient_log acpl
            where acpl.patient_id = tb.patient_id and acpl.collect_id = #{collectorId}
        )
    </select>

</mapper>
