<?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.lantone.daqe.mapper.ColumnResultMapper">

    <select id="getColumnResultNumber" resultType="com.lantone.daqe.dto.GetColumnResultNumberDTO">
        SELECT
        sum(CASE dcr.is_solved WHEN 1 THEN 1 ELSE 0 END) resolved,
        sum(CASE dcr.is_solved WHEN 0 THEN 1 ELSE 0 END) notSolved,
        sum(CASE dcr.is_solved WHEN 1 THEN (CASE dcr.type WHEN 1 THEN 1 ELSE 0 END) ELSE 0 END) resolvedNonnull,
        sum(CASE dcr.is_solved WHEN 1 THEN (CASE dcr.type WHEN 2 THEN 1 ELSE 0 END) ELSE 0 END) resolvedStandardvalue,
        sum(CASE dcr.is_solved WHEN 1 THEN (CASE dcr.type WHEN 3 THEN 1 ELSE 0 END) ELSE 0 END) resolvedRegular,
        sum(CASE dcr.is_solved WHEN 0 THEN (CASE dcr.type WHEN 1 THEN 1 ELSE 0 END) ELSE 0 END) notSolvedNonnull,
        sum(CASE dcr.is_solved WHEN 0 THEN (CASE dcr.type WHEN 2 THEN 1 ELSE 0 END) ELSE 0 END) notSolvedStandardvalue,
        sum(CASE dcr.is_solved WHEN 0 THEN (CASE dcr.type WHEN 3 THEN 1 ELSE 0 END) ELSE 0 END) notSolvedRegular
        FROM
        dc_column_result dcr
        WHERE
        dcr.is_deleted = 'N'
        and hospital_id=#{getColumnResultNumberVO.hospitalId}
        <if test="getColumnResultNumberVO.behospitalStartDate != null ">
            <![CDATA[ AND behospital_date >= #{getColumnResultNumberVO.behospitalStartDate}]]>
        </if>
        <if test="getColumnResultNumberVO.behospitalEndDate != null ">
            <![CDATA[ AND behospital_date <= #{getColumnResultNumberVO.behospitalEndDate}]]>
        </if>

    </select>


    <select id="getSpecialTableColumnVal" resultType="com.lantone.daqe.dto.GetTableColumnInfoDTO">
        SELECT
        a.behospital_code,
        a.hospital_id,
        sdd.id tableId,
        a.behospital_date,
        sdd.${getTableColumnInfoVO.columnEname} val,
        sdd.${getTableColumnInfoVO.numberColumnCname} numberId
        FROM
        (
        SELECT
        behospital_code,
        hospital_id,
        behospital_date
        FROM
        med_behospital_info
        WHERE
        is_deleted = 'N'
        <if test="getTableColumnInfoVO.hospitalId != null and getTableColumnInfoVO.hospitalId != ''">
        AND hospital_id = #{getTableColumnInfoVO.hospitalId}
        </if>
        AND behospital_code in
        <foreach collection="getTableColumnInfoVO.behospitalCodeList" item="behospitalCode" open="(" separator=","
                 close=")">
            #{behospitalCode}
        </foreach>
        ) a,
        ${getTableColumnInfoVO.tableEname} sdd
        where
        sdd.behospital_code = a.behospital_code
        AND sdd.is_deleted = 'N'
        AND a.hospital_id = sdd.hospital_id
    </select>


    <select id="getRecSpecialTableColumnVal" resultType="com.lantone.daqe.dto.GetTableColumnInfoDTO">
        SELECT
        a.behospital_code,
        a.hospital_id,
        sdd.id tableId,
        a.behospital_date,
        mmr.rec_id,
        mmr.rec_title,
        sdd.${getTableColumnInfoVO.columnEname} val,
        sdd.${getTableColumnInfoVO.numberColumnCname} numberId
        FROM
        (
        SELECT
        behospital_code,
        hospital_id,
        behospital_date
        FROM
        med_behospital_info
        WHERE
        is_deleted = 'N'
        <if test="getTableColumnInfoVO.hospitalId != null and getTableColumnInfoVO.hospitalId != ''">
            AND hospital_id = #{getTableColumnInfoVO.hospitalId}
        </if>
        AND behospital_code in
        <foreach collection="getTableColumnInfoVO.behospitalCodeList" item="behospitalCode" open="(" separator=","
                 close=")">
            #{behospitalCode}
        </foreach>
        ) a,
        med_medical_record mmr ,
        ${getTableColumnInfoVO.tableEname} sdd
        where
        a.behospital_code = mmr.behospital_code
        and sdd.behospital_code = a.behospital_code
        and sdd.rec_id=mmr.rec_id
        AND sdd.is_deleted = 'N'
        AND mmr.is_deleted = 'N'
        AND a.hospital_id = sdd.hospital_id
    </select>
</mapper>