<?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.ClassGroupDao">
    <resultMap type="com.ym.mec.biz.dal.entity.ClassGroup" id="ClassGroup">
        <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="subject_id_list_" property="subjectIdList"/>
        <result column="subject_name_" property="subjectName"/>
        <result column="name_" property="name"/>
        <result column="group_name_" property="groupName"/>
        <result column="student_num_" property="studentNum"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="del_flag_" property="delFlag"/>
        <result column="expect_student_num_" property="expectStudentNum"/>
        <result column="total_class_times_" property="totalClassTimes"/>
        <result column="img_" property="img"/>
        <result column="memo_" property="memo"/>
        <result column="current_class_times_" property="currentClassTimes"/>
    </resultMap>

    <resultMap type="com.ym.mec.biz.dal.entity.ClassGroup4Teacher" id="ClassGroup4Teacher">
        <result column="id_" property="id"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="subject_id_list_" property="subjectIdList"/>
        <result column="subject_name" property="subjectName"/>
        <result column="name_" property="name"/>
        <result column="student_num_" property="studentNum"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="name_" property="musicGroupName"/>
        <result column="status" property="musicGroupStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ClassGroup" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO class_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id!=null">id_,</if>
            <if test="groupType!=null">group_type_,</if>
            <if test="musicGroupId!=null">music_group_id_,</if>
            <if test="subjectIdList!=null">subject_id_list_,</if>
            <if test="name!=null">name_,</if>
            <if test="studentNum!=null">student_num_,</if>
            <if test="createTime!=null">create_time_,</if>
            <if test="updateTime!=null">update_time_,</if>
            <if test="type!=null">type_,</if>
            <if test="expectStudentNum!=null">expect_student_num_,</if>
            <if test="totalClassTimes!=null">total_class_times_,</if>
            <if test="currentClassTimes!=null">current_class_times_,</if>
            <if test="img!=null">img_,</if>
            <if test="memo!=null">memo_,</if>
            <if test="delFlag!=null">del_flag_,</if>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id!=null">#{id},</if>
            <if test="groupType!=null">#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},</if>
            <if test="musicGroupId!=null">#{musicGroupId},</if>
            <if test="subjectIdList!=null">#{subjectIdList},</if>
            <if test="name!=null">#{name},</if>
            <if test="studentNum!=null">#{studentNum},</if>
            <if test="createTime!=null">#{createTime},</if>
            <if test="updateTime!=null">NOW(),</if>
            <if test="type!=null">#{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},</if>
            <if test="expectStudentNum!=null">#{expectStudentNum},</if>
            <if test="totalClassTimes!=null">#{totalClassTimes},</if>
            <if test="currentClassTimes!=null">#{currentClassTimes},</if>
            <if test="img!=null">#{img},</if>
            <if test="memo!=null">#{memo},</if>
            <if test="delFlag!=null">#{delFlag},</if>
        </trim>
    </insert>

    <delete id="deleteByGroup">
        DELETE FROM class_group WHERE music_group_id_=#{groupId} AND group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
    </delete>

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

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

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

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.ClassGroup">
        UPDATE class_group
        <set>
            <if test="subjectIdList != null">
                subject_id_list_ = #{subjectIdList},
            </if>
            <if test="studentNum != null">
                student_num_ = #{studentNum},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="type != null">
                type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="groupType != null">
                group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="name != null">
                name_ = #{name},
            </if>
            <if test="expectStudentNum != null">
                expect_student_num_ = #{expectStudentNum},
            </if>
            <if test="totalClassTimes != null">
                total_class_times_ = #{totalClassTimes},
            </if>
            <if test="currentClassTimes!=null">
                current_class_times_=#{currentClassTimes},
            </if>
            <if test="img!=null">
                img_=#{img},
            </if>
            <if test="memo!=null">
                memo_=#{memo},
            </if>
            <if test="delFlag != null">
                del_flag_ = #{delFlag},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <!-- 根据主键删除一条记录 -->
    <update id="delete">
        UPDATE class_group SET del_flag_ = 1,update_time_=NOW() WHERE id_ = #{id}
    </update>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="ClassGroup" parameterType="map">
        SELECT * FROM class_group 
        <where>
        	<if test="delFlag != null">
				and del_flag_ = #{delFlag}
			</if>
        	<if test="id != null">
				and id_ = #{id}
			</if>
        	<if test="type != null">
				and type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
        	<if test="groupType != null">
				and group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
        	<if test="musicGroupId != null and musicGroupId != ''">
				and music_group_id_ = #{musicGroupId}
			</if>
        	<if test="name != null and name != ''">
				and name_ LIKE CONCAT('%',#{name},'%')
			</if>
        </where>
        ORDER BY id_ desc
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM class_group
        <where>
        	<if test="delFlag != null">
				and del_flag_ = #{delFlag}
			</if>
        	<if test="id != null">
				and id_ = #{id}
			</if>
        	<if test="type != null">
				and type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
        	<if test="groupType != null">
				and group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
        	<if test="musicGroupId != null and musicGroupId != ''">
				and music_group_id_ = #{musicGroupId}
			</if>
        	<if test="name != null and name != ''">
				and name_ LIKE CONCAT('%',#{name},'%')
			</if>
        </where>
    </select>

    <!-- 查询根据主教助教查询班级信息 -->
    <select id="findClassGroup4Teacher" resultMap="ClassGroup4Teacher">
        SELECT c.*,m.* FROM
        class_group c, music_group m
        WHERE
        (c.main_teacher_id_ = #{teacherId} OR c.assistant_teacher_id_ = #{teacherId})
        AND c.del_flag_ = 0
    </select>
    <select id="findAllMixClassGroup" resultMap="ClassGroup">
        SELECT * FROM
        class_group cg
        WHERE cg.type_='MIX' AND cg.music_group_id_=#{musicGroupId} AND group_type_='MUSIC' AND del_flag_='0'
    </select>
    <select id="findMixClassChildClassGroup" resultMap="ClassGroup">
        SELECT * FROM class_group WHERE id_ IN
        (SELECT sub_class_group_id_ FROM class_group_relation
        WHERE class_group_id_ = #{mixClassGroupId}) AND del_flag_ = 0
    </select>

    <!-- 根据乐团id获取单技班信息 -->
    <select id="findAllNormalClassGroupByMusicGroupId" resultMap="ClassGroup">
        SELECT
        cg.*, (SELECT group_concat(sb.name_) FROM subject sb WHERE FIND_IN_SET(sb.id_,cg.subject_id_list_)) AS
        subject_name_
        FROM
        class_group cg
        WHERE cg.music_group_id_=#{musicGroupId} AND group_type_='MUSIC' AND cg.type_='NORMAL' AND cg.del_flag_='0'
    </select>

    <!-- 查询合奏班包含子班名字信息 -->
    <select id="findMixClassChildClassGroupNames" resultMap="ClassGroup">
        SELECT
        group_concat(cg.name_) as name_
        FROM
        class_group_relation cgr
        LEFT JOIN class_group cg ON cgr.sub_class_group_id_=cg.id_
        WHERE cgr.class_group_id_=#{classGroupId} AND del_flag_='0'
    </select>

    <!-- 根据ids查询班级列表 -->
    <select id="findClassGroupByIds" resultMap="ClassGroup">
        SELECT * FROM class_group WHERE FIND_IN_SET(id_,#{ids}) AND del_flag_ = 0
    </select>

    <resultMap type="com.ym.mec.biz.dal.dto.TeacherMusicClassInfoDto" id="getTeaCourseInfosMap">
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="music_group_name_" property="musicGroupName"/>
        <result column="educational_teacher_id_" property="educationalTeacherId"/>
        <result column="team_teacher_id_" property="teamTeacherId"/>
        <result column="teacher_role_" property="teacherRole"/>
        <result column="class_group_name_" property="classGroupNames"/>
        <result column="class_group_id_" property="classGroupIds"/>
        <result column="hasRestClass" property="hasRestClass"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="job_type_" property="jobType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="job_nature_" property="jobNature" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>
    <select id="queryGroupCourses" resultMap="getTeaCourseInfosMap">
        SELECT mg.id_ music_group_id_,mg.name_ music_group_name_,
        mg.team_teacher_id_,mg.educational_teacher_id_,GROUP_CONCAT(DISTINCT cg.name_) class_group_name_,GROUP_CONCAT(DISTINCT cg.id_) class_group_id_,
        mg.status_,t.job_type_,t.job_nature_,cgtm.teacher_role_,CASE WHEN COUNT(cs.id_) > 0 THEN 1 ELSE 0 END hasRestClass
        FROM class_group_teacher_mapper cgtm
        LEFT JOIN music_group mg ON cgtm.music_group_id_ = mg.id_
        LEFT JOIN class_group cg ON cg.id_ = cgtm.class_group_id_
        LEFT JOIN teacher t ON t.id_ = cgtm.user_id_
        LEFT JOIN course_schedule cs ON (FIND_IN_SET(cs.class_group_id_,cg.id_) AND CONCAT(cs.class_date_,' ',cs.start_class_time_) > NOW())
        WHERE cgtm.user_id_ = #{search} AND cg.del_flag_ = 0 AND cg.group_type_ = 'MUSIC'
        <if test="status != null">
            AND mg.status_ = #{status}
        </if>
        GROUP BY cgtm.id_
        <include refid="global.limit"/>
    </select>


    <resultMap type="com.ym.mec.biz.dal.dto.ClassGroupTeachersDto" id="ClassGroupTeachers">
        <result column="id_" property="id"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="subject_id_list_" property="subjectIdList"/>
        <result column="name_" property="name"/>
        <result column="student_num_" property="studentNum"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="del_flag_" property="delFlag"/>
        <result column="expect_student_num_" property="expectStudentNum"/>
        <result column="total_class_times_" property="totalClassTimes"/>
    </resultMap>
    <!-- 根据乐团id获取乐团下所有班级 -->
    <select id="findClassGroupByMusicGroupId" resultMap="ClassGroupTeachers">
        SELECT * FROM class_group WHERE music_group_id_ = #{musicGroupId}
        <if test="type !=null">
            AND FIND_IN_SET(type_,#{type})
        </if>
        <if test="classGroupId !=null">
            AND id_ = #{classGroupId}
        </if>
        AND del_flag_ = 0
    </select>

    <resultMap id="imGroupModel" type="com.ym.mec.common.entity.ImGroupModel">
        <result column="id_" property="id"/>
        <result column="name_" property="name"/>
        <result column="student_num_" property="count"/>
        <result column="img_" property="img"/>
        <result column="teacher_ids_" property="teacherIds"/>
        <result column="group_name_" property="groupName"/>
    </resultMap>
    <select id="queryUserMusicGroups" resultMap="imGroupModel">
        SELECT cg.id_,CASE WHEN cg.group_name_ IS NULL OR cg.group_name_ = '' THEN cg.name_ ELSE cg.group_name_ END name_,
        cg.student_num_,cg.img_,mg.name_ group_name_
        FROM class_group_student_mapper cgsm
        LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
        LEFT JOIN music_group mg ON mg.id_ = cg.music_group_id_
        <where>
            cg.del_flag_ = 0 AND cgsm.status_ != 'QUIT' AND mg.status_ = 'PROGRESS' AND cgsm.group_type_ = 'MUSIC'
            <if test="userId != null">
                AND cgsm.user_id_ = #{userId}
            </if>
            <if test="search != null and search != ''">
                AND (cg.name_ LIKE CONCAT('%',#{search},'%') OR cg.group_name_ LIKE CONCAT('%',#{search},'%') OR mg.name_ LIKE CONCAT('%',#{search},'%'))
            </if>
        </where>
        GROUP BY cg.id_
    </select>

    <select id="queryUserVipGroups" resultMap="imGroupModel">
        SELECT cg.id_,CASE WHEN cg.group_name_ IS NULL OR cg.group_name_ = '' THEN cg.name_ ELSE cg.group_name_ END name_,
        cg.student_num_,cg.img_
        FROM class_group_student_mapper cgsm
        LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
        LEFT JOIN vip_group vg ON vg.id_ = cg.music_group_id_
        <where>
            cg.del_flag_ = 0 AND cgsm.status_ != 'QUIT' AND vg.group_status_ = 2 AND cgsm.group_type_ = 'VIP'
            <if test="userId != null">
                AND cgsm.user_id_ = #{userId}
            </if>
            <if test="search != null and search != ''">
                AND cg.name_ LIKE CONCAT('%',#{search},'%')
            </if>
        </where>
        GROUP BY cg.id_
    </select>
    <select id="queryUserPracticeGroups" resultMap="imGroupModel">
        SELECT cg.id_,CASE WHEN cg.group_name_ IS NULL OR cg.group_name_ = '' THEN cg.name_ ELSE cg.group_name_ END name_,
        cg.student_num_,cg.img_
        FROM class_group_student_mapper cgsm
        LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
        LEFT JOIN practice_group pg ON pg.id_ = cg.music_group_id_
        <where>
            cg.del_flag_ = 0 AND cgsm.status_ != 'QUIT' AND pg.group_status_ = 'NORMAL' AND cgsm.group_type_ = 'PRACTICE'
            <if test="userId != null">
                AND cgsm.user_id_ = #{userId}
            </if>
            <if test="search != null and search != ''">
                AND cg.name_ LIKE CONCAT('%',#{search},'%')
            </if>
        </where>
        GROUP BY cg.id_
    </select>
    <select id="queryUserCommGroups" resultMap="imGroupModel">
        SELECT cg.id_,CASE WHEN cg.group_name_ IS NULL OR cg.group_name_ = '' THEN cg.name_ ELSE cg.group_name_ END name_,
        cg.student_num_,cg.img_
        FROM class_group_student_mapper cgsm
        LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
        LEFT JOIN courses_group vg ON vg.id_ = cg.music_group_id_
        <where>
            cg.del_flag_ = 0 AND cgsm.status_ != 'QUIT' AND vg.status_ = 'NORMAL' AND cgsm.group_type_ = 'COMM'
            <if test="userId != null">
                AND cgsm.user_id_ = #{userId}
            </if>
            <if test="search != null and search != ''">
                AND cg.name_ LIKE CONCAT('%',#{search},'%')
            </if>
        </where>
        GROUP BY cg.id_
    </select>
    <select id="findVipStuNames" resultType="map">
        SELECT cg.music_group_id_ 'key',GROUP_CONCAT(su.username_) 'value'
        FROM class_group cg
        LEFT JOIN class_group_student_mapper cgsm ON cg.id_ = cgsm.class_group_id_
        LEFT JOIN sys_user su ON cgsm.user_id_ = su.id_
        WHERE FIND_IN_SET(cg.music_group_id_,#{vipIds}) AND cg.group_type_ = #{groupType} AND cg.del_flag_ = 0 AND cgsm.status_ != 'QUIT'
        GROUP BY cg.music_group_id_
    </select>
    <select id="findGroupUsers" resultMap="com.ym.mec.biz.dal.dao.MusicGroupDao.ImUserModel">
        SELECT su.id_,su.avatar_,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'
        <if test="search != null and search != ''">
            AND su.username_ LIKE CONCAT('%',#{search},'%')
        </if>
        GROUP BY su.id_
    </select>
    <select id="findGroupTeacher" resultType="java.lang.Integer">
        SELECT cgtm.user_id_ FROM class_group_teacher_mapper cgtm
        WHERE cgtm.class_group_id_ = #{classGroupId}
        GROUP BY cgtm.user_id_
    </select>
    <select id="countStudentPayLog" resultType="java.lang.Integer">
        SELECT COUNT(spo.id_)
        FROM student_payment_order spo
        LEFT JOIN music_group mg ON spo.music_group_id_=mg.id_
        <where>
            spo.status_ = 'SUCCESS' AND spo.group_type_ != 'SPORADIC'
            <if test="date != null and date != ''">
                AND spo.create_time_ LIKE CONCAT(#{date},'%')
            </if>
            <if test="userId != null">
                AND spo.user_id_ = #{userId}
            </if>
            <if test="type != null">
                AND spo.type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </select>
    <resultMap id="StudentPayInfoDto" type="com.ym.mec.biz.dal.dto.StudentPayInfoDto">
        <result column="actual_amount_" property="actualAmount"/>
        <result column="name_" property="name"/>
        <result column="create_time_" property="createTime"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>
    <select id="queryStudentPayLog" resultMap="StudentPayInfoDto">
        SELECT spo.actual_amount_,spo.status_,spo.type_,IF(mg.name_ IS NULL,vg.name_,mg.name_) name_,spo.create_time_
        FROM student_payment_order spo
        LEFT JOIN music_group mg ON spo.music_group_id_=mg.id_
        LEFT JOIN vip_group vg ON spo.music_group_id_ = vg.id_
        <where>
            spo.status_ = 'SUCCESS' AND spo.group_type_ != 'SPORADIC'
            <if test="date != null and date != ''">
                AND spo.create_time_ LIKE CONCAT(#{date},'%')
            </if>
            <if test="userId != null">
                AND spo.user_id_ = #{userId}
            </if>
            <if test="type != null">
                AND spo.type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
        <include refid="global.limit"/>
    </select>

    <select id="countClassStudent" resultType="int">
        select count(*) from class_group_student_mapper
        where class_group_id_=#{classGroupId} AND cgsm.status_ != 'QUIT'
    </select>

    <select id="findAllClassGroupByMusicGroup" resultMap="ClassGroup">
        SELECT * FROM class_group cg
        WHERE cg.music_group_id_=#{musicGroupId} AND del_flag_ = '0'
        AND cg.group_type_='MUSIC'
        <if test="classGroupType != null">
            AND cg.type_ = #{classGroupType}
        </if>
    </select>

    <select id="queryTeacherIds" resultType="java.lang.Integer">
        SELECT DISTINCT cgtm.user_id_ FROM class_group_teacher_mapper cgtm WHERE cgtm.class_group_id_ = #{id}
    </select>

    <select id="queryTeacherGroups" resultMap="imGroupModel">
        SELECT cg.id_,CASE WHEN cg.group_name_ IS NULL OR cg.group_name_ = '' THEN cg.name_ ELSE cg.group_name_ END name_,
        cg.student_num_,cg.img_,mg.name_ group_name_
        FROM class_group_teacher_mapper cgtm
        LEFT JOIN class_group cg ON cgtm.class_group_id_ = cg.id_
        LEFT JOIN music_group mg ON mg.id_ = cg.music_group_id_ AND cg.group_type_ = 'MUSIC'
        LEFT JOIN vip_group vg ON vg.id_ = cg.music_group_id_ AND cg.group_type_ = 'VIP'
        LEFT JOIN courses_group csg ON csg.id_ = cg.music_group_id_ AND cg.group_type_ = 'COMM'
        WHERE (cgtm.user_id_ = #{userId} OR mg.team_teacher_id_ = #{userId} OR mg.educational_teacher_id_ = #{userId} OR mg.director_user_id_ = #{userId})
        AND cg.del_flag_ = 0 AND ((mg.status_ = 'PROGRESS' OR vg.group_status_ = 2 OR csg.status_ = 'NORMAL')
        OR (vg.group_status_ = 2 AND vg.educational_teacher_id_ =  #{userId}) OR (csg.status_ = 'NORMAL' AND csg.educational_teacher_id_ =  #{userId}))
        <if test="search != null">
            AND (cg.name_ LIKE CONCAT('%',#{search},'%') OR cg.group_name_ LIKE CONCAT('%',#{search},'%')
             OR mg.name_ LIKE CONCAT('%',#{search},'%'))
        </if>
        GROUP BY cg.id_
    </select>

    <select id="findNoClassSubjects" resultMap="ClassGroup">
        SELECT cg.* FROM class_group cg LEFT JOIN class_group_relation cgr ON cgr.sub_class_group_id_=cg.id_
        WHERE cg.music_group_id_=#{musicGroupId} AND cg.type_ = 'NORMAL'
        AND cg.group_type_='MUSIC' AND cg.del_flag_ = '0' and cgr.id_ is null
    </select>

    <select id="countTeacherNum" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT cgtm.user_id_) FROM class_group_teacher_mapper cgtm
        WHERE cgtm.class_group_id_ = #{classGroupId}
    </select>

    <select id="countTeacherNums" resultType="java.util.Map">
        SELECT COUNT(DISTINCT cgtm.user_id_) `value`,cgtm.class_group_id_ `key`
        FROM class_group_teacher_mapper cgtm
        WHERE FIND_IN_SET(cgtm.class_group_id_,#{classGroupIds}) GROUP BY cgtm.class_group_id_
    </select>
    <select id="findByVipGroup" resultMap="ClassGroup">
        SELECT
        cg.*
        FROM
        class_group cg
        WHERE cg.music_group_id_=#{vipGroupId}
        AND cg.group_type_='VIP'
        <if test="delFlag!=null">
            AND cg.del_flag_ = #{delFlag}
        </if>
    </select>

    <select id="lockClassGroup" resultMap="ClassGroup">
        SELECT
        cg.*
        FROM
        class_group cg
        WHERE cg.id_=#{classGroupId} FOR UPDATE
    </select>
    <select id="countTeacherVipNum" resultType="java.util.Map">
        SELECT vg.user_id_ `key`,COUNT(vg.id_) `value` FROM vip_group vg
        WHERE FIND_IN_SET(vg.user_id_,#{teacherIds}) GROUP BY user_id_
    </select>

    <resultMap id="TeacherClassGroupDto" type="com.ym.mec.biz.dal.dto.TeacherClassGroupDto">
        <result property="classGroupId" column="class_group_id_"/>
        <result property="classGroupName" column="class_group_name_"/>
        <result property="studentNum" column="student_num_"/>
        <result property="studentNames" column="student_names_"/>
        <result property="currentClassTimes" column="current_class_times_"/>
        <result property="totalClassTimes" column="total_class_times_"/>
        <result property="musicGroupId" column="music_group_id_"/>
        <result property="musicGroupName" column="music_group_name_"/>
        <result property="schoolId" column="school_id_"/>
        <result property="schoolName" column="school_name_"/>
        <result property="subjectIdList" column="subject_id_list_"/>
        <result property="surplusClassTimes" column="surplus_class_times_"/>
        <result property="paymentExpireDate" column="payment_expire_date_"/>
        <result property="coursesExpireDate" column="courses_expire_date_"/>
        <result property="singleClassTimes" column="single_class_minutes_"/>
        <result column="online_classes_num_" property="onlineClassesNum"/>
        <result column="offline_classes_num_" property="offLineClassesNum"/>
        <result property="type" column="type_"/>
        <result property="courseStartDate" column="courses_start_date_"/>
        <result property="classMode" column="class_mode_"/>
        <result property="buyMonths" column="buy_months_"/>
        <result property="practiceGroupType" column="practice_group_type_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

    <select id="findTeacherMusicClassGroup" resultMap="TeacherClassGroupDto">
        SELECT
            cg.id_ class_group_id_,
            cg.name_ class_group_name_,
            cg.student_num_,
            cg.current_class_times_,
            cg.total_class_times_,
            cg.subject_id_list_,
            cg.type_,
            COUNT(cg.id_) surplus_class_times_,
            mg.id_ music_group_id_,
            mg.name_ music_group_name_,
            s.id_ school_id_,
            CASE WHEN cg.type_ IN ("HIGH_ONLINE","MUSIC_NETWORK") THEN '网络教室' ELSE s.name_ END school_name_
        FROM
            course_schedule_teacher_salary csts
            LEFT JOIN course_schedule cs ON cs.id_ = csts.course_schedule_id_
            LEFT JOIN class_group cg ON cs.class_group_id_ = cg.id_
            LEFT JOIN music_group mg ON cg.music_group_id_ = mg.id_
            LEFT JOIN school s ON mg.school_id_ = s.id_
        WHERE
            csts.user_id_ = #{teacherId}
            AND cg.group_type_='MUSIC'
            AND cg.del_flag_ = 0
            AND cs.del_flag_ = 0
            AND mg.status_ = 'PROGRESS'
        GROUP BY cg.id_
    </select>
    <select id="findTeacherPracticeClassGroup" resultMap="TeacherClassGroupDto">
        SELECT
            cg.id_ class_group_id_,
            cg.name_ class_group_name_,
            cg.type_,
            cg.subject_id_list_,
            pg.courses_start_date_,
            pg.courses_expire_date_,
            pg.single_class_minutes_,
            cg.total_class_times_ online_classes_num_,
						0 offline_classes_num_,
            cg.total_class_times_,
            pg.id_ music_group_id_,
            pg.name_ music_group_name_,
						1 class_mode_,
            pg.buy_months_,
            pg.type_ practice_group_type_
        FROM
            practice_group pg
            LEFT JOIN class_group cg ON pg.id_=cg.music_group_id_ AND cg.group_type_='PRACTICE'
        WHERE
            pg.user_id_ = #{teacherId} AND cg.del_flag_ = 0 AND pg.group_status_!='CANCEL' AND pg.group_status_!='LOCK'
    </select>
    <select id="findTeacherVipClassGroup" resultMap="TeacherClassGroupDto">
        SELECT
            cg.id_ class_group_id_,
            cg.name_ class_group_name_,
            cg.type_,
            cg.subject_id_list_,
            vg.payment_expire_date_,
            vg.courses_expire_date_,
            vg.single_class_minutes_,
            vg.courses_start_date,
            IF(vg.online_classes_num_ IS NULL,0,vg.online_classes_num_) online_classes_num_,
            IF(vg.offline_classes_num_ IS NULL,0,vg.offline_classes_num_) offline_classes_num_,
            cg.total_class_times_,
            COUNT(vg.id_) surplus_class_times_,
            vg.id_ music_group_id_,
            vg.name_ music_group_name_,
            s.id_ school_id_,
            s.name_ school_name_
        FROM
            vip_group vg
            LEFT JOIN class_group cg ON vg.id_=cg.music_group_id_ AND cg.group_type_='VIP'
            LEFT JOIN course_schedule cs ON cg.id_=cs.class_group_id_
            LEFT JOIN school s ON vg.teacher_school_id_ = s.id_
        WHERE
            vg.user_id_ = #{teacherId} AND vg.group_status_!=7 AND (vg.group_status_ = 2 OR vg.group_status_ = 5)
        GROUP BY
        vg.id_,cg.id_
    </select>
    <select id="countGroupCourses" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT cgtm.id_)
        FROM class_group_teacher_mapper cgtm
        LEFT JOIN music_group mg ON cgtm.music_group_id_ = mg.id_
        LEFT JOIN class_group cg ON cg.id_ = cgtm.class_group_id_
        LEFT JOIN teacher t ON t.id_ = cgtm.user_id_
        WHERE cgtm.user_id_ = #{search} AND cg.del_flag_ = 0 AND cg.group_type_ = 'MUSIC'
        <if test="status != null">
            AND mg.status_ = #{status}
        </if>
    </select>
    <select id="findStudentOrganIdsByClassGroup" resultType="int">
        SELECT
        DISTINCT su.organ_id_
        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="queryNamesByClassId" resultType="java.util.Map">
        SELECT cgr.class_group_id_ 'key',GROUP_CONCAT(cg.name_) 'value'
        FROM class_group_relation cgr
        LEFT JOIN class_group cg ON cgr.sub_class_group_id_ = cg.id_
        WHERE FIND_IN_SET(cgr.class_group_id_,#{classGroupIds}) AND cg.del_flag_ = 0
        GROUP BY cgr.class_group_id_
    </select>

    <resultMap id="MusicGroupClassGroupDto" type="com.ym.mec.biz.dal.dto.MusicGroupClassGroupDto">
        <result property="classGroupName" column="class_group_name_"/>
        <result property="classGroupType" column="class_group_type_"/>
        <result property="currentClassTimes" column="current_class_times_"/>
        <result property="totalClassTimes" column="total_class_times_"/>
        <result property="classGroupId" column="class_group_id_"/>
    </resultMap>
    <select id="queryMusicGroupClassGroup" resultMap="MusicGroupClassGroupDto">
        SELECT cg.name_ class_group_name_,cg.type_ class_group_type_,
        cg.current_class_times_,cg.id_ class_group_id_,cg.total_class_times_
        FROM class_group cg
        WHERE cg.music_group_id_ = #{search} AND cg.del_flag_ = 0 AND cg.group_type_ = 'MUSIC'
        <include refid="global.limit"/>
    </select>

    <select id="countMusicGroupClassGroup" resultType="java.lang.Integer">
        SELECT COUNT(cg.id_)
        FROM class_group cg
        WHERE cg.music_group_id_ = #{search} AND cg.del_flag_ = 0 AND cg.group_type_ = 'MUSIC'
    </select>

    <select id="countStudentNum" resultType="java.util.Map">
        SELECT cg.class_group_id_ 'key',GROUP_CONCAT(su.real_name_) 'value' FROM class_group_teacher_mapper cg
        LEFT JOIN sys_user su ON cg.user_id_ = su.id_
        WHERE FIND_IN_SET(cg.class_group_id_,#{classGroupIds}) AND cg.teacher_role_ = #{teacherRole}
        GROUP BY cg.class_group_id_
    </select>

    <select id="countStudyNum" resultType="java.util.Map">
        SELECT cgsm.class_group_id_ 'key',COUNT(cgsm.user_id_) 'value' FROM class_group_student_mapper cgsm
        WHERE FIND_IN_SET(cgsm.class_group_id_,#{classGroupIds}) AND cgsm.status_ != 'QUIT'
        GROUP BY cgsm.class_group_id_
    </select>

    <select id="countQuitNum" resultType="java.util.Map">
        SELECT cgsm.class_group_id_ 'key',COUNT(cgsm.user_id_) 'value' FROM class_group_student_mapper cgsm
        WHERE FIND_IN_SET(cgsm.class_group_id_,#{classGroupIds}) AND cgsm.status_ = 'QUIT'
        GROUP BY cgsm.class_group_id_
    </select>

    <sql id="queryMusicGroupCourseScheduleSql">
        <where>
            cs.group_type_ = 'MUSIC' AND cs.del_flag_ = 0
            <if test="startTime != null">
                AND cs.class_date_ &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                AND cs.class_date_ &lt;= #{endTime}
            </if>
            <if test="musicGroupId != null">
                AND cg.music_group_id_ = #{musicGroupId}
            </if>
        </where>
    </sql>

    <sql id="queryMusicGroupCourseScheduleDetailSql">
        <where>
            (ta.teacher_id_ = cs.actual_teacher_id_ OR ta.id_ IS NULL) AND cs.group_type_ = 'MUSIC' AND cs.del_flag_ = 0
            <if test="classScheduleType != null">
                AND cs.type_ = #{classScheduleType}
            </if>
            <if test="classScheduleStatus != null">
                AND cs.status_ = #{classScheduleStatus}
            </if>
            <if test="startTime != null">
                AND cs.class_date_ &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                AND cs.class_date_ &lt;= #{endTime}
            </if>
            <if test="musicGroupId != null">
                AND cg.music_group_id_ = #{musicGroupId}
            </if>
            <if test="classGroupId != null">
                AND cg.id_ = #{classGroupId}
            </if>
            <if test="isSettlement == 0">
                AND csts.settlement_time_ IS NULL
            </if>
            <if test="isSettlement == 1">
                AND csts.settlement_time_ IS NOT NULL
            </if>
        </where>
    </sql>
    <resultMap id="MusicGroupCourseScheduleDto" type="com.ym.mec.biz.dal.dto.MusicGroupCourseScheduleDto">
        <result property="courseScheduleId" column="course_schedule_id_"/>
        <result property="classGroupName" column="class_group_name_"/>
        <result property="classDate" column="class_date_"/>
        <result property="startClassTime" column="start_class_time_"/>
        <result property="startClassTimeStr" column="start_class_time_"/>
        <result property="endClassTime" column="end_class_time_"/>
        <result property="endClassTimeStr" column="end_class_time_"/>
        <result property="classGroupId" column="class_group_id_"/>
        <result property="mixClassGroupId" column="mix_class_group_id_"/>
        <result property="courseScheduleType" column="course_schedule_type_"/>
        <result property="courseScheduleStatus" column="course_schedule_status_"/>
        <result property="signInTime" column="sign_in_time_"/>
        <result property="signInStatus" column="sign_in_status_"/>
        <result property="signOutTime" column="sign_out_time_"/>
        <result property="signOutStatus" column="sign_out_status_"/>
        <result property="settlementTime" column="settlement_time_"/>
        <result property="remark" column="remark_"/>
        <result property="courseScheduleName" column="course_schedule_name_"/>
    </resultMap>
    <select id="queryMusicGroupCourseSchedule" resultMap="MusicGroupCourseScheduleDto">
        SELECT cs.id_ course_schedule_id_,cs.class_date_,cs.start_class_time_,cs.end_class_time_,cs.class_group_id_,
        cs.type_ course_schedule_type_,cg.name_ course_schedule_name_
        FROM course_schedule cs
        LEFT JOIN class_group cg ON cg.id_ = cs.class_group_id_
        <include refid="queryMusicGroupCourseScheduleSql"/>
        ORDER BY cs.class_date_ ,cs.start_class_time_ ,cg.name_
        <include refid="global.limit"/>
    </select>

    <select id="countMusicGroupCourseSchedule" resultType="java.lang.Integer">
        SELECT COUNT(cs.id_)
        FROM course_schedule cs
        LEFT JOIN class_group cg ON cg.id_ = cs.class_group_id_
        <include refid="queryMusicGroupCourseScheduleSql"/>
    </select>

    <select id="queryMusicGroupCourseScheduleDetail" resultMap="MusicGroupCourseScheduleDto">
        SELECT cs.id_ course_schedule_id_,
        cs.class_date_,
        cs.start_class_time_,
        cs.end_class_time_,
        cs.class_group_id_,
        cs.name_ course_schedule_name_,
        cs.type_ course_schedule_type_,cs.status_ course_schedule_status_,cg.name_ course_schedule_name_,
        IF(ta.sign_in_status_ IS NULL,3,ta.sign_in_status_) sign_in_status_,
        IF(ta.sign_out_status_ IS NULL,3,ta.sign_out_status_) sign_out_status_,
        csts.settlement_time_,
        cg.name_ class_group_name_,
        ta.sign_in_time_,
        ta.sign_out_time_,
        ta.remark_
        FROM course_schedule cs
        LEFT JOIN class_group cg ON cg.id_ = cs.class_group_id_
        LEFT JOIN teacher_attendance ta ON ta.course_schedule_id_ = cs.id_
        LEFT JOIN course_schedule_teacher_salary csts ON csts.course_schedule_id_ = cs.id_ AND csts.teacher_role_ = 'BISHOP'
        <include refid="queryMusicGroupCourseScheduleDetailSql"/>
        ORDER BY cs.class_date_ ,cs.start_class_time_ ,cg.name_
        <include refid="global.limit"/>
    </select>

    <select id="countMusicGroupCourseScheduleDetail" resultType="java.lang.Integer">
        SELECT COUNT(cs.id_)
        FROM course_schedule cs
        LEFT JOIN class_group cg ON cg.id_ = cs.class_group_id_
        LEFT JOIN teacher_attendance ta ON ta.course_schedule_id_ = cs.id_
        LEFT JOIN course_schedule_teacher_salary csts ON csts.course_schedule_id_ = cs.id_ AND csts.teacher_role_ = 'BISHOP'
        <include refid="queryMusicGroupCourseScheduleDetailSql"/>
    </select>

    <select id="findNameById" resultType="java.util.Map">
        SELECT cg.id_ 'key',cg.name_ 'value'
        FROM class_group cg WHERE FIND_IN_SET(cg.id_,#{classGroupIds}) AND cg.del_flag_ = 0
    </select>

    <resultMap id="ConditionDto" type="com.ym.mec.biz.dal.dto.ConditionDto">
        <result property="id" column="id_"/>
        <result property="name" column="name_"/>
    </resultMap>
    <select id="queryStudentClassGroup" resultMap="ConditionDto">
        SELECT cg.id_,cg.name_,cg.student_num_ FROM class_group_student_mapper cgsm
        LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
        where cg.music_group_id_ = #{musicGroupId} AND cgsm.user_id_ = #{teacherId}
        AND cg.del_flag_ = 0 AND cgsm.status_ != 'QUIT' AND cg.group_type_ = 'MUSIC'
    </select>

    <resultMap type="com.ym.mec.biz.dal.dto.CourseListDto" id="CourseListDto">
        <result column="class_group_id_" property="classGroupId"/>
        <result column="music_group_name_" property="musicGroupName"/>
        <result column="real_name_" property="teacherName"/>
        <!--<result column="music_group_id_" property="musicGroupId"/>-->
        <result column="music_id_" property="musicId"/>
        <result column="vip_id_" property="vipId"/>
        <result column="practice_id_" property="practiceId"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="total_class_times_" property="totalClassTimes"/>
        <!--<result column="teach_mode_" property="teachMode"/>-->
        <result column="current_class_times_" property="currentClassTimes"/>
        <!--<result column="address_" property="address"/>-->
        <result column="single_class_minutes_" property="singleClassMinutes"/>
        <result column="online_classes_num_" property="onlineClassesNum"/>
        <result column="offline_classes_num_" property="offLineClassesNum"/>
        <result column="teacher_school_id_" property="teacherSchoolId"/>
        <result column="class_mode_" property="classMode"/>
        <result column="practice_type_" property="practiceType"/>
        <result column="courses_start_date_" property="courseStartDate"/>
        <result column="courses_expire_date_" property="courseExpireDate"/>
        <result column="payment_status_" property="paymentStatus"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>
    <select id="queryStudentVipCourses" resultMap="CourseListDto">
        SELECT cg.id_ class_group_id_,vg.name_ music_group_name_,su.username_,
        cg.current_class_times_,cg.total_class_times_,vg.single_class_minutes_,
        IF(vg.online_classes_num_ IS NULL,0,vg.online_classes_num_) online_classes_num_,
        IF(vg.offline_classes_num_ IS NULL,0,vg.offline_classes_num_) offline_classes_num_,
        vg.teacher_school_id_,vg.id_ vip_id_,cg.type_
        FROM vip_group vg
        LEFT JOIN class_group cg ON cg.music_group_id_ = vg.id_
        LEFT JOIN sys_user su ON vg.user_id_ = su.id_
        WHERE cg.group_type_ = 'VIP' AND FIND_IN_SET(vg.id_,#{musicGroupIds})
    </select>
    <select id="queryStudentMusicGroupCourses" resultMap="CourseListDto">
        SELECT mg.id_ music_id_,mg.name_ music_group_name_,mgsf.payment_status_,
        SUM(cg.current_class_times_) current_class_times_,
        SUM(cg.total_class_times_) total_class_times_,mg.school_id_ teacher_school_id_
        FROM music_group mg
        LEFT JOIN class_group cg ON cg.music_group_id_ = mg.id_
        LEFT JOIN music_group_student_fee_ mgsf ON mgsf.music_group_id_ = mg.id_
        LEFT JOIN class_group_student_mapper cgsm ON cgsm.class_group_id_ = cg.id_
        WHERE FIND_IN_SET(mg.id_,#{musicGroupIds}) AND cg.group_type_ = 'MUSIC' AND mgsf.user_id_ =
        #{userId} AND cgsm.user_id_ = #{userId}
        GROUP BY mg.id_,mgsf.payment_status_
    </select>

    <select id="queryStudentPracticeCourses" resultMap="CourseListDto">
        SELECT cg.id_ class_group_id_,pg.name_ music_group_name_,su.real_name_,
        cg.current_class_times_,cg.total_class_times_,pg.single_class_minutes_,
        pg.id_ practice_id_,cg.type_,1 class_mode_,pg.courses_start_date_,
        pg.courses_expire_date_,
        CASE pg.type_ WHEN 'FREE' THEN 0 WHEN 'CHARGE' THEN 1 END practice_type_
        FROM practice_group pg
        LEFT JOIN class_group cg ON cg.music_group_id_ = pg.id_
        LEFT JOIN sys_user su ON pg.user_id_ = su.id_
        WHERE cg.group_type_ = 'PRACTICE' AND FIND_IN_SET(pg.id_,#{musicGroupIds})
    </select>

    <!-- 增加实际学生人数 -->
    <update id="addStudentNum" parameterType="com.ym.mec.biz.dal.entity.ClassGroup">
        UPDATE class_group SET student_num_ = student_num_+1,update_time_ = NOW()
        WHERE id_ = #{classGroup.id} AND
        student_num_=#{classGroup.studentNum}
    </update>

    <update id="batchIncreaseClassTimes" parameterType="java.util.List">
        UPDATE class_group SET current_class_times_ = current_class_times_+1,update_time_ = now() WHERE id_ in
        <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>
    <update id="batchUpdateClassGroupCourseTimes" parameterType="map">
        <foreach collection="classGroupCourseTimes.entrySet()" index="key" item="value" open="" close="" separator=";">
            UPDATE class_group SET total_class_times_=#{value},update_time_ = NOW()
            WHERE id_=#{key}
        </foreach>
    </update>

    <select id="findClassGroupByMusicGroupIdAndType" resultMap="ClassGroup">
        SELECT * FROM class_group WHERE music_group_id_=#{musicGroupId}
        <if test="type !=null ">
            AND type_=#{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        </if>
        AND del_flag_='0'
    </select>

    <select id="findClassGroups" resultMap="ClassGroup">
        SELECT * FROM class_group WHERE music_group_id_=#{musicGroupId}
        AND del_flag_='0' AND group_type_ NOT IN ('PRACTICE','VIP','HIGH')
    </select>

    <!-- 根据合奏班id获取子班及老师 -->
    <select id="findClassGroupByMixClassGroupId" resultMap="ClassGroupTeachers">
        SELECT * FROM class_group WHERE id_ IN
        (SELECT sub_class_group_id_ FROM class_group_relation
        WHERE class_group_id_ = #{mixClassGroupId}) AND del_flag_ = 0
    </select>
    <select id="findClassGroupNamesByClassGroups" resultType="string">
        SELECT
        name_
        FROM
        class_group
        WHERE
        id_ IN
        <foreach collection="classGroups" open="(" close=")" item="classGroupId" separator=",">
            #{classGroupId}
        </foreach>
        AND del_flag_ = 0
    </select>
    <select id="findByCourseSchedules" resultMap="ClassGroup">
        SELECT
        cg.*
        FROM
        course_schedule cs
        LEFT JOIN class_group cg ON cs.class_group_id_=cg.id_
        WHERE cs.id_ IN
        <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
            #{courseScheduleId}
        </foreach>
        AND cg.del_flag_ = 0
    </select>
    <select id="findByCourseSchedule" resultMap="ClassGroup">
        SELECT
        cg.*
        FROM
        course_schedule cs
        LEFT JOIN class_group cg ON cs.class_group_id_=cg.id_
        WHERE cs.id_ = #{courseScheduleId}
        <if test="delFlag != null">
            AND cg.del_flag_ = #{delFlag}
        </if>
    </select>
    <select id="findClassGroupsByMixClassGroup" resultMap="ClassGroup">
        SELECT
        cg.*
        FROM
        class_group_relation cgr
        LEFT JOIN class_group cg ON cgr.sub_class_group_id_=cg.id_
        WHERE cgr.class_group_id_=#{maxCourseScheduleId} AND cg.del_flag_ = 0
    </select>

    <!-- 根据乐团id和声部id查提高班 -->
    <resultMap type="com.ym.mec.biz.dal.dto.HighClassGroupDto" id="HighClassGroup">
        <result column="id_" property="id"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="subject_id_list_" property="subjectIdList"/>
        <result column="subject_name_" property="subjectName"/>
        <result column="expect_student_num_" property="expectStudentNum"/>
        <result column="name_" property="name"/>
        <result column="student_num_" property="studentNum"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="del_flag_" property="delFlag"/>
        <result column="total_class_times_" property="totalClassTimes"/>
        <result column="img_" property="img"/>
        <result column="current_class_times_" property="currentClassTimes"/>
        <result column="memo_" property="memo"/>
    </resultMap>
    <select id="findClassGroupByMusicGroupIdAndSubjectId" resultMap="HighClassGroup">
        SELECT * FROM class_group WHERE music_group_id_=#{musicGroupId}
        <if test="subjectId != null">
            AND FIND_IN_SET(#{subjectId},subject_id_list_)
        </if>
        AND type_ IN ('HIGH','HIGH_ONLINE') AND del_flag_='0';
    </select>
    <select id="findByMusicGroupAndType" resultMap="ClassGroup">
        SELECT
            *
        FROM
            class_group
        WHERE
            music_group_id_ = #{musicGroupId}
            AND group_type_ = #{groupType}
    </select>
    <select id="lockByMusicGroupAndType" resultMap="ClassGroup">
        SELECT
            *
        FROM
            class_group
        WHERE
            music_group_id_ = #{musicGroupId}
            AND group_type_ = #{groupType} FOR UPDATE
    </select>

    <select id="findByMusicGroupsAndType" resultMap="ClassGroup">
        SELECT
        *
        FROM
        class_group
        WHERE
        music_group_id_ IN
        <foreach collection="musicGroupIds" item="musicGroupId" open="(" close=")" separator=",">
            #{musicGroupId}
        </foreach>
        AND group_type_ = #{groupType}
    </select>

    <!-- 根据主键批量删除班级-->
    <update id="batchSoftDelete">
        UPDATE class_group SET del_flag_ = 1,update_time_=NOW() WHERE id_ IN
        <foreach collection="classGroupIds" item="classGroupId" open="(" close=")" separator=",">
            #{classGroupId}
        </foreach>
    </update>

    <select id="findClassGroupByType" resultMap="ClassGroupTeachers">
        SELECT * FROM class_group WHERE music_group_id_=#{musicGroupId} AND group_type_ = 'MUSIC'
        <if test="type !=null ">
            AND type_=#{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        </if>
        AND del_flag_='0'
    </select>
    <select id="countGroupTotalClassTimes" resultType="map">
        SELECT
            music_group_id_ AS 'key',
            SUM( total_class_times_ ) AS 'value' 
        FROM
            class_group 
        WHERE
            music_group_id_ IN 
            <foreach collection="groupIds" item="groupId" open="(" close=")" separator=",">
                #{groupId}
            </foreach>
            AND group_type_ = #{groupType}
        GROUP BY music_group_id_,group_type_
    </select>
    <select id="countGroupOverClassTimes" resultType="map">
        SELECT
            music_group_id_ AS 'key',
            SUM( current_class_times_ ) AS 'value'
        FROM
          class_group
        WHERE
            music_group_id_ IN
            <foreach collection="groupIds" item="groupId" open="(" close=")" separator=",">
                #{groupId}
            </foreach>
            AND group_type_ = #{groupType}
        GROUP BY music_group_id_,group_type_
    </select>

    <select id="findByClassGroupIds" resultMap="ClassGroup">
      SELECT * FROM class_group where id_ IN
      <foreach collection="classGroupIds" item="classGroupId" open="(" close=")" separator=",">
          #{classGroupId}
      </foreach>
    </select>

    <resultMap id="ImGroupModelMap" type="com.ym.mec.common.entity.ImGroupModel">
        <result property="name" column="class_group_name_"/>
        <result property="id" column="class_group_id_"/>
        <collection property="members" ofType="com.ym.mec.common.entity.ImGroupMember">
            <result property="id" column="user_id_"/>
        </collection>
    </resultMap>

    <select id="queryStudentGroup" resultMap="ImGroupModelMap">
        SELECT cg.name_ class_group_name_,cg.id_ class_group_id_,su.id_ user_id_
        FROM class_group cg
        LEFT JOIN class_group_student_mapper cgsm ON cg.id_ = cgsm.class_group_id_
        LEFT JOIN sys_user su ON su.id_ = cgsm.user_id_
        WHERE su.id_ IS NOT NULL
    </select>

    <select id="queryTeacherGroup" resultMap="ImGroupModelMap">
        SELECT cg.name_ class_group_name_,cg.id_ class_group_id_,su.id_ user_id_
        FROM class_group cg
        LEFT JOIN class_group_teacher_mapper cgtm ON cg.id_ = cgtm.class_group_id_
        LEFT JOIN sys_user su ON su.id_ = cgtm.user_id_
        WHERE su.id_ IS NOT NULL
    </select>
    <resultMap id="ImUserModelMap" type="com.ym.mec.common.entity.ImUserModel">
        <result property="name" column="real_name_"/>
        <result property="id" column="id_"/>
        <result property="portrait" column="head_url_"/>
    </resultMap>
    <select id="queryImUser" resultMap="ImUserModelMap">
        SELECT su.id_,su.real_name_,su.avatar_ head_url_
        FROM sys_user su
        WHERE su.id_ IS NOT NULL
    </select>
    <select id="findByGroupAndType" resultMap="ClassGroup">
        SELECT * FROM class_group WHERE music_group_id_=#{groupId} AND group_type_=#{groupType} LIMIT 1
    </select>

    <update id="batchUpdateStudentNumMinusOne" parameterType="java.util.List">
		update class_group set student_num_ = (student_num_ - 1),update_time_ = now()
		where id_ IN
      <foreach collection="classGroupIds" item="classGroupId" open="(" close=")" separator=",">
          #{classGroupId}
      </foreach>
	</update>
    <update id="updateTest">
      update class_group set del_flag_=#{delFlag,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},update_time_ = NOW()
      where id_=1
    </update>

    <update id="batchUpdate">
        <foreach collection="classGroups" item="classGroup" separator=";">
            UPDATE class_group
            <set>
                <if test="classGroup.subjectIdList != null">
                    subject_id_list_ = #{classGroup.subjectIdList},
                </if>
                <if test="classGroup.studentNum != null">
                    student_num_ = #{classGroup.studentNum},
                </if>
                update_time_ = NOW(),
                <if test="classGroup.musicGroupId != null">
                    music_group_id_ = #{classGroup.musicGroupId},
                </if>
                <if test="classGroup.type != null">
                    type_ = #{classGroup.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="classGroup.groupType != null">
                    group_type_ = #{classGroup.groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="classGroup.name != null">
                    name_ = #{classGroup.name},
                </if>
                <if test="classGroup.expectStudentNum != null">
                    expect_student_num_ = #{classGroup.expectStudentNum},
                </if>
                <if test="classGroup.totalClassTimes != null">
                    total_class_times_ = #{classGroup.totalClassTimes},
                </if>
                <if test="classGroup.currentClassTimes!=null">
                    current_class_times_=#{classGroup.currentClassTimes},
                </if>
                <if test="classGroup.img!=null">
                    img_=#{classGroup.img},
                </if>
                <if test="classGroup.memo!=null">
                    memo_=#{classGroup.memo},
                </if>
                <if test="classGroup.delFlag != null">
                    del_flag_ = #{classGroup.delFlag},
                </if>
            </set>
            WHERE id_ = #{classGroup.id}
        </foreach>
    </update>
    <update id="updateGroupName">
        UPDATE class_group SET group_name_ = #{groupName} WHERE id_ = #{groupId}
    </update>

    <resultMap id="SuperClassGroupInfo" type="com.ym.mec.biz.dal.dto.SuperClassGroupDto" extends="ClassGroup">

    </resultMap>

    <sql id="superFindClassGroupsCondition">
        <where>
            <if test="search != null">
                AND (cg.name_ LIKE CONCAT('%',#{search},'%') OR cg.id_ = #{search}
                <if test="groups != null and organIdList==null">
                    OR cg.music_group_id_ IN
                    <foreach collection="groups" item="group" open="(" close=")" separator=",">
                        #{group.id}
                    </foreach>
                </if>
                <if test="classGroupIds != null and organIdList==null">
                    OR cg.id_ IN
                    <foreach collection="classGroupIds" item="classGroupId" open="(" close=")" separator=",">
                        #{classGroupId}
                    </foreach>
                </if>
                )
            </if>
            <if test="groups != null and organIdList!=null">
                AND cg.music_group_id_ IN
                <foreach collection="groups" item="group" open="(" close=")" separator=",">
                    #{group.id}
                </foreach>
            </if>
            <if test="classGroupIds != null and organIdList!=null">
                AND cg.id_ IN
                <foreach collection="classGroupIds" item="classGroupId" open="(" close=")" separator=",">
                    #{classGroupId}
                </foreach>
            </if>
            <if test="groupType != null">
                AND cg.group_type_=#{groupType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="groupId != null">
                AND cg.music_group_id_=#{groupId}
            </if>
            <if test="classGroupId != null">
                AND cg.id_=#{classGroupId}
            </if>
            <if test="classGroupType!=null">
                AND cg.type_=#{classGroupType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>
    <select id="superFindClassGroups" resultMap="SuperClassGroupInfo">
        SELECT
            *
        FROM
          class_group cg
            <include refid="superFindClassGroupsCondition"/>
            ORDER BY cg.create_time_ DESC
        <include refid="global.limit"/>
    </select>
    <select id="superCountClassGroups" resultType="int">
        SELECT
          COUNT(id_)
        FROM
            class_group cg
            <include refid="superFindClassGroupsCondition"/>
    </select>
    <select id="findClassGroupIdsByStudent" resultType="int">
        SELECT
            cgsm.class_group_id_
        FROM
            class_group_student_mapper cgsm
            LEFT JOIN sys_user su ON cgsm.user_id_ = su.id_
        WHERE su.username_ LIKE CONCAT('%',#{search},'%') OR su.id_=#{search}
    </select>
    <select id="findClassGroupIdsByTeacher" resultType="int">
        SELECT
            cgtm.class_group_id_
        FROM
            class_group_teacher_mapper cgtm
            LEFT JOIN sys_user su ON cgtm.user_id_ = su.id_
        WHERE su.real_name_ LIKE CONCAT('%',#{search},'%') OR su.id_=#{search}
    </select>
    <select id="countClassNumByType" resultType="java.lang.Integer">
        SELECT COUNT(id_) FROM class_group WHERE type_ IN ('HIGH','HIGH_ONLINE') AND del_flag_ = '0'
    </select>

    <select id="findClassGroupSubjectNameMaps" resultType="java.util.Map">
        SELECT
        cs.id_ 'key',
        GROUP_CONCAT(DISTINCT j.name_) 'value'
        FROM
        course_schedule cs
        LEFT JOIN class_group cg ON cg.id_=cs.class_group_id_
        LEFT JOIN `subject` j ON  FIND_IN_SET(j.id_,cg.subject_id_list_)
        WHERE cs.id_ IN
        <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
            #{courseScheduleId}
        </foreach>
        GROUP BY cs.id_
    </select>
    <select id="queryClassGroupIds" resultType="java.lang.Integer">
        SELECT cg.id_ FROM class_group cg
        WHERE cg.music_group_id_ IN
        <foreach collection="vipGroupIds" item="groupId" open="(" close=")" separator=",">
            #{groupId}
        </foreach>
        AND cg.group_type_ = 'VIP'
    </select>
    <select id="findClassGroupWithIds" resultMap="ClassGroup">
        SELECT * FROM class_group WHERE id_ IN
        <foreach collection="classGroupIds" item="classGroupId" open="(" close=")" separator=",">
            #{classGroupId}
        </foreach>
    </select>
    <select id="findHighClassGroupByTypeAndSubjectId" resultMap="HighClassGroup">
        SELECT * FROM class_group WHERE music_group_id_=#{musicGroupId}
        <if test="subjectId != null and type=='HIGH'">
            AND FIND_IN_SET(#{subjectId},subject_id_list_)
        </if>
        <if test="subjectId != null and type=='HIGH_ONLINE'">
            AND FIND_IN_SET(#{subjectId},memo_)
        </if>
        <if test="classGroupIds != null">
            AND FIND_IN_SET(id_,#{classGroupIds})
        </if>
        AND type_ = #{type} AND del_flag_='0';
    </select>

    <select id="findHighClassGroup" resultMap="HighClassGroup">
        SELECT * FROM class_group WHERE music_group_id_=#{musicGroupId}
        <if test="classGroupIds != null">
            AND FIND_IN_SET(id_,#{classGroupIds})
        </if>
        AND type_ IN ('HIGH','HIGH_ONLINE') AND del_flag_ = '0'
    </select>
    <select id="queryClassGroups" resultMap="ClassGroup">
        SELECT cg.* FROM class_group cg WHERE cg.music_group_id_ = #{practiceGroupId} AND cg.group_type_ = #{groupType}
    </select>
</mapper>
