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

    <resultMap type="com.ym.mec.biz.dal.entity.ClassGroupStudentMapper" id="ClassGroupStudentMapper">
        <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="class_group_id_" property="classGroupId"/>
        <result column="user_id_" property="userId"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="username_" property="userName"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="tenant_id_" property="tenantId" />
    </resultMap>

    <resultMap id="ClassGroupStudentInfoDto" type="com.ym.mec.biz.dal.dto.ClassGroupStudentInfoDto" extends="ClassGroupStudentMapper">
        <result property="avatar" column="avatar_"/>
        <result property="phone" column="phone_"/>
    </resultMap>

    <resultMap id="ClassGroupStudentMapperDto" type="com.ym.mec.biz.dal.dto.ClassGroupStudentMapperDto" extends="ClassGroupStudentMapper">
        <result property="teacherId" column="teacher_id_"/>
    </resultMap>

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

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

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ClassGroupStudentMapper" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO class_group_student_mapper (id_,group_type_,music_group_id_,class_group_id_,user_id_,status_,create_time_,update_time_,tenant_id_)
        VALUES(#{id},#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{musicGroupId},#{classGroupId},#{userId},#{status},now(),now(),#{tenantId})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.ClassGroupStudentMapper">
        UPDATE class_group_student_mapper
        <set>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="classGroupId != null">
                class_group_id_ = #{classGroupId},
            </if>
            <if test="status != null">
                status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="groupType != null">
                group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            update_time_ = now(),
        </set>
        WHERE id_ = #{id}
    </update>

    <update id="batchUpdate" parameterType="com.ym.mec.biz.dal.entity.ClassGroupStudentMapper">
        <foreach collection="classGroupStudentMappers" item="classGroupStudentMapper" separator=";">
            UPDATE class_group_student_mapper
            <set>
                <if test="classGroupStudentMapper.status != null">
                    status_ = #{classGroupStudentMapper.status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="classGroupStudentMapper.groupType != null">
                    group_type_ = #{classGroupStudentMapper.groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                update_time_ = now(),
            </set>
            WHERE id_ = #{classGroupStudentMapper.id}
        </foreach>
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		DELETE FROM class_group_student_mapper WHERE id_ = #{id} 
	</delete>
    <delete id="deleteByClassId">
        UPDATE class_group_student_mapper SET status_='QUIT' WHERE class_group_id_ = #{classGroupId} AND status_='NORMAL'
    </delete>
    <delete id="deleteByGroup">
        DELETE FROM class_group_student_mapper WHERE music_group_id_=#{groupId} AND group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
    </delete>
    <delete id="deleteClassGroupStudent">
        DELETE FROM class_group_student_mapper WHERE class_group_id_=#{classGroupId} AND user_id_=#{userId}
    </delete>

    <update id="deleteByGroupWithLogic">
        UPDATE class_group_student_mapper SET status_='QUIT',update_time_=NOW() WHERE music_group_id_=#{groupId} AND group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
    </update>

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

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

    <!-- 班级学生批量插入 -->
    <insert id="classGroupStudentsInsert" parameterType="java.util.List">
        INSERT INTO class_group_student_mapper (group_type_,music_group_id_,class_group_id_,user_id_,status_,create_time_,tenant_id_)
        VALUE
        <foreach collection="classGroupStudentMapperList" item="item" index="index" separator=",">
            (#{item.groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{item.musicGroupId},#{item.classGroupId},#{item.userId},#{item.status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},now(),#{item.tenantId})
        </foreach>
    </insert>
    
    <select id="query" resultMap="ClassGroupStudentMapper" parameterType="map">
		SELECT * FROM class_group_student_mapper
		where class_group_id_ = #{classGroupId} and user_id_ = #{userId}  AND status_ != 'QUIT'
	</select>

    <select id="findClassGroupStudent" resultMap="ClassGroupStudentMapper">
		SELECT * FROM class_group_student_mapper
		where class_group_id_ = #{classGroupId} and user_id_ = #{userId}
	</select>
    <select id="findStudentNumByClassGroupId" resultType="string">
        SELECT
            su.username_
        FROM
            class_group_student_mapper cgsm
            LEFT JOIN sys_user su ON cgsm.user_id_=su.id_
            WHERE cgsm.class_group_id_=#{classGroupId} AND cgsm.status_!='QUIT'
    </select>

    <select id="findCourseStudentName" resultType="string">
        SELECT
            su.username_
        FROM
            course_schedule_student_payment cssp
            LEFT JOIN sys_user su ON cssp.user_id_ = su.id_
        WHERE
            cssp.course_schedule_id_ = #{courseScheduleId}
    </select>

    <select id="findCourseStudentNameAndPhone" resultType="com.ym.mec.biz.dal.dto.StudentNameAndPhoneDto">
        SELECT
            su.id_ userId,
            su.username_ userName,
            su.phone_ phone,
            su.avatar_ avatar
        FROM
            course_schedule_student_payment cssp
            LEFT JOIN sys_user su ON cssp.user_id_ = su.id_
        WHERE
            cssp.course_schedule_id_ = #{courseScheduleId}
    </select>

    <resultMap id="teacherClassStudentDto" type="com.ym.mec.biz.dal.dto.TeacherClassStudentDto">
        <result property="userId" column="user_id_"/>
        <result property="userName" column="username_"/>
        <result property="avatar" column="avatar_"/>
        <result property="phone" column="phone_"/>
        <result property="continuousAbsenteeismTimes" column="continuous_absenteeism_times_"/>
    </resultMap>

    <select id="findClassStudent" resultMap="teacherClassStudentDto">
        SELECT
            su.id_ user_id_,
            su.username_,
            su.avatar_,
            su.phone_,
            mgsf.continuous_absenteeism_times_
        FROM
            class_group_student_mapper cgsm
            LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
            LEFT JOIN sys_user su ON cgsm.user_id_ = su.id_
            LEFT JOIN music_group_student_fee_ mgsf ON cg.music_group_id_=mgsf.music_group_id_ AND su.id_ = mgsf.user_id_
        WHERE
            cgsm.class_group_id_ = #{classGroupId} AND cgsm.status_ != 'QUIT'
        ORDER BY su.id_
        <include refid="global.limit"/>
    </select>
    <select id="findClassStudentNum" resultType="int">
        SELECT
            COUNT(*)
        FROM
            class_group_student_mapper cgsm
        WHERE
            cgsm.class_group_id_ = #{classGroupId} AND cgsm.status_ != 'QUIT'
    </select>
    <select id="findStudentByCourse" resultMap="com.ym.mec.biz.dal.dao.StudentAttendanceDao.studentAttendanceViewUtilEntity">
        SELECT
            cssp.group_type_,
            cssp.music_group_id_,
            cssp.user_id_ student_id_,
            su.username_,
            su.avatar_,
            cssp.be_merged_,
            st.current_grade_num_,
            st.current_class_,
            IF(sa.status_ IS NULL,'TRUANT',sa.status_) status_
        FROM
            course_schedule_student_payment cssp
            LEFT JOIN sys_user su ON cssp.user_id_ = su.id_
            LEFT JOIN student st ON st.user_id_ = su.id_
            LEFT JOIN student_attendance sa ON cssp.course_schedule_id_ = sa.course_schedule_id_ AND cssp.user_id_ = sa.user_id_
        WHERE
            cssp.course_schedule_id_ =#{courseScheduleId}
            AND su.id_ IS NOT NULL
    </select>

    <select id="findStudentByCourseWithPage" resultMap="com.ym.mec.biz.dal.dao.StudentAttendanceDao.studentAttendanceViewUtilEntity">
        SELECT
            cssp.group_type_,
            cssp.music_group_id_,
            cssp.user_id_ student_id_,
            su.username_,
            su.phone_,
            su.avatar_,
            s.name_ subject_name_,
            IF(sa.status_ IS NULL,'TRUANT',sa.status_) status_
        FROM
            course_schedule_student_payment cssp
            LEFT JOIN sys_user su ON cssp.user_id_ = su.id_
            LEFT JOIN student_attendance sa ON cssp.course_schedule_id_ = sa.course_schedule_id_ AND cssp.user_id_ = sa.user_id_
            LEFT JOIN student_registration sr ON sr.user_id_=cssp.user_id_ AND cssp.music_group_id_=sr.music_group_id_
            LEFT JOIN `subject` s ON sr.subject_id_=s.id_
        WHERE
            cssp.course_schedule_id_ =#{courseScheduleId}
            AND su.id_ IS NOT NULL
        ORDER BY cssp.user_id_
        <include refid="global.limit"/>
    </select>

    <select id="countStudentByCourseWithPage" resultType="int">
        SELECT
            COUNT(cssp.user_id_)
        FROM
            course_schedule_student_payment cssp
            LEFT JOIN sys_user su ON cssp.user_id_ = su.id_
            LEFT JOIN student_attendance sa ON cssp.course_schedule_id_ = sa.course_schedule_id_ AND cssp.user_id_ = sa.user_id_
        WHERE
            cssp.course_schedule_id_ =#{courseScheduleId}
            AND su.id_ IS NOT NULL
    </select>

    <update id="deleteStudentByMusicGroupId" parameterType="map">
        update class_group_student_mapper set status_ = 'QUIT',update_time_ = NOW()
        where class_group_id_ in (select id_ from class_group where music_group_id_ = #{musicGroupId}) and user_id_ = #{userId}
    </update>

    <update id="deleteStudentByClassGroupId">
        update class_group_student_mapper set status_ = 'QUIT',update_time_ = NOW()
        WHERE class_group_id_=#{classGroupId} AND user_id_=#{userId}
    </update>

    <update id="deleteByClassGroupIdAndStudents">
        update class_group_student_mapper set status_ = 'QUIT',update_time_ = NOW()
        WHERE class_group_id_=#{classGroupId} AND FIND_IN_SET(user_id_,#{userIds})
    </update>

    <update id="updateVipGroupStudentStatus">
        update class_group_student_mapper set status_ = #{status},update_time_ = NOW()
        where music_group_id_=#{groupId} and group_type_=#{groupType}
    </update>

    <select id="findClassStudentMapperByUserIdAndClassGroupId" resultMap="ClassGroupStudentMapper">
        SELECT * FROM class_group_student_mapper
        WHERE user_id_=#{userId} AND class_group_id_=#{classGroupId}
        <if test="status != null">
            AND status_ = #{status}
        </if>
        LIMIT 1
    </select>
    <select id="findByClassGroups" resultMap="ClassGroupStudentMapper">
        SELECT * FROM class_group_student_mapper
        WHERE class_group_id_ IN
        <foreach collection="classGroupIds" item="classGroupId" open="(" close=")" separator=",">
            #{classGroupId}
        </foreach>
        AND status_ = 'NORMAL'
    </select>
    <select id="findAllByClassGroup" resultMap="ClassGroupStudentMapper">
        SELECT
          cgsm.*
        FROM class_group_student_mapper cgsm
        WHERE class_group_id_ = #{classGroupId}
    </select>
    <select id="findByClassGroup" resultMap="ClassGroupStudentMapper">
        SELECT
          cgsm.*,
	      su.username_
        FROM class_group_student_mapper cgsm
            LEFT JOIN sys_user su ON cgsm.user_id_ = su.id_
        WHERE class_group_id_ = #{classGroupId}
          AND status_ = 'NORMAL'
    </select>

    <!-- 查询班级某状态的所有学生 -->
    <select id="findClassStudentList" resultMap="com.ym.mec.biz.dal.dao.StudentRegistrationDao.StudentRegistration">
        SELECT sr.*,s.name_ subject_name_,su.username_,su.gender_  FROM class_group_student_mapper cgsm
        LEFT JOIN student_registration sr ON (sr.user_id_ = cgsm.user_id_ AND sr.music_group_id_ = cgsm.music_group_id_)
        LEFT JOIN sys_user su on sr.user_id_ = su.id_
        LEFT JOIN subject s ON sr.actual_subject_id_ = s.id_
        WHERE cgsm.class_group_id_ = #{classGroupId} AND sr.music_group_status_ != 'QUIT'
        AND cgsm.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
    </select>

    <select id="findHighClassGroupHasUser" resultMap="ClassGroupStudentMapper">
        SELECT cgsm.* FROM class_group_student_mapper cgsm
        LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
        WHERE cgsm.music_group_id_ = #{musicGroupId}
        AND cgsm.user_id_=#{userId}
        AND cg.type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        AND FIND_IN_SET(cgsm.status_,'NORMAL,LEAVE')
        AND cg.del_flag_='0'
    </select>
    <select id="findClassGroupIdByUserAndCourseDateAndStatus" resultType="int">
        SELECT
            cgsm.class_group_id_
        FROM
            class_group_student_mapper cgsm
            LEFT JOIN course_schedule cs ON cgsm.class_group_id_=cs.class_group_id_
            WHERE cgsm.user_id_=#{userId}
            AND cs.class_date_ = DATE_FORMAT(#{data},'%Y-%m-%d')
            AND cgsm.status_=#{status}
    </select>
    <select id="countClassGroupsStudentNum" resultType="map">
        SELECT
            class_group_id_ AS 'key',
            COUNT( user_id_ ) AS 'value'
        FROM
            class_group_student_mapper
        WHERE
            class_group_id_ IN
            <foreach collection="classGroupIds" item="classGroupId" open="(" close=")" separator=",">
                #{classGroupId}
            </foreach>
            AND status_!='QUIT'
            <if test="status!=null">
                AND status_=#{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        GROUP BY
            class_group_id_
    </select>

    <select id="countGroupAllStudentNum" resultType="map">
        SELECT
            music_group_id_ AS 'key',
            COUNT( user_id_ ) AS 'value'
        FROM
          class_group_student_mapper
        WHERE
            music_group_id_ IN
            <foreach collection="groupIds" item="groupId" open="(" close=")" separator=",">
                #{groupId}
            </foreach>
            AND group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        GROUP BY
        music_group_id_
    </select>

    <select id="countClassGroupStudentNum" resultType="int">
        SELECT
            COUNT( user_id_ )
        FROM
          class_group_student_mapper
        WHERE
            class_group_id_ = #{classGroupId}
            AND status_!='QUIT'
    </select>

    <select id="countClassGroupNormalStudentNum" resultType="int">
        SELECT
            COUNT( user_id_ )
        FROM
          class_group_student_mapper
        WHERE
            class_group_id_ = #{classGroupId}
            AND status_!='QUIT' AND status_!='QUIT_SCHOOL'
    </select>

    <select id="countGroupNormalStudentNum" resultType="int">
        SELECT
            COUNT( user_id_ )
        FROM
          class_group_student_mapper
        WHERE
            group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
	        AND music_group_id_= #{groupId}
            AND status_='NORMAL'
    </select>

    <select id="countGroupStudentNum" resultType="int">
        SELECT
            COUNT( user_id_ )
        FROM
          class_group_student_mapper
        WHERE
            group_type_ = #{groupType}
            AND music_group_id_ = #{groupId}
            AND status_!='QUIT' AND status_!='QUIT_SCHOOL'
    </select>

    <select id="countClassGroupExitStudentNum" resultType="int">
        SELECT
            COUNT( user_id_ )
        FROM
          class_group_student_mapper
        WHERE
            class_group_id_ = #{classGroupId}
            AND user_id_ IN
            <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
                #{userId}
            </foreach>
            AND status_!='QUIT'
    </select>
    <select id="countUserGroups" resultType="int">
      SELECT COUNT(DISTINCT cgsm.music_group_id_)
      FROM class_group_student_mapper cgsm
      LEFT JOIN class_group cg ON cgsm.class_group_id_=cg.id_
      WHERE cgsm.user_id_=#{userId} AND cgsm.group_type_=#{groupType} AND cgsm.status_!='QUIT' AND cg.del_flag_!=1
    </select>
    
    <select id="queryClassGroupIdList" resultType="int" parameterType="map">
      SELECT distinct class_group_id_ FROM class_group_student_mapper WHERE user_id_=#{userId}
        AND group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        AND music_group_id_ = #{musicGroupId}
        AND status_ != 'QUIT'
    </select>

    <select id="findMusicGroupClassGroupByType" resultMap="ClassGroupStudentMapper">
        SELECT  cgsm.* FROM  class_group_student_mapper cgsm
        LEFT JOIN class_group cg on cgsm.class_group_id_ = cg.id_
        WHERE cgsm.music_group_id_ = #{musicGroupId} AND cg.group_type_ ='MUSIC' AND cg.type_=#{classGroupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler} AND cg.del_flag_='0' AND FIND_IN_SET(cgsm.status_,'NORMAL,LEAVE')
    </select>
    <select id="queryStudentIdMap" resultType="java.util.Map">
        SELECT cgsm.user_id_ 'key',cgsm.user_id_ 'value' FROM class_group_student_mapper cgsm
        WHERE cgsm.music_group_id_ = #{vipGroupId} AND cgsm.group_type_ = 'VIP' AND cgsm.status_ != 'QUIT'
    </select>
    <select id="queryTeacherIdMap" resultType="java.util.Map">
        SELECT cgtm.user_id_ 'key',cgtm.user_id_ 'value' FROM class_group_teacher_mapper cgtm
        WHERE cgtm.music_group_id_ = #{vipGroupId} AND cgtm.group_type_ = 'VIP'
    </select>
    <select id="findByClassGroupAndStudent" resultMap="ClassGroupStudentMapper">
        SELECT
            *
        FROM
            class_group_student_mapper
        WHERE
            class_group_id_ = #{classGroupId}
            AND user_id_ IN
            <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
                #{userId}
            </foreach>
        ORDER BY create_time_ DESC
    </select>
    
    <select id="queryNewStudentListByCourseScheduleId" resultMap="ClassGroupStudentMapper">
      	SELECT cgsm.* FROM class_group_student_mapper cgsm LEFT JOIN course_schedule cs ON cgsm.class_group_id_= cs.class_group_id_
		LEFT JOIN course_schedule_student_payment cssp on cssp.course_schedule_id_ = cs.id_ and cssp.user_id_ = cgsm.user_id_
		WHERE cssp.id_ IS NULL AND cgsm.status_ != 'QUIT' and cs.id_ = #{courseScheduleId}
    </select>
    <select id="findByGroups" resultMap="ClassGroupStudentMapper">
      SELECT
          cgsm.*,
	      su.username_
        FROM class_group_student_mapper cgsm
            LEFT JOIN sys_user su ON cgsm.user_id_ = su.id_
        WHERE
          cgsm.group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
          AND cgsm.music_group_id_ IN
          <foreach collection="groupIds" item="groupId" separator="," open="(" close=")">
              #{groupId}
          </foreach>
    </select>
    <select id="findByGroupOrClassGroup" resultMap="ClassGroupStudentInfoDto">
        SELECT
            cgsm.*,
            su.username_,
            su.avatar_,
            su.phone_
        FROM class_group_student_mapper cgsm
          LEFT JOIN sys_user su ON cgsm.user_id_ = su.id_
        <where>
            <if test="groupType!=null">
                AND cgsm.group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="groupId!=null">
                AND cgsm.music_group_id_ = #{groupId}
            </if>
            <if test="classGroupId!=null">
                AND cgsm.class_group_id_=#{classGroupId}
            </if>
            <if test="tenantId!=null">
                AND cgsm.tenant_id_=#{tenantId}
            </if>
        </where>
    </select>
    <select id="findGroupStudents" resultMap="ClassGroupStudentMapper">
        SELECT * FROM class_group_student_mapper cgsm
        <where>
            <if test="groupType!=null">
                AND cgsm.group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="groupId!=null">
                AND cgsm.music_group_id_ = #{groupId}
            </if>
            <if test="userId!=null">
                AND user_id_=#{userId}
            </if>
            <if test="status!=null">
                AND status_=#{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </select>
    <select id="getStudentEducationalTeachers" resultType="com.ym.mec.biz.dal.dto.StudentEduTeacherDto">
        SELECT
        t.user_id_ studentId,
        t.educational_teacher_id_ educationalTeacherId,
        su.real_name_ educationalTeacherName,
        t.group_type_ groupType
        FROM
        (
        SELECT
        mgsf.user_id_,
        g.educational_teacher_id_,
        'MUSIC' group_type_
        FROM
        music_group_student_fee_ mgsf
        LEFT JOIN music_group g ON mgsf.music_group_id_ = g.id_
        WHERE
        mgsf.user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
        UNION ALL
        SELECT
        cgsm.user_id_,
        g.educational_teacher_id_,
        'VIP' group_type_
        FROM
        class_group_student_mapper cgsm
        LEFT JOIN vip_group g ON cgsm.music_group_id_ = g.id_
        WHERE
        cgsm.group_type_ = 'VIP'
        AND cgsm.user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
        UNION ALL
        SELECT
        cgsm.user_id_,
        g.educational_teacher_id_,
        'PRACTICE' group_type_
        FROM
        class_group_student_mapper cgsm
        LEFT JOIN practice_group g ON cgsm.music_group_id_ = g.id_
        WHERE
        cgsm.group_type_ = 'PRACTICE'
        AND cgsm.user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
        ) t
        LEFT JOIN sys_user su ON t.educational_teacher_id_=su.id_
    </select>
    <select id="existByClassGroupIds" resultType="java.lang.Boolean">
        SELECT COUNT(0) FROM class_group_student_mapper
        WHERE FIND_IN_SET(class_group_id_,#{classGroupIdListStr}) AND user_id_ = #{studentId} AND status_ = 'NORMAL'
    </select>
    <resultMap id="StudentNameAndPhoneDtoMap" type="com.ym.mec.biz.dal.dto.StudentNameAndPhoneDto">
        <result property="userId" column="user_id_"/>
        <result property="avatar" column="avatar"/>
        <result property="gender" column="gender_"/>
        <result property="phone" column="phone_"/>
        <result property="subjectName" column="subject_name_"/>
        <result property="userName" column="username_"/>
    </resultMap>
    <select id="findCourseStudentNameAndPhoneByClassGroupId" resultMap="StudentNameAndPhoneDtoMap">
        SELECT cssp.user_id_,su.username_,su.phone_,su.avatar_,GROUP_CONCAT(DISTINCT s.name_) subject_name_,su.gender_
        FROM course_schedule_student_payment cssp
        LEFT JOIN student_registration sr ON cssp.music_group_id_ = sr.music_group_id_ AND cssp.user_id_ = sr.user_id_
        LEFT JOIN `subject` s ON s.id_ = sr.subject_id_
        LEFT JOIN sys_user su ON su.id_ = cssp.user_id_
        WHERE cssp.class_group_id_ = #{classGroupId} AND sr.music_group_status_ != 'QUIT'
        GROUP BY cssp.user_id_
    </select>

    <select id="hasStudent" resultType="java.lang.Boolean">
        SELECT COUNT(DISTINCT class_group_id_) FROM class_group_student_mapper
        WHERE class_group_id_ = #{masterClassGroupId} AND status_ != 'QUIT' AND user_id_ IN
        <foreach collection="studentIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </select>
    <select id="queryStudentIdsByClassGroupId" resultType="java.lang.Integer">
        SELECT DISTINCT user_id_ FROM class_group_student_mapper
        <include refid="queryStudentIdsByClassGroupIdSql"/>
        AND status_ != 'QUIT'
        UNION ALL
        SELECT DISTINCT user_id_ FROM course_schedule_student_payment csts
        <include refid="queryStudentIdsByClassGroupIdSql"/>
    </select>

    <sql id="queryStudentIdsByClassGroupIdSql">
        <where>
            class_group_id_ = #{classGroupId}
            <if test="groupType != null">
                AND group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="musicGroupId != null">
                AND music_group_id_ = #{musicGroupId}
            </if>
        </where>
    </sql>
    <select id="getStudentClassGroupBishopTeacherMap" resultType="com.ym.mec.biz.dal.dto.BaseMapDto">
        SELECT
        cgsm.user_id_ 'key',
        cgtm.user_id_ 'value'
        FROM
            class_group_student_mapper cgsm
            LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
            LEFT JOIN class_group_teacher_mapper cgtm ON cgsm.class_group_id_ = cgtm.class_group_id_
        WHERE
        ( cg.del_flag_ IS NULL OR cg.del_flag_ = 0 )
        AND cg.type_ IN ( 'NORMAL')
        AND cgtm.teacher_role_ = 'BISHOP'
        AND cgsm.status_='NORMAL'
        <if test="musicGroupId!=null and musicGroupId!=''">
            AND cgsm.music_group_id_ = #{musicGroupId}
        </if>
        <if test="studentIds!=null and studentIds.size()>0">
            AND cgsm.user_id_ IN
            <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
                #{studentId}
            </foreach>
        </if>
    </select>

    <select id="getLessThenThreeClassGroupIds" resultType="java.lang.Long">
        SELECT DISTINCT cg.id_ FROM
        class_group cg
        LEFT JOIN class_group_student_mapper cgsm ON cg.id_ = cgsm.class_group_id_
        LEFT JOIN music_group mg ON cg.music_group_id_=mg.id_
        LEFT JOIN course_schedule cs ON cs.class_group_id_=cg.id_
        WHERE
        cg.type_ IN ('HIGH', 'HIGH_ONLINE')
        AND mg.status_ = 'PROGRESS' AND cg.del_flag_ = 0
        AND cs.del_flag_=0
        AND cs.status_='NOT_START'
        AND ( cs.new_course_id_ IS NULL OR cs.new_course_id_ = cs.id_ )
        <if test="organIds!=null and organIds!=''">
            AND FIND_IN_SET(mg.organ_id_, #{organIds})
        </if>
        GROUP BY cg.id_
        HAVING
        COUNT(DISTINCT CASE WHEN cgsm.status_ = 'NORMAL' THEN cgsm.user_id_ ELSE NULL END) &lt; 3
    </select>

    <select id="getStudentClassGroupWithTeacher" resultMap="com.ym.mec.biz.dal.dao.ClassGroupDao.ClassGroup">
        SELECT
            DISTINCT cg.*
        FROM
            class_group_student_mapper cgsm
            LEFT JOIN class_group_teacher_mapper cgtm ON cgsm.class_group_id_ = cgtm.class_group_id_
            LEFT JOIN class_group cg ON cg.id_ = cgsm.class_group_id_
        WHERE
            cg.del_flag_ = 0
            AND cg.lock_flag_ =0
            AND cgsm.status_='NORMAL' and cg.group_type_ = 'MUSIC'
            AND cgsm.user_id_ IN
            <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
                #{studentId}
            </foreach>
            <if test="teacherId!=null">
                AND cgtm.user_id_ = #{teacherId}
            </if>
            <if test="musicGroupId != null and musicGroupId != ''">
                AND cg.music_group_id_ = #{musicGroupId}
            </if>
            AND cg.id_ IS NOT NULL
        ORDER BY cg.id_ DESC
    </select>
    
    <select id="queryByClassGroupType" resultMap="ClassGroupStudentMapperDto">
    
    	SELECT cgsm.*,cgtm.user_id_ teacher_id_ from class_group_student_mapper cgsm LEFT JOIN class_group cg on cgsm.class_group_id_ = cg.id_
		LEFT JOIN class_group_teacher_mapper cgtm on cgsm.class_group_id_ = cgtm.class_group_id_
		WHERE cgtm.music_group_id_ = #{musicGroupId} and cg.type_ = #{classGroupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler} 
        and cgtm.teacher_role_ = #{teacherRole,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler} and cg.del_flag_ = 0 and cgsm.status_ = 'NORMAL'
		and cgsm.user_id_ IN
                <foreach collection="studentIdList" item="studentId" open="(" close=")" separator=",">
                    #{studentId}
                </foreach>
        group by cgsm.user_id_
        
    </select>
</mapper>
