<?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="main_teacher_user_id_" property="mainTeacherUserId" />
		<result column="assistant_teacher_user_id_list_" property="assistantTeacherUserIdList" />
		<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="exam_plan_push_flag_" property="examPlanPushFlag" typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler"/>
		<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" />
	</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_,main_teacher_user_id_,
		assistant_teacher_user_id_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(#{id},#{examinationBasicId},#{examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},#{examLocationId},#{subjectIdList},
		#{mainTeacherUserId},#{assistantTeacherUserIdList},
		#{examStartTime},#{examEndTime},#{delFlag},#{organId},#{examPlanPushFlag,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
		#{examRoomStudentNum},NOW(),NOW(),#{tenantId})
	</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_,main_teacher_user_id_,
		assistant_teacher_user_id_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.mainTeacherUserId},#{examRoom.assistantTeacherUserIdList},
			#{examRoom.examStartTime},#{examRoom.examEndTime},#{examRoom.delFlag},#{examRoom.organId},#{examRoom.examPlanPushFlag,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			#{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="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="tenantId != null">
				tenant_id_ = #{tenantId},
			</if>
			<if test="examMode != null">
				exam_mode_ = #{examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="examLocationId != null">
				exam_location_id_ = #{examLocationId},
			</if>
			<if test="mainTeacherUserId != null">
				main_teacher_user_id_ = #{mainTeacherUserId},
			</if>
			<if test="assistantTeacherUserIdList != null">
				assistant_teacher_user_id_list_ = #{assistantTeacherUserIdList},
			</if>
			<if test="organId != null">
				organ_id_ = #{organId},
			</if>
			<if test="examPlanPushFlag != null">
				exam_plan_push_flag_ = #{examPlanPushFlag,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="examRoomStudentNum!=null">
				exam_room_student_num_=#{examRoomStudentNum},
			</if>
				update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</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="tenantId != null">
				tenant_id_ = #{tenantId},
			</if>
			<if test="examMode != null">
				exam_mode_ = #{examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="examLocationId != null">
				exam_location_id_ = #{examLocationId},
			</if>
			<if test="mainTeacherUserId != null">
				main_teacher_user_id_ = #{mainTeacherUserId},
			</if>
			<if test="assistantTeacherUserIdList != null">
				assistant_teacher_user_id_list_ = #{assistantTeacherUserIdList},
			</if>
			<if test="organId != null">
				organ_id_ = #{organId},
			</if>
			<if test="examPlanPushFlag != null">
				exam_plan_push_flag_ = #{examPlanPushFlag,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</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 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" javaType="com.keao.edu.user.entity.ExamLocation" resultMap="com.keao.edu.user.dao.ExamLocationDao.ExamLocation"/>
		<association property="examinationBasic" javaType="com.keao.edu.user.entity.ExaminationBasic" resultMap="com.keao.edu.user.dao.ExaminationBasicDao.ExaminationBasic"/>
	</resultMap>
	
    <select id="countExamRoomPage" resultType="java.lang.Integer">
		SELECT COUNT(er.id_) FROM exam_room er
		LEFT JOIN exam_location el ON er.exam_location_id_ = el.id_ AND er.tenant_id_ = el.tenant_id_
		LEFT JOIN examination_basic eb ON FIND_IN_SET(er.exam_location_id_,eb.exam_location_id_list_) AND er.tenant_id_ = eb.tenant_id_
		<include refid="queryExamRoomPageSql"/>
	</select>
	
	<select id="queryExamRoomPage" resultMap="ExamRoomDto">
		SELECT er.*,eb.* FROM exam_room er
		LEFT JOIN exam_location el ON er.exam_location_id_ = el.id_ AND er.tenant_id_ = el.tenant_id_
		LEFT JOIN examination_basic eb ON FIND_IN_SET(er.exam_location_id_,eb.exam_location_id_list_) AND er.tenant_id_ = eb.tenant_id_
		<include refid="queryExamRoomPageSql"/>
		ORDER BY eb.expect_exam_start_time_ DESC
		<include refid="global.limit"/>
	</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" resultType="com.keao.edu.user.api.entity.ExamRoom">
		SELECT * FROM exam_room WHERE id_=#{examRoomId} FOR UPDATE
	</select>
    <select id="getWithDays" resultType="com.keao.edu.user.api.entity.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>
</mapper>
