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


    <sql id="relativeWhereSql">
        and os = #{data.os}
        and appkey in (${data.appkeys})
        <if test="params.device_status!=null">
            and device_status = #{params.device_status}
        </if>
        <if test="params.appIsUpdate!=null">
            and appIsUpdate = #{params.appIsUpdate}
        </if>
        <if test="params.quantity!=null">
            and quantity = #{params.quantity}
        </if>
    </sql>


    <select id="relative" resultType="int">
        select COALESCE(sum(distinct_count_num),0) from
        (
        <if test="data.dateType==1 or data.dateType==3 ">
            select
            distinct_count_num
            from `yy-flink`.data_info_day
            where ds &gt;= #{data.startDateStream} and ds &lt;= #{data.endDateStream}
            <include refid="relativeWhereSql"/>
        </if>
        <if test="data.dateType==3 ">
            union all
        </if>
        <if test="data.dateType==2 or data.dateType==3 ">
            select
            distinct_count_num
              from `yy_etl`.data_info_day
            where ds &gt;= #{data.startDateOffLine} and ds &lt;= #{data.endDateOffLine}
            <include refid="relativeWhereSql"/>
        </if>
        ) t
    </select>
<!--dau_info_day的dau潜在风险=总潜在风险-->
    <select id="relativeNew" resultType="int">
        select COALESCE(sum(distinct_count_num),0) from
        (
        <if test="data.dateType==1 or data.dateType==3 ">
            select
            distinct_count_num
            from `yy-flink`.dau_info_day
            where ds &gt;= #{data.startDateStream} and ds &lt;= #{data.endDateStream}
            and os = #{data.os}
            and appkey in (${data.appkeys})
            and quantity = '5'
        </if>
        <if test="data.dateType==3 ">
            union all
        </if>
        <if test="data.dateType==2 or data.dateType==3 ">
            select
            distinct_count_num
            from `yy_etl`.dau_info_day
            where ds &gt;= #{data.startDateOffLine} and ds &lt;= #{data.endDateOffLine}
            and os = #{data.os}
            and appkey in (${data.appkeys})
            and quantity = '5'
        </if>
        ) t
    </select>




    <sql id="dashboardChartColumnSQL">
        <if test="params.device_status!=null">
            case WHEN device_status = #{params.device_status}  and quantity = #{params.quantity_normal} then distinct_count_num else 0 end normal,
            case WHEN device_status = #{params.device_status} and quantity = #{params.quantity_exception} then distinct_count_num else 0 end exception,
            <if test="data.os==2">
                case WHEN device_status = #{params.device_status} and quantity = #{params.quantity_potential_risk} then distinct_count_num else 0 end potential_risk,
            </if>
        </if>

        <if test="params.appIsUpdate!=null">
            case WHEN appIsUpdate =  #{params.appIsUpdate} and quantity = #{params.quantity_normal} then distinct_count_num else 0 end normal,
            case WHEN appIsUpdate =  #{params.appIsUpdate} and quantity = #{params.quantity_exception} then distinct_count_num else 0 end exception,
            <if test="data.os==2">
                case WHEN appIsUpdate =  #{params.appIsUpdate} and quantity = #{params.quantity_potential_risk} then distinct_count_num else 0 end potential_risk,
            </if>
        </if>
        ds
    </sql>

    <select id="dashboardChart" resultType="Map">
        select
        sum(normal) normal
        ,sum(exception) exception
        <if test="data.os==2">
            ,sum(potential_risk) potential_risk
        </if>
        ,ds
        from
        (
        <if test="data.dateType==1 or data.dateType==3 ">
        select
            <include refid="dashboardChartColumnSQL"/>
        from
        `yy-flink`.data_info_day
        where  ds &gt;= #{data.startDateStream} and ds &lt;= #{data.endDateStream}and os = #{data.os} and appkey in (${data.appkeys})
        </if>

        <if test="data.dateType==3 ">
            union all
        </if>

        <if test="data.dateType==2 or data.dateType==3 ">
            select
            <include refid="dashboardChartColumnSQL"/>
            from
            `yy_etl`.data_info_day
            where  ds &gt;= #{data.startDateOffLine} and ds &lt;= #{data.endDateOffLine} and os = #{data.os} and appkey in (${data.appkeys})
        </if>
        )a
        GROUP BY ds
    </select>









    <!--dau用新表-->
    <sql id="dashboardChartForExceptionColumnSQL">
        case WHEN device_status = '1' and quantity = '2' then distinct_count_num else 0 end exception_new_user,
        0 as exception_dau,
        case WHEN appIsUpdate = '1' and quantity = '2' then distinct_count_num else 0 end exception_update,
        <if test="data.os==2">
            0 as potential_risk,
        </if>
        ds
    </sql>
    <sql id="dashboardChartForExceptionColumnSQLNew">
        0 as exception_new_user,
        case WHEN quantity = '2' then distinct_count_num else 0 end exception_dau,
        0 as exception_update,
        <if test="data.os==2">
            case WHEN quantity = '5' then distinct_count_num else 0 end potential_risk,
        </if>
        ds
    </sql>

    <select id="dashboardChartForException" resultType="Map">
        select
        sum(exception_new_user) exception_new_user
        ,sum(exception_dau) exception_dau
        ,sum(exception_update) exception_update
        <if test="data.os==2">
            ,sum(potential_risk) potential_risk
        </if>
        ,ds
        from (
        <if test="data.dateType==1 or data.dateType==3 ">
            select
                <include refid="dashboardChartForExceptionColumnSQL"/>
            from
            `yy-flink`.data_info_day
            where  ds &gt;= #{data.startDateStream} and ds &lt;= #{data.endDateStream}  and os = #{data.os} and appkey in (${data.appkeys})

            union all

            select
            <include refid="dashboardChartForExceptionColumnSQLNew"/>
            from
            `yy-flink`.dau_info_day
            where  ds &gt;= #{data.startDateStream} and ds &lt;= #{data.endDateStream}  and os = #{data.os} and appkey in (${data.appkeys})
        </if>

        <if test="data.dateType==3 ">
            union all
        </if>

        <if test="data.dateType==2 or data.dateType==3 ">
            select
                <include refid="dashboardChartForExceptionColumnSQL"/>
            from
            `yy_etl`.data_info_day
            where  ds &gt;= #{data.startDateOffLine} and ds &lt;= #{data.endDateOffLine}  and os = #{data.os} and appkey in (${data.appkeys})

            union all

            select
            <include refid="dashboardChartForExceptionColumnSQLNew"/>
            from
            `yy_etl`.dau_info_day
            where  ds &gt;= #{data.startDateOffLine} and ds &lt;= #{data.endDateOffLine}  and os = #{data.os} and appkey in (${data.appkeys})
        </if>
            ) a
            GROUP BY ds

    </select>







    <sql id="channelQualityTopChartSumSQL">
        <!--<if test="type=='num'">
            sum(normal) normal,sum(exception) exception,cid
        </if>
        <if test="type=='rate'">
            sum(normal)/sum(total) normal,sum(exception)/sum(total) exception,cid
        </if>-->
        sum(normal) normal,sum(exception) exception,cid
    </sql>

    <sql id="channelQualityTopChartSQL">
        case WHEN device_status = '1' and quantity = '1' then distinct_count_num else 0 end normal,
        case WHEN device_status = '1' and quantity = '2' then distinct_count_num else 0 end exception,
        case WHEN device_status = '1' and (quantity = '2' or quantity = '1') then distinct_count_num else 0 end total,
        cid
    </sql>

    <select id="channelQualityTopChart" resultType="Map">
        select
        <include refid="channelQualityTopChartSumSQL"/>
        from
        (
        <if test="data.dateType==1 or data.dateType==3 ">
            select
                <include refid="channelQualityTopChartSQL"/>
            from
                `yy-flink`.data_info_day
            where  ds &gt;= #{data.startDateStream} and ds &lt;= #{data.endDateStream}  and os = #{data.os} and appkey in (${data.appkeys})
        </if>

        <if test="data.dateType==3 ">
            union all
        </if>

        <if test="data.dateType==2 or data.dateType==3 ">
            select
            <include refid="channelQualityTopChartSQL"/>
            from
            `yy_etl`.data_info_day
            where  ds &gt;= #{data.startDateOffLine} and ds &lt;= #{data.endDateOffLine}  and os = #{data.os} and appkey in (${data.appkeys})
        </if>
            )a

            GROUP BY cid ORDER BY

        <if test="type=='num'">
            sum(exception)
        </if>
        <if test="type=='rate'">
            sum(exception)/sum(total)
        </if>

        desc limit 10
    </select>






    <select id="findSDKReportCount" resultType="java.lang.Long">
        SELECT sum(countNum) from (
        SELECT sum(count_num) AS 'countNum'  FROM `yy-flink`.data_info_day WHERE appkey in
        <foreach collection="appkeys" item="appkey" separator="," open="(" close=")">
            #{appkey}
        </foreach>
        AND ds &gt;= #{date} AND ds &gt;= #{pubDate}
        UNION ALL
        SELECT sum(count_num) AS 'countNum' FROM `yy_etl`.data_info_day WHERE appkey in
        <foreach collection="appkeys" item="appkey" separator="," open="(" close=")">
            #{appkey}
        </foreach>
        AND ds &lt; #{date} AND ds &gt;= #{pubDate}
        ) t1
    </select>





    <sql id="channelQualityTopListSumSQL">
        sum(total) num_total,
        sum(normal) num_normal,
        case WHEN sum(total) = 0 then 0.0 else sum(normal)*100.0/sum(total) end  rate_normal,
        sum(exception) num_exception,
        case WHEN sum(total) = 0 then 0.0 else sum(exception)*100.0/sum(total) end  rate_exception,
        cid
    </sql>
    <sql id="channelQualityTopListSQL">
        case WHEN device_status = '1' and quantity = '1' then distinct_count_num else 0 end normal,
        case WHEN device_status = '1' and quantity = '2' then distinct_count_num else 0 end exception,
        case WHEN device_status = '1' and (quantity = '2' or quantity = '1') then distinct_count_num else 0 end total,
        cid
    </sql>

    <select id="channelQualityTopList" resultType="Map">
        select
            <include refid="channelQualityTopListSumSQL"/>
        from
        (
        <if test="data.dateType==1 or data.dateType==3 ">
            select
            <include refid="channelQualityTopListSQL"/>
            from
            `yy-flink`.data_info_day
            where  ds &gt;= #{data.startDateStream} and ds &lt;= #{data.endDateStream}  and os = #{data.os} and appkey in (${data.appkeys})
        </if>

        <if test="data.dateType==3 ">
            union all
        </if>

        <if test="data.dateType==2 or data.dateType==3 ">
            select
            <include refid="channelQualityTopListSQL"/>
            from
            `yy_etl`.data_info_day
            where  ds &gt;= #{data.startDateOffLine} and ds &lt;= #{data.endDateOffLine}  and os = #{data.os} and appkey in (${data.appkeys})
        </if>
        )a
        GROUP BY cid
        <if test="data.orderBy==null">
            order by num_exception desc
        </if>
    </select>


    <select id="channelQualityTopListTotal" resultType="Map">
        select
            <include refid="channelQualityTopListSumSQL"/>
        from
        (
        <if test="data.dateType==1 or data.dateType==3 ">
            select
            <include refid="channelQualityTopListSQL"/>
            from
            `yy-flink`.data_info_day
            where  ds &gt;= #{data.startDateStream} and ds &lt;= #{data.endDateStream}  and os = #{data.os} and appkey in (${data.appkeys})
        </if>

        <if test="data.dateType==3 ">
            union all
        </if>

        <if test="data.dateType==2 or data.dateType==3 ">
            select
            <include refid="channelQualityTopListSQL"/>
            from
            `yy_etl`.data_info_day
            where  ds &gt;= #{data.startDateOffLine} and ds &lt;= #{data.endDateOffLine}  and os = #{data.os} and appkey in (${data.appkeys})
        </if>
        )a
    </select>








    <!--DAU单独查表-->
    <select id="relativeDau" resultType="int">
        select COALESCE(sum(distinct_count_num),0) from
        (
        <if test="data.dateType==1 or data.dateType==3 ">
            select
            distinct_count_num
            from `yy-flink`.dau_info_day
            where ds &gt;= #{data.startDateStream} and ds &lt;= #{data.endDateStream} and os = #{data.os} and appkey in (${data.appkeys})
            <if test="params.quantity!=null">
                and quantity = #{params.quantity}
            </if>
        </if>
        <if test="data.dateType==3 ">
            union all
        </if>
        <if test="data.dateType==2 or data.dateType==3 ">
            select
            distinct_count_num
            from `yy_etl`.dau_info_day
            where ds &gt;= #{data.startDateOffLine} and ds &lt;= #{data.endDateOffLine}  and os = #{data.os} and appkey in (${data.appkeys})
            <if test="params.quantity!=null">
                and quantity = #{params.quantity}
            </if>
        </if>
        ) t

    </select>

    <select id="dashboardChartDAU" resultType="Map">
        select
        sum(normal) normal
        ,sum(exception) exception
        <if test="data.os==2">
            ,sum(potential_risk) potential_risk
        </if>
        ,ds
        from
        (
        <if test="data.dateType==1 or data.dateType==3 ">
            select
                case WHEN quantity = #{params.quantity_normal} then distinct_count_num else 0 end normal,
                case WHEN quantity = #{params.quantity_exception} then distinct_count_num else 0 end exception,
                <if test="data.os==2">
                    case WHEN quantity = #{params.quantity_potential_risk} then distinct_count_num else 0 end potential_risk,
                </if>
                ds
            from
            `yy-flink`.dau_info_day
            where  ds &gt;= #{data.startDateStream} and ds &lt;= #{data.endDateStream} and os = #{data.os} and appkey in (${data.appkeys})
        </if>

        <if test="data.dateType==3 ">
            union all
        </if>

        <if test="data.dateType==2 or data.dateType==3 ">
            select
                case WHEN quantity = #{params.quantity_normal} then distinct_count_num else 0 end normal,
                case WHEN quantity = #{params.quantity_exception} then distinct_count_num else 0 end exception,
                <if test="data.os==2">
                    case WHEN quantity = #{params.quantity_potential_risk} then distinct_count_num else 0 end potential_risk,
                </if>
                ds
            from
            `yy_etl`.dau_info_day
            where  ds &gt;= #{data.startDateOffLine} and ds &lt;= #{data.endDateOffLine} and os = #{data.os} and appkey in (${data.appkeys})
        </if>
        )a
        GROUP BY ds
    </select>


</mapper>