<?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.ExamCertificationDao">
	
	<resultMap type="com.keao.edu.user.entity.ExamCertification" id="ExamCertification">
		<result column="id_" property="id" />
		<result column="examination_basic_id_" property="examinationBasicId" />
		<result column="exam_registration_id_" property="examRegistrationId" />
		<result column="student_id_" property="studentId" />
		<result column="card_no_" property="cardNo" />
		<result column="subject_id_" property="subjectId" />
		<result column="level_" property="level" />
		<result column="exam_start_time_" property="examStartTime" />
		<result column="exam_end_time_" property="examEndTime" />
		<result column="exam_address_" property="examAddress" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExamCertification" >
		SELECT * FROM exam_certification WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ExamCertification">
		SELECT * FROM exam_certification ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.entity.ExamCertification" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_certification (id_,examination_basic_id_,student_id_,card_no_,subject_id_,
		level_,exam_start_time_,exam_end_time_,exam_address_,create_time_,update_time_,tenant_id_,exam_registration_id_)
		VALUES(#{id},#{examinationBasicId},#{studentId},#{cardNo},#{subjectId},#{level},#{examStartTime},
		#{examEndTime},#{examAddress},NOW(),NOW(),#{tenantId},#{examRegistrationId})
	</insert>

	<insert id="batchInsert" parameterType="com.keao.edu.user.entity.ExamCertification" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_certification (examination_basic_id_,student_id_,card_no_,subject_id_,
		level_,exam_start_time_,exam_end_time_,exam_address_,create_time_,update_time_,tenant_id_,exam_registration_id_)
		VALUES
		<foreach collection="ecs" item="ec" separator=",">
			(#{ec.examinationBasicId},#{ec.studentId},#{ec.cardNo},#{ec.subjectId},#{ec.level},#{ec.examStartTime},
			#{ec.examEndTime},#{ec.examAddress},NOW(),NOW(),#{ec.tenantId},#{ec.examRegistrationId})
		</foreach>
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.keao.edu.user.entity.ExamCertification">
		UPDATE exam_certification
		<set>
			<if test="examinationBasicId != null">
				examination_basic_id_ = #{examinationBasicId},
			</if>
			<if test="examRegistrationId != null">
				exam_registration_id_ = #{examRegistrationId},
			</if>
			<if test="subjectId != null">
				subject_id_ = #{subjectId},
			</if>
			<if test="examEndTime != null">
				exam_end_time_ = #{examEndTime},
			</if>
			<if test="examStartTime != null">
				exam_start_time_ = #{examStartTime},
			</if>
			<if test="tenantId != null and tenantId != 0">
				tenant_id_ = #{tenantId},
			</if>
			<if test="cardNo != null">
				card_no_ = #{cardNo},
			</if>
			<if test="studentId != null">
				student_id_ = #{studentId},
			</if>
			<if test="level != null">
				level_ = #{level},
			</if>
			<if test="examAddress != null">
				exam_address_ = #{examAddress},
			</if>
			update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM exam_certification WHERE id_ = #{id} 
	</delete>
    <delete id="deleteWithRegist">
		DELETE FROM exam_certification WHERE exam_registration_id_ IN
		<foreach collection="registIds" item="registId" separator="," open="(" close=")">
			#{registId}
		</foreach>
	</delete>

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

    <select id="findByStuAndBasicId" resultMap="ExamCertification">
		SELECT * FROM exam_certification WHERE exam_registration_id_ = #{examRegistrationId} AND student_id_ = #{studentId} LIMIT 1
	</select>

	<select id="getExamCertificationDto" resultMap="ExamCertificationDtoMap">
		SELECT ec.exam_registration_id_,ec.exam_start_time_,ec.subject_id_,ec.level_,ec.exam_address_,er.song_json_,ec.card_no_,er.student_id_
		FROM exam_registration er
		LEFT JOIN exam_certification ec ON er.id_ = ec.exam_registration_id_
		WHERE er.id_ = #{examRegistrationId} LIMIT 1
	</select>
	<resultMap id="ExamCertificationDtoMap" type="com.keao.edu.user.dto.ExamCertificationDto">
		<result column="exam_registration_id_" property="examRegistrationId" />
		<result column="exam_start_time_" property="examTime" />
		<result column="card_no_" property="cardNo" />
		<result column="song_json_" property="songJson" />
		<result column="subject_id_" property="subjectId" />
		<result column="student_id_" property="studentId" />
		<result column="level_" property="level" />
		<result column="address_" property="examAddress" />
	</resultMap>
	<select id="queryExamCertificationDtoPage" resultMap="ExamCertificationDtoMap">
		SELECT ec.exam_registration_id_,ec.exam_start_time_,ec.card_no_,ec.subject_id_,ec.level_,ec.exam_address_
		FROM exam_room_student_relation ersr
		LEFT JOIN exam_certification ec ON ec.exam_registration_id_ = ersr.exam_registration_id_
		LEFT JOIN student_exam_result ser ON ser.exam_registration_id_ = ersr.exam_registration_id_
		WHERE ersr.student_id_ = #{studentId} AND ser.is_finished_exam_ = 0
		ORDER BY ec.exam_start_time_
	</select>
	<select id="countExamCertificationDtoPage" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT ersr.exam_registration_id_)
		FROM exam_room_student_relation ersr
		LEFT JOIN student_exam_result ser ON ser.exam_registration_id_ = ersr.exam_registration_id_
		WHERE ersr.student_id_ = #{studentId} AND ser.is_finished_exam_ = 0
	</select>
	<resultMap id="NeedCheckingDetailDtoMap" type="com.keao.edu.user.dto.NeedCheckingDetailDto">
		<result column="exam_registration_id_" property="examRegistrationId" />
		<result property="examRoomStudentRelationId" column="exam_room_student_relation_id_"/>
		<result property="examRoomId" column="exam_room_id_"/>
		<result property="signInTime" column="sign_in_time_"/>
		<result property="examEndTime" column="exam_end_time_"/>
		<result property="examStartTime" column="exam_start_time_"/>
		<result property="baseExamName" column="exam_base_name_"/>
		<result property="classroomSwitch" column="classroom_switch_"/>
		<result property="openFlag" column="open_flag_"/>
		<result property="finishedExam" column="is_finished_exam_"/>
		<result property="recordFlag" column="record_flag_"/>
	</resultMap>
	<select id="needCheckingDetail" resultMap="NeedCheckingDetailDtoMap">
		SELECT ersr.id_ exam_room_student_relation_id_,eb.name_ exam_base_name_,er.exam_start_time_,er.exam_end_time_,er.open_flag_,
		ser.is_finished_exam_,ersr.exam_room_id_,ersr.sign_in_time_,ersr.classroom_switch_,ersr.exam_registration_id_,ser.record_flag_
		FROM exam_room_student_relation ersr
		LEFT JOIN examination_basic eb ON eb.id_ = ersr.examination_basic_id_
		LEFT JOIN exam_room er ON er.id_ = ersr.exam_room_id_
		LEFT JOIN student_exam_result ser ON ser.exam_registration_id_ = ersr.exam_registration_id_
		WHERE ersr.exam_registration_id_ = #{examRegistrationId} LIMIT 1
	</select>
</mapper>
