<?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.AdvertisingClickRecordDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingClickRecord">
        <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"/>
    </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
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="Long">
        select
        <include refid="Base_Column_List"/>
        from advertising_click_record
        where id = #{id}
    </select>

    <select id="getClickNum" parameterType="map" resultType="Long">
        SELECT
            count(1)
        FROM
            advertising_click_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="getClickNumByMasterId" parameterType="map" resultType="Long">
        SELECT
            count(1)
        FROM
            advertising_click_record r
        INNER JOIN advertising_space s ON r.ad_id = s.id
        WHERE
            s.master_id = #{masterId}
    </select>

    <select id="getClickNumByBookId" parameterType="map" resultType="Long">
        SELECT
            count(1)
        FROM
            advertising_click_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="getClickNumByGroupQrcodeId" parameterType="map" resultType="Long">
        SELECT
            count(1)
        FROM
            advertising_click_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>

    <!--计算点击人数-包含userId=null-->
    <select id="getClickUserNumByGroupQrcodeId" parameterType="map" resultType="Long">
        SELECT
            count(1)
        FROM
        (
        SELECT
            wechat_user_id
        FROM
            advertising_click_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>
        GROUP BY
        wechat_user_id
        ) AS a
    </select>

    <select id="getTotalClickNum" resultType="Long">
        SELECT
            count(1)
        FROM
            advertising_click_record
    </select>

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

    <select id="clickUserList4AdvertisingWechatGroup" parameterType="map" resultType="com.pcloud.book.advertising.dto.WechatGroupClickUserDTO">
        SELECT
            ad_id adId,
            official_account_id officialAccountId,
            from_type fromType,
            from_id fromId,
            wechat_user_id wechatUserId,
            create_time createTime
        FROM
            advertising_click_record
        WHERE
            from_id = #{qrcodeId}
        AND from_type = 'WECHAT_GROUP'
        AND ad_id = #{adId}
        <if test="statisMonth != null">
            AND create_month = #{statisMonth}
        </if>
        ORDER BY
            createTime DESC
    </select>

    <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingClickRecord"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_click_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,
        </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'),
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.advertising.entity.AdvertisingClickRecord">
        update advertising_click_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>

    <select id="getClickUserNumByMasterId" parameterType="map" resultType="Long">
        SELECT
        COUNT(DISTINCT wechat_user_id)
        FROM
        advertising_click_record r
        INNER JOIN advertising_space s ON r.ad_id = s.id
        WHERE
        s.master_id = #{masterId}
    </select>

    <!--计算点击人数-包含userId=null-->
    <select id="getClickUserNumByAdId" parameterType="map" resultType="Long">
        SELECT
            COUNT(1)
        FROM
        (
        SELECT
            wechat_user_id
        FROM
            advertising_click_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>
        GROUP BY
            wechat_user_id
        ) AS a
    </select>

    <!--计算点击人数-包含userId=null-->
    <select id="getClickUserNumByBookId" parameterType="map" resultType="Long">
        SELECT
        COUNT(1)
        FROM
        (
        SELECT
        wechat_user_id
        FROM
        advertising_click_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>
        GROUP BY
        wechat_user_id
        ) AS a
    </select>

    <select id="getClickNumByCPAId" parameterType="map" resultType="Long">
        SELECT
        count(1)
        FROM
        advertising_click_record r
        INNER JOIN advertising_space s ON r.ad_id = s.id
        WHERE
        s.cpa_id = #{cpaId}
    </select>

    <select id="getClickUserNumByCPAId" parameterType="map" resultType="Long">
        SELECT
        COUNT(DISTINCT wechat_user_id)
        FROM
        advertising_click_record r
        INNER JOIN advertising_space s ON r.ad_id = s.id
        WHERE
        s.cpa_id = #{cpaId}
    </select>

</mapper>