<?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.riddle.dao.impl.RiddleClickRecordDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.riddle.entity.RiddleClickRecord" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="wx_group_id" property="wxGroupId" jdbcType="VARCHAR" />
    <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT" />
    <result column="click_type" property="clickType" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, wx_group_id, wechat_user_id, click_type, create_time
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.riddle.entity.RiddleClickRecord" useGeneratedKeys="true" keyProperty="id">
    insert into riddle_click_record (wx_group_id, wechat_user_id,
      click_type, create_time)
    values (#{wxGroupId,jdbcType=VARCHAR}, #{wechatUserId,jdbcType=BIGINT},
      #{clickType,jdbcType=INTEGER}, NOW())
  </insert>

  <select id="getCountByType" parameterType="map" resultType="integer">
    SELECT
        COUNT(1)
    FROM
        riddle_click_record
    WHERE
        wx_group_id = #{wxGroupId}
    AND wechat_user_id = #{wechatUserId}
    AND click_type = #{type}
  </select>

  <select id="getUserCountByType" parameterType="map" resultType="integer">
  SELECT
      COUNT(DISTINCT wechat_user_id)
  FROM
      riddle_click_record
  WHERE
      wx_group_id = #{wxGroupId}
  AND click_type = #{clickType}
  </select>

</mapper>