<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "http://mybatis.org/dtd/mybatis-3-mapper.dtd" "mybatis-3-mapper.dtd" >
<mapper namespace="com.reyun.saas.report.data.dao.WarningReportMapper">
    <sql id="warningDataSQL">
        distinct_count_num as volume_dose,
        case WHEN quantity = '1' then distinct_count_num else 0 end volume_dose_normal,
        case WHEN quantity = '2' then distinct_count_num else 0 end volume_dose_exception,
        <if test="os==2">
            case WHEN quantity = '5' then distinct_count_num else 0 end  volume_dose_potential_risk,
        </if>
        case WHEN device_status = '1' then distinct_count_num else 0 end added,
        case WHEN device_status = '1' and quantity = '1' then distinct_count_num else 0 end added_normal,
        case WHEN device_status = '1' and quantity = '2' then distinct_count_num else 0 end added_exception,
        case WHEN device_status = '2'  then distinct_count_num else 0 end dau,
        case WHEN device_status = '2' and quantity = '1' then distinct_count_num else 0 end dau_normal,
        case WHEN device_status = '2' and quantity = '2' then distinct_count_num else 0 end dau_exception,
        case WHEN appIsUpdate = '1' then distinct_count_num else 0 end `updates`,
        case WHEN appIsUpdate = '1' and quantity = '1' then distinct_count_num else 0 end update_normal,
        case WHEN appIsUpdate = '1' and quantity = '2' then distinct_count_num else 0 end update_exception
    </sql>



    <sql id="warningSumSQL">
        IFNULL(sum(volume_dose),0) as volume_dose,
        IFNULL(sum(volume_dose_normal),0) as volume_dose_normal,
        case WHEN sum(volume_dose) = 0 or sum(volume_dose) is null then 0 else (IFNULL(sum(volume_dose_normal),0) / sum(volume_dose) * 100.0) end rate_volume_dose_normal,
        IFNULL(sum(volume_dose_exception),0) as volume_dose_exception,
        case WHEN sum(volume_dose) = 0 then 0 else (IFNULL(sum(volume_dose_exception),0) / sum(volume_dose) * 100.0) end rate_volume_dose_exception,
        <if test="os==2">
            (sum(volume_dose_potential_risk)+sum(volume_dose_exception)) as volume_dose_exception_risk,
            case WHEN (sum(volume_dose_potential_risk)+sum(volume_dose_exception)) = 0 then 0 else ((sum(volume_dose_potential_risk)+sum(volume_dose_exception)) / sum(volume_dose) * 100.0) end rate_volume_dose_exception_risk,
            IFNULL(sum(volume_dose_potential_risk),0) as volume_dose_potential_risk,
            case WHEN sum(volume_dose) = 0 then 0 else (IFNULL(sum(volume_dose_potential_risk),0) / sum(volume_dose) * 100.0) end rate_volume_dose_potential_risk,
        </if>
        IFNULL(sum(added),0) as added,
        IFNULL(sum(added_normal),0) as added_normal,
        IFNULL(sum(added_exception),0) as added_exception,
        CASE WHEN sum(added) = 0  then 0 else (IFNULL(sum(added_exception),0) / sum(added) * 100.0) end rate_added_exception,
        IFNULL(sum(dau),0) as dau,
        IFNULL(sum(dau_normal),0) as dau_normal,
        IFNULL(sum(dau_exception),0) as dau_exception,
        CASE WHEN sum(dau) = 0 then 0 else (IFNULL(sum(dau_exception),0) / sum(dau) * 100.0) end rate_dau_exception,
        IFNULL(sum(`updates`),0) as `updates`,
        IFNULL(sum(update_normal),0) as update_normal,
        IFNULL(sum(update_exception),0) as update_exception,
        CASE WHEN sum(`updates`) = 0 then 0 else (IFNULL(sum(update_exception),0) / sum(`updates`) * 100.0) end rate_update_exception
    </sql>


    <select id="report" resultType="com.reyun.saas.mob.user.domain.WarningDataInfo">
        select CASE WHEN ${eval} then 1 else 0 end as warningLimit,t0.* from (
            select
                <include refid="warningSumSQL"/>
            from (
                select
                    <include refid="warningDataSQL"/>
                from
                    `yy-flink`.data_info_hour
                where  ds = #{ds} and hour &gt;= #{startHour} and hour &lt;= #{endHour}  and os = #{os} and appkey = #{appkey} and cid =#{cid}
            ) a
        ) t0
    </select>
</mapper>