<?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.UserPaperRelationMapper">
  <resultMap id="IdMap" type="com.qxgmat.data.dao.entity.UserPaper">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
  </resultMap>
  <sql id="Id_Column_List">
    <!--
      WARNING - @mbg.generated
    -->
    up.`id`
  </sql>

  <!--累加做题记录-->
  <update id="accumulation">
    UPDATE `user_paper`
    <trim prefix="set" suffixOverrides=",">
      `total_number`=`total_number`+#{number, jdbcType=INTEGER},
      `total_time`=`total_time`+#{time, jdbcType=INTEGER},
      `total_correct`=`total_correct`+#{correct, jdbcType=INTEGER},
      `times` = `times`+#{times, jdbcType=INTEGER},
    </trim>
    WHERE `id` = #{id, jdbcType=VARCHAR}
  </update>

  <!--
    用户预习作业列表: 后台
    https://blog.csdn.net/t_1007/article/details/52369261
  -->
  <select id="listHomeworkPreviewAdmin" resultMap="IdMap">
    select
    <include refid="Id_Column_List" />
    from `user_paper` up
    left join `homework_preview` hp on hp.`id` = up.`module_id`
      and up.`module` = "homework_preview"
      <if test="previewId != null">
        and hp.`id` = #{previewId,jdbcType=VARCHAR}
      </if>
      <if test="category != null">
        and hp.`category` = #{category,jdbcType=VARCHAR}
      </if>
    where
    hp.`id` != null
    <if test="userId != null">
      and up.`user_id` = #{userId,jdbcType=VARCHAR}
    </if>
    <if test="startTime != null">
      and up.`createTime` &gt; #{startTime,jdbcType=VARCHAR}
    </if>
    <if test="endTime != null">
      and up.`createTime` &lt; #{endTime,jdbcType=VARCHAR}
    </if>
  </select>

  <!--
    用户预习作业列表: 用户端
  -->
  <select id="listHomeworkPreview" resultMap="IdMap">
    select
    <include refid="Id_Column_List" />
    from `user_paper` up
    left join `homework_preview` hp on hp.`id` = up.`module_id`
      and up.`module` = "homework_preview"
      <if test="category != null">
        and hp.`category` = #{category,jdbcType=VARCHAR}
      </if>
      <if test="endTime != null">
        and hp.`endTime` &lt; #{endTime,jdbcType=VARCHAR}
      </if>
    where
    hp.`id` != null
    <if test="userId != null">
      and up.`user_id` = #{userId,jdbcType=VARCHAR}
    </if>
    <if test="finish != null">
      <if test="finish == true">
      and up.`number` > 1
      </if>
      <if test="finish == false">
        and up.`number` = 0
      </if>>
    </if>
  </select>

  <!--
    用户预习作业Top列表: 用户端
  -->
  <select id="listHomeworkPreviewGroupTop" resultMap="IdMap">
    select
    <include refid="Id_Column_List" />
    from `user_paper` up
    where
    hp.`id` != null
    and up.`module` = "homework_preview"
    <if test="userId != null">
      and up.`user_id` = #{userId,jdbcType=VARCHAR}
    </if>
    and ( select count(up1.id)
        from `user_paper` up1
        where
        up.`module_id` = up1.`module_id`
        and up.`module` = "homework_preview"
        and up1.create_time &gt; up.create_time) &lt; ${top}
  </select>

  <!--
    用户练习-练习册列表: 用户端
  -->
  <select id="listExercisePaper" resultMap="IdMap">
    select
    <include refid="Id_Column_List" />
    from `user_paper` up
    left join `exercise_paper` hp on hp.`id` = up.`module_id` and ep.`status` = 1
    and up.`module` = "homework_preview"
    <if test="category != null">
      and hp.`category` = #{category,jdbcType=VARCHAR}
    </if>
    <if test="endTime != null">
      and hp.`endTime` &lt; #{endTime,jdbcType=VARCHAR}
    </if>
    where
    hp.`id` != null
    <if test="userId != null">
      and up.`user_id` = #{userId,jdbcType=VARCHAR}
    </if>
    <if test="finish != null">
      <if test="finish == true">
        and up.`number` > 1
      </if>
      <if test="finish == false">
        and up.`number` = 0
      </if>>
    </if>
  </select>
</mapper>