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

    <resultMap type="com.ym.mec.biz.dal.entity.CourseSchedule" id="CourseSchedule">
        <result column="id_" property="id"/>
        <result column="class_group_id_" property="classGroupId"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="subsidy_" property="subsidy"/>
        <result column="class_date_" property="classDate"/>
        <result column="start_class_time_" property="startClassTime"/>
        <result column="end_class_time_" property="endClassTime"/>
        <result column="teacher_id_" property="teacherId"/>
        <result column="actual_teacher_id_" property="actualTeacherId"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="teach_mode_" property="teachMode" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="name_" property="name"/>
        <result column="student_num_" property="studentNum"/>
        <result column="leave_student_num_" property="leaveStudentNum"/>
    </resultMap>

    <resultMap type="com.ym.mec.biz.dal.dto.CourseScheduleDto" id="courseScheduleDto" extends="CourseSchedule">
        <result column="teacher_name_" property="teacherName"/>
        <result column="name_" property="classGroupName"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="school_name_" property="schoolName"/>
        <result column="sign_in_status_" property="signInStatus"/>
        <result column="sign_out_status_" property="signOutStatus"/>
        <result column="seal_class_id_" property="sealClassId"/>
        <result column="total_student_num_" property="totalStudentNum"/>
    </resultMap>

    <resultMap type="com.ym.mec.biz.dal.entity.StudentCourseScheduleRecordDto" id="studentCourseScheduleRecord">
        <result column="id_" property="id"/>
        <result column="course_schedule_id_" property="courseScheduleId"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="name_" property="courseName"/>
        <result column="class_date_" property="classDate"/>
        <result column="start_class_time_" property="startClassTime"/>
        <result column="end_class_time_" property="endClassTime"/>
        <result column="teacher_id_" property="teacherId"/>
        <result column="teacher_name_" property="teacherName"/>
        <result column="teach_mode_" property="teachMode" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="rest_of_class_" property="restOfClass"/>
    </resultMap>

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

    <!-- 全查询 -->
    <select id="findAll" resultMap="CourseSchedule">
        SELECT *
        FROM course_schedule
        ORDER BY id_
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.CourseSchedule" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        <!--
        <selectKey resultClass="int" keyProperty="id" >
        SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
        </selectKey>
        -->
        INSERT INTO course_schedule
        (id_,class_group_id_,status_,subsidy_,class_date_,start_class_time_,end_class_time_,teacher_id_,actual_teacher_id_,create_time_,update_time_,type_,name_,teach_mode_,student_num_,leave_student_num_)
        VALUES(#{id},#{classGroupId},#{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{subsidy},#{classDate},#{startClassTime},#{endClassTime},#{teacherId},#{teacherId},now(),now(),#{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{name},#{teachMode,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{studentNum},#{leaveStudentNum})
    </insert>

    <insert id="batchAddCourseSchedules" parameterType="java.util.List" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO course_schedule
        (id_,class_group_id_,status_,subsidy_,class_date_,start_class_time_,end_class_time_,teacher_id_,actual_teacher_id_,create_time_,update_time_,type_,name_,teach_mode_,student_num_,leave_student_num_)
        VALUE
        <foreach collection="list" item="course" separator=",">
            (#{course.id},#{course.classGroupId},#{course.status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{course.subsidy},#{course.classDate},#{course.startClassTime},#{course.endClassTime},#{course.teacherId},#{course.teacherId},now(),now(),#{course.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{course.name},#{course.teachMode,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{course.studentNum},#{course.leaveStudentNum})
        </foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.CourseSchedule">
        UPDATE course_schedule
        <set>
            <if test="classDate != null">
                class_date_ = #{classDate},
            </if>
            <if test="endClassTime != null">
                end_class_time_ = #{endClassTime},
            </if>
            <if test="status != null">
                status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="type != null">
                type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="actualTeacherId != null">
                actual_teacher_id_ = #{actualTeacherId},
            </if>
            <if test="subsidy != null">
                subsidy_ = #{subsidy},
            </if>
            <if test="classGroupId != null">
                class_group_id_ = #{classGroupId},
            </if>
            <if test="startClassTime != null">
                start_class_time_ = #{startClassTime},
            </if>
            <if test="teacherId != null">
                teacher_id_ = #{teacherId},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
            <if test="studentNum != null">
                student_num_ = #{studentNum},
            </if>
            <if test="leaveStudentNum != null">
                leave_student_num_ = #{leaveStudentNum},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

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

    <delete id="deleteCourseSchedulesByMusicGroupID">
        DELETE
        cs
        FROM
        course_schedule cs
        INNER JOIN class_group cg ON cs.class_group_id_=cg.id_
        WHERE cg.music_group_id_=#{musicGroupID}
    </delete>

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

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

    <resultMap type="com.ym.mec.biz.dal.dto.TeacherAttendanceDto" id="TeacherAttendanceViewUtilEntity">
        <result property="courseScheduleId" column="id_"/>
        <result property="courseScheduleName" column="course_schedule_name_"/>
        <result property="classDate" column="class_date_"/>
        <result property="startClassTime" column="start_class_time_"/>
        <result property="endClassTime" column="end_class_time_"/>
        <result property="courseStatus" column="course_status_"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="classId" column="class_id"/>
        <result property="className" column="class_name"/>
        <result property="classType" column="class_type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="musicGroupId" column="music_group_id"/>
        <result property="musicGroupName" column="music_group_name"/>
        <result property="schoolId" column="school_id_"/>
        <result property="address" column="address_"/>
        <result property="longitudeAndLatitude" column="longitude_latitude_"/>
        <result property="teacherName" column="teacher_name_"/>
        <result property="signInTime" column="sign_in_time_"/>
    </resultMap>

    <select id="getCurrentCourseDetail"
            resultMap="TeacherAttendanceViewUtilEntity">
        SELECT
        cs.id_ ,
        cs.name_ course_schedule_name_,
        cs.class_date_,
        CONCAT(cs.class_date_,' ',cs.start_class_time_) start_class_time_,
        CONCAT(cs.class_date_,' ',cs.end_class_time_) end_class_time_,
        cs.status_ course_status_,
        cg.id_ class_id,
        cg.name_ class_name,
        cs.type_ class_type,
        mg.id_ music_group_id,
        mg.name_ music_group_name,
        s.id_ school_id_,
        s.address_,
        s.longitude_latitude_,
        su.username_ teacher_name_,
        ta.sign_in_time_
        FROM
        course_schedule cs
        LEFT JOIN class_group cg ON cs.class_group_id_=cg.id_
        LEFT JOIN music_group mg ON cg.music_group_id_=mg.id_
        LEFT JOIN school s ON mg.school_id_=s.id_
        LEFT JOIN sys_user su ON cs.actual_teacher_id_=su.id_
        LEFT JOIN teacher_attendance ta ON cs.id_=ta.course_schedule_id_
        WHERE cs.id_= #{courseID}
    </select>

    <select id="getTeacherCourseByDateSpeed"
            resultMap="TeacherAttendanceViewUtilEntity">
        SELECT
        cs.id_ ,
        cs.name_ course_schedule_name_,
        cs.class_date_,
        CONCAT(cs.class_date_,' ',cs.start_class_time_) start_class_time_,
        CONCAT(cs.class_date_,' ',cs.end_class_time_) end_class_time_,
        cs.status_ course_status_,
        cg.id_ class_id,
        cg.name_ class_name,
        cs.type_ class_type,
        mg.id_ music_group_id,
        mg.name_ music_group_name,
        s.id_ school_id_,
        s.address_,
        s.longitude_latitude_,
        su.username_ teacher_name_,
        ta.sign_in_time_
        FROM
        course_schedule cs
        LEFT JOIN class_group cg ON cs.class_group_id_=cg.id_
        LEFT JOIN music_group mg ON cg.music_group_id_=mg.id_
        LEFT JOIN school s ON mg.school_id_=s.id_
        LEFT JOIN sys_user su ON cs.actual_teacher_id_=su.id_
        LEFT JOIN teacher_attendance ta ON cs.id_=ta.course_schedule_id_
        WHERE cs.actual_teacher_id_= #{teacherId}
        AND (cs.end_class_time_ BETWEEN #{startTime} AND #{endTime})
        ORDER BY cs.id_ DESC LIMIT 1
    </select>

    <resultMap id="studentAttendanceViewUtilEntity" type="com.ym.mec.biz.dal.dto.StudentAttendanceViewDto">
        <result property="classGroupId" column="class_group_id_"/>
        <result property="studentId" column="student_id_"/>
        <result property="userName" column="username_"/>
        <result property="musicGroupId" column="music_group_id_"/>
        <result property="subjectId" column="subject_id_"/>
        <result property="subjectName" column="subject_name_"/>
        <result property="status" column="status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

    <select id="getCurrentCourseStudents" resultMap="studentAttendanceViewUtilEntity">
        SELECT
        cgsm.class_group_id_,
        su.id_ student_id_,
        su.username_,
        mg.id_ music_group_id_,
        s.id_ subject_id_,
        s.name_ subject_name_,
        cgsm.status_
        FROM
        class_group cg
        LEFT JOIN class_group_student_mapper cgsm ON cg.id_=cgsm.class_group_id_
        LEFT JOIN sys_user su ON cgsm.user_id_=su.id_
        LEFT JOIN music_group mg ON cg.music_group_id_=mg.id_
        LEFT JOIN `subject` s ON cg.subject_id_=s.id_
        WHERE cgsm.class_group_id_=#{classID} AND cgsm.status_!="DROP_OUT"
    </select>

    <select id="countStudentInClass" resultType="java.lang.Integer">
        SELECT
        COUNT(class_group_id_)
        FROM
        class_group_student_mapper cgsm
        WHERE class_group_id_ IN
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
        GROUP BY user_id_
    </select>

    <select id="getCourseSchedulesWithDate" resultMap="courseScheduleDto">
        SELECT
        cg.id_ seal_class_id_,
        cs.name_,
        cs.type_,
        cs.id_,
        cs.class_date_,
        CONCAT(cs.class_date_,' ',cs.start_class_time_) start_class_time_,
        CONCAT(cs.class_date_,' ',cs.end_class_time_) end_class_time_,
        cs.status_,
        cs.student_num_,
        cs.leave_student_num_,
        cg.student_num_ total_student_num_,
        su.username_ teacher_name_,
        ta.sign_in_status_ attendance_status_
        FROM
        music_group mg
        LEFT JOIN class_group cg ON mg.id_ = cg.music_group_id_
        LEFT JOIN course_schedule cs ON cg.id_ = cs.class_group_id_
        LEFT JOIN sys_user su ON cs.teacher_id_=su.id_
        LEFT JOIN teacher_attendance ta ON cs.id_=ta.class_group_id_ AND ta.teacher_id_=cs.teacher_id_
        WHERE
        mg.organ_id_ = #{organId}
        AND cs.id_ IS NOT NULL
        AND cs.class_date_ = DATE_FORMAT(#{date},'%Y-%m-%d')
        <include refid="global.limit"/>
    </select>

    <select id="countCourseSchedulesWithDate" resultType="int">
        SELECT
        count(*)
        FROM
        music_group mg
        LEFT JOIN class_group cg ON mg.id_ = cg.music_group_id_
        LEFT JOIN course_schedule cs ON cg.id_ = cs.class_group_id_
        WHERE
        mg.organ_id_ = #{organId}
        AND cs.id_ IS NOT NULL
        AND cs.class_date_ = DATE_FORMAT(#{date},'%Y-%m-%d')
    </select>

    <select id="getStudentCourseSchedulesWithDate" resultMap="courseScheduleDto">
        SELECT
        cgsm.class_group_id_ seal_class_id_,
        cs.name_,
        cs.type_,
        cs.id_,
        cs.class_date_,
        CONCAT(cs.class_date_,' ',cs.start_class_time_) start_class_time_,
        CONCAT(cs.class_date_,' ',cs.end_class_time_) end_class_time_,
        cs.status_,
        su.username_ teacher_name_,
        cgsm.status_ attendance_status_,
        cs.teach_mode_
        FROM
        course_schedule cs
        LEFT JOIN sys_user su ON cs.teacher_id_=su.id_
        LEFT JOIN class_group_student_mapper cgsm ON cgsm.class_group_id_ = cs.class_group_id_
        WHERE
        cs.class_date_ = DATE_FORMAT(#{classDate},'%Y%m%d')
        AND cgsm.user_id_ = #{studentId}
    </select>

    <select id="getTeacherCourseSchedulesWithDate" resultMap="courseScheduleDto">
        SELECT
        cg.id_ seal_class_id_,
        cg.name_,
        su.username_ teacher_name_,
        cs.type_,
        cs.id_,
        cs.class_date_,
        CONCAT(cs.class_date_,' ',cs.start_class_time_) start_class_time_,
        CONCAT(cs.class_date_,' ',cs.end_class_time_) end_class_time_,
        cs.status_,
        s.name_ school_name_,
        if(ta.sign_in_status_ IS NULL,0,1) sign_in_status_,
        if(ta.sign_out_status_ IS NULL,0,1) sign_out_status_,
        cs.teach_mode_
        FROM
        course_schedule cs
        LEFT JOIN sys_user su ON cs.actual_teacher_id_=su.id_
        LEFT JOIN class_group cg ON cs.class_group_id_ = cg.id_
        LEFT JOIN music_group mg ON cg.music_group_id_=mg.id_
        LEFT JOIN school s ON mg.school_id_=s.id_
        LEFT JOIN teacher_attendance ta ON cs.id_=ta.course_schedule_id_ AND ta.teacher_id_=#{teacherId}
        WHERE
        cs.class_date_ = DATE_FORMAT(#{classDate},'%Y%m%d')
        AND cs.actual_teacher_id_ = #{teacherId}
    </select>

    <select id="getCourseScheduleDateByMonth" resultType="java.util.Date">
        SELECT
        cs.class_date_
        FROM
        course_schedule cs
        LEFT JOIN class_group cg ON cs.class_group_id_=cg.id_
        WHERE DATE_FORMAT(cs.class_date_,'%Y%m')=DATE_FORMAT(#{month},'%Y%m')
        AND cg.music_group_id_=#{musicGroupID}
        GROUP BY cs.class_date_
    </select>

    <select id="countTeacherCourseInOnDayRepeats" parameterType="com.ym.mec.biz.dal.entity.CourseSchedule"
            resultType="int">
        SELECT
        COUNT( * )
        FROM
        course_schedule cs
        WHERE
        cs.teacher_id_ = #{teacherId}
        AND cs.class_date_ = DATE_FORMAT( #{classDate}, '%Y-%m-%d' )
        AND (
        CONCAT( cs.class_date_, ' ', cs.start_class_time_ ) &lt; DATE_FORMAT( 'endClassTime', '%Y-%m-%d %H:%i:%s' )
        OR CONCAT( cs.class_date_, ' ', cs.end_class_time_ ) &gt; DATE_FORMAT( 'startClassTime', '%Y-%m-%d %H:%i:%s' )
        )
    </select>
    <select id="findTeacherCoursesInDates" resultMap="CourseSchedule">
        SELECT
        id_,
        class_group_id_,
        status_,
        subsidy_,
        class_date_,
        concat(class_date_,' ',start_class_time_) start_class_time_,
        concat(class_date_,' ',end_class_time_) end_class_time_,
        teacher_id_,
        actual_teacher_id_,
        create_time_,
        update_time_
        FROM
        course_schedule cs
        WHERE
        cs.teacher_id_ = #{teacherId}
        AND cs.class_date_ IN
        <foreach collection="dates" item="date" open="(" close=")" separator=",">
            #{date}
        </foreach>
    </select>
    <select id="findCoursesByDates" resultMap="CourseSchedule">
        SELECT
        *
        FROM
        course_schedule cs
        WHERE
        cs.class_date_ IN
        <foreach collection="dates" item="date" open="(" close=")" separator=",">
            DATE_FORMAT(#{date},'%Y-%m-%d')
        </foreach>
    </select>

    <select id="findOverLapCoursesInDay" resultMap="CourseSchedule">
        SELECT
        *
        FROM
        course_schedule cs
        WHERE
        cs.class_date_ IN
        <foreach collection="dates" item="date" open="(" close=")" separator=",">
            DATE_FORMAT(#{date},'%Y-%m-%d')
        </foreach>
        AND (
        DATE_FORMAT(cs.start_class_time_,"%H:%i:%s") &gt; DATE_FORMAT(#{endClassTime}, '%H:%i:%s' )
        OR DATE_FORMAT(cs.end_class_time_,"%H:%i:%s") &lt; DATE_FORMAT( #{endClassTime}, '%H:%i:%s' )
        )=0
    </select>

    <select id="findCourseSchedulesByIds" resultMap="CourseSchedule">
        SELECT
        *
        FROM
        course_schedule cs
        WHERE
        cs.id_ IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <!-- 获取班级未开始的课程 -->
    <select id="findClassGroupNoStartCourseSchedules" resultMap="CourseSchedule">
        SELECT * FROM course_schedule
        WHERE status_ = 'NOT_START'
        AND class_group_id_ IN
        <foreach collection="classGroupTeacherMapperList" item="classGroupTeacherMapper" index="index" open="("
                 close=")" separator=",">
            #{classGroupTeacherMapper.classGroupId}
        </foreach>
    </select>

    <select id="getStudentCourseScheduleDate" resultType="java.util.Date">
        SELECT
        cs.class_date_
        FROM
        class_group_student_mapper cgsm
        LEFT JOIN course_schedule cs ON cgsm.class_group_id_ = cs.class_group_id_
        WHERE
        cgsm.user_id_ = #{studentId}
        <if test="month==null">
            AND DATE_FORMAT( NOW(), '%Y%m' ) = DATE_FORMAT( cs.class_date_, '%Y%m' )
        </if>
        <if test="month!=null">
            AND DATE_FORMAT( #{month}, '%Y%m' ) = DATE_FORMAT( cs.class_date_, '%Y%m' )
        </if>
        <if test="isAttend!=null">
            AND cs.status_='NOT_START'
        </if>
        GROUP BY
        cs.class_date_
    </select>

    <select id="getTeacherCourseScheduleDate" resultType="java.util.Date">
        SELECT
        cs.class_date_
        FROM
        course_schedule cs
        WHERE
        cs.actual_teacher_id_ = #{teacherId}
        <if test="month==null">
            AND DATE_FORMAT( NOW(), '%Y%m' ) = DATE_FORMAT( cs.class_date_, '%Y%m' )
        </if>
        <if test="month!=null">
            AND DATE_FORMAT( #{month}, '%Y%m' ) = DATE_FORMAT( cs.class_date_, '%Y%m' )
        </if>
        GROUP BY
        cs.class_date_
    </select>

    <select id="getCourseScheduleDate" resultType="java.util.Date">
        SELECT
        cs.class_date_
        FROM
        music_group mg
        LEFT JOIN class_group cg ON mg.id_ = cg.music_group_id_
        LEFT JOIN course_schedule cs ON cg.id_ = cs.class_group_id_
        WHERE
        mg.organ_id_ = #{organId} AND cs.id_ IS NOT NULL
        <if test="month==null">
            AND DATE_FORMAT( NOW(), '%Y%m' ) = DATE_FORMAT( cs.class_date_, '%Y%m' )
        </if>
        <if test="month!=null">
            AND DATE_FORMAT( #{month}, '%Y%m' ) = DATE_FORMAT( cs.class_date_, '%Y%m' )
        </if>
        GROUP BY
        cs.class_date_
    </select>

    <select id="findClassSubjects" resultType="string">
        SELECT
        CONCAT(cg.id_,'-',IF(GROUP_CONCAT(s.name_) IS NULL,'',GROUP_CONCAT(s.name_)))
        FROM
        class_group cg
        LEFT JOIN `subject` s ON FIND_IN_SET(s.id_,cg.subject_id_list_)
        WHERE cg.id_ IN
        <foreach collection="list" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        GROUP BY cg.id_
    </select>

    <sql id="queryStudentCourseScheduleRecordCondition">
        <where>
            sa.user_id_=#{userId}
            <if test="teachMode!=null and teachMode!=''">
                AND cs.teach_mode_=#{teachMode}
            </if>
            <if test="type!=null and type!=''">
                AND cs.type_=#{type}
            </if>
            <if test="status!=null and status!=''">
                AND sa.status_=#{status}
            </if>
        </where>
    </sql>

    <select id="findStudentCourseScheduleRecords" resultMap="studentCourseScheduleRecord">
        SELECT
        sa.id_,
        sa.status_,
        sa.teacher_id_,
        su.username_,
        cs.id_ course_schedule_id_,
        cs.name_,
        cs.class_date_,
        cs.start_class_time_,
        cs.end_class_time_,
        cs.teacher_id_,
        cs.teach_mode_
        FROM
        student_attendance sa
        LEFT JOIN course_schedule cs ON sa.course_schedule_id_=cs.id_
        LEFT JOIN sys_user su ON sa.teacher_id_=su.id_
        <include refid="queryStudentCourseScheduleRecordCondition"/>
        ORDER BY sa.id_
        <include refid="global.limit"/>
    </select>
    <select id="countStudentCourseScheduleRecords" resultType="int">
        SELECT
        count(*)
        FROM
        student_attendance sa
        LEFT JOIN course_schedule cs ON sa.course_schedule_id_=cs.id_
        LEFT JOIN sys_user su ON sa.teacher_id_=su.id_
        <include refid="queryStudentCourseScheduleRecordCondition"/>
    </select>
    <select id="findStudentCourseScheduleNotStartRecords" resultMap="studentCourseScheduleRecord">
        SELECT
        cs.teacher_id_,
        su.username_ teacher_name_,
        cs.id_ course_schedule_id_,
        cs.name_,
        cs.class_date_,
        cs.start_class_time_,
        cs.end_class_time_,
        cs.teach_mode_,
        (cg.total_class_times_-cg.current_class_times_) rest_of_class_
        FROM
        class_group_student_mapper cgsm
        LEFT JOIN course_schedule cs ON cs.class_group_id_=cgsm.class_group_id_
        LEFT JOIN sys_user su ON cs.teacher_id_ = su.id_
        LEFT JOIN class_group cg ON cgsm.class_group_id_=cg.id_
        WHERE cs.status_='NOT_START' AND cgsm.user_id_=#{userId} AND cs.class_date_=DATE_FORMAT(#{date},'%Y%m%d')
    </select>
    <select id="findCourseScheduleByMusicGroup" resultMap="CourseSchedule">
        SELECT
        cs.*
        FROM
        class_group cg
        LEFT JOIN course_schedule cs ON cg.id_=cs.class_group_id_
        WHERE cg.music_group_id_=#{musicGroupId}
    </select>
    <select id="countClassTimes" resultType="int">
        SELECT
        COUNT(*)
        FROM
        course_schedule cs
        WHERE cs.class_group_id_=#{classGroupId} AND cs.status_!="NOT_START"
    </select>
    <select id="getCourseSchedules" resultMap="courseScheduleDto">
        SELECT
        cs.type_,
        cs.id_,
        cs.class_date_,
        CONCAT(cs.class_date_,' ',cs.start_class_time_) start_class_time_,
        CONCAT(cs.class_date_,' ',cs.end_class_time_) end_class_time_,
        sc.paran_value_
        FROM
        course_schedule cs
        LEFT JOIN sys_config sc ON sc.param_name_="vip_appeal_days_range"
        WHERE
        cs.id_=#{courseScheduleId}
    </select>

    <select id="findStudentNamesByCourseSchedule" resultType="string">
        SELECT
        su.username_
        FROM
        student_attendance sa
        LEFT JOIN sys_user su ON sa.user_id_=su.id_
        WHERE sa.course_schedule_id_=#{courseScheduleId}
    </select>
    <select id="findVipGroupCourseSchedules" resultMap="CourseSchedule">
        SELECT
        cs.*
        FROM
        vip_group_class_group_mapper vgcgm
        LEFT JOIN course_schedule cs ON vgcgm.class_group_id_=cs.class_group_id_
        WHERE
        vgcgm.vip_group_id_ =#{vipGroupId}
    </select>
    <select id="findByClassGroupAndDate" resultMap="CourseSchedule">
        SELECT
        id_,
        class_group_id_,
        status_,
        subsidy_,
        class_date_,
        CONCAT(class_date_,' ',start_class_time_) start_class_time_,
        CONCAT(class_date_,' ',end_class_time_) end_class_time_,
        teacher_id_,
        actual_teacher_id_,
        create_time_,
        update_time_,
        type_,name_,
        teach_mode_
        FROM
        course_schedule
        <where>
            <if test="classGroupId!=null">
                AND class_group_id_ = #{classGroupId}
            </if>
            <if test="classDates!=null">
                AND class_date_ IN
                <foreach collection="classDates" item="classDate" open="(" close=")" separator=",">
                    #{classDate}
                </foreach>
            </if>
        </where>
    </select>
    
    <select id="findClassTypeByCourse" resultType="string">
        SELECT
        cg.type_
        FROM
        course_schedule cs
        LEFT JOIN class_group cg ON cs.class_group_id_=cg.id_
        WHERE cs.id_=#{courseScheduleId}
    </select>
    
    <select id="queryVipGroupTeachereClassTimesByMonth" resultType="map" parameterType="map">
        SELECT vp.organ_id_ organ_id_,cs.actual_teacher_id_ teacher_id_,count(cs.id_) times FROM course_schedule cs left join vip_group_class_group_mapper vgm on cs.class_group_id_ = vgm.class_group_id_ left join vip_group vp on vgm.vip_group_id_ = vp.id_ where cs.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler} and date_format(#{monthDate} , '%Y%m' ) = date_format(cs.class_date_, '%Y%m' ) group by vp.organ_id_,cs.actual_teacher_id_
    </select>
    
    <select id="queryFinishedWithNoUpdateStatus" resultMap="CourseSchedule">
    	select * from course_schedule where CONCAT(class_date_,' ',end_class_time_) &lt;= now() and status_ != 'OVER'
    </select>
    
    <update id="batchUpdate" parameterType="com.ym.mec.biz.dal.entity.CourseSchedule">
        UPDATE course_schedule
        <set>
            <if test="classDate != null">
                class_date_ = #{classDate},
            </if>
            <if test="endClassTime != null">
                end_class_time_ = #{endClassTime},
            </if>
            <if test="status != null">
                status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="type != null">
                type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="actualTeacherId != null">
                actual_teacher_id_ = #{actualTeacherId},
            </if>
            <if test="subsidy != null">
                subsidy_ = #{subsidy},
            </if>
            <if test="classGroupId != null">
                class_group_id_ = #{classGroupId},
            </if>
            <if test="startClassTime != null">
                start_class_time_ = #{startClassTime},
            </if>
            <if test="teacherId != null">
                teacher_id_ = #{teacherId},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
            <if test="studentNum != null">
                student_num_ = #{studentNum},
            </if>
            <if test="leaveStudentNum != null">
                leave_student_num_ = #{leaveStudentNum},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>
</mapper>
