<?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.ym.mec.biz.dal.dao.SysMusicScoreAccompanimentDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.SysMusicScoreAccompaniment" id="SysMusicScoreAccompaniment">
		<result column="id_" property="id" />
		<result column="exam_song_id_" property="examSongId" />
		<result column="subject_id_" property="subjectId" />
		<result column="subject_name_" property="subjectName" />
		<result column="mp3_url_" property="mp3Url" />
		<result column="metronome_mp3_url_" property="metronomeMp3Url" />
		<result column="xml_url_" property="xmlUrl" />
		<result column="name_" property="examSongName" />
		<result column="categories_name_" property="categoriesName" />
		<result column="categories_id_" property="categoriesId" />
		<result column="parent_categories_id_" property="parentCategoriesId" />
		<result column="type_" property="type" />
		<result column="url_" property="url" />
		<result column="metronome_url_" property="metronomeUrl" />
		<result column="midi_url_" property="midiUrl" />
		<result column="del_flag_" property="delFlag" />
		<result column="speed_" property="speed" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="is_show_fingering_" property="isShowFingering" />
		<result column="is_open_metronome_" property="isOpenMetronome" />
		<result column="memo_" property="memo" />
		<result column="rank_ids_" property="rankIds" />
		<result column="render_from_" property="renderFrom" />
		<result column="enable_evaluation_" property="enableEvaluation" />
		<result column="client_type_" property="clientType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="play_mode_" property="playMode" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler" />
		<result column="muse_score_url_" property="museScoreUrl" />
		<result column="muse_score_memo_" property="museScoreMemo" />
		<result column="track_" property="track" />
		<result column="ext_config_json_" property="extConfigJson" />
	</resultMap>

	<delete id="deleteBySongId">
		UPDATE sys_music_score_accompaniment SET del_flag_ = 1,update_time_ = NOW() WHERE exam_song_id_ = #{id}
	</delete>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="SysMusicScoreAccompaniment" >
		SELECT sesa.id_,sesa.exam_song_id_,sesa.subject_id_,CASE WHEN sesa.metronome_mp3_url_ IS NULL OR sesa.metronome_mp3_url_ = '' THEN sesa.mp3_url_ ELSE sesa.metronome_mp3_url_ END url_,sesa.speed_,
		sesa.xml_url_,sesa.is_show_fingering_,sesa.is_open_metronome_,sesa.memo_,sesa.track_,
		ses.name_,ses.type_,ses.url_ mp3_url_,sesc.name_ categories_name_,sesc.id_ categories_id_,ses.client_type_,ses.metronome_url_,ses.midi_url_
		FROM sys_music_score ses
		LEFT JOIN sys_music_score_accompaniment sesa ON ses.id_ = sesa.exam_song_id_
		LEFT JOIN sys_music_score_categories sesc ON sesc.id_ = ses.music_score_categories_id_
		WHERE sesa.id_ = #{id}
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="SysMusicScoreAccompaniment">
		SELECT * FROM sys_music_score_accompaniment ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.SysMusicScoreAccompaniment" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO sys_music_score_accompaniment (exam_song_id_,subject_id_,mp3_url_,metronome_mp3_url_,speed_,xml_url_,create_time_,update_time_,is_open_metronome_,track_)
		VALUES(#{examSongId},#{subjectId},#{mp3Url},#{metronomeMp3Url},#{speed},#{xmlUrl},NOW(),NOW(),#{isOpenMetronome},#{track})
	</insert>
    <insert id="batchInsert">
		INSERT INTO sys_music_score_accompaniment (exam_song_id_,subject_id_,mp3_url_,metronome_mp3_url_,speed_,xml_url_,
		                                           create_time_,update_time_,is_show_fingering_,memo_,is_open_metronome_,track_)
		VALUES
		<foreach collection="sysMusicScoreAccompaniments" item="item"  separator=",">
			(#{sysMusicScoreId},#{item.subjectId},#{item.mp3Url},#{item.metronomeMp3Url},#{item.speed},
			 #{item.xmlUrl},NOW(),NOW(),#{item.isShowFingering},#{item.memo},#{item.isOpenMetronome},#{item.track})
		</foreach>
	</insert>

    <!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.SysMusicScoreAccompaniment">
		UPDATE sys_music_score_accompaniment
		<set>
		<if test="isShowFingering != null">
			is_show_fingering_ = #{isShowFingering},
		</if>
		<if test="isOpenMetronome != null">
			is_open_metronome_ = #{isOpenMetronome},
		</if>
		<if test="memo != null">
			memo_ = #{memo},
		</if>
		<if test="speed != null">
			speed_ = #{speed},
		</if>
		<if test="examSongId != null">
			exam_song_id_ = #{examSongId},
		</if>
		<if test="track != null">
			track_ = #{track},
		</if>
			subject_id_ = #{subjectId},xml_url_ = #{xmlUrl},
			mp3_url_ = #{mp3Url},metronome_mp3_url_ = #{metronomeMp3Url}, update_time_ = NOW()
	</set>WHERE id_ = #{id}
	</update>
	<update id="batchDel">
		UPDATE sys_music_score_accompaniment SET del_flag_ = 1,update_time_ = NOW() WHERE id_ IN
		<foreach collection="delExamSongAccompanimentIds" item="item" open="(" close=")" separator=",">
			#{item}
		</foreach>
	</update>
	<update id="batchUpdate">
		<foreach collection="sysMusicScoreAccompaniments" item="item" separator=";">
			UPDATE sys_music_score_accompaniment
			<set>
				<if test="item.isShowFingering != null">
					is_show_fingering_ = #{item.isShowFingering},
				</if>
				<if test="isOpenMetronome != null">
					is_open_metronome_ = #{isOpenMetronome},
				</if>
				<if test="item.memo != null">
					memo_ = #{item.memo},
				</if>
				<if test="item.speed != null">
					speed_ = #{item.speed},
				</if>
				<if test="item.examSongId != null">
					exam_song_id_ = #{item.examSongId},
				</if>
				<if test="item.track != null">
					track_ = #{item.track},
				</if>
				subject_id_ = #{item.subjectId},xml_url_ = #{item.xmlUrl},metronome_mp3_url_ = #{item.metronomeMp3Url},
				mp3_url_ = #{item.mp3Url}, update_time_ = NOW()
			</set> WHERE id_ = #{item.id}
		</foreach>
	</update>

	<!-- 根据主键删除一条记录 -->
	<update id="delete" >
		UPDATE sys_music_score_accompaniment SET del_flag_ = 1,update_time_ = NOW() WHERE id_ = #{id}
	</update>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="SysMusicScoreAccompaniment" parameterType="map">
		SELECT
		    sesa.*,ses.name_,ses.type_,ses.url_,
		       s.name_ subject_name_,sesc.name_ categories_name_,sesc.id_ categories_id_,sesc.parent_id_ parent_categories_id_,ses.client_type_,
		ses.rank_ids_,ses.render_from_,ses.enable_evaluation_,ses.metronome_url_,ses.midi_url_,ses.play_mode_,ses.render_from_,ses.ext_config_json_
		FROM sys_music_score ses
		LEFT JOIN sys_music_score_accompaniment sesa ON ses.id_ = sesa.exam_song_id_
		LEFT JOIN sys_music_score_categories sesc ON sesc.id_ = ses.music_score_categories_id_
		LEFT JOIN `subject` s ON s.id_ = sesa.subject_id_
		<include refid="queryPageSql"/>
		ORDER BY ses.order_,ses.name_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(sesa.id_)
		FROM sys_music_score ses
		LEFT JOIN sys_music_score_accompaniment sesa ON ses.id_ = sesa.exam_song_id_
		LEFT JOIN sys_music_score_categories sesc ON sesc.id_ = ses.music_score_categories_id_
		<include refid="queryPageSql"/>
	</select>

	<select id="findSubjectByMusicScoreId" resultType="java.lang.Integer">
		SELECT subject_id_ FROM sys_music_score_accompaniment
		<where>
			del_flag_ = 0
			<if test="sysMusicScoreId != null">
				AND exam_song_id_ = #{sysMusicScoreId}
			</if>
			<if test="idList != null and idList.size > 0">
				AND id_ NOT IN
				<foreach collection="idList" open="(" close=")" item="item" separator=",">
					#{item}
				</foreach>
			</if>
		</where>
	</select>
	<select id="queryAccPage" resultMap="SysMusicScoreAccompaniment">
		SELECT sesa.*,ses.name_,ses.type_,ses.url_,s.name_ subject_name_,sesc.name_ categories_name_,sesc.id_ categories_id_,sesc.parent_id_ parent_categories_id_,ses.client_type_,
		ses.enable_evaluation_,ses.metronome_url_,ses.midi_url_,ses.play_mode_,ses.render_from_,ses.ext_config_json_
		FROM sys_music_score ses
		LEFT JOIN sys_music_score_accompaniment sesa ON ses.id_ = sesa.exam_song_id_
		LEFT JOIN sys_music_score_categories sesc ON sesc.id_ = ses.music_score_categories_id_
		LEFT JOIN subject s ON s.id_ = sesa.subject_id_
		<include refid="queryPageSql"/>
		ORDER BY ses.order_,ses.name_
	</select>

	<select id="findAccCount" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT sesa.id_)
		FROM sys_music_score ses
		LEFT JOIN sys_music_score_accompaniment sesa ON ses.id_ = sesa.exam_song_id_
		LEFT JOIN sys_music_score_categories sesc ON sesc.id_ = ses.music_score_categories_id_
		<include refid="queryPageSql"/>
	</select>
    <select id="querySubjectIds" resultMap="com.ym.mec.biz.dal.dao.SubjectDao.Subject">
		SELECT s.* FROM sys_music_score_accompaniment sesa
		LEFT JOIN `subject` s ON s.id_ = sesa.subject_id_
		LEFT JOIN sys_music_score sms ON sms.id_ = sesa.exam_song_id_
		LEFT JOIN sys_music_score_categories sesc ON sesc.id_ = sms.music_score_categories_id_
		WHERE sesa.del_flag_ = 0 AND sesa.subject_id_ IS NOT NULL
		<if test="categoriesIdList != null and categoriesIdList.size > 0">
			AND sesc.id_ IN
			<foreach collection="categoriesIdList" separator="," item="item" open="(" close=")">
				#{item}
			</foreach>
		</if>
		GROUP BY sesa.subject_id_
	</select>

	<sql id="queryPageSql">
		<where>
			sesa.del_flag_ = 0
			<if test="enable != null">
				AND sesc.enable_ = #{enable}
			</if>
			<if test="parentId != null">
				AND sesc.parent_id_ = #{parentId}
			</if>
			<!-- <if test="organId != null and organId != ''">
				AND INTE_ARRAY(#{organId},sesc.organ_id_)
			</if> -->
			<if test="search != null and search != ''">
				AND (sesa.id_ = #{search} OR ses.name_ LIKE CONCAT('%',#{search},'%'))
			</if>
			<if test="sysMusicScoreId != null">
				AND sesa.exam_song_id_ = #{sysMusicScoreId}
			</if>
			<if test="showFlag != null">
				AND ses.show_flag_ = #{showFlag}
			</if>
			<if test="categoriesIdList != null">
				AND sesc.id_ IN
				<foreach collection="categoriesIdList" separator="," item="item" open="(" close=")">
					#{item}
				</foreach>
			</if>
			<if test="filterCategoriesIds != null and filterCategoriesIds != ''">
				AND find_in_set(sesc.id_,#{filterCategoriesIds}) = 0
			</if>
			<if test="categoriesIds != null and categoriesIds != ''">
				AND find_in_set(sesc.id_,#{categoriesIds}) > 0
			</if>
			<if test="parentId != null">
				AND sesc.parent_id_ = #{parentId}
			</if>
			<if test="subjectId != null">
				AND (sesa.subject_id_ = #{subjectId} OR sesa.subject_id_ IS NULL)
			</if>
			<if test="isShowFingering != null">
				AND sesa.is_show_fingering_ = #{isShowFingering}
			</if>
			<if test="clientType != null">
				AND ses.client_type_ = #{clientType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
			<if test="type != null and type == 'COMMON'">
				AND ses.type_ = #{type}
			</if>
			<if test="type != null and type == 'ALL'">
				<if test="createUserId != null">
					AND (ses.type_ = 'COMMON' OR (ses.create_user_id_ = #{createUserId} AND ses.type_ = 'PERSON'))
				</if>
			</if>
			<if test="type != null and type == 'PERSON'">
				<if test="createUserId != null">
					AND ses.type_ = #{type} AND ses.create_user_id_ = #{createUserId}
				</if>
				<if test="createUserId == null">
					AND ses.type_ = #{type}
				</if>
			</if>
			<if test="type == null or type == ''">
				<if test="createUserId != null">
					AND ses.create_user_id_ = #{createUserId}
				</if>
			</if>
		</where>
	</sql>
</mapper>
