<?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.MusicGroupSchoolTermStudentCourseDetailDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.MusicGroupSchoolTermStudentCourseDetail" id="MusicGroupSchoolTermStudentCourseDetail">
		<result column="id_" property="id" />
		<result column="music_group_school_term_course_detail_id_" property="musicGroupSchoolTermCourseDetailId" />
		<result column="user_id_" property="userId" />
		<result column="member_course_minutes_" property="memberCourseMinutes" />
		<result column="pre_member_course_minutes_" property="preMemberCourseMinutes" />
		<result column="mix_course_minutes_" property="mixCourseMinutes" />
		<result column="single_course_minutes_" property="singleCourseMinutes" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="MusicGroupSchoolTermStudentCourseDetail" >
		SELECT * FROM music_group_school_term_student_course_detail WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="MusicGroupSchoolTermStudentCourseDetail">
		SELECT * FROM music_group_school_term_student_course_detail ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.MusicGroupSchoolTermStudentCourseDetail" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO music_group_school_term_student_course_detail
		    (music_group_school_term_course_detail_id_,user_id_,member_course_minutes_,
		   pre_member_course_minutes_,mix_course_minutes_,single_course_minutes_,create_time_,update_time_)
		   VALUES(#{musicGroupSchoolTermCourseDetailId},#{userId},#{memberCourseMinutes},#{preMemberCourseMinutes},#{mixCourseMinutes},#{singleCourseMinutes},NOW(),NOW())
	</insert>
	<insert id="init">
		INSERT INTO music_group_school_term_student_course_detail
		(music_group_school_term_course_detail_id_,user_id_,member_course_minutes_,create_time_,update_time_)
		VALUES
		<foreach collection="studentIds" item="userId" separator=",">
			(#{studentCourseDetail.musicGroupSchoolTermCourseDetailId},#{userId},#{studentCourseDetail.memberCourseMinutes},NOW(),NOW())
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.MusicGroupSchoolTermStudentCourseDetail">
		UPDATE music_group_school_term_student_course_detail <set>
		<if test="mixCourseMinutes != null">
			mix_course_minutes_ = #{mixCourseMinutes},
		</if>
		<if test="singleCourseMinutes != null">
			single_course_minutes_ = #{singleCourseMinutes},
		</if>
		<if test="userId != null">
			user_id_ = #{userId},
		</if>
		<if test="musicGroupSchoolTermCourseDetailId != null">
			music_group_school_term_course_detail_id_ = #{musicGroupSchoolTermCourseDetailId},
		</if>
		<if test="memberCourseMinutes != null">
			member_course_minutes_ = #{memberCourseMinutes},
		</if>
		<if test="preMemberCourseMinutes != null">
			pre_member_course_minutes_ = #{preMemberCourseMinutes},
		</if>
			update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>
    <update id="updateMinutes">
		UPDATE music_group_school_term_student_course_detail SET pre_member_course_minutes_ = #{preMemberCourseMinutes} + pre_member_course_minutes_,
																 mix_course_minutes_ = #{mixCourseMinutes} + mix_course_minutes_,
																 single_course_minutes_ = #{singleCourseMinutes} + single_course_minutes_
		WHERE music_group_school_term_course_detail_id_ = #{musicGroupSchoolTermCourseDetailId} AND user_id_ IN
		<foreach collection="studentIds" separator="," open="(" close=")" item="userId">
			#{userId}
		</foreach>
	</update>
    <update id="cutMinutes">
		UPDATE music_group_school_term_student_course_detail SET pre_member_course_minutes_ = pre_member_course_minutes_ - #{preMemberCourseMinutes},
																 mix_course_minutes_ = mix_course_minutes_ - #{mixCourseMinutes},
																 single_course_minutes_ = single_course_minutes_ - #{singleCourseMinutes}
		WHERE music_group_school_term_course_detail_id_ = #{musicGroupSchoolTermCourseDetailId} AND user_id_ = #{studentId}
	</update>
	<update id="cleanMinutes">
		UPDATE music_group_school_term_student_course_detail SET pre_member_course_minutes_ = 0,
																 mix_course_minutes_ = 0,
																 single_course_minutes_ = 0
		WHERE music_group_school_term_course_detail_id_ = #{musicGroupSchoolTermCourseDetailId}
	</update>

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

	<delete id="deleteByDetailId">
		DELETE FROM music_group_school_term_student_course_detail
		WHERE music_group_school_term_course_detail_id_ = #{courseDetailId}
		<if test="studentId != null">
			AND user_id_ = #{studentId}
		</if>
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="MusicGroupSchoolTermStudentCourseDetail" parameterType="map">
		SELECT * FROM music_group_school_term_student_course_detail ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM music_group_school_term_student_course_detail
	</select>

	<select id="getMixCourseMinutes" resultType="java.lang.Integer">
		SELECT MIN(member_course_minutes_ - pre_member_course_minutes_)
		FROM music_group_school_term_student_course_detail
		WHERE music_group_school_term_course_detail_id_ = #{courseDetailId} AND user_id_ IN
		<foreach collection="studentIds" open="(" close=")" item="userId" separator=",">
			#{userId}
		</foreach>
		GROUP BY music_group_school_term_course_detail_id_
	</select>
	<select id="getMaxSingleCourseMinutes" resultType="java.lang.Integer">
		SELECT MAX(single_course_minutes_)
		FROM music_group_school_term_student_course_detail
		WHERE music_group_school_term_course_detail_id_ = #{courseDetailId} AND user_id_ IN
		<foreach collection="studentIds" open="(" close=")" item="userId" separator=",">
			#{userId}
		</foreach>
		GROUP BY music_group_school_term_course_detail_id_
	</select>

	<select id="getMaxMixCourseMinutes" resultType="java.lang.Integer">
		SELECT MAX(mix_course_minutes_)
		FROM music_group_school_term_student_course_detail
		WHERE music_group_school_term_course_detail_id_ = #{courseDetailId} AND user_id_ IN
		<foreach collection="studentIds" open="(" close=")" item="userId" separator=",">
			#{userId}
		</foreach>
		GROUP BY music_group_school_term_course_detail_id_
	</select>
	<select id="getNoCourseStudentName" resultType="java.lang.String">
		SELECT GROUP_CONCAT(DISTINCT su.username_) FROM music_group_school_term_student_course_detail mgs
		LEFT JOIN sys_user su ON su.id_ = mgs.user_id_
		WHERE music_group_school_term_course_detail_id_ = #{musicGroupSchoolTermCourseDetailId} AND pre_member_course_minutes_ = 0
	</select>
	<select id="findByDetailId" resultMap="MusicGroupSchoolTermStudentCourseDetail">
		SELECT * FROM music_group_school_term_student_course_detail WHERE music_group_school_term_course_detail_id_ = #{courseDetailId}
	</select>
	<select id="findByDetailIdAndStudentId" resultMap="MusicGroupSchoolTermStudentCourseDetail">
		SELECT * FROM music_group_school_term_student_course_detail WHERE music_group_school_term_course_detail_id_ = #{courseDetailId} AND user_id_ = #{userId}
	</select>
</mapper>
