<?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.ExamSongDao">
	
	<resultMap type="com.keao.edu.user.entity.ExamSong" id="ExamSong">
		<result column="id_" property="id" />
		<result column="song_name_" property="songName" />
		<result column="song_author_" property="songAuthor" />
		<result column="subject_list_" property="subjectList" />
		<result column="subject_names_" property="subjectNames" />
		<result column="level_list_" property="levelList" />
		<result column="type_" property="type"  typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler"/>
		<result column="file_url_list_" property="fileUrlList" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExamSong" >
		SELECT * FROM exam_song WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ExamSong">
		SELECT * FROM exam_song WHERE tenant_id_ = #{tenantId} ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.entity.ExamSong" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_song (id_,song_name_,song_author_,subject_list_,level_list_,type_,file_url_list_,create_time_,update_time_,tenant_id_)
		VALUES(#{id},#{songName},#{songAuthor},#{subjectList},#{levelList},#{type, typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},#{fileUrlList},NOW(),NOW(),#{tenantId})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.keao.edu.user.entity.ExamSong">
		UPDATE exam_song
		<set>
			<if test="levelList != null">
				level_list_ = #{levelList},
			</if>
			<if test="subjectList != null">
				subject_list_ = #{subjectList},
			</if>
			<if test="fileUrlList != null">
				file_url_list_ = #{fileUrlList},
			</if>
			<if test="songAuthor != null">
				song_author_ = #{songAuthor},
			</if>
			<if test="type != null">
				type_ = #{type, typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="songName != null">
				song_name_ = #{songName},
			</if>
			<if test="delFlag != null">
				del_flag_ = #{delFlag}
			</if>
			<if test="tenantId">
				tenant_id_ = #{tenantId},
			</if>
			update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<update id="delete" >
		UPDATE exam_song SET del_flag_=1 WHERE id_ = #{id}
	</update>

	<sql id="queryCondition">
		<where>
			es.del_flag_ = 0
			<if test="tenantId != null">
				AND es.tenant_id_ = #{tenantId}
			</if>
			<if test="subjectList!=null">
				AND FIND_IN_SET(#{subjectList}, es.subject_list_)
			</if>
			<if test="level!=null">
				AND FIND_IN_SET(#{level},level_list_)
			</if>
			<if test="type!=null">
				AND type_=#{type, typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler}
			</if>
			<if test="songName!=null">
				AND es.song_name_ LIKE CONCAT('%', #{songName}, '%')
			</if>
			<if test="search!=null">
				AND (es.id_=#{search} OR es.song_name_ LIKE CONCAT('%', #{search}, '%') OR es.song_author_ LIKE CONCAT('%', #{search}, '%'))
			</if>
		</where>
	</sql>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ExamSong" parameterType="map">
		SELECT es.*,GROUP_CONCAT(s.name_) subject_names_ FROM exam_song es
		LEFT JOIN subject s ON FIND_IN_SET(s.id_, es.subject_list_)
		<include refid="queryCondition"/>
		GROUP BY es.id_
		ORDER BY id_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM exam_song es
		<include refid="queryCondition"/>
	</select>
    <select id="getExamSongs" resultMap="ExamSong">
		SELECT * FROM exam_song WHERE FIND_IN_SET(id_,#{ids})
	</select>
	<select id="getWithSubject" resultMap="ExamSong">
		SELECT * FROM exam_song WHERE FIND_IN_SET(#{subjectId}, subject_list_) AND del_flag_=0
	</select>

    <select id="getWithLevelAndType" resultMap="ExamSong">
		SELECT * FROM exam_song
		WHERE tenant_id_=#{tenantId}
		AND level_list_=#{level}
		AND type_=#{songType, typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler}
    </select>
	<sql id="querySongPageMap">
		<where>
			es.del_flag_ = 0
			<if test="subjectList != null">
				AND FIND_IN_SET(#{subjectList},es.subject_list_)
			</if>
			<if test="level != null">
				AND FIND_IN_SET(#{level},es.level_list_)
			</if>
			<if test="tenantId != null">
				AND es.tenant_id_ = #{tenantId}
			</if>
		</where>
	</sql>
	<select id="countSongPage" resultType="java.lang.Integer">
		SELECT COUNT(es.id_) FROM exam_song es
		<include refid="querySongPageMap"/>
	</select>
	<select id="querySongPage" resultMap="ExamSong">
		SELECT es.* FROM exam_song es
		<include refid="querySongPageMap"/>
		<include refid="global.limit"/>
	</select>

	<select id="getWithIds" resultMap="ExamSong">
		SELECT * FROM exam_song WHERE id_ IN
		<foreach collection="ids" item="id" separator="," open="(" close=")">
			#{id}
		</foreach>
	</select>
</mapper>
