<?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.StudentRegistrationDao">
    <resultMap type="com.ym.mec.biz.dal.entity.StudentRegistration" id="StudentRegistration">
        <result column="id_" property="id"/>
        <result column="tenant_id_" property="tenantId"/>
        <result column="user_id_" property="userId"/>
        <result column="name_" property="name"/>
        <result column="id_card_no_" property="idCardNo"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="current_grade_" property="currentGrade"/>
        <result column="current_grade_date_" property="currentGradeDate"/>
        <result column="current_grade_" property="currentGrade"/>
        <result column="current_grade_num_" property="currentGradeNum"/>
        <result column="current_class_" property="currentClass"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="actual_subject_id_" property="actualSubjectId"/>
        <result column="is_allow_adjust_" property="isAllowAdjust"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="kit_purchase_method_" property="kitPurchaseMethod"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="remark_" property="remark"/>
        <result column="class_group_id_" property="classGroupId"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="parents_name_" property="parentsName"/>
        <result column="parents_phone_" property="parentsPhone"/>
        <result column="parents_company_" property="parentsCompany"/>
        <result column="payment_status_" property="paymentStatus"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="music_group_status_" property="musicGroupStatus"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="class_group_name_" property="classGroupName"/>
        <result column="temporary_course_fee_" property="temporaryCourseFee"/>
        <result column="birthdate_" property="birthdate"/>
        <result column="gender_" property="gender"/>
        <result column="organ_name_" property="organName"/>
        <result column="organ_id_" property="organId"/>
        <result column="school_name_" property="schoolName"/>
        <result column="subject_name_" property="subjectName"/>
        <result column="kit_group_purchase_type_" property="kitType"/>
        <result column="username_" property="name"/>
        <result column="deposit_fee_" property="depositFee"/>
        <result column="surplus_course_fee_" property="surplusCourseFee"/>
        <result column="paying_status_" property="payingStatus"/>
        <result column="has_cloud_teacher_" property="hasCloudTeacher"/>
        <result column="none_need_cloud_teacher_" property="noneNeedCloudTeacher"/>
        <result column="membership_end_time_" property="membershipEndTime"/>
        <result column="music_group_payment_calender_id_" property="musicGroupPaymentCalenderId"/>
        <result column="membership_end_time_" property="membershipEndTime"/>
    </resultMap>

    <resultMap type="com.ym.mec.biz.dal.dto.StudentMusicGroupDto" id="StudentMusicGroupDto"
               extends="StudentRegistration">
        <result column="music_group_quit_status_" property="musicGroupQuit.status"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="group_status_" property="musicGroup.status"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="music_group_name_" property="musicGroup.name"/>
    </resultMap>

    <resultMap type="com.ym.mec.biz.dal.dto.StudentInfo" id="StudentInfo">
        <result column="id_" property="id"/>
        <result column="username_" property="username"/>
        <result column="password_" property="password"/>
        <result column="salt_" property="salt"/>
        <result column="phone_" property="phone"/>
        <result column="avatar_" property="avatar"/>
        <result column="organ_id_" property="organId"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="tenant_id_" property="tenantId"/>
        <result column="lock_flag_" property="lockFlag"/>
        <result column="del_flag_" property="delFlag" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="wx_openid_" property="wxOpenid"/>
        <result column="qq_openid_" property="qqOpenid"/>
        <result column="user_type_" property="userType"/>
        <result column="gender_" property="gender"/>
        <result column="nation_" property="nation"/>
        <result column="birthdate_" property="birthdate"/>
        <result column="email_" property="email"/>
        <result column="im_token_" property="imToken"/>
        <result column="id_card_no_" property="idCardNo"/>
        <result column="real_name_" property="realName"/>
        <result column="wechat_id_" property="wechatId"/>
        <result column="certificate_type_" property="certificateType"/>
        <result column="name_" property="name"/>
        <result column="parents_name_" property="parentsName"/>
        <result column="current_grade_" property="grade"/>
        <result column="current_grade_date_" property="gradeDate"/>
        <result column="current_class_" property="clazz"/>
        <result column="actual_subject_id_" property="subject.id"/>
        <result column="subject_name_" property="subject.name"/>
    </resultMap>

    <resultMap id="UserGroupDto" type="com.ym.mec.biz.dal.dto.UserGroupDto">
        <result column="user_id_" property="userId"/>
        <result column="group_id_" property="groupId"/>
        <result column="group_name_" property="groupName"/>
    </resultMap>

    <resultMap id="Mapper" type="com.ym.mec.biz.dal.dto.Mapper">
        <result column="key_" property="key"/>
        <result column="value_" property="value"/>
        <result column="subject_id_" property="courseScheduleId"/>
    </resultMap>

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

    <select id="lock" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration
        WHERE id_ = #{id} FOR
        UPDATE
    </select>

    <select id="lockWithUserAndMusic" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration
        WHERE user_id_ = #{userId}
        AND music_group_id_ = #{musicGroupId}
        AND music_group_status_ != 'QUIT' FOR
        UPDATE
    </select>

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


    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentRegistration" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO student_registration
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">
                user_id_,
            </if>
            <if test="name != null">
                name_,
            </if>
            <if test="musicGroupId != null">
                music_group_id_,
            </if>
            <if test="currentGrade != null">
                current_grade_,
            </if>
            <if test="currentGradeNum != null">
                current_grade_num_,
            </if>
            <if test="currentGradeDate != null">
                current_grade_date_,
            </if>
            <if test="currentClass != null">
                current_class_,
            </if>
            <if test="subjectId != null">
                subject_id_,
            </if>
            <if test="actualSubjectId != null">
                actual_subject_id_,
            </if>
            <if test="isAllowAdjust != null">
                is_allow_adjust_,
            </if>
            <if test="kitPurchaseMethod != null">
                kit_purchase_method_,
            </if>
            <if test="remark != null">
                remark_,
            </if>
            <if test="parentsName != null">
                parents_name_,
            </if>
            <if test="parentsPhone != null">
                parents_phone_,
            </if>
            <if test="parentsCompany != null">
                parents_company_,
            </if>
            <if test="paymentStatus != null">
                payment_status_,
            </if>
            <if test="classGroupId != null">
                class_group_id_,
            </if>
            <if test="musicGroupStatus != null">
                music_group_status_,
            </if>
            <if test="temporaryCourseFee != null">
                temporary_course_fee_,
            </if>
            <if test="surplusCourseFee != null">
                surplus_course_fee_,
            </if>
            <if test="createTime != null">
                create_time_,
            </if>
            <if test="updateTime != null">
                update_time_,
            </if>
            <if test="musicGroupPaymentCalenderId != null">
                music_group_payment_calender_id_,
            </if>
            tenant_id_
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">
                #{userId},
            </if>
            <if test="name != null">
                #{name},
            </if>
            <if test="musicGroupId != null">
                #{musicGroupId},
            </if>
            <if test="currentGrade != null">
                #{currentGrade},
            </if>
            <if test="currentGradeNum != null">
                #{currentGradeNum},
            </if>
            <if test="currentGradeDate != null">
                #{currentGradeDate},
            </if>
            <if test="currentClass != null">
                #{currentClass},
            </if>
            <if test="subjectId != null">
                #{subjectId},
            </if>
            <if test="actualSubjectId != null">
                #{actualSubjectId},
            </if>
            <if test="isAllowAdjust != null">
                #{isAllowAdjust,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="kitPurchaseMethod != null">
                #{kitPurchaseMethod,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="remark != null">
                #{remark},
            </if>
            <if test="parentsName != null">
                #{parentsName},
            </if>
            <if test="parentsPhone != null">
                #{parentsPhone},
            </if>
            <if test="parentsCompany != null">
                #{parentsCompany},
            </if>
            <if test="paymentStatus != null">
                #{paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="classGroupId != null">
                #{classGroupId},
            </if>
            <if test="musicGroupStatus != null">
                #{musicGroupStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="temporaryCourseFee != null">
                #{temporaryCourseFee},
            </if>
            <if test="surplusCourseFee != null">
                #{surplusCourseFee},
            </if>
            <if test="createTime != null">
                NOW(),
            </if>
            <if test="updateTime != null">
                NOW(),
            </if>
            <if test="musicGroupPaymentCalenderId != null">
                #{musicGroupPaymentCalenderId},
            </if>
            #{tenantId}
        </trim>
    </insert>

    <!-- 根据主键更新一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentRegistration">
        UPDATE student_registration
        <set>
            <if test="parentsPhone != null">
                parents_phone_ = #{parentsPhone},
            </if>
            <if test="subjectId != null">
                subject_id_ = #{subjectId},
            </if>
            <if test="currentGrade != null">
                current_grade_ = #{currentGrade},
            </if>
            <if test="currentGradeNum != null">
                current_grade_num_ = #{currentGradeNum},
            </if>
            <if test="currentGradeDate != null">
                current_grade_date_ = #{currentGradeDate},
            </if>
            <if test="currentClass != null">
                current_class_ = #{currentClass},
            </if>
            <if test="isAllowAdjust != null">
                is_allow_adjust_ = #{isAllowAdjust,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="kitPurchaseMethod != null">
                kit_purchase_method_ = #{kitPurchaseMethod,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="musicGroupStatus != null">
                music_group_status_ = #{musicGroupStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="name != null">
                name_ = #{name},
            </if>
            <if test="remark != null">
                remark_ = #{remark},
            </if>
            <if test="paymentStatus != null">
                payment_status_ = #{paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="parentsName != null">
                parents_name_ = #{parentsName},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="createTime != null">
                create_time_ = #{createTime},
            </if>
            <if test="parentsCompany != null">
                parents_company_ = #{parentsCompany},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="actualSubjectId != null">
                actual_subject_id_ = #{actualSubjectId},
            </if>
            <if test="classGroupId != null">
                class_group_id_ = #{classGroupId},
            </if>
            <if test="temporaryCourseFee != null">
                temporary_course_fee_ =#{temporaryCourseFee},
            </if>
            <if test="surplusCourseFee != null">
                surplus_course_fee_ =#{surplusCourseFee},
            </if>
            <if test="payingStatus != null">
                paying_status_ =#{payingStatus},
            </if>
            <if test="hasCloudTeacher != null">
                has_cloud_teacher_ =#{hasCloudTeacher},
            </if>
            <if test="noneNeedCloudTeacher != null">
                none_need_cloud_teacher_ =#{noneNeedCloudTeacher},
            </if>
            <if test="membershipEndTime != null">
                membership_end_time_ =#{membershipEndTime},
            </if>
                music_group_payment_calender_id_ =#{musicGroupPaymentCalenderId}
        </set>
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

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

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

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


    <sql id="queryStudentDetailPageSql">
        <where>
            sr.music_group_status_ != 'QUIT' and sr.tenant_id_ = #{tenantId}
            <if test="musicGroupId != null">
                AND sr.music_group_id_ = #{musicGroupId}
            </if>
            <if test="subjectId != null">
                AND sr.subject_id_ = #{subjectId}
            </if>
            <if test="isAllowAdjust != null">
                AND sr.is_allow_adjust_ = #{isAllowAdjust}
            </if>
            <if test="actualSubjectId != null">
                AND sr.actual_subject_id_ = #{actualSubjectId}
            </if>
            <if test="paymentStatus != null">
                AND sr.payment_status_ = #{paymentStatus}
            </if>
            <if test="studentStatus != null">
                AND sr.music_group_status_ = #{studentStatus}
            </if>
            <if test="visited != null and visited == true">
                AND sv.num >0
            </if>
            <if test="visited != null and visited == false">
                AND sv.num IS NULL
            </if>
            <if test="name != null">
                AND (su.username_ LIKE CONCAT('%',#{name},'%') OR sr.parents_phone_ LIKE CONCAT('%',#{name},'%'))
            </if>
            <if test="currentGrade != null">
                AND st.current_grade_num_ = #{currentGrade}
            </if>
            <if test="createYear != null">
                AND DATE_FORMAT(sr.create_time_,'%Y') = #{createYear}
            </if>
            <if test="hasCloudTeacher != null">
                AND sr.has_cloud_teacher_ = #{hasCloudTeacher}
            </if>
            <if test="payingStatus != null">
                AND sr.paying_status_ = #{payingStatus}
            </if>
            <if test="noneNeedCloudTeacher != null">
                AND sr.none_need_cloud_teacher_ = #{noneNeedCloudTeacher}
            </if>
        </where>
    </sql>

    <resultMap type="com.ym.mec.biz.dal.dto.StudentApplyDetailDto" id="studentApplyDetail">
        <result column="current_grade_" property="currentGrade"/>
        <result column="current_grade_date_" property="currentGradeDate"/>
        <result column="current_class_" property="currentClass"/>
        <result column="is_allow_adjust_" property="isAllowAdjust"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="parents_name_" property="parentsName"/>
        <result column="parents_phone_" property="parentsPhone"/>
        <result column="subject_name_" property="subjectName"/>
        <result column="actual_subject_id_" property="actualSubjectId"/>
        <result column="username_" property="studentName"/>
        <result column="actual_subject_name_" property="actualSubjectName"/>
        <result column="payment_status_" property="paymentStatus"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="music_group_status_" property="studentStatus"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="gender_" property="gender" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="user_id_" property="studentId"/>
        <result column="id_" property="id"/>
        <result column="remark_" property="remark"/>
        <result column="create_time_" property="createTime"/>
        <result column="visit_num_" property="visitNum"/>
        <result column="paying_status_" property="payingStatus"/>
        <result column="has_cloud_teacher_" property="hasCloudTeacher"/>
        <result column="none_need_cloud_teacher_" property="noneNeedCloudTeacher"/>
    </resultMap>
    <select id="queryStudentDetailPage" resultMap="studentApplyDetail">
        SELECT sr.id_,
        sr.create_time_,
        sr.user_id_,
        su.username_ username_,
        sr.parents_name_,
        st.current_class_,
        st.current_grade_num_ current_grade_,
        sr.current_grade_date_,
        su.gender_,
        sr.is_allow_adjust_,
        s.name_ subject_name_,
        ss.name_
        actual_subject_name_,
        su.phone_ parents_phone_,
        sr.payment_status_,
        sr.music_group_status_,
        sr.remark_,
        sr.actual_subject_id_,
        IFNULL(sv.num, 0) visit_num_,
        CASE WHEN st.member_rank_setting_id_ = 1 THEN 1 ELSE 0 END has_cloud_teacher_,
        sr.paying_status_,
        sr.none_need_cloud_teacher_
        FROM student_registration sr
        LEFT JOIN sys_user su ON sr.user_id_ = su.id_
        LEFT JOIN student st ON st.user_id_ = su.id_
        LEFT JOIN `subject` s ON sr.subject_id_ = s.id_
        LEFT JOIN `subject` ss ON sr.actual_subject_id_ = ss.id_
        LEFT JOIN (
        SELECT v.student_id_, COUNT(*) num
        FROM student_visit v
        WHERE v.music_group_id_ = #{musicGroupId}
        GROUP BY v.student_id_
        ) sv ON sv.student_id_ = sr.user_id_
        <include refid="queryStudentDetailPageSql"/>
        <include refid="global.limit"/>
    </select>
    <select id="queryStudentDetailCount" resultType="java.lang.Integer">
        SELECT COUNT(sr.id_)
        FROM student_registration sr
        LEFT JOIN sys_user su ON sr.user_id_ = su.id_
        LEFT JOIN student st ON st.user_id_ = su.id_
        LEFT JOIN (
        SELECT v.student_id_, COUNT(*) num
        FROM student_visit v
        WHERE v.music_group_id_ = #{musicGroupId}
        GROUP BY v.student_id_
        ) sv on sv.student_id_ = sr.user_id_
        <include refid="queryStudentDetailPageSql"/>
    </select>

    <select id="countPayNum" resultType="map">
        SELECT COUNT(DISTINCT user_id_) `value`, actual_subject_id_ `key`
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND payment_status_ = 2
        AND music_group_status_ != 'QUIT'
        GROUP BY actual_subject_id_
    </select>

    <resultMap id="queryFeeDetailMap" type="com.ym.mec.biz.dal.dto.StudentFeeDetailDto">
        <result column="actual_amount_" property="totalAmount"/>
        <collection property="goods" ofType="com.ym.mec.biz.dal.dto.StudentFeeDto">
            <result property="goodsName" column="goods_name_"/>
            <result property="goodsType" column="type_"/>
            <result property="musicalFee" column="price_"/>
        </collection>
    </resultMap>
    <select id="queryFeeDetail" resultMap="queryFeeDetailMap">
        SELECT spo.actual_amount_,
        SUM(spod.price_) price_,
        GROUP_CONCAT(g.name_) goods_name_,
        IF(MAX(g.type_) IS NULL, 'COURSE', MAX(g.type_)) type_
        FROM student_payment_order spo
        LEFT JOIN student_payment_order_detail spod ON spod.payment_order_id_ = spo.id_
        LEFT JOIN goods g ON FIND_IN_SET(g.id_, spod.goods_id_list_)
        WHERE spo.type_ = 'APPLY'
        AND spo.group_type_ = 'MUSIC'
        AND spo.user_id_ = #{studentId}
        AND spo.music_group_id_ = #{musicGroupId}
        AND spo.status_ = 'SUCCESS'
        GROUP BY spo.id_, spod.type_
        ORDER BY spo.id_ DESC
        LIMIT 1
    </select>

    <!-- 根据乐团和声部获取未分班的学生 -->
    <select id="getNoClassStuBySubjectId" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND payment_status_ = 2
        AND (class_group_id_ = 0 OR class_group_id_ IS NULL)
        AND music_group_status_ != 'QUIT'
        <if test="actualSubjectId != null">
            AND FIND_IN_SET(actual_subject_id_, #{actualSubjectId})
        </if>
    </select>

    <resultMap type="com.ym.mec.biz.dal.entity.MusicGroupSubjectPlan" id="getNoClassStuCountByMusicGroupId">
        <result column="actual_subject_id_" property="subjectId"/>
        <result column="nums" property="noClassStudentNum"/>
    </resultMap>
    <!-- 根据乐团和声部获取未分班人数 -->
    <select id="getNoClassStuCountByMusicGroupId" resultMap="getNoClassStuCountByMusicGroupId">
        SELECT actual_subject_id_, COUNT(*) nums
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND payment_status_ = 2
        AND (class_group_id_ = 0 OR class_group_id_ IS NULL)
        AND music_group_status_ != 'QUIT'
        GROUP BY actual_subject_id_
    </select>

    <update id="updateByUserIdAndMusicGroupId" parameterType="com.ym.mec.biz.dal.entity.StudentRegistration">
        UPDATE student_registration
        SET class_group_id_ = #{studentRegistration.classGroupId}
        WHERE user_id_ =
        #{studentRegistration.userId}
        AND music_group_id_ = #{studentRegistration.musicGroupId}
    </update>

    <update id="batchUpdateSubject">
        UPDATE student_registration
        SET actual_subject_id_ = #{subId},
        update_time_ = now()
        WHERE user_id_ = #{userId}
        AND payment_status_ != 2
        AND music_group_id_ = #{musicGroupId}
    </update>

    <select id="queryStudentInfo" resultMap="StudentInfo">
        SELECT sut.name_ real_name_,
        sut.card_no_ id_card_no_,
        u.*,
        sr.name_,
        sr.current_class_,
        sr.current_grade_,
        sr.actual_subject_id_,
        sr.parents_name_,
        s.name_ subject_name_
        FROM sys_user u
        LEFT JOIN student_registration sr ON u.id_ = sr.user_id_
        LEFT JOIN subject s ON sr.actual_subject_id_ = s.id_
        LEFT JOIN sys_user_tsign sut ON sut.user_id_ = u.id_
        WHERE u.id_ = #{userId}
        ORDER BY sr.create_time_ DESC
        limit 1
    </select>

    <select id="queryByUserIdAndMusicGroupId" resultMap="StudentRegistration">
        SELECT sr.*, su.id_card_no_, su.organ_id_
        FROM student_registration sr
        LEFT JOIN sys_user su ON su.id_ = sr.user_id_
        WHERE sr.music_group_id_ = #{musicGroupId}
        AND sr.user_id_ = #{userId}
        AND sr.music_group_status_ != 'QUIT'
        LIMIT 1
    </select>

    <!-- 根据乐团编号和手机号查询注册乐团注册信息-->
    <select id="getByPhoneAndMusicGroupId" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND parents_phone_ = #{parentsPhone}
    </select>

    <resultMap type="com.ym.mec.auth.api.entity.SysUser" id="FindSysUser">
        <result column="id_" property="id"/>
        <result column="username_" property="username"/>
        <result column="password_" property="password"/>
        <result column="salt_" property="salt"/>
        <result column="phone_" property="phone"/>
        <result column="avatar_" property="avatar"/>
        <result column="organ_id_" property="organId"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="lock_flag_" property="lockFlag"/>
        <result column="del_flag_" property="delFlag" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="wx_openid_" property="wxOpenid"/>
        <result column="qq_openid_" property="qqOpenid"/>
        <result column="user_type_" property="userType"/>
        <result column="gender_" property="gender"/>
        <result column="nation_" property="nation"/>
        <result column="birthdate_" property="birthdate"/>
        <result column="email_" property="email"/>
        <result column="im_token_" property="imToken"/>
        <result column="id_card_no_" property="idCardNo"/>
        <result column="wechat_id_" property="wechatId"/>
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>
    <select id="getSysUserByPhone" resultMap="FindSysUser">
        SELECT *
        FROM sys_user
        WHERE phone_ = #{phone}
        LIMIT 1
    </select>

    <!-- 获取班级下的学生 -->
    <select id="findClassGroupStu" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND music_group_status_ != 'QUIT'
        <if test="classGroupId != null">
            AND class_group_id_ = #{classGroupId}
        </if>
    </select>

    <!-- 根据班级和userId查询学生信息-->
    <select id="findStudentByClassGroupIdAndUserId" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration
        WHERE user_id_ = #{userId}
        AND class_group_id_ = #{classGroupId}
        AND music_group_status_ != 'QUIT'
    </select>

    <!-- 获取乐团未分班的学生 -->
    <select id="findMusicGroupNoClassGroupStudent" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration WHERE music_group_id_ = #{musicGroupId}
        <if test="actualSubjectId != null">
            AND actual_subject_id_ = #{actualSubjectId}
        </if>
        AND payment_status_ = 2
        AND (class_group_id_ = 0 OR class_group_id_ IS NULL)
        AND music_group_status_ != 'QUIT'
    </select>


    <!-- 获取乐团声部下的学生 -->
    <select id="findMusicGroupStudent" resultMap="StudentRegistration">
        SELECT sr.*, cg.name_ class_group_name_, su.gender_ gender_, su.username_
        FROM student_registration sr
        LEFT JOIN class_group cg ON cg.id_ = sr.class_group_id_
        LEFT JOIN sys_user su on sr.user_id_ = su.id_
        LEFT JOIN sys_user_tsign sut ON sut.user_id_ = su.id_
        WHERE sr.music_group_id_ = #{musicGroupId}
        <if test="actualSubjectId != null">
            AND FIND_IN_SET(sr.actual_subject_id_, #{actualSubjectId})
        </if>
        AND payment_status_ = 2
        AND sr.music_group_status_ != 'QUIT'
    </select>

    <!-- 根据注册id list查询报名学生的信息 -->
    <select id="findStudentListByIdList" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration WHERE id_ IN
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        AND music_group_status_ != 'QUIT'
    </select>

    <insert id="batchInsert" parameterType="list" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO student_registration
        (parents_phone_, id_, user_id_, name_, music_group_id_, current_grade_, current_class_, subject_id_,
        is_allow_adjust_, kit_purchase_method_, remark_, create_time_, update_time_,
        parents_name_, parents_company_, payment_status_, actual_subject_id_, music_group_status_, is_merge_,music_group_payment_calender_id_,tenant_id_)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.parentsPhone}, #{item.id}, #{item.userId}, #{item.name}, #{item.musicGroupId}, #{item.currentGrade},
            #{item.currentClass}, #{item.subjectId},
            #{item.isAllowAdjust,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            #{item.kitPurchaseMethod,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            #{item.remark}, now(), now(), #{item.parentsName}, #{item.parentsCompany},
            #{item.paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{item.subjectId},
            #{item.musicGroupStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{item.isMerge},#{item.musicGroupPaymentCalenderId},#{item.tenantId})
        </foreach>
    </insert>

    <insert id="insertBasic" parameterType="com.ym.mec.biz.dal.entity.StudentRegistration" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO student_registration
        (parents_phone_, user_id_, name_, music_group_id_, current_grade_, current_class_, subject_id_,
        create_time_, update_time_, parents_name_, payment_status_, actual_subject_id_, music_group_status_,
        temporary_course_fee_, class_group_id_,current_grade_num_,current_grade_date_,music_group_payment_calender_id_,tenant_id_)
        VALUES (#{parentsPhone}, #{userId}, #{name}, #{musicGroupId}, #{currentGrade}, #{currentClass}, #{subjectId},
        now(), now(), #{parentsName}, #{paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{subjectId},
        #{musicGroupStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{temporaryCourseFee},
        #{classGroupId},#{currentGradeNum},#{currentGradeDate},#{musicGroupPaymentCalenderId},#{tenantId})
    </insert>

    <!-- 查询乐团userIdList的学生 -->
    <select id="findStudentListByUserIdList" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration WHERE music_group_id_ = #{musicGroupId}
        AND music_group_status_ != 'QUIT'
        AND user_id_ IN
        <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
            #{userId}
        </foreach>
    </select>
    <select id="countApplyNum" resultType="java.util.Map">
        SELECT sr.actual_subject_id_ 'key', COUNT(DISTINCT sr.user_id_) 'value'
        FROM student_registration sr
        WHERE sr.music_group_id_ = #{musicGroupId}
        AND sr.music_group_status_ != 'QUIT'
        GROUP BY actual_subject_id_
    </select>
    <select id="findParentId" resultType="map">
        SELECT MAX(sr.user_id_) `key`, sr.parents_phone_ `value`
        FROM student_registration sr
        WHERE FIND_IN_SET(sr.parents_phone_, #{parentPhones})
        AND sr.music_group_status_ != 'QUIT'
        GROUP BY sr.parents_phone_
    </select>
    <select id="queryStudentByMusicGroupId" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration sr
        WHERE sr.music_group_id_ = #{musicGroupId}
        AND sr.payment_status_ = 2
        AND sr.music_group_status_ != 'QUIT'
    </select>
    <select id="updateOriginalFlag">
        UPDATE student_registration sr SET original_flag_ = 1
        WHERE sr.music_group_id_ = #{musicGroupId}
        AND sr.payment_status_ = 2
        AND sr.music_group_status_ != 'QUIT'
    </select>

    <!-- 批量开启缴费 -->
    <update id="batchOpenPay">
        UPDATE student_registration
        SET payment_status_ = #{paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        update_time_ = now()
        WHERE FIND_IN_SET(id_, #{ids})
        AND payment_status_ = '0'
    </update>

    <!-- 乐团批量开启缴费 -->
    <update id="musicGroupOpenPay">
        UPDATE student_registration
        SET payment_status_ = #{paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        update_time_ = now()
        WHERE music_group_id_ = #{musicGroupId}
        AND payment_status_ = '0'
    </update>

    <select id="queryStudentNum" resultType="map">
        SELECT count(distinct sr.user_id_) total_num_,
        sum(case
        when sr.create_time_ >= timestamp(date_add(curdate(), interval - day(curdate()) + 1 day)) then 1
        else 0 end) new_num_,
        sum(case
        when sr.music_group_status_ = 'QUIT' and
        sr.update_time_ >= timestamp(date_add(curdate(), interval - day(curdate()) + 1 day)) then 1
        else 0 end) quit_num_
        FROM student_registration sr
        left join music_group mg on sr.music_group_id_ = mg.id_
        where mg.organ_id_ not in (36, 38)
            AND FIND_IN_SET(mg.organ_id_, #{organId})
    </select>
    <select id="findMapByMusicGroupId" resultType="java.util.Map">
        SELECT sr.user_id_ 'key', MIN(sr.parents_phone_) 'value'
        FROM student_registration sr
        WHERE sr.music_group_id_ = #{musicGroupId}
        AND sr.music_group_status_ != 'QUIT'
        <if test="paymentStatus != null">
            AND sr.payment_status_ = #{paymentStatus}
        </if>
        GROUP BY sr.user_id_
    </select>
    <select id="findIdMapByMusicGroupId" resultType="java.util.Map">
        SELECT sr.user_id_ 'key', MIN(sr.user_id_) 'value'
        FROM student_registration sr
        WHERE sr.music_group_id_ = #{musicGroupId}
        AND sr.music_group_status_ != 'QUIT'
        <if test="paymentStatus != null">
            AND sr.payment_status_ = #{paymentStatus}
        </if>
        GROUP BY sr.user_id_
    </select>

    <!-- 删除学生的班级 -->
    <update id="deleteClassGroup">
        UPDATE student_registration
        SET class_group_id_ = 0,
        update_time_ = now()
        WHERE class_group_id_ = #{classGroupId}
    </update>

    <!-- 获取乐团学生人数 -->
    <select id="findMusicGroupStudentNum" resultType="integer">
        SELECT Count(*)
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND payment_status_ = '2'
        AND music_group_status_ != 'QUIT'
    </select>
    <select id="countNormalNum" resultType="java.util.Map">
        SELECT sr.music_group_id_ 'key', COUNT(DISTINCT sr.user_id_) 'value'
        FROM student_registration sr
        WHERE sr.payment_status_ = 2
        AND sr.music_group_status_ != 'QUIT'
        AND FIND_IN_SET(sr.music_group_id_, #{musicGroupIds})
        GROUP BY sr.music_group_id_
    </select>
    <select id="queryNormalUserList" resultType="integer">
        SELECT sr.user_id_
        FROM student_registration sr
        WHERE sr.music_group_status_ = 'NORMAL'
        AND sr.music_group_id_ = #{musicGroupIds}
    </select>
    <select id="queryUserByPhone" resultMap="StudentRegistration">
        SELECT su.id_,
        CASE WHEN sut.name_ IS NULL THEN su.real_name_ ELSE sut.name_ END parents_name_,
        su.phone_,
        sr.current_grade_,
        sr.current_grade_num_,
        sr.current_class_,
        su.username_ name_,
        su.birthdate_,
        su.gender_
        FROM sys_user su
        LEFT JOIN sys_user_tsign sut ON sut.user_id_ = su.id_
        LEFT JOIN student_registration sr ON sr.user_id_ = su.id_
        WHERE su.phone_ = #{mobile}
        ORDER BY sr.id_ DESC
        LIMIT 1
    </select>
    <select id="queryPurchaseTypeMap" resultType="java.util.Map">
        SELECT sr.user_id_ 'key', mgsp.kit_group_purchase_type_ 'value'
        FROM student_registration sr
        LEFT JOIN music_group_subject_plan mgsp
        ON sr.actual_subject_id_ = mgsp.subject_id_ AND sr.music_group_id_ = mgsp.music_group_id_
        WHERE sr.music_group_id_ = #{musicGroupId}
    </select>
    <resultMap id="StudentRegisterPerDtoMap" type="com.ym.mec.biz.dal.dto.StudentRegisterPerDto">
        <result property="organName" column="organ_name_"/>
        <result property="studentNum" column="student_num_"/>
        <result property="per" column="per_"/>
        <result property="perNum" column="per_num_"/>
    </resultMap>
    <select id="queryStudentPer1" resultMap="StudentRegisterPerDtoMap">
        SELECT o.name_ organ_name_, md.total_num_ student_num_, md.activate_num_ per_num_, md.percent_ per_
        FROM index_base_month_data md
        LEFT JOIN organization o ON o.id_ = md.organ_id_
        WHERE data_type_ = 'ACTIVATION_RATE' and md.tenant_id_ = #{tenantId}
        AND month_ = #{month}
        ORDER BY md.percent_ DESC, o.id_
    </select>
    <select id="queryStudentPer" resultMap="StudentRegisterPerDtoMap">
        SELECT a.name_ organ_name_, a.num_ student_num_, b.num_ per_num_, ifnull(b.num_, 0) * 100 / a.num_ per_
        FROM (SELECT o.id_, o.name_, COUNT(DISTINCT su.id_) num_
        FROM sys_user su
        LEFT JOIN organization o ON su.organ_id_ = o.id_
        WHERE su.user_type_ LIKE '%STUDENT%'
        AND su.id_ IN
        <foreach collection="userIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
        GROUP BY su.organ_id_) a
        LEFT JOIN
        (SELECT su.organ_id_ id_, COUNT(DISTINCT su.id_) num_
        FROM sys_user su
        WHERE su.user_type_ LIKE '%STUDENT%'
        AND su.password_ IS NOT NULL
        AND su.id_ IN
        <foreach collection="userIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
        GROUP BY su.organ_id_) b
        ON a.id_ = b.id_
        ORDER BY per_ DESC
    </select>

    <update id="updateUser">
        UPDATE sys_user
        <set>
            <if test="name != null">
                username_ = #{name},
            </if>
            <if test="realName != null">
                real_name_ = #{realName},
            </if>
            <if test="idCardNo != null">
                id_card_no_ = #{idCardNo},
            </if>
            <if test="certificateType != null">
                certificate_type_ = #{certificateType},
            </if>
            <if test="gender != null">
                gender_ = #{gender},
            </if>
            update_time_=NOW()
            WHERE id_ = #{id}
        </set>
    </update>
    <update id="updateCurrentClass" parameterType="com.ym.mec.biz.dal.entity.StudentRegistration">
        UPDATE student_registration sr
        SET sr.current_grade_ = #{currentGrade},
        sr.current_class_ = #{currentClass},
        sr.current_grade_date_= NOW(),
        sr.update_time_ = NOW()
        WHERE sr.user_id_ = #{userId}
    </update>
    <update id="updatePaymentStatusByClaenderId">
        UPDATE student_registration sr
        LEFT JOIN music_group_payment_calender mgpc ON mgpc.music_group_id_ = sr.music_group_id_
        LEFT JOIN music_group_payment_calender_detail mgpcd ON mgpc.id_ = mgpcd.music_group_payment_calender_id_
        SET sr.payment_status_ = 2,
        sr.music_group_status_ = 'NORMAL',
        sr.update_time_ = NOW()
        WHERE mgpcd.user_id_ = sr.user_id_
        AND mgpcd.expect_amount_ = 0
        AND mgpc.id_ = #{calenderId}
    </update>

    <select id="findStudentByMusicGroupIdAndUserId" resultMap="StudentRegistration">
        SELECT s.name_ school_name_, s2.name_ subject_name_, co.name_ remark_, su.real_name_ name_
        FROM student_registration sr
        LEFT JOIN music_group mg ON mg.id_ = sr.music_group_id_
        LEFT JOIN school s ON mg.school_id_ = s.id_
        LEFT JOIN subject s2 ON sr.actual_subject_id_ = s2.id_
        LEFT JOIN cooperation_organ co ON mg.cooperation_organ_id_ = co.id_
        LEFT JOIN sys_user su on mg.educational_teacher_id_ = su.id_
        WHERE sr.music_group_id_ = #{musicGroupId}
        AND sr.user_id_ = #{userId}
        AND sr.payment_status_ = 2
        ORDER BY sr.id_ DESC
        LIMIT 1
    </select>
    <select id="queryStudentMusicGroup" 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_
        WHERE sr.user_id_ = #{studentId}
        AND sr.music_group_status_ != 'QUIT'
        AND mg.status_ = 'PROGRESS'
    </select>
    <select id="getSubjectIds" resultType="java.lang.String">
        SELECT GROUP_CONCAT(sr.subject_id_)
        FROM student_registration sr
        WHERE sr.music_group_status_ != 'QUIT'
        AND sr.user_id_ = #{userId}
        GROUP BY sr.user_id_
    </select>
    <select id="findMusicGroupAllStudentSubjects" resultMap="com.ym.mec.biz.dal.dao.SubjectDao.Subject">
        SELECT DISTINCT s.*
        FROM student_registration sr
        LEFT JOIN subject s ON sr.actual_subject_id_ = s.id_
        WHERE sr.music_group_id_ = #{musicGroupId}
        ORDER BY s.id_
    </select>
    <resultMap id="getStudentDetail" type="com.ym.mec.biz.dal.dto.StudentMusicDetailDto">
        <result property="gender" column="gender_"/>
        <result property="username" column="username_"/>
        <result property="userId" column="id_"/>
        <result property="phone" column="phone_"/>
        <result property="avatar" column="avatar_"/>
        <result property="imToken" column="im_token_"/>
        <collection property="musicGroups" ofType="com.ym.mec.biz.dal.entity.MusicGroup">
            <result property="name" column="music_group_name_"/>
            <result column="music_group_id_" property="id"/>
            <result column="subject_name_" property="subjectName"/>
        </collection>
    </resultMap>
    <select id="getStudentDetail" resultMap="getStudentDetail">
        SELECT su.username_,
        su.gender_,
        su.phone_,
        su.avatar_,
        su.id_,
        su.im_token_,
        mg.name_ music_group_name_,
        mg.id_ music_group_id_,
        sj.name_ subject_name_
        FROM student s
        LEFT JOIN music_group_student_fee_ mgsf ON mgsf.user_id_ = s.user_id_
        LEFT JOIN music_group mg ON mg.id_ = mgsf.music_group_id_
        LEFT JOIN `subject` sj ON sj.id_ = mgsf.subject_id_
        LEFT JOIN sys_user su ON su.id_ = s.user_id_
        WHERE s.user_id_ = #{studentId}
    </select>

    <select id="getMusicGroupStu" resultMap="StudentRegistration">
        SELECT sr.user_id_, s.username_ name_, sr.music_group_status_,st.membership_end_time_
        FROM student_registration sr
        LEFT JOIN sys_user s on sr.user_id_ = s.id_
        LEFT JOIN student st on st.user_id_ = s.id_
        WHERE music_group_id_ = #{musicGroupId}
    </select>

    <select id="getMusicGroupStu4Teacher" resultMap="StudentRegistration">
        SELECT s.id_ user_id_, s.username_ name_
        FROM course_schedule cs
        LEFT JOIN course_schedule_student_payment cssp ON cssp.course_schedule_id_ = cs.id_
        LEFT JOIN sys_user s ON s.id_ = cssp.user_id_
        WHERE cs.music_group_id_ = #{musicGroupId}
        AND cs.actual_teacher_id_ = #{teacherId}
        AND cs.group_type_ = 'MUSIC'
        GROUP BY s.id_
    </select>
    <select id="getStudentRegister" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND user_id_ = #{studentId}
        ORDER BY id_ DESC
        LIMIT 1
    </select>

    <!-- 获取分部乐团在读人数 -->
    <select id="getOrganNormalStudent" resultMap="com.ym.mec.biz.dal.dao.StudentPaymentOrderDao.luckStatis">
        SELECT mg.organ_id_ organId, COUNT(DISTINCT sr.user_id_) estimatedNums
        FROM student_registration sr
                     LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        WHERE sr.music_group_status_ = 'NORMAL' and sr.tenant_id_ = #{tenantId}
        GROUP BY mg.organ_id_
    </select>

    <select id="getUserGroups" resultMap="UserGroupDto">
        SELECT DISTINCT sr.user_id_, mg.id_ group_id_, mg.name_ group_name_
        FROM student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        WHERE sr.payment_status_ = 2
        AND sr.music_group_status_ = 'NORMAL'
        AND sr.user_id_ IN
        <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
            #{userId}
        </foreach>
    </select>
    <select id="getNoPaymentStudent" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND payment_status_ IN (0, 1) AND music_group_status_ != 'QUIT'
    </select>

    <!-- 获取乐团声部学生数量 -->
    <select id="getMusicGroupSubjectStudentNum" resultType="int">
        SELECT COUNT(*)
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND INTE_ARRAY(actual_subject_id_, #{subjectIds})
        AND music_group_status_ = 'NORMAL'
    </select>

    <select id="queryStudentMusicGroupInfo" resultMap="StudentMusicGroupDto">
        select sr.*,
        s.name_ subject_name_,
        mg.status_ group_status_,
        mg.name_ music_group_name_,
        mgq.status_ music_group_quit_status_
        from student_registration sr
        left join subject s on s.id_ = sr.actual_subject_id_
        left join music_group mg on sr.music_group_id_ = mg.id_
        left join (select mgq.*
        from music_group_quit mgq
        where mgq.id_ in
        (select max(id_) from music_group_quit group by music_group_id_, user_id_)) mgq
        ON mgq.music_group_id_ = sr.music_group_id_
        AND mgq.user_id_ = sr.user_id_ AND mgq.join_date_ = sr.create_time_
        where sr.user_id_ = #{userId}
        <if test="musicGroupId != null and musicGroupId != ''">
            AND sr.music_group_id_ = #{musicGroupId}
        </if>
        order by sr.id_
    </select>

    <select id="querySurplusCourseFeeByClassGroupId" resultType="java.util.Map">
        SELECT sr.user_id_ 'key', sr.surplus_course_fee_ 'value'
        FROM student_registration sr
        LEFT JOIN class_group cg ON cg.music_group_id_ = sr.music_group_id_
        WHERE cg.id_ = #{classGroupId}
        AND FIND_IN_SET(sr.user_id_, #{studentIds})
        AND sr.music_group_status_ != 'QUIT'
    </select>

    <select id="queryByUserIdsAndMusicGroupId" resultMap="StudentRegistration">
        SELECT sr.*, su.id_card_no_, su.organ_id_
        FROM student_registration sr
        LEFT JOIN sys_user su ON su.id_ = sr.user_id_
        WHERE sr.music_group_id_ = #{musicGroupId}
        AND FIND_IN_SET(sr.user_id_, #{studentIds})
        AND sr.music_group_status_ != 'QUIT'
    </select>
    <select id="getStudentSubjectName" resultType="java.lang.String">
        SELECT s.name_
        FROM student_registration sr
        LEFT JOIN subject s ON sr.actual_subject_id_ = s.id_
        where sr.music_group_id_ = #{musicGroupId}
        and sr.user_id_ = #{studentId}
    </select>
    <select id="getStudentSubjectNameByStuIds" resultType="java.util.Map">
        SELECT sr.user_id_ 'key', s.name_ 'value'
        FROM student_registration sr
        LEFT JOIN subject s ON sr.actual_subject_id_ = s.id_
        where sr.music_group_id_ = #{musicGroupId}
        and sr.user_id_ IN
        <foreach collection="studentIds" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="getStudentNormalMusicGroups" resultMap="com.ym.mec.biz.dal.dao.MusicGroupDao.MusicGroup">
        SELECT DISTINCT mg.*
        FROM student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        WHERE mg.status_ = 'PROGRESS'
        AND sr.music_group_status_ = 'NORMAL'
        AND sr.user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
        ORDER BY mg.create_time_ DESC
    </select>
    <select id="getUserLastNormalMusicGroup" resultMap="com.ym.mec.biz.dal.dao.MusicGroupDao.MusicGroup">
        SELECT DISTINCT mg.*
        FROM student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        WHERE mg.del_flag_ = 0
        AND mg.status_ = 'PROGRESS'
        AND sr.music_group_status_ = 'NORMAL'
        AND sr.user_id_ = #{userId}
        ORDER BY sr.create_time_ DESC
        LIMIT 1
    </select>
    <select id="getStudentNormalRegistration" resultMap="StudentRegistration">
        SELECT sr.*
        FROM student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        WHERE mg.status_ IN ('PROGRESS','PAUSE')
        AND mg.del_flag_ = 0
        AND sr.music_group_status_ = 'NORMAL'
        AND sr.user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
    </select>
    <!-- 批量删除信息 -->
    <delete id="batchDelete">
        DELETE
        FROM student_registration WHERE id_ IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

    <!-- 根据乐团编号和手机号查询注册乐团注册信息-->
    <select id="getByUserIdAndMusicGroupId" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND user_id_ = #{userId}
    </select>

    <resultMap id="NoClassMusicStudentDto" type="com.ym.mec.biz.dal.dto.NoClassMusicStudentDto">
        <result property="organId" column="organ_id_"/>
        <result property="organName" column="organ_name_"/>
        <result property="groupId" column="music_group_id_"/>
        <result property="groupName" column="music_group_name_"/>
        <result property="studentId" column="student_id_"/>
        <result property="studentName" column="student_name_"/>
        <result property="subjectId" column="subject_id_"/>
        <result property="subjectName" column="subject_name_"/>
    </resultMap>

    <sql id="queryNoClassMusicStudentsCondition">
        <where>
            sr.music_group_status_ = 'NORMAL' and sr.tenant_id_ = #{tenantId}
            AND mg.status_ = 'PROGRESS'
            AND cgsm.id_ IS NULL
            <if test="organIds != null and organIds != ''">
                AND FIND_IN_SET(mg.organ_id_,#{organIds})
            </if>
            <if test="educationUserId != null">
                AND mg.educational_teacher_id_ = #{educationUserId}
            </if>
            <if test="musicGroupSearch != null and musicGroupSearch != ''">
                AND (sr.music_group_id_ = #{musicGroupSearch} OR mg.name_ LIKE CONCAT('%', #{musicGroupSearch}, '%'))
            </if>
            <if test="studentSearch != null and studentSearch != ''">
                AND (sr.user_id_ = #{studentSearch} OR stu.username_ LIKE CONCAT('%', #{studentSearch}, '%'))
            </if>
            <if test="organId != null">
                AND mg.organ_id_ = #{organId}
            </if>
        </where>
    </sql>

    <select id="queryNoClassMusicStudents" resultMap="NoClassMusicStudentDto">
        SELECT mg.organ_id_,
        organ.name_ organ_name_,
        sr.music_group_id_,
        mg.name_ music_group_name_,
        sr.user_id_ student_id_,
        stu.username_ student_name_,
        sr.actual_subject_id_ subject_id_,
        sub.name_ subject_name_
        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_
        AND cgsm.status_ = 'NORMAL'
        LEFT JOIN sys_user stu ON stu.id_ = sr.user_id_
        LEFT JOIN organization organ ON mg.organ_id_ = organ.id_
        LEFT JOIN `subject` sub ON sub.id_ = sr.actual_subject_id_
        <include refid="queryNoClassMusicStudentsCondition"/>
        ORDER BY sr.id_
        <include refid="global.limit">
        </include>
    </select>

    <select id="countNoClassMusicStudents" resultType="int">
        SELECT COUNT(sr.user_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_
        AND cgsm.status_ = 'NORMAL'
        LEFT JOIN sys_user stu ON stu.id_ = sr.user_id_
        <include refid="queryNoClassMusicStudentsCondition"/>
    </select>

    <select id="getStudentClassInfo" resultType="com.ym.mec.biz.dal.dto.StudentClassInfoDto">
        SELECT sr.user_id_ studentId,
        sr.music_group_id_ musicGroupId,
        sr.actual_subject_id_ subjectId,
        cgsm.class_group_id_ classGroupId,
        cgtm.user_id_ mainTeacherId,
        mg.cooperation_organ_id_ cooperationOrganId
        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_
        LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
        LEFT JOIN class_group_teacher_mapper cgtm
        ON cgsm.class_group_id_ = cgtm.class_group_id_ AND cgtm.teacher_role_ = 'BISHOP'
        WHERE sr.music_group_status_ = 'NORMAL'
        AND mg.status_ = 'PROGRESS'
        AND cgsm.status_ = 'NORMAL'
        AND cg.type_ = 'NORMAL'
        AND cg.del_flag_ = 0
        AND cg.lock_flag_ = 0
            AND sr.user_id_ IN
            <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
                #{studentId}
            </foreach>
    </select>

    <select id="getPaymentStudentNum" resultType="int">
        SELECT COUNT(*)
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND payment_status_ = 2
        AND music_group_status_ = 'NORMAL'
    </select>
    <select id="findApplyStudent" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND music_group_status_ = 'APPLY'
        AND user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
        LIMIT 1
    </select>
    <select id="findByMusicGroupIds" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration
        WHERE music_group_id_ IN
        <foreach collection="groupIds" item="groupId" open="(" close=")" separator=",">
            #{groupId}
        </foreach>
        <if test="musicGroupStatus != null">
            AND music_group_status_ = #{musicGroupStatus, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        </if>
    </select>

    <update id="quitAllStudent">
        UPDATE student_registration
        SET music_group_status_ = 'QUIT',
        remark_=#{remark}
        WHERE music_group_id_ = #{musicGroupId}
        AND music_group_status_ IN ('APPLY', 'NORMAL')
    </update>
    <update id="cleanMusicMember">
        UPDATE student_registration SET membership_end_time_ = NULL
        WHERE user_id_ = #{userId}
        <if test="musicGroupId != null and musicGroupId != ''">
            AND music_group_id_ = #{musicGroupId}
        </if>
    </update>

    <select id="countPayingNum" resultType="map">
        SELECT COUNT(DISTINCT user_id_) `value`, actual_subject_id_ `key`
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND paying_status_ = #{payingStatus}
        AND music_group_status_ != 'QUIT'
        GROUP BY actual_subject_id_
    </select>

    <select id="getRegisterOrPreList" resultType="com.ym.mec.biz.dal.dto.RegisterDto">
        SELECT u.user_id_ userId,
        su.username_ studentName,
        <if test="orderByPerRegister == null">
            IFNULL(spr.create_time_, '2099-12-31') perRegisterTime,
        </if>
        <if test="orderByPerRegister != null and orderByPerRegister == 'ASC'">
            IFNULL(spr.create_time_, '2099-12-31') perRegisterTime,
        </if>
        <if test="orderByPerRegister != null and orderByPerRegister == 'DESC'">
            IFNULL(spr.create_time_, '1990-12-31') perRegisterTime,
        </if>
        <if test="orderByPayTime == null">
            IFNULL(o.payTime, '2099-12-31') payTime,
        </if>
        <if test="orderByPayTime != null and orderByPayTime == 'ASC'">
            IFNULL(o.payTime, '2099-12-31') payTime,
        </if>
        <if test="orderByPayTime != null and orderByPayTime == 'DESC'">
            IFNULL(o.payTime, '1990-12-31') payTime,
        </if>
        IFNULL(s2.name_, s1.name_) actualSubjectName
        FROM (SELECT user_id_
        FROM student_pre_registration
        WHERE music_group_id_ = #{musicGroupId}
        UNION
        SELECT user_id_
        FROM student_registration
        where music_group_id_ = #{musicGroupId}
        ) u
        LEFT JOIN sys_user su ON su.id_ = u.user_id_
        LEFT JOIN student_pre_registration spr
        ON spr.user_id_ = u.user_id_ AND spr.music_group_id_ = #{musicGroupId}
        LEFT JOIN student_registration sr
        ON sr.user_id_ = u.user_id_ AND sr.music_group_id_ = #{musicGroupId}
        LEFT JOIN subject s1 ON spr.subject_first_ = s1.id_
        LEFT JOIN subject s2 ON sr.actual_subject_id_ = s2.id_
        LEFT JOIN (
        SELECT spo.user_id_, min(spo.pay_time_) payTime
        FROM student_payment_order spo
        WHERE spo.music_group_id_ = #{musicGroupId}
        AND spo.status_ = 'SUCCESS'
        AND type_ = 'APPLY'
        GROUP BY spo.user_id_
        ) o ON o.user_id_ = u.user_id_
        <where>
            <if test="userIds != null">
                AND u.user_id_ IN
                <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
                    #{userId}
                </foreach>
            </if>
            <if test="subjectId">
                AND (sr.actual_subject_id_ = #{subjectId} OR spr.subject_first_ = #{subjectId})
            </if>
        </where>
        <if test="orderByPerRegister != null and orderByPayTime == null">
            ORDER BY perRegisterTime ${orderByPerRegister},u.user_id_ ASC
        </if>
        <if test="orderByPerRegister != null and orderByPayTime != null">
            ORDER BY perRegisterTime ${orderByPerRegister}, payTime ${orderByPayTime}, u.user_id_ ASC
        </if>
        <if test="orderByPerRegister == null and orderByPayTime != null">
            ORDER BY payTime ${orderByPayTime}, u.user_id_ ASC
        </if>
        <if test="orderByPerRegister == null and orderByPayTime == null">
            ORDER BY perRegisterTime ASC, u.user_id_ ASC
        </if>
        <include refid="global.limit"/>
    </select>

    <select id="getRegisterOrPreCount" resultType="int">
        SELECT COUNT(*)
        FROM (SELECT user_id_
        FROM student_pre_registration
        WHERE music_group_id_ = #{musicGroupId}
        UNION
        SELECT user_id_
        FROM student_registration
        where music_group_id_ = #{musicGroupId}
        ) u
        LEFT JOIN sys_user su ON su.id_ = u.user_id_
        LEFT JOIN student_pre_registration spr
        ON spr.user_id_ = u.user_id_ AND spr.music_group_id_ = #{musicGroupId}
        LEFT JOIN student_registration sr
        ON sr.user_id_ = u.user_id_ AND sr.music_group_id_ = #{musicGroupId}
        LEFT JOIN subject s1 ON spr.subject_first_ = s1.id_
        LEFT JOIN subject s2 ON sr.actual_subject_id_ = s2.id_
        <where>
            <if test="userIds != null">
                AND u.user_id_ IN
                <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
                    #{userId}
                </foreach>
            </if>
            <if test="subjectId">
                AND (sr.actual_subject_id_ = #{subjectId} OR spr.subject_first_ = #{subjectId})
            </if>
        </where>
    </select>

    <select id="getRegisterOrPreMinTime" resultType="com.ym.mec.biz.dal.dto.RegisterDto">
        SELECT MIN(o.payTime) minPayTime,MIN(spr.create_time_) minPerRegisterTime
        FROM (SELECT user_id_
        FROM student_pre_registration
        WHERE music_group_id_ = #{musicGroupId}
        UNION
        SELECT user_id_
        FROM student_registration
        where music_group_id_ = #{musicGroupId}
        ) u
        LEFT JOIN student_pre_registration spr
        ON spr.user_id_ = u.user_id_ AND spr.music_group_id_ = #{musicGroupId}
        LEFT JOIN (
        SELECT spo.user_id_, min(spo.pay_time_) payTime
        FROM student_payment_order spo
        WHERE spo.music_group_id_ = #{musicGroupId}
        AND spo.status_ = 'SUCCESS'
        AND spo.type_ = 'APPLY'
        GROUP BY spo.user_id_
        ) o ON o.user_id_ = u.user_id_
    </select>

    <select id="getRegisterNum" resultType="int">
        SELECT COUNT(*) FROM student_registration WHERE music_group_id_ = #{musicGroupId} AND music_group_status_ != 'QUIT'
    </select>

    <select id="getFirstPayNum" resultType="int">
        SELECT COUNT(*)
        FROM student_registration sr
        LEFT JOIN (
        SELECT spo.user_id_, MAX(spo.pay_time_) payTime
        FROM student_payment_order spo
        WHERE spo.music_group_id_ = #{musicGroupId}
        AND spo.status_ = 'SUCCESS'
        AND spo.type_ = 'APPLY'
        GROUP BY spo.user_id_
        ) o ON o.user_id_ = sr.user_id_
        WHERE sr.music_group_id_ = #{musicGroupId} AND sr.payment_status_=2
        <if test="startTime != null">
            AND o.payTime >= #{startTime}
        </if>
        <if test="endTime != null">
            <![CDATA[ AND o.payTime <= #{endTime}]]>
        </if>
        AND sr.music_group_status_ != 'QUIT'
    </select>
    <select id="getNotClassName" resultType="java.lang.String">
        SELECT GROUP_CONCAT(DISTINCT su.username_) FROM student_registration sr
        LEFT JOIN sys_user su ON su.id_ = sr.user_id_
        WHERE sr.music_group_id_ = #{musicGroupId} AND sr.music_group_status_ = 'NORMAL'
        AND sr.user_id_ NOT IN (SELECT DISTINCT cgsm.user_id_ FROM class_group_student_mapper cgsm
        LEFT JOIN class_group cg ON cg.id_ = cgsm.class_group_id_
        WHERE cgsm.music_group_id_ = #{musicGroupId} AND cgsm.group_type_ = 'MUSIC'
        AND cgsm.status_ != 'QUIT' AND cg.del_flag_ = 0 AND cg.type_ IN ('NORMAL','MIX'))
    </select>
    <select id="checkHasMemberGroup" resultType="java.lang.Boolean">
        SELECT COUNT(sr.id_) > 0 FROM student_registration sr
        LEFT JOIN music_group mg ON mg.id_ = sr.music_group_id_
        WHERE sr.music_group_id_ != #{musicGroupId} AND sr.music_group_status_ != 'QUIT'
          AND sr.user_id_ = #{userId} AND mg.course_view_type_ = 2
    </select>

    <!-- 获取报名相关数据 -->
    <select id="getRegisters" resultType="com.ym.mec.biz.dal.dto.MusicGroupRegAndMoneyDto">
        SELECT music_group_id_ musicGroupId, COUNT(*) regNum,SUM(IF(payment_status_ = '2',1,0)) payNum,
        SUM(IF(paying_status_='2',1,0)) checkNum,SUM(IF((payment_status_= '2' and has_cloud_teacher_=1),1,0)) buyCloudTeacherNum,
        SUM(IF((music_group_status_= 'NORMAL' and paying_status_ != 2),1,0)) studentNumOfNormal,
        SUM(IF((music_group_status_= 'NORMAL' and has_cloud_teacher_ = 0),1,0)) noCloudTeacherStudentNumOfNormal,
        SUM(IF((music_group_status_ != 'NORMAL' and payment_status_= '2'),1,0)) cloudTeacherStudentNumOfApply
        FROM student_registration
        WHERE music_group_id_ IN
        <foreach collection="musicGroupIds" item="musicGroupId" open="(" close=")" separator=",">
            #{musicGroupId}
        </foreach>
        AND music_group_status_ != 'QUIT'
        GROUP BY music_group_id_
    </select>

    <select id="countPayAndCheckNum" resultType="int">
        SELECT COUNT(*)
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
          AND paying_status_ = 2
          AND music_group_status_ = 'NORMAL'
    </select>
    <select id="queryMemberEndAutoQuitMusic" resultMap="StudentRegistration">
        SELECT sr.music_group_id_,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_ = 1 AND cto.music_group_id_ IS NOT NULL
        WHERE sr.music_group_status_ = 'NORMAL' AND mg.course_view_type_ = 2 AND mg.status_ = 'PROGRESS'
        <if test="educationUserId != null">
            AND mg.educational_teacher_id_ = #{educationUserId}
        </if>
            AND FIND_IN_SET(mg.organ_id_,#{organIds})
        <if test="memberEndAutoQuitMusic != null">
            AND cto.id_ IS NULL AND DATEDIFF(sr.membership_end_time_,NOW()) &lt;= #{memberEndAutoQuitMusic} AND sr.membership_end_time_ > NOW()
        </if>
        <if test="memberEndAutoQuitMusic == null">
            AND cto.id_ IS NULL AND (sr.membership_end_time_ IS NULL OR sr.membership_end_time_ &lt; NOW())
        </if>
        GROUP BY sr.music_group_id_, sr.user_id_
    </select>
    <select id="queryOrganMemberEndAutoQuitMusic" resultType="Map">
        SELECT mg.organ_id_ 'key',COUNT(DISTINCT sr.user_id_) 'value' 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_ = 1 AND cto.music_group_id_ IS NOT NULL
        WHERE sr.music_group_status_ = 'NORMAL' AND mg.course_view_type_ = 2 AND mg.status_ = 'PROGRESS'
        AND cto.id_ IS NULL AND (sr.membership_end_time_ IS NULL OR sr.membership_end_time_ &lt; NOW())
        <if test="educationUserId != null">
            AND mg.educational_teacher_id_ = #{educationUserId}
        </if>
            AND mg.organ_id_ IN
            <foreach collection="organIds" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        <if test="memberEndAutoQuitMusic != null">
            AND cto.id_ IS NULL AND DATEDIFF(sr.membership_end_time_,NOW()) &lt;= #{memberEndAutoQuitMusic} AND sr.membership_end_time_ > NOW()
        </if>
        <if test="memberEndAutoQuitMusic == null">
            AND cto.id_ IS NULL AND (sr.membership_end_time_ IS NULL OR sr.membership_end_time_ &lt; NOW())
        </if>
        GROUP BY mg.organ_id_
    </select>

    <select id="countStudentWithSubjectByMusicGroupId" resultMap="Mapper">
        select sr.actual_subject_id_ subject_id_, s.name_ key_,count(sr.id_) value_ from student_registration sr LEFT JOIN subject s on sr.actual_subject_id_ = s.id_
		where sr.music_group_status_ = 'NORMAL' and sr.music_group_id_ = #{musicGroupId}
		group by sr.actual_subject_id_
    </select>

    <select id="countStudentWithGradeByMusicGroupId" resultMap="Mapper">
        select s.current_grade_num_ key_,count(sr.id_) value_ from student_registration sr LEFT JOIN student s on sr.user_id_= s.user_id_
		where sr.music_group_status_ = 'NORMAL' and sr.music_group_id_ = #{musicGroupId}
		group by s.current_grade_num_
    </select>
    
    <select id="queryStudentRegisterInstrumentsDetailList" resultType="com.ym.mec.biz.dal.dto.StudentRegisterInstrumentsDetailDto" parameterType="map">
        SELECT sr.user_id_ userId,u.username_ name,u.phone_ phone,u.gender_ phone,sub.name_ subjectName,sr.actual_subject_id_ actualSubjectId,s.current_grade_num_ currentGradeNum,
        si.goods_brand_ goodsBrand,si.goods_name_ goodsName,si.specification_ specification,si.create_time_ goodsBuyTime,si.start_time_ insuranceStartTime,si.end_time_ insuranceEndTime
		from student_registration sr LEFT JOIN sys_user u on sr.user_id_ = u.id_ 
		LEFT JOIN student s on s.user_id_ = sr.user_id_ LEFT JOIN `subject` sub on sub.id_ = sr.actual_subject_id_
		LEFT JOIN (select * from student_instrument where id_ in ((select max(id_) from student_instrument group by student_id_))) si on si.student_id_ = sr.user_id_
		WHERE sr.music_group_status_ = 'NORMAL' and sr.tenant_id_ = #{tenantId}
		<if test="musicGroupId != null">
			AND sr.music_group_id_ = #{musicGroupId}
		</if>
		<if test="subjectId != null">
			AND sr.actual_subject_id_ = #{subjectId}
		</if>
		<if test="currentGradeNum != null">
			AND s.current_grade_num_ = #{currentGradeNum}
		</if>
		<if test="isBuyInstrumentsInsurance != null and isBuyInstrumentsInsurance == true">
			AND si.start_time_ is not null
		</if>
		<if test="isBuyInstrumentsInsurance != null and isBuyInstrumentsInsurance == false">
			AND si.start_time_ is null
		</if>
		<if test="search != null and search != ''">
			AND (u.username_ LIKE CONCAT('%',#{search},'%') OR u.phone_ LIKE CONCAT('%',#{search},'%') or u.id_ LIKE CONCAT('%',#{search},'%'))
		</if>
        ORDER BY sr.create_time_ desc
        <include refid="global.limit"/>
    </select>
    
    <select id="queryStudentRegisterInstrumentsDetailCount" resultType="int" parameterType="map">
        SELECT count(sr.id_)
		from student_registration sr LEFT JOIN sys_user u on sr.user_id_ = u.id_ 
		LEFT JOIN student s on s.user_id_ = sr.user_id_ LEFT JOIN `subject` sub on sub.id_ = sr.actual_subject_id_
		LEFT JOIN (select * from student_instrument where id_ in ((select max(id_) from student_instrument group by student_id_))) si on si.student_id_ = sr.user_id_
		WHERE sr.music_group_status_ = 'NORMAL' and sr.tenant_id_ = #{tenantId}
		<if test="musicGroupId != null">
			AND sr.music_group_id_ = #{musicGroupId}
		</if>
		<if test="subjectId != null">
			AND sr.actual_subject_id_ = #{subjectId}
		</if>
		<if test="currentGradeNum != null">
			AND s.current_grade_num_ = #{currentGradeNum}
		</if>
		<if test="isBuyInstrumentsInsurance != null and isBuyInstrumentsInsurance == true">
			AND si.start_time_ is not null
		</if>
		<if test="isBuyInstrumentsInsurance != null and isBuyInstrumentsInsurance == false">
			AND si.start_time_ is null
		</if>
		<if test="search != null and search != ''">
			AND (u.username_ LIKE CONCAT('%',#{search},'%') OR u.phone_ LIKE CONCAT('%',#{search},'%') or u.id_ LIKE CONCAT('%',#{search},'%'))
		</if>
        ORDER BY sr.create_time_ desc
        <include refid="global.limit"/>
    </select>

    <select id="queryStudentApplyDetail" resultType="com.ym.mec.biz.dal.dto.SubjectApplyDetailDto">
        SELECT music_group_id_ musicGroupId,actual_subject_id_ subjectId, COUNT(*) applyStudentNum,SUM(IF(payment_status_ = '2',1,0)) payNum,
        SUM(IF(paying_status_='2',1,0)) checkNum,SUM(IF((payment_status_= '2' and has_cloud_teacher_=1),1,0)) buyCloudTeacherNum,
        SUM(IF((music_group_status_= 'NORMAL' and paying_status_ != 2),1,0)) normalNum,
        SUM(IF((music_group_status_= 'NORMAL' and has_cloud_teacher_ = 0),1,0)) noCloudTeacherStudentNumOfNormal,
        SUM(IF((music_group_status_ != 'NORMAL' and payment_status_= '2'),1,0)) cloudTeacherStudentNumOfApply,
        SUM(IF((paying_status_ = '1'),1,0)) payingNum
        FROM student_registration
        WHERE music_group_id_ = #{musicGroupId}
        AND music_group_status_ != 'QUIT'
        GROUP BY music_group_id_,actual_subject_id_
    </select>
    <select id="queryAutoCreateStudentMap" resultType="java.util.Map">
        SELECT GROUP_CONCAT(sr.user_id_) 'value',sr.music_group_id_ 'key' FROM student_registration sr
        WHERE sr.membership_end_time_ IS NOT NULL AND DATEDIFF(sr.membership_end_time_,NOW()) + 1 = #{pushMemberRenew} and sr.tenant_id_ = #{tenantId}
        GROUP BY sr.music_group_id_
    </select>

    <select id="queryStudentMusicGroupNamesMap" 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.music_group_status_ != 'QUIT'
            AND sr.user_id_ IN
            <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
                #{studentId}
            </foreach>
        GROUP BY sr.user_id_
    </select>

    <select id="queryStudentMusicGroupEduMap" resultType="java.util.Map">
        SELECT sr.user_id_ 'key',GROUP_CONCAT(DISTINCT su.real_name_) 'value'
        FROM student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_=mg.id_
        LEFT JOIN sys_user su ON mg.educational_teacher_id_ = su.id_
        WHERE mg.status_='PROGRESS' AND sr.music_group_status_ != 'QUIT'  AND mg.educational_teacher_id_ IS NOT NULL
            AND sr.user_id_ IN
            <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
                #{studentId}
            </foreach>
        GROUP BY sr.user_id_
    </select>

    <select id="queryStudentSchoolNamesMap" resultType="java.util.Map">
        SELECT sr.user_id_ 'key',GROUP_CONCAT(sch.name_) 'value'
        FROM student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_=mg.id_
        LEFT JOIN school sch ON mg.school_id_=sch.id_
        WHERE mg.status_='PROGRESS'
            AND sr.user_id_ IN
            <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
                #{studentId}
            </foreach>
        GROUP BY sr.user_id_
    </select>
    <select id="findStudentIds" resultType="java.lang.Integer">
        SELECT DISTINCT sr.user_id_ FROM student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        WHERE sr.music_group_status_ = 'NORMAL' AND mg.status_ = 'PROGRESS'
          AND sr.user_id_ NOT IN (SELECT DISTINCT(cssp.`user_id_`)  FROM `course_schedule_student_payment` cssp
                                                 LEFT JOIN course_schedule cs on cssp.`course_schedule_id_` = cs.`id_`
                                                 LEFT JOIN `music_group` mg on mg.`id_` = cssp.`music_group_id_`
      WHERE cs.`group_type_` = 'MUSIC' AND cs.`status_` = 'OVER' AND mg.`status_` = 'PROGRESS' and mg.tenant_id_ = #{tenantId}
      GROUP BY cssp.`user_id_` HAVING COUNT(cs.`id_`) > 4) 
      <!-- AND sr.user_id_ NOT IN (SELECT user_id_ FROM student WHERE subject_id_list_ REGEXP '21|25|26|27|28|29') -->
    </select>
    <resultMap id="CloudTeacherActiveTargetJsonDto" type="com.ym.mec.biz.dal.dto.CloudTeacherActiveTargetJsonDto">
        <result property="targetNum" column="target_num_"/>
        <result property="targetAmount" column="target_amount_"/>
        <result property="organId" column="organ_id_"/>
    </resultMap>

    <resultMap id="MusicStudentInstrumentDto" type="com.ym.mec.biz.dal.dto.MusicStudentInstrumentDto">
        <result property="username" column="username_"/>
        <result property="userId" column="user_id_"/>
        <result property="avatar" column="avatar_"/>
        <result property="phone" column="phone_"/>
        <result property="hasInstrument" column="has_instrument_"/>
    </resultMap>
    <select id="queryMusicStudentInstrument" resultMap="MusicStudentInstrumentDto">
        SELECT sr.user_id_,su.username_,su.phone_,su.avatar_,CASE WHEN si.id_ IS NULL THEN 0 ELSE 1 END has_instrument_ FROM student_registration sr
        LEFT JOIN student_instrument si ON sr.user_id_ = si.student_id_ AND si.del_flag_ = 0 AND si.end_time_ > NOW()
        LEFT JOIN sys_user su ON su.id_ = sr.user_id_
        WHERE sr.music_group_status_ = 'NORMAL' AND sr.music_group_id_ = #{musicGroupId}
        <if test="search != null and search != ''">
            AND (sr.user_id_ = #{search} OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.phone_ LIKE CONCAT('%',#{search},'%'))
        </if>
        GROUP BY sr.user_id_
        <include refid="global.limit"/>
    </select>
    <select id="countMusicStudentInstrument" resultType="int">
        SELECT COUNT(DISTINCT sr.user_id_) FROM student_registration sr
        LEFT JOIN sys_user su ON su.id_ = sr.user_id_
        LEFT JOIN student_instrument si ON sr.user_id_ = si.student_id_ AND si.del_flag_ = 0 AND si.end_time_ > NOW()
        WHERE sr.music_group_status_ = 'NORMAL' AND sr.music_group_id_ = #{musicGroupId}
        <if test="search != null and search != ''">
            AND (sr.user_id_ = #{search} OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.phone_ LIKE CONCAT('%',#{search},'%'))
        </if>
    </select>
    <resultMap id="MusicGroup" type="com.ym.mec.biz.dal.entity.MusicGroup">
        <result property="payNum" column="pay_num_"/>
        <result property="hasInstrumentNum" column="has_instrument_num_"/>
        <result property="id" column="music_group_id_"/>
    </resultMap>
    <select id="countInstrument" resultMap="MusicGroup">
        SELECT sr.music_group_id_,COUNT(DISTINCT sr.user_id_) pay_num_,SUM(CASE WHEN si.id_ IS NULL THEN 0 ELSE 1 END) has_instrument_num_ FROM student_registration sr
        LEFT JOIN (SELECT * FROM student_instrument WHERE del_flag_ = 0 AND end_time_ > NOW()
        GROUP BY student_id_ ) si ON sr.user_id_ = si.student_id_ AND si.del_flag_ = 0 AND si.end_time_ > NOW()
        WHERE sr.music_group_status_ = 'NORMAL' AND sr.music_group_id_ IN
        <foreach collection="musicGroupIds" separator="," item="id" open="(" close=")">
            #{id}
        </foreach>
        GROUP BY sr.music_group_id_
    </select>
    <select id="querySubjectAmount" resultType="java.util.Map">
        SELECT sr.actual_subject_id_ 'key', SUM(CASE WHEN sc.id_ IS NULL THEN spo.expect_amount_ ELSE (spo.expect_amount_ + sc.goods_margin_) END) 'value'
        FROM student_payment_order spo
        LEFT JOIN student_registration sr ON sr.user_id_ = spo.user_id_
        LEFT JOIN (SELECT id_,music_group_id_,original_order_id_,CASE WHEN goods_margin_ &lt; 0 THEN goods_margin_ ELSE sell_amount_ END goods_margin_ FROM subject_change WHERE id_ IN (SELECT MAX(id_) id_ FROM subject_change WHERE status_ = 2 GROUP BY music_group_id_,student_id_ ORDER BY id_ DESC)) sc ON spo.music_group_id_ = sc.music_group_id_ AND spo.id_ = sc.original_order_id_
        WHERE spo.music_group_id_ = #{musicGroupId} AND sr.music_group_id_ = spo.music_group_id_ AND sr.music_group_status_ = 'NORMAL'
        AND spo.type_ = 'APPLY' AND spo.status_ = 'SUCCESS'
        GROUP BY sr.actual_subject_id_
    </select>
    <select id="getLastMusicGroupId" resultType="java.util.Map">
        SELECT sr.user_id_ 'key',sr.music_group_id_ 'value' FROM student_registration sr
        WHERE sr.id_ IN (SELECT MAX(sr.id_) FROM student_registration sr
        LEFT JOIN music_group mg ON mg.id_ = sr.music_group_id_
        WHERE sr.music_group_status_ = 'NORMAL' AND mg.status_ IN ('PROGRESS','PAUSE') AND sr.user_id_ IN
        <foreach collection="userIds" separator="," item="id" open="(" close=")">
            #{id}
        </foreach>
        GROUP BY sr.user_id_)
    </select>
    <select id="getLastMusicGroupName" resultType="java.util.Map">
        SELECT sr.user_id_ 'key',mg.name_ 'value' FROM student_registration sr
        LEFT JOIN music_group mg ON mg.id_ = sr.music_group_id_
        WHERE sr.id_ IN (SELECT MAX(sr.id_) FROM student_registration sr
        LEFT JOIN music_group mg ON mg.id_ = sr.music_group_id_
        WHERE sr.music_group_status_ = 'NORMAL' AND mg.status_ IN ('PROGRESS','PAUSE') AND sr.user_id_ IN
        <foreach collection="userIds" separator="," item="id" open="(" close=")">
            #{id}
        </foreach>
        GROUP BY sr.user_id_)
    </select>
    
    <select id="queryServiceStudentByMusicGroupStatus" resultMap="StudentRegistration">
    	select sr.* from student_registration sr 
    	left join music_group mg ON mg.id_ = sr.music_group_id_
    	left join student s on s.user_id_ = sr.user_id_
    	where s.service_tag_ = 1 AND (s.service_tag_update_time_ IS NULL OR s.service_tag_update_time_&lt;#{nextMonday})
    	and sr.music_group_status_ = 'NORMAL' AND mg.status_ = 'PROGRESS' and sr.tenant_id_ = #{tenantId}
    </select>
    
    <select id="queryEffectiveStudent" resultType="com.ym.mec.biz.dal.dto.StudentOrganDto">
    	SELECT sr.user_id_ userId, u.organ_id_ organId FROM `student_registration` sr LEFT JOIN `music_group` mg on sr.`music_group_id_` = mg.`id_`
		LEFT JOIN `sys_user` u on u.`id_` = sr.`user_id_`
		WHERE mg.`status_` = 'PROGRESS' AND sr.`music_group_status_` = 'NORMAL'
		AND u.organ_id_ IN
        <foreach collection="organIds" item="organId" open="(" close=")" separator=",">
            #{organId}
        </foreach>
        group by sr.user_id_ , u.organ_id_
    </select>
</mapper>
