<?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.MusicGroupDao">
    <resultMap type="com.ym.mec.biz.dal.entity.MusicGroup" id="MusicGroup">
        <result column="id_" property="id"/>
        <result column="name_" property="name"/>
        <result column="organ_id_" property="organId"/>
        <result column="school_id_" property="schoolId"/>
        <result column="apply_expire_date_" property="applyExpireDate"/>
        <result column="pre_apply_expire_date_" property="preApplyExpireDate"/>
        <result column="team_teacher_id_" property="teamTeacherId"/>
        <result column="educational_teacher_id_" property="educationalTeacherId"/>
        <result column="transaction_teacher_id_" property="transactionTeacherId"/>
        <result column="charge_type_id_" property="chargeTypeId"/>
        <result column="course_form_" property="courseForm"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="bill_start_date_" property="billStartDate"/>
        <result column="improvent_classes_num_" property="improventClassesNum"/>
        <result column="enroll_classes_" property="enrollClasses"/>
        <result column="payment_expire_date_" property="paymentExpireDate"/>
        <result column="is_extra_class_" property="isExtraClass"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="cooperation_organ_id_" property="cooperationOrganId"/>
        <result column="enlightenment_course_time_" property="enlightenmentCourseTime"/>
        <result column="parent_meeting_time_" property="parentMeetingTime"/>
        <result column="img_" property="img"/>
        <result column="director_user_id_" property="directorUserId"/>
        <result column="settlement_type_" property="settlementType"/>
        <result column="expect_start_group_date_" property="expectStartGroupDate"/>
        <result column="group_member_num_" property="groupMemberNum"/>
        <result column="memo_" property="memo"/>
        <result column="repair_user_id_" property="repairUserId"/>
        <result column="ownership_type_" property="ownershipType"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="del_flag_" property="delFlag"/>
        <result column="payment_valid_start_date_" property="paymentValidStartDate"/>
        <result column="payment_valid_end_date_" property="paymentValidEndDate"/>
        <result column="payment_pattern_" property="paymentPattern"/>
        <result column="is_classroom_lessons_" property="isClassroomLessons"/>
        <result column="course_view_type_" property="courseViewType"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

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

    <select id="getLocked" resultMap="MusicGroup">
        SELECT *
        FROM music_group
        WHERE id_ = #{id} for
        update
    </select>

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

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.MusicGroup"
            useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO music_group
                (id_, name_, organ_id_, school_id_, apply_expire_date_, pre_apply_expire_date_, team_teacher_id_,
                 educational_teacher_id_,
                 charge_type_id_, course_form_, create_time_, update_time_, status_,
                 bill_start_date_, improvent_classes_num_, enroll_classes_, payment_expire_date_, is_extra_class_,
                 settlement_type_, cooperation_organ_id_, enlightenment_course_time_,
                 parent_meeting_time_, img_, director_user_id_, is_classroom_lessons_, memo_, expect_start_group_date_,
                 ownership_type_, repair_user_id_, del_flag_, payment_valid_start_date_, payment_valid_end_date_,
                 payment_pattern_, course_view_type_, transaction_teacher_id_)
        VALUES (#{id}, #{name}, #{organId}, #{schoolId}, #{applyExpireDate}, #{preApplyExpireDate}, #{teamTeacherId},
                #{educationalTeacherId},
                #{chargeTypeId}, #{courseForm}, now(), now(),
                #{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{billStartDate},
                #{improventClassesNum}, #{enrollClasses}, #{paymentExpireDate},
                #{isExtraClass, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                #{settlementType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                #{cooperationOrganId}, #{enlightenmentCourseTime}, #{parentMeetingTime}, #{img}, #{directorUserId},
                #{isClassroomLessons}, #{memo}, #{expectStartGroupDate},
                #{ownershipType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{repairUserId},
                #{delFlag}, #{paymentValidStartDate}, #{paymentValidEndDate}, #{paymentPattern},
                #{courseViewType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{transactionTeacherId})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.MusicGroup">
        UPDATE music_group
        <set>
            <if test="paymentPattern != null">
                payment_pattern_ = #{paymentPattern},
            </if>
            <if test="paymentValidStartDate != null">
                payment_valid_start_date_ = #{paymentValidStartDate},
                payment_valid_end_date_ = #{paymentValidEndDate},
            </if>
            <if test="ownershipType != null">
                ownership_type_ = #{ownershipType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="status != null">
                status_ = #{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            repair_user_id_ = #{repairUserId},
            transaction_teacher_id_ = #{transactionTeacherId},
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="improventClassesNum != null">
                improvent_classes_num_ = #{improventClassesNum},
            </if>
            <if test="courseForm != null">
                course_form_ = #{courseForm},
            </if>
            <if test="chargeTypeId != null">
                charge_type_id_ = #{chargeTypeId},
            </if>
            <if test="educationalTeacherId != null">
                educational_teacher_id_ = #{educationalTeacherId},
            </if>
            <if test="enrollClasses != null">
                enroll_classes_ = #{enrollClasses},
            </if>
            <if test="name != null">
                name_ = #{name},
            </if>
            <if test="isExtraClass != null">
                is_extra_class_ = #{isExtraClass, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="paymentExpireDate != null">
                payment_expire_date_ = #{paymentExpireDate},
            </if>
            <if test="schoolId != null">
                school_id_ = #{schoolId},
            </if>
            <if test="applyExpireDate != null">
                apply_expire_date_ = #{applyExpireDate},
            </if>
            <if test="preApplyExpireDate != null">
                pre_apply_expire_date_ = #{preApplyExpireDate},
            </if>
            <if test="billStartDate != null">
                bill_start_date_ = #{billStartDate},
            </if>
            <if test="cooperationOrganId != null">
                cooperation_organ_id_ = #{cooperationOrganId},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="enlightenmentCourseTime != null">
                enlightenment_course_time_ = #{enlightenmentCourseTime},
            </if>
            <if test="teamTeacherId != null">
                team_teacher_id_ = #{teamTeacherId},director_user_id_ = #{directorUserId},
            </if>
            <if test="parentMeetingTime != null">
                parent_meeting_time_ = #{parentMeetingTime},
            </if>
            <if test="img != null">
                img_ = #{img},
            </if>
            <if test="settlementType != null">
                settlement_type_ = #{settlementType},
            </if>
            <if test="isClassroomLessons != null">
                is_classroom_lessons_ = #{isClassroomLessons},
            </if>
            <if test="expectStartGroupDate != null">
                expect_start_group_date_ = #{expectStartGroupDate},
            </if>
            <if test="groupMemberNum != null">
                group_member_num_ = #{groupMemberNum},
            </if>
            <if test="courseViewType != null">
                course_view_type_ = #{courseViewType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="memo != null">
                memo_ = #{memo},
            </if>
            <if test="delFlag != null">
                del_flag_ = #{delFlag},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>
    <update id="batchUpdateEdu">
        UPDATE music_group
        SET educational_teacher_id_ = #{educationUserId}
        WHERE cooperation_organ_id_ = #{cooperationOrganId}
    </update>

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

    <select id="queryPage" resultMap="MusicGroup" parameterType="map">
        SELECT * FROM music_group
        <include refid="musicGroupQueryPageSql"/>
        ORDER BY create_time_ DESC,id_
        <include refid="global.limit"/>
    </select>

    <sql id="musicGroupQueryPageSql">
        <where>
            del_flag_ = 0
            <if test="organId != null">
                AND FIND_IN_SET(organ_id_,#{organId})
            </if>
            <if test="musicGroupName != null and musicGroupName != ''">
                AND (name_ LIKE CONCAT('%',#{musicGroupName},'%') OR id_ LIKE CONCAT('%',#{musicGroupName},'%'))
            </if>
            <if test="chargeTypeId != null">
                AND charge_type_id_ = #{chargeTypeId}
            </if>
            <if test="musicGroupStatus != null">
                AND status_ = #{musicGroupStatus}
            </if>
            <if test="musicGroupId != null">
                AND FIND_IN_SET(id_,#{musicGroupId})
            </if>
            <if test="transactionTeacherId!=null">
                AND transaction_teacher_id_ = #{transactionTeacherId}
            </if>
            <if test="musicGroupIds!=null and musicGroupIds.size()>0">
                AND id_ IN
                <foreach collection="musicGroupIds" item="musicGroupId" open="(" close=")" separator=",">
                    #{musicGroupId}
                </foreach>
            </if>
        </where>
    </sql>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM music_group
        <include refid="musicGroupQueryPageSql"/>
    </select>

    <resultMap type="com.ym.mec.biz.dal.dto.MusicCardDto" id="queryUserMusicGroupsMap">
        <result column="register_id_" property="registerId"/>
        <result column="user_name_" property="userName"/>
        <result column="subject_name_" property="subjectName"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="current_class_" property="currentClass"/>
        <result column="current_grade_" property="currentGrade"/>
        <result column="music_group_name_" property="musicGroupName"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="vip_class_num_" property="vipClassNum"/>
        <result column="music_group_num_" property="musicGroupNum"/>
        <result column="music_group_status_" property="musicGroupStatus"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

    <select id="queryUserMusicGroups" resultMap="queryUserMusicGroupsMap">
        SELECT sr.id_       register_id_
             , su.username_ user_name_
             , sr.current_class_
             , sr.current_grade_
             , mg.name_     music_group_name_
             , s.name_      subject_name_
             , s.id_        subject_id_
             , mg.id_       music_group_id_
             , mg.status_   music_group_status_
        FROM student_registration sr
                     LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
                     LEFT JOIN `subject` s ON sr.actual_subject_id_ = s.id_
                     LEFT JOIN sys_user su ON su.id_ = sr.user_id_
        WHERE sr.user_id_ = #{userId}
          AND mg.status_ IN ('APPLY', 'PAY', 'PREPARE', 'PROGRESS')
          AND sr.music_group_status_ != 'QUIT'
        ORDER BY mg.create_time_ DESC
    </select>

    <select id="queryPersonalMusicGroups" resultMap="queryUserMusicGroupsMap">
        SELECT sr.name_ user_name_, mg.name_ music_group_name_, s.name_ subject_name_
        FROM student_registration sr
                     LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
                     LEFT JOIN `subject` s ON sr.actual_subject_id_ = s.id_
        WHERE sr.user_id_ = #{userId}
          AND sr.music_group_status_ != 'QUIT'
        ORDER BY mg.update_time_ DESC
        LIMIT 1
    </select>

    <select id="countUserMusicGroups" resultType="int">
        SELECT COUNT(mg.id_)
        FROM student_registration sr
                     LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        WHERE sr.user_id_ = #{userId}
          AND sr.music_group_status_ != 'QUIT'
    </select>
    <select id="countCourseByType" resultType="java.lang.Integer">
        SELECT SUM(cg.total_class_times_) - SUM(cg.current_class_times_)
        FROM class_group_student_mapper cgsm
                     LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
        WHERE cgsm.user_id_ = #{userId}
          AND cg.type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
          AND cg.del_flag_ = 0
          AND cgsm.status_ != 'QUIT'
    </select>

    <resultMap type="com.ym.mec.biz.dal.dto.BasicUserDto" id="BasicUserDto">
        <result column="subject_name_" property="subjectName"/>
        <result column="username_" property="name"/>
        <result column="user_id_" property="userId"/>
        <result column="organ_id_" property="organId"/>
        <result column="head_url_" property="headUrl"/>
        <result column="gender_" property="gender"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="user_type_" property="userType"/>
        <result column="music_group_name_" property="musicGroupName"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="phone_" property="phone"/>
        <result column="care_package_" property="carePackage"/>
        <result column="come_on_package_" property="comeOnPackage"/>
    </resultMap>

    <select id="queryUserGroups" resultMap="com.ym.mec.biz.dal.dao.ClassGroupDao.imGroupModel">
        SELECT mg.id_,mg.name_,mg.img_
        FROM student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        <where>
            sr.music_group_status_ != 'QUIT'
            <if test="userId != null">
                AND sr.user_id_ = #{userId}
            </if>
            <if test="search != null">
                AND mg.name_ LIKE CONCAT('%',#{search},'%')
            </if>
        </where>
    </select>

    <select id="countMusicGroupUserNum" resultType="map" parameterType="list">
        SELECT SUM(student_num_) 'value', music_group_id_ 'key'
        FROM class_group
        WHERE FIND_IN_SET(music_group_id_, #{musicGroupIds})
        GROUP BY music_group_id_
    </select>

    <resultMap id="ImUserModel" type="com.ym.mec.common.entity.ImUserModel">
        <result column="id_" property="id"/>
        <result property="name" column="username_"/>
        <result property="portrait" column="avatar_"/>
    </resultMap>
    <select id="queryTeacherGroups" resultMap="com.ym.mec.biz.dal.dao.ClassGroupDao.imGroupModel">
        SELECT mg.id_,mg.name_,mg.img_,GROUP_CONCAT(DISTINCT cgtm.user_id_) teacher_ids_
        FROM music_group mg
        LEFT JOIN class_group cg ON mg.id_ = cg.music_group_id_
        LEFT JOIN class_group_teacher_mapper cgtm ON cg.id_ = cgtm.class_group_id_
        WHERE (cgtm.user_id_ = #{userId} OR mg.team_teacher_id_ = #{userId} OR mg.educational_teacher_id_ = #{userId}
        OR director_user_id_ = #{userId}) AND mg.status_ IN ('PROGRESS','PAUSE')
        <if test="search != null and search != ''">
            AND mg.name_ LIKE CONCAT('%',#{search},'%')
        </if>
        GROUP BY mg.id_
    </select>
    <select id="countPayNum" resultType="java.util.Map" parameterType="list">
        SELECT cg.music_group_id_ `key`, COUNT(cgsm.id_) `value`
        FROM class_group cg
                     LEFT JOIN class_group_student_mapper cgsm ON cg.id_ = cgsm.class_group_id_
        WHERE FIND_IN_SET(cg.music_group_id_, #{musicGroupIds})
          AND cgsm.status_ != 'QUIT'
          AND cg.type_ != 'MIX'
          AND cg.group_type_ = 'MUSIC'
        GROUP BY cg.music_group_id_
    </select>

    <select id="queryListByIds" resultMap="MusicGroup" parameterType="list">
        SELECT *
        FROM music_group
        WHERE FIND_IN_SET(id_, #{musicGroupIds})
    </select>
    <select id="findSettlementTypeByMusicGroups" resultType="map">
        SELECT
        id_ AS 'key',
        settlement_type_ AS 'value'
        FROM
        music_group
        WHERE
        id_ IN
        <foreach collection="musicGroupIds" item="musicGroupId" open="(" close=")" separator=",">
            #{musicGroupId}
        </foreach>
    </select>

    <select id="queryOrganMusicGroupNum" resultType="map">
        select count(*) total,sum(case when status_ = 'PREPARE' then 1 else 0 end) prepare_num_,sum(case when status_ =
        'PROGRESS' then 1 else 0 end) progress_num_,sum(case when status_ = 'PAUSE' then 1 else 0 end) pause_num_ from
        music_group
        where organ_id_ not in (36,38)
        <if test="organId != null">
            and FIND_IN_SET(organ_id_,#{organId})
        </if>
    </select>

    <select id="queryCurrentMonthCoursesNum" resultType="int">
        SELECT count(cs.id_) from course_schedule cs
        left join class_group cg on cs.class_group_id_ = cg.id_
        left join music_group mg on cg.music_group_id_ = mg.id_
        where cs.group_type_ = 'MUSIC' AND cg.group_type_ = 'MUSIC'
        and DATE_FORMAT(cs.class_date_, '%Y%m' ) = DATE_FORMAT(CURDATE() , '%Y%m') and (cs.del_flag_ = 0 or cs.del_flag_
        is null) and mg.organ_id_ not in (36,38) AND cs.pre_course_flag_ = 0
        <if test="organId != null">
            AND FIND_IN_SET(mg.organ_id_,#{organId})
        </if>
    </select>
    <select id="queryTeacherMap" resultType="java.util.Map">
        SELECT su.id_ 'key', su.phone_ 'value'
        FROM class_group_teacher_mapper cgtm
                     LEFT JOIN sys_user su ON cgtm.user_id_ = su.id_
        WHERE cgtm.music_group_id_ = #{musicGroupId}
          AND su.phone_ IS NOT NULL
        GROUP BY su.id_
    </select>
    <select id="queryUserIdByRoleId" resultType="java.lang.Integer" parameterType="list">
        SELECT DISTINCT sur.user_id_ FROM sys_user_role sur
        LEFT JOIN employee e ON sur.user_id_ = e.user_id_
        WHERE sur.role_id_ IN
        <foreach collection="roleIds" open="(" separator="," close=")" item="item">
            #{item}
        </foreach>
        <if test="organId != null">
            AND FIND_IN_SET(#{organId},e.organ_id_list_)
        </if>
    </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_ = #{musicGroupId}
        GROUP BY cgtm.user_id_
    </select>
    <select id="queryEducationalMusicGroupId" resultType="java.lang.String">
        SELECT GROUP_CONCAT(mg.id_)
        FROM music_group mg
        WHERE mg.educational_teacher_id_ = #{userIds}
    </select>

    <select id="findUserMusicGroup" resultMap="MusicGroup">
        SELECT mg.*
        FROM music_group mg
                     LEFT JOIN student_registration sr on sr.music_group_id_ = mg.id_
        WHERE sr.user_id_ = #{userId}
        ORDER BY sr.id_ ASC
        LIMIT 1
    </select>

    <select id="findUserMusicGroups" resultMap="MusicGroup">
        SELECT mg.*
        FROM music_group mg
                     LEFT JOIN student_registration sr on sr.music_group_id_ = mg.id_
        WHERE sr.user_id_ = #{userId}
    </select>

    <select id="findMusicGroup" resultMap="MusicGroup">
        SELECT mg.* FROM student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        WHERE sr.id_ IN
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        AND mg.id_ IS NOT NULL LIMIT 1
    </select>
    <select id="queryMusicGroupNameMap" resultType="java.util.Map">
        SELECT id_ 'key',name_ 'value' FROM music_group WHERE id_ IN
        <foreach collection="musicGroupIds" open="(" close=")" item="item" separator=",">
            #{item}
        </foreach>
        AND name_ IS NOT NULL
    </select>

    <select id="findUserSchool" resultMap="com.ym.mec.biz.dal.dao.SchoolDao.School">
        SELECT s.*
        FROM music_group mg
                     LEFT JOIN student_registration sr on sr.music_group_id_ = mg.id_
                     LEFT JOIN school s on mg.school_id_ = s.id_
        WHERE sr.user_id_ = #{userId}
        ORDER BY sr.id_ DESC
        LIMIT 1
    </select>
    <select id="findByClassId" resultMap="MusicGroup">
        SELECT mg.*
        FROM class_group cg
                     LEFT JOIN music_group mg ON cg.music_group_id_ = mg.id_
        WHERE cg.id_ = #{groupId}
          AND cg.group_type_ = 'MUSIC'
          AND mg.status_ IN ('PROGRESS', 'PAUSE')
          AND cg.del_flag_ = 0
    </select>
    <select id="queryUserMusicNames" resultType="java.util.Map">
        SELECT sr.user_id_ 'key',GROUP_CONCAT(mg.name_) 'value' FROM student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        WHERE mg.status_ = 'PROGRESS' AND sr.user_id_ IN
        <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
            #{userId}
        </foreach>
        GROUP BY sr.user_id_
    </select>
    <select id="queryUserMusicStatus" resultType="java.util.Map">
        SELECT sr.user_id_ 'key',GROUP_CONCAT(CASE WHEN sr.music_group_status_ = 'NORMAL' THEN '在读'
        WHEN sr.music_group_status_ = 'LEAVE' THEN '请假'
        WHEN sr.music_group_status_ = 'QUIT' THEN '退班'
        WHEN sr.music_group_status_ = 'APPLY' THEN '报名'
        WHEN sr.music_group_status_ = 'QUIT_SCHOOL' THEN '休学' END) 'value'
        FROM student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        WHERE mg.status_ = 'PROGRESS' AND sr.user_id_ IN
        <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
            #{userId}
        </foreach>
        GROUP BY sr.user_id_
    </select>

    <sql id="findMusicGroupsCondition">
        <where>
            <if test="educationalTeacherId!=null">
                AND educational_teacher_id_=#{educationalTeacherId}
            </if>
            <if test="search!=null">
                AND (id_=#{search} OR name_ LIKE CONCAT('%', #{search}, '%'))
            </if>
            <if test="status!=null">
                AND status_=#{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>
    <select id="findMusicGroups" resultMap="MusicGroup">
        SELECT * FROM music_group
        <include refid="findMusicGroupsCondition"/>
        ORDER BY id_
        <include refid="global.limit"/>
    </select>
    <select id="countMusicGroups" resultType="int">
        SELECT COUNT(id_) FROM music_group
        <include refid="findMusicGroupsCondition"/>
    </select>

    <select id="getMusicGroupRegister" resultType="com.ym.mec.biz.dal.dto.MusicGroupRegisterDto">
        SELECT mg.id_ musicGroupId ,o.name_ organName, mg.name_ musicGroupName, mg.status_ musicGroupStatus,
        COUNT(sr.id_) registerNum,SUM(IF(sr.music_group_status_='NORMAL',1,0)) normalNum
        FROM music_group mg
        LEFT JOIN student_registration sr ON mg.id_ = sr.music_group_id_
        LEFT JOIN organization o on o.id_ = mg.organ_id_
        <where>
            <if test="musicGroupStatusList != null">
                mg.status_ IN
                <foreach collection="musicGroupStatusList" item="musicGroupStatus" open="(" close=")"
                         separator=",">
                    #{musicGroupStatus}
                </foreach>
            </if>
            <if test="organIds != null">
                AND FIND_IN_SET (mg.organ_id_,#{organIds})
            </if>
            AND (mg.del_flag_ IS NULL OR mg.del_flag_=0)
        </where>
        GROUP BY mg.id_
        ORDER BY mg.create_time_ ASC
    </select>

    <select id="getRegisterSubjectNum" resultType="com.ym.mec.biz.dal.dto.RegisterSubjectDto">
        SELECT sr.music_group_id_ musicGroupId,s.id_ subjectId,s.name_ subjectName,count(sr.actual_subject_id_) num FROM
        student_registration sr
        LEFT JOIN subject s ON s.id_ = sr.actual_subject_id_
        WHERE sr.music_group_id_ IN
        <foreach collection="musicGroupIds" item="musicGroupId" open="(" close=")" separator=",">
            #{musicGroupId}
        </foreach>
        <if test="studentMusicGroupStatus != null">
            AND sr.music_group_status_ =
            #{studentMusicGroupStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        </if>
        <if test="payStatus != null">
            AND sr.payment_status_ = #{payStatus}
        </if>
        GROUP BY sr.music_group_id_,sr.actual_subject_id_
    </select>

    <select id="getMusicGroupRegisterOrder" resultType="com.ym.mec.biz.dal.dto.MusicGroupRegisterDto">
        SELECT spo.music_group_id_ musicGroupId, COUNT(spod.id_) GroupBuyMusicalNum FROM student_payment_order spo
        LEFT JOIN student_payment_order_detail spod ON spod.payment_order_id_ = spo.id_
        WHERE spo.music_group_id_ IN
        <foreach collection="musicGroupIds" item="musicGroupId" open="(" close=")" separator=",">
            #{musicGroupId}
        </foreach>
        AND spo.status_ = 'SUCCESS'
        AND spo.type_ = 'APPLY'
        AND spod.kit_group_purchase_type_ = 'GROUP'
        AND spod.type_='MUSICAL'
        GROUP BY spo.music_group_id_
    </select>

    <select id="getMusicGroupRegisterTotalMoney" resultType="com.ym.mec.biz.dal.dto.MusicGroupRegisterDto">
        SELECT music_group_id_ musicGroupId, SUM(actual_amount_) totalMoney FROM student_payment_order WHERE
        music_group_id_ IN
        <foreach collection="musicGroupIds" item="musicGroupId" open="(" close=")" separator=",">
            #{musicGroupId}
        </foreach>
        AND status_ = 'SUCCESS'
        AND type_ = 'APPLY'
        GROUP BY music_group_id_
    </select>

    <select id="getTeacherMusicGroup" resultMap="MusicGroup">
        SELECT mg.*
        FROM course_schedule cs
                     LEFT JOIN music_group mg ON mg.id_ = cs.music_group_id_
        WHERE cs.actual_teacher_id_ = #{teacherId}
          AND cs.group_type_ = 'MUSIC' AND cs.pre_course_flag_ = 0
        GROUP BY mg.id_
    </select>
    <select id="queryUserMusicInfos" resultMap="queryUserMusicGroupsMap">
        SELECT mg.name_ music_group_name_, mg.id_ music_group_id_
        FROM student_registration sr
                     LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        WHERE sr.user_id_ = #{userId}
          AND mg.status_ IN ('APPLY', 'PAY', 'PREPARE', 'PROGRESS')
          AND sr.music_group_status_ != 'QUIT'
        GROUP BY mg.id_
        ORDER BY mg.create_time_ DESC
    </select>
    <select id="queryOrganMusicInfos" resultMap="queryUserMusicGroupsMap">
        SELECT
        mg.name_ music_group_name_,
        mg.id_ music_group_id_
        FROM music_group mg
        WHERE mg.status_ IN ('APPLY','PAY','PREPARE','PROGRESS')
        <if test="organId != null">
            AND mg.organ_id_ = #{organId}
        </if>
        ORDER BY mg.create_time_ DESC
    </select>
    <select id="getFirstEduTeacherId" resultType="java.lang.Integer">
        SELECT mg.educational_teacher_id_
        FROM student_registration sr
                     LEFT JOIN music_group mg ON mg.id_ = sr.music_group_id_
        WHERE user_id_ = #{userId}
          AND mg.status_ != 'CANCELED'
          AND sr.music_group_status_ != 'QUIT'
          AND mg.educational_teacher_id_ IS NOT NULL
        LIMIT 1
    </select>
    <select id="getStuMusic" resultMap="MusicGroup">
        SELECT mg.*
        FROM music_group mg
                     LEFT JOIN student_registration sr ON mg.id_ = sr.music_group_id_
        WHERE mg.status_ != 'CANCELED'
          AND sr.music_group_status_ != 'QUIT'
          AND sr.user_id_ = #{studentId}
        LIMIT 1
    </select>
    <select id="getStuEduMusic" resultMap="MusicGroup">
        SELECT mg.*
        FROM music_group mg
                     LEFT JOIN student_registration sr ON mg.id_ = sr.music_group_id_
        WHERE mg.status_ != 'CANCELED'
          AND sr.music_group_status_ != 'QUIT'
          AND sr.user_id_ = #{studentId}
          AND mg.educational_teacher_id_ = #{educationalTeacherId}
        LIMIT 1
    </select>
    <select id="findByClassGroupId" resultMap="MusicGroup">
        SELECT mg.*
        FROM music_group mg
                     LEFT JOIN class_group cg ON cg.music_group_id_ = mg.id_
        WHERE cg.id_ = #{classGroupId}
        LIMIT 1
    </select>
    <select id="queryIdsByEduIdAndOrganIds" resultType="java.lang.String">
        SELECT DISTINCT id_ FROM music_group
        <where>
            status_ = 'PROGRESS'
            <if test="educationId != null">
                AND educational_teacher_id_ = #{educationId}
            </if>
            <if test="organIds != null and organIds != ''">
                AND FIND_IN_SET(organ_id_,#{organIds})
            </if>
        </where>
    </select>
    <select id="getMusicGroupSchool" resultMap="com.ym.mec.biz.dal.dao.SchoolDao.School">
        SELECT s.*
        FROM music_group mg
                     LEFT JOIN school s ON s.id_ = mg.school_id_
        WHERE mg.id_ = #{id}
    </select>
    <select id="getByEduTeacher" resultMap="MusicGroup">
        SELECT * FROM music_group WHERE organ_id_ = #{organId}
        AND educational_teacher_id_ = #{eduTeacherId} AND status_ = 'PROGRESS'
        <if test="search != null">
            AND name_ LIKE CONCAT('%',#{search},'%')
        </if>
    </select>

    <select id="getMusicGroupEduTeacher" resultMap="com.ym.mec.biz.dal.dao.TeacherDao.SysUser">
        SELECT su.*
        FROM music_group mg
                     LEFT JOIN sys_user su ON su.id_ = mg.educational_teacher_id_
        WHERE mg.organ_id_ = #{organId}
          AND status_ = 'PROGRESS'
          AND mg.educational_teacher_id_ > 0
        GROUP BY su.id_
    </select>

    <select id="findUserMusicGroupCooperationOrgan"
            resultMap="com.ym.mec.biz.dal.dao.CooperationOrganDao.CooperationOrgan">
        SELECT co.name_, su.real_name_ linkman_
        FROM music_group mg
                     LEFT JOIN student_registration sr ON sr.music_group_id_ = mg.id_
                     LEFT JOIN cooperation_organ co ON co.id_ = mg.cooperation_organ_id_
                     LEFT JOIN sys_user su ON su.id_ = mg.educational_teacher_id_
        WHERE sr.user_id_ = #{userId}
          AND mg.status_ IN ('PREPARE', 'PROGRESS', 'PAUSE', 'CLOSE')
          AND sr.payment_status_ = 2
        ORDER BY sr.id_ DESC
        LIMIT 1
    </select>

    <select id="getMusicGroupByIds" resultMap="MusicGroup">
        SELECT * FROM music_group WHERE id_ IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="getNoClassStudentMusicGroupIds" resultType="java.lang.String">
        SELECT
        DISTINCT sr.music_group_id_
        FROM
        student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        LEFT JOIN class_group_student_mapper cgsm ON cgsm.group_type_='MUSIC' AND
        cgsm.music_group_id_=sr.music_group_id_ AND cgsm.user_id_=sr.user_id_
        WHERE
        sr.music_group_status_ = 'NORMAL'
        AND mg.status_ = 'PROGRESS'
        AND cgsm.id_ IS NULL
        <if test="organIds!=null and organIds.size()>0">
            AND mg.organ_id_ IN
            <foreach collection="organIds" item="organId" open="(" close=")" separator=",">
                #{organId}
            </foreach>
        </if>
    </select>

    <select id="getCooperationMusicGroupNum" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM music_group
        WHERE cooperation_organ_id_ = #{cooperationOrganId}
          AND status_ IN ('PROGRESS', 'PAUSE', 'CLOSE')
          AND id_ != #{musicGroupId}
    </select>

    <select id="findCooperationOrganEduTeacher" resultMap="com.ym.mec.biz.dal.dao.CooperationOrganDao.CooperationOrgan">
        SELECT co.name_, su.real_name_ linkman_
        FROM music_group mg
                     LEFT JOIN cooperation_organ co ON co.id_ = mg.cooperation_organ_id_
                     LEFT JOIN sys_user su ON su.id_ = mg.educational_teacher_id_
        WHERE co.id_ = #{id}
          AND mg.status_ IN ('PREPARE', 'PROGRESS', 'PAUSE', 'CLOSE')
        ORDER BY mg.create_time_ DESC
        LIMIT 1
    </select>
    <select id="queryEduNameMapByCourseId" resultType="java.util.Map">
        SELECT cs.id_ 'key',su.real_name_ 'value' FROM course_schedule cs
        LEFT JOIN music_group mg ON mg.id_ = cs.music_group_id_
        LEFT JOIN sys_user su ON su.id_ = mg.educational_teacher_id_
        WHERE cs.id_ IN
        <foreach collection="courseIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        AND mg.educational_teacher_id_ IS NOT NULL
    </select>
    <select id="findByCooperationIdAndStatus" resultMap="MusicGroup">
        SELECT *
        FROM music_group
        WHERE cooperation_organ_id_ = #{cooperationOrganId}
          AND status_ IN ('PROGRESS', 'PAUSE', 'PREPARE')
    </select>
    <select id="findByCooperationId" resultMap="MusicGroup">
        SELECT *
        FROM music_group
        WHERE cooperation_organ_id_ = #{cooperationId}
          AND del_flag_ = 0
    </select>

    <select id="getStudentLastMusicGroup" resultMap="MusicGroup">
        SELECT mg.*
        FROM music_group mg
                     LEFT JOIN student_registration sr ON mg.id_ = sr.music_group_id_
        WHERE sr.user_id_ = #{userId}
          AND mg.status_ IN ('PREPARE', 'PROGRESS', 'PAUSE', 'CLOSE')
        ORDER BY mg.create_time_ DESC
        LIMIT 1
    </select>

    <select id="getMusicGroupByStatus" resultType="com.ym.mec.biz.dal.dto.MusicGroupRegAndMoneyDto">
        SELECT mg.id_ musicGroupId,o.name_ organName,mg.name_ musicGroupName,mg.status_ musicGroupStatus FROM
        music_group mg
        LEFT JOIN organization o ON mg.organ_id_ = o.id_
        WHERE status_ IN ('PRE_APPLY','PRE_BUILD_FEE','FEE_AUDIT','APPLY','PAY')
        <if test="organId != null">
            AND FIND_IN_SET(mg.organ_id_,#{organId})
        </if>
        <if test="cooperationOrganIds != null">
            AND mg.cooperation_organ_id_ IN
            <foreach collection="cooperationOrganIds" item="cooperationOrganId" open="(" close=")" separator=",">
                #{cooperationOrganId}
            </foreach>
        </if>
        <if test="musicGroupName != null">
           AND mg.name_ LIKE CONCAT('%',#{musicGroupName},'%')
        </if>
    </select>
    <select id="countMusicMemberList" resultType="java.lang.Integer">
        SELECT COUNT(c.user_id_) FROM
        (SELECT sr.user_id_ FROM student_registration sr
        LEFT JOIN music_group mg ON mg.id_ = sr.music_group_id_
        LEFT JOIN cloud_teacher_order cto ON cto.student_id_ = sr.user_id_ AND cto.status_ IN (1,2)
        LEFT JOIN sys_user su ON su.id_ = sr.user_id_
        LEFT JOIN organization o ON o.id_ = mg.organ_id_
        LEFT JOIN student_visit sv ON sv.student_id_ = sr.user_id_ AND sv.purpose_ = '会员续费'
        <include refid="queryMusicMemberListSql"/>
        GROUP BY sr.user_id_ HAVING (MAX(cto.id_) IS NULL OR MIN(cto.status_) != 1)
        <if test="hasMember != null">
            <if test="hasMember == 0">
                AND (MAX(cto.id_) IS NULL OR DATEDIFF(MAX(cto.end_time_),NOW()) &lt; 0)
            </if>
            <if test="hasMember == 1">
                AND DATEDIFF(MAX(cto.end_time_),NOW()) &lt;= #{memberEndAutoQuitMusic} AND DATEDIFF(MAX(cto.end_time_),NOW()) >= 0
            </if>
        </if>
        <if test="visitStartTime != null">
            AND MAX(sv.visit_time_) BETWEEN #{visitStartTime} AND #{visitEndTime}
        </if>) c
    </select>
    <resultMap id="MusicMemberDto" type="com.ym.mec.biz.dal.entity.MusicMemberDto">
        <result property="organName" column="organ_name_"/>
        <result property="musicGroupName" column="music_group_name_"/>
        <result property="musicGroupId" column="music_group_id_"/>
        <result property="userId" column="user_id_"/>
        <result property="username" column="username_"/>
        <result property="memberDay" column="member_day_"/>
        <result property="visitTime" column="visit_time_"/>
        <result property="phone" column="phone_"/>
    </resultMap>
    <select id="queryMusicMemberList" resultMap="MusicMemberDto">
        SELECT sr.user_id_,su.username_,su.phone_,o.name_ organ_name_,mg.name_ music_group_name_,
        DATEDIFF(MAX(cto.end_time_),NOW()) + 1 member_day_,sr.music_group_id_,MAX(sv.visit_time_) visit_time_ FROM student_registration sr
        LEFT JOIN music_group mg ON mg.id_ = sr.music_group_id_
        LEFT JOIN cloud_teacher_order cto ON cto.student_id_ = sr.user_id_ AND cto.status_ IN (1,2)
        LEFT JOIN sys_user su ON su.id_ = sr.user_id_
        LEFT JOIN organization o ON o.id_ = mg.organ_id_
        LEFT JOIN student_visit sv ON sv.student_id_ = sr.user_id_ AND sv.purpose_ = '会员续费'
        <include refid="queryMusicMemberListSql"/>
        GROUP BY sr.user_id_ HAVING (MAX(cto.id_) IS NULL OR MIN(cto.status_) != 1)
        <if test="hasMember != null">
            <if test="hasMember == 0">
                AND (MAX(cto.id_) IS NULL OR DATEDIFF(MAX(cto.end_time_),NOW()) &lt; 0)
            </if>
            <if test="hasMember == 1">
                AND DATEDIFF(MAX(cto.end_time_),NOW()) &lt;= #{memberEndAutoQuitMusic} AND DATEDIFF(MAX(cto.end_time_),NOW()) >= 0
            </if>
        </if>
        <if test="visitStartTime != null">
            AND MAX(sv.visit_time_) BETWEEN #{visitStartTime} AND #{visitEndTime}
        </if>
        <include refid="global.limit"/>
    </select>
    <sql id="queryMusicMemberListSql">
        <where>
            sr.music_group_status_ != 'QUIT' AND mg.course_view_type_ = 1
            AND mg.status_ = 'PROGRESS'
            <if test="search != null and search != ''">
                AND (mg.name_ LIKE CONCAT('%',#{search},'%') OR mg.id_ LIKE CONCAT('%',#{search},'%') OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.id_ = #{search})
            </if>
            <if test="organId != null and organId != ''">
                AND FIND_IN_SET(mg.organ_id_,#{organId})
            </if>
            <if test="educationUserId != null">
                AND mg.educational_teacher_id_ = #{educationUserId}
            </if>
        </where>
    </sql>
</mapper>
