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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.MedicalRecord">
        <id column="rec_id" property="recId" />
        <result column="hospital_id" property="hospitalId" />
        <result column="behospital_code" property="behospitalCode" />
        <result column="org_code" property="orgCode" />
        <result column="rec_type_id" property="recTypeId" />
        <result column="mode_id" property="modeId" />
        <result column="rec_date" property="recDate" />
        <result column="rec_title" property="recTitle" />
        <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="getRecordData"  resultType="com.diagbot.dto.RecordContentDTO">
        select t1.hospital_id,t1.rec_id, t1.rec_title, t1.rec_type_id, t1.mode_id , t2.name stand_model_name, t3.content_text, t3.html_text, t3.xml_text
        from med_medical_record t1, qc_mode t2, med_medical_record_content t3
        where
            t1.is_deleted = 'N'
        and t2.is_deleted = 'N'
        and t3.is_deleted = 'N'
        and t1.mode_id = t2.id
        and t1.hospital_id = t3.hospital_id
        and t1.rec_id = t3.rec_id
        and t1.hospital_id = #{hospitalId}
        and t1.behospital_code = #{behospitalCode}
        and t1.mode_id = #{modeId}
        and t3.xml_text != ''
    </select>

    <select id="getRecordContent"  resultType="com.diagbot.dto.RecordContentDTO">
        select t1.hospital_id,t1.rec_id, t1.rec_date, t1.rec_title, t1.rec_type_id, t1.mode_id , t2.name stand_model_name, t3.content_text, t3.html_text, t3.xml_text
        from med_medical_record t1, qc_mode t2, med_medical_record_content t3
        where
            t1.is_deleted = 'N'
        and t2.is_deleted = 'N'
        and t3.is_deleted = 'N'
        and t1.mode_id = t2.id
        and t1.hospital_id = t3.hospital_id
        and t1.rec_id = t3.rec_id
        and t1.hospital_id = #{hospitalId}
        and t1.behospital_code = #{behospitalCode}
    </select>

    <select id="getRecordCount"  resultType="java.lang.Integer">
        select count(*) as num from
          (select  DISTINCT behospital_code,mode_id  from med_medical_record where behospital_code in
                (select  behospital_code from med_behospital_info   where hospital_id = #{hospitalId}
                <if test="isPlacefile !=null and isPlacefile !=''">
                    and is_placefile  =#{isPlacefile}
                </if>
                <if test="startTime !=null and endTime !=null">
                    and placefile_date BETWEEN #{startTime} AND #{endTime}
                </if>
                )
                <if test="modeList != null and modeList.size() > 0">
                    and mode_id in
                    <foreach collection="modeList" open="(" separator="," close=")" item="item">
                        '${item}'
                    </foreach>
                </if>
                <if test="start != null and end !=null">
                    limit #{start},#{end}
                </if>
        ) a
    </select>

    <select id="getMedicalRecord"  resultType="com.diagbot.dto.MedicalRecordDTO">
        SELECT DISTINCT
        a.behospital_code behospitalCode,
        a.hospital_id hospitalId,
        c.mode_id
        from
        med_behospital_info a LEFT JOIN med_behospital_type b
        on a.behospital_code = b.behospital_code and a.hospital_id = b.hospital_id and a.is_deleted = 'N' and b.is_deleted = 'N'
        LEFT JOIN med_medical_record c
        on a.behospital_code = c.behospital_code and a.hospital_id = c.hospital_id and a.is_deleted = 'N' and c.is_deleted = 'N'
        where  b.`value` is null
        <if test="modeList != null and modeList.size() > 0">
            and c.mode_id in
            <foreach collection="modeList" open="(" separator="," close=")" item="item">
                '${item}'
            </foreach>
        </if>
    </select>

    <update id="updateBatchByKey">
        <foreach collection="list" item="item"  separator=";">
            update med_medical_record
            <set>
                    org_code = #{item.orgCode},
                    rec_type_id = #{item.recTypeId},
                    rec_date = #{item.recDate},
                    rec_title = #{item.recTitle},
                <if test="item.gmtModified != null">
                    gmt_modified = #{item.gmtModified},
                </if>
                <if test="item.modifier != null">
                    modifier = #{item.modifier},
                </if>
            </set>
            where rec_id = #{item.recId} and hospital_id = #{item.hospitalId} and behospital_code = #{item.behospitalCode}
        </foreach>
    </update>

</mapper>
