<?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.diagbot.mapper.MedLisResultMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.MedLisResult">
        <result column="rep_no" property="repNo"/>
        <result column="hospital_id" property="hospitalId"/>
        <result column="behospital_code" property="behospitalCode"/>
        <result column="item_code" property="itemCode"/>
        <result column="item_name" property="itemName"/>
        <result column="result" property="result"/>
        <result column="reference" property="reference"/>
        <result column="reference_min" property="referenceMin"/>
        <result column="reference_max" property="referenceMax"/>
        <result column="unit" property="unit"/>
        <result column="abnormal" property="abnormal"/>
        <result column="color" property="color"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="creator" property="creator"/>
        <result column="modifier" property="modifier"/>
    </resultMap>
    <select id="getMedLisResults" parameterType="com.diagbot.entity.BehospitalInfo"
            resultType="com.diagbot.dto.MedLisResultDTO">
        SELECT
        t1.rep_no,
        t1.rep_name as repName,
        t2.behospital_code,
        t2.hospital_id,
        t2.item_code,
        t2.item_name as reportName,
        t2.result,
        t2.reference,
        t2.reference_max,
        t2.reference_min,
        t2.unit,
        t2.abnormal,
        t2.color,
        t2.is_deleted,
        date_format (t1.check_date,'%Y-%m-%d %H:%i:%s') as gmt_create,
        t2.gmt_modified,
        t2.creator,
        t2.modifier
        FROM
        med_lis_info t1,
        med_lis_result t2
        WHERE
        t1.is_deleted = "N"
        and t2.is_deleted = "N"
        AND t1.rep_name IS NOT NULL
        AND t1.check_date IS NOT NULL
        AND t2.item_name IS NOT NULL
        and t1.hospital_id = t2.hospital_id
        and t1.behospital_code = t2.behospital_code
        <if test="fileCode != null and fileCode != ''">
            and t1.behospital_code like CONCAT('%',#{fileCode},'%')
        </if>
        <if test="hospitalId != null and hospitalId != ''">
            and t1.hospital_id = #{hospitalId}
        </if>
        and t1.rep_no = t2.rep_no
        <if test="isPlacefile != null and isPlacefile == 0">
            <if test="behospitalDate != null">
                <![CDATA[ AND date_format (t1.check_date,'%Y-%m-%d %H:%i:%s') >= #{behospitalDate}]]>
            </if>
            <if test="leaveHospitalDate != null">
                <![CDATA[AND date_format (t1.check_date,'%Y-%m-%d %H:%i:%s') <= #{leaveHospitalDate}]]>
            </if>
        </if>
        <if test="isPlacefile != null and isPlacefile == 1">
            <if test="leaveHospitalDate != null">
                <![CDATA[AND date_format (t1.check_date,'%Y-%m-%d %H:%i:%s') <= #{leaveHospitalDate}]]>
            </if>
            <if test="behospitalDate != null">
                <![CDATA[AND date_format (t1.check_date,'%Y-%m-%d %H:%i:%s') >= #{behospitalDate}]]>
            </if>
        </if>

    </select>

    <update id="updateBatchByKey">
        <foreach collection="list" item="item"  separator=";">
            update med_lis_result
            <set>
                behospital_code = #{item.behospitalCode},
                item_code = #{item.itemCode},
                item_name = #{item.itemName},
                result = #{item.result},
                reference_min = #{item.referenceMin},
                reference_max = #{item.referenceMax},
                unit = #{item.unit},
                abnormal = #{item.abnormal},
                color = #{item.color},
                <if test="item.gmtModified != null">
                    gmt_modified = #{item.gmtModified},
                </if>
                <if test="item.modifier != null">
                    modifier = #{item.modifier},
                </if>
            </set>
            where rep_no = #{item.repNo} and hospital_id = #{item.hospitalId}
        </foreach>
    </update>

</mapper>
