<?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="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_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"/>
        <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="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" />
    </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="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="name_" property="name"/>
        <result column="parents_name_" property="parentsName"/>
        <result column="current_grade_" property="grade"/>
        <result column="current_class_" property="clazz"/>
        <result column="actual_subject_id_" property="subject.id"/>
        <result column="subject_name_" property="subject.name"/>
    </resultMap>

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

    <!-- 全查询 -->
    <select id="findAll" resultMap="StudentRegistration">
        SELECT *
        FROM student_registration
        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="id!=null">id_,</if>
            <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="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="createTime!=null">create_time_,</if>
            <if test="updateTime!=null">update_time_,</if>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id!=null">#{id},</if>
            <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="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="createTime!=null">#{createTime},</if>
            <if test="updateTime!=null">NOW(),</if>
        </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="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="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>
        </set>
        WHERE id_ = #{id}
    </update>

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

    <sql id="queryPageSql">
        <where>
            <if test="isAllowAdjust != null">
                AND is_allow_adjust_ = #{isAllowAdjust,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="subjectId != null">
                AND subject_id_ = #{subjectId}
            </if>
        </where>
    </sql>

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

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


    <sql id="queryStudentDetailPageSql">
        <where>
                sr.music_group_status_ != 'QUIT'
            <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="name != null">
                AND (sr.name_ LIKE CONCAT('%',#{name},'%') OR sr.parents_phone_ LIKE CONCAT('%',#{name},'%'))
            </if>
        </where>
    </sql>

    <resultMap type="com.ym.mec.biz.dal.dto.StudentApplyDetailDto" id="studentApplyDetail">
        <result column="current_grade_" property="currentGrade"/>
        <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="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"/>
    </resultMap>
    <select id="queryStudentDetailPage" resultMap="studentApplyDetail">
        SELECT sr.id_, sr.user_id_,su.username_ username_,sr.parents_name_,sr.current_class_,sr.current_grade_,
        su.gender_,sr.is_allow_adjust_, s.name_ subject_name_,ss.name_
        actual_subject_name_,su.phone_ parents_phone_,sr.payment_status_,sr.remark_,sr.actual_subject_id_
        FROM student_registration sr
        LEFT JOIN sys_user su ON sr.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_
        <include refid="queryStudentDetailPageSql"/>
        <include refid="global.limit"/>
    </select>
    <select id="queryStudentDetailCount" resultType="java.lang.Integer">
        SELECT COUNT(sr.id_) FROM student_registration sr
        <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}
        GROUP BY spo.id_,spod.type_
        ORDER BY spo.id_ DESC
    </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 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_
        where u.id_ = #{userId}
        order by sr.create_time_ desc limit 1
    </select>

    <select id="queryByUserIdAndMusicGroupId" resultMap="StudentRegistration">
        SELECT sr.*,su.id_card_no_ 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} LIMIT 1
    </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"/>
    </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_
        WHERE sr.music_group_id_ = #{musicGroupId}
        <if test="actualSubjectId != null">
            AND 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">
        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_)
        VALUES
        <foreach collection="studentRegistrationList" 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})
        </foreach>
    </insert>

    <insert id="insertBasic" parameterType="com.ym.mec.biz.dal.entity.StudentRegistration">
        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_)
        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})
    </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>

    <!-- 批量开启缴费 -->
    <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)
   		<if test="organId != null">
   			AND FIND_IN_SET(mg.organ_id_,#{organId})
   		</if>
    </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="queryUserByPhone" resultMap="StudentRegistration">
        SELECT su.id_,su.real_name_ parents_name_,su.phone_,sr.current_grade_,
        sr.current_class_,su.username_ name_,su.birthdate_,su.gender_
        FROM sys_user su
        LEFT JOIN student_registration sr ON sr.user_id_ = su.id_
        WHERE su.phone_ = #{mobile}
        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="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 real_name_ = #{realName},id_card_no_=#{idCardNo} WHERE id_=#{id}
    </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.update_time_ = NOW()
        WHERE sr.user_id_ = #{userId}
    </update>

    <select id="findStudentByMusicGroupIdAndUserId" resultMap="StudentRegistration">
        SELECT s.name_ school_name_,s2.name_ subject_name_,mgsp.kit_group_purchase_type_ kit_group_purchase_type_,
        mgsp.deposit_fee_, co.name_ remark_ 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 music_group_subject_plan mgsp ON mgsp.subject_id_ = sr.actual_subject_id_ AND mgsp.music_group_id_ = sr.music_group_id_
        LEFT JOIN cooperation_organ co ON mg.cooperation_organ_id_ = co.id_
        WHERE sr.music_group_id_ = #{musicGroupId} AND sr.user_id_ = #{userId} AND sr.music_group_status_ != 'QUIT' 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>
</mapper>
