<?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">
		<result column="id_" property="id" />
		<result column="examination_basic_id_" property="examinationBasicId" />
		<result column="exam_room_id_" property="examRoomId" />
		<result column="student_id_" property="studentId" />
		<result column="classroom_switch_" property="classroomSwitch" typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler"/>
		<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_room_id_,student_id_,create_time_,update_time_,tenant_id_)
		VALUES(#{id},#{examinationBasicId},#{examRoomId},#{studentId},NOW(),NOW(),#{tenantId})
	</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_room_id_,student_id_,create_time_,update_time_,tenant_id_)
		VALUES
		<foreach collection="roomStudents" item="roomStudent" separator=",">
			(#{roomStudent.examinationBasicId},#{roomStudent.examRoomId},#{roomStudent.studentId},NOW(),NOW(),#{roomStudent.tenantId})
		</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="classroomSwitch != null">
				classroom_switch_ = #{classroomSwitch,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="id != null">
				id_ = #{id},
			</if>
			<if test="tenantId != null">
				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>

    <!-- 根据主键删除一条记录 -->
	<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 student_id_ IN
		<foreach collection="studentIds" item="studentId" separator="," open="(" close=")">
			#{studentId}
		</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_='AUDIT_PASS'
			<if test="organIds!=null">
				AND ersr.organ_id_ IN
				<foreach collection="organIds" item="organI" separator="," open="(" close=")">
					#{organI}
				</foreach>
			</if>
			<if test="examId!=null">
				AND er.examination_basic_id_ = 1
			</if>
			<if test="inRoom!=null and inRoom=0">
				AND ersr.id_ IS NULL
			</if>
			<if test="inRoom!=null and inRoom=1">
				AND ersr.exam_room_id_ =1
			</if>
		</where>
	</sql>

	<select id="findExamRoomStudents" resultMap="ExamRoomStudentRelationDto" parameterType="map">
		SELECT
			ersr.id_,
			ersr.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.student_id_ = er.student_id_ ON ersr.examination_basic_id_=er.examination_basic_id_
		<include refid="queryCondition"/>
		ORDER BY id_ <include refid="global.limit"/>
	</select>

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

	<select id="findStudentsWithExamRoom" resultMap="ExamRoomStudentRelation">
		SELECT id_, examination_basic_id_, exam_room_id_, student_id_ FROM exam_room_student_relation WHERE exam_room_id_=#{examRoomId}
	</select>
	<select id="getExamRoomStudentRelation" resultMap="ExamRoomStudentRelation">
		SELECT * FROM exam_room_student_relation
		WHERE examination_basic_id_ = #{basicId} AND exam_room_id_ = #{roomId} AND student_id_ = #{studentId} LIMIT 1
	</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>
</mapper>
