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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.MedPacsInfo">
        <id column="rep_no" property="repNo"/>
        <result column="hospital_id" property="hospitalId"/>
        <result column="behospital_code" property="behospitalCode"/>
        <result column="rep_name" property="repName"/>
        <result column="rep_type" property="repType"/>
        <result column="rep_type_name" property="repTypeName"/>
        <result column="part" property="part"/>
        <result column="rep_date" property="repDate"/>
        <result column="check_date" property="checkDate"/>
        <result column="apply_doctor" property="applyDoctor"/>
        <result column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <result column="register_flag" property="registerFlag"/>
        <result column="report_doctor" property="reportDoctor"/>
        <result column="checker" property="checker"/>
        <result column="yjpdz" property="yjpdz"/>
        <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="getTime" resultType="java.util.HashMap">
        select
        t.behospital_date,
        t.leave_hospital_date
        from med_behospital_info t
        where
        t.is_deleted = 'N'
        <if test="behospitalCode != null">
            and t.behospital_code = #{behospitalCode}
        </if>
    </select>

    <select id="getCheckInfo" resultType="com.diagbot.dto.CheckInfoDTO">
        select DISTINCT b.*
        from(
        SELECT
        t1.rep_name as checkItemName,
        t1.rep_type_name as checkItemType,
        CONCAT( t1.rep_name, '(',DATE_FORMAT( t1.check_date,'%Y-%m-%d %H:%i:%s'),')')as info,
        date_format (t1.rep_date,'%Y-%m-%d %H:%i:%s') as reptTime,
        date_format (t1.check_date,'%Y-%m-%d %H:%i:%s') as checkTime,
        t1.apply_doctor as doctorName,
        t2.hospital_id as hospitalId,
        t2.behospital_code as behospitalCode,
        t2.rept_diag as checkResult
        FROM
        med_pacs_info t1
        left join
        med_pacs_result t2
        on t1.hospital_id = t2.hospital_id
        AND t1.is_deleted = "N"
        AND t2.is_deleted = "N"
        AND t1.rep_name IS NOT NULL
        AND t1.check_date IS NOT NULL
        AND t1.behospital_code = t2.behospital_code
        AND t1.rep_no = t2.rep_no
        WHERE
        1=1
        <if test="checkInfoVO.hospitalId != null">
            and t1.hospital_id = #{checkInfoVO.hospitalId}
        </if>
        <if test="checkInfoVO.behospitalCode != null and checkInfoVO.behospitalCode != ''">
            and t1.behospital_code = #{checkInfoVO.behospitalCode}
        </if>
        <if test="checkInfoVO.checkItemType != null and checkInfoVO.checkItemType != ''">
            and t1.rep_type_name like CONCAT('%',#{checkInfoVO.checkItemType},'%')
        </if>
        <if test="checkInfoVO.checkItemName != null and checkInfoVO.checkItemName != ''">
            and t1.rep_name like CONCAT('%',#{checkInfoVO.checkItemName},'%')
        </if>
        <if test="checkInfoVO.isPlacefile != null and checkInfoVO.isPlacefile == 1">
            <if test="checkInfoVO.leaveHospitalDate != null and checkInfoVO.behospitalDate != null">
                <![CDATA[ AND date_format (t1.check_date,'%Y-%m-%d %H:%i:%s') <= #{checkInfoVO.leaveHospitalDate}]]>
                <![CDATA[ AND date_format (t1.check_date,'%Y-%m-%d %H:%i:%s') >= #{checkInfoVO.behospitalDate}]]>
            </if>
        </if>
        <if test="checkInfoVO.isPlacefile != null and checkInfoVO.isPlacefile == 0">
            <if test="checkInfoVO.leaveHospitalDate != null">
                <![CDATA[ AND date_format (t1.check_date,'%Y-%m-%d %H:%i:%s') <= #{checkInfoVO.leaveHospitalDate}]]>
            </if>
            <if test="checkInfoVO.behospitalDate != null">
                <![CDATA[ AND date_format (t1.check_date,'%Y-%m-%d %H:%i:%s') >= #{checkInfoVO.behospitalDate}]]>
            </if>
        </if>
        ) b
        where 1=1
        <if test="checkInfoVO.jxDateStart != null">
            <![CDATA[ AND b.checkTime >= #{checkInfoVO.jxDateStart}]]>
        </if>
        <if test="checkInfoVO.jxDateEnd != null">
            <![CDATA[ AND b.checkTime <= #{checkInfoVO.jxDateEnd}]]>
        </if>
        <if test="checkInfoVO.checkType != null and checkInfoVO.checkType == 1">
            <if test="infos != null and infos.size() > 0">
                and b.info in
                <foreach collection="infos" open="(" separator="," close=")" item="item">
                    '${item}'
                </foreach>
            </if>
            <if test="infos = null || infos.size() == 0">
                and 1 = 2
            </if>
        </if>
        <if test="checkInfoVO.checkType != null and checkInfoVO.checkType == 2 ">
            <if test="infos != null and infos.size() > 0">
                and b.info not in
                <foreach collection="infos" open="(" separator="," close=")" item="item">
                    '${item}'
                </foreach>
            </if>
            <if test="infos = null || infos.size() == 0">
                and 1 = 1
            </if>
        </if>
    </select>

    <!--    获取质控提示信息-->
    <select id="getInfo" resultType="java.lang.String">
        select b.info
        from
        med_behospital_info a,
        med_qcresult_detail b
        where
        a.is_deleted = 'N'
        and b.is_deleted = 'N'
        and a.hospital_id = b.hospital_id
        and a.behospital_code = b.behospital_code
        <if test="hospitalId != null">
            and a.hospital_id = #{hospitalId}
        </if>
        <if test="behospitalCode != null and behospitalCode != ''">
            and a.behospital_code = #{behospitalCode}
        </if>
    </select>

    <update id="updateBatchByKey">
        <foreach collection="list" item="item"  separator=";">
            update med_pacs_info
            <set>
                behospital_code = #{item.behospitalCode},
                rep_name= #{item.repName},
                rep_type= #{item.repType},
                rep_type_name= #{item.repTypeName},
                part= #{item.part},
                rep_date= #{item.repDate},
                check_date= #{item.checkDate},
                apply_doctor= #{item.applyDoctor},
                dept_id= #{item.deptId},
                dept_name= #{item.deptName},
                register_flag= #{item.registerFlag},
                report_doctor= #{item.reportDoctor},
                checker= #{item.checker},
                yjpdz= #{item.yjpdz},
                <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>
