<?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.record.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="insertTBasic">
        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_name`, `dis_date`, `dis_dept_name`,`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.admissDeptName},#{tBasic.disDate},#{tBasic.disDeptName},
        #{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>

    <update id="updateTBasic">
        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_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.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 `docus_medicalrecord`.`t_basic` where jzh=#{jzh}
    </select>

    <select id="getPatientId" resultType="java.lang.String">
        select patient_id from `docus_medicalrecord`.`t_basic` where jzh=#{jzh}
    </select>
    <select id="selectBasicListByAdmissDate" resultType="com.docus.server.record.common.pojo.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.record.common.pojo.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.record.common.pojo.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="getBasicForZYHs" resultType="map">
        SELECT
        patient_id,
        inpatient_no,
        DATE_FORMAT( admiss_date, '%Y-%m-%d %H:%i:%s' ) admiss_date,
        DATE_FORMAT( dis_date, '%Y-%m-%d %H:%i:%s' ) dis_date,
        jzh
        FROM
        docus_medicalrecord.t_basic
        WHERE
        1 = 1
        <if test="zyhs!= null and zyhs.size()>0">
            AND inpatient_no IN
            <foreach collection="zyhs" item="zyh" open="(" close=")" separator=",">
                #{zyh}
            </foreach>
        </if>
    </select>
    <!--查询基础出院数据增量数据-->
    <select id="getBasicOutData" resultType="java.util.Map">
        SELECT
        patient_id patientid,
        jzh,
        inpatient_no inpatientno,
        admiss_times admisstimes,
        NAME,
        date_format(create_time, '%Y-%m-%d %H:%i:%s') createtime,
        date_format(admiss_date, '%Y-%m-%d %H:%i:%s') admissdate,
        date_format(dis_date, '%Y-%m-%d %H:%i:%s') disdate,
        dis_dept_name disdeptname
        FROM
        docus_medicalrecord.t_basic
        WHERE
        1 = 1
        AND jzh IS NOT NULL
        AND dis_date IS NOT NULL
        AND dis_date != '1801-02-03 00:00:00'
        AND is_cancel = 0
        <if test="param.startTime != null">
            AND dis_date <![CDATA[ >= ]]> #{param.startTime}
        </if>
        <if test="param.endTime != null">
            AND dis_date <![CDATA[ <= ]]> #{param.endTime}
        </if>
        <if test="param.where != null">
            AND #{param.where}
        </if>
        LIMIT #{param.offset}, #{param.pageSize}
    </select>
    <!--查询基础创建数据增量数据-->
    <select id="getBasicData" resultType="java.util.Map">
        SELECT
        patient_id patientid,
        jzh,
        inpatient_no inpatientno,
        admiss_times admisstimes,
        NAME,
        date_format( create_time, '%Y-%m-%d %H:%i:%s' ) createtime,
        date_format( admiss_date, '%Y-%m-%d %H:%i:%s' ) admissdate,
        date_format( dis_date, '%Y-%m-%d %H:%i:%s' ) disdate,
        dis_dept_name disdeptname
        FROM
        docus_medicalrecord.t_basic
        WHERE
        1 = 1
        AND jzh IS NOT NULL
        AND is_cancel =0

        <if test="param.startTime != null">
            AND dis_date <![CDATA[ >= ]]> #{param.startTime}
        </if>
        <if test="param.endTime != null">
            AND dis_date <![CDATA[ <= ]]> #{param.endTime}
        </if>
        <if test="param.where != null">
            AND #{param.where}
        </if>
        LIMIT #{param.offset}, #{param.pageSize}
    </select>
</mapper>
