<?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.keywords.dao.impl.BookKeywordRecordDaoImpl">

    <insert id="insert" parameterType="bookKeywordRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into book_keyword_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            book_id,
            channel_id,
            book_group_id,
            classify_id,
            adviser_id,
            weixin_qrcode_id,
            weixin_group_id,
            group_qrcode_id,
            keyword_id,
            user_wx_id,
            is_send,
            create_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{bookId,jdbcType=BIGINT},
            #{channelId,jdbcType=BIGINT},
            #{bookGroupId,jdbcType=BIGINT},
            #{classifyId,jdbcType=BIGINT},
            #{adviserId,jdbcType=BIGINT},
            #{weixinQrcodeId,jdbcType=BIGINT},
            #{weixinGroupId,jdbcType=BIGINT},
            #{groupQrcodeId,jdbcType=BIGINT},
            #{keywordId,jdbcType=BIGINT},
            #{userWxId,jdbcType=VARCHAR},
            #{isSend},
            NOW(),
        </trim>
    </insert>

    <resultMap id="KeywordMap" type="KeywordUserCountDTO">
        <id column="weixin_qrcode_id" property="qrcodeId" jdbcType="BIGINT"/>
        <result column="useCount" property="useCount" jdbcType="BIGINT"/>
    </resultMap>
    <select id="listKeywordUseCount" resultMap="KeywordMap" parameterType="list">
        SELECT
            weixin_qrcode_id,
            count(1) useCount
        FROM
        book_keyword_record
        WHERE
        weixin_qrcode_id IN
        <foreach collection = "list" index="index" item = "item" open = "("  separator= "," close = ")">
            ${item}
        </foreach>
        GROUP BY
        weixin_qrcode_id
    </select>

    <!--获得统计-->
    <select id="getKeywordStatistics" parameterType="map" resultType="com.pcloud.book.keywords.dto.KeywordStatisticsDTO">
        SELECT
        t.keyword_id keywordId,
        COUNT(t.id) touchCount,
        MAX(t.create_time) lastTouchTime
        FROM
        book_keyword_record t
        WHERE
        t.keyword_id IN
        <foreach collection="keywordIds" separator="," open="(" close=")" item="item" index="index">
            #{item}
        </foreach>
        <if test="bookGroupId!=null">
            AND t.book_group_id = #{bookGroupId}
        </if>
        <if test="weixinGroupId!=null">
            AND t.weixin_group_id = #{weixinGroupId}
        </if>
        GROUP BY
        t.keyword_id
    </select>

    <!--根据时间获取触发次数-->
    <select id="getKeywordStatisticsByTime" parameterType="map" resultType="com.pcloud.book.keywords.dto.KeywordStatisticsDTO">
        SELECT
        t.keyword_id keywordId,
        COUNT(t.id) weekTouchCount
        FROM
        book_keyword_record t
        WHERE
        t.keyword_id IN
        <foreach collection="keywordIds" separator="," open="(" close=")" item="item" index="index">
            #{item}
        </foreach>
        <if test="bookGroupId!=null">
            AND t.book_group_id = #{bookGroupId}
        </if>
        <if test="weixinGroupId!=null">
            AND t.weixin_group_id = #{weixinGroupId}
        </if>
        <if test="startDate!=null">
           AND t.create_time>#{startDate}
        </if>
        GROUP BY
        t.keyword_id
    </select>
</mapper>