<?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.qxgmat.data.relation.UserReportRelationMapper">
  <resultMap id="IdMap" type="com.qxgmat.data.dao.entity.UserReport">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
  </resultMap>
  <resultMap id="limitMap" type="com.qxgmat.data.relation.entity.UserReportLimitRelation">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="user_number" jdbcType="INTEGER" property="user_number" />
    <id column="user_correct" jdbcType="INTEGER" property="user_correct" />
  </resultMap>
  <resultMap id="studyMap" type="com.qxgmat.data.relation.entity.UserStudyStatRelation">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="user_number" jdbcType="INTEGER" property="user_number" />
    <id column="user_time" jdbcType="INTEGER" property="user_time" />
  </resultMap>
  <sql id="Id_Column_List">
    <!--
      WARNING - @mbg.generated
    -->
    ur.`id`
  </sql>

  <!--累加做题记录-->
  <update id="accumulation">
    UPDATE `user_report`
    <trim prefix="set" suffixOverrides=",">
      `user_number`=`user_number`+#{number, jdbcType=INTEGER},
      `user_time`=`user_time`+#{time, jdbcType=INTEGER},
      `user_correct`=`user_correct`+#{correct, jdbcType=INTEGER},
    </trim>
    WHERE `id` = #{id, jdbcType=VARCHAR}
  </update>

  <!--
    用户完成度最高的最后一次
    https://blog.csdn.net/t_1007/article/details/52369261
  -->
  <select id="listFinishLast" resultMap="IdMap">
    select
      SUBSTRING_INDEX(GROUP_CONCAT(ur.`id` ORDER BY ur.`user_number` desc, ur.`create_time` desc),',',1)
    from `user_report` ur
    where
    up.`paper_id` IN
    <foreach collection="paperIds" item="item" index="index" open="(" close=")" separator=",">
      #{id}
    </foreach>

    <!--<if test="startTime != null">-->
      <!--and up.`createTime` &gt; #{startTime,jdbcType=VARCHAR}-->
    <!--</if>-->
    <!--<if test="endTime != null">-->
      <!--and up.`createTime` &lt; #{endTime,jdbcType=VARCHAR}-->
    <!--</if>-->
    group by up.`paper_id`
  </select>

  <!--
    用户最后一次
    https://blog.csdn.net/t_1007/article/details/52369261
  -->
  <select id="listLast" resultMap="IdMap">
    select
    SUBSTRING_INDEX(GROUP_CONCAT(ur.`id` ORDER BY ur.`create_time` desc),',',1)
    from `user_report` ur
    where
    up.`paper_id` IN
    <foreach collection="paperIds" item="item" index="index" open="(" close=")" separator=",">
      #{id}
    </foreach>

    <!--<if test="startTime != null">-->
    <!--and up.`createTime` &gt; #{startTime,jdbcType=VARCHAR}-->
    <!--</if>-->
    <!--<if test="endTime != null">-->
    <!--and up.`createTime` &lt; #{endTime,jdbcType=VARCHAR}-->
    <!--</if>-->
    group by up.`paper_id`
  </select>

  <!--
    用户限时完成的统计记录
  -->
  <select id="statLimit" resultMap="limitMap">
    select
      sum(`user_number`) as `user_number`, sum(`user_correct`) as `user_correct`
    from `user_report`
    where
      `user_time` &lt; `time`
    and `user_number` = `question_number`
    and `module` = #{module,jdbcType=VARCHAR}
    and `module_id` = #{module_id,jdbcType=INTEGER}
  </select>


  <!--
    用户学习记录统计，分学科
  -->
  <select id="statByModule" resultMap="studyMap">
    select
    sum(`user_number`) as `user_number`, sum(`user_time`) as `user_time`
    from `user_report`
    where
    `user_id` = #{userId,jdbcType=VARCHAR}
    and `module` =
    and `user_number` = `question_number`
    and `module` = #{module,jdbcType=VARCHAR}
    and `module_id` = #{module_id,jdbcType=INTEGER}
  </select>
</mapper>