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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.MedPacsResult">
        <result column="rep_no" property="repNo"/>
        <result column="hospital_id" property="hospitalId"/>
        <result column="behospital_code" property="behospitalCode"/>
        <result column="rept_name" property="reptName"/>
        <result column="part" property="part"/>
        <result column="clinic_sight" property="clinicSight"/>
        <result column="rept_diag" property="reptDiag"/>
        <result column="remark" property="remark"/>
        <result column="clinic_diag" property="clinicDiag"/>
        <result column="apply_doctor" property="applyDoctor"/>
        <result column="checker_doctor" property="checkerDoctor"/>
        <result column="report_doctor" property="reportDoctor"/>
        <result column="dept_name" property="deptName"/>
        <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="getMedPacsResults" parameterType="com.diagbot.entity.BehospitalInfo"
            resultType="com.diagbot.dto.MedPacsResultDTO">
        SELECT
        t2.rep_no repNo,
        t1.rep_name as reptName,
        t2.dept_name,
        t1.part,
        t2.hospital_id,
        t2.behospital_code,
        t2.clinic_diag,
        t2.apply_doctor,
        t2.clinic_sight,
        t2.rept_diag,
        t2.remark,
        t2.report_doctor,
        t2.checker_doctor,
        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_pacs_info t1,
        med_pacs_result t2
        WHERE
        t1.is_deleted = "N"
        AND t2.is_deleted = "N"
        AND t1.check_date 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_pacs_result
            <set>
                behospital_code = #{item.behospitalCode},
                rept_name = #{item.reptName},
                part = #{item.part},
                clinic_sight = #{item.clinicSight},
                rept_diag = #{item.reptDiag},
                remark = #{item.remark},
                clinic_diag = #{item.clinicDiag},
                apply_doctor = #{item.applyDoctor},
                checker_doctor = #{item.checkerDoctor},
                report_doctor = #{item.reportDoctor},
                dept_name = #{item.deptName},
                <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>
