<?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.collection.mapper.TBasicMapper">

    <select id="getPatientIdByInpatientNoAndAdminssTimes" resultType="java.lang.String">
        select `patient_id`
        from `docus_medicalrecord`.`t_basic`
        where `inpatient_no` = #{inpatientNo}
          and `admiss_times` = #{adminssTimes}
    </select>



    <insert id="insert">
        INSERT INTO `docus_medicalrecord`.`t_basic`
        (`admiss_days`,`is_dead`,`sex_name`,`bed_num`,`age`,`sex`,`id_card`,`dis_dept`,`patient_id`, `admiss_times`, `inpatient_no`,`name`,  `admiss_date`,`admiss_dept`,  `admiss_dept_name`, `dis_date`, `dis_dept_name`,`attending`,`attending_name`,`jzh`,`create_time`)
        VALUES
        (#{tBasic.admissDays},#{tBasic.isDead},#{tBasic.sexName},#{tBasic.bedNum},#{tBasic.age},#{tBasic.sex},#{tBasic.idCard},
        #{tBasic.disDept},#{tBasic.patientId},#{tBasic.admissTimes},#{tBasic.inpatientNo},
        #{tBasic.name},#{tBasic.admissDate},#{tBasic.admissDept},#{tBasic.admissDeptName},#{tBasic.disDate},#{tBasic.disDeptName},
        #{tBasic.attending},#{tBasic.attendingName},#{tBasic.jzh},now())
    </insert>
    <insert id="insertExtend">
        INSERT INTO `docus_medicalrecord`.`t_basic_extend`
        (`patient_id`,`ward_code`,`ward_name`,`sdry_index`)
        VALUES
        (#{tBasicExtend.patientId},#{tBasicExtend.wardCode},#{tBasicExtend.wardName},#{tBasicExtend.sdryIndex})
    </insert>
    <insert id="saveNisRemoveFilesCount">
        INSERT INTO `docus_medicalrecord`.`t_basic_extend`
            (`patient_id`, `nurse_file_collect_remove_count`)
        VALUES (#{patientId}, #{count})
        on DUPLICATE KEY UPDATE
            `nurse_file_collect_remove_count`=#{count};
    </insert>

    <update id="update">
        UPDATE `docus_medicalrecord`.`t_basic`
        <set>
            <if test="tBasic.admissDays !=null ">
                `admiss_days`=#{tBasic.admissDays},
            </if>
            <if test="tBasic.isDead !=null ">
                `is_dead`=#{tBasic.isDead},
            </if>
            <if test="tBasic.sexName !=null ">
                `sex_name`=#{tBasic.sexName},
            </if>
            <if test="tBasic.bedNum !=null ">
                `bed_num`=#{tBasic.bedNum},
            </if>
            <if test="tBasic.age !=null ">
                `age`=#{tBasic.age},
            </if>
            <if test="tBasic.sex !=null ">
                `sex`=#{tBasic.sex},
            </if>
            <if test="tBasic.idCard !=null ">
                `id_card`=#{tBasic.idCard},
            </if>
            <if test="tBasic.disDept !=null ">
                `dis_dept`=#{tBasic.disDept},
            </if>
            <if test="tBasic.admissTimes !=null ">
                `admiss_times`=#{tBasic.admissTimes},
            </if>
            <if test="tBasic.inpatientNo !=null ">
                `inpatient_no`=#{tBasic.inpatientNo},
            </if>
            <if test="tBasic.name !=null ">
                `name`=#{tBasic.name},
            </if>
            <if test="tBasic.admissDate !=null ">
                `admiss_date`=#{tBasic.admissDate},
            </if>
            <if test="tBasic.admissDeptName !=null ">
                `admiss_dept`=#{tBasic.admissDept},
            </if>
            <if test="tBasic.admissDeptName !=null ">
                `admiss_dept_name`=#{tBasic.admissDeptName},
            </if>
            <if test="tBasic.disDate !=null ">
                `dis_date`=#{tBasic.disDate},
            </if>
            <if test="tBasic.disDeptName !=null ">
                `dis_dept_name`=#{tBasic.disDeptName},
            </if>
            <if test="tBasic.attending !=null ">
                `attending`=#{tBasic.attending},
            </if>
            <if test="tBasic.attendingName !=null ">
                `attending_name`=#{tBasic.attendingName},
            </if>
            `update_time`=now()
        </set>
        WHERE `patient_id`=#{tBasic.patientId,jdbcType=VARCHAR};
    </update>

    <update id="updateExtend">
        INSERT INTO `docus_medicalrecord`.`t_basic_extend`
        (`patient_id`,`ward_code`,`ward_name`,`sdry_index`)
        VALUES
        (#{tBasicExtend.patientId},#{tBasicExtend.wardCode},#{tBasicExtend.wardName},#{tBasicExtend.sdryIndex})
        on DUPLICATE KEY UPDATE
            `ward_code`=#{tBasicExtend.wardCode},
            `ward_name`=#{tBasicExtend.wardName},
            `sdry_index`=#{tBasicExtend.sdryIndex}
    </update>

    <select id="selectOne" resultType="java.lang.Integer">
        select count(patient_id) from t_basic where jzh=#{jzh}
    </select>

    <select id="getPatientId" resultType="java.lang.String">
        select patient_id from t_basic where jzh=#{jzh}
    </select>
    <select id="selectBasicListByAdmissDate" resultType="com.docus.server.collection.entity.TBasic">
        select *
        from `docus_medicalrecord`.`t_basic`
        where admiss_date between #{admissStartDate} AND #{admissEndDate}
        limit #{offset}
            , #{size}
    </select>
    <select id="getSdRyIndexByPatientId" resultType="java.lang.String">
        select sdry_index
        from `docus_medicalrecord`.`t_basic_extend`
        where patient_id=#{patientId}
    </select>
    <select id="selectBasicListByCreateOrUpdateTime" resultType="com.docus.server.collection.entity.TBasic">
        select `patient_id`,
               `admiss_times`,
               `inpatient_no`,
               `admiss_id`,
               `ph`,
               `sex`,
               `age`,
               `id_card`,
               `admiss_date`,
               `dis_date`,
               `admiss_days`,
               `create_time`,
               `update_time`,
               `file_source`,
               `jzh`
        from `docus_medicalrecord`.`t_basic`
        where (`create_time` between #{startDate} AND #{endDate}
        or `update_time` between #{startDate} AND #{endDate})
        and `dis_date` > '1801-02-03 00:00:00'
            limit #{offset}
            , #{size}
    </select>
    <select id="selectBasicListByPatientIds" resultType="com.docus.server.collection.entity.TBasic">
        select `patient_id`,
               `admiss_times`,
               `inpatient_no`,
               `admiss_id`,
               `ph`,
               `sex`,
               `age`,
               `id_card`,
               `admiss_date`,
               `dis_date`,
               `admiss_days`,
               `create_time`,
               `update_time`,
               `file_source`,
               `jzh`
        from `docus_medicalrecord`.`t_basic`
        where patient_id in <foreach collection="patientIds" item="patientId" open="(" close=")" separator=",">
        #{patientId}
    </foreach>
    </select>
    <select id="getNurseFileCount" resultType="java.lang.Integer">
        select nurse_file_count
        from `docus_medicalrecord`.`t_basic_extend`
        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="getParentSdRyIndex" resultType="java.lang.String">
        SELECT mom_id
        FROM `docus_medicalrecord`.`t_maternal_infant_relationship`
        where baby_id=#{babyIndex}
    </select>
    <select id="getPatientIdBySdRyIndex" resultType="java.lang.String">
        select patient_id
        from `docus_medicalrecord`.`t_basic_extend`
        WHERE sdry_index=#{sdRyIndex}
    </select>
    <select id="getByPatientId" resultType="com.docus.server.collection.entity.TBasic">
        select *
        from `docus_medicalrecord`.`t_basic`
        where patient_id=#{patientId}
    </select>
</mapper>
