<?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.ExamReviewDao">
	
	<resultMap type="com.keao.edu.user.entity.ExamReview" id="ExamReview">
		<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="teacher_id_" property="teacherId" />
		<result column="teacher_type_" property="teacherType"/>
		<result column="student_id_" property="studentId" />
		<result column="evaluation_content_" property="evaluationContent" />
		<result column="evaluation_result_" property="evaluationResult"/>
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
		<result column="enable_edit_" property="enableEdit" typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler"/>
		<association property="examinationBasic" column="exam_" resultMap="com.keao.edu.user.dao.ExaminationBasicDao.ExaminationBasic"/>
	</resultMap>

	<resultMap id="ExamReviewDto" type="com.keao.edu.user.dto.ExamReviewDto" extends="ExamReview">
		<result column="exam_start_time_" property="examStartTime"/>
		<association property="examRegistration" columnPrefix="regist_" resultMap="com.keao.edu.user.dao.ExamRegistrationDao.ExamRegistration"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExamReview" >
		SELECT * FROM exam_review WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ExamReview">
		SELECT * FROM exam_review ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.entity.ExamReview" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_review (id_,examination_basic_id_,exam_room_id_,teacher_id_,teacher_type_,student_id_,evaluation_content_,
		evaluation_result_,create_time_,update_time_,tenant_id_,enable_edit_,exam_registration_id_)
		VALUES(#{id},#{examinationBasicId},#{examRoomId},#{teacherId},#{teacherType},#{studentId},#{evaluationContent},
		#{evaluationResult},NOW(),NOW(),
		#{tenantId},#{enableEdit,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},#{examRegistrationId})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.keao.edu.user.entity.ExamReview">
		UPDATE exam_review
		<set>
			<if test="examinationBasicId != null">
				examination_basic_id_ = #{examinationBasicId},
			</if>
			<if test="examRegistrationId != null">
				exam_registration_id_ = #{examRegistrationId},
			</if>
			<if test="examRoomId != null">
				exam_room_id_ = #{examRoomId},
			</if>
			<if test="teacherId != null">
				teacher_id_ = #{teacherId},
			</if>
			<if test="teacherType != null">
				teacher_type_ = #{teacherType},
			</if>
			<if test="evaluationResult != null">
				evaluation_result_ = #{evaluationResult},
			</if>
			<if test="tenantId != null and tenantId != 0">
				tenant_id_ = #{tenantId},
			</if>
			<if test="studentId != null">
				student_id_ = #{studentId},
			</if>
			<if test="evaluationContent != null">
				evaluation_content_ = #{evaluationContent},
			</if>
			<if test="enableEdit != null">
				enable_edit_ = #{enableEdit,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
				update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM exam_review WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ExamReview" parameterType="map">
		SELECT * FROM exam_review
		ORDER BY id_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM exam_review
	</select>

	<sql id="findExamResultCondition">
		<where>
			<if test="organIds!=null">
				AND ere.organ_id_ IN
				<foreach collection="organIds" item="organId" separator="," open="(" close=")">
					#{organId}
				</foreach>
			</if>
			<if test="registId!=null">
				AND er.exam_registration_id_=#{registId}
			</if>
			<if test="roomId!=null">
				AND er.exam_room_id_ = #{roomId}
			</if>
			<if test="examinationBaseId!=null">
				AND er.examination_basic_id_ = #{examinationBaseId}
			</if>
			<if test="cardNo!=null">
				AND ere.card_no_=#{cardNo}
			</if>
			<if test="organId!=null">
				AND ere.organ_id_=#{organId}
			</if>
			<if test="subjectId!=null">
				AND ere.subject_id_=#{subjectId}
			</if>
			<if test="level!=null">
				AND ere.level_=#{level}
			</if>
			<if test="studentStatus!=null">
				AND ere.status_=#{studentStatus,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler}
			</if>
			<if test="teacherType!=null">
				AND er.teacher_type_=#{teacherType}
			</if>
			<if test="examStartTime!=null and examEndTime!=null">
				AND DATE_FORMAT( ero.exam_start_time_, '%Y-%m-%d' ) BETWEEN  DATE_FORMAT( #{examStartTime}, '%Y-%m-%d' ) AND DATE_FORMAT( #{examEndTime}, '%Y-%m-%d' )
			</if>
			<if test="search!=null and search!=''">
				AND (ere.card_no_=#{search} OR su.real_name_ LIKE CONCAT('%', #{search}, '%') OR stu.real_name_ LIKE CONCAT('%', #{search}, '%'))
			</if>
		</where>
	</sql>
	<select id="findExamResult" resultMap="ExamReviewDto">
		SELECT
			er.*,
			ere.id_ regist_id_,
			ere.student_id_ regist_student_id_,
			ere.organ_id_ regist_organ_id_,
			ere.subject_id_ regist_subject_id_,
			ere.level_ regist_level_,
			ere.status_ regist_status_,
			ere.card_no_ regist_card_no_,
			ero.exam_start_time_
		FROM
			exam_review er
			LEFT JOIN exam_registration ere ON ere.id_=er.exam_registration_id_
			LEFT JOIN exam_room ero ON ero.id_=er.exam_room_id_
			LEFT JOIN sys_user su ON su.id_=er.teacher_id_
			LEFT JOIN sys_user stu ON stu.id_=ere.student_id_
		<include refid="findExamResultCondition"/>
		ORDER BY ere.id_ DESC
		<include refid="global.limit"/>
	</select>
	<select id="countExamResult" resultType="int">
		SELECT
			COUNT(er.id_)
		FROM
			exam_review er
			LEFT JOIN exam_registration ere ON ere.id_=er.exam_registration_id_
			LEFT JOIN exam_room ero ON ero.id_=er.exam_room_id_
			LEFT JOIN sys_user su ON su.id_=er.teacher_id_
			LEFT JOIN sys_user stu ON stu.id_=ere.student_id_
		<include refid="findExamResultCondition"/>
	</select>
	<select id="countExamReviewRecordList" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT ser.id_)
		FROM exam_room_student_relation ersr
		LEFT JOIN student_exam_result ser ON ser.exam_registration_id_ = ersr.exam_registration_id_
		LEFT JOIN exam_review er ON ser.exam_registration_id_ = er.exam_registration_id_ AND er.teacher_id_ = #{teacherId}
		<include refid="queryExamReviewRecordListSql"/>
	</select>
	<resultMap id="ExamReviewRecordDtoMap" type="com.keao.edu.user.dto.ExamReviewRecordDto">
		<result property="recordStartTime" column="record_start_time_"/>
		<result property="confirmStatus" column="confirm_status_"/>
		<result property="recordFlag" column="record_flag_"/>
		<result property="realName" column="real_name_"/>
		<result property="studentId" column="student_id_"/>
		<result property="evaluationContent" column="evaluation_content_"/>
		<result property="evaluationResult" column="evaluation_result_"/>
		<result property="examReviewId" column="id_"/>
		<result property="finishedExam" column="is_finished_exam_"/>
		<result property="videoUrl" column="video_url_"/>
		<result property="examRegistrationId" column="exam_registration_id_"/>
	</resultMap>
	<sql id="queryExamReviewRecordListSql">
		<where>
			<if test="reviewFlag != null and reviewFlag == 1">
				AND er.id_ IS NOT NULL
			</if>
			<if test="reviewFlag != null and reviewFlag == 0">
				AND er.id_ IS NULL
			</if>
			<if test="roomId != 0">
				AND ersr.exam_room_id_ = #{roomId}
			</if>
		</where>
	</sql>

	<select id="queryExamReviewRecordList" resultMap="ExamReviewRecordDtoMap">
		SELECT ser.is_finished_exam_,ser.record_flag_,ser.confirm_status_,er.evaluation_content_,
		er.evaluation_result_,ersr.exam_registration_id_,er.id_,ersr.student_id_,ser.video_url_
		FROM exam_room_student_relation ersr
		LEFT JOIN student_exam_result ser ON ser.exam_registration_id_ = ersr.exam_registration_id_
		LEFT JOIN exam_review er ON ser.exam_registration_id_ = er.exam_registration_id_ AND er.teacher_id_ = #{teacherId}
		<include refid="queryExamReviewRecordListSql"/>
		ORDER BY ser.is_finished_exam_ DESC
		<include refid="global.limit"/>
	</select>

	<select id="findByRegistrationId" resultMap="ExamReviewRecordDtoMap">
		SELECT * FROM exam_review WHERE exam_registration_id_ = #{examRegistrationId} AND teacher_id_ = #{teacherId} LIMIT 1
	</select>
</mapper>
