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

    <resultMap type="com.ym.mec.biz.dal.entity.Student" id="Student">
        <result column="user_id_" property="userId"/>
        <result column="subject_id_list_" property="subjectIdList"/>
        <result column="service_tag_" property="serviceTag"/>
        <result column="operating_tag_" property="operatingTag"/>
        <result column="operating_temp_tag_" property="operatingTempTag"/>
        <result column="teacher_id_" property="teacherId"/>
        <result column="teacher_name_" property="teacherName"/>
        <result column="is_new_user_" property="isNewUser"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="care_package_" property="carePackage"/>
        <result column="come_on_package_" property="comeOnPackage"/>
        <result column="current_grade_num_" property="currentGradeNum"/>
        <result column="current_class_" property="currentClass"/>
        <result column="cooperation_organ_id_" property="cooperationOrganId"/>
        <result column="member_rank_setting_id_" property="memberRankSettingId"/>
        <result column="membership_start_time_" property="membershipStartTime"/>
        <result column="membership_end_time_" property="membershipEndTime"/>
        <result column="activity_course_detail_" property="activityCourseDetail"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="Student">
        SELECT *
        FROM student
        WHERE user_id_ = #{id}
    </select>
    
    <select id="getLocked" resultMap="Student">
        SELECT *
        FROM student
        WHERE user_id_ = #{id} for update
    </select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="Student">
        SELECT *
        FROM student
    </select>

    <select id="lockUser" resultType="int" useCache="false" flushCache="true">
        SELECT id_
        FROM sys_user
        WHERE id_ = #{userId} FOR
        UPDATE
    </select>

    <select id="lockUserReturnInfo" resultMap="com.ym.mec.biz.dal.dao.TeacherDao.SysUser">
        SELECT *
        FROM sys_user
        WHERE id_ = #{userId} FOR UPDATE
    </select>

    <select id="getUser" resultMap="com.ym.mec.biz.dal.dao.TeacherDao.SysUser">
        SELECT *
        FROM sys_user
        WHERE id_ = #{userId}
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.Student" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO student (user_id_,subject_id_list_,
        <if test="serviceTag != null">
            service_tag_,
        </if>
        <if test="operatingTag != null">
            operating_tag_,
        </if>
        teacher_id_,create_time_,update_time_,service_tag_update_time_,cooperation_organ_id_,
        care_package_,come_on_package_,member_rank_setting_id_,membership_start_time_,membership_end_time_)
        VALUES
        (#{userId},#{subjectIdList},
        <if test="serviceTag != null">
            #{serviceTag},
        </if>
        <if test="operatingTag != null">
            #{operatingTag},
        </if>
        #{teacherId},NOW(),NOW(),NOW(),#{cooperationOrganId},
         #{carePackage},#{comeOnPackage},#{memberRankSettingId},#{membershipStartTime},#{membershipEndTime})
    </insert>

    <update id="update" parameterType="com.ym.mec.biz.dal.entity.Student">
        UPDATE student
        <set>
            <if test="subjectIdList != null">
                subject_id_list_ = #{subjectIdList},
            </if>
            <if test="serviceTag != null">
                service_tag_update_time_=IF(service_tag_ = #{serviceTag}, service_tag_update_time_, NOW()),
                service_tag_ = #{serviceTag},
            </if>
            <if test="operatingTag != null">
                operating_tag_ = #{operatingTag},
            </if>
            <if test="operatingTempTag != null">
                operating_temp_tag_ = #{operatingTempTag},
            </if>
            <if test="teacherId != null">
                teacher_id_=#{teacherId},
            </if>
            <if test="isNewUser != null">
                is_new_user_=#{isNewUser},
            </if>
            <if test="carePackage != null">
                care_package_=#{carePackage},
            </if>
            <if test="comeOnPackage != null">
                come_on_package_=#{comeOnPackage},
            </if>
            <if test="currentGradeNum != null">
                current_grade_num_=#{currentGradeNum},
            </if>
            <if test="currentClass != null">
                current_class_=#{currentClass},
            </if>
            <if test="memberRankSettingId != null">
                member_rank_setting_id_=#{memberRankSettingId},
            </if>
            <if test="membershipStartTime != null">
                membership_start_time_=#{membershipStartTime},
            </if>
            <if test="membershipEndTime != null">
                membership_end_time_=#{membershipEndTime},
            </if>
            <if test="activityCourseDetail != null">
                activity_course_detail_=#{activityCourseDetail},
            </if>
                cooperation_organ_id_=#{cooperationOrganId},
                update_time_ = NOW()
        </set>
        WHERE user_id_ = #{userId}
    </update>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="Student" parameterType="map">
        SELECT * FROM student
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*)
        FROM student
    </select>
    <select id="findStudents" resultMap="com.ym.mec.biz.dal.dao.TeacherDao.SysUser">
        SELECT id_, username_, phone_,avatar_ FROM sys_user
        <include refid="studentQueryCondition"/>
        ORDER BY id_
        <include refid="global.limit"/>
    </select>
    <select id="countStudents" resultType="int">
        SELECT COUNT(id_) FROM sys_user
        <include refid="studentQueryCondition"/>
    </select>
    <sql id="studentQueryCondition">
        <where>
            <if test="organIdList!=null">
                organ_id_ IN
                <foreach collection="organIdList" item="organId" open="(" close=")" separator=",">
                    #{organId}
                </foreach>
            </if>
            <if test="search!=null">
                AND (username_ LIKE CONCAT('%', #{search}, '%') OR phone_ LIKE CONCAT('%', #{search}, '%'))
            </if>
            AND FIND_IN_SET("STUDENT", user_type_)
        </where>
    </sql>

    <select id="queryByOperatingTempTag" resultMap="Student">
        SELECT *
        FROM student
        WHERE operating_temp_tag_ = #{operatingTempTag}
    </select>

    <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="studentList" item="item" index="index" open="" close="" separator=";">
            UPDATE student
            <set>
                <if test="item.subjectIdList != null">
                    subject_id_list_ = #{item.subjectIdList},
                </if>
                <if test="item.serviceTag != null">
                    service_tag_ = #{item.serviceTag},
                </if>
                <if test="item.operatingTag != null">
                    operating_tag_ = #{item.operatingTag},
                </if>
                <if test="item.operatingTempTag != null">
                    operating_temp_tag_ = #{item.operatingTempTag},
                </if>
                <if test="item.teacherId != null">
                    teacher_id_=#{item.teacherId},
                </if>
                <if test="item.updateTime != null">
                    update_time_ = #{item.updateTime},
                </if>
                <if test="item.carePackage != null">
                    care_package_=#{item.carePackage},
                </if>
                <if test="item.comeOnPackage != null">
                    come_on_package_=#{item.comeOnPackage},
                </if>
                <if test="item.cooperationOrganId != null">
                    cooperation_organ_id_=#{item.cooperationOrganId},
                </if>
                <if test="item.activityCourseDetail != null">
                    activity_course_detail_=#{item.activityCourseDetail},
                </if>
	            <if test="item.memberRankSettingId != null">
	                member_rank_setting_id_=#{item.memberRankSettingId},
	            </if>
	            <if test="item.membershipStartTime != null">
	                membership_start_time_=#{item.membershipStartTime},
	            </if>
	            <if test="item.membershipEndTime != null">
	                membership_end_time_=#{item.membershipEndTime},
	            </if>
                update_time_ = NOW()
            </set>
            WHERE user_id_ = #{item.userId}
        </foreach>
    </update>
    <update id="updateStudentServiceOrOperatingTag">
        UPDATE student
        <set>
            <if test="serviceTag != null">
                service_tag_ = #{serviceTag},
            </if>
            <if test="operatingTag != null">
                operating_tag_ = #{operatingTag},
            </if>
                update_time_ = NOW()
        </set>
        WHERE
        <if test="studentId!=null">
            user_id_ = #{studentId}
        </if>
        <if test="studentIds!=null and studentIds.size()>0">
            user_id_ IN
            <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
                #{studentId}
            </foreach>
        </if>
    </update>

    <update id="updateStudentServiceTag">
        UPDATE student
        <set>
            <if test="serviceTag != null">
                service_tag_update_time_ = IF(service_tag_=#{serviceTag}, service_tag_update_time_, NOW()),
                service_tag_ = #{serviceTag},
            </if>
            update_time_ = NOW()
        </set>
        WHERE
            service_tag_=0
        <if test="studentId!=null">
            AND user_id_ = #{studentId}
        </if>
        <if test="studentIds!=null and studentIds.size()>0">
            AND user_id_ IN
            <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
                #{studentId}
            </foreach>
        </if>
    </update>
    <update id="batchUpdateAdviser">
      UPDATE student s SET s.teacher_id_ = #{teacherId},s.update_time_ = NOW() WHERE FIND_IN_SET(s.user_id_,#{studentIds})
    </update>
    <update id="updateServiceTag">
        UPDATE student SET service_tag_ = #{serviceTag},update_time_ = NOW() WHERE user_id_ IN
        <foreach collection="studentIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    <resultMap id="student4operating" type="com.ym.mec.biz.dal.dto.Student4operating">
        <result column="organ_name_" property="organName"/>
        <result column="organ_id_" property="organId"/>
        <result column="student_name_" property="studentName"/>
        <result column="student_id_" property="studentId"/>
        <result column="teacher_id_" property="teacherId"/>
        <result column="teacher_name_" property="teacherName"/>
        <result column="operating_tag_" property="operatingTag"/>
        <result column="vip_times_" property="vipTimes"/>
        <result column="free_practice_times_" property="freePracticeTimes"/>
        <result column="buy_practice_times_" property="buyPracticeTimes"/>
        <result column="music_netWork_times_" property="musicNetWorkTimes"/>
        <result column="student_num_" property="studentNum"/>
    </resultMap>

    <select id="getOperatingStudents" resultMap="student4operating">
        SELECT su.organ_id_,
        o.name_ organ_name_,
        su.username_ student_name_,
        s.user_id_ student_id_,
        tsu.id_ teacher_id_,
        tsu.real_name_ teacher_name_,
        s.operating_tag_,
        a.vip_times_,
        a.buy_practice_times_,
        a.music_netWork_times_,
        p.free_practice_times_
        FROM student s
        LEFT JOIN sys_user su ON s.user_id_ = su.id_
        LEFT JOIN sys_user tsu ON tsu.id_ = s.teacher_id_
        LEFT JOIN organization o ON o.id_ = su.organ_id_
        LEFT JOIN (
        SELECT cssp.user_id_,
        SUM(case when (cs.group_type_ = 'VIP' AND cs.teach_mode_='ONLINE') then 1 ELSE 0 END) vip_times_,
        SUM(case when (pg.type_='CHARGE') then 1 ELSE 0 END) buy_practice_times_,
        SUM(case when (cs.type_='MUSIC_NETWORK' OR cs.type_='HIGH_ONLINE') then 1 ELSE 0 END) music_netWork_times_
        FROM course_schedule_student_payment cssp
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
        LEFT JOIN practice_group pg ON pg.`id_` = cs.`music_group_id_` AND cs.`group_type_` = 'PRACTICE'
        WHERE cs.status_ IN ('NOT_START','UNDERWAY') AND (cs.is_lock_ IS NULL OR cs.is_lock_=0) AND (cs.del_flag_ IS NULL OR cs.del_flag_=0)
        GROUP BY cssp.user_id_
        ) a on a.user_id_ = s.user_id_
        LEFT JOIN (
        SELECT student_id_, count(id_) free_practice_times_
        FROM practice_group
        WHERE type_='FREE' AND group_status_ IN ('NORMAL', 'FINISH')
        GROUP BY student_id_
        ) p ON p.student_id_ = s.user_id_
        <include refid="student4OperatingQueryCondition"/>
        ORDER BY s.user_id_
        <include refid="global.limit"/>
    </select>

    <select id="countOperatingStudents" resultType="int">
        SELECT COUNT(s.user_id_) FROM student s
        LEFT JOIN sys_user su ON s.user_id_ = su.id_
        LEFT JOIN sys_user tsu ON tsu.id_ = s.teacher_id_
        LEFT JOIN (
        SELECT cssp.user_id_,
        SUM(case when (cs.group_type_ = 'VIP' AND cs.teach_mode_='ONLINE') then 1 ELSE 0 END) vip_times_,
        SUM(case when (pg.type_='CHARGE') then 1 ELSE 0 END) buy_practice_times_,
        SUM(case when (cs.type_='MUSIC_NETWORK' OR cs.type_='HIGH_ONLINE') then 1 ELSE 0 END) music_netWork_times_
        FROM course_schedule_student_payment cssp
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
        LEFT JOIN practice_group pg ON pg.`id_` = cs.`music_group_id_` AND cs.`group_type_` = 'PRACTICE'
        WHERE cs.status_ IN ('NOT_START','UNDERWAY') AND (cs.is_lock_ IS NULL OR cs.is_lock_=0) AND (cs.del_flag_ IS NULL OR cs.del_flag_=0)
        GROUP BY cssp.user_id_
        ) a on a.user_id_ = s.user_id_
        LEFT JOIN (
        SELECT student_id_, count(id_) free_practice_times_
        FROM practice_group
        WHERE type_='FREE' AND group_status_ IN ('NORMAL', 'FINISH')
        GROUP BY student_id_
        ) p ON p.student_id_ = s.user_id_
        <include refid="student4OperatingQueryCondition"/>
    </select>

    <sql id="student4OperatingQueryCondition">
        <where>
            su.user_type_ LIKE '%STUDENT%'
            <if test="search != null and search != ''">
                AND (su.phone_ LIKE CONCAT('%',#{search},'%') OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.id_
                LIKE CONCAT('%',#{search},'%'))
            </if>
            <if test="organId != null and organId != ''">
                AND FIND_IN_SET(su.organ_id_,#{organId})
            </if>
            <if test="teacherId!=null">
                AND s.teacher_id_ = #{teacherId}
            </if>
            <if test="operatingTag !=null">
                AND s.operating_tag_ = #{operatingTag}
            </if>
            <if test='hasVip != null and hasVip=="1"'>
                AND a.vip_times_ >=1
            </if>
            <if test='hasVip != null and hasVip=="0"'>
                AND (a.vip_times_ =0 OR a.vip_times_ IS NULL)
            </if>
            <if test='hasFreePractice != null and hasFreePractice=="1"'>
                AND p.free_practice_times_ >=1
            </if>
            <if test='hasFreePractice != null and hasFreePractice=="0"'>
                AND (p.free_practice_times_ =0 OR p.free_practice_times_ IS NULL)
            </if>
            <if test='hasBuyPractice != null and hasBuyPractice=="1"'>
                AND a.buy_practice_times_ >=1
            </if>
            <if test='hasBuyPractice != null and hasBuyPractice=="0"'>
                AND (a.buy_practice_times_ =0 OR a.buy_practice_times_ IS NULL)
            </if>
            <if test='hasMusicNetWork != null and hasMusicNetWork=="1"'>
                AND a.music_netWork_times_ >=1
            </if>
            <if test='hasMusicNetWork != null and hasMusicNetWork=="0"'>
                AND (a.music_netWork_times_ =0 OR a.music_netWork_times_ IS NULL)
            </if>
        </where>
    </sql>

    <select id="getTeacherOperatingStudentsNum" resultType="java.util.Map">
        select teacher_id_ 'key', count(*) 'value'
        FROM student
        WHERE FIND_IN_SET(teacher_id_, #{teacherIds})
          AND operating_tag_ = 1
        GROUP BY teacher_id_
    </select>

    <select id="getBuyNums" resultType="java.util.Map">
        SELECT s.teacher_id_ 'key', COUNT(DISTINCT cssp.user_id_) 'value'
        FROM course_schedule_student_payment cssp
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_ AND cs.teach_mode_ = 'ONLINE'
        LEFT JOIN student s ON s.user_id_ = cssp.user_id_
        <if test="groupType != null and groupType==@com.ym.mec.biz.dal.enums.GroupType@PRACTICE">
            LEFT JOIN practice_group pg ON cssp.music_group_id_ = pg.id_ AND cssp.group_type_ = 'PRACTICE'
        </if>
        WHERE s.teacher_id_ = #{teacherId}
        AND cs.status_ IN ('NOT_START', 'UNDERWAY')
        AND (cs.is_lock_ IS NULL OR cs.is_lock_ = 0)
        <if test="groupType != null">
            AND cs.group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        </if>
        <if test="groupType != null and groupType==@com.ym.mec.biz.dal.enums.GroupType@PRACTICE">
            AND pg.group_status_ IN ('NORMAL', 'FINISH')
            AND pg.buy_months_ >= 1
        </if>
        GROUP BY s.teacher_id_
    </select>
    <select id="getPracticeAndVipNums" resultType="java.util.Map">
        SELECT s.teacher_id_ 'key', COUNT(DISTINCT pg.student_id_) 'value'
        FROM practice_group pg
                 LEFT JOIN student s on s.user_id_ = pg.student_id_
                 LEFT JOIN course_schedule_student_payment vcssp
                           ON vcssp.user_id_ = pg.student_id_ AND vcssp.group_type_ = 'VIP'
                 LEFT JOIN course_schedule_student_payment pcssp
                           ON pcssp.user_id_ = pg.student_id_ AND pcssp.group_type_ = 'PRACTICE'
                 LEFT JOIN course_schedule vcs ON vcs.id_ = vcssp.course_schedule_id_ AND vcs.teach_mode_ = 'ONLINE'
                 LEFT JOIN course_schedule pcs ON pcs.id_ = pcssp.course_schedule_id_
        WHERE FIND_IN_SET(s.teacher_id_, #{teacherIds})
          AND pg.group_status_ IN ('NORMAL', 'FINISH')
          AND vcs.status_ IN ('NOT_START', 'UNDERWAY')
          AND pcs.status_ IN ('NOT_START', 'UNDERWAY')
          AND (vcs.is_lock_ IS NULL OR vcs.is_lock_ = 0)
          AND (pcs.is_lock_ IS NULL OR pcs.is_lock_ = 0)
          AND pg.buy_months_ >= 1
        GROUP BY s.teacher_id_
    </select>
    <select id="getServiceStudents" resultMap="Student">
        SELECT su.id_ user_id_,s.teacher_id_ FROM sys_user su
        LEFT JOIN student s ON s.user_id_=su.id_
        WHERE s.service_tag_=1 AND FIND_IN_SET('STUDENT', su.user_type_)
    </select>

    <resultMap id="StudentServeDto" type="com.ym.mec.biz.dal.dto.StudentServeDto" extends="Student">
        <result column="group_type_" property="groupType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="teach_mode_" property="teachMode" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="courseStartTime" column="course_start_time_"/>
    </resultMap>
    <select id="getBeServiceStudents" resultMap="StudentServeDto">
        SELECT
            s.user_id_,
            s.teacher_id_,
            cs.group_type_,
            cs.teach_mode_,
            IF(CONCAT( cs.class_date_, ' ', cs.end_class_time_ )&lt;#{courseExpireDate} AND (sa.status_ != 'NORMAL' OR sa.status_ IS NULL), NULL, cs.class_date_) course_start_time_
        FROM
            student s
            LEFT JOIN course_schedule_student_payment cssp ON cssp.user_id_ = s.user_id_
	        LEFT JOIN student_attendance sa ON cssp.course_schedule_id_=sa.course_schedule_id_ AND sa.user_id_=cssp.user_id_
            LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
            AND cs.group_type_ IN ('VIP', 'PRACTICE')
            AND CONCAT( cs.class_date_, ' ', cs.start_class_time_ ) > #{monday}
        WHERE
            s.service_tag_ = 1
    </select>
    <select id="getBeServiceStudentsInfoWithStudents" resultMap="StudentServeDto">
        SELECT
            s.user_id_,
            s.teacher_id_,
            cs.group_type_,
            cs.teach_mode_,
            IF(CONCAT( cs.class_date_, ' ', cs.end_class_time_ )&lt;#{courseExpireDate} AND (sa.status_ != 'NORMAL' OR sa.status_ IS NULL), NULL, cs.class_date_) course_start_time_
        FROM
          student s
        LEFT JOIN course_schedule_student_payment cssp ON cssp.user_id_ = s.user_id_
        LEFT JOIN student_attendance sa ON cssp.course_schedule_id_=sa.course_schedule_id_ AND sa.user_id_=cssp.user_id_
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
        AND cs.group_type_ IN ('VIP', 'PRACTICE')
        AND CONCAT( cs.class_date_, ' ', cs.start_class_time_ ) > #{monday}
        WHERE
        s.service_tag_ = 1 AND s.user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
    </select>
    <select id="getBeServiceStudentsInfoWithStudentsForHistory" resultMap="StudentServeDto">
        SELECT
            s.student_id_ user_id_,
            s.teacher_id_,
            cs.group_type_,
            cs.teach_mode_,
            IF(CONCAT( cs.class_date_, ' ', cs.end_class_time_ )&lt;#{courseExpireDate} AND (sa.status_ != 'NORMAL' OR sa.status_ IS NULL), NULL, cs.class_date_) course_start_time_
        FROM
          student_extracurricular_exercises_situation_ s
        LEFT JOIN course_schedule_student_payment cssp ON cssp.user_id_ = s.student_id_
        LEFT JOIN student_attendance sa ON cssp.course_schedule_id_=sa.course_schedule_id_ AND sa.user_id_=cssp.user_id_
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
        AND cs.group_type_ IN ('VIP', 'PRACTICE')
        AND CONCAT( cs.class_date_, ' ', cs.start_class_time_ ) > #{monday}
        WHERE
        s.monday_ = #{monday} AND s.student_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
    </select>
    <select id="getServeStudentIds" resultType="int">
        SELECT user_id_
        FROM student stu
          LEFT JOIN sys_user su ON stu.user_id_=su.id_
        WHERE service_tag_=1 AND su.del_flag_=0
    </select>
    <select id="getServeStudentCourseStartTimes" resultType="com.ym.mec.biz.dal.dto.StudentServeCourseDto">
        SELECT
            cssp.user_id_ studentId,
            stu.service_tag_update_time_ serviceTagUpdateTime,
            CONCAT(cs.class_date_, ' ', cs.start_class_time_) courseStartTime
        FROM course_schedule_student_payment cssp
            LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_
            LEFT JOIN student stu ON cssp.user_id_ = stu.user_id_
        WHERE stu.service_tag_=1 AND class_date_&gt;=#{monday}
    </select>

    <resultMap id="StudentServeCourseDto" type="com.ym.mec.biz.dal.dto.StudentServeCourseDto">
        <result column="student_id_" property="studentId"/>
        <result column="lead_teacher_id_" property="leadTeacherId"/>
        <result column="course_schedule_id_" property="courseScheduleId"/>
        <result column="group_type_" property="groupType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="class_group_id_" property="classGroupId"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="actual_teacher_id_" property="actualTeacherId"/>
        <result column="course_start_time_" property="courseStartTime"/>
        <result column="teach_mode_" property="teachMode"/>
    </resultMap>

    <select id="getStudentFutureCourseInfo" resultMap="StudentServeCourseDto">
        SELECT
            cssp.user_id_ student_id_,
            stu.teacher_id_ lead_teacher_id_,
            cs.id_ course_schedule_id_,
            cs.group_type_,
            cs.music_group_id_,
            cs.class_group_id_,
            cs.status_,
            CASE cs.type_ WHEN 'TRAINING_SINGLE' THEN 'SINGLE' WHEN 'TRAINING_MIX' THEN 'MIX' ELSE cs.type_ END type_,
            cs.actual_teacher_id_,
            CONCAT(cs.class_date_, ' ', cs.start_class_time_) course_start_time_,
            cs.teach_mode_
        FROM course_schedule_student_payment cssp
            LEFT JOIN course_schedule cs ON cssp.course_schedule_id_=cs.id_
            LEFT JOIN student stu ON cssp.user_id_ = stu.user_id_
        WHERE stu.service_tag_=1
            AND (stu.service_tag_update_time_ IS NULL OR stu.service_tag_update_time_&lt;#{nextMonday})
            AND (cs.del_flag_ IS NULL OR cs.del_flag_=0)
            AND (cs.new_course_id_ IS NULL OR cs.new_course_id_=cs.id_)
            AND cs.class_date_&gt;=#{monday}
            AND cs.type_ IN ('SINGLE', 'TRAINING_SINGLE', 'VIP', 'MIX', 'TRAINING_MIX', 'PRACTICE')
            AND cssp.create_time_&lt;#{nextMonday}
            <if test="studentIds!=null and studentIds.size()>0">
                AND cssp.user_id_ IN
                <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
                    #{studentId}
                </foreach>
            </if>
        ORDER BY cssp.user_id_,course_start_time_
    </select>

    <select id="getNoCourseBeServeStudentInfo" resultMap="StudentServeCourseDto">
        SELECT
            sr.user_id_ student_id_,
            stu.teacher_id_ lead_teacher_id_,
            'MUSIC' group_type_,
            sr.music_group_id_
        FROM
            student_registration sr
            LEFT JOIN student stu ON sr.user_id_ = stu.user_id_
            LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        WHERE
            mg.del_flag_ = 0
            AND mg.organ_id_ IS NOT NULL
            AND mg.status_ = 'PROGRESS'
            AND sr.music_group_status_ = 'NORMAL'
            <if test="noCourseStudentIds!=null and noCourseStudentIds.size()>0">
                AND sr.user_id_ IN
                <foreach collection="noCourseStudentIds" item="noCourseStudentId" open="(" close=")" separator=",">
                    #{noCourseStudentId}
                </foreach>
            </if>
    </select>

    <select id="getStudentNames" resultType="string">
        SELECT username_ FROM sys_user WHERE id_ IN
        <foreach collection="studentIds" item="studentId" separator="," open="(" close=")">
            #{studentId}
        </foreach>
    </select>
    <select id="getStudentsWithOrgan" resultType="com.ym.mec.biz.dal.dto.SimpleUserDto">
        SELECT
            su.id_ userId,su.username_ userName, su.avatar_ avatar, su.phone_ phone
        FROM
            sys_user su
        WHERE
            1=1
            AND su.del_flag_=0 AND su.lock_flag_=0
            AND FIND_IN_SET('STUDENT',su.user_type_)
            <if test="organIds!=null">
                AND su.organ_id_ IN
                <foreach collection="organIds" item="organId" open="(" close=")" separator=",">
                    #{organId}
                </foreach>
            </if>
            <if test="search != null">
                AND (su.username_ LIKE CONCAT('%', #{search}, '%') OR su.phone_ LIKE CONCAT('%', #{search}, '%'))
            </if>
        ORDER BY su.id_
        <include refid="global.limit"/>
    </select>

    <select id="countStudentsWithOrgan" resultType="int">
        SELECT
            COUNT(su.id_)
        FROM
            sys_user su
        WHERE
            1=1
            AND su.del_flag_=0 AND su.lock_flag_=0
            AND FIND_IN_SET('STUDENT',su.user_type_)
            <if test="organIds!=null">
                AND su.organ_id_ IN
                <foreach collection="organIds" item="organId" open="(" close=")" separator=",">
                    #{organId}
                </foreach>
            </if>
            <if test="search != null">
                AND (su.username_ LIKE CONCAT('%', #{search}, '%') OR su.phone_ LIKE CONCAT('%', #{search}, '%'))
            </if>
    </select>

    <select id="getStudentTeacher" resultMap="Student">
        SELECT s.user_id_,t.real_name_ teacher_name_ FROM student s
        LEFT JOIN sys_user t ON t.id_ = s.teacher_id_
        WHERE s.user_id_ IN
        <foreach collection="studentIds" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="findByStudentIds" resultMap="Student">
        SELECT * FROM student WHERE user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
    </select>

    <select id="lockStudents" resultMap="Student">
        SELECT * FROM student WHERE user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
        FOR UPDATE
    </select>

    <select id="queryCloseServiceTagIds" resultType="java.lang.Integer">
        SELECT s.user_id_ FROM student s
        LEFT JOIN course_schedule_student_payment cssp ON cssp.user_id_ = s.user_id_
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
        LEFT JOIN student_registration sr ON sr.user_id_ = s.user_id_
        LEFT JOIN music_group mg ON mg.id_ = sr.music_group_id_
        WHERE s.service_tag_ = 1
        GROUP BY s.user_id_ HAVING COUNT(CASE WHEN cs.status_ = 'NOT_START' THEN 1 ELSE NULL END) = 0
        AND COUNT(CASE WHEN sr.music_group_status_ = 'NORMAL' AND mg.status_ = 'PROGRESS' THEN 1 ELSE NULL END) = 0;
    </select>

    <select id="getStudentSubjects" resultMap="com.ym.mec.biz.dal.dao.SubjectDao.Subject">
        SELECT
            DISTINCT sub.*
        FROM
            student stu
            LEFT JOIN `subject` sub ON FIND_IN_SET( sub.id_, stu.subject_id_list_ )
        WHERE
            stu.user_id_ IN
            <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
                #{studentId}
            </foreach>
            AND sub.id_ IS NOT NULL
        ORDER BY sub.id_ DESC
    </select>

    <select id="queryByIds" resultMap="Student">
        SELECT * FROM student
        WHERE user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
        ORDER BY user_id_
        <include refid="global.limit"></include>
    </select>
    <select id="countByIds" resultType="int">
        SELECT count(user_id_) FROM student
        WHERE user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
    </select>
    <select id="getNoTeacherOrNoSubjectStudent" resultMap="Student">
        SELECT * FROM student WHERE teacher_id_ IS NULL OR subject_id_list_ IS NULL OR cooperation_organ_id_ IS NULL;
    </select>

    <select id="getHasVipCourseStudentIdsFromNewStudents" resultType="int">
        SELECT DISTINCT s.user_id_
        FROM student s
        LEFT JOIN course_schedule_student_payment cssp ON s.user_id_=cssp.user_id_
        WHERE s.is_new_user_=1 AND cssp.group_type_='VIP'
    </select>
    <select id="getMemberNum" resultType="int">
        SELECT COUNT(0) FROM student WHERE member_rank_setting_id_ IS NOT NULL AND FIND_IN_SET(user_id_,#{studentIdsStr})
    </select>
    <select id="queryPushMemberRenew" resultType="java.lang.Integer">
        SELECT user_id_ FROM student WHERE DATEDIFF(membership_end_time_,NOW()) = #{pushMemberRenew};
    </select>

    <update id="updateGrade"><![CDATA[
        UPDATE student SET current_grade_num_=current_grade_num_+1
        WHERE current_grade_num_>=1
          AND current_grade_num_<= 11
        ]]>
    </update>

    <update id="updateNewStudentsToOld">
        UPDATE student SET is_new_user_=0
        WHERE user_id_ IN
        <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
            #{userId}
        </foreach>
    </update>
    <update id="cleanStudentMember">
        UPDATE student SET member_rank_setting_id_ = NULL,membership_start_time_ = NULL,membership_end_time_ = NULL
        WHERE DATEDIFF(NOW(),membership_end_time_) > 0
    </update>
    <update id="cleanMember">
        UPDATE student SET member_rank_setting_id_ = NULL,membership_start_time_ = NULL,membership_end_time_ = NULL
        WHERE user_id_ = #{userId}
    </update>
</mapper>
