<?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.emr.dao.BatchApplyMapper">
    <insert id="insertBatch">
        insert into batch_apply
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="batchName != null" >
                batch_name,
            </if>
            <if test="borrowingNum != null" >
                borrowing_num,
            </if>
            <if test="statc != null" >
                statc,
            </if>
            <if test="readNum != null" >
                read_num,
            </if>
            <if test="createName != null" >
                create_name,
            </if>
            <if test="createDate != null" >
                create_date,
            </if>
            <if test="applyName != null" >
                apply_name,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="batchName != null" >
                #{batchName},
            </if>
            <if test="borrowingNum != null" >
                #{borrowingNum},
            </if>
            <if test="statc != null" >
                #{statc},
            </if>
            <if test="readNum != null" >
                #{readNum},
            </if>
            <if test="createName != null" >
                #{createName},
            </if>
            <if test="createDate != null" >
                #{createDate},
            </if>
            <if test="applyName != null" >
                #{applyName},
            </if>
        </trim>
    </insert>
    <update id="updateBatch">
        update batch_apply set batch_name=#{batchName} where id=#{id}
    </update>
    <update id="updateBorrowingNum">
        update batch_apply set borrowing_num=#{borrowingNum} where id=#{batchId}
    </update>
    <update id="updateReadNumm">
        update batch_apply set read_num=#{readNum} where id=#{batchId}
    </update>
    <update id="updateStatc">
        update batch_apply set statc=#{statc} where id=#{batchId}
    </update>
    <delete id="deleteById">
        DELETE FROM batch_apply WHERE id =#{id}
    </delete>


    <select id="selectBatchApply" resultType="com.emr.entity.BatchApply">
        select * ,
        (SELECT COUNT(id)FROM batch_apply_details WHERE apply_statc in(1,5)  AND batch_id=batch_apply.id) readNums
        from  batch_apply
        <where> 1=1
            <if test="batchName != null and batchName != ''">
                and  batch_name=#{batchName}
            </if>
            <if test="createName != null and createName != ''">
                AND create_name=#{createName}
            </if>
            <if test="startTime1 != null and startTime1 != ''">
                AND create_date > #{startTime1}
            </if>
            <if test="endTime1 != null and endTime1 != ''">
                AND create_date &lt; #{endTime1}
            </if>
        </where>
        order by create_date DESC
    </select>
    <select id="getCommomVo" resultType="com.emr.vo.commomSearch.CommomVo">
        select patient_id,inpatient_no,admiss_times,dis_dept,dis_date,name from commomtable
        <where> 1=1
            <if test="inpatientNo != null and inpatientNo != ''">
                and  inpatient_no=#{inpatientNo}
            </if>
            <if test="admissTimes != null and admissTimes != ''">
                and  admiss_times=#{admissTimes}
            </if>
            <if test="disDept != null and disDept != ''">
                and  dis_dept=#{disDept}
            </if>
            <if test="startTime != null and startTime != ''">
                AND dis_date > #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND dis_date &lt; #{endTime}
            </if>
        </where>
    </select>
    <select id="getBorrowingNum" resultType="com.emr.vo.BatchApplyVo">
        select borrowing_num from batch_apply where id=#{batchId}
    </select>
    <select id="getCommomVoByPatientIds" resultType="com.emr.vo.commomSearch.CommomVo">
        select patient_id,inpatient_no,admiss_times,dis_dept,dis_date,name from commomtable
        WHERE
        patient_id in
        <foreach item="item" collection="patientIds.split(',')" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="getreadNum" resultType="com.emr.vo.BatchApplyVo">
        select borrowing_num,read_num from batch_apply where id=#{batchId}
    </select>

</mapper>