<?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.yonge.cooleshow.biz.dal.dao.CourseScheduleDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.CourseSchedule">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="course_group_id_" jdbcType="INTEGER" property="courseGroupId"/>
        <result column="type_" jdbcType="VARCHAR" property="type"/>
        <result column="status_" jdbcType="VARCHAR" property="status"/>
        <result column="class_num_" jdbcType="INTEGER" property="classNum"/>
        <result column="teacher_id_" jdbcType="INTEGER" property="teacherId"/>
        <result column="class_date_" jdbcType="TIMESTAMP" property="classDate"/>
        <result column="start_time_" jdbcType="TIMESTAMP" property="startTime"/>
        <result column="end_time_" jdbcType="TIMESTAMP" property="endTime"/>
        <result column="lock_" jdbcType="INTEGER" property="lock"/>
        <result column="lock_time_" jdbcType="TIMESTAMP" property="lockTime"/>
        <result column="ex_student_num_" jdbcType="INTEGER" property="exStudentNum"/>
        <result column="created_by_" jdbcType="INTEGER" property="createdBy"/>
        <result column="created_time_" jdbcType="TIMESTAMP" property="createdTime"/>
        <result column="updated_by_" jdbcType="INTEGER" property="updatedBy"/>
        <result column="updated_time_" jdbcType="TIMESTAMP" property="updatedTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_
        , course_group_id_, type_, status_,class_num_, teacher_id_, class_date_, start_time_, end_time_, lock_, lock_time_, ex_student_num_, created_by_, created_time_, updated_by_, updated_time_
    </sql>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.yonge.cooleshow.biz.dal.entity.CourseSchedule">
        insert into course_schedule(course_group_id_, type_, status_,class_num_, teacher_id_, class_date_, start_time_,
        end_time_, lock_, lock_time_, ex_student_num_, created_by_, created_time_, updated_by_,
        updated_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.courseGroupId}, #{entity.type}, #{entity.status},#{entity.classNum}, #{entity.teacherId}, #{entity.classDate},
            #{entity.startTime}, #{entity.endTime}, #{entity.lock}, #{entity.lockTime},
            #{entity.exStudentNum}, #{entity.createdBy}, #{entity.createdTime}, #{entity.updatedBy},
            #{entity.updatedTime})
        </foreach>
    </insert>

    <select id="queryTeacherTotal" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherTotalVo">
        select
            t.user_id_ as userId,
            a.expTime,
            a.unExpTime,
            b.starGrade
        from teacher t
        left join (
            <!-- 统计查询已上课时数,未上课时数 -->
            select
                a.teacher_id_ as userId,
                sum(if(a.end_time_ &lt;= now(),1,0)) as expTime,
                sum(if(a.end_time_ &gt; now(),1,0)) as unExpTime
            from course_schedule a
            where a.lock_ = 0 and a.type_ in ('PRACTICE','PIANO_ROOM_CLASS')
            <if test="userId != null and userId != ''">
                and a.teacher_id_ = #{userId}
            </if>
            group by a.teacher_id_
        ) a on t.user_id_ = a.userId
        left join (
            <!-- 统计老师星级评分 -->
            select
                a.teacher_id_ as userId,
                avg (b.score_) as starGrade
            from course_schedule a
            join course_schedule_replied b on a.id_ = b.course_schedule_id_
            where a.lock_ = 0 and a.type_ in ('PRACTICE','PIANO_ROOM_CLASS') and b.score_ is not null
            <if test="userId != null and userId != ''">
                and a.teacher_id_ = #{userId}
            </if>
            group by a.teacher_id_
        ) b on t.user_id_ = b.userId
        <where>
            <if test="userId != null and userId != ''">
                and t.user_id_ = #{userId}
            </if>
        </where>
    </select>
    <select id="queryStudentTotal" resultType="com.yonge.cooleshow.biz.dal.vo.StudentTotalVo">
        select
            t.user_id_ as userId,
            sum(if(b.end_time_ &lt;= now(),1,0)) as finshHours,
            sum(if(b.end_time_ &gt; now(),1,0)) as unfinshHours
        from student t
        left join course_schedule_student_payment a on t.user_id_ = a.user_id_
        left join course_schedule b on a.course_id_ = b.id_
        where b.lock_ = 0 and b.type_ in ('PRACTICE','PIANO_ROOM_CLASS')
            <if test="userId != null and userId != ''">
                and t.user_id_ = #{userId}
            </if>
        group by t.user_id_
    </select>

    <select id="queryStudentCourse" resultMap="BaseResultMap">
        select b.id_,
               b.course_group_id_,
               b.type_,
               b.class_num_,
               b.teacher_id_,
               b.class_date_,
               b.start_time_,
               b.end_time_,
               b.lock_,
               b.lock_time_,
               b.ex_student_num_,
               b.status_,
               b.created_by_,
               b.created_time_,
               b.updated_by_,
               b.updated_time_
        from course_schedule_student_payment as a
                 left join course_schedule as b on a.course_id_ = b.id_
        <where>
            a.user_id_ = #{param.studentId}
            <if test="param.greaterDate != null">
                AND <![CDATA[ b.start_time_ > #{param.greaterDate} ]]>
            </if>
            <if test="param.startClassDate != null">
                AND <![CDATA[ b.class_date_  >= #{param.startClassDate} ]]>
            </if>
            <if test="param.endClassDate != null">
                AND <![CDATA[ b.class_date_  <= #{param.endClassDate} ]]>
            </if>
            <if test="param.classDate != null">
                AND b.class_date_ = #{param.classDate}
            </if>
            <if test="param.status != null">
                AND b.status_ = #{param.status}
            </if>
            <if test="param.statusList != null">
                AND b.status_ IN
                <foreach collection="param.statusList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by b.start_time_ desc
    </select>

    <select id="queryLiveTeacherCourse" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherLiveCourseInfoVo">
        select
        b.id_ as courseGroupId,
        b.name_ as courseGroupName,
        a.id_ as courseId,
        a.class_num_ as classNum,
        s.name_ as subjectName,
        a.start_time_ as startTime,
        a.end_time_ as endTime,
        a.status_ as `status`,
        b.pre_student_num_ as studentCount,
        b.background_pic_ as backgroundPic,
        b.im_group_id_  as imGroupId
        from course_schedule as a
        left join course_group as b on a.course_group_id_ = b.id_
        left join subject as s on b.subject_id_ = s.id_
        where b.teacher_id_ = #{param.teacherId}
        AND a.lock_ = 0
        AND a.type_ = #{param.type}
        <![CDATA[ AND a.class_date_  >= #{param.startDate} ]]>
        <![CDATA[ AND a.class_date_  <= #{param.endDate} ]]>
        <if test="param.groupState !=null and param.groupState !=''">
            AND  find_in_set(b.status_, #{param.groupState})
        </if>
        <if test="param.status !=null and param.status !=''">
            AND a.status_ = #{param.status}
        </if>
        <if test="param.subjectId != null and param.subjectId !=''">
            AND b.subject_id_ = #{param.subjectId}
        </if>
        order by start_time_
    </select>

    <select id="queryTeacherPracticeCourse" resultType="com.yonge.cooleshow.biz.dal.vo.MyCourseVo">
        SELECT distinct
            u.id_ AS userId,
            u.username_ AS userName,
            u.real_name_ AS realName,
            u.avatar_ AS avatar,
            cs.class_date_ AS classDate,
            cs.start_time_ AS startTime,
            cs.end_time_ AS endTime,
            cs.status_ AS `status`,
            g.subject_id_ AS subjectId,
            sb.name_ AS subjectName,
            p.course_id_ AS courseId,
            p.course_group_id_ AS courseGoupId,
            (r.student_replied_ IS NOT NULL) AS studentReplied,
            (r.teacher_replied_ IS NOT NULL) AS teacherReplied
        FROM course_schedule_student_payment p
        LEFT JOIN sys_user u ON p.user_id_ =u.id_
        LEFT JOIN course_schedule cs ON p.course_id_=cs.id_
        LEFT JOIN course_group g ON p.course_group_id_ = g.id_
        LEFT JOIN `subject` sb ON g.subject_id_=sb.id_
        LEFT JOIN course_schedule_replied r ON cs.id_=r.course_schedule_id_
        WHERE p.course_id_ IN
        (SELECT s.id_ FROM course_schedule s WHERE s.type_='PRACTICE' AND lock_=0 AND s.teacher_id_=#{param.teacherId})
        <if test="param.status !=null and param.status !=''">
            AND cs.status_ = #{param.status}
        </if>
        <if test="param.subjectId !=null">
            AND g.subject_id_ = #{param.subjectId}
        </if>
        <if test="param.classDate !=null and param.classDate !=''">
            AND cs.class_date_ = #{param.classDate}
        </if>
        <if test="param.startDate !=null and param.startDate !=''">
            <![CDATA[ AND cs.class_date_  >= #{param.startDate} ]]>
        </if>
        <if test="param.endDate !=null and param.endDate !=''">
            <![CDATA[ AND cs.class_date_  <= #{param.endDate} ]]>
        </if>
        <if test="param.repliedIds !=null and param.repliedIds.size > 0">
            AND p.user_id_ IN
            <foreach collection="param.repliedIds" item="repliedIds" open="(" close=")" separator=",">
                #{repliedIds}
            </foreach>
        </if>
        <if test="param.studentName !=null and param.studentName !=''">
            AND u.username_ LIKE CONCAT('%', #{param.studentName}, '%')
        </if>
        ORDER BY cs.start_time_
    </select>

    <select id="countTeacherNoDecorateHomework" resultType="java.lang.Integer">

        select count(1)
        from course_schedule cs
        left join course_homework ch on ch.course_schedule_id_ = cs.id_
        <where>
            <!-- 没有布置作业记录 -->
            <if test="param.decorate != null">
                <if test="param.decorate.code == 0">
                    and ch.id_ is null
                </if>
                <if test="param.decorate.code == 1">
                    and ch.id_ is not null
                </if>
            </if>
            <if test="param.courseStatus != null">
                and cs.type_ = #{param.courseType}
            </if>
            <if test="param.courseType != null">
                and cs.status_ = #{param.courseStatus}
            </if>
            <if test="param.teacherId != null">
                and cs.teacher_id_ = #{param.teacherId}
            </if>
            <if test="param.courseScheduleId != null ">
                and cs.id_ = #{param.courseScheduleId}
            </if>
        </where>
    </select>

    <select id="queryCourseSchedule" resultType="java.lang.String"
            parameterType="com.yonge.cooleshow.biz.dal.dto.search.MyCourseSearch">
        SELECT s.class_date_
        FROM course_schedule_student_payment p
        LEFT JOIN course_schedule s ON p.course_id_ = s.id_
        WHERE s.teacher_id_=#{teacherId}
        AND s.lock_=0
        AND s.status_ IN ('ING','COMPLETE','NOT_START')
        AND p.course_type_ IN ('PIANO_ROOM_CLASS','PRACTICE')
        <![CDATA[ AND s.class_date_  >= #{startDate} ]]>
        <![CDATA[ AND s.class_date_  <= #{endDate} ]]>
        UNION
        SELECT s.class_date_
        FROM course_group g
        LEFT JOIN course_schedule s ON g.id_=s.course_group_id_
        WHERE g.teacher_id_=#{teacherId}
        AND g.type_='LIVE'
        AND g.status_ IN ('COMPLETE','ING')
        <![CDATA[ AND s.class_date_  >= #{startDate} ]]>
        <![CDATA[ AND s.class_date_  <= #{endDate} ]]>
    </select>

    <select id="queryStudentPracticeCourse" resultType="com.yonge.cooleshow.biz.dal.vo.MyCourseVo">
        SELECT
            s.id_ AS courseId,
            s.course_group_id_ AS courseGoupId,
            s.class_date_ AS classDate,
            s.start_time_ AS startTime,
            s.end_time_ AS endTime,
            s.status_ AS `status`,
            s.teacher_id_ AS teacherId,
            u.id_ AS userId,
            u.username_ AS userName,
            u.real_name_ AS realName,
            u.avatar_ AS avatar,
            g.subject_id_ AS subjectId,
            b.name_ AS subjectName
        FROM course_schedule s
        LEFT JOIN sys_user u ON s.teacher_id_ = u.id_
        LEFT JOIN course_group g ON s.course_group_id_ = g.id_
        LEFT JOIN `subject` b ON g.subject_id_ = b.id_
        WHERE s.lock_=0
        AND s.status_ IN ('ING','NOT_START','COMPLETE')
        AND s.id_ IN
        (SELECT course_id_ FROM course_schedule_student_payment WHERE user_id_ = #{param.studentId} AND course_type_ = 'PRACTICE')
        <if test="param.status !=null and param.status !=''">
            AND s.status_ = #{param.status}
        </if>
        <if test="param.subjectId !=null">
            AND g.subject_id_ = #{param.subjectId}
        </if>
        <if test="param.classDate !=null and param.classDate !=''">
            AND s.class_date_ = #{param.classDate}
        </if>
        <if test="param.startDate !=null and param.startDate !=''">
            <![CDATA[ AND s.class_date_  >= #{param.startDate} ]]>
        </if>
        <if test="param.endDate !=null and param.endDate !=''">
            <![CDATA[ AND s.class_date_  <= #{param.endDate} ]]>
        </if>
        ORDER BY s.start_time_
    </select>

    <select id="queryCourseUser" resultType="com.yonge.cooleshow.biz.dal.vo.CourseStudent">
        SELECT
            cs.id_ AS courseId,
            cs.course_group_id_ AS courseGoupId,
            cs.class_date_ AS classDate,
            cs.start_time_ AS startTime,
            cs.end_time_ AS endTime,
            cs.status_ AS `status`,
            cs.type_ AS courseType,
            NULL AS userId,
            CONCAT(g.name_,'-第',cs.class_num_,'课') AS name,
            NULL AS realName,
            p.payCount AS payCount,
            g.background_pic_ AS cover,
            g.subject_id_ AS subjectId,
            sb.name_ AS subjectName,
            (r.student_replied_ IS NOT NULL) AS studentReplied,
            (r.teacher_replied_ IS NOT NULL) AS teacherReplied,
            i.id_ AS imGroupId
        FROM course_schedule cs
        LEFT JOIN course_group g ON cs.course_group_id_ = g.id_
        LEFT JOIN (SELECT course_id_ AS pid,count(*) AS payCount FROM course_schedule_student_payment GROUP BY course_id_ ) p ON cs.id_=p.pid
        LEFT JOIN `subject` sb ON g.subject_id_=sb.id_
        LEFT JOIN course_schedule_replied r ON cs.id_ = r.course_schedule_id_
        LEFT JOIN im_group i ON g.id_ = i.course_group_id_
        WHERE cs.lock_=0
        AND cs.status_ IN ('ING','COMPLETE','NOT_START')
        AND g.status_ IN ('ING', 'COMPLETE')
        AND cs.type_ IN ('LIVE','PIANO_ROOM_CLASS')
        AND cs.teacher_id_=#{param.teacherId}
        AND cs.class_date_=#{param.classDate}
        AND cs.id_ IN(
            SELECT c.id_
            FROM course_schedule_student_payment p,course_schedule c
            WHERE p.course_id_=c.id_
            AND p.course_group_id_ = c.course_group_id_
            AND c.teacher_id_=#{param.teacherId}
            AND c.class_date_=#{param.classDate}
            AND c.type_ IN ('LIVE','PIANO_ROOM_CLASS')
            )
        UNION
        SELECT
            p.course_id_ AS courseId,
            p.course_group_id_ AS courseGoupId,
            cs.class_date_ AS classDate,
            cs.start_time_ AS startTime,
            cs.end_time_ AS endTime,
            cs.status_ AS `status`,
            cs.type_ AS courseType,
            u.id_ AS userId,
            u.username_ AS name,
            u.real_name_ AS realName,
            NULL AS payCount,
            u.avatar_ AS cover,
            g.subject_id_ AS subjectId,
            sb.name_ AS subjectName,
            (r.student_replied_ IS NOT NULL) AS studentReplied,
            (r.teacher_replied_ IS NOT NULL) AS teacherReplied,
            NULL AS imGroupId
        FROM course_schedule_student_payment p
        LEFT JOIN sys_user u ON p.user_id_ =u.id_
        LEFT JOIN course_schedule cs ON p.course_id_=cs.id_
        LEFT JOIN course_group g ON p.course_group_id_ = g.id_
        LEFT JOIN `subject` sb ON g.subject_id_=sb.id_
        LEFT JOIN course_schedule_replied r ON cs.id_ = r.course_schedule_id_
        WHERE cs.lock_=0
        AND cs.status_ IN ('ING','COMPLETE','NOT_START')
        AND p.course_id_ IN (SELECT s.id_ FROM course_schedule s WHERE s.type_='PRACTICE' AND s.teacher_id_=#{param.teacherId})
        AND cs.class_date_=#{param.classDate}
        ORDER BY startTime
    </select>

    <select id="queryCourseScheduleStudent" resultType="java.lang.String"
            parameterType="com.yonge.cooleshow.biz.dal.dto.search.MyCourseSearch">
        SELECT class_date_ FROM course_schedule
        WHERE lock_=0
        AND status_ IN ('ING','NOT_START','COMPLETE')
        AND id_ IN (SELECT course_id_ FROM course_schedule_student_payment WHERE user_id_ = #{studentId})
        <![CDATA[ AND class_date_  >= #{startDate} ]]>
        <![CDATA[ AND class_date_  <= #{endDate} ]]>
    </select>

    <select id="teacherList" resultType="com.yonge.cooleshow.biz.dal.vo.PracticeTeacherVo">
        SELECT
        t.user_id_ AS teacherId,
        u.username_ AS userName,
        u.real_name_ AS realName,
        u.avatar_ AS avatar,

        tt.star_grade_ AS starGrade,
        tt.exp_time_ AS expTime,
        t.graduate_school_ AS school,
        t.subject_ AS schoolSubject,

        (SELECT group_concat(p.subject_name_) FROM teacher_subject_price p WHERE find_in_set(t.user_id_,p.teacher_id_)) AS configSubject,
        sp.subjectId AS subjectId,
        sp.subjectName AS subjectName,
        sp.subjectPrice AS subjectPrice,
        sp.courseMinutes AS courseMinutes

        FROM teacher t
        LEFT JOIN sys_user u ON t.user_id_ = u.id_
        LEFT JOIN teacher_total tt ON t.user_id_=tt.user_id_
        LEFT JOIN (SELECT
        f.teacher_id_ AS teacherId,
        p.subject_id_ AS subjectId,
        p.subject_name_ AS subjectName,
        p.subject_price_ AS subjectPrice,
        p.course_minutes_ AS courseMinutes
        FROM teacher_free_time f
        LEFT JOIN teacher_subject_price p ON f.id_=p.teacher_free_time_id
        WHERE f.default_flag_=1 AND f.enable_flag_=1
        AND p.subject_id_=#{param.subjectId}) sp ON t.user_id_=sp.teacherId

        <where>
        <if test="param.teacherIdList != null and param.teacherIdList.size>0">
            AND t.user_id_ IN
            <foreach collection="param.teacherIdList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="param.subjectId !=null">
            AND sp.subjectId=#{param.subjectId}
        </if>
        <if test="param.search !=null and param.search !=''">
            AND u.username_ LIKE CONCAT('%', #{param.search}, '%')
        </if>
        <choose>
            <when test="param.sort !=null and param.sort !=''">
                ORDER BY ${param.sort}
            </when>
        </choose>
        </where>
    </select>

    <select id="queryCourseTeacher" resultType="com.yonge.cooleshow.biz.dal.vo.CourseStudent"
            parameterType="com.yonge.cooleshow.biz.dal.dto.search.MyCourseSearch">
        SELECT
            s.id_ AS courseId,
            s.course_group_id_ AS courseGoupId,
            s.class_date_ AS classDate,
            s.start_time_ AS startTime,
            s.end_time_ AS endTime,
            s.type_ AS courseType,
            s.status_ AS `status`,
            u.id_ AS userId,
            u.username_ AS name,
            u.real_name_ AS realName,
            u.avatar_ AS cover,
            g.subject_id_ AS subjectId,
            b.name_ AS subjectName,
            ig.id_ AS imGroupId,
            (r.student_replied_ IS NOT NULL) AS studentReplied,
            (r.teacher_replied_ IS NOT NULL) AS teacherReplied,
            1 AS payCount
        FROM course_schedule s
        LEFT JOIN sys_user u ON s.teacher_id_ = u.id_
        LEFT JOIN course_group g ON s.course_group_id_ = g.id_
        LEFT JOIN `subject` b ON g.subject_id_ = b.id_
        LEFT JOIN course_schedule_replied r ON s.id_ = r.course_schedule_id_
        LEFT JOIN im_group ig ON ig.course_group_id_ = g.id_
        WHERE s.lock_=0
        AND s.status_ IN ('ING','NOT_START','COMPLETE')
        AND s.id_ IN
        (SELECT course_id_ FROM course_schedule_student_payment WHERE user_id_ = #{param.studentId} AND course_type_ = 'PRACTICE')
        AND s.class_date_ = #{param.classDate}
        UNION
        SELECT
            s.id_ AS courseId,
            s.course_group_id_ AS courseGoupId,
            s.class_date_ AS classDate,
            s.start_time_ AS startTime,
            s.end_time_ AS endTime,
            s.type_ AS courseType,
            s.status_ AS `status`,
            NULL AS userId,
            CONCAT(g.name_,'-第',s.class_num_,'课') AS name,
            NULL AS realName,
            g.background_pic_ AS cover,
            g.subject_id_ AS subjectId,
            sb.name_ AS subjectName,
            ig.id_ AS imGroupId,
            (r.student_replied_ IS NOT NULL) AS studentReplied,
            (r.teacher_replied_ IS NOT NULL) AS teacherReplied,
            IFNULL(a.payCount,0) AS payCount
        FROM course_schedule s
        LEFT JOIN course_group g ON s.course_group_id_ = g.id_
        LEFT JOIN `subject` sb ON g.subject_id_=sb.id_
        LEFT JOIN course_schedule_replied r ON s.id_ = r.course_schedule_id_
        LEFT JOIN im_group ig ON ig.course_group_id_ = g.id_
        LEFT JOIN (SELECT course_id_,COUNT(1) AS payCount FROM course_schedule_student_payment GROUP BY course_id_) a ON s.id_=a.course_id_
        WHERE s.lock_=0
        AND s.status_ IN ('ING','NOT_START','COMPLETE')
        AND g.status_ IN ('ING', 'COMPLETE','APPLY')
        AND s.id_ IN
        (SELECT course_id_ FROM course_schedule_student_payment WHERE user_id_ = #{param.studentId} AND course_type_ IN ('LIVE','PIANO_ROOM_CLASS'))
        AND s.class_date_ = #{param.classDate}
        ORDER BY startTime
    </select>

    <select id="queryStudentLiveCourse" parameterType="map" resultType="com.yonge.cooleshow.biz.dal.vo.CourseStudent">
        SELECT distinct
        cs.id_ AS courseId,
        cs.course_group_id_ AS courseGoupId,
        cs.class_date_ AS classDate,
        cs.start_time_ AS startTime,
        cs.end_time_ AS endTime,
        cs.status_ AS `status`,
        cs.type_ AS courseType,
        su.username_ AS userId,
        CONCAT(g.name_,'-第',cs.class_num_,'课') AS name,
        IFNULL(g.pre_student_num_, 0) AS payCount,
        g.background_pic_ AS cover,
        g.subject_id_ AS subjectId,
        sb.name_ AS subjectName,
        su.avatar_ AS avatar,
        g.im_group_id_  as imGroupId
        FROM
        course_schedule_student_payment as a
        LEFT JOIN course_schedule cs on a.course_id_ = cs.id_
        LEFT JOIN course_group g ON cs.course_group_id_ = g.id_
        LEFT JOIN `subject` sb ON g.subject_id_=sb.id_
        LEFT JOIN sys_user su on g.teacher_id_ = su.id_
        LEFT JOIN user_order o on a.order_no_  = o.order_no_
        WHERE cs.type_=#{param.type}
        AND a.user_id_ = #{param.studentId}
        <![CDATA[ AND class_date_  >= #{param.startDate} ]]>
        <![CDATA[ AND class_date_  <= #{param.endDate} ]]>
        <if test="param.orderState != null">
            AND o.status_ = #{param.orderState}
        </if>
        <if test="param.subjectId != null">
            AND g.subject_id_ = #{param.subjectId}
        </if>
        <if test="param.courseState != null">
            and cs.status_ = #{param.courseState}
        </if>
    </select>

    <insert id="addCourseGroup" parameterType="com.yonge.cooleshow.biz.dal.dto.PracticeScheduleDto" useGeneratedKeys="true" keyProperty="groupId">
        Insert INTO course_group(teacher_id_,type_,name_,subject_id_,single_course_minutes_,course_num_,course_introduce_,
        course_price_,status_,created_by_,mix_student_num_,course_start_time_,pre_student_num_)
        VALUES (#{teacherId},#{type},#{courseGroupName},#{subjectId},#{singleCourseMinutes},#{courseNum},#{courseIntroduce},
        #{coursePrice},#{status},#{studentId},#{mixStudentNum},#{courseStartTime},1)
    </insert>

    <update id="updateLock" parameterType="java.util.List">
        UPDATE course_schedule SET lock_ = 0 WHERE id_ IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <update id="courseAdjust" parameterType="com.yonge.cooleshow.biz.dal.vo.CourseAdjustVo">
        UPDATE course_schedule
        SET class_date_=#{classDate},start_time_=#{startTime},end_time_=#{endTime}
        WHERE id_ = #{courseId}
    </update>
    <select id="selectLive" resultType="com.yonge.cooleshow.biz.dal.vo.StudentHomePage$Live">
        SELECT
            u.id_ AS teacherId,
            u.username_ AS teacherName,
            u.real_name_ AS realName,
            u.avatar_ AS avatar,
            g.id_ AS courseGroupId,
            g.name_ AS courseGroupName,
            g.course_price_ AS courseGroupPrice,
            g.course_start_time_ AS courseStartTime,
            g.background_pic_ AS backgroundPic,
            g.course_num_ AS courseNum,
            g.pre_student_num_ AS buyCount,
            g.subject_id_ AS subjectId,
            s.name_ AS subjectName
        FROM course_group g
        LEFT JOIN sys_user u ON g.teacher_id_=u.id_
        LEFT JOIN subject s ON g.subject_id_=s.id_
        WHERE u.del_flag_ = 0 and type_='LIVE' and g.status_ = 'APPLY' and #{appAuditVersion} = g.audit_version_
        ORDER BY courseStartTime DESC LIMIT 4
    </select>
    <select id="selectVideo" resultType="com.yonge.cooleshow.biz.dal.vo.StudentHomePage$Video">
        SELECT
            u.id_ AS teacherId,
            u.username_ AS teacherName,
            u.real_name_ AS realName,
            u.avatar_ AS avatar,
            g.id_ AS videoGroupId,
            g.lesson_name_ AS videoGroupName,
            g.lesson_price_ AS lessonPrice,
            g.create_time_ AS createTime,
            g.lesson_cover_url_ AS lessonCoverUrl,
            g.lesson_count_ AS lessonCount,
            g.lesson_subject_ AS subjectId,
            s.name_ AS subjectName,
            IFNULL(r.count_,0) AS buyCount
        FROM video_lesson_group g
        LEFT JOIN sys_user u ON g.teacher_id_=u.id_
        LEFT JOIN `subject` s ON g.lesson_subject_=s.id_
        LEFT JOIN (SELECT video_lesson_group_id_ ,COUNT(1) AS count_ FROM video_lesson_purchase_record WHERE order_status_='PAID' GROUP BY video_lesson_group_id_) r ON g.id_= r.video_lesson_group_id_
        WHERE u.del_flag_ = 0 and g.audit_status_='PASS' and #{appAuditVersion} = g.audit_version_
        ORDER BY g.create_time_ DESC LIMIT 4
    </select>
    <select id="selectRecentCourses" resultType="com.yonge.cooleshow.biz.dal.vo.StudentHomePage$RecentCourses">
        SELECT
            u.id_ AS teacherId,
            u.username_ AS teacherName,
            u.real_name_ AS realName,
            u.avatar_ AS avatar,
            p.course_group_id_ AS courseGroupId,
            p.course_id_ AS courseId,
            g.name_ AS courseGroupName,
            p.course_type_ AS courseType,
            s.status_ AS `status`,
            s.start_time_ AS courseStartTime
        FROM course_schedule_student_payment p
        LEFT JOIN course_schedule s ON p.course_id_=s.id_
        LEFT JOIN sys_user u ON s.teacher_id_=u.id_
        LEFT JOIN course_group g ON p.course_group_id_=g.id_
        WHERE p.user_id_=#{studentId}
        AND s.status_ = 'ING'
        ORDER BY ABS(NOW() - s.start_time_) ASC
        limit 1
    </select>
    <select id="selectRecentCoursesPractice" resultType="com.yonge.cooleshow.biz.dal.vo.StudentHomePage$RecentCourses">
        SELECT
            p.user_id_ AS studentId,
            u.id_ AS teacherId,
            u.username_ AS teacherName,
            u.real_name_ AS realName,
            u.avatar_ AS avatar,
            s.course_group_id_ AS courseGroupId,
            s.id_ AS courseId,
            g.name_ AS courseGroupName,
            s.type_ AS courseType,
            s.status_ AS `status`,
            s.start_time_ AS courseStartTime
        FROM course_schedule s
        LEFT JOIN sys_user u ON s.teacher_id_=u.id_
        LEFT JOIN course_group g ON s.course_group_id_=g.id_
        LEFT JOIN course_schedule_student_payment p ON s.id_=p.course_id_
        WHERE s.teacher_id_=#{teacherId}
        AND s.type_='PRACTICE'
        AND s.status_ = 'ING'
        ORDER BY ABS(NOW() - s.start_time_) ASC
        limit 1
    </select>
    <select id="selectRecentCoursesLive" resultType="com.yonge.cooleshow.biz.dal.vo.StudentHomePage$RecentCourses"
            parameterType="java.lang.Long">
        SELECT
            u.id_ AS teacherId,
            u.username_ AS teacherName,
            u.real_name_ AS realName,
            u.avatar_ AS avatar,
            s.course_group_id_ AS courseGroupId,
            s.id_ AS courseId,
            g.name_ AS courseGroupName,
            s.type_ AS courseType,
            s.status_ AS `status`,
            s.start_time_ AS courseStartTime
        FROM course_schedule s
        LEFT JOIN sys_user u ON s.teacher_id_=u.id_
        LEFT JOIN course_group g ON s.course_group_id_=g.id_
        WHERE s.teacher_id_=#{teacherId}
        AND s.type_ IN ('LIVE','PIANO_ROOM_CLASS')
        AND s.status_ ='ING'
        ORDER BY ABS(NOW() - s.start_time_) ASC
        limit 1
    </select>
    <select id="selectWeekNotStartCourseSchedule" resultType="java.lang.Integer">
        select count(1)
        from course_schedule cs
        left join course_group cg on cs.course_group_id_ = cg.id_
        where cs.teacher_id_ = #{userId} and cs.status_ in ('NOT_START','ING') and cg.status_ = 'ING'
        and YEARWEEK(date_format(cs.class_date_,'%Y-%m-%d'),7) = YEARWEEK(now(),7)
    </select>

    <select id="selectHomeworkNotDecorate" resultType="java.lang.Integer">
        select count(1)
        from course_schedule cs
        left join course_homework ch on cs.id_ = ch.course_schedule_id_
        where cs.teacher_id_ = #{userId} and cs.status_ = 'COMPLETE' and cs.type_ in( 'PRACTICE','PIANO_ROOM_CLASS')
        and YEARWEEK(date_format(cs.class_date_,'%Y-%m-%d'),7) = YEARWEEK(now(),7)
        and ch.id_ is null
    </select>

    <select id="selectNotRepliedCourseSchedule" resultType="java.lang.Integer">
        select count(1)
        from course_schedule cs
        left join course_schedule_replied csr on cs.id_ = csr.course_schedule_id_
        where cs.teacher_id_ = #{userId} and cs.status_ = 'COMPLETE' and cs.type_ = 'PRACTICE'
        and csr.teacher_replied_ is null
    </select>

    <select id="selectWeekStudentRepliedCourseSchedule" resultType="java.lang.Integer">
        select count(1)
        from course_schedule cs
        left join course_schedule_replied csr on cs.id_ = csr.course_schedule_id_
        where cs.teacher_id_ = #{userId} and cs.status_ = 'COMPLETE' and cs.type_ = 'PRACTICE'
        and csr.student_replied_ is not null
        and YEARWEEK(date_format(csr.student_replied_time_,'%Y-%m-%d'),7) = YEARWEEK(now(),7)
    </select>
    <select id="selectStartTime" resultType="java.lang.String" parameterType="java.lang.String">
        SELECT s.start_time_
        FROM course_schedule_student_payment p
        LEFT JOIN course_schedule s ON p.course_id_=s.id_
        WHERE p.order_no_=#{orderNo}
    </select>
    <select id="selectTeacher" resultType="com.yonge.cooleshow.auth.api.entity.SysUser"
            parameterType="java.lang.String">
        SELECT DISTINCT
            s.teacher_id_ AS id,
            u.phone_ AS phone
        FROM course_schedule s
        LEFT JOIN sys_user u ON s.teacher_id_ = u.id_
        LEFT JOIN course_group g ON s.course_group_id_=g.id_
        WHERE lock_=0
        AND class_date_ = DATE_SUB(curdate(),INTERVAL -1 DAY)
        AND g.status_='ING'
    </select>
    <select id="selectTypeCount" resultType="com.yonge.cooleshow.biz.dal.vo.CountVo">
        SELECT
            s.type_ AS type,
            COUNT(1) AS `count`
        FROM course_schedule s
        LEFT JOIN course_group g ON s.course_group_id_=g.id_
        WHERE s.lock_=0
        AND s.teacher_id_=#{teacherId}
        AND s.class_date_=DATE_SUB(curdate(),INTERVAL -1 DAY)
        AND g.status_='ING'
        GROUP BY s.type_
    </select>


    <select id="selectTodayNotRepliedAndNotDecorateHomework"
            resultType="com.yonge.cooleshow.biz.dal.vo.TodayNotRepliedAndNotDecorateHomeworkVo">

        select sum(if(ch.id_ is null,1,0)) as decorateNum,
            sum(if(csr.teacher_replied_ is null,1,0)) as repliedNum,
            su.id_ as teacherId,
            su.phone_ as phone
        from course_schedule cs
        left join course_schedule_replied csr on cs.id_ = csr.course_schedule_id_
        left join course_homework ch on cs.id_ = ch.course_schedule_id_
        left join sys_user su on cs.teacher_id_ = su.id_
        where cs.status_ = 'COMPLETE' and date_format(cs.class_date_,'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')
        and cs.type_ = 'PRACTICE'
        group by  su.id_,su.phone_
    </select>

    <select id="countAllReplie" resultType="com.yonge.cooleshow.biz.dal.vo.RepliedCountVo"
            parameterType="java.lang.String">
        SELECT
            p.user_id_ AS userId,
            u.phone_ AS phone,
            COUNT(1) AS count
        FROM course_schedule_student_payment p
        LEFT JOIN sys_user u ON p.user_id_=u.id_
        WHERE p.course_id_ IN (SELECT id_ FROM course_schedule  WHERE class_date_=#{today} AND lock_=0 AND type_='PRACTICE')
        GROUP BY p.user_id_;
    </select>
    <select id="countReplies" resultType="java.lang.Integer" parameterType="java.lang.Integer">
        SELECT COUNT(1)
        FROM course_schedule_replied
        WHERE student_id_=#{studentId}
    </select>


    <select id="queryCourseHomeOfYear" parameterType="map"
            resultType="com.yonge.cooleshow.biz.dal.vo.CourseHomeVo$CourseHomeInfoVo">
        select `date`,
               sum(a.not_start_count) as undoneCount,
               sum(a.complete_count)  as doneCount
        from (select date_format(class_date_, '%Y-%m-01')                                      as `date`,
                     ifnull(case when status_ in ('NOT_START', 'ING') then count(1) end, 0) as not_start_count,
                     ifnull(case when status_ = 'COMPLETE' then count(1) end, 0)            as complete_count
              from course_schedule
              where type_ = #{param.type}
                and status_ in ('NOT_START', 'ING', 'COMPLETE')
        <![CDATA[ AND class_date_ >= #{param.startDate} ]]>
        <![CDATA[ AND class_date_ <= #{param.endDate} ]]>
        group by class_date_) as a
        group by date
    </select>

    <select id="queryCourseHomeOfMonth" parameterType="map"
            resultType="com.yonge.cooleshow.biz.dal.vo.CourseHomeVo$CourseHomeInfoVo">
        select class_date_                                                            as `date`,
               ifnull(case when status_ in ('NOT_START', 'ING') then count(1) end, 0) as undoneCount,
               ifnull(case when status_ = 'COMPLETE' then count(1) end, 0)            as doneCount
        from course_schedule
        where type_ = #{param.type}
          and status_ in ('NOT_START', 'ING', 'COMPLETE')
        <![CDATA[ AND class_date_ >= #{param.startDate} ]]>
        <![CDATA[ AND class_date_ <= #{param.endDate} ]]>
        group by class_date_
    </select>
    <select id="selectPrice" resultType="java.math.BigDecimal">
        SELECT p.subject_price_
        FROM teacher_free_time t
        LEFT JOIN teacher_subject_price p ON t.id_=p.teacher_free_time_id
        WHERE t.teacher_id_=#{teacherId}
        AND p.subject_id_=#{subjectId}
        AND t.default_flag_=1
    </select>
    <select id="selectSchedule" resultType="com.yonge.cooleshow.biz.dal.entity.CourseSchedule"
            parameterType="java.lang.Integer">
        SELECT * FROM course_schedule
        WHERE id_ IN (
            SELECT course_id_ FROM course_schedule_student_payment
            WHERE user_id_=(SELECT user_id_ FROM course_schedule_student_payment WHERE course_id_=#{courseId} AND course_type_='PRACTICE')
        )
    </select>
    <select id="teacherIdList" resultType="java.lang.Long">
        SELECT teacher_id_ FROM teacher_free_time WHERE enable_flag_=1 GROUP BY teacher_id_
    </select>
    <select id="selectIdList" resultType="java.lang.Long">
        SELECT id_ FROM course_schedule WHERE lock_=0 AND class_date_ &lt;= #{day}
    </select>
    <select id="selectComplete" resultType="com.yonge.cooleshow.biz.dal.vo.CourseCompleteVo">
        SELECT *
        FROM (
            SELECT course_group_id_ AS courseGroupId ,COUNT(1) AS courseCount
            FROM course_schedule
            WHERE lock_=0 AND status_='COMPLETE' GROUP BY course_group_id_) a
        LEFT JOIN course_group g ON g.id_=a.courseGroupId
    </select>
    <update id="updateStartTime">
        UPDATE course_schedule SET status_='ING' WHERE id_ IN(
        <foreach collection="list" item="item" index="index" open="" close="" separator=",">
            #{item.id}
        </foreach>)
    </update>
    <update id="updateEndTime">
        UPDATE course_schedule SET status_='COMPLETE' WHERE id_ IN(
        <foreach collection="list" item="item" index="index" open="" close="" separator=",">
            #{item.courseId}
        </foreach>)
    </update>
    <update id="updateTeacherSalary" parameterType="java.util.List">
        UPDATE course_schedule_teacher_salary
        SET status_='COMPLETE',settlement_time_=NOW()
        WHERE status_='NOT_START'
        AND course_schedule_id_ IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <select id="queryPianoClass" resultType="com.yonge.cooleshow.biz.dal.vo.PianoClassVo">
        SELECT DISTINCT
            s.id_ AS courseId,
            g.id_ AS courseGroupId,
            CONCAT(g.name_,'-第',s.class_num_,'课') AS courseGroupName,
            b.id_ AS subjectId,
            b.name_ AS subjectName,
            s.teacher_id_ AS teacherId,
            u.username_ AS teacherName,
            u.real_name_ AS teacherRealName,
            u.avatar_ AS avatar,
            s.status_ AS `status`,
            i.id_ AS imGroupId,
            s.start_time_ AS startTime,
            s.end_time_ AS endTime,
            CASE WHEN a.sign_in_time_ IS NOT NULL THEN 1 ELSE 0 END AS studentSign
        FROM course_schedule s
        LEFT JOIN sys_user u ON s.teacher_id_=u.id_
        LEFT JOIN course_group g ON s.course_group_id_=g.id_
        LEFT JOIN `subject` b ON g.subject_id_=b.id_
        LEFT JOIN course_schedule_student_payment p ON s.id_=p.course_id_
        LEFT JOIN student_attendance a ON s.id_=a.course_group_id_
        LEFT JOIN im_group i ON s.course_group_id_=i.course_group_id_
        WHERE s.type_='PIANO_ROOM_CLASS'
        AND p.user_id_=#{param.studentId}
        <if test="param.status !=null and param.status !=''">
            AND s.status_ = #{param.status}
        </if>
        <if test="param.subjectId !=null">
            AND b.id_ = #{param.subjectId}
        </if>
        <if test="param.classMonth !=null">
            <![CDATA[ AND s.class_date_ >= #{param.startDate} ]]>
            <![CDATA[ AND s.class_date_ <= #{param.endDate} ]]>
        </if>
        ORDER BY s.start_time_
    </select>
    <select id="selectWaitCourse" resultType="com.yonge.cooleshow.biz.dal.dto.UserAccountRecordDto">
        SELECT DISTINCT
            ts.teacher_id_ AS userId,
            ts.actual_salary_ AS transAmount,
            ts.course_group_type_ AS bizType,
            ts.course_schedule_id_ AS bizId,
            lr.room_title_ AS bizName,
            'IN' AS inOrOut,
            sp.order_no_ AS orderNo,
            ts.update_time_ AS updateTime
        FROM course_schedule_teacher_salary ts
        LEFT JOIN live_room lr ON ts.course_schedule_id_ = lr.course_id_
        LEFT JOIN course_schedule_student_payment sp ON ts.course_schedule_id_ = sp.course_id_
        WHERE ts.course_group_type_ = 'LIVE'
        AND ts.status_ = 'WAIT'
        <![CDATA[ AND DATE_FORMAT(ts.update_time_,'%Y-%m-%d') <= #{day}]]>
        UNION
        SELECT DISTINCT
            ts.teacher_id_ AS userId,
            ts.actual_salary_ AS transAmount,
            ts.course_group_type_ AS bizType,
            ts.course_schedule_id_ AS bizId,
            g.name_ AS bizName,
            'IN' AS inOrOut,
            sp.order_no_ AS orderNo,
            ts.update_time_ AS updateTime
        FROM course_schedule_teacher_salary ts
        LEFT JOIN course_group g ON ts.course_group_id_ = g.id_
        LEFT JOIN course_schedule_student_payment sp ON ts.course_schedule_id_ = sp.course_id_
        WHERE ts.course_group_type_ = 'PRACTICE'
        AND ts.status_ = 'WAIT'
        <![CDATA[ AND DATE_FORMAT(ts.update_time_,'%Y-%m-%d') <= #{day}]]>
    </select>
</mapper>