<?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.AdvertisingExposureRecordDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingExposureRecord">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="ad_id" property="adId" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="is_book_group" property="isBookGroup" jdbcType="BIT"/>
        <result column="scene_id" property="sceneId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="agent_id" property="agentId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="official_account_id" property="officialAccountId" jdbcType="BIGINT"/>
        <result column="from_type" property="fromType" jdbcType="VARCHAR"/>
        <result column="from_id" property="fromId" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="create_day" property="createDay" jdbcType="DATE"/>
        <result column="create_month" property="createMonth" jdbcType="VARCHAR"/>
        <result column="count" property="count" jdbcType="BIGINT"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, ad_id, book_id, is_book_group, scene_id, adviser_id, agent_id, channel_id, official_account_id, from_type,
        from_id, wechat_user_id, create_time, create_day, create_month, count
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="Long">
        select
        <include refid="Base_Column_List"/>
        from advertising_exposure_record
        where id = #{id}
    </select>
    
    <select id="getExposureNum" parameterType="map" resultType="Long">
        SELECT
            ifnull(sum(count), 0)
        FROM
            advertising_exposure_record
        WHERE
            ad_id = #{adId}
        <if test="bookId != null">
            AND book_id = #{bookId}
            AND adviser_id = #{adviserId}
            AND channel_id = #{channelId}
        </if>
        <if test="statisMonth != null">
            and create_month = #{statisMonth}
        </if>
    </select>

    <select id="getExposureNumByMasterId" parameterType="map" resultType="Long">
        SELECT
            ifnull(sum(count), 0)
        FROM
            advertising_exposure_record r
        INNER JOIN advertising_space s ON r.ad_id = s.id
        WHERE
            s.master_id = #{masterId}
    </select>

    <select id="getExposureNumByBookId" parameterType="map" resultType="Long">
        SELECT
            ifnull(sum(count), 0)
        FROM
            advertising_exposure_record
        WHERE
            book_id = #{bookId}
        AND adviser_id = #{adviserId}
        AND channel_id = #{channelId}
        AND from_type != 'WECHAT_GROUP'
        <if test="adId != null">
            AND ad_id = #{adId}
        </if>
        <if test="statisMonth != null">
            AND create_month = #{statisMonth}
        </if>
    </select>

    <select id="getExposureNumByGroupQrcodeId" parameterType="map" resultType="Long">
        SELECT
            ifnull(sum(count), 0)
        FROM
            advertising_exposure_record
        WHERE
            from_id = #{qrcodeId}
        AND from_type = 'WECHAT_GROUP'
        <if test="adId != null">
            AND ad_id = #{adId}
        </if>
        <if test="statisMonth != null">
            AND create_month = #{statisMonth}
        </if>
    </select>

    <select id="getTotalExposureNum" resultType="Long">
        SELECT
            ifnull(sum(count), 0)
        FROM
            advertising_exposure_record
    </select>

    <select id="getFlowStatisTrend" resultType="com.pcloud.book.advertising.dto.DateCountDTO" parameterType="map">
        SELECT
            tmp.date,
            ifnull(er.exposure_num, 0) exposureNum,
            ifnull(cr.click_num, 0) clickNum
        FROM
            <foreach collection="collectDates" index="index" item="item" open="(" separator="date union all" close="date ) tmp">
                select '${item}'
            </foreach>
        LEFT JOIN (
            SELECT
                create_day,
                sum(count) exposure_num
            FROM
                advertising_exposure_record
            WHERE
                create_day BETWEEN #{startDate}
            AND #{endDate}
            GROUP BY
                create_day
        ) er ON er.create_day = tmp.date
        LEFT JOIN (
            SELECT
                create_day,
                count(id) click_num
            FROM
                advertising_click_record
            WHERE
                create_day BETWEEN #{startDate}
            AND #{endDate}
            GROUP BY
                create_day
        ) cr ON cr.create_day = tmp.date
        GROUP BY
            tmp.date
        ORDER BY
            tmp.date
    </select>

    <select id="advertisingWechatGroupRankTop" resultType="AdGroupQrcodeDTO">
        SELECT
            r.from_id groupQrcodeId,
            q.group_name groupQrcodeName,
            r.book_id bookId,
            b.BOOK_NAME bookName,
            sum(r.count) exposureNum
        FROM
            advertising_exposure_record r
        LEFT JOIN book_group_qrcode q ON q.id = r.from_id
        LEFT JOIN book b ON b.BOOK_ID = r.book_id
        WHERE
            r.from_type = 'WECHAT_GROUP'
        GROUP BY
            r.from_id
        ORDER BY
            exposureNum DESC,
            q.id DESC
        LIMIT 6
    </select>

    <delete id="deleteById" parameterType="Long">
        delete from advertising_exposure_record
        where id = #{id}
    </delete>

    <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingExposureRecord"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_exposure_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            ad_id,
            book_id,
            is_book_group,
            scene_id,
            adviser_id,
            agent_id,
            channel_id,
            official_account_id,
            from_type,
            from_id,
            wechat_user_id,
            create_time,
            create_day,
            create_month,
            count
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{adId,jdbcType=BIGINT},
            #{bookId,jdbcType=BIGINT},
            #{isBookGroup,jdbcType=BIT},
            #{sceneId,jdbcType=BIGINT},
            #{adviserId,jdbcType=BIGINT},
            #{agentId,jdbcType=BIGINT},
            #{channelId,jdbcType=BIGINT},
            #{officialAccountId,jdbcType=BIGINT},
            #{fromType,jdbcType=VARCHAR},
            #{fromId,jdbcType=BIGINT},
            #{wechatUserId,jdbcType=BIGINT},
            NOW(),
            NOW(),
            DATE_FORMAT(NOW(), '%Y-%m'),
            #{count,jdbcType=BIGINT}
        </trim>
    </insert>

    <insert id="batchInsert" parameterType="com.pcloud.book.advertising.entity.AdvertisingExposureRecord"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_exposure_record
        (
            ad_id,
            book_id,
            is_book_group,
            scene_id,
            adviser_id,
            agent_id,
            channel_id,
            official_account_id,
            from_type,
            from_id,
            wechat_user_id,
            create_time,
            create_day,
            create_month,
            count
        ) values
        <foreach collection="list" item="item" index="index"
                 separator=",">
            (#{item.adId,jdbcType=BIGINT},
            #{item.bookId,jdbcType=BIGINT},
            #{item.isBookGroup,jdbcType=BIT},
            #{item.sceneId,jdbcType=BIGINT},
            #{item.adviserId,jdbcType=BIGINT},
            #{item.agentId,jdbcType=BIGINT},
            #{item.channelId,jdbcType=BIGINT},
            #{item.officialAccountId,jdbcType=BIGINT},
            #{item.fromType,jdbcType=VARCHAR},
            #{item.fromId,jdbcType=BIGINT},
            #{item.wechatUserId,jdbcType=BIGINT},
            NOW(),
            NOW(),
            DATE_FORMAT(NOW(), '%Y-%m'),
            #{item.count,jdbcType=BIGINT})
        </foreach>
    </insert>

    <update id="update" parameterType="com.pcloud.book.advertising.entity.AdvertisingExposureRecord">
        update advertising_exposure_record
        <set>
            <if test="adId != null">
                ad_id = #{adId,jdbcType=BIGINT},
            </if>
            <if test="bookId != null">
                book_id = #{bookId,jdbcType=BIGINT},
            </if>
            <if test="isBookGroup != null">
                is_book_group = #{isBookGroup,jdbcType=BIT},
            </if>
            <if test="sceneId != null">
                scene_id = #{sceneId,jdbcType=BIGINT},
            </if>
            <if test="adviserId != null">
                adviser_id = #{adviserId,jdbcType=BIGINT},
            </if>
            <if test="agentId != null">
                agent_id = #{agentId,jdbcType=BIGINT},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId,jdbcType=BIGINT},
            </if>
            <if test="officialAccountId != null">
                official_account_id = #{officialAccountId,jdbcType=BIGINT},
            </if>
            <if test="fromType != null">
                from_type = #{fromType,jdbcType=VARCHAR},
            </if>
            <if test="fromId != null">
                from_id = #{fromId,jdbcType=BIGINT},
            </if>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId,jdbcType=BIGINT},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <update id="updateExposureByQrcode" parameterType="map">
        UPDATE advertising_exposure_record
        SET count = #{count}
        WHERE
        from_type = 'WECHAT_GROUP'
        AND from_id = #{fromId}
        AND count = 0
    </update>

</mapper>