<?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.AdvertisingBrowseRecordDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingBrowseRecord">
        <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="scene_id" property="sceneId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" 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="browse_times" property="browseTimes" jdbcType="BIGINT"/>
        <result column="temp_mark" property="tempMark" jdbcType="VARCHAR"/>
        <result column="record_id" property="recordId" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, ad_id, book_id, scene_id, adviser_id, channel_id, official_account_id, from_type,
        from_id, wechat_user_id, create_time, create_day, create_month, browse_times, temp_mark, record_id
    </sql>

    <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingBrowseRecord" useGeneratedKeys="true" keyProperty="id">
        insert into advertising_browse_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            ad_id,
            book_id,
            scene_id,
            adviser_id,
            channel_id,
            official_account_id,
            from_type,
            from_id,
            wechat_user_id,
            create_time,
            create_day,
            create_month,
            browse_times,
            temp_mark,
            record_id,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{adId,jdbcType=BIGINT},
            #{bookId,jdbcType=BIGINT},
            #{sceneId,jdbcType=BIGINT},
            #{adviserId,jdbcType=BIGINT},
            #{channelId,jdbcType=BIGINT},
            #{officialAccountId,jdbcType=BIGINT},
            #{fromType,jdbcType=VARCHAR},
            #{fromId,jdbcType=BIGINT},
            #{wechatUserId,jdbcType=BIGINT},
            NOW(),
            NOW(),
            DATE_FORMAT(NOW(), '%Y-%m'),
            #{browseTimes},
            #{tempMark},
            #{recordId},
        </trim>
    </insert>

    <update id="addBrowseTimesByRecordId" parameterType="map">
        update advertising_browse_record
        set browse_times = browse_times + #{browseTimes}
        where record_id = #{recordId}
    </update>

    <select id="getBrowseTimesByQrcode" parameterType="map" resultType="Long">
        SELECT
        IFNULL(SUM(browse_times), 0)
        FROM
        advertising_browse_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="getBrowseUserCountByQrcode" parameterType="map" resultType="Long">
        SELECT
        COUNT(DISTINCT wechat_user_id)
        FROM
        advertising_browse_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="getBrowseTimesByBook" resultType="Long" parameterType="map">
        SELECT
        IFNULL(SUM(browse_times), 0)
        FROM
        advertising_browse_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="getBrowseUserCountByBook" parameterType="map" resultType="Long">
        SELECT
        COUNT(DISTINCT wechat_user_id)
        FROM
        advertising_browse_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>

</mapper>