<?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.AppletLinkClickDaoImpl">

    <resultMap type="com.pcloud.book.applet.entity.AppletLinkClick" id="AppletLinkClickMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="wechatUserId" column="wechat_user_id" jdbcType="INTEGER"/>
        <result property="typeId" column="type_id" jdbcType="INTEGER"/>
        <result property="fromId" column="from_id" jdbcType="INTEGER"/>
        <result property="recordType" column="record_type" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.applet_link_click(wechat_user_id, type_id, from_id, record_type, create_time, create_date)
        values (#{wechatUserId}, #{typeId}, #{fromId}, #{recordType}, NOW(),NOW())
    </insert>

    <select id="mapPVUV" parameterType="map" resultType="com.pcloud.book.applet.dto.PvuvDTO">
        SELECT
        from_id fromId,
        COUNT(1) pv,
--         COUNT(DISTINCT wechat_user_id,create_date) uv
        COUNT(DISTINCT wechat_user_id) uv
        FROM applet_link_click
        WHERE type_id = #{typeId}
        AND record_type = #{recordType}
        AND from_id in
        <foreach collection="list" item="item" index="index" separator="," close=")" open="(">
            #{item}
        </foreach>
        <if test="intervalDaya != null">
            AND create_date between DATE_SUB(CURDATE(),INTERVAL #{intervalDaya} DAY) and DATE_SUB(CURDATE(),INTERVAL 1 DAY)
        </if>
        GROUP BY from_id
    </select>

    <select id="mapCouponUseCount" parameterType="map" resultType="com.pcloud.book.applet.dto.PvuvDTO">
        SELECT
        from_id fromId,
        COUNT(DISTINCT wechat_user_id) count
        FROM applet_link_click
        WHERE type_id=2
        AND record_type=3
        AND from_id in
        <foreach collection="list" item="item" index="index" separator="," close=")" open="(">
            #{item}
        </foreach>
        GROUP BY from_id
    </select>


</mapper>