<?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.ExamSubjectSongDao">
	
	<resultMap type="com.keao.edu.user.entity.ExamSubjectSong" id="ExamSubjectSong">
		<result column="id_" property="id" />
		<result column="examination_basic_id_" property="examinationBasicId" />
		<result column="exam_subject_id_" property="examSubjectId" />
		<result column="level_" property="level" />
		<result column="practice_num_" property="practiceNum" />
		<result column="practice_song_id_list_" property="practiceSongIdList" />
		<result column="perform_num_" property="performNum" />
		<result column="perform_song_id_list_" property="performSongIdList" />
		<result column="registration_fee_" property="registrationFee" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
		<association property="subject" columnPrefix="sub_" resultMap="com.keao.edu.user.dao.SubjectDao.Subject" />
	</resultMap>

	<resultMap id="ExamSubjectSongDto" type="com.keao.edu.user.dto.ExamSubjectSongDto" extends="ExamSubjectSong">
		<result property="practiceSongNames" column="practice_song_names_"/>
		<result property="performSongNames" column="perform_song_names_"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExamSubjectSong" >
		SELECT * FROM exam_subject_song WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ExamSubjectSong">
		SELECT * FROM exam_subject_song ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.entity.ExamSubjectSong" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_subject_song (examination_basic_id_,exam_subject_id_,level_,practice_num_,practice_song_id_list_,perform_num_,perform_song_id_list_,registration_fee_,create_time_,update_time_,tenant_id_)
		VALUES (#{examinationBasicId},#{examSubjectId},#{level},#{practiceNum},#{practiceSongIdList},#{performNum},#{performSongIdList},#{registrationFee},NOW(),NOW(),#{tenantId})
	</insert>

	<insert id="batchInsert" parameterType="com.keao.edu.user.entity.ExamSubjectSong" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_subject_song (examination_basic_id_,exam_subject_id_,level_,practice_num_,practice_song_id_list_,perform_num_,perform_song_id_list_,registration_fee_,create_time_,update_time_,tenant_id_)
		VALUES
		<foreach collection="examSubjectSongs" item="examSubjectSong" separator=",">
			(#{examSubjectSong.examinationBasicId},#{examSubjectSong.examSubjectId},#{examSubjectSong.level},#{examSubjectSong.practiceNum},#{examSubjectSong.practiceSongIdList},#{examSubjectSong.performNum},#{examSubjectSong.performSongIdList},#{examSubjectSong.registrationFee},NOW(),NOW(),#{examSubjectSong.tenantId})
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.keao.edu.user.entity.ExamSubjectSong">
		UPDATE exam_subject_song
		<set>
			<if test="examinationBasicId != null">
			examination_basic_id_ = #{examinationBasicId},
			</if>
			<if test="performNum != null">
			perform_num_ = #{performNum},
			</if>
			<if test="id != null">
			id_ = #{id},
			</if>
			<if test="practiceSongIdList != null">
			practice_song_id_list_ = #{practiceSongIdList},
			</if>
			<if test="practiceNum != null">
			practice_num_ = #{practiceNum},
			</if>
			<if test="tenantId != null and tenantId != 0">
			tenant_id_ = #{tenantId},
			</if>
			<if test="examSubjectId != null">
			exam_subject_id_ = #{examSubjectId},
			</if>
			<if test="performSongIdList != null">
			perform_song_id_list_ = #{performSongIdList},
			</if>
			<if test="registrationFee != null">
			registration_fee_ = #{registrationFee},
			</if>
			<if test="updateTime != null">
			update_time_ = #{updateTime},
			</if>
			<if test="level != null">
			level_ = #{level},
			</if>
			<if test="createTime != null">
			create_time_ = #{createTime},
			</if>
		</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM exam_subject_song WHERE id_ = #{id} 
	</delete>

	<sql id="queryCondition">
		<where>
			<if test="examId!=null">
				AND ess.examination_basic_id_ = #{examId}
			</if>
			<if test="level!=null">
				AND ess.level_=#{level}
			</if>
			<if test="search!=null">
				AND (ess.exam_subject_id_=#{search} OR s.name_ LIKE CONCAT('%', #{search}, '%'))
			</if>
		</where>
	</sql>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ExamSubjectSong" parameterType="map">
		SELECT
			ess.*,
			s.id_ sub_id_,
			s.name_ sub_name_
		FROM exam_subject_song ess
			LEFT JOIN subject s ON ess.exam_subject_id_=s.id_
		<include refid="queryCondition"/>
		ORDER BY ess.id_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM exam_subject_song ess
		LEFT JOIN subject s ON ess.exam_subject_id_=s.id_
		<include refid="queryCondition"/>
	</select>

	<select id="queryExamSubjectSongs" resultMap="ExamSubjectSongDto" parameterType="map">
		SELECT
			ess.*,
			s.id_ sub_id_,
			s.name_ sub_name_
			FROM exam_subject_song ess
		LEFT JOIN subject s ON ess.exam_subject_id_=s.id_
		<include refid="queryCondition"/>
		ORDER BY ess.id_
		<include refid="global.limit"/>
	</select>

	<select id="countExamSubjectSongs" resultType="int">
		SELECT COUNT(*) FROM exam_subject_song ess
		LEFT JOIN subject s ON ess.exam_subject_id_=s.id_
		<include refid="queryCondition"/>
	</select>

	<select id="findByExam" resultMap="ExamSubjectSong">
		SELECT * FROM exam_subject_song WHERE examination_basic_id_=#{examId}
	</select>

    <select id="getExamSubjectSong" resultMap="ExamSubjectSong">
		SELECT * FROM exam_subject_song WHERE examination_basic_id_=#{examinationBasicId} AND exam_subject_id_ = #{examSubjectId} AND level_ = #{level}
	</select>

	<select id="getExamSubjectLevels" resultMap="ExamSubjectSong">
		SELECT * FROM exam_subject_song WHERE examination_basic_id_=#{examinationBasicId} AND exam_subject_id_ = #{examSubjectId} ORDER BY level_ ASC
	</select>
    <select id="countExamSongsWithSubject" resultType="int">
		SELECT COUNT(id_) FROM exam_subject_song WHERE examination_basic_id_=#{examId} AND exam_subject_id_=#{subjectId}
    </select>
</mapper>
