<?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.xhkjedu.sstudy.mapper.recite.ReciteStudentMapper">
    <insert id="saveBatch" parameterType="java.util.List">
        INSERT INTO t_recite_student (reciteid,classid,studentid,createid,createtime,schoolid)
        VALUES
        <foreach collection ="list" item="r" index= "index" separator =",">
             (#{r.reciteid},#{r.classid},#{r.studentid},#{r.createid},#{r.createtime},#{r.schoolid})
        </foreach>
    </insert>

    <!-- 学生背诵列表 -->
    <select id="listStuReceites" resultType="com.xhkjedu.sstudy.vo.recite.ReciteStudentVo">
        SELECT rs.reciteid,r.recitetitle,r.subjectid,s.subjectname,r.createid,
        u.username AS createname,rg.starttime,rg.stoptime,rs.rsstate,r.recitecont
        FROM t_recite_student rs LEFT JOIN t_recite r ON rs.reciteid=r.reciteid
        LEFT JOIN t_recite_group rg ON rs.reciteid=rg.reciteid AND rg.classid=rs.classid
        LEFT JOIN t_subject s ON r.subjectid = s.subjectid
        LEFT JOIN t_user u ON r.createid=u.userid
        WHERE rs.studentid=#{rs.studentid} AND rg.starttime&lt;=UNIX_TIMESTAMP()
        <if test="rs.rsstate==1">
            and rs.rsstate=0
        </if>
        <if test="rs.rsstate==2">
            and rs.rsstate!=0
        </if>
        <if test="rs.recitetitle!=null and rs.recitetitle!=''">
            and rs.recitetitle like '%${rs.recitetitle}%'
        </if>
        GROUP BY rs.reciteid ORDER BY rg.stoptime desc limit #{rs.page},#{rs.pageSize}
    </select>

    <!-- 保存学生背诵 -->
    <update id="updateStudRecite" parameterType="com.xhkjedu.sstudy.vo.recite.ReciteStudentVo">
        UPDATE t_recite_student SET rsstate=#{rs.rsstate},starttime=#{rs.starttime},stoptime=#{rs.stoptime},
        costtime=#{rs.costtime},userscore=#{rs.userscore},
        answerpath=#{rs.answerpath},answertxt=#{rs.answertxt}
        WHERE reciteid=#{rs.reciteid} and studentid=#{rs.studentid}
    </update>

    <!-- 根据rsid获取学生背诵内容 -->
    <select id="getReciteStuByRsid" resultType="com.xhkjedu.sstudy.vo.recite.ReciteStudentVo">
        SELECT rs.reciteid,rs.rsstate,rs.starttime,rs.stoptime,rs.costtime,rs.userscore,
        rs.answerpath,rs.answertxt,r.recitetitle,r.recitecont
        FROM t_recite_student rs LEFT JOIN t_recite r ON rs.reciteid=r.reciteid
        WHERE rs.studentid=#{studentid} and rs.reciteid=#{reciteid} limit 1
    </select>

    <!-- 班级背诵统计 -->
    <select id="listRecitesClassForAnalyze" resultType="java.util.Map">
        SELECT rg.classid,c.classname,c.gradeid,
        rs.rsid,rs.studentid,u.username,u.headpic,rs.userscore,rs.answerpath,rs.rsstate,rs.stoptime,rs.answertxt
        FROM t_recite_group rg LEFT JOIN t_class c ON rg.classid=c.classid
        LEFT JOIN t_recite_student rs ON rs.reciteid=rg.reciteid AND rs.classid=rg.classid
        LEFT JOIN t_user u ON rs.studentid=u.userid
        WHERE rg.reciteid=#{reciteid}
         <if test="classid!=null"> and rg.classid=#{classid}</if>
         order by c.classtype,c.classorder,rs.userscore desc
    </select>

    <!--//////////////////////////////////////////////学生WEB端//////////////////////////////////////////////-->
    <!-- 学生背诵列表 -->
    <select id="listReciteForStuWeb" resultType="com.xhkjedu.sstudy.vo.recite.ReciteStudentVo">
        SELECT rs.reciteid,r.recitetitle,r.recitecont,r.subjectid,s.subjectname,r.createid,u.username AS createname
        ,rg.starttime,rg.stoptime,rs.stoptime committime,rs.rsstate,rs.userscore,rs.answerpath,rs.answertxt
        FROM t_recite_student rs LEFT JOIN t_recite r ON rs.reciteid=r.reciteid
        LEFT JOIN t_recite_group rg ON rs.reciteid=rg.reciteid AND rg.classid=rs.classid
        LEFT JOIN t_subject s ON r.subjectid = s.subjectid
        LEFT JOIN t_user u ON r.createid=u.userid
        WHERE rs.studentid=#{rs.studentid} AND rg.starttime&lt;=UNIX_TIMESTAMP()
        <if test="rs.rsstate==1">
            and rs.rsstate=0
        </if>
        <if test="rs.rsstate==2">
            and rs.rsstate!=0
        </if>
        <if test="rs.subjectid!=null and rs.subjectid!='0'.toString() and rs.subjectid!=''">
            and r.subjectid=#{rs.subjectid}
        </if>
        GROUP BY rs.reciteid ORDER BY rs.rsstate,rg.stoptime desc
    </select>
</mapper>
