<?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.applet.dao.impl.AppletRecordStatisDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletRecordStatis">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="record_type" property="recordType" jdbcType="INTEGER"/>
        <result column="source_type" property="sourceType" jdbcType="VARCHAR"/>
        <result column="click_count" property="clickCount" jdbcType="BIGINT"/>
        <result column="statis_date" property="statisDate" jdbcType="DATE"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="BaseResultDTOMap" type="com.pcloud.book.applet.dto.AppletRecordStatisDTO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="record_type" property="recordType" jdbcType="INTEGER"/>
        <result column="source_type" property="sourceType" jdbcType="VARCHAR"/>
        <result column="click_count" property="clickCount" jdbcType="BIGINT"/>
        <result column="statis_date" property="statisDate" jdbcType="DATE"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        id, wechat_user_id, record_type, source_type, click_count, statis_date, create_time, update_time        
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_record_statis
        WHERE id = #{id}
    </select>

    <select id="getBookSourceWeekStatis" resultMap="BaseResultDTOMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        `applet_record_statis`
        WHERE
        wechat_user_id = #{wechatUserId}
        AND record_type IN (5,6)
        AND statis_date = #{statisDate, jdbcType=DATE}
        ORDER BY
        click_count DESC
        LIMIT 5;
    </select>

    <select id="getBookSourceTotalClickCount" resultType="integer">
        SELECT IFNULL(SUM(click_count),0)
        FROM
        `applet_record_statis`
        WHERE
        wechat_user_id = #{wechatUserId}
        AND statis_date = #{statisDate, jdbcType=DATE}
        AND record_type IN (5,6)
    </select>

    <select id="getBookNewsWeekStatics" resultMap="BaseResultDTOMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        `applet_record_statis`
        WHERE
        wechat_user_id = #{wechatUserId}
        AND statis_date = #{statisDate, jdbcType=DATE}
        AND record_type = 1
        ORDER BY
        click_count DESC
        LIMIT 5;
    </select>

    <select id="getBookNewsTotalClickCount" resultType="integer">
        SELECT IFNULL(SUM(click_count),0)
        FROM
        `applet_record_statis`
        WHERE
        wechat_user_id = #{wechatUserId}
        AND statis_date = #{statisDate, jdbcType=DATE}
        AND record_type = 1
    </select>

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_record_statis
    </select>

    <insert id="addWeekAppletRecordStatis4App">
        INSERT INTO applet_record_statis (
            wechat_user_id,
            record_type,
            source_type,
            click_count,
            statis_date,
            create_time,
            update_time
        )
        SELECT ar.wechat_user_id, ar.record_type, ar.type_code, COUNT(1), #{prevMonday}, NOW(), NOW()
        FROM `applet_record` ar
        INNER JOIN (SELECT DISTINCT wechat_user_id FROM applet_record WHERE create_date BETWEEN #{prevMonday, jdbcType=DATE} AND #{prevSunday, jdbcType=DATE} ORDER BY wechat_user_id LIMIT #{limitStart}, #{limitNumber}) t ON t.wechat_user_id = ar.wechat_user_id
        WHERE ar.record_type IN (5,6) AND ar.create_date BETWEEN #{prevMonday, jdbcType=DATE} AND #{prevSunday, jdbcType=DATE}
        GROUP BY ar.wechat_user_id, ar.record_type, ar.type_code
    </insert>

    <insert id="addWeekAppletRecordStatis4News">
        INSERT INTO applet_record_statis (
            wechat_user_id,
            record_type,
            source_type,
            click_count,
            statis_date,
            create_time,
            update_time
        )
        SELECT ar.wechat_user_id, record_type, an.source, COUNT(1), #{prevMonday}, NOW(), NOW()
        FROM applet_record ar
        INNER JOIN (SELECT DISTINCT wechat_user_id FROM applet_record WHERE create_date BETWEEN #{prevMonday, jdbcType=DATE} AND #{prevSunday, jdbcType=DATE} ORDER BY wechat_user_id LIMIT #{limitStart}, #{limitNumber}) t ON t.wechat_user_id = ar.wechat_user_id
        INNER JOIN applet_news an ON ar.from_id = an.id
        WHERE record_type = 1 AND create_date BETWEEN #{prevMonday, jdbcType=DATE} AND #{prevSunday, jdbcType=DATE}
        GROUP BY ar.wechat_user_id, an.source
    </insert>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO applet_record_statis(
            wechat_user_id, 
            record_type, 
            source_type, 
            click_count, 
            statis_date, 
            create_time, 
            update_time
        ) VALUES (
            #{wechatUserId, jdbcType=BIGINT}, 
            #{recordType, jdbcType=INTEGER}, 
            #{sourceType, jdbcType=VARCHAR}, 
            #{clickCount, jdbcType=BIGINT}, 
            #{statisDate, jdbcType=DATE}, 
            #{createTime, jdbcType=TIMESTAMP}, 
            #{updateTime, jdbcType=TIMESTAMP}
        )
    </insert>
    
    <insert id="batchInsert">
        INSERT INTO applet_record_statis (
            wechat_user_id, 
            record_type, 
            source_type, 
            click_count, 
            statis_date, 
            create_time, 
            update_time
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.wechatUserId, jdbcType=BIGINT}, 
            #{item.recordType, jdbcType=INTEGER}, 
            #{item.sourceType, jdbcType=VARCHAR}, 
            #{item.clickCount, jdbcType=BIGINT}, 
            #{item.statisDate, jdbcType=DATE}, 
            #{item.createTime, jdbcType=TIMESTAMP}, 
            #{item.updateTime, jdbcType=TIMESTAMP}
        )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE applet_record_statis
        <set>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="recordType != null">
                record_type = #{recordType},
            </if>
            <if test="sourceType != null and sourceType != ''">
                source_type = #{sourceType},
            </if>
            <if test="clickCount != null">
                click_count = #{clickCount},
            </if>
            <if test="statisDate != null">
                statis_date = #{statisDate},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteWeekAppletRecordStatis">
        DELETE FROM applet_record_statis where statis_date = #{monday}
    </delete>

</mapper>
