<?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.StudentPreRegistrationDao">

    <resultMap type="com.ym.mec.biz.dal.entity.StudentPreRegistration" id="StudentPreRegistration">
        <result column="id_" property="id"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="user_id_" property="userId"/>
        <result column="phone_" property="phone"/>
        <result column="user_name_" property="userName"/>
        <result column="gender_" property="gender"/>
        <result column="current_grade_" property="currentGrade"/>
        <result column="current_grade_num_" property="currentGradeNum"/>
        <result column="current_class_" property="currentClass"/>
        <result column="subject_first_" property="subjectFirst"/>
        <result column="subject_second_" property="subjectSecond"/>
        <result column="is_allow_adjust_" property="isAllowAdjust"/>
        <result column="kit_purchase_method_" property="kitPurchaseMethod"/>
        <result column="cloud_teacher_method_" property="cloudTeacherMethod"/>
        <result column="create_time_" property="createTime"/>
        <result column="is_registered_" property="isRegistered"/>
    </resultMap>

    <resultMap type="com.ym.mec.biz.dal.dto.StudentPreRegistrationDto" id="StudentPreRegistrationDto"
               extends="StudentPreRegistration">
        <result column="subject_first_name_" property="subjectFirstName"/>
        <result column="subject_second_name_" property="subjectSecondName"/>
        <result column="is_registered_" property="isRegistered"/>
    </resultMap>

    <resultMap type="com.ym.mec.common.entity.Mapper" id="Mapper">
        <result column="key_" property="key"/>
        <result column="value_" property="value"/>
    </resultMap>

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

    <!-- 全查询 -->
    <select id="findAll" resultMap="StudentPreRegistration">
        SELECT spr.*
        FROM student_pre_registration spr
        <where>
            <if test="musicGroupId != null">
                and spr.music_group_id_ = #{musicGroupId}
            </if>
            <if test="isAllowAdjust != null">
                and spr.is_allow_adjust_ = #{isAllowAdjust}
            </if>
            <if test="subjectId != null">
                and (spr.subject_first_ = #{subjectId} or spr.subject_second_ = #{subjectId})
            </if>
            <if test="name != null">
                and (spr.phone_ like concat('%',#{name},'%') or spr.user_name_ like concat('%',#{name},'%') or
                spr.user_id_ like concat('%',#{name},'%'))
            </if>
            <if test="isAllowAdjust != null">
                and spr.is_allow_adjust_ = #{isAllowAdjust}
            </if>
            <if test="kitPurchaseMethod != null">
                and spr.kit_purchase_method_ = #{kitPurchaseMethod}
            </if>
            <if test="cloudTeacherMethod != null">
                and spr.cloud_teacher_method_ = #{cloudTeacherMethod}
            </if>
        </where>
        ORDER BY spr.id_
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentPreRegistration" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        <!--
        <selectKey resultClass="int" keyProperty="id" >
        SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
        </selectKey>
        -->
        INSERT INTO student_pre_registration
        (id_,music_group_id_,user_id_,phone_,user_name_,gender_,current_grade_,current_grade_num_,current_class_,subject_first_,subject_second_,is_allow_adjust_,kit_purchase_method_,cloud_teacher_method_,create_time_)
        VALUES(#{id},#{musicGroupId},#{userId},#{phone},#{userName},#{gender},#{currentGrade},#{currentGradeNum},#{currentClass},#{subjectFirst},#{subjectSecond},#{isAllowAdjust},#{kitPurchaseMethod},#{cloudTeacherMethod},#{createTime})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentPreRegistration">
        UPDATE student_pre_registration
        <set>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="userName != null">
                user_name_ = #{userName},
            </if>
            <if test="id != null">
                id_ = #{id},
            </if>
            <if test="currentGrade != null">
                current_grade_ = #{currentGrade},
            </if>
            <if test="currentGradeNum != null">
                current_grade_num_ = #{currentGradeNum},
            </if>
            <if test="gender != null">
                gender_ = #{gender},
            </if>
            <if test="subjectFirst != null">
                subject_first_ = #{subjectFirst},
            </if>
            <if test="phone != null">
                phone_ = #{phone},
            </if>
            <if test="subjectSecond != null">
                subject_second_ = #{subjectSecond},
            </if>
            <if test="currentClass != null">
                current_class_ = #{currentClass},
            </if>
            <if test="isAllowAdjust != null">
                is_allow_adjust_ = #{isAllowAdjust},
            </if>
            <if test="kitPurchaseMethod != null">
                kit_purchase_method_ = #{kitPurchaseMethod},
            </if>
            <if test="cloudTeacherMethod != null">
                cloud_teacher_method_ = #{cloudTeacherMethod},
            </if>
            <if test="createTime != null">
                create_time_ = #{createTime},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

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

    <!-- 分页查询 -->
    <select id="queryListForPage" resultMap="StudentPreRegistrationDto" parameterType="map">
        SELECT spr.*,case when sr.id_ is null then false else true end is_registered_,s1.name_
        subject_first_name_,s2.name_ subject_second_name_ FROM student_pre_registration spr
        left join subject s1 on s1.id_ = spr.subject_first_
        left join subject s2 on s2.id_ = spr.subject_second_
        left join student_registration sr on spr.music_group_id_ = sr.music_group_id_ and spr.user_id_ = sr.user_id_
        <where>
            <if test="musicGroupId != null">
                and spr.music_group_id_ = #{musicGroupId}
            </if>
            <if test="isAllowAdjust != null">
                and spr.is_allow_adjust_ = #{isAllowAdjust}
            </if>
            <if test="subjectId != null">
                and (spr.subject_first_ = #{subjectId} or spr.subject_second_ = #{subjectId})
            </if>
            <if test="name != null">
                and (spr.phone_ like concat('%',#{name},'%') or spr.user_name_ like concat('%',#{name},'%') or
                spr.user_id_ like concat('%',#{name},'%'))
            </if>
            <if test="isAllowAdjust != null">
                and spr.is_allow_adjust_ = #{isAllowAdjust}
            </if>
            <if test="kitPurchaseMethod != null">
                and spr.kit_purchase_method_ = #{kitPurchaseMethod}
            </if>
            <if test="cloudTeacherMethod != null">
                and spr.cloud_teacher_method_ = #{cloudTeacherMethod}
            </if>
        </where>
        ORDER BY spr.id_
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(spr.id_) FROM student_pre_registration spr
        <where>
            <if test="musicGroupId != null">
                and spr.music_group_id_ = #{musicGroupId}
            </if>
            <if test="isAllowAdjust != null">
                and spr.is_allow_adjust_ = #{isAllowAdjust}
            </if>
            <if test="subjectId != null">
                and (spr.subject_first_ = #{subjectId} or spr.subject_second_ = #{subjectId})
            </if>
            <if test="name != null">
                and (spr.phone_ like concat('%',#{name},'%') or spr.user_name_ like concat('%',#{name},'%') or
                spr.user_id_ like concat('%',#{name},'%'))
            </if>
            <if test="isAllowAdjust != null">
                and spr.is_allow_adjust_ = #{isAllowAdjust}
            </if>
            <if test="kitPurchaseMethod != null">
                and spr.kit_purchase_method_ = #{kitPurchaseMethod}
            </if>
            <if test="cloudTeacherMethod != null">
                and spr.cloud_teacher_method_ = #{cloudTeacherMethod}
            </if>
        </where>
    </select>

    <select id="queryFirstSubjectNum" resultMap="Mapper">
        SELECT ifnull(s.name_, '听从老师安排') key_, COUNT(spr.id_) value_
        FROM student_pre_registration spr
                 left join subject s on s.id_ = spr.subject_first_
        where spr.music_group_id_ = #{musicGroupId}
        group by spr.subject_first_
    </select>

    <select id="querySecondSubjectNum" resultMap="Mapper">
        SELECT ifnull(s.name_, '听从老师安排') key_, COUNT(spr.id_) value_
        FROM student_pre_registration spr
                 left join subject s on s.id_ = spr.subject_second_
        where spr.music_group_id_ = #{musicGroupId}
        group by spr.subject_second_
    </select>

    <select id="queryByMusicGroupIdAndUserId" resultMap="StudentPreRegistration" parameterType="map">
        SELECT *
        FROM student_pre_registration
        WHERE user_id_ = #{userId}
          and music_group_id_ = #{musicGroupId}
    </select>

    <select id="queryByMusicGroupIdAndPhone" resultMap="StudentPreRegistration" parameterType="map">
        SELECT *
        FROM student_pre_registration
        WHERE phone_ = #{phone}
          and music_group_id_ = #{musicGroupId}
    </select>

    <select id="queryByMusicGroupId" resultMap="StudentPreRegistration">
        SELECT *
        FROM student_pre_registration
        WHERE music_group_id_ = #{musicGroupId}
    </select>

    <select id="getPreRegisterNum" resultType="com.ym.mec.biz.dal.dto.MusicGroupRegAndMoneyDto">
        SELECT music_group_id_ musicGroupId, COUNT(*) preRegNum FROM student_pre_registration WHERE music_group_id_ IN
        <foreach collection="musicGroupIds" item="musicGroupId" open="(" close=")" separator=",">
            #{musicGroupId}
        </foreach>
        GROUP BY music_group_id_
    </select>
</mapper>
