<?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.sclass.mapper.classroom.ClassroomActivestudentMapper">
    <!--更新活跃值排名-->
    <update id="updateActiveRank">
        UPDATE t_classroom_activestudent a LEFT JOIN (SELECT s.roomid,s.activenum,
        (SELECT COUNT(DISTINCT activenum)+1 FROM t_classroom_activestudent WHERE activenum>s.activenum)activerank
        FROM t_classroom_activestudent s WHERE s.roomid=#{roomid} ORDER BY s.activenum DESC)s2
        ON a.activenum=s2.activenum AND a.roomid=s2.roomid
        SET a.activerank=s2.activerank WHERE a.roomid=#{roomid}
    </update>
    <!--更新学生活跃值-->
    <update id="updateActiveNum">
        UPDATE t_classroom_activestudent SET activenum=activenum+1 WHERE roomid=#{roomid} AND studentid=#{studentid}
    </update>
    <!--批量更新学生活跃值-->
    <update id="batchUpdateActiveNum">
        UPDATE t_classroom_activestudent SET activenum=activenum+1 WHERE
        <foreach collection="students" item="s" open="(" close=")" separator="or">
            (roomid=${s.roomid} AND studentid=${s.studentid})
        </foreach>
    </update>
    <!--批量更新活跃值排名-->
    <update id="updateBatchActiveRank">
        <foreach collection="roomids" item="roomid" separator=";">
            UPDATE t_classroom_activestudent a LEFT JOIN (SELECT s.roomid,s.activenum,
            (SELECT COUNT(DISTINCT activenum)+1 FROM t_classroom_activestudent WHERE activenum>s.activenum)activerank
            FROM t_classroom_activestudent s WHERE s.roomid=${roomid} ORDER BY s.activenum DESC)s2
            ON a.activenum=s2.activenum AND a.roomid=s2.roomid
            SET a.activerank=s2.activerank WHERE a.roomid=${roomid}
        </foreach>
    </update>
    <!--添加学生课堂活跃度表信息-->
    <insert id="saveStuActive">
        insert into t_classroom_activestudent(roomid,studentid,createtime,schoolid)
        SELECT #{roomid},s.studentid,UNIX_TIMESTAMP(),#{schoolid} FROM t_class_student s WHERE s.classid=#{classid}
    </insert>
    <!--批量添加学生课堂活跃度-->
    <insert id="batchSaveStuActive">
        <foreach collection="rooms" item="r" separator=";">
            insert into t_classroom_activestudent(roomid,studentid,createtime,schoolid)
            SELECT ${r.roomid},s.studentid,UNIX_TIMESTAMP(),${r.schoolid} FROM t_class_student s WHERE
            s.classid=${r.classid}
        </foreach>
    </insert>
</mapper>
