<?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.StudentExamResultDao">
	
	<resultMap type="com.keao.edu.user.entity.StudentExamResult" id="StudentExamResult">
		<result column="id_" property="id" />
		<result column="exam_registration_id_" property="examRegistrationId" />
		<result column="examination_basic_id_" property="examinationBasicId" />
		<result column="student_id_" property="studentId" />
		<result column="result_" property="result" typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler" />
		<result column="avg_score_" property="avgScore"/>
		<result column="confirm_status_" property="confirmStatus" />
		<result column="record_start_time_" property="recordStartTime" />
		<result column="record_flag_" property="recordFlag" />
		<result column="is_finished_exam_" property="isFinishedExam" />
		<result column="memo_" property="memo" />
		<result column="video_url_" property="videoUrl" />
		<result column="operator_id_" property="operatorId" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
		<association property="examRegistration" columnPrefix="regist_" resultMap="com.keao.edu.user.dao.ExamRegistrationDao.ExamRegistration"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="StudentExamResult" >
		SELECT * FROM student_exam_result WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="StudentExamResult">
		SELECT * FROM student_exam_result ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.entity.StudentExamResult" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO student_exam_result (id_,examination_basic_id_,student_id_,result_,avg_score_,video_url_,
		confirm_status_,memo_,operator_id_,is_finished_exam_,create_time_,update_time_,tenant_id_,exam_registration_id_)
		VALUES(#{id},#{examinationBasicId},#{studentId},#{result,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
		#{avgScore},#{#{videoUrl}},#{confirmStatus},#{memo},#{operatorId},#{isFinishedExam},NOW(),NOW(),#{tenantId},#{examRegistrationId})
	</insert>

	<insert id="batchInsert" parameterType="com.keao.edu.user.entity.StudentExamResult" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO student_exam_result (examination_basic_id_,student_id_,result_,avg_score_,video_url_,
		confirm_status_,memo_,operator_id_,is_finished_exam_,create_time_,update_time_,tenant_id_,exam_registration_id_)
		VALUES
		<foreach collection="results" item="result" separator=",">
			(#{result.examinationBasicId},#{result.studentId},#{result.result,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			#{result.avgScore},#{result.videoUrl},#{result.confirmStatus},#{result.memo},#{result.operatorId},#{result.isFinishedExam},NOW(),NOW(),#{result.tenantId},#{result.examRegistrationId})
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.keao.edu.user.entity.StudentExamResult">
		UPDATE student_exam_result
		<set>
			<if test="recordStartTime != null">
				record_start_time_ = #{recordStartTime},
			</if>
			<if test="recordFlag != null">
				record_flag_ = #{recordFlag},
			</if>
			<if test="examRegistrationId != null">
				exam_registration_id_ = #{examRegistrationId},
			</if>
			<if test="examinationBasicId != null">
				examination_basic_id_ = #{examinationBasicId},
			</if>
			<if test="operatorId != null">
				operator_id_ = #{operatorId},
			</if>
			<if test="tenantId != null and tenantId != 0">
				tenant_id_ = #{tenantId},
			</if>
			<if test="confirmStatus != null">
				confirm_status_ = #{confirmStatus},
			</if>
			<if test="studentId != null">
				student_id_ = #{studentId},
			</if>
			<if test="result != null">
				result_ = #{result,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="avgScore != null">
				avg_score_ = #{avgScore},
			</if>
			<if test="memo != null">
				memo_ = #{memo},
			</if>
			<if test="videoUrl != null">
				video_url_ = #{videoUrl},
			</if>
			<if test="isFinishedExam != null">
				is_finished_exam_ = #{isFinishedExam},
			</if>
				update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>
    <update id="endExam">
		UPDATE student_exam_result SET is_finished_exam_ = 1 AND update_time_ = NOW() WHERE exam_registration_id_ = #{examRegistrationId}
	</update>
    <update id="confirmStudent">
		UPDATE student_exam_result SET confirm_status_ = 1 AND update_time_ = NOW()
		WHERE exam_registration_id_ = #{examRegistrationId}
	</update>

    <!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM student_exam_result WHERE id_ = #{id} 
	</delete>

    <delete id="deleteWithRegists">
		DELETE FROM student_exam_result WHERE exam_registration_id_ IN
		<foreach collection="registIds" item="registId" separator="," open="(" close=")">
			#{registId}
		</foreach>
	</delete>

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

	<sql id="queryStudentExamResultCondition">
		<where>
			<if test="organIds!=null">
				AND er.organ_id_ IN
				<foreach collection="organIds" item="organId" separator="," open="(" close=")">
					#{organId}
				</foreach>
			</if>
			<if test="examId!=null">
				AND er.examination_basic_id_ = #{examId}
			</if>
			<if test="cardNo!=null and cardNo!=''">
				AND er.card_no_ = #{cardNo}
			</if>
			<if test="studentName!=null and studentName!=''">
				AND su.real_name_ LIKE CONCAT('%', #{studentName}, '%')
			</if>
			<if test="studentId != null">
				AND su.id_ = #{studentId}
			</if>
			<if test="examName!=null and examName!=''">
				AND eb.name_ LIKE CONCAT('%', #{examName}, '%')
			</if>
			<if test="subjectId!=null">
				AND er.subject_id_=#{subjectId}
			</if>
			<if test="level!=null">
				AND er.level_=#{level}
			</if>
			<if test="examStartTime!=null and examEndTime!=null">
				AND DATE_FORMAT(eb.actual_exam_start_time_, '%Y-%m-%d') BETWEEN #{examStartTime} AND #{examEndTime}
			</if>
			<if test="isFinishedExam!=null">
				AND ser.is_finished_exam_=#{isFinishedExam}
			</if>
			<if test="result!=null">
				AND ser.result_=#{result,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler}
			</if>
			<if test="resultConfirm!=null and resultConfirm==1">
				AND eb.status_='RESULT_CONFIRM'
			</if>
			<if test="resultConfirm!=null and resultConfirm==0">
				AND eb.status_='EXAM_END'
			</if>
			<if test="resultConfirm==null">
				AND eb.status_ IN ('EXAM_END','RESULT_CONFIRM')
			</if>
		</where>
	</sql>

	<select id="queryStudentExamResult" resultMap="StudentExamResult" parameterType="map">
		SELECT
			ser.*,
			er.id_ regist_id_,
			er.card_no_ regist_card_no_,
			er.student_id_ regist_student_id_,
			er.organ_id_ regist_organ_id_,
			er.subject_id_ regist_subject_id_,
			er.level_ regist_level_
		FROM
			student_exam_result ser
			LEFT JOIN examination_basic eb ON ser.examination_basic_id_=eb.id_
			LEFT JOIN exam_registration er ON ser.exam_registration_id_=er.id_
			LEFT JOIN sys_user su ON ser.student_id_=su.id_
		<include refid="queryStudentExamResultCondition"/>
		ORDER BY er.id_ <include refid="global.limit"/>
	</select>

	<select id="countStudentExamResult" resultType="int">
		SELECT
			COUNT(er.id_)
		FROM
			student_exam_result ser
			LEFT JOIN examination_basic eb ON ser.examination_basic_id_=eb.id_
			LEFT JOIN exam_registration er ON ser.exam_registration_id_=er.id_
			LEFT JOIN sys_user su ON ser.student_id_=su.id_
		<include refid="queryStudentExamResultCondition"/>
	</select>

	<select id="getStudentExamResultStatisticsInfo" resultType="com.keao.edu.user.dto.StudentExamResultStatisticsDto">
		SELECT
			COUNT( er.id_ ) totalRegistrationStudentNum,
			SUM( CASE WHEN ser.id_ > 0 THEN 0 ELSE 1 END ) notExamStudentNum,
			SUM( CASE WHEN ser.result_ = 'FAIL' THEN 1 ELSE 0 END ) examFailStudentNum,
			SUM( CASE WHEN ser.id_ > 0 THEN 1 ELSE 0 END ) examStudentNum
		FROM
			exam_registration er
			LEFT JOIN student_exam_result ser ON er.examination_basic_id_ = ser.examination_basic_id_
			AND er.student_id_ = ser.student_id_
		WHERE
			er.examination_basic_id_ = #{examId}
			AND er.status_ IN ('AUDIT_PASS', 'AUDIT_WAIT')
			<if test="organIds!=null">
				AND er.organ_id_ IN
				<foreach collection="organIds" item="organId" separator="," open="(" close=")">
					#{organId}
				</foreach>
			</if>
		GROUP BY
			er.examination_basic_id_
	</select>
	<select id="countExamNum" resultType="java.util.HashMap">
		SELECT ser.student_id_ 'key',COUNT(ser.id_) 'value' FROM student_exam_result ser
		WHERE ser.tenant_id_ = #{tenantId} AND ser.is_finished_exam_ = 1 AND ser.student_id_ IN
		<foreach collection="studentIds" item="item" open="(" close=")" separator=",">
			#{item}
		</foreach>
		GROUP BY ser.student_id_
	</select>
    <select id="findByRegistrationId" resultMap="StudentExamResult">
		SELECT * FROM student_exam_result WHERE exam_registration_id_ = #{examRegistrationId} LIMIT 1
	</select>

	<resultMap type="com.keao.edu.user.api.enums.StudentExamResultApiDto" id="StudentExamResultApiDto">
		<result column="id_" property="id" />
		<result column="exam_registration_id_" property="examRegistrationId" />
		<result column="examination_basic_id_" property="examinationBasicId" />
		<result column="student_id_" property="studentId" />
		<result column="result_" property="result" typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler" />
		<result column="avg_score_" property="avgScore"/>
		<result column="confirm_status_" property="confirmStatus" />
		<result column="record_start_time_" property="recordStartTime" />
		<result column="record_flag_" property="recordFlag" />
		<result column="is_finished_exam_" property="isFinishedExam" />
		<result column="memo_" property="memo" />
		<result column="video_url_" property="videoUrl" />
		<result column="operator_id_" property="operatorId" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
	</resultMap>
	<select id="getStudentExamResultApiDto" resultMap="StudentExamResultApiDto">
		SELECT * FROM student_exam_result WHERE exam_registration_id_ = #{examRegistrationId} LIMIT 1
	</select>
</mapper>
