<?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.message.mapper.TBasicMapper">
    <insert id="insertOrUpdateDoctorSubmitTime">
        insert into `docus_medicalrecord`.`t_basic_extend` (patient_id,doctor_submit_time) values (#{patientId},#{submitTime})
        ON DUPLICATE KEY UPDATE doctor_submit_time=#{submitTime}
    </insert>
    <insert id="insertOrUpdateNurseSubmitTime">
        insert into `docus_medicalrecord`.`t_basic_extend` (patient_id,nurse_submit_time) values (#{patientId},#{submitTime})
            ON DUPLICATE KEY UPDATE nurse_submit_time=#{submitTime}
    </insert>
    <insert id="insertOrUpdateNurseFileCount">
        insert into `docus_medicalrecord`.`t_basic_extend` (patient_id,nurse_file_count) values (#{patientId},#{fileCount})
            ON DUPLICATE KEY UPDATE nurse_file_count=#{fileCount}
    </insert>
    <insert id="insertOrUpdateNurse">
        insert into `docus_medicalrecord`.`t_basic_extend` (patient_id,qc_nurse,duty_nurse) values (#{patientId},#{qcNurse},#{dutyNurse})
        ON DUPLICATE KEY UPDATE qc_nurse=#{qcNurse},duty_nurse=#{dutyNurse}
    </insert>
    <update id="updateDisDateTime">
        update `docus_medicalrecord`.`t_basic`
        set `dis_date`=#{disDate}
        where `patient_id`=#{patientId}
    </update>
    <update id="updIcuRecordState">
        update `docus_medicalrecord`.`t_basic`
        set `is_icu`=#{state}
        where inpatient_no=#{inpatientNo}
            and admiss_times=#{admissTimes}
    </update>
    <update id="updateEmrSubmitState">
        update `docus_medicalrecord`.`t_basic`
        set `emr_submit`=#{emrSubmitState}
        where patient_id=#{patientId}
    </update>
    <update id="updateDisDeptAndWardCode">
        update `docus_medicalrecord`.`t_basic`
        set `dis_dept`=#{deptCode},
         `dis_dept_name`=#{deptCodeName},
         `ward_code`=#{wardCode},
         `ward_name`=#{wardCodeName}
        where patient_id=#{patientId}
    </update>


    <select id="getPatientIdsByInpatientNoAndTimes" resultType="java.lang.String">
        SELECT patient_id
        FROM `docus_medicalrecord`.t_basic tb
        WHERE tb.inpatient_no=#{inpatientNo} and tb.admiss_times=#{admissTimes}
    </select>
    <select id="getDischargeCount" resultType="java.lang.Integer">
        SELECT count(1)
        FROM `docus_medicalrecord`.t_basic tb
        WHERE tb.patient_id=#{patientId}
        and tb.dis_date > '1801-02-03 00:00:00'

    </select>
    <select id="getFileCountByPatientAndSource" resultType="java.lang.Integer">
        SELECT count(1)  FROM  `docus_archivefile`.`t_scan_assort`
        where patient_id=#{patientId} and source=#{source};
    </select>
    <select id="getDischargeDateByInpatientNoAndTimes" resultType="java.lang.String">
        SELECT
            DATE_FORMAT(dis_date,'%Y%m%d') disDate
        FROM
             `docus_medicalrecord`.t_basic tb
        WHERE
            tb.inpatient_no=#{inpatientNo}
            and tb.admiss_times=#{admissTimes}
        LIMIT 1
    </select>
    <select id="isHaveBaby" resultType="java.lang.Integer">
        select
            count(1)
        FROM
            `docus_medicalrecord`.t_basic
        WHERE
            patient_id=#{patientId}
            ${haveBabyQuery}
    </select>
    <select id="getTBasicInfo" resultType="com.docus.server.message.entity.TBasic">
        select
               *
        FROM
            `docus_medicalrecord`.t_basic
        WHERE
            patient_id=#{patientId}
    </select>
    <select id="getSdRyIndex" resultType="java.lang.String">
        select
            sdry_index
        FROM
            `docus_medicalrecord`.t_basic_extend
        WHERE
            patient_id=#{patientId}
    </select>
    <select id="getMiRelationByPatientIndexAndAdmissTimes"
            resultType="com.docus.server.message.entity.MaternalInfantRelationship">
        SELECT
               *
        FROM
             `docus_medicalrecord`.`t_maternal_infant_relationship`
        where
            mom_id=#{parentIndex}
            and parent_times=#{parentTimes}

    </select>
    <select id="getPatientIdBySdRyIndex" resultType="java.lang.String">
        select
            patient_id
        FROM
            `docus_medicalrecord`.t_basic_extend
        WHERE
            sdry_index=#{sdRyIndex}
    </select>
</mapper>
