<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.advertising.dao.impl.AdvertisingIncomeDailyDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingIncomeDaily">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="ad_id" property="adId" jdbcType="BIGINT"/>
        <result column="master_id" property="masterId" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="qrcode_id" property="qrcodeId" jdbcType="BIGINT"/>
        <result column="settlement_method" property="settlementMethod" jdbcType="VARCHAR"/>
        <result column="price_each_time" property="priceEachTime" jdbcType="DECIMAL"/>
        <result column="exposure_num" property="exposureNum" jdbcType="INTEGER"/>
        <result column="click_num" property="clickNum" jdbcType="INTEGER"/>
        <result column="income" property="income" jdbcType="DECIMAL"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="statis_day" property="statisDay" jdbcType="TIMESTAMP"/>
        <result column="statis_month" property="statisMonth" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, ad_id, master_id, book_id, channel_id, classify_id, qrcode_id, settlement_method, price_each_time,
        exposure_num, click_num, income, create_time, statis_day, statis_month
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from advertising_income_daily
        where id = #{id,jdbcType=BIGINT}
    </select>

    <select id="getTotalIncome" resultType="Decimal">
        select
            ifnull(sum(income), 0)
        from
            advertising_income_daily
    </select>

    <select id="getIncomeByAdId" resultType="Decimal" parameterType="map">
        select
            ifnull(sum(income), 0)
        from
            advertising_income_daily
        where
            ad_id = #{adId}
        <if test="bookId != null">
            AND book_id = #{bookId}
            AND channel_id = #{channelId}
        </if>
        <if test="statisMonth != null">
            and statis_month = #{statisMonth}
        </if>
    </select>

    <select id="getIncomeByMasterId" resultType="Decimal" parameterType="map">
        select
            ifnull(sum(income), 0)
        from
            advertising_income_daily
        where
            master_id = #{masterId}
    </select>

    <select id="getIncomeByBookId" resultType="Decimal" parameterType="map">
        select
            ifnull(sum(income), 0)
        from
            advertising_income_daily
        where
            book_id = #{bookId}
        AND channel_id = #{channelId}
        AND qrcode_id = 0
    </select>

    <select id="getIncomeByGroupQrcodeId" resultType="Decimal" parameterType="map">
        select
            ifnull(sum(income), 0)
        from
            advertising_income_daily
        where
            qrcode_id = #{qrcodeId}
        <if test="adId != null">
            and ad_id = #{adId}
        </if>
        <if test="statisMonth != null">
            and statis_month = #{statisMonth}
        </if>
    </select>

    <select id="getIncomeStatisTrend" resultType="com.pcloud.book.advertising.dto.DateCountDTO" parameterType="map">
        SELECT
            tmp.date,
            ifnull(sum(d.income), 0) income
        FROM
            <foreach collection="collectDates" index="index" item="item" open="(" separator="date union all" close="date ) tmp">
                select '${item}'
            </foreach>
        LEFT JOIN advertising_income_daily d ON d.statis_day = tmp.date
        GROUP BY
            tmp.date
        ORDER BY
            tmp.date
    </select>

    <delete id="deleteById" parameterType="java.lang.Long">
        delete from advertising_income_daily
        where id = #{id,jdbcType=BIGINT}
    </delete>

    <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingIncomeDaily"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_income_daily
        <trim prefix="(" suffix=")" suffixOverrides=",">
            ad_id,
            master_id,
            book_id,
            channel_id,
            classify_id,
            qrcode_id,
            settlement_method,
            price_each_time,
            exposure_num,
            click_num,
            income,
            create_time,
            statis_day,
            statis_month,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{adId,jdbcType=BIGINT},
            #{masterId,jdbcType=BIGINT},
            #{bookId,jdbcType=BIGINT},
            #{channelId,jdbcType=BIGINT},
            #{classifyId,jdbcType=BIGINT},
            #{qrcodeId,jdbcType=BIGINT},
            #{settlementMethod,jdbcType=VARCHAR},
            #{priceEachTime,jdbcType=DECIMAL},
            #{exposureNum,jdbcType=INTEGER},
            #{clickNum,jdbcType=INTEGER},
            #{income,jdbcType=DECIMAL},
            NOW(),
            #{statisDay,jdbcType=TIMESTAMP},
            #{statisMonth,jdbcType=VARCHAR},
        </trim>
    </insert>
    
    <insert id="insertRecordWithoutWechatGroupMsgAd" parameterType="map" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO advertising_income_daily (
            ad_id,
            master_id,
            book_id,
            channel_id,
            classify_id,
            qrcode_id,
            settlement_method,
            price_each_time,
            exposure_num,
            click_num,
            income,
            create_time,
            statis_day,
            statis_month
        ) SELECT
            b.ad_id,
            s.master_id,
            b.book_id,
            b.channel_id,
            ifnull(b.classify_id, 0),
            ifnull(b.qrcode_id, 0),
            m.settlement_method,
            m.price_each_time,
            ifnull(er.exposure_num, 0) exposure_num,
            ifnull(cr.click_num, 0) click_num,
            (
                CASE m.settlement_method
                WHEN 'CPC' THEN
                    ifnull(cr.click_num, 0) * m.price_each_time
                WHEN 'CPM' THEN
                    ifnull(er.exposure_num, 0) * m.price_each_time
                WHEN 'CPA' THEN
                ifnull(br.register_num, 0) * ifnull(m.price_each_time,0)
                ELSE
                    0
                END
            ) income,
            now(),
            #{createDay},
            DATE_FORMAT(#{createDay}, '%Y-%m')
        FROM
            advertising_settlement_method m,
            advertising_space s,
            advertising_distribution_book b
        LEFT JOIN (
            SELECT
                ad_id,
                book_id,
                channel_id,
                sum(count) exposure_num
            FROM
                advertising_exposure_record
            WHERE
                create_day = #{createDay}
            AND from_type != 'WECHAT_GROUP'
            GROUP BY
                ad_id,
                book_id,
                channel_id
        ) er ON er.ad_id = b.ad_id
        AND er.book_id = b.book_id
        AND er.channel_id = b.channel_id
        LEFT JOIN (
            SELECT
                ad_id,
                book_id,
                channel_id,
                count(id) click_num
            FROM
                advertising_click_record
            WHERE
                create_day = #{createDay}
            AND from_type != 'WECHAT_GROUP'
            GROUP BY
                ad_id,
                book_id,
                channel_id
        ) cr ON cr.ad_id = b.ad_id
        AND cr.book_id = b.book_id
        AND cr.channel_id = b.channel_id
        LEFT JOIN (
        SELECT
            ad_id,
            book_id,
            channel_id,
            COUNT(id) register_num
        FROM
            advertising_bm_register
        WHERE
            create_day = #{createDay}
        AND from_type != 'WECHAT_GROUP'
        GROUP BY
            ad_id,
            book_id,
            channel_id
        ) br ON br.ad_id = b.ad_id
        AND br.book_id = b.book_id
        AND br.channel_id = b.channel_id
        WHERE
            b.ad_id = s.id
        AND s.id = m.ad_id
        AND (
            s.ad_position = 'BOOK_GROUP_PAGE'
            OR s.ad_position = 'APP_OR_PRODUCT'
        )
        AND b.qrcode_id IS NULL
        GROUP BY
            b.ad_id,
            b.book_id,
            b.channel_id
        HAVING
            income > 0
    </insert>

    <insert id="insertWechatGroupMsgAdRecord" parameterType="map" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO advertising_income_daily (
            ad_id,
            master_id,
            book_id,
            channel_id,
            classify_id,
            qrcode_id,
            settlement_method,
            price_each_time,
            exposure_num,
            click_num,
            income,
            create_time,
            statis_day,
            statis_month
        ) SELECT
            b.ad_id,
            s.master_id,
            b.book_id,
            b.channel_id,
            ifnull(b.classify_id, 0),
            ifnull(b.qrcode_id, 0),
            m.settlement_method,
            m.price_each_time,
            ifnull(er.exposure_num, 0) exposure_num,
            ifnull(cr.click_num, 0) click_num,
            (
                CASE m.settlement_method
                WHEN 'CPC' THEN
                    ifnull(cr.click_num, 0) * m.price_each_time
                WHEN 'CPM' THEN
                    ifnull(er.exposure_num, 0) * m.price_each_time
                WHEN 'CPA' THEN
                ifnull(br.register_num, 0) * ifnull(m.price_each_time,0)
                ELSE
                    0
                END
            ) income,
            now(),
            #{createDay},
            DATE_FORMAT(#{createDay}, '%Y-%m')
        FROM
            advertising_settlement_method m,
            advertising_space s,
            advertising_distribution_book b
        LEFT JOIN (
            SELECT
                ad_id,
                book_id,
                channel_id,
                from_id,
                sum(count) exposure_num
            FROM
                advertising_exposure_record
            WHERE
                create_day = #{createDay}
            AND from_type = 'WECHAT_GROUP'
            GROUP BY
                ad_id,
                book_id,
                channel_id,
                from_id
        ) er ON er.ad_id = b.ad_id
        AND er.book_id = b.book_id
        AND er.channel_id = b.channel_id
        AND er.from_id = b.qrcode_id
        LEFT JOIN (
            SELECT
                ad_id,
                book_id,
                channel_id,
                from_id,
                count(id) click_num
            FROM
                advertising_click_record
            WHERE
                create_day = #{createDay}
            AND from_type = 'WECHAT_GROUP'
            GROUP BY
                ad_id,
                book_id,
                channel_id,
                from_id
        ) cr ON cr.ad_id = b.ad_id
        AND cr.book_id = b.book_id
        AND cr.channel_id = b.channel_id
        AND cr.from_id = b.qrcode_id
        LEFT JOIN (
            SELECT
                ad_id,
                book_id,
                channel_id,
                from_id,
                COUNT(id) register_num
            FROM
                advertising_bm_register
            WHERE
                create_day = #{createDay}
            AND from_type = 'WECHAT_GROUP'
            GROUP BY
                ad_id,
                book_id,
                channel_id,
                from_id
        ) br ON br.ad_id = b.ad_id
        AND br.book_id = b.book_id
        AND br.channel_id = b.channel_id
        AND br.from_id = b.qrcode_id
        WHERE
            b.ad_id = s.id
        AND s.id = m.ad_id
        AND s.ad_position = 'WECHAT_GROUP_MSG'
        AND b.qrcode_id IS NOT NULL
        GROUP BY
            b.ad_id,
            b.book_id,
            b.channel_id,
            b.classify_id,
            b.qrcode_id
        HAVING
            income > 0
    </insert>

    <update id="update" parameterType="com.pcloud.book.advertising.entity.AdvertisingIncomeDaily">
        update advertising_income_daily
        <set>
            <if test="adId != null">
                ad_id = #{adId,jdbcType=BIGINT},
            </if>
            <if test="masterId != null">
                master_id = #{masterId,jdbcType=BIGINT},
            </if>
            <if test="bookId != null">
                book_id = #{bookId,jdbcType=BIGINT},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId,jdbcType=BIGINT},
            </if>
            <if test="classifyId != null">
                classify_id = #{classifyId,jdbcType=BIGINT},
            </if>
            <if test="qrcodeId != null">
                qrcode_id = #{qrcodeId,jdbcType=BIGINT},
            </if>
            <if test="settlementMethod != null">
                settlement_method = #{settlementMethod,jdbcType=VARCHAR},
            </if>
            <if test="priceEachTime != null">
                price_each_time = #{priceEachTime,jdbcType=DECIMAL},
            </if>
            <if test="exposureNum != null">
                exposure_num = #{exposureNum,jdbcType=INTEGER},
            </if>
            <if test="clickNum != null">
                click_num = #{clickNum,jdbcType=INTEGER},
            </if>
            <if test="income != null">
                income = #{income,jdbcType=DECIMAL},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="statisDay != null">
                statis_day = #{statisDay,jdbcType=TIMESTAMP},
            </if>
            <if test="statisMonth != null">
                statis_month = #{statisMonth,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
</mapper>