<?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.ExamRoomDao">
	
	<resultMap type="com.keao.edu.user.api.entity.ExamRoom" id="ExamRoom">
		<result column="id_" property="id" />
		<result column="examination_basic_id_" property="examinationBasicId"/>
		<result column="exam_mode_" property="examMode" typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler"/>
		<result column="exam_location_id_" property="examLocationId" />
		<result column="subject_id_list_" property="subjectIdList" />
		<result column="subject_name_list_" property="subjectNameList"/>
		<result column="main_teacher_user_id_" property="mainTeacherUserId" />
		<result column="main_teacher_user_name_" property="mainTeacherName"/>
		<result column="assistant_teacher_user_id_list_" property="assistantTeacherUserIdList" />
		<result column="assistant_teacher_user_name_list_" property="assistantTeacherUserNameList"/>
		<result column="exam_start_time_" property="examStartTime" />
		<result column="exam_end_time_" property="examEndTime" />
		<result column="del_flag_" property="delFlag" />
		<result column="organ_id_" property="organId"/>
		<result column="open_flag_" property="openFlag"/>
		<result column="exam_plan_push_flag_" property="examPlanPushFlag"/>
		<result column="exam_room_student_num_" property="examRoomStudentNum"/>
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
		<result column="exam_flag_" property="examFlag" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExamRoom" >
		SELECT * FROM exam_room WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ExamRoom">
		SELECT * FROM exam_room ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.api.entity.ExamRoom" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_room (id_,examination_basic_id_,exam_mode_,exam_location_id_,subject_id_list_,subject_name_list_,
		main_teacher_user_id_,main_teacher_user_name_,assistant_teacher_user_id_list_,assistant_teacher_user_name_list_,
		exam_start_time_,exam_end_time_,del_flag_,organ_id_,exam_plan_push_flag_,
		exam_room_student_num_,create_time_,update_time_,tenant_id_,exam_flag_)
		VALUES(#{id},#{examinationBasicId},#{examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},#{examLocationId},
		#{subjectIdList},#{subjectNameList},#{mainTeacherUserId},#{mainTeacherName},#{assistantTeacherUserIdList},#{assistantTeacherUserNameList},
		#{examStartTime},#{examEndTime},#{delFlag},#{organId},#{examPlanPushFlag},
		#{examRoomStudentNum},NOW(),NOW(),#{tenantId},#{examFlag})
	</insert>

	<insert id="batchInsert" parameterType="com.keao.edu.user.api.entity.ExamRoom" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_room (examination_basic_id_,exam_mode_,exam_location_id_,subject_id_list_,subject_name_list_,
		main_teacher_user_id_,main_teacher_user_name_,assistant_teacher_user_id_list_,assistant_teacher_user_name_list_,
		exam_start_time_,exam_end_time_,del_flag_,organ_id_,exam_plan_push_flag_,
		exam_room_student_num_,create_time_,update_time_,tenant_id_)
		VALUES
		<foreach collection="examRooms" item="examRoom" separator=",">
			(#{examRoom.examinationBasicId},#{examRoom.examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},#{examRoom.examLocationId},
			#{examRoom.subjectIdList},#{examRoom.subjectNameList},
			#{examRoom.mainTeacherUserId},#{examRoom.mainTeacherName},#{examRoom.assistantTeacherUserIdList},#{examRoom.assistantTeacherUserNameList},
			#{examRoom.examStartTime},#{examRoom.examEndTime},#{examRoom.delFlag},#{examRoom.organId},#{examRoom.examPlanPushFlag},
			#{examRoom.examRoomStudentNum},NOW(),NOW(),#{examRoom.tenantId})
		</foreach>
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.keao.edu.user.api.entity.ExamRoom">
		UPDATE exam_room
		<set>
			<if test="examFlag != null">
				exam_flag_ = #{examFlag},
			</if>
			<if test="delFlag != null">
				del_flag_ = #{delFlag},
			</if>
			<if test="openFlag != null">
				open_flag_ = #{openFlag},
			</if>
			<if test="examinationBasicId != null">
				examination_basic_id_ = #{examinationBasicId},
			</if>
			<if test="examStartTime != null">
				exam_start_time_ = #{examStartTime},
			</if>
			<if test="examEndTime != null">
				exam_end_time_ = #{examEndTime},
			</if>
			<if test="subjectIdList != null">
				subject_id_list_ = #{subjectIdList},
			</if>
			<if test="subjectNameList != null">
				subject_name_list_ = #{subjectNameList},
			</if>
			<if test="tenantId != null and tenantId != 0">
				tenant_id_ = #{tenantId},
			</if>
			<if test="examMode != null">
				exam_mode_ = #{examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			exam_location_id_ = #{examLocationId},
			<if test="mainTeacherUserId != null">
				main_teacher_user_id_ = #{mainTeacherUserId},
			</if>
			<if test="mainTeacherName != null">
				main_teacher_user_name_ = #{mainTeacherName},
			</if>
			<if test="assistantTeacherUserIdList != null">
				assistant_teacher_user_id_list_ = #{assistantTeacherUserIdList},
			</if>
			<if test="assistantTeacherUserNameList != null">
				assistant_teacher_user_name_list_ = #{assistantTeacherUserNameList},
			</if>
			<if test="organId != null">
				organ_id_ = #{organId},
			</if>
			<if test="examPlanPushFlag != null">
				exam_plan_push_flag_ = #{examPlanPushFlag},
			</if>
			<if test="examRoomStudentNum!=null">
				exam_room_student_num_=#{examRoomStudentNum},
			</if>
				update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>

	<!-- 根据主键查询一条记录 -->
	<update id="batchUpdate" parameterType="com.keao.edu.user.api.entity.ExamRoom">
		<foreach collection="examRooms" item="er" separator=";">
			UPDATE exam_room
			<set>
				<if test="er.delFlag != null">
					del_flag_ = #{er.delFlag},
				</if>
				<if test="er.openFlag != null">
					open_flag_ = #{er.openFlag},
				</if>
				<if test="er.examinationBasicId != null">
					examination_basic_id_ = #{er.examinationBasicId},
				</if>
				<if test="er.examStartTime != null">
					exam_start_time_ = #{er.examStartTime},
				</if>
				<if test="er.examEndTime != null">
					exam_end_time_ = #{er.examEndTime},
				</if>
				<if test="er.subjectIdList != null">
					subject_id_list_ = #{er.subjectIdList},
				</if>
				<if test="er.subjectNameList != null">
					subject_name_list_ = #{er.subjectNameList},
				</if>
				<if test="er.tenantId != null and er.tenantId != 0">
					tenant_id_ = #{er.tenantId},
				</if>
				<if test="er.examMode != null">
					exam_mode_ = #{er.examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
				</if>
				exam_location_id_ = #{er.examLocationId},
				<if test="er.mainTeacherUserId != null">
					main_teacher_user_id_ = #{er.mainTeacherUserId},
				</if>
				<if test="er.mainTeacherName != null">
					main_teacher_user_name_ = #{er.mainTeacherName},
				</if>
				<if test="er.assistantTeacherUserIdList != null">
					assistant_teacher_user_id_list_ = #{er.assistantTeacherUserIdList},
				</if>
				<if test="er.assistantTeacherUserNameList != null">
					assistant_teacher_user_name_list_ = #{er.assistantTeacherUserNameList},
				</if>
				<if test="er.organId != null">
					organ_id_ = #{er.organId},
				</if>
				<if test="er.examPlanPushFlag != null">
					exam_plan_push_flag_ = #{er.examPlanPushFlag},
				</if>
				<if test="er.examRoomStudentNum!=null">
					exam_room_student_num_=#{er.examRoomStudentNum},
				</if>
				update_time_ = NOW()
			</set> WHERE id_ = #{er.id}
		</foreach>
	</update>

	<update id="updateSL" parameterType="com.keao.edu.user.api.entity.ExamRoom">
		UPDATE exam_room
		<set>
			<if test="delFlag != null">
				del_flag_ = #{delFlag},
			</if>
			<if test="examinationBasicId != null">
				examination_basic_id_ = #{examinationBasicId},
			</if>
			<if test="examStartTime != null">
				exam_start_time_ = #{examStartTime},
			</if>
			<if test="examEndTime != null">
				exam_end_time_ = #{examEndTime},
			</if>
			<if test="subjectIdList != null">
				subject_id_list_ = #{subjectIdList},
			</if>
			<if test="subjectNameList != null">
				subject_name_list_ = #{subjectNameList},
			</if>
			<if test="tenantId != null and tenantId != 0">
				tenant_id_ = #{tenantId},
			</if>
			<if test="examMode != null">
				exam_mode_ = #{examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
				exam_location_id_ = #{examLocationId},
			<if test="mainTeacherUserId != null">
				main_teacher_user_id_ = #{mainTeacherUserId},
			</if>
			<if test="mainTeacherName != null">
				main_teacher_user_name_ = #{mainTeacherName},
			</if>
			<if test="assistantTeacherUserIdList != null">
				assistant_teacher_user_id_list_ = #{assistantTeacherUserIdList},
			</if>
			<if test="assistantTeacherUserNameList != null">
				assistant_teacher_user_name_list_ = #{assistantTeacherUserNameList},
			</if>
			<if test="organId != null">
				organ_id_ = #{organId},
			</if>
			<if test="examPlanPushFlag != null">
				exam_plan_push_flag_ = #{examPlanPushFlag},
			</if>
			<if test="examRoomStudentNum!=null">
				exam_room_student_num_=#{examRoomStudentNum},
			</if>
			update_time_ = NOW()
		</set> WHERE id_ = #{id} AND update_time_=#{updateTime}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM exam_room WHERE id_ = #{id} 
	</delete>

	<delete id="batchDeleteExamRooms">
		DELETE FROM exam_room WHERE exam_plan_push_flag_=0 AND id_ IN
		<foreach collection="examRoomIds" item="examRoomId" separator="," open="(" close=")">
			#{examRoomId}
		</foreach>
	</delete>

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

	<sql id="queryExamRoomPageSql">
		<where>
			<if test="organIds!=null">
				AND er.organ_id_ IN
				<foreach collection="organIds" item="organId" separator="," open="(" close=")">
					#{organId}
				</foreach>
			</if>
			<if test="examRoomId!=null">
				AND er.id_ = #{examRoomId}
			</if>
			<if test="examId!=null">
				AND er.examination_basic_id_ = #{examId}
			</if>
			<if test="tenantId != null and tenantId != 0">
				AND er.tenant_id_ = #{tenantId}
			</if>
			<if test="startTime != null">
				AND eb.expect_exam_start_time_ >= #{startTime}
			</if>
			<if test="endTime != null">
				AND eb.expect_exam_end_time_ &lt;= #{endTime}
			</if>
			<if test="search != null and search != ''">
				AND eb.name_ LIKE CONCAT('%',#{search},'%')
			</if>
			<if test="examStatusEnum != null and examStatusEnum != ''">
				AND eb.status_ = #{examStatusEnum}
			</if>
		</where>
	</sql>
	
	<resultMap type="com.keao.edu.user.dto.ExamRoomDto" id="ExamRoomDto" extends="ExamRoom">
		<association property="examLocation" columnPrefix="el_" resultMap="com.keao.edu.user.dao.ExamLocationDao.ExamLocation"/>
		<association property="examinationBasic" columnPrefix="exam_" resultMap="com.keao.edu.user.dao.ExaminationBasicDao.ExaminationBasic"/>
	</resultMap>

	<resultMap id="ExamRoomListDtoMap" type="com.keao.edu.user.dto.ExamRoomListDto">
		<result property="examEndTime" column="exam_end_time_"/>
		<result property="examStartTime" column="exam_start_time_"/>
		<result property="openFlag" column="open_flag_"/>
		<result property="examRoomId" column="exam_room_id_"/>
		<result property="assistantTeacherUserIdList" column="assistant_teacher_user_id_list_"/>
		<result property="assistantTeacherUserNameList" column="assistant_teacher_user_name_list_"/>
		<result property="examBaseStatus" column="exam_base_status_"/>
		<result property="examName" column="exam_name_"/>
		<result property="getMainTeacherUserId" column="main_teacher_user_id_"/>
		<result property="mainTeacherUserName" column="main_teacher_user_name_"/>
		<result property="subjectNameList" column="subject_name_list_"/>
	</resultMap>
	<sql id="queryExamRoomPageListSql">
		<where>
			(er.main_teacher_user_id_ = #{teacherId} OR FIND_IN_SET(#{teacherId},er.assistant_teacher_user_id_list_))
			<if test="startTime != null and startTime != ''">
				AND DATE_FORMAT(er.exam_end_time_,'%Y-%m-%d') BETWEEN #{startTime} AND #{endTime}
			</if>
			<if test="examBaseStatus != null and examBaseStatus != ''">
				AND eb.status_ = #{examBaseStatus}
			</if>
		</where>
	</sql>
	<select id="countExamRoomPage" resultType="java.lang.Integer">
		SELECT COUNT(er.id_)
		FROM exam_room er
		LEFT JOIN examination_basic eb ON er.examination_basic_id_ = eb.id_
		<include refid="queryExamRoomPageListSql"/>
	</select>
	<select id="queryExamRoomPage" resultMap="ExamRoomListDtoMap">
		SELECT eb.name_ exam_name_,er.exam_end_time_,er.exam_start_time_,
		er.id_ exam_room_id_,er.subject_name_list_,er.main_teacher_user_name_,er.main_teacher_user_id_,
		er.assistant_teacher_user_id_list_,er.assistant_teacher_user_name_list_,eb.status_ exam_base_status_,er.open_flag_
		FROM exam_room er
		LEFT JOIN examination_basic eb ON er.examination_basic_id_ = eb.id_
		<include refid="queryExamRoomPageListSql"/>
		ORDER BY er.exam_start_time_ DESC
		<include refid="global.limit"/>
	</select>

	<sql id="queryExamRoomCondition">
		<where>
			<if test="organIds!=null">
				AND er.organ_id_ IN
				<foreach collection="organIds" item="organId" separator="," open="(" close=")">
					#{organId}
				</foreach>
			</if>
			<if test="examRoomId!=null">
				AND er.id_=#{examRoomId}
			</if>
			<if test="examId!=null">
				AND er.examination_basic_id_ = #{examId}
			</if>
			<if test="search!=null and search!=''">
				AND (er.id_=#{search})
			</if>
		</where>
	</sql>

	<select id="queryExamRoom" resultMap="ExamRoomDto">
		SELECT
			er.*,
			el.id_ el_id_,
			el.name_ el_name_
		FROM exam_room er
			LEFT JOIN exam_location el ON er.exam_location_id_ = el.id_
		<include refid="queryExamRoomCondition"/>
		ORDER BY er.id_ DESC
		<include refid="global.limit"/>
	</select>

	<select id="countExamRoom" resultType="java.lang.Integer">
		SELECT COUNT(er.id_) FROM exam_room er
		LEFT JOIN exam_location el ON er.exam_location_id_ = el.id_
		<include refid="queryExamRoomCondition"/>
	</select>
	
    <select id="getWithExam" resultMap="ExamRoom">
		SELECT * FROM exam_room WHERE examination_basic_id_=#{examId}
		<if test="organIds!=null">
			AND organ_id_ IN
			<foreach collection="organIds" item="organId" separator="," open="(" close=")">
				#{organId}
			</foreach>
		</if>
    </select>

	<select id="lockRoom" resultMap="ExamRoom">
		SELECT * FROM exam_room WHERE id_=#{examRoomId} FOR UPDATE
	</select>
    <select id="getWithDays" resultMap="ExamRoom">
		SELECT
			* 
		FROM
			exam_room 
		WHERE
			DATE_FORMAT( exam_start_time_, '%Y-%m-%d' ) IN 
			<foreach collection="days" item="day" separator="," open="(" close=")" >
				#{day}
			</foreach>
			OR DATE_FORMAT( exam_end_time_, '%Y-%m-%d' ) IN
			<foreach collection="days" item="day" separator="," open="(" close=")" >
				#{day}
			</foreach>
	</select>
	<select id="getWithExamAndSubject" resultMap="ExamRoom">
		SELECT * FROM exam_room WHERE examination_basic_id_=#{examId} AND FIND_IN_SET(#{subjectId}, subject_id_list_);
	</select>

    <select id="getExamRoomExamTime" resultType="com.keao.edu.user.dto.ExamRoomExamTimeDto">
		SELECT
			MIN(exam_start_time_) examStartTime,
			MAX(exam_end_time_) examEndTime
		FROM exam_room WHERE examination_basic_id_=#{examId}
    </select>
</mapper>
