<?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.RiddleRecordDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.riddle.entity.RiddleRecord" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="riddle_id" property="riddleId" jdbcType="BIGINT" />
    <result column="message_content" property="messageContent" jdbcType="VARCHAR" />
    <result column="wx_group_id" property="wxGroupId" jdbcType="VARCHAR" />
    <result column="wx_user_id" property="wxUserId" jdbcType="VARCHAR" />
    <result column="correct" property="correct" jdbcType="BIT" />
    <result column="status" property="status" jdbcType="TINYINT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="finish_count" property="finishCount" jdbcType="INTEGER" />
  </resultMap>
 
  <sql id="Base_Column_List" >
    id, riddle_id, message_content, wx_group_id, wx_user_id, correct, status, create_time, finish_count
  </sql>


  <insert id="insert" parameterType="com.pcloud.book.riddle.entity.RiddleRecord" useGeneratedKeys="true" keyProperty="id">
    insert into riddle_record (riddle_id, message_content,
      wx_group_id, wx_user_id, correct, status,
      create_time, finish_count)
    values (#{riddleId,jdbcType=BIGINT}, #{messageContent,jdbcType=VARCHAR},
      #{wxGroupId,jdbcType=VARCHAR}, #{wxUserId,jdbcType=VARCHAR}, #{correct,jdbcType=BIT}, #{status},
      NOW(), #{finishCount})
  </insert>

  <select id="getLastRecordByWxGroupId" resultMap="BaseResultMap" parameterType="map">
    SELECT
    <include refid="Base_Column_List"/>
    FROM riddle_record
    WHERE wx_group_id = #{wxGroupId}
      <if test="status != null">
          AND `status`= #{status}
      </if>
    ORDER BY
    create_time DESC, id DESC
    LIMIT 1
  </select>

  <select id="getCorrectCountByGroup" parameterType="map" resultType="Integer">
    SELECT
      COUNT(DISTINCT riddle_id,finish_count)
    FROM
        riddle_record
    WHERE
        wx_group_id = #{wxGroupId}
        AND correct = 1
      <if test="finishCount != null">
          AND finish_count = #{finishCount}
      </if>
  </select>

  <select id="getParticipateCount" parameterType="String" resultType="com.pcloud.book.riddle.dto.GroupRiddleDTO">
    SELECT
	COUNT(DISTINCT wx_user_id) userNum,
	COUNT(1) participateCount
    FROM
        riddle_record
    WHERE
        wx_group_id = #{wxGroupId}
    AND correct IS NOT NULL
  </select>

  <select id="getGroupRanking" parameterType="string" resultType="integer">
    SELECT
	    b.rownum
    FROM
        (
            SELECT
                t.wx_group_id,
                @rownum := @rownum + 1 AS rownum
            FROM
                (SELECT @rownum := 0) r,
                (
                    SELECT
                        wx_group_id
                    FROM
                        riddle_record
                    WHERE
                        correct = 1
                    GROUP BY
                        wx_group_id
                    ORDER BY
                        COUNT(DISTINCT riddle_id,finish_count) DESC,
                        create_time DESC
                ) AS t
        ) AS b
    WHERE
        b.wx_group_id = #{wxGroupId}
  </select>

    <select id="getUserRanking" parameterType="map" resultType="com.pcloud.book.riddle.dto.UserRankingDTO">
        SELECT
            COUNT(DISTINCT riddle_id,finish_count) count,
            wx_user_id wxUserId
        FROM
            riddle_record
        WHERE
            wx_group_id = #{wxGroupId}
        AND correct = 1
        AND create_time > #{createTime}
        GROUP BY
            wx_user_id
        ORDER BY
            COUNT(DISTINCT riddle_id,finish_count) DESC
    </select>

    <select id="getCorrectCountByTime" resultType="integer" parameterType="map">
         SELECT
          COUNT(DISTINCT riddle_id,finish_count)
        FROM
            riddle_record
        WHERE
            wx_group_id = #{wxGroupId}
            AND correct = 1
            AND create_time > #{createTime}
    </select>

    <!--连答对5题过关次数-->
    <select id="getOverTime" resultType="integer" parameterType="string">
        SELECT
            COUNT(1)
        FROM
            riddle_record
        WHERE
            wx_group_id = #{wxGroupId}
        AND `status` = 3
    </select>

    <select id="getCorrectGroupCount" resultType="integer">
        SELECT
            COUNT(DISTINCT wx_group_id)
        FROM
            riddle_record
        WHERE
            correct = 1
    </select>

</mapper>