<?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="cn.com.ctop.crawler.modules.pangolin.mapper.PangolinReportChannelDailyMapper">

    <select id="getActivationList"
            resultType="cn.com.ctop.crawler.modules.pangolin.entity.PangolinReportChannelDaily">
        select
            daily.date,
--             daily.channel_name as 'channel_name',
            '-' as 'channel_name',
            <if test="appId == null">
                '-' as 'app_name',
            </if>
            <if test="appId != null">
                daily.app_name,
            </if>
            daily.oprate_system,
            sum(daily.external_activation) as 'external_activation',
            sum(daily.total_price)as 'total_price'
        from
        ctop_pangolin_report_channel_daily daily
        left join ctop_bind_account_login login on login.account_name = daily.account_name
        left join ctop_pangolin_app app on app.name = daily.app_name and app.account_name = daily.account_name
        where 1=1
        <if test="userId!=null">
            and login.user_id = #{userId}
        </if>
        <if test="appId!=null">
            and app.app_id = #{appId}
        </if>
        <if test="startDate!=null">
            and daily.date &gt;= #{startDate}
        </if>
        <if test="endDate!=null">
            and daily.date &lt;= #{endDate}
        </if>
        <if test="type!=null">
            and daily.os = #{type}
        </if>
        <if test="accountNames!=null or projectIds!=null">
            and  ( 1=0
            <if test="accountNames!=null">
                or daily.account_name in
                <foreach item="item" index="index" collection="accountNames" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="projectIds!=null">
                or login.project_id in
                <foreach item="item" index="index" collection="projectIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            )
        </if>
        group by daily.date
    </select>
    <select id="sumAppList" resultType="java.lang.Integer">
        select
            sum(daily.external_activation)
        from
        ctop_pangolin_report_channel_daily daily
        left join ctop_bind_account_login login on login.account_name = daily.account_name
        left join ctop_pangolin_app app on app.account_name = daily.account_name and daily.app_name = app.name
        where 1=1
        <if test="userId!=null">
            and login.user_id = #{userId}
        </if>
        <if test="appId!=null">
            and app.app_id = #{appId}
        </if>
        <if test="startDate!=null">
            and daily.date &gt;= #{startDate}
        </if>
        <if test="endDate!=null">
            and daily.date &lt;= #{endDate}
        </if>
        <if test="type!=null">
            and daily.os = #{type}
        </if>
        <if test="accountNames!=null or projectIds!=null">
            and  ( 1=0
            <if test="accountNames!=null">
                or daily.account_name in
                <foreach item="item" index="index" collection="accountNames" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="projectIds!=null">
                or login.project_id in
                <foreach item="item" index="index" collection="projectIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            )
        </if>
    </select>

</mapper>
