<?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.BatchApplyDetailsMapper">

    <insert id="addBatchApplyDetails" parameterType="java.util.List">
        insert into batch_apply_details (batch_id,apply_name,apply_time,apply_reason,apply_flag,
        apply_date,apply_statc,dis_time,effective_day,patient_name,inpatient_no,admiss_times,handling_name,handling_name_string
        )
        values
        <foreach collection="list" item="item" separator=",">(#{item.batchId},#{item.applyName},#{item.applyTime},
            #{item.applyReason},#{item.applyFlag},#{item.applyDate},#{item.applyStatc},
            #{item.disTime},#{item.effectiveDay},#{item.patientName},#{item.inpatientNo},#{item.admissTimes},#{item.handlingName},#{item.handlingNameString}
            )
        </foreach>
    </insert>
    <update id="upadteApplyDetails">
        update batch_apply_details
        <set>
            <if test="effectiveTime != null">
                effective_time = #{effectiveTime},
            </if>
            <if test="handlingName != null">
                handling_name = #{handlingName},
            </if>
            <if test="examineName != null">
                examine_name = #{examineName},
            </if>
            <if test="examineTime != null">
                examine_time = #{examineTime},
            </if>
            <if test="applyStatc != null">
                apply_statc=#{applyStatc},
            </if>
            <if test="examineNameString != null">
                examine_name_string=#{examineNameString},
            </if>
        </set>
        where id = #{id}
    </update>
    <update id="upadteApplyDetailsList">
        <foreach collection="list" item="item" index="index" separator=";">
            UPDATE batch_apply_details
            <set>
                <if test="item.effectiveTime != null and item.effectiveTime != ''" >
                    effective_time = #{item.effectiveTime},
                </if>
                <if test="item.handlingName != null and item.handlingName != ''" >
                    handling_name = #{item.handlingName},
                </if>
                <if test="item.examineName != null and item.examineName != ''" >
                    examine_name = #{item.examineName},
                </if>
                <if test="item.examineTime != null and item.examineTime != ''" >
                    examine_time = #{item.examineTime},
                </if>
                <if test="item.applyStatc != null and item.applyStatc != ''" >
                    apply_statc = #{item.applyStatc},
                </if>
            </set>
            where id = #{item.id}
        </foreach>
    </update>
    <update id="updateBatchApplyDetails">
        update batch_apply_details set apply_statc=5,return_name=#{barCodeName},return_time=#{returnTime},return_name_string=#{userName}
        where id=#{id}
    </update>


    <delete id="deleteByBatchId">
        delete from batch_apply_details where  batch_id=#{batchId}
    </delete>
    <delete id="deleteById">
        delete from batch_apply_details where  id=#{id}
    </delete>


    <select id="getEffectiveTime" resultType="java.lang.String">
        SELECT MAX(b.date) effectiveTime
        FROM (SELECT TOP(SELECT effective_day FROM batch_apply_details WHERE id = #{ids} ) DATE
              FROM emr_holiday_set
              WHERE DATE > #{examineTimes} AND flag = 1) b
    </select>
    <select id="getBatchApplyDetails" resultType="com.emr.entity.BatchApplyDetails">
        select * from batch_apply_details
        <where> 1=1
            <if test="batchId !=null and batchId != ''">
                and   batch_id=#{batchId}
            </if>
            <if test="inpatientNo != null and inpatientNo != ''">
                and  inpatient_no=#{inpatientNo}
            </if>
        </where>
    </select>
    <select id="getNumInpNoAndAdmissTimes" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM batch_apply_details WHERE apply_statc !=3 AND effective_time>convert(varchar(10),getdate()-1,120)
        AND inpatient_no=#{inpatientNo} AND admiss_times=#{admissTimes} AND  batch_id=#{batchId}
    </select>
    <select id="getIdInpNoAndAdmissTimes" resultType="java.lang.String">
        SELECT id FROM batch_apply_details WHERE apply_statc !=3 and apply_statc !=2 and apply_statc!=5
        AND inpatient_no=#{inpatientNo} AND admiss_times=#{admissTimes} AND  batch_id=#{batchId}
    </select>

</mapper>