<?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.group.dao.impl.AppTouchRecordDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.group.entity.AppTouchRecord">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="qrcode_id" property="qrcodeId" jdbcType="BIGINT"/>
        <result column="weixin_group_id" property="weixinGroupId" jdbcType="VARCHAR"/>
        <result column="serve_id" property="serveId" jdbcType="BIGINT"/>
        <result column="serve_type" property="serveType" jdbcType="VARCHAR"/>
        <result column="touch_type" property="touchType" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>


    <sql id="Base_Column_List">
        id, book_group_id, classify_id, qrcode_id, weixin_group_id, serve_id, serve_type, touch_type, create_time
    </sql>

    <insert id="insert" parameterType="com.pcloud.book.group.entity.AppTouchRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into app_touch_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            book_group_id,
            classify_id,
            qrcode_id,
            weixin_group_id,
            serve_id,
            serve_type,
            touch_type,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{bookGroupId,jdbcType=BIGINT},
            #{classifyId,jdbcType=BIGINT},
            #{qrcodeId,jdbcType=BIGINT},
            #{weixinGroupId,jdbcType=VARCHAR},
            #{serveId,jdbcType=BIGINT},
            #{serveType,jdbcType=VARCHAR},
            #{touchType,jdbcType=INTEGER},
            NOW(),
        </trim>
    </insert>

    <!--获取应用统计数据-->
    <select id="getAppTouchStatistics" parameterType="map" resultType="com.pcloud.book.group.dto.AppStatisticsDTO">
        SELECT
        t.serve_id serveId,
        t.serve_type serveType,
        COUNT(1) pushCount
        FROM
        app_touch_record t
        WHERE 1=1
        <if test="bookGroupId!=null">
            AND t.book_group_id =#{bookGroupId}
        </if>
        <if test="qrcodeId!=null">
            AND t.qrcode_id =#{qrcodeId}
        </if>
        GROUP BY
        t.serve_id
        order by t.create_time desc
    </select>

    <select id="getCountByBokkGroupId" parameterType="map" resultType="long">
        select count(1) from app_touch_record where book_group_id =#{bookGroupId} and serve_type = #{type}
    </select>
</mapper>