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

	<resultMap type="com.ym.mec.biz.dal.entity.CourseScheduleStudentPayment" id="CourseScheduleStudentPayment">
		<result column="id_" property="id" />
        <result column="group_type_" property="groupType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="music_group_id_" property="musicGroupId"/>
		<result column="course_schedule_id_" property="courseScheduleId" />
		<result column="user_id_" property="userId" />
		<result column="original_price_" property="originalPrice"/>
		<result column="expect_price_" property="expectPrice" />
		<result column="actual_price_" property="actualPrice" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="settlement_time_" property="settlementTime" />
		<result column="class_group_id_" property="classGroupId" />
		<result column="batch_no_" property="batchNo"/>
		<result column="be_merged_" property="beMerged"/>
		<result column="tenant_id_" property="tenantId" />
		<result column="activity_user_mapper_id_" property="activityUserMapperId" />
	</resultMap>

	<resultMap type="com.ym.mec.biz.dal.dto.StudentCourseTimesDto" id="studentCourseTimesDto">
		<result column="user_id_" property="userId" />
        <result column="totalTimes" property="totalCourseTimes"/>
		<result column="vipTimes" property="vipCourseTimes" />
		<result column="practiceTmes" property="practiceCourseTimes" />
		<result column="freePracticeTimes" property="freePracticeCourseTimes" />
	</resultMap>

	<resultMap id="CourseScheduleStudentPaymentDto" type="com.ym.mec.biz.dal.entity.CourseScheduleStudentPaymentDto" extends="CourseScheduleStudentPayment">
		<result column="course_status_" property="courseStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="course_start_time_" property="courseStartTime"/>
	</resultMap>
	
	<resultMap type="com.ym.mec.biz.dal.dto.Mapper" id="Mapper">
		<result column="key_" property="key" />
		<result column="value_" property="value" />
	</resultMap>

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

	<!-- 全查询 -->
	<select id="findAll" resultMap="CourseScheduleStudentPayment">
		SELECT * FROM course_schedule_student_payment where tenant_id_ = #{tenantId} ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleStudentPayment" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO course_schedule_student_payment (id_,group_type_,music_group_id_,course_schedule_id_,user_id_,
			original_price_,expect_price_,actual_price_,create_time_,update_time_,settlement_time_,class_group_id_,batch_no_,be_merged_,tenant_id_,activity_user_mapper_id_)
		VALUES(#{id},#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{musicGroupId},#{courseScheduleId},#{userId},
			#{originalPrice},#{expectPrice},#{actualPrice},#{createTime},NOW(),#{settlementTime},#{classGroupId},#{batchNo},#{beMerged},#{tenantId},#{activityUserMapperId})
	</insert>
    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id_">
		INSERT INTO course_schedule_student_payment (id_,group_type_,music_group_id_,course_schedule_id_,user_id_,expect_price_,
				original_price_,actual_price_,create_time_,update_time_,settlement_time_,class_group_id_,batch_no_,be_merged_,tenant_id_,activity_user_mapper_id_)
		VALUE
		<foreach collection="list" item="data" separator=",">
			(#{data.id},#{data.groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{data.musicGroupId},#{data.courseScheduleId},#{data.userId},#{data.expectPrice},
			#{data.originalPrice},#{data.actualPrice},now(),now(),#{data.settlementTime},#{data.classGroupId},#{data.batchNo},#{data.beMerged},#{data.tenantId},#{data.activityUserMapperId})
		</foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleStudentPayment">
		UPDATE course_schedule_student_payment
		<set>
			<if test="userId != null">
				user_id_ = #{userId},
			</if>
			<if test="courseScheduleId != null">
				course_schedule_id_ = #{courseScheduleId},
			</if>
            <if test="groupType != null">
                group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
			<if test="settlementTime != null">
				settlement_time_ = #{settlementTime},
			</if>
			<if test="expectPrice != null">
				expect_price_ = #{expectPrice},
			</if>
			<if test="actualPrice != null">
				actual_price_ = #{actualPrice},
			</if>
			<if test="createTime != null">
				create_time_ = #{createTime},
			</if>
			<if test="classGroupId != null">
				class_group_id_ = #{classGroupId},
			</if>
			<if test="batchNo != null">
				batch_no_ = #{batchNo},
			</if>
			<if test="originalPrice != null">
				original_price_ = #{originalPrice},
			</if>
			<if test="beMerged != null">
				be_merged_ = #{beMerged},
			</if>
				update_time_ = NOW()
		</set> WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>

	<update id="batchUpdate" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleStudentPayment">
		<foreach collection="courseScheduleStudentPayments" item="courseScheduleStudentPayment" separator=";">
			UPDATE course_schedule_student_payment
			<set>
				<if test="courseScheduleStudentPayment.userId != null">
					user_id_ = #{courseScheduleStudentPayment.userId},
				</if>
				<if test="courseScheduleStudentPayment.courseScheduleId != null">
					course_schedule_id_ = #{courseScheduleStudentPayment.courseScheduleId},
				</if>
				<if test="courseScheduleStudentPayment.groupType != null">
					group_type_ = #{courseScheduleStudentPayment.groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
				</if>
				<if test="courseScheduleStudentPayment.musicGroupId != null">
					music_group_id_ = #{courseScheduleStudentPayment.musicGroupId},
				</if>
				<if test="courseScheduleStudentPayment.settlementTime != null">
					settlement_time_ = #{courseScheduleStudentPayment.settlementTime},
				</if>
				<if test="courseScheduleStudentPayment.expectPrice != null">
					expect_price_ = #{courseScheduleStudentPayment.expectPrice},
				</if>
				<if test="courseScheduleStudentPayment.actualPrice != null">
					actual_price_ = #{courseScheduleStudentPayment.actualPrice},
				</if>
				<if test="courseScheduleStudentPayment.createTime != null">
					create_time_ = #{courseScheduleStudentPayment.createTime},
				</if>
				<if test="courseScheduleStudentPayment.classGroupId != null">
					class_group_id_ = #{courseScheduleStudentPayment.classGroupId},
				</if>
				<if test="courseScheduleStudentPayment.batchNo != null">
					batch_no_ = #{courseScheduleStudentPayment.batchNo},
				</if>
				<if test="courseScheduleStudentPayment.originalPrice != null">
					original_price_ = #{courseScheduleStudentPayment.originalPrice},
				</if>
				<if test="courseScheduleStudentPayment.beMerged != null">
					be_merged_ = #{courseScheduleStudentPayment.beMerged},
				</if>
				update_time_ = NOW()
			</set> WHERE id_ = #{courseScheduleStudentPayment.id} and tenant_id_ = #{courseScheduleStudentPayment.tenantId}
		</foreach>
	</update>
    <update id="adjustPlayMidi">
		UPDATE course_schedule_student_payment cssp
		<set>
			<if test="content == null or content == ''">
				cssp.open_play_midi_ = NULL,cssp.update_time_ = NOW()
			</if>
			<if test="content != null and content != ''">
				cssp.open_play_midi_ = #{content},cssp.update_time_ = NOW()
			</if>
		</set>
		<where>
			cssp.course_schedule_id_ = #{courseScheduleId}
			<if test="userId != null and userId != ''">
				AND FIND_IN_SET(cssp.user_id_,#{userId})
			</if>
		</where>
	</update>
	<update id="adjustExamSong">
		UPDATE course_schedule_student_payment SET exam_song_download_json_ = #{examSongJson}
		WHERE course_schedule_id_ = #{roomId}
		<if test="userId != null">
			AND user_id_ = #{userId}
		</if>
	</update>
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM course_schedule_student_payment WHERE id_ = #{id}
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="CourseScheduleStudentPayment" parameterType="map">
		SELECT * FROM course_schedule_student_payment where tenant_id_ = #{tenantId} ORDER BY id_ <include refid="global.limit"/>
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM course_schedule_student_payment where tenant_id_ = #{tenantId}
	</select>

    <select id="findByCourseScheduleIds" resultMap="CourseScheduleStudentPayment">
		SELECT * FROM course_schedule_student_payment WHERE course_schedule_id_ IN
		<foreach collection="ids" item="id" open="(" close=")" separator=",">
			#{id}
		</foreach>
    </select>

	<resultMap id="StudentAttendanceStatisticsResp" type="com.ym.mec.biz.dal.dto.StudentAttendanceStatisticsResponse">
		<result property="userId" column="user_id_"/>
		<result property="studentName" column="username_"/>
	</resultMap>

	<select id="findStudentByClassGroup" resultMap="StudentAttendanceStatisticsResp">
		SELECT
			cssp.user_id_,
			su.username_
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN sys_user su ON cssp.user_id_ = su.id_
		WHERE
			cssp.class_group_id_ = #{classGroupId}
		GROUP BY
			cssp.user_id_
		ORDER BY cssp.user_id_
		<include refid="global.limit"/>
	</select>

	<select id="countStudentByClassGroup" resultType="int">
		SELECT
			COUNT(DISTINCT cssp.user_id_)
		FROM
			course_schedule_student_payment cssp
		WHERE
			cssp.class_group_id_ = #{classGroupId}
	</select>
    <select id="countSurplusCourseFee" resultType="java.math.BigDecimal">
		SELECT
			SUM(expect_price_)
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
		WHERE
			(cs.del_flag_ != 1 OR cs.del_flag_ IS NULL)
			AND cssp.user_id_ = #{userId}
			AND CONCAT(cs.class_date_	,' ',cs.start_class_time_) &gt; NOW()
			AND cs.class_group_id_ = #{classGroupId} AND cs.pre_course_flag_ = 0
    </select>
	<select id="countAllCourseFee" resultType="java.math.BigDecimal">
		SELECT
			SUM(expect_price_)
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
		WHERE
		(cs.del_flag_ != 1 OR cs.del_flag_ IS NULL)
		AND cs.class_group_id_ = #{classGroupId} AND cs.pre_course_flag_ = 0
    </select>
	<select id="findNotStartCourseStudentPaymentIdsWithClassGroupAndStudent" resultType="int">
		SELECT
			cssp.id_
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
		WHERE
			cssp.class_group_id_ = #{classGroupId}
			AND cssp.user_id_ = #{userId}
			AND CONCAT(cs.class_date_,' ',cs.start_class_time_) > NOW() AND cs.pre_course_flag_ = 0
	</select>
	<select id="findNotStartCourseStudentPaymentsWithClassGroupAndStudent" resultMap="CourseScheduleStudentPayment">
		SELECT
			cssp.*
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
		WHERE
			cssp.class_group_id_ = #{classGroupId}
			AND cssp.user_id_ = #{userId}
			AND CONCAT(cs.class_date_,' ',cs.start_class_time_) > NOW() AND cs.pre_course_flag_ = 0
	</select>

    <select id="findVipGroupCoursePrice" resultType="java.math.BigDecimal">
		SELECT
			cssp.expect_price_
		FROM
			course_schedule cs
			LEFT JOIN course_schedule_student_payment cssp ON cs.id_ = cssp.course_schedule_id_
		WHERE
			cs.music_group_id_ = #{vipGroupId}
			AND cs.teach_mode_ = #{teachMode}
			AND cs.group_type_ = 'VIP'
			AND cssp.expect_price_ IS NOT NULL
		ORDER BY CONCAT(cs.class_date_,' ',cs.start_class_time_) DESC LIMIT 1
	</select>
    <select id="findStudentCourseNum" resultType="java.util.Map">
		SELECT COUNT(DISTINCT cssp.id_) 'value',cssp.music_group_id_ 'key' FROM course_schedule cs
		LEFT JOIN course_schedule_student_payment cssp ON cs.music_group_id_ = cssp.music_group_id_ AND cs.id_ = cssp.course_schedule_id_
		WHERE cs.music_group_id_ IN
		<foreach collection="musicGroupIds" separator="," item="item" open="(" close=")">
			#{item}
		</foreach>
		AND cs.group_type_ = #{groupType} AND cssp.user_id_ = #{userId}
		AND cs.status_ = 'NOT_START' AND cs.del_flag_ = 0 AND cs.pre_course_flag_ = 0
		GROUP BY cssp.music_group_id_
	</select>
    <select id="countStudentCourseNumWithGroup" resultType="int">
		SELECT COUNT(id_) FROM course_schedule_student_payment WHERE group_type_=#{groupType} AND music_group_id_=#{groupId}
	</select>
    <select id="queryMusicStudentPer" resultType="java.lang.Integer">
		SELECT cssp.user_id_ FROM course_schedule_student_payment cssp
		LEFT JOIN music_group mg ON cssp.music_group_id_ = mg.id_ AND cssp.group_type_ = 'MUSIC'
		LEFT JOIN student_registration sr ON cssp.user_id_ = sr.user_id_ AND cssp.music_group_id_ = sr.music_group_id_
		WHERE mg.status_ = 'PROGRESS' AND sr.music_group_status_ != 'QUIT' and cssp.tenant_id_ = #{tenantId}
		GROUP BY cssp.user_id_
	</select>
    <select id="queryVipStudentPer" resultType="java.lang.Integer">
		SELECT cssp.user_id_ FROM course_schedule_student_payment cssp
		LEFT JOIN vip_group vg ON cssp.music_group_id_ = vg.id_
		WHERE vg.group_status_ IN (2,4) AND cssp.group_type_ = 'VIP' and cssp.tenant_id_ = #{tenantId}
		GROUP BY cssp.user_id_
	</select>
    <select id="countCourseStudentNum" resultType="java.util.Map">
		SELECT
			sa.course_schedule_id_ AS 'key',
			COUNT( DISTINCT sa.user_id_ ) AS 'value'
		FROM
			course_schedule_student_payment sa
		WHERE course_schedule_id_ IN
			<foreach collection="courseIds" item="courseId" open="(" close=")" separator=",">
				#{courseId}
			</foreach>
		GROUP BY
			course_schedule_id_
	</select>
    <select id="checkStudentHaveCourse" resultType="int">
		SELECT COUNT(id_) FROM course_schedule_student_payment WHERE course_schedule_id_=#{courseScheduleId} AND user_id_=#{userId}
	</select>
	<select id="findByCourseSchedule" resultMap="CourseScheduleStudentPayment">
		SELECT * FROM course_schedule_student_payment WHERE  course_schedule_id_ = #{courseScheduleId}
	</select>

	<resultMap id="VipGroupGiveCourseSortDto" type="com.ym.mec.biz.dal.dto.VipGroupGiveCourseSortDto" extends="CourseScheduleStudentPayment">
		<result property="courseStartTime" column="course_start_time_"/>
		<result property="courseEndTime" column="course_end_time_"/>
		<result property="teachMode" column="teach_mode_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
	</resultMap>
	<select id="findVipGroupSortCourseByGroup" resultMap="VipGroupGiveCourseSortDto">
		SELECT
			 cssp.*,
			 cs.teach_mode_,
			 CONCAT(cs.class_date_,' ',cs.start_class_time_) course_start_time_,
			 CONCAT(cs.class_date_,' ',cs.end_class_time_) course_end_time_
		FROM course_schedule_student_payment cssp
			LEFT JOIN course_schedule cs ON cssp.course_schedule_id_=cs.id_
		WHERE cssp.music_group_id_=#{groupId} AND cssp.group_type_='VIP'
-- 		AND CONCAT(cs.class_date_,' ',cs.start_class_time_) &gt; NOW()
		AND cssp.settlement_time_ IS NULL
	</select>
	<select id="countStudentNum" resultType="java.lang.Integer">
		SELECT COUNT(id_) FROM course_schedule_student_payment WHERE course_schedule_id_ = #{courseScheduleId}
	</select>
	<select id="countCourseOnlyStudentNum" resultType="int">
		SELECT COUNT(user_id_) FROM course_schedule_student_payment WHERE course_schedule_id_ = #{courseScheduleId}
	</select>
    <select id="findStudents" resultMap="com.ym.mec.biz.dal.dao.MusicGroupDao.BasicUserDto">
		SELECT su.id_ user_id_,su.username_,su.avatar_ head_url_,su.gender_
		FROM course_schedule_student_payment cssp
		LEFT JOIN sys_user su ON su.id_ = cssp.user_id_
		WHERE cssp.course_schedule_id_ = #{courseScheduleId}
		GROUP BY su.id_
	</select>
	<select id="getStudentIdMap" resultType="java.util.Map">
		SELECT cssp.course_schedule_id_ 'key',GROUP_CONCAT(DISTINCT cssp.user_id_) 'value'
		FROM course_schedule_student_payment cssp
		WHERE cssp.course_schedule_id_ IN
		<foreach collection="courseScheduleIds" item="scheduleId" open="(" close=")" separator=",">
			#{scheduleId}
		</foreach>
		GROUP BY cssp.course_schedule_id_
	</select>

	<resultMap id="Practice4ExercisesSituationDto" type="com.ym.mec.biz.dal.dto.Practice4ExercisesSituationDto">
		<result property="userId" column="user_id_"/>
		<result property="courseStartTime" column="start_class_time_"/>
		<result property="actualTeacherId" column="actual_teacher_id_"/>
	</resultMap>

	<select id="findNoPracticeStudentTeacherId" resultType="java.lang.Integer">
		SELECT cs.actual_teacher_id_
		FROM course_schedule_student_payment cssp
		LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_
		WHERE cssp.group_type_='PRACTICE' AND cssp.user_id_=#{studentId} AND cs.actual_teacher_id_ IS NOT NULL
		ORDER BY CONCAT(cs.class_date_, ' ', cs.start_class_time_) DESC LIMIT 1;
	</select>

	<delete id="deleteStudentCourseSchedule">
		DELETE FROM course_schedule_student_payment WHERE user_id_ = #{userId} AND course_schedule_id_ IN
		<foreach collection="courseScheduleIdList" item="courseScheduleId" index="index" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
	</delete>
    <delete id="deleteByCourseSchedule">
		DELETE FROM course_schedule_student_payment WHERE course_schedule_id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" index="index" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
	</delete>
    <delete id="batchDeleteWithID">
		DELETE FROM course_schedule_student_payment WHERE id_ IN
		<foreach collection="ids" item="id" open="(" close=")" separator=",">
			#{id}
		</foreach>
    </delete>
	<delete id="deleteStudentCourseScheduleByUserId">
		DELETE FROM course_schedule_student_payment WHERE course_schedule_id_ = #{courseScheduleId} AND user_id_  IN
		<foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
			#{userId}
		</foreach>
	</delete>

	<delete id="deleteByMusicGroupId" parameterType="map">
		DELETE cssp FROM course_schedule_student_payment cssp left join course_schedule cs on cssp.course_schedule_id_ = cs.id_ WHERE cssp.music_group_id_=#{musicGroupId} AND cssp.group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler} and cs.status_ = 'NOT_START'
	</delete>
	<delete id="deleteByGroup">
		DELETE FROM course_schedule_student_payment WHERE music_group_id_=#{groupId} AND group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
	</delete>
    <delete id="deletePreCourse">
		DELETE FROM course_schedule_student_payment WHERE course_schedule_id_ IN (
		SELECT id_ FROM course_schedule WHERE music_group_id_ = #{musicGroupId} AND group_type_ = 'MUSIC' AND pre_course_flag_ = 1)
	</delete>

    <select id="queryStudentNotStartCourseTimesOfOnline" resultMap="studentCourseTimesDto">
		SELECT cssp.`user_id_`,count(*) totalTimes,
		sum(CASE pg.type_ WHEN 'FREE' THEN 0 WHEN 'CHARGE' THEN 1 END) practiceTmes,
		sum(case when (pg.type_='FREE' AND cssp.`group_type_` = 'PRACTICE' ) then 1 ELSE 0 END) freePracticeTimes
		FROM course_schedule_student_payment cssp LEFT JOIN course_schedule cs on cssp.course_schedule_id_ = cs.id_
		LEFT JOIN practice_group pg ON pg.id_ = cs.music_group_id_ AND cs.group_type_ = 'PRACTICE'
		WHERE cs.`teach_mode_` = 'ONLINE' AND cs.status_ = 'NOT_START'
		GROUP BY cssp.`user_id_`
	</select>

	<resultMap id="StudentTeacherCourseDto" type="com.ym.mec.biz.dal.dto.StudentTeacherCourseDto">
		<result property="studentId" column="student_id_"/>
		<result property="teacherId" column="teacher_id_"/>
		<result property="groupType" column="group_type_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result property="groupId" column="group_id_"/>
		<result property="courseScheduleType" column="course_schedule_type_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result property="classesStartTime" column="classes_start_time_"/>
	</resultMap>

	<select id="findAllStudentCourseInfo" resultMap="StudentTeacherCourseDto">
		SELECT
			cssp.user_id_ student_id_,
			cssp.group_type_ group_type_,
			cssp.music_group_id_ group_id_,
			cs.type_ course_schedule_type_,
			CONCAT(cs.class_date_, ' ', cs.start_class_time_) classes_start_time_,
			cgtm.user_id_ teacher_id_
		FROM
			student s
			LEFT JOIN course_schedule_student_payment cssp ON s.user_id_=cssp.user_id_
			LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
			LEFT JOIN class_group_teacher_mapper cgtm ON cssp.class_group_id_=cgtm.class_group_id_
		WHERE cs.type_ IN ('PRACTICE', 'VIP', 'SINGLE', 'MIX') AND cs.pre_course_flag_ = 0 AND cgtm.teacher_role_='BISHOP' and s.tenant_id_ = #{tenantId};
	</select>
    <select id="findCoursePayment" resultType="map">
		SELECT
			course_schedule_id_ AS 'key',
			SUM( CASE WHEN actual_price_ IS NULL THEN expect_price_ ELSE actual_price_ END ) AS 'value'
		FROM
			course_schedule_student_payment
		WHERE course_schedule_id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
		GROUP BY
			course_schedule_id_
    </select>
    <select id="countStudentNotStartCourseNumWithCourseType" resultType="int">
		SELECT COUNT(DISTINCT cssp.course_schedule_id_) FROM course_schedule_student_payment cssp
		LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_
		WHERE cssp.user_id_=#{studentId}
		<if test="courseScheduleType != null">
		AND cs.type_= #{courseScheduleType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
		</if>
		AND CONCAT(cs.class_date_, ' ', cs.start_class_time_) &gt; NOW() AND cs.pre_course_flag_ = 0 AND cs.del_flag_ = 0 AND cs.is_lock_ = 0
	</select>
	<select id="countStudentsNotStartCourseNumWithCourseType" resultType="map">
		SELECT cssp.user_id_ 'key',COUNT(DISTINCT cssp.course_schedule_id_) 'value'
		FROM course_schedule_student_payment cssp
		LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_
		WHERE cssp.user_id_ IN
		<foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
			#{studentId}
		</foreach>
		<if test="courseScheduleType != null">
			AND cs.type_= #{courseScheduleType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
		</if>
		AND CONCAT(cs.class_date_, ' ', cs.start_class_time_) &gt; NOW() AND cs.pre_course_flag_ = 0 AND cs.del_flag_ = 0 AND cs.is_lock_ = 0
		GROUP BY cssp.user_id_
	</select>
	<select id="queryMidiByUserIdsAndCourseId" resultType="java.util.Map">
		SELECT cssp.user_id_ 'key',CASE WHEN cssp.open_play_midi_ IS NULL THEN '' ELSE cssp.open_play_midi_ END 'value' FROM course_schedule_student_payment cssp
		WHERE cssp.user_id_ IN
		<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
			#{userId}
		</foreach>
		AND cssp.course_schedule_id_ = #{courseScheduleId}
	</select>
	<select id="queryExamSongByUserIdsAndCourseId" resultType="java.util.Map">
		SELECT user_id_ 'key',CASE WHEN exam_song_download_json_ IS NULL THEN '' ELSE exam_song_download_json_ END 'value' FROM course_schedule_student_payment
		WHERE user_id_ IN
		<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
			#{userId}
		</foreach>
		AND course_schedule_id_ = #{courseScheduleId}
	</select>
	<select id="getMidiByCourseIdAndUserId" resultType="java.lang.String">
		SELECT cssp.open_play_midi_ FROM course_schedule_student_payment cssp
		WHERE cssp.user_id_ = #{userId} AND cssp.course_schedule_id_ = #{courseScheduleId}
	</select>
	<select id="getExamJsonByCourseIdAndUserId" resultType="java.lang.String">
		SELECT cssp.exam_song_download_json_ FROM course_schedule_student_payment cssp
		WHERE cssp.user_id_ = #{userId} AND cssp.course_schedule_id_ = #{courseScheduleId}
	</select>

    <select id="findGroupCoursesUnitPrice" resultType="java.util.Map">
		SELECT
			music_group_id_ AS 'key',
			CONVERT ( SUM( expect_price_ ) / COUNT(DISTINCT course_schedule_id_ ), DECIMAL ( 10, 2 ) ) AS 'value'
		FROM
			course_schedule_student_payment
		WHERE
			group_type_ = #{groupType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			AND music_group_id_ IN
			<foreach collection="groupIds" item="groupId" separator="," open="(" close=")">
				#{groupId}
			</foreach>
		GROUP BY
			music_group_id_;
	</select>
    <resultMap id="RongyunBasicUserDto" type="com.ym.mec.biz.dal.dto.RongyunBasicUserDto">
        <result property="userName" column="username_"/>
        <result property="userId" column="user_id_"/>
        <result property="headUrl" column="avatar_"/>
    </resultMap>
    <select id="queryNoJoinStu" resultMap="RongyunBasicUserDto">
		SELECT CASE WHEN su.username_ IS NULL THEN su.real_name_ ELSE su.username_ END username_,
		su.avatar_,cssp.user_id_
		FROM course_schedule_student_payment cssp
		LEFT JOIN sys_user su ON su.id_ = cssp.user_id_
		WHERE cssp.course_schedule_id_ = #{courseScheduleId}
		AND NOT EXISTS (SELECT * FROM rongyun_room_member rrm WHERE rrm.rid = #{roomId} AND rrm.uid = cssp.user_id_)
	</select>

	<select id="countNotStartCourseStudentNumWithGroup" resultType="int">
		SELECT
			COUNT( cssp.user_id_ )
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
		WHERE
			cssp.group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			AND cssp.music_group_id_ = #{groupId} AND cs.pre_course_flag_ = 0
			AND CONCAT( cs.class_date_, ' ', cs.start_class_time_ ) > NOW( )
	</select>

    <select id="findByBatchNo" resultMap="CourseScheduleStudentPayment">
		SELECT * FROM course_schedule_student_payment WHERE batch_no_=#{batchNo}
	</select>

    <select id="getClassGroupNoStartCourse" resultMap="CourseScheduleStudentPayment">
		SELECT cssp.* FROM course_schedule_student_payment cssp
        LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
        WHERE cssp.class_group_id_ IN
		<foreach collection="classGroupIds" item="classGroupId" open="(" close=")" separator=",">
			#{classGroupId}
		</foreach>
        AND cs.status_ = 'NOT_START' AND cs.pre_course_flag_ = 0 ORDER BY cssp.id_ ASC
	</select>

	<select id="findStudentLastCourseSchedule" resultType="java.util.Map">
		SELECT
			cssp.user_id_ AS 'key',
			MAX(CONCAT( cs.class_date_, ' ', cs.start_class_time_ )) AS 'value'
		FROM
			course_schedule_student_payment cssp
		LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
		WHERE
			cssp.user_id_ IN
			<foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
				#{studentId}
			</foreach>
			AND CONCAT( cs.class_date_, ' ', cs.start_class_time_ ) &lt; NOW( ) AND cs.pre_course_flag_ = 0
		GROUP BY user_id_;
	</select>

	<sql id="queryAllCondition">
		<where>
			teannt_id_ = #{tenantId}
			<if test="courseScheduleId != null">
				and course_schedule_id_ = #{courseScheduleId}
			</if>
		</where>
	</sql>

	<select id="queryAll" resultMap="CourseScheduleStudentPayment">
		SELECT * FROM course_schedule_student_payment
		<include refid="queryAllCondition" />
		ORDER BY id_
		<include refid="global.limit"/>
	</select>
	<select id="countAll" resultType="int">
		SELECT COUNT(*) FROM course_schedule_student_payment
		<include refid="queryAllCondition" />
	</select>

    <select id="countStudentDouble11CourseTime" resultMap="com.ym.mec.biz.dal.dao.StudentPaymentOrderDao.StudentVipDouble11Dto">
		SELECT
			cssp.user_id_ userId,
			SUM(CASE vgc.student_num_ WHEN 1 THEN 1 ELSE 0 END) course_one_to_one_time_,
			SUM(CASE vgc.student_num_ WHEN 2 THEN 1 ELSE 0 END) course_one_to_two_time_
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN vip_group vg ON cssp.music_group_id_ = vg.id_
			LEFT JOIN vip_group_category vgc ON vg.vip_group_category_id_ = vgc.id_
		WHERE
			cssp.group_type_ = 'VIP'
			AND cssp.create_time_ > '2020-11-11 00:00:00'
			AND cssp.user_id_ IN
			<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
				#{userId}
			</foreach>
			AND vg.vip_group_activity_id_ IN
			<foreach collection="specialActivityIds" item="specialActivityId" open="(" close=")" separator=",">
				#{specialActivityId}
			</foreach>
		GROUP BY
			cssp.user_id_
    </select>

	<select id="findNeedUpdateActualPriceStudentCourses" resultMap="CourseScheduleStudentPaymentDto">
		SELECT
			cssp.*,
			CASE WHEN cs.new_course_id_ IS NULL THEN CONCAT( cs.class_date_, ' ', cs.start_class_time_ ) ELSE CONCAT( cs1.class_date_, ' ', cs1.start_class_time_ ) END course_start_time_,
			CASE WHEN cs.new_course_id_ IS NULL THEN cs.status_ ELSE cs1.status_ END course_status_
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
			LEFT JOIN course_schedule cs1 ON cs.new_course_id_=cs1.id_
		WHERE
			cssp.batch_no_ IS NOT NULL
			AND cs.id_ IS NOT NULL AND cs.pre_course_flag_ = 0
			AND (((cs.status_ IN ('OVER', 'UNDERWAY') OR cs1.status_ IN ( 'OVER', 'UNDERWAY' ))
			          AND (cssp.actual_price_ IS NULL OR cssp.actual_price_ &lt; cssp.expect_price_))
			OR ( cs.status_ = 'NOT_START' AND cssp.actual_price_ IS NOT NULL AND cssp.actual_price_ &gt; 0))
			<if test="groupIds!=null and groupIds.size()>0">
				AND cs.group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
				AND cs.music_group_id_ IN
				<foreach collection="groupIds" item="groupId" open="(" close=")" separator=",">
					#{groupId}
				</foreach>
			</if>
			<if test="userIds!=null and userIds.size()>0">
				AND cssp.user_id_ IN
				<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
					#{userId}
				</foreach>
			</if>
	</select>
	<select id="queryCourseByIdsAndStudentId" resultMap="CourseScheduleStudentPayment">
		SELECT cssp.* FROM course_schedule_student_payment cssp
		LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
		WHERE cssp.course_schedule_id_ IN
		<foreach collection="courseIds" open="(" item="item" close=")" separator=",">
			#{item}
		</foreach>
		AND cssp.user_id_ = #{studentId} AND cs.type_ = #{courseType}
	</select>
	<select id="queryStudentNotStartByClassIdsAndStudentIds" resultType="java.lang.Long">
		SELECT cssp.id_ FROM course_schedule_student_payment cssp
		LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
		WHERE cssp.class_group_id_ IN
		<foreach collection="classGroupIds" open="(" close=")" separator="," item="item">
			#{item}
		</foreach>
		<if test="studentIds != null">
			AND cssp.user_id_ IN
			<foreach collection="studentIds" open="(" close=")" separator="," item="item">
				#{item}
			</foreach>
		</if>
		AND CONCAT(cs.class_date_," ",cs.start_class_time_) > NOW() AND cs.pre_course_flag_ = 0
		GROUP BY cssp.id_
	</select>
	<select id="queryByIdsAndStudentId" resultMap="CourseScheduleStudentPayment">
		SELECT cssp.* FROM course_schedule_student_payment cssp
		LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
		WHERE cssp.id_ IN
		<foreach collection="studentPaymentIds" open="(" item="item" close=")" separator=",">
			#{item}
		</foreach>
		AND cssp.user_id_ = #{studentId} AND cs.type_ = #{courseType}
	</select>

	<select id="countFinishCoursePriceWithMusicGroup" resultType="java.math.BigDecimal">
		SELECT SUM(expect_price_)-SUM(actual_price_) FROM course_schedule_student_payment
		WHERE music_group_id_=#{groupId} AND user_id_=#{userId} AND group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
	</select>

    <select id="getStudentHistoryLastCourse" resultMap="com.ym.mec.biz.dal.dao.CourseScheduleDao.CourseSchedule">
		SELECT cs.* FROM course_schedule_student_payment cssp
		LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_
		WHERE cssp.user_id_=#{studentId}
		  <if test="groupId!=null">
			  AND cs.music_group_id_=#{groupId}
		  </if>
		  AND cs.class_date_&lt;#{monday}
		  AND cs.type_=#{courseType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
		  AND (cs.del_flag_ != 1 OR cs.del_flag_ IS NULL) AND cs.pre_course_flag_ = 0
		ORDER BY CONCAT(cs.class_date_, ' ', cs.start_class_time_) DESC LIMIT 1
	</select>

	<select id="findByMainMergedCourse" resultMap="CourseScheduleStudentPayment">
		SELECT cssp.*
			FROM course_schedule_student_payment cssp
			LEFT JOIN course_schedule cs ON cssp.course_schedule_id_=cs.id_
		WHERE cs.new_course_id_=#{mainMergeCourseId}
	</select>
    <select id="querySubCoursePriceMap" resultType="java.math.BigDecimal">
		SELECT SUM(cssp.expect_price_) FROM course_schedule_student_payment cssp
		LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
		WHERE cssp.class_group_id_ = #{classGroupId} AND cssp.user_id_ = #{studentId} AND CONCAT(cs.class_date_,' ',cs.start_class_time_) > NOW()
		AND cs.pre_course_flag_ = 0
	  <if test="courseType != null">
		  AND cs.type_ = #{courseType}
	  </if>
	</select>
    <select id="getCourseScheduleStudentNum" resultMap="com.ym.mec.biz.dal.dao.CourseScheduleDao.CourseSchedule">
		SELECT course_schedule_id_ id_,COUNT(*) student_num_ FROM course_schedule_student_payment WHERE course_schedule_id_ IN
		<foreach collection="courseIds" item="courseId" open="(" close=")" separator=",">
			#{courseId}
		</foreach>
		GROUP BY course_schedule_id_
	</select>

	<select id="getWithClassGroupIds" resultMap="CourseScheduleStudentPayment">
		SELECT * FROM course_schedule_student_payment WHERE class_group_id_ IN
		<foreach collection="classGroupIds" item="classGroupId" open="(" close=")" separator=",">
			#{classGroupId}
		</foreach>
		<if test="studentId!=null">
			AND user_id_ = #{studentId}
		</if>
	</select>

	<select id="getWithGroup" resultMap="CourseScheduleStudentPayment">
		SELECT * FROM course_schedule_student_payment
		WHERE music_group_id_ = #{groupId} AND group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
		<if test="studentIds!=null and studentIds.size()>0">
			AND user_id_ IN
			<foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
				#{studentId}
			</foreach>
		</if>
	</select>

	<select id="getStudentEduTeacher" resultType="string">
		SELECT edut.real_name_ FROM course_schedule_student_payment cssp
		LEFT JOIN vip_group vg ON vg.id_ = cssp.music_group_id_ AND cssp.group_type_='VIP'
		LEFT JOIN sys_user edut ON edut.id_ = vg.educational_teacher_id_
		WHERE cssp.user_id_ = #{studentId}
		AND vg.group_status_ IN (0,1,2)
		ORDER BY vg.create_time_ DESC LIMIT 1
	</select>

    <select id="getHasVipCourseStudentIds" resultType="int">
		SELECT cssp.user_id_ FROM course_schedule_student_payment cssp
		LEFT JOIN course_schedule cs ON cssp.course_schedule_id_=cs.id_
		WHERE cssp.group_type_='VIP' AND cs.del_flag_=0
		<if test="studentIds!=null and studentIds.size()>0">
			AND cssp.user_id_ IN
			<foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
				#{studentId}
			</foreach>
		</if>
	</select>

	<select id="getHasVPCourseStudentNum" resultType="int">
		SELECT
			COUNT( DISTINCT cssp.user_id_ )
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN course_schedule cs ON cssp.course_schedule_id_=cs.id_
			LEFT JOIN sys_user su ON cssp.user_id_ = su.id_
		WHERE
			cssp.group_type_ IN ('VIP', 'PRACTICE')
			AND cs.status_='NOT_START'
			AND su.del_flag_ = 0
			AND su.organ_id_ = #{organId}
	</select>

    <select id="getOrganHasVPCourseStudentNum" resultType="map">
		SELECT
			su.organ_id_ 'key',
			COUNT( DISTINCT cssp.user_id_ ) 'value'
		FROM
			course_schedule_student_payment cssp
				LEFT JOIN course_schedule cs ON cssp.course_schedule_id_=cs.id_
				LEFT JOIN sys_user su ON cssp.user_id_ = su.id_
		WHERE
			cssp.group_type_ IN ('VIP', 'PRACTICE')
			  AND cs.status_='NOT_START'
			  AND su.del_flag_ = 0
			<if test="studentIds!=null and studentIds.size()>0">
				AND su.organ_id_ IN
				<foreach collection="organIds" item="organId" open="(" close=")" separator=",">
					#{organId}
				</foreach>
			</if>
		GROUP BY
			su.organ_id_
	</select>
    <select id="getCourseStudents" resultType="com.ym.mec.biz.dal.dto.BasicUserDto">
		SELECT cssp.user_id_ userId,st.member_rank_setting_id_ memberRankSettingId,s.name_ subjectName,su.avatar_ headUrl,su.username_ name,st.subject_id_list_ subjectIdList
		FROM course_schedule_student_payment cssp
		LEFT JOIN sys_user su ON su.id_ = cssp.user_id_
		LEFT JOIN student st ON st.user_id_ = cssp.user_id_
		LEFT JOIN `subject` s ON s.id_ = st.subject_id_list_
		WHERE cssp.course_schedule_id_ = #{courseScheduleId}
	</select>
	<select id="hasCourse" resultType="java.lang.Boolean">
		SELECT CASE WHEN s.user_id_ = NULL THEN 0 ELSE 1 END hasCourse FROM student s
		LEFT JOIN sys_user su ON s.user_id_ = su.id_
		LEFT JOIN course_schedule_student_payment cssp FORCE INDEX(group_type_) ON cssp.user_id_ = s.user_id_
		WHERE cssp.group_type_ IN ('VIP','PRACTICE') and cssp.tenant_id_ = #{tenantId}
		<if test="organId != null and organId != ''">
			AND FIND_IN_SET(su.organ_id_,#{organId})
		</if>
		LIMIT 1
	</select>
	<resultMap id="StudentVipPracticeExportDto" type="com.ym.mec.biz.dal.dto.StudentVipPracticeExportDto">
		<result property="userId" column="user_id_"/>
		<result property="username" column="username_"/>
		<result property="vipOverNum" column="vip_over_num_"/>
		<result property="vipNotStartNum" column="vip_not_start_num_"/>
		<result property="vipTotalNum" column="vip_total_num_"/>
		<result property="practiceOverNum" column="practice_over_num_"/>
		<result property="practiceNotStartNum" column="practice_not_start_num_"/>
		<result property="teacherId" column="teacher_id_"/>
		<result property="teacherName" column="real_name_"/>
		<result property="organName" column="organ_name_"/>
		<result property="courseBalance" column="course_balance_"/>
		<result property="subjectName" column="subject_name_"/>
		<result property="beforeClassNum" column="before_class_num_"/>
		<result property="grade" column="current_grade_num_"/>
	</resultMap>
	<select id="exportStudentVipPractice" resultMap="StudentVipPracticeExportDto">
		SELECT s.user_id_,su.username_,o.name_ organ_name_,uca.course_balance_,sb.name_ subject_name_,
		MAX(CASE WHEN cs.`status_` != 'NOT_START' THEN cs.`class_date_` END) before_class_num_,
		COUNT(CASE WHEN cssp.group_type_ = 'VIP' THEN 1 ELSE NULL END) vip_total_num_,
		COUNT(CASE WHEN cssp.group_type_ = 'VIP' AND cs.status_ != 'NOT_START' THEN 1 ELSE NULL END) vip_over_num_,
		COUNT(CASE WHEN cssp.group_type_ = 'VIP' AND cs.status_ = 'NOT_START' THEN 1 ELSE NULL END) vip_not_start_num_,
		COUNT(CASE WHEN cssp.group_type_ = 'PRACTICE' AND cs.status_ != 'NOT_START' THEN 1 ELSE NULL END) practice_over_num_,
		COUNT(CASE WHEN cssp.group_type_ = 'PRACTICE' AND cs.status_ = 'NOT_START' THEN 1 ELSE NULL END) practice_not_start_num_
		,s.teacher_id_,ste.real_name_,s.`current_grade_num_`
		FROM student s
		LEFT JOIN course_schedule_student_payment cssp FORCE INDEX(group_type_) ON cssp.user_id_ = s.user_id_
		LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
		LEFT JOIN sys_user su ON s.user_id_ = su.id_
		LEFT JOIN sys_user ste ON s.teacher_id_ = ste.id_
		LEFT JOIN organization o ON o.id_ = su.organ_id_
		LEFT JOIN `subject` sb ON sb.id_ = s.subject_id_list_
		LEFT JOIN sys_user_cash_account uca ON uca.user_id_ = s.user_id_
		WHERE cssp.group_type_ IN ('VIP','PRACTICE') and cssp.tenant_id_ = #{tenantId}
		<if test="organId != null">
			AND FIND_IN_SET(su.organ_id_,#{organId})
		</if>
		GROUP BY s.user_id_
	</select>
	<resultMap id="BaseNameDto" type="com.ym.mec.biz.dal.dto.BaseNameDto">
		<result property="teacherName" column="real_name_"/>
		<result property="teacherId" column="teacher_id_"/>
		<result property="userId" column="user_id_"/>
	</resultMap>
	<select id="queryLastOverTeacher" resultMap="BaseNameDto">
		SELECT cssp.user_id_,vg.user_id_ teacher_id_,su.real_name_ FROM course_schedule_student_payment cssp
		LEFT JOIN (SELECT cs.id_,cs.music_group_id_ FROM course_schedule cs
		LEFT JOIN (SELECT music_group_id_,MIN(class_date_) class_date_,MIN(start_class_time_) start_class_time_ FROM course_schedule cs
		WHERE del_flag_ = 0 AND is_lock_ = 0 AND status_ = 'OVER' AND group_type_ = #{groupType}
		GROUP BY music_group_id_) cs1 ON cs.music_group_id_ = cs1.music_group_id_
		WHERE cs.class_date_ = cs1.class_date_ AND cs.start_class_time_ = cs1.start_class_time_) cs ON cs.id_ = cssp.course_schedule_id_
		<if test="groupType == 'VIP'">
			LEFT JOIN vip_group vg ON vg.id_ = cssp.music_group_id_
		</if>
		<if test="groupType == 'PRACTICE'">
			LEFT JOIN practice_group vg ON vg.id_ = cssp.music_group_id_
		</if>
		LEFT JOIN sys_user su ON su.id_ = vg.user_id_
		WHERE cssp.group_type_ = #{groupType} AND cs.id_ = cssp.course_schedule_id_ AND cssp.user_id_ IN
		<foreach collection="studentIds" open="(" close=")" item="userId" separator=",">
			#{userId}
		</foreach>
		GROUP BY cssp.user_id_
	</select>
    <select id="hasStudentMusicTheoryCourseInfo" resultType="java.lang.Boolean">
		SELECT CASE WHEN cssp.user_id_ = NULL THEN 0 ELSE 1 END hasCourse
		FROM `course_schedule_student_payment` cssp
				 LEFT JOIN `course_schedule` cs on cssp.`course_schedule_id_` = cs.`id_`
				 LEFT JOIN `vip_group` vg on vg.`id_` = cssp.`music_group_id_` AND cssp.`group_type_` = 'VIP'
				 LEFT JOIN `sys_user` u on u.`id_` = cssp.`user_id_`
		<include refid="hasStudentMusicTheoryCourseInfo"/>
		LIMIT 1
	</select>
	<resultMap id="ExportStudentCourseInfoDto" type="com.ym.mec.biz.dal.dto.ExportStudentCourseInfoDto">
		<result property="username" column="username_"/>
		<result property="userId" column="user_id_"/>
		<result property="organName" column="organ_name_"/>
		<result property="teacherId" column="teacher_id_"/>
		<result property="teacherName" column="teacher_name_"/>
		<result property="beforeClassNum" column="before_class_num_"/>
		<result property="eduId" column="edu_id_"/>
		<result property="eduName" column="edu_name_"/>
		<result property="grade" column="current_grade_num_"/>
		<result property="startClassNum" column="start_class_num_"/>
		<result property="subClassNum" column="sub_class_num_"/>
		<result property="totalClassNum" column="total_class_num_"/>
	</resultMap>
	<select id="queryStudentCourseInfo" resultMap="ExportStudentCourseInfoDto">
		SELECT o.`name_` organ_name_,u.`username_`,u.`id_` user_id_,tu.`real_name_` teacher_name_,
		       tu.`id_` teacher_id_,edu.`real_name_` edu_name_ ,edu.`id_` edu_id_ ,
			   count(vg.`online_classes_num_` + vg.`offline_classes_num_` )  total_class_num_,
			   sum(case when cs.`status_` != 'NOT_START' then 1 else 0 end) start_class_num_,
			   sum(case when cs.`status_` = 'NOT_START' then 1 else 0 end) sub_class_num_,
			   max(case when cs.`status_` != 'NOT_START' then cs.`class_date_` end) before_class_num_,s.`current_grade_num_`
		FROM `course_schedule_student_payment` cssp
				 LEFT JOIN `course_schedule` cs on cssp.`course_schedule_id_` = cs.`id_`
				 LEFT JOIN `vip_group` vg on vg.`id_` = cssp.`music_group_id_` AND cssp.`group_type_` = 'VIP'
				 LEFT JOIN `sys_user` u on u.`id_` = cssp.`user_id_`
				 LEFT JOIN `student` s on s.`user_id_` = cssp.`user_id_`
				 LEFT JOIN `sys_user` tu on tu.`id_` = s.`teacher_id_`
				 LEFT JOIN `sys_user` edu on edu.`id_` = vg.`educational_teacher_id_`
				 LEFT JOIN `organization` o on o.`id_` = u.`organ_id_`
		<include refid="hasStudentMusicTheoryCourseInfo"/>
		GROUP BY cssp.`user_id_` ;
	</select>
	<sql id="hasStudentMusicTheoryCourseInfo">
		<where>
			cssp.`group_type_` = 'VIP' AND cs.`del_flag_` != 1 AND cs.`is_lock_` != 1
			AND u.`organ_id_` NOT IN (38,43,56,36) AND vg.`group_status_` IN (2,4)
			<if test="groupType == 'VIP'">
				AND vg.`vip_group_category_id_` != 16
			</if>
			<if test="groupType != 'VIP'">
				AND vg.`vip_group_category_id_` = 16
			</if>
			<if test="organId != null and organId != ''">
				AND FIND_IN_SET(u.organ_id_,#{organId})
			</if>
		</where>
	</sql>

	<select id="queryUserMusicGroupCourseNumByClassTime" resultMap="Mapper" parameterType="map">
		select cssp.music_group_id_ key_,count(cssp.id_) value_ from course_schedule_student_payment cssp left join course_schedule cs on cssp.course_schedule_id_ = cs.id_
		where cssp.group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler} and
		cssp.music_group_id_ IN
		<foreach collection="musicGroupIds" separator="," item="musicGroupId" open="(" close=")">
			#{musicGroupId}
		</foreach>
		and cssp.user_id_ = #{userId} and cs.class_date_ between #{startDate} and #{endDate}
		group by cssp.music_group_id_
	</select>
	<select id="findUserFirstVipMap" resultType="java.util.Map">
		SELECT user_id_ 'key',music_group_id_ 'value' FROM course_schedule_student_payment cssp
		RIGHT JOIN (SELECT MIN(id_) id_ FROM course_schedule_student_payment
		WHERE group_type_ = #{groupType} AND user_id_ IN
		<foreach collection="studentIds" separator="," item="userId" open="(" close=")">
			#{userId}
		</foreach>
		GROUP BY user_id_) csp ON csp.id_ = cssp.id_
	</select>
	<select id="queryClassTeacher" resultMap="BaseNameDto">
		SELECT cgsm.user_id_,cgtm.user_id_ teacher_id_,su.real_name_ FROM class_group_student_mapper cgsm
		LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
		LEFT JOIN student_registration sr ON sr.music_group_id_ = cgsm.music_group_id_
		LEFT JOIN class_group_teacher_mapper cgtm ON cgtm.class_group_id_ = cgsm.class_group_id_
		LEFT JOIN sys_user su ON cgtm.user_id_ = su.id_
		WHERE cg.del_flag_ = 0 AND cg.lock_flag_ = 0 AND cg.type_ = 'NORMAL' AND cgsm.status_ = 'NORMAL'
		AND sr.music_group_status_ = 'NORMAL' AND cgsm.user_id_ IN
		<foreach collection="studentIds" open="(" close=")" item="userId" separator=",">
			#{userId}
		</foreach>
		GROUP BY cgsm.user_id_
	</select>
	<select id="queryFirstNotStartTeacher" resultMap="BaseNameDto">
		SELECT cssp.user_id_,vg.user_id_ teacher_id_,su.real_name_ FROM course_schedule_student_payment cssp
		LEFT JOIN (SELECT cs.id_,cs.music_group_id_ FROM course_schedule cs
		LEFT JOIN (SELECT music_group_id_,MAX(class_date_) class_date_,MAX(start_class_time_) start_class_time_ FROM course_schedule cs
		WHERE del_flag_ = 0 AND is_lock_ = 0 AND status_ = 'NOT_START' AND group_type_ = #{groupType}
		GROUP BY music_group_id_) cs1 ON cs.music_group_id_ = cs1.music_group_id_
		WHERE cs.class_date_ = cs1.class_date_ AND cs.start_class_time_ = cs1.start_class_time_) cs ON cs.id_ = cssp.course_schedule_id_
		<if test="groupType == 'VIP'">
			LEFT JOIN vip_group vg ON vg.id_ = cssp.music_group_id_
		</if>
		<if test="groupType == 'PRACTICE'">
			LEFT JOIN practice_group vg ON vg.id_ = cssp.music_group_id_
		</if>
		LEFT JOIN sys_user su ON su.id_ = vg.user_id_
		WHERE cssp.group_type_ = #{groupType} AND cs.id_ = cssp.course_schedule_id_ AND cssp.user_id_ IN
		<foreach collection="studentIds" open="(" close=")" item="userId" separator=",">
			#{userId}
		</foreach>
		GROUP BY cssp.user_id_
	</select>
	<resultMap id="ExportVipStudentCourseManageDto" type="com.ym.mec.biz.dal.dto.ExportVipStudentCourseManageDto">
		<result property="username" column="student_name_"/>
		<result property="userId" column="id_"/>
		<result property="totalClassNum" column="total_times_"/>
		<result property="eduName" column="eduName"/>
		<result property="eduId" column="eduId"/>
		<result property="teacherName" column="teacherName"/>
		<result property="teacherId" column="teacherId"/>
		<result property="organName" column="organ_name_"/>
		<result property="courseType" column="course_type_"/>
		<result property="activeFlag" column="activeFlag"/>
		<result property="completedTimes" column="completed_times_"/>
		<result property="consumerTimes" column="consumerTimes"/>
		<result property="lastCourseTime" column="last_course_time_"/>
		<result property="remaindTimes" column="remaind_times_"/>
		<result property="subjectTeacherName" column="subjectTeacherName"/>
		<result property="subjectTeacherId" column="subjectTeacherId"/>
	</resultMap>
    <select id="queryVipStudentCourseManage" resultMap="ExportVipStudentCourseManageDto">
		SELECT a.organ_name_ ,a.student_name_, a.id_,a.course_type_,a.total_times_,
		a.completed_times_, a.remaind_times_, a.last_course_time_,
		ifnull(b.vip_teacher_name_,a.teacher_name_) teacherName,ifnull(b.actual_teacher_id_,a.teacher_id_) teacherId,
		ifnull(b.edu_teacher_name_,a.edu_teacher_name_) eduName,ifnull(b.educational_teacher_id_,a.edu_teacher_id_) eduId,
		c.real_name_ subjectTeacherName,c.actual_teacher_id_ subjectTeacherId,ifnull(d.month_count_,0) consumerTimes,
		       CASE WHEN d.month_count_ > 2 THEN '是' ELSE '否' END activeFlag FROM
		(
		SELECT o.`name_` organ_name_,u.`username_` student_name_,vgc.`name_` course_type_,u.`id_` ,
		count(vg.`online_classes_num_` + vg.`offline_classes_num_` )  total_times_,
		sum(case when cs.`status_` != 'NOT_START' then 1 else 0 end) completed_times_,
		sum(case when cs.`status_` = 'NOT_START' then 1 else 0 end) remaind_times_,
		max(case when cs.`status_` != 'NOT_START' then cs.`class_date_` end) last_course_time_ ,
		vg.`vip_group_category_id_` ,tu.id_ teacher_id_ ,tu.`real_name_` teacher_name_,edu.`id_` edu_teacher_id_,edu.`real_name_` edu_teacher_name_
		FROM `course_schedule_student_payment` cssp
		LEFT JOIN `course_schedule` cs on cssp.`course_schedule_id_` = cs.`id_`
		LEFT JOIN `vip_group` vg on vg.`id_` = cssp.`music_group_id_` AND cssp.`group_type_` = 'VIP'
		LEFT JOIN `vip_group_category` vgc on vgc.`id_` = vg.`vip_group_category_id_`
		LEFT JOIN `sys_user` u on u.`id_` = cssp.`user_id_`
		LEFT JOIN `sys_user` tu on tu.`id_` = vg.`user_id_`
		LEFT JOIN `sys_user` edu on edu.`id_` = vg.`educational_teacher_id_`
		LEFT JOIN `organization` o on o.`id_` = u.`organ_id_`
		WHERE cssp.`group_type_` = 'VIP' AND cs.`del_flag_` != 1 AND cs.`is_lock_` != 1
		AND u.`organ_id_` not in (38,43,56,36) AND vg.`group_status_` in (2, 4) AND u.tenant_id_ = #{tenantId}
		<if test="organId != null and organId != ''">
			AND FIND_IN_SET(u.`organ_id_`,#{organId})
		</if>
		GROUP BY cssp.`user_id_` ,vg.`vip_group_category_id_`
		) a LEFT JOIN
		(
		SELECT cssp.`user_id_` ,cs.`actual_teacher_id_`,tu.`real_name_` vip_teacher_name_,vg.`educational_teacher_id_` ,edu.`real_name_` edu_teacher_name_,vg.`vip_group_category_id_`
		FROM `course_schedule_student_payment` cssp LEFT JOIN `course_schedule` cs on cssp.`course_schedule_id_` = cs.`id_`
		LEFT JOIN `sys_user` tu on tu.`id_` = cs.`actual_teacher_id_`
		LEFT JOIN `vip_group` vg on vg.`id_` = cssp.`music_group_id_`
		LEFT JOIN `sys_user` edu on edu.`id_` = vg.`educational_teacher_id_`
		WHERE cs.`id_`   in
		(SELECT max(cs.`id_`)  FROM `course_schedule_student_payment` cssp LEFT JOIN `course_schedule` cs on cssp.`course_schedule_id_` = cs.`id_`
		LEFT JOIN `vip_group` vg on vg.`id_` = cssp.`music_group_id_`
		WHERE cs.`status_` = 'OVER' AND cssp.`group_type_` = 'VIP' GROUP BY cssp.`user_id_`,vg.`vip_group_category_id_` )
		) b on a.id_ = b.user_id_ and a.vip_group_category_id_ = b.vip_group_category_id_
		LEFT JOIN
		(
		SELECT cssp.`user_id_` ,cs.`actual_teacher_id_`,tu.`real_name_` FROM `course_schedule_student_payment` cssp LEFT JOIN `course_schedule` cs on cssp.`course_schedule_id_` = cs.`id_`
		LEFT JOIN `sys_user` tu on tu.`id_` = cs.`actual_teacher_id_`
		WHERE cs.`status_` = 'OVER' and cs.`type_` = 'SINGLE'
		GROUP BY cssp.`user_id_`
		) c on b.user_id_ = c.user_id_
		LEFT JOIN
		(
		SELECT cssp.`user_id_` ,vg.`vip_group_category_id_` ,ifnull(count(cssp.`id_`),0) month_count_  FROM `course_schedule_student_payment` cssp LEFT JOIN `course_schedule` cs on cssp.`course_schedule_id_` = cs.`id_`
		LEFT JOIN `vip_group` vg on vg.`id_` = cssp.`music_group_id_`
		WHERE cs.`status_` = 'OVER' and cssp.`group_type_` = 'VIP' AND DATE_SUB(CURDATE(), INTERVAL 30 DAY) &lt; cs.`class_date_`
		GROUP BY cssp.`user_id_` ,vg.`vip_group_category_id_`
		) d on b.user_id_ = d.user_id_ and a.vip_group_category_id_ = d.vip_group_category_id_
	</select>
	
	<select id="queryRemainCourseStudent" resultType="com.ym.mec.biz.dal.dto.StudentOrganDto">
		SELECT cssp.user_id_ userId, u.organ_id_ organId FROM `course_schedule_student_payment` cssp LEFT JOIN `course_schedule` cs on cssp.`course_schedule_id_` = cs.`id_`
		LEFT JOIN `sys_user` u on u.`id_` = cssp.`user_id_`
		WHERE cs.`status_` = 'NOT_START'
		AND u.organ_id_ IN
        <foreach collection="organIds" item="organId" open="(" close=")" separator=",">
            #{organId}
        </foreach>
        group by cssp.user_id_ , u.organ_id_
	</select>
</mapper>
