<?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.mapper.AppletBannerRecordMapper">
    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletBannerRecord">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="banner_id" jdbcType="BIGINT" property="bannerId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="wechat_user_id" jdbcType="BIGINT" property="wechatUserId"/>
        <result column="serve_id" jdbcType="BIGINT" property="serveId"/>
        <result column="serve_type" jdbcType="TINYINT" property="serveType"/>
        <result column="jump_type" jdbcType="BOOLEAN" property="jumpType"/>
    </resultMap>
    <sql id="Base_Column_List">
    id, banner_id, create_time, update_time, wechat_user_id, serve_id, serve_type, jump_type
  </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from applet_banner_record
        where id = #{id,jdbcType=BIGINT}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from applet_banner_record
    where id = #{id,jdbcType=BIGINT}
  </delete>
    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.pcloud.book.applet.entity.AppletBannerRecord"
            useGeneratedKeys="true">
    insert into applet_banner_record (banner_id, create_time, update_time, 
      wechat_user_id, serve_id, serve_type, 
      jump_type, create_hour, create_date, location_type)
    values (#{bannerId,jdbcType=BIGINT}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, 
      #{wechatUserId,jdbcType=BIGINT}, #{serveId,jdbcType=BIGINT}, #{serveType,jdbcType=TINYINT}, 
      #{jumpType,jdbcType=BOOLEAN}, #{createHour}, #{createDate}, #{locationType})
  </insert>
    <insert id="insertSelective" keyColumn="id" keyProperty="id"
            parameterType="com.pcloud.book.applet.entity.AppletBannerRecord" useGeneratedKeys="true">
        insert into applet_banner_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="bannerId != null">
                banner_id,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="wechatUserId != null">
                wechat_user_id,
            </if>
            <if test="serveId != null">
                serve_id,
            </if>
            <if test="serveType != null">
                serve_type,
            </if>
            <if test="jumpType != null">
                jump_type,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="bannerId != null">
                #{bannerId,jdbcType=BIGINT},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="wechatUserId != null">
                #{wechatUserId,jdbcType=BIGINT},
            </if>
            <if test="serveId != null">
                #{serveId,jdbcType=BIGINT},
            </if>
            <if test="serveType != null">
                #{serveType,jdbcType=TINYINT},
            </if>
            <if test="jumpType != null">
                #{jumpType,jdbcType=BOOLEAN},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.applet.entity.AppletBannerRecord">
        update applet_banner_record
        <set>
            <if test="bannerId != null">
                banner_id = #{bannerId,jdbcType=BIGINT},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId,jdbcType=BIGINT},
            </if>
            <if test="serveId != null">
                serve_id = #{serveId,jdbcType=BIGINT},
            </if>
            <if test="serveType != null">
                serve_type = #{serveType,jdbcType=TINYINT},
            </if>
            <if test="jumpType != null">
                jump_type = #{jumpType,jdbcType=BOOLEAN},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.pcloud.book.applet.entity.AppletBannerRecord">
    update applet_banner_record
    set banner_id = #{bannerId,jdbcType=BIGINT},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      wechat_user_id = #{wechatUserId,jdbcType=BIGINT},
      serve_id = #{serveId,jdbcType=BIGINT},
      serve_type = #{serveType,jdbcType=TINYINT},
      jump_type = #{jumpType,jdbcType=BOOLEAN}
    where id = #{id,jdbcType=BIGINT}
  </update>
    <select id="getBannerPVUV" resultType="com.pcloud.book.applet.dto.AppletBannerStats">
        SELECT
        t.banner_id bannerId,
        count( 0 ) uv,
        SUM( t.p ) pv
        FROM
        ( SELECT banner_id, count( 0 ) p FROM applet_banner_record WHERE
        banner_id IN
        <foreach collection="bannerIds" close=")" item="item" open="(" separator=",">
            ${item}
        </foreach>
        GROUP BY banner_id, wechat_user_id ) t
        GROUP BY
        t.banner_id
    </select>


    <select id="getBannerRecordTrendByHour" parameterType="string" resultType="com.pcloud.book.applet.dto.AppletBannerTrendDTO">
        SELECT
        create_hour hour ,
        count(1) PV,
        COUNT(DISTINCT wechat_user_id) UV
        FROM
        `applet_banner_record`
        WHERE
        create_time >= #{startTime}
        AND
        create_time &lt; #{endTime}
        <if test="id != null">
            and banner_id = #{id}
        </if>
        and
        location_type = #{locationType}
        GROUP BY
        create_hour
    </select>


    <select id="getBannerRecordTrendByDay" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletBannerTrendDTO">
        SELECT
        create_date date ,
        count(1) PV,
        COUNT(DISTINCT wechat_user_id) UV
        FROM
        `applet_banner_record`
        WHERE
        create_time >= #{startDate}
        AND create_time &lt;= #{endDate}
        <if test="id != null">
            and banner_id = #{id}
        </if>
        and
        location_type = #{locationType}
        GROUP BY
        create_date
    </select>

</mapper>