<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pcloud.book.mapper.clickhouse.AppletRecordCHMapper">

    <insert id="batchInsert">
        INSERT INTO applet_record (
        id,
        wechat_user_id,
        record_type,
        rights_setting_id,
        book_id,
        channel_id,
        adviser_id,
        from_id,
        from_name,
        type_code,
        account_setting_id,
        create_time,
        create_date,
        is_delete,
        link_url
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.id},
            #{item.wechatUserId},
            #{item.recordType},
            #{item.rightsSettingId},
            #{item.bookId},
            #{item.channelId},
            #{item.adviserId},
            #{item.fromId},
            #{item.fromName},
            #{item.typeCode},
            #{item.accountSettingId},
            #{item.createTime},
            #{item.createDate},
            #{item.isDelete},
            #{item.linkUrl}
            )
        </foreach>
    </insert>

    <select id="getAggrStatis" resultType="com.pcloud.book.applet.dto.AppletRecordDTO">
        SELECT record_type recordType, from_id fromId, any(from_name) fromName, any(link_url) linkUrl, any(book_id) bookId, any(type_code) typeCode,
        COUNT(id) clickCount, COUNT(DISTINCT wechat_user_id) userCount
        FROM applet_record
        <where>
            <if test="recordTypes != null and recordTypes.size() > 0">
                (
            </if>
            <foreach collection="recordTypes" item="item" separator=" OR " open="(" close=")">
                <if test="item.recordType != null">
                    record_type = #{item.recordType}
                </if>
                <if test="item.typeCodes != null and item.typeCodes.size() > 0">
                    AND type_code IN
                    <foreach collection="item.typeCodes" item="typeCode" separator="," open="(" close=")">
                        #{typeCode}
                    </foreach>
                </if>
            </foreach>
            <if test="recordTypes != null and recordTypes.size() > 0">
                )
            </if>
            <if test="beginDate != null">
                AND create_time BETWEEN #{beginDate} AND #{endDate}
            </if>
        </where>
        GROUP BY record_type, from_id
        ORDER BY userCount DESC
        LIMIT #{offset},#{limit}
    </select>

    <update id="optimize">
        optimize table applet_record
    </update>


    <select id="getAggrStatis4H5" resultType="com.pcloud.book.applet.dto.AppletRecordDTO">
        SELECT
        record_type recordType,
        from_id fromId,
        any(from_name) fromName,
        any(link_url) linkUrl,
        any(type_code) typeCode,
        COUNT(id) clickCount,
        COUNT(DISTINCT wechat_user_id) userCount
        FROM applet_record
        WHERE
            record_type in (5,6)
            <if test="sourceKeywords != null and sourceKeywords.size() > 0">
                AND
                <foreach collection="sourceKeywords" item="sourceKeyword" separator=" OR " open="(" close=")">
                    from_name LIKE  concat('%',#{sourceKeyword},'%')
                </foreach>
            </if>
            <if test="beginDate != null">
                AND create_time >= #{beginDate}
            </if>
            <if test="endDate != null">
                AND create_time &lt;= #{endDate}
            </if>
            <if test="fromIdList != null and fromIdList.size() > 0">
                AND from_id in
                <foreach collection="fromIdList" index="index" separator="," open="(" close=")" item="item">
                    #{item}
                </foreach>
            </if>
        GROUP BY record_type, from_id
        ORDER BY userCount DESC
        <if test="offset != null and limit > 0">
            LIMIT #{offset}, #{limit}
        </if>
    </select>
</mapper>
