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

    <resultMap type="com.ym.mec.biz.dal.entity.CourseScheduleStudentMusicScore" id="CourseScheduleStudentMusicScore">
        <result column="id_" property="id"/>
        <result column="course_schedule_id_" property="courseScheduleId"/>
        <result column="user_id_" property="userId"/>
        <result column="user_type_" property="userType"/>
        <result column="down_status_" property="downStatus"/>
        <result column="music_score_accompaniment_id_" property="musicScoreAccompanimentId"/>
        <result column="play_status_" property="playStatus"/>
        <result column="accompaniment_play_status_" property="accompanimentPlayStatus"/>
        <result column="speed_" property="speed"/>
        <result column="mp3_url_" property="mp3Url"/>
        <result column="url_" property="url"/>
        <result column="music_score_name_" property="musicScoreName"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
    </resultMap>

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

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

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleStudentMusicScore" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO course_schedule_student_music_score (course_schedule_id_,user_id_,user_type_,down_status_,music_score_accompaniment_id_,play_status_,accompaniment_play_status_,speed_,create_time_,update_time_)
        VALUES(#{courseScheduleId},#{userId},#{userType},#{downStatus},#{musicScoreAccompanimentId},#{playStatus},#{accompanimentPlayStatus},#{speed},now(),now())
    </insert>
    <insert id="batchInsert">
        INSERT INTO course_schedule_student_music_score (course_schedule_id_,user_id_,user_type_,down_status_,music_score_accompaniment_id_,play_status_,accompaniment_play_status_,speed_,create_time_,update_time_)
        VALUE
        <foreach collection="scheduleStudentMusicScores" item="item" separator=",">
            (#{item.courseScheduleId},#{item.userId},#{item.userType},#{item.downStatus},#{item.musicScoreAccompanimentId},#{item.playStatus},#{item.accompanimentPlayStatus},#{item.speed},now(),now())
        </foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleStudentMusicScore">
        UPDATE course_schedule_student_music_score
        <set>
            <if test="accompanimentPlayStatus != null">
                accompaniment_play_status_ = #{accompanimentPlayStatus},
            </if>
            <if test="downStatus != null">
                down_status_ = #{downStatus},
            </if>
            <if test="playStatus != null">
                play_status_ = #{playStatus},
            </if>
            <if test="speed != null">
                speed_ = #{speed},
            </if>
            update_time_ = NOW()
        </set>
        WHERE id_ = #{id}
    </update>

    <update id="updateByMusicScore" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleStudentMusicScore">
        UPDATE course_schedule_student_music_score
        SET down_status_ = #{downStatus},play_status_ = #{playStatus},speed_ = #{speed},update_time_ = NOW()
        WHERE course_schedule_id_ = #{courseScheduleId}
        <if test="userId != null">
            AND user_id_ = #{userId}
        </if>
        <if test="musicScoreAccompanimentId != null">
            AND music_score_accompaniment_id_ = #{musicScoreAccompanimentId}
        </if>
    </update>
    <update id="closePlayStatus">
        UPDATE course_schedule_student_music_score SET play_status_ = 0,accompaniment_play_status_ = 0,update_time_ = NOW()
        WHERE course_schedule_id_ = #{scheduleId}
        <if test="musicScoreAccompanimentId != null">
            AND music_score_accompaniment_id_ = #{musicScoreAccompanimentId}
        </if>
        <if test="userId != null">
            AND user_id_ = #{userId}
        </if>
    </update>
    <update id="openPlayStatus">
        UPDATE course_schedule_student_music_score SET play_status_ = 1,update_time_ = NOW()
        WHERE course_schedule_id_ = #{scheduleId}
        AND music_score_accompaniment_id_ = #{musicScoreAccompanimentId}
    </update>
    <update id="openAccompanimentPlayStatus">
        UPDATE course_schedule_student_music_score SET accompaniment_play_status_ = 1,update_time_ = NOW()
        WHERE course_schedule_id_ = #{scheduleId} AND music_score_accompaniment_id_ = #{musicScoreAccompanimentId}
    </update>

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

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="CourseScheduleStudentMusicScore" parameterType="map">
        SELECT * FROM course_schedule_student_music_score
        <include refid="global.limit" />
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM course_schedule_student_music_score
	</select>
    <select id="queryByScoreIdAndCourseId"
            resultMap="CourseScheduleStudentMusicScore">
        SELECT cssm.*,smsa.mp3_url_,sms.url_,sms.name_ music_score_name_  FROM course_schedule_student_music_score cssm
        LEFT JOIN sys_music_score_accompaniment smsa ON cssm.music_score_accompaniment_id_ = smsa.id_
        LEFT JOIN sys_music_score sms ON sms.id_ = smsa.exam_song_id_
        <where>
            <if test="courseId != null">
                AND cssm.course_schedule_id_ = #{courseId}
            </if>
            <if test="musicScoreAccompanimentId != null">
                AND cssm.music_score_accompaniment_id_ = #{musicScoreAccompanimentId}
            </if>
            <if test="userType != null">
                AND cssm.user_type_ = #{userType}
            </if>
            <if test="userId != null">
                AND cssm.user_id_ = #{userId}
            </if>
            <if test="downStatus != null">
                AND cssm.down_status_ = #{downStatus}
            </if>
        </where>
    </select>
</mapper>
