<?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.keao.edu.user.dao.ExamRoomStudentRelationDao">
	
	<resultMap type="com.keao.edu.user.api.entity.ExamRoomStudentRelation" id="ExamRoomStudentRelation" extends="com.keao.edu.user.dao.ExamRoomDao.ExamRoom">
		<result column="id_" property="id" />
		<result column="examination_basic_id_" property="examinationBasicId" />
		<result column="exam_registration_id_" property="examRegistrationId" />
		<result column="exam_room_id_" property="examRoomId" />
		<result column="student_id_" property="studentId" />
		<result column="classroom_switch_" property="classroomSwitch"/>
		<result column="sign_in_time_" property="signInTime" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
	</resultMap>

	<resultMap id="ExamRoomStudentRelationDto" type="com.keao.edu.user.dto.ExamRoomStudentRelationDto" extends="ExamRoomStudentRelation">
		<association property="studentInfo" columnPrefix="sys_user_" resultMap="com.keao.edu.user.dao.SysUserDao.SysUser"/>
		<association property="subject" columnPrefix="subject_" resultMap="com.keao.edu.user.dao.SubjectDao.Subject"/>
		<association property="examRegistration" columnPrefix="regist_" resultMap="com.keao.edu.user.dao.ExamRegistrationDao.ExamRegistration"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExamRoomStudentRelation" >
		SELECT * FROM exam_room_student_relation WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ExamRoomStudentRelation">
		SELECT * FROM exam_room_student_relation ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.api.entity.ExamRoomStudentRelation" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_room_student_relation (id_,examination_basic_id_,exam_registration_id_,
		exam_room_id_,student_id_,create_time_,update_time_,tenant_id_,sign_in_time_)
		VALUES(#{id},#{examinationBasicId},#{examRegistrationId},#{examRoomId},
		#{studentId},NOW(),NOW(),#{tenantId},#{signInTime})
	</insert>

	<insert id="batchInsert" parameterType="com.keao.edu.user.api.entity.ExamRoomStudentRelation" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_room_student_relation (examination_basic_id_,exam_registration_id_,
		exam_room_id_,student_id_,create_time_,update_time_,tenant_id_,sign_in_time_)
		VALUES
		<foreach collection="roomStudents" item="roomStudent" separator=",">
			(#{roomStudent.examinationBasicId},#{roomStudent.examRegistrationId},
			#{roomStudent.examRoomId},#{roomStudent.studentId},NOW(),NOW(),#{roomStudent.tenantId},#{roomStudent.signInTime})
		</foreach>
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.keao.edu.user.api.entity.ExamRoomStudentRelation">
		UPDATE exam_room_student_relation
		<set>
			<if test="examinationBasicId != null">
				examination_basic_id_ = #{examinationBasicId},
			</if>
			<if test="signInTime != null">
				sign_in_time_ = #{signInTime},
			</if>
			<if test="examRegistrationId != null">
				exam_registration_id_ = #{examRegistrationId},
			</if>
			<if test="classroomSwitch != null">
				classroom_switch_ = #{classroomSwitch},
			</if>
			<if test="tenantId != null and tenantId != 0">
				tenant_id_ = #{tenantId},
			</if>
			<if test="examRoomId != null">
				exam_room_id_ = #{examRoomId},
			</if>
			<if test="studentId != null">
				student_id_ = #{studentId},
			</if>
				update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>
    <update id="switchClassRoom">
		UPDATE exam_room_student_relation SET classroom_switch_ = #{openFlag},update_time_ = NOW()
		WHERE examination_basic_id_ = #{examinationBasicId} AND student_id_ = #{studentId}
	</update>

	<update id="cleanSignInTime">
		UPDATE exam_room_student_relation SET sign_in_time_ = NULL,update_time_ = NOW()
		WHERE exam_registration_id_ = #{examRegistrationId}
	</update>

    <!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM exam_room_student_relation WHERE id_ = #{id} 
	</delete>
	<delete id="deleteStudentsFromExamRoom">
		DELETE FROM exam_room_student_relation WHERE exam_room_id_=#{examRoomId}
		AND exam_registration_id_ IN
		<foreach collection="registIds" item="registId" separator="," open="(" close=")">
			#{registId}
		</foreach>
	</delete>

	<delete id="deleteWithExamRooms">
		DELETE FROM exam_room_student_relation
		WHERE exam_room_id_ IN
		<foreach collection="examRoomIds" item="examRoomId" separator="," open="(" close=")">
			#{examRoomId}
		</foreach>
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ExamRoomStudentRelation" parameterType="map">
		SELECT * FROM exam_room_student_relation ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM exam_room_student_relation
	</select>

	<sql id="queryCondition">
		<where>
			er.status_ IN ('AUDIT_PASS', 'AUDIT_WAIT')
			<if test="organIds!=null">
				AND er.organ_id_ IN
				<foreach collection="organIds" item="organI" separator="," open="(" close=")">
					#{organI}
				</foreach>
			</if>
			<if test="examId!=null">
				AND er.examination_basic_id_ = #{examId}
			</if>
			<if test="inRoom!=null and inRoom==0">
				AND ersr.id_ IS NULL AND FIND_IN_SET(er.subject_id_, #{subjectIds})
			</if>
			<if test="inRoom!=null and inRoom==1">
				AND ersr.exam_room_id_ =#{examRoomId}
			</if>
			<if test="search!=null and search!=''">
				AND (er.student_id_ = #{search} OR su.real_name_ LIKE CONCAT('%', #{search}, '%'))
			</if>
		</where>
	</sql>

	<select id="findExamRoomStudents" resultMap="ExamRoomStudentRelationDto" parameterType="map">
		SELECT
			ersr.id_,
			ersr.exam_registration_id_,
			ersr.examination_basic_id_,
			er.id_ regist_id_,
			er.student_id_ regist_student_id_,
			er.subject_id_ regist_subject_id_,
			er.level_ regist_level_,
			er.create_time_ regist_create_time_
		FROM
			exam_registration er
		LEFT JOIN exam_room_student_relation ersr ON ersr.exam_registration_id_ = er.id_
		LEFT JOIN sys_user su ON er.student_id_=su.id_
		<include refid="queryCondition"/>
		ORDER BY er.id_ DESC <include refid="global.limit"/>
	</select>

	<select id="countExamRoomStudents" resultType="int">
		SELECT
			COUNT(er.id_)
		FROM
		exam_registration er
		LEFT JOIN exam_room_student_relation ersr ON ersr.exam_registration_id_ = er.id_
		LEFT JOIN sys_user su ON er.student_id_=su.id_
		<include refid="queryCondition"/>
	</select>

	<select id="findStudentsWithExamRoom" resultMap="ExamRoomStudentRelation">
		SELECT id_, exam_registration_id_, examination_basic_id_, exam_room_id_, student_id_
		FROM exam_room_student_relation WHERE exam_room_id_=#{examRoomId}
	</select>

	<select id="findStudentsWithExamRooms" resultMap="ExamRoomStudentRelation">
		SELECT id_, exam_registration_id_, examination_basic_id_, exam_room_id_, student_id_
		FROM exam_room_student_relation WHERE exam_room_id_ IN
		<foreach collection="examRoomIds" item="examRoomId" separator="," open="(" close=")">
			#{examRoomId}
		</foreach>
	</select>

	<select id="getExamRoomStudentRelations" resultMap="ExamRoomStudentRelation">
		SELECT * FROM exam_room_student_relation
		WHERE examination_basic_id_ = #{basicId}
		<if test="roomId!=null">
			AND exam_room_id_ = #{roomId}
		</if>
		<if test="studentId">
			AND student_id_ = #{studentId}
		</if>
	</select>
    <select id="getStudentExamRoom" resultMap="ExamRoomStudentRelation">
		SELECT * FROM exam_room_student_relation WHERE exam_registration_id_ = #{registId}
    </select>
	<select id="findByStudentIdAndRoomId" resultMap="ExamRoomStudentRelation">
		SELECT ersr.* FROM exam_room_student_relation ersr
		WHERE ersr.exam_room_id_ = #{roomId} AND ersr.student_id_ = #{currentUserId} LIMIT 1
	</select>
	<resultMap id="RoomStudentListDtoMap" type="com.keao.edu.user.dto.RoomStudentListDto">
		<result property="subjectName" column="subject_name_"/>
		<result property="examRegistrationId" column="exam_registration_id_"/>
		<result property="studentId" column="student_id_"/>
		<result property="level" column="level_"/>
		<result property="realName" column="real_name_"/>
		<result property="examRoomId" column="exam_room_id_"/>
		<result property="signInTime" column="sign_in_time_"/>
		<result property="examRoomStudentRelationId" column="exam_room_student_relation_id_"/>
		<result property="finishedExam" column="is_finished_exam_"/>
	</resultMap>
	<select id="queryStudentList" resultMap="RoomStudentListDtoMap">
		SELECT ersr.id_ exam_room_student_relation_id_,ersr.exam_registration_id_,su.real_name_,ersr.sign_in_time_,
		s.name_ subject_name_,ec.level_,ersr.exam_room_id_,ersr.student_id_,ser.is_finished_exam_
		FROM exam_room_student_relation ersr
		LEFT JOIN exam_certification ec ON ec.exam_registration_id_ = ersr.exam_registration_id_
		LEFT JOIN `subject` s ON s.id_ = ec.subject_id_
		LEFT JOIN sys_user su ON ersr.student_id_ = su.id_
		LEFT JOIN student_exam_result ser ON ser.exam_registration_id_ = ersr.exam_registration_id_
		WHERE ersr.exam_room_id_ = #{roomId}
		ORDER BY ersr.sign_in_time_ IS NULL,ser.is_finished_exam_ ASC,ersr.sign_in_time_ ASC
	</select>
    <select id="querySignTotalNum" resultType="java.lang.Integer">
		SELECT COUNT(ersr.id_) FROM exam_room_student_relation ersr
		WHERE ersr.exam_room_id_ = #{roomId} AND ersr.sign_in_time_ IS NOT NULL
	</select>
    <select id="queryNoSignTotalNum" resultType="java.lang.Integer">
		SELECT COUNT(ersr.id_) FROM exam_room_student_relation ersr
		WHERE ersr.exam_room_id_ = #{roomId} AND ersr.sign_in_time_ IS NULL
	</select>
	<select id="querySurplusNum" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT ersr.id_) FROM exam_room_student_relation ersr
		LEFT JOIN student_exam_result ser ON ersr.exam_registration_id_ = ser.exam_registration_id_
		WHERE ersr.exam_room_id_ = #{roomId} AND ersr.sign_in_time_ IS NOT NULL AND ser.is_finished_exam_ IN (1,2,3)
	</select>
	<select id="sumWaitNum" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT ersr.id_)
		FROM exam_room_student_relation ersr
		LEFT JOIN student_exam_result ser ON ser.exam_registration_id_ = ersr.exam_registration_id_
		WHERE ersr.exam_room_id_ = #{examRoomId} AND ersr.sign_in_time_ IS NOT NULL
		AND ser.is_finished_exam_ IN (0,1,2) AND ersr.student_id_ != #{studentId}
		<if test="signInTime != null and signInTime != ''">
			AND ersr.sign_in_time_ &lt; #{signInTime}
		</if>
	</select>
    <select id="getStudentIds" resultType="java.lang.String">
		SELECT GROUP_CONCAT(DISTINCT student_id_) FROM exam_room_student_relation WHERE exam_room_id_ = #{examRoomId}
	</select>
	<select id="getStuRegistrationMap" resultType="java.util.Map">
		SELECT student_id_ 'key',GROUP_CONCAT(exam_registration_id_) 'value' FROM exam_room_student_relation
		WHERE exam_room_id_ = #{examRoomId}
		GROUP BY student_id_
	</select>

    <select id="getNoSendExamPlanRooms" resultMap="ExamRoomStudentRelation">
		SELECT
			*
		FROM
			exam_room_student_relation ersr
			LEFT JOIN exam_room er ON ersr.exam_room_id_ = er.id_
		WHERE er.exam_plan_push_flag_=0
			AND ersr.examination_basic_id_ = #{examId}
			<if test="organIds!=null">
				AND er.organ_id_ IN
				<foreach collection="organIds" item="organId" open="(" close=")" separator=",">
					#{organId}
				</foreach>
			</if>
	</select>

	<select id="getTomorrowExamStudents" resultMap="ExamRoomStudentRelation">
		SELECT
			*
		FROM
			exam_room_student_relation ersr
			LEFT JOIN exam_room er ON ersr.exam_room_id_ = er.id_
		WHERE DATE_FORMAT(er.exam_start_time_,'%Y-%m-%d') = #{day}
	</select>
    <select id="getStudentNumMap" resultType="java.util.Map">
		SELECT ersr.exam_room_id_ 'key',COUNT(DISTINCT ersr.student_id_) 'value'
		FROM exam_room_student_relation ersr
		WHERE ersr.exam_room_id_ IN
		<foreach collection="roomIds" open="(" close=")" separator="," item="item">
			#{item}
		</foreach>
		GROUP BY ersr.exam_room_id_
	</select>
	<select id="countSignInNum" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT id_) FROM student_exam_result WHERE is_finished_exam_ = 2 AND exam_room_id_ = #{examRoomId}
	</select>
</mapper>
