<?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="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="last_payment_date_" property="lastPaymentDate"/>
        <result column="next_payment_date_" property="nextPaymentDate"/>
        <result column="music_group_status_" property="musicGroupStatus"/>
    </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" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <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" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <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="name_" property="name"/>
        <result column="current_grade_" property="grade"/>
        <result column="current_class_" property="clazz"/>
        <result column="actual_subject_id_" property="subject.id"/>
    </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
        (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(#{parentsPhone},#{id},#{userId},#{name},#{musicGroupId},#{currentGrade},#{currentClass},#{subjectId},#{isAllowAdjust,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{kitPurchaseMethod,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{remark},now(),now(),#{parentsName},#{parentsCompany},#{paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{subjectId},#{musicGroupStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler})
    </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="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_ = #{updateTime},
            </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>
        </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>
            <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="username_" property="studentName"/>
        <result column="actual_subject_name_" property="actualSubjectName"/>
        <result column="payment_status_" property="paymentStatus"/>
        <result column="gender_" property="gender" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>
    <select id="queryStudentDetailPage" resultMap="studentApplyDetail">
        SELECT sr.name_ 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_,sr.parents_phone_,sr.payment_status_
        FROM student_registration sr
        LEFT JOIN sys_user su ON sr.subject_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`,subject_id_ `key` FROM student_registration
        WHERE music_group_id_ = #{musicGroupId} AND payment_status_ = 1 GROUP BY subject_id_
    </select>

    <resultMap id="queryFeeDetailMap" type="com.ym.mec.biz.dal.dto.StudentFeeDto">
        <result column="deposit_fee_" property="depositFee"/>
        <result column="course_fee_" property="courseFee"/>
        <result column="musical_fee_" property="musicalFee"/>
        <result column="total_amount_" property="totalAmount"/>
    </resultMap>
    <select id="queryFeeDetail" resultMap="queryFeeDetailMap">

    </select>

    <!-- 根据乐团和声部获取未分班的学生 -->
    <select id="getNoClassStuBySubjectId" resultMap="StudentRegistration">
        SELECT sr.* FROM student_registration sr
        WHERE 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 cg.music_group_id_ = #{musicGroupId})
        AND sr.music_group_id_ = #{musicGroupId}
        <if test="actualSubjectId != null">
            AND FIND_IN_SET(sr.actual_subject_id_,#{actualSubjectId})
        </if>
    </select>

    <!-- 根据乐团和声部获取未分班人数 -->
    <select id="getNoClassStuCountBySubjectId" resultType="map">
        SELECT COUNT(DISTINCT sr.user_id_) `value`,sr.actual_subject_id_ `key` FROM student_registration sr
        WHERE 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 cg.music_group_id_ = #{musicGroupId})
        AND sr.music_group_id_ = #{musicGroupId} GROUP BY sr.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>
    
    <select id="queryStudentInfo" resultMap="StudentInfo">
        SELECT u.*,sr.name_,sr.current_class_,sr.current_grade_,sr.actual_subject_id_ FROM sys_user u left join student_registration  sr on u.id_ = sr.user_id_ where u.id_ = #{userId} order by sr.create_time_ desc limit 0,1
    </select>
    
    <select id="queryByUserIdAndMusicGroupId" resultMap="StudentRegistration" parameterType="map">
    	select * from student_registration where music_group_id_ = #{musicGroupId} and user_id_ = #{userId}
    </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" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <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" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <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}
    </select>

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