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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.DoctorAdvice">
        <id column="doctor_advice_id" property="doctorAdviceId"/>
        <result column="hospital_id" property="hospitalId"/>
        <result column="behospital_code" property="behospitalCode"/>
        <result column="order_doctor_name" property="orderDoctorName"/>
        <result column="frequency" property="frequency"/>
        <result column="parent_type_id" property="parentTypeId"/>
        <result column="doctor_advice_type" property="doctorAdviceType"/>
        <result column="usage_num" property="usageNum"/>
        <result column="usage_unit" property="usageUnit"/>
        <result column="dose" property="dose"/>
        <result column="dose_unit" property="doseUnit"/>
        <result column="med_mode_type" property="medModeType"/>
        <result column="da_frequency" property="daFrequency"/>
        <result column="da_deal_type" property="daDealType"/>
        <result column="da_start_date" property="daStartDate"/>
        <result column="da_item_name" property="daItemName"/>
        <result column="da_status" property="daStatus"/>
        <result column="da_stop_date" property="daStopDate"/>
        <result column="da_group_no" property="daGroupNo"/>
        <result column="da_prescription_type" property="daPrescriptionType"/>
        <result column="medicine_type" property="medicineType"/>
        <result column="da_med_type" property="daMedType"/>
        <result column="doctor_notice" property="doctorNotice"/>
        <result column="doctor_id" property="doctorId"/>
        <result column="doctor_name" property="doctorName"/>
        <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="getPage" parameterType="com.diagbot.vo.DoctorAdviceVO" resultType="com.diagbot.dto.DoctorAdviceDTO">
        select b.*
        from(
        SELECT
        t.doctor_advice_id,
        t.hospital_id,
        t.behospital_code,
        t.doctor_advice_type,
        t.da_start_date,
        t.da_stop_date,
        t.da_item_name,
        CONCAT( t.da_item_name, '(',DATE_FORMAT( t.da_start_date,'%Y-%m-%d %H:%i:%s'),')')as info,
        t.da_prescription_type,
        t.medicine_type,
        t.dose,
        t.dose_unit,
        t.med_mode_type,
        t.da_frequency,
        t.doctor_name
        FROM
        `med_doctor_advice` t
        where
        t.is_deleted = 'N'
        <if test="doctorAdviceVO.hospitalId != null">
            and t.hospital_id = #{doctorAdviceVO.hospitalId}
        </if>
        <if test="doctorAdviceVO.medicineType != null and doctorAdviceVO.medicineType != '' and doctorAdviceVO.medicineType == 0"  >
            and (t.medicine_type = "普药" or t.medicine_type = #{doctorAdviceVO.medicineType})
        </if>
        <if test="doctorAdviceVO.medicineType != null and doctorAdviceVO.medicineType != '' and doctorAdviceVO.medicineType == 1 "  >
            and (t.medicine_type = "抗生素" or t.medicine_type = #{doctorAdviceVO.medicineType})
        </if>
        <if test="doctorAdviceVO.medicineType != null and doctorAdviceVO.medicineType != '' and doctorAdviceVO.medicineType == 2 "  >
            and (t.medicine_type = "激素" or t.medicine_type = #{doctorAdviceVO.medicineType})
        </if>
        <if test="doctorAdviceVO.behospitalCode != null and doctorAdviceVO.behospitalCode != ''">
            and t.behospital_code = #{doctorAdviceVO.behospitalCode}
        </if>
        <if test="doctorAdviceVO.doctorAdviceType != null and doctorAdviceVO.doctorAdviceType != ''">
            and t.doctor_advice_type like CONCAT('%',#{doctorAdviceVO.doctorAdviceType},'%')
        </if>
        <if test="doctorAdviceVO.daItemName != null and doctorAdviceVO.daItemName != ''">
            and t.da_item_name like CONCAT('%',#{doctorAdviceVO.daItemName},'%')
        </if>
        <if test="doctorAdviceVO.daStartDateStart != null">
            <![CDATA[ and t.da_start_date >= #{doctorAdviceVO.daStartDateStart}]]>
        </if>
        <if test="doctorAdviceVO.daStartDateEnd != null">
            <![CDATA[ and t.da_start_date < #{doctorAdviceVO.daStartDateEnd}]]>
        </if>) b
        where 1=1
        <if test="doctorAdviceVO.adviceType != null and doctorAdviceVO.adviceType == 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="doctorAdviceVO.adviceType != null and doctorAdviceVO.adviceType == 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_doctor_advice
            <set>
                    order_doctor_name = #{item.orderDoctorName},
                    frequency = #{item.frequency},
                    parent_type_id = #{item.parentTypeId},
                    doctor_advice_type = #{item.doctorAdviceType},
                    usage_num = #{item.usageNum},
                    usage_unit = #{item.usageUnit},
                    dose = #{item.dose},
                    dose_unit = #{item.doseUnit},
                    med_mode_type = #{item.medModeType},
                    da_frequency = #{item.daFrequency},
                    da_deal_type = #{item.daDealType},
                    da_start_date = #{item.daStartDate},
                    da_item_name = #{item.daItemName},
                    da_status = #{item.daStatus},
                    da_stop_date = #{item.daStopDate},
                    da_group_no = #{item.daGroupNo},
                    da_prescription_type = #{item.daPrescriptionType},
                    da_med_type = #{item.daMedType},
                    doctor_notice = #{item.doctorNotice},
                    doctor_id = #{item.doctorId},
                    doctor_name = #{item.doctorName},
                    medicine_type = #{item.medicineType},
                <if test="item.gmtModified != null">
                    gmt_modified = #{item.gmtModified},
                </if>
                <if test="item.modifier != null">
                    modifier = #{item.modifier},
                </if>
            </set>
            where doctor_advice_id = #{item.doctorAdviceId} and hospital_id = #{item.hospitalId} and behospital_code =
            #{item.behospitalCode}
        </foreach>
    </update>



<!--    根据条件获取医嘱-->
    <select id="getDoctorAdviceNum" parameterType="com.diagbot.vo.FilterVO" resultType="java.util.Map">
        SELECT
        a.behospital_code,
        b.da_item_name
        FROM
        (
        SELECT
        a.behospital_code,
        a.hospital_id
        FROM
        med_behospital_info a,
        med_qcresult_info c
        WHERE
        a.is_deleted = 'N'
        AND c.is_deleted = 'N'
        AND a.is_placefile = #{filterVO.isPlacefile}
        <if test="filterVO.startDate != null and  filterVO.endDate != null">
            <![CDATA[ and a.leave_hospital_date >= #{filterVO.startDate}]]>
            <![CDATA[ AND a.leave_hospital_date <= #{filterVO.endDate}]]>
        </if>
        <![CDATA[ and  a.qc_type_id <>'0' ]]>
        AND a.behospital_code = c.behospital_code
        AND a.hospital_id = #{filterVO.hospitalId}
        AND a.hospital_id = c.hospital_id
        ) a,
        med_doctor_advice b
        WHERE
        b.is_deleted = 'N'
        <![CDATA[ and  b.da_status <> '作废' ]]>
        AND a.behospital_code = b.behospital_code
        AND a.hospital_id = b.hospital_id
        <if test="filterVO.flagStr == 1">
            AND b.doctor_advice_type='临时医嘱'
        </if>
        <if test="filterVO.flagStr == 2">
            AND (b.doctor_advice_type='临时医嘱' or b.doctor_advice_type='长期医嘱')
        </if>
    </select>
</mapper>
