<?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.ExamRegistrationDao">

	<resultMap type="com.keao.edu.user.entity.ExamRegistration" id="ExamRegistration">
		<id column="id_" property="id" />
		<result column="examination_basic_id_" property="examinationBasicId" />
		<result column="student_id_" property="studentId" />
		<result column="organ_id_" property="organId" />
		<result column="subject_id_" property="subjectId" />
		<result column="level_" property="level" />
		<result column="level_fee_" property="levelFee" />
		<result column="song_json_" property="songJson" />
		<result column="last_exam_level_" property="lastExamLevel" />
		<result column="last_exam_certificate_url_" property="lastExamCertificateUrl" />
		<result column="exam_music_theory_level_" property="examMusicTheoryLevel" />
		<result column="theory_level_fee_" property="theoryLevelFee" />
		<result column="last_music_theory_level_" property="lastMusicTheoryLevel" />
		<result column="last_music_theory_certificate_url_" property="lastMusicTheoryCertificateUrl" />
		<result column="adviser_name_" property="adviserName" />
		<result column="adviser_phone_" property="adviserPhone" />
		<result column="card_no_" property="cardNo" />
		<result column="status_" property="status" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
		<result column="memo_" property="memo" />
		<association property="sysUser" columnPrefix="sys_user_" resultMap="com.keao.edu.user.dao.StudentDao.Student"/>
		<association property="subject" columnPrefix="subject_" resultMap="com.keao.edu.user.dao.SubjectDao.Subject"/>
		<association property="examinationBasic" columnPrefix="examination_basic_" resultMap="com.keao.edu.user.dao.ExaminationBasicDao.ExaminationBasic"/>
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExamRegistration" >
		SELECT * FROM exam_registration WHERE id_ = #{id}
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="ExamRegistration">
		SELECT * FROM exam_registration ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.entity.ExamRegistration" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO  exam_registration (examination_basic_id_, student_id_, organ_id_,subject_id_, level_, level_fee_,
		song_json_, last_exam_level_, last_exam_certificate_url_,exam_music_theory_level_, theory_level_fee_,
		last_music_theory_level_,last_music_theory_certificate_url_, adviser_name_,adviser_phone_, card_no_, status_,
		create_time_, update_time_, tenant_id_,memo_)
		VALUES (#{examinationBasicId}, #{studentId}, #{organId}, #{subjectId}, #{level}, #{levelFee}, #{songJson},
		#{lastExamLevel}, #{lastExamCertificateUrl}, #{examMusicTheoryLevel}, #{theoryLevelFee}, #{lastMusicTheoryLevel},
		#{lastMusicTheoryCertificateUrl}, #{adviserName}, #{adviserPhone}, #{cardNo}, #{status},#{createTime},
		#{updateTime}, #{tenantId}, #{memo})
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.keao.edu.user.entity.ExamRegistration">
		UPDATE exam_registration
		<set>
			<if test="examinationBasicId != null">
				examination_basic_id_ = #{examinationBasicId},
			</if>
			<if test="subjectId != null">
				subject_id_ = #{subjectId},
			</if>
			<if test="adviserPhone != null">
				adviser_phone_ = #{adviserPhone},
			</if>
			<if test="lastExamLevel != null">
				last_exam_level_ = #{lastExamLevel},
			</if>
			<if test="tenantId != null">
				tenant_id_ = #{tenantId},
			</if>
			<if test="level != null">
				level_ = #{level},
			</if>
			<if test="organId != null">
				organ_id_ = #{organId},
			</if>
			<if test="songJson != null">
				song_json_ = #{songJson},
			</if>
			<if test="adviserName != null">
				adviser_name_ = #{adviserName},
			</if>
			<if test="studentId != null">
				student_id_ = #{studentId},
			</if>
			<if test="lastExamCertificateUrl != null">
				last_exam_certificate_url_ = #{lastExamCertificateUrl},
			</if>
			<if test="cardNo!=null">
				card_no_ =#{cardNo},
			</if>
			<if test="status!=null">
				status_ = #{status,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="memo!=null">
				memo_ = #{memo},
			</if>
				update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>

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

	<sql id="queryCondition">
		<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="organId!=null">
				AND er.organ_id_ = #{organId}
			</if>
			<if test="subjectId!=null">
				AND er.subject_id_ = #{subjectId}
			</if>
			<if test="level!=null">
				AND er.level_ = #{level}
			</if>
			<if test="status!=null">
				AND er.status_ = #{status,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler}
			</if>
		</where>
	</sql>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ExamRegistration" parameterType="map">
		SELECT
			er.id_,
			er.examination_basic_id_,
			er.exam_music_theory_level_,
			er.card_no_,
			er.student_id_,
			er.memo_,
			su.real_name_ sys_user_real_name_,
			er.last_exam_certificate_url_,
			er.last_music_theory_certificate_url_,
			er.level_fee_,
			er.theory_level_fee_,
			er.organ_id_,
			er.subject_id_,
			er.level_,
			erp.trans_amount_,
			er.status_,
			er.create_time_
		FROM
			exam_registration er
			LEFT JOIN sys_user su ON er.student_id_ = su.id_
			LEFT JOIN exam_registration_payment erp ON er.id_ = erp.exam_registration_id_
		<include refid="queryCondition"/>
		ORDER BY er.id_
		<include refid="global.limit"/>
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM exam_registration er
		<include refid="queryCondition"/>
	</select>
    <select id="countWithoutExamRoomStudentNum" resultType="int">
		SELECT
			COUNT(ersr.id_)
		FROM
			exam_registration er
		LEFT JOIN exam_room_student_relation ersr ON ersr.student_id_ = er.student_id_
		WHERE 1=1
		<if test="organIds!=null">
			AND ersr.organ_id_ IN
			<foreach collection="organIds" item="organI" separator="," open="(" close=")">
				#{organI}
			</foreach>
		</if>
		AND er.examination_basic_id_ = #{examId}
		AND ersr.id_ IS NULL
	</select>

	<select id="countInExamRoomStudentNum" resultType="int">
		SELECT
			COUNT(ersr.id_)
		FROM
			exam_room_student_relation ersr
		LEFT JOIN exam_registration er ON ersr.student_id_ = er.student_id_ AND er.examination_basic_id_=ersr.examination_basic_id_
		WHERE 1=1
		<if test="organIds!=null">
			er.organ_id_ IN
			<foreach collection="organIds" item="organI" separator="," open="(" close=")">
				#{organI}
			</foreach>
		</if>
		AND ersr.examination_basic_id_ = #{examId}
	</select>

	<resultMap id="ExamRegistrationStatisticsDto" type="com.keao.edu.user.dto.ExamRegistrationStatisticsDto">
		<result property="totalRegistrationStudentNum" column="totalRegistrationStudentNum"/>
		<result property="childOrganRegistrationStudentNum" column="childOrganRegistrationStudentNum"/>
		<result property="waitAuditStudentNum" column="waitAuditStudentNum"/>
		<result property="refundedStudentNum" column="refundedStudentNum"/>
	</resultMap>

    <select id="getExamRegistrationStaticsInfo" resultMap="ExamRegistrationStatisticsDto">
		SELECT
			SUM( CASE WHEN er.organ_id_ = #{selfOrganId} THEN 1 ELSE 0 END ) totalRegistrationStudentNum,
			SUM( CASE WHEN er.organ_id_ != #{selfOrganId} THEN 1 ELSE 0 END ) childOrganRegistrationStudentNum,
			SUM( CASE WHEN er.status_ = 'AUDIT_WAIT' THEN 1 ELSE 0 END ) waitAuditStudentNum,
			SUM( CASE WHEN er.status_ = 'REFUNDED' THEN 1 ELSE 0 END ) refundedStudentNum
		FROM
			exam_registration er
		WHERE
			er.examination_basic_id_ = #{examId}
			<if test="organIds!=null">
				AND er.organ_id_ IN
				<foreach collection="organIds" item="organId" separator="," open="(" close=")">
					#{organId}
				</foreach>
			</if>
    </select>

	<select id="countTotalRegistrationStudentNumWithExam" resultType="int">
		SELECT
			COUNT(er.id_)
		FROM
		exam_registration er
		WHERE 1=1
		<if test="organIds!=null">
			AND er.organ_id_ IN
			<foreach collection="organIds" item="organI" separator="," open="(" close=")">
				#{organI}
			</foreach>
		</if>
		AND er.examination_basic_id_ = #{examId}
		AND er.status_ != ''
	</select>
    <select id="getWithExamAndStudents" resultMap="ExamRegistration">
		SELECT * FROM exam_registration WHERE examination_basic_id_=#{examId} AND student_id_ IN
		<foreach collection="studentIds" item="studentId" separator="," open="(" close=")">
			#{studentId}
		</foreach>
	</select>
	<resultMap id="ExamRegistrationDtoMap" type="com.keao.edu.user.dto.ExamRegistrationDto" extends="ExamRegistration">
		<result property="studentName" column="studentName"/>
		<result property="subjectName" column="subjectName"/>
		<result property="examBaseName" column="examBaseName"/>
		<result property="examStartTime" column="expect_exam_start_time_"/>
		<result property="examEndTime" column="expect_exam_end_time_"/>
		<result property="enrollEndTime" column="enroll_end_time_"/>
		<result property="enrollStartTime" column="enroll_start_time_"/>
	</resultMap>
    <select id="countStudentList" resultType="java.lang.Integer">
		SELECT COUNT(er.id_) FROM exam_registration er
		<include refid="queryStudentListSql"/>
	</select>
	<sql id="queryStudentListSql">
		<where>
			<if test="studentId != null">
				er.student_id_ = #{studentId}
			</if>
			<if test="examRegistrationId != null">
				er.id_ = #{examRegistrationId}
			</if>
			<if test="tenantId != null">
				er.tenant_id_ = #{tenantId}
			</if>
			<if test="status != null and status != ''">
				er.status_ = #{status}
			</if>
		</where>
	</sql>
	<select id="queryStudentList" resultMap="ExamRegistrationDtoMap">
		SELECT er.*,eb.enroll_end_time_,eb.enroll_start_time_,eb.expect_exam_end_time_,eb.expect_exam_start_time_,eb.name_ examBaseName
		FROM exam_registration er
		LEFT JOIN examination_basic eb ON er.examination_basic_id_ = eb.id_
		<include refid="queryStudentListSql"/>
		ORDER BY er.update_time_ DESC
		<include refid="global.limit"/>
	</select>
	<resultMap id="ExamRecordDtoMap" type="com.keao.edu.user.dto.ExamRecordDto" extends="ExamRegistration">
		<result property="subjectName" column="subjectName"/>
		<result property="examBaseName" column="examBaseName"/>
		<result property="examCertificationId" column="examCertificationId"/>
		<association property="studentExamResult" resultMap="com.keao.edu.user.dao.StudentExamResultDao.StudentExamResult"/>
	</resultMap>
	<select id="queryExamList" resultMap="ExamRecordDtoMap">
		SELECT er.*,ser.* FROM exam_registration er
		LEFT JOIN student_exam_result ser ON er.id_ = ser.exam_registration_id_
		WHERE ser.id_ IS NOT NULL AND er.student_id_ = #{studentId}
	</select>
	<select id="countExamList" resultType="java.lang.Integer">
		SELECT COUNT(er.id_) FROM exam_registration er
		LEFT JOIN student_exam_result ser ON er.id_ = ser.exam_registration_id_
		WHERE ser.id_ IS NOT NULL AND er.student_id_ = #{studentId}
	</select>

	<!-- 获取报名信息 -->
	<select id="getExamRegistration" resultMap="ExamRegistrationDtoMap">
		SELECT er.*,s.name_ subjectName,su.real_name_ studentName,eb.expect_exam_start_time_,eb.expect_exam_end_time_ FROM exam_registration er
		LEFT JOIN sys_user su ON su.id_ = er.subject_id_
		LEFT JOIN examination_basic eb ON er.examination_basic_id_ = eb.id_
		LEFT JOIN subject s on er.subject_id_ = s.id_
		WHERE er.id_ = #{examRegistrationId}
	</select>

	<select id="getExamRegistrationInfo" resultMap="ExamRegistration">
		SELECT er.*, su.id_ sys_user_id_, su.real_name_ sys_user_real_name_,su.phone_ sys_user_phone_ , s.id_ subject_id_,
		s.name_ subject_name_ ,eb.name_ examination_basic_name_
		FROM exam_registration er
		LEFT JOIN examination_basic eb ON eb.id_=er.examination_basic_id_
		LEFT JOIN sys_user su ON er.student_id_ = su.id_
		LEFT JOIN subject s on er.subject_id_ = s.id_
		WHERE er.id_ = #{id}
	</select>
</mapper>
