<?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.CourseGroupDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.CourseGroup">
        <id column="id_" jdbcType="BIGINT" property="id"/>
        <result column="type_" jdbcType="VARCHAR" property="type"/>
        <result column="teacher_id_" jdbcType="INTEGER" property="teacherId"/>
        <result column="name_" jdbcType="VARCHAR" property="name"/>
        <result column="subject_id_" jdbcType="INTEGER" property="subjectId"/>
        <result column="single_course_minutes_" jdbcType="INTEGER" property="singleCourseMinutes"/>
        <result column="course_num_" jdbcType="INTEGER" property="courseNum"/>
        <result column="complete_course_num_" jdbcType="INTEGER" property="completeCourseNum"/>
        <result column="course_introduce_" jdbcType="VARCHAR" property="courseIntroduce"/>
        <result column="course_price_" jdbcType="VARCHAR" property="coursePrice"/>
        <result column="status_" jdbcType="VARCHAR" property="status"/>
        <result column="sales_start_date_" jdbcType="TIMESTAMP" property="salesStartDate"/>
        <result column="sales_end_date_" jdbcType="TIMESTAMP" property="salesEndDate"/>
        <result column="background_pic_" jdbcType="VARCHAR" property="backgroundPic"/>
        <result column="mix_student_num_" jdbcType="INTEGER" property="mixStudentNum"/>
        <result column="course_start_time_" jdbcType="TIMESTAMP" property="courseStartTime"/>
        <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"/>
        <result column="pre_student_num_" jdbcType="INTEGER" property="preStudentNum"/>
        <result column="im_group_id_" jdbcType="VARCHAR" property="imGroupId"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_
        , type_, teacher_id_, name_, subject_id_, single_course_minutes_, course_num_, complete_course_num_, course_introduce_, course_price_, status_, sales_start_date_, sales_end_date_, background_pic_, mix_student_num_,pre_student_num_, im_group_id_, course_start_time_, 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.CourseGroup">
        insert into course_group(type_, teacher_id_, name_, subject_id_, single_course_minutes_, course_num_,
        complete_course_num_, course_introduce_, course_price_, status_, sales_start_date_, sales_end_date_, background_pic_,
        mix_student_num_,pre_student_num_,im_group_id_, course_start_time_, created_by_, created_time_, updated_by_, updated_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.type}, #{entity.teacherId}, #{entity.name}, #{entity.subjectId}, #{entity.singleCourseMinutes},
            #{entity.courseNum}, #{entity.completeCourseNum}, #{entity.courseIntroduce}, #{entity.coursePrice}, #{entity.status},
            #{entity.salesStartDate}, #{entity.salesEndDate}, #{entity.backgroundPic}, #{entity.mixStudentNum},
            #{entity.preStudentNum},#{entity.imGroupId},#{entity.courseStartTime}, #{entity.createdBy}, #{entity.createdTime}, #{entity.updatedBy},
            #{entity.updatedTime})
        </foreach>
    </insert>

    <select id="queryTeacherCourseGroup" resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupVo">
        select distinct
        b.id_                         as courseGroupId,
        b.name_                       as courseGroupName,
        s.name_                       as subjectName,
        b.teacher_id_                 as teacherId,
        u.username_                  as teacherName,
        b.course_start_time_          as courseStartTime,
        b.single_course_minutes_      as singleCourseMinutes,
        b.status_                     as `status`,
        ifnull(b.pre_student_num_, 0) as studentCount,
        b.background_pic_             as backgroundPic,
        b.course_price_               as coursePrice,
        b.course_num_                 as courseNum,
        u.avatar_ as avatar,
        b.sales_start_date_           as salesStartDate,
        b.sales_end_date_             as salesEndDate,
        b.mix_student_num_            as mixStudentNum,
        b.im_group_id_              as imGroupId,
        0  as existBuy
        from course_group as b
        left join subject as s on b.subject_id_ = s.id_
        left join sys_user as u on b.teacher_id_ = u.id_
        <where>
            u.del_flag_ = 0
            <if test="param.teacherId != null">
                and b.teacher_id_ = #{param.teacherId}
            </if>
            <if test="param.groupStatus != null">
                and b.status_ = #{param.groupStatus}
            </if>
            <if test="param.subjectId != null">
                AND b.subject_id_ = #{param.subjectId}
            </if>
            <if test="param.search != null and param.search !=''">
                and  (
                u.real_name_ like concat('%',#{param.search},'%')
                or b.name_ like concat('%',#{param.search},'%')
                )
            </if>
            <if test="param.type != null and param.type != ''">
                and b.type_ = #{param.type}
            </if>
        </where>
        order by b.created_time_
    </select>

    <select id="queryStudentCourseGroup" resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupVo">
        select distinct b.id_                         as courseGroupId,
                        b.name_                       as courseGroupName,
                        s.name_                       as subjectName,
                        b.teacher_id_                 as teacherId,
                        u.username_                   as teacherName,
                        b.course_start_time_          as courseStartTime,
                        b.single_course_minutes_      as singleCourseMinutes,
                        b.status_                     as `status`,
                        ifnull(b.pre_student_num_, 0) as studentCount,
                        b.background_pic_             as backgroundPic,
                        b.course_price_               as coursePrice,
                        b.course_num_                 as courseNum,
                        u.avatar_                     as avatar,
                        b.sales_start_date_           as salesStartDate,
                        b.sales_end_date_             as salesEndDate,
                        b.mix_student_num_            as mixStudentNum,
                        b.im_group_id_                as imGroupId,
                        if(y.status_ = 'PAID', 1, 0)  as existBuy
        from course_group as b
                 left join subject as s on b.subject_id_ = s.id_
                 left join sys_user as u on b.teacher_id_ = u.id_
                 left join
             (select x.user_id_, x.course_group_id_, o.status_
              from course_schedule_student_payment as x
                       left join user_order as o on x.order_no_ = o.order_no_ and x.user_id_ = o.user_id_
                       left join user_order_detail as d on d.order_no_ = x.order_no_
              where x.user_id_ = #{param.studentId}) as y on b.id_ = y.course_group_id_
        <where>
            u.del_flag_ = 0
            <if test="param.teacherId != null">
                and b.teacher_id_ = #{param.teacherId}
            </if>
            <if test="param.groupStatus != null">
                and b.status_ = #{param.groupStatus}
            </if>
            <if test="param.subjectId != null">
                AND b.subject_id_ = #{param.subjectId}
            </if>
            <if test="param.search != null and param.search !=''">
                and  (
                u.real_name_ like concat('%',#{param.search},'%')
                or b.name_ like concat('%',#{param.search},'%')
                )
            </if>
            <if test="param.auditVersion != null ">
                and #{param.auditVersion} = b.audit_version_
            </if>
            <if test="param.subjectId != null ">
                and #{param.subjectId} = b.subject_id_
            </if>
        </where>
    </select>

    <select id="selectAdminLivePage" resultType="com.yonge.cooleshow.biz.dal.vo.LiveCourseGroupVo">
        select distinct
        cg.id_ as courseGroupId,
        cg.name_ as name,
        cg.complete_course_num_ as endCourseNum,
        if(cg.complete_course_num_ = 0 ,cg.course_num_,cg.course_num_ - cg.complete_course_num_) as noStartCourseNum,
        cg.course_price_ as coursePrice,
        cg.pre_student_num_ as preStudentNum,
        cg.status_ as status,
        cg.course_num_ as courseNum,
        cg.created_time_ as createTime,
        cg.course_introduce_ as courseIntroduce,
        <if test="param.studentId != null">
            cssp.order_no_ as orderNo,
        </if>
        cg.im_group_id_  as imGroupId
        from course_group cg
        left join course_schedule_student_payment cssp on cg.id_ = cssp.course_group_id_
        left join sys_user su on su.id_ = cssp.user_id_
        <where>
            <if test="param.teacherId != null">
                and #{param.teacherId} = cg.teacher_id_
            </if>
            <if test="param.studentId != null">
                and #{param.studentId} = cssp.user_id_
            </if>
            <if test="param.search != null and param.search !=''">
                and  (
                    cg.id_ like concat('%',#{param.search},'%')
                    or su.id_ like concat('%',#{param.search},'%')
                    or su.username_ like concat('%',#{param.search},'%')
                    or su.phone_  like concat('%',#{param.search},'%')
                )
            </if>
            <if test="param.orderNo != null and param.orderNo != ''">
                and cssp.order_no_ like concat('%',#{param.orderNo},'%')
            </if>
            <if test="param.subjectId != null">
                and #{param.subjectId} = cg.subject_id_
            </if>
            <if test="param.status != null">
                and #{param.status} = cg.status_
            </if>
            <if test="param.startTime != null">
                and #{param.startTime} &lt;= cg.created_time_
            </if>
            <if test="param.endTime != null">
                and #{param.endTime} &gt;= cg.created_time_
            </if>
            <if test="param.courseType != null">
                and #{param.courseType} = cg.type_
            </if>
        </where>
        order by cg.id_ desc
    </select>

    <select id="selectCourseGroupSubject" resultType="com.yonge.cooleshow.biz.dal.vo.LiveCourseGroupVo">
        select
        cg.id_ as courseGroupId,
        s.name_ as subjectName
        from course_group cg
        left join subject s on cg.subject_id_ = s.id_
        <where>
            <if test="courseGroupIdList != null and courseGroupIdList.size() != 0">
                and cg.id_ in
                <foreach collection="courseGroupIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectAdminLiveStudentPage" resultType="com.yonge.cooleshow.biz.dal.vo.LiveCourseGroupStudentVo">
        select
        cssp.id_ as paymentId,
        su.id_ as studentId,
        su.username_ as studentName,
        sum(cssp.actual_price_) as price,
        cssp.order_no_ as orderNo,
        su.phone_ as phone,
        cssp.created_time_ as payTime
        from course_schedule_student_payment cssp
        left join sys_user su on cssp.user_id_ = su.id_
        <where>
            <if test="param.courseGroupId != null">
                and #{param.courseGroupId} = cssp.course_group_id_
            </if>
            <if test="param.search != null and param.search != ''">
                and (
                    cssp.course_group_id_ like concat('%',#{param.search},'%')
                    or su.id_ like concat('%',#{param.search},'%')
                    or su.username_ like concat('%',#{param.search},'%')
                    or su.phone_ like concat('%',#{param.search},'%')
                )
            </if>
            <if test="param.orderNo != null and param.orderNo != ''">
                and cssp.order_no_ like concat('%',#{param.orderNo},'%')
            </if>
            <if test="param.startTime != null">
                and #{param.startTime} &lt;= cssp.created_time_
            </if>
            <if test="param.endTime != null">
                and #{param.endTime} &gt;= cssp.created_time_
            </if>
        </where>
        group by cssp.user_id_
        order by  cssp.id_ desc
    </select>

    <select id="selectAdminLivePlan" resultType="com.yonge.cooleshow.biz.dal.vo.LiveCourseGroupPlanVo">
        select
        cs.id_ as courseId,
        cs.class_num_ as classNum,
        cs.class_date_ as classDate,
        cs.start_time_ as startTime,
        cs.end_time_ as endTime,
        cs.status_ as courseStatus,
        cp.plan_ as planInfo,
        lrv.url_ as videoUrl
        from course_schedule cs
        left join live_room_video lrv on cs.id_ = lrv.course_id_
        left join course_plan cp on cp.course_group_id_ = cs.course_group_id_ and cp.class_num_ = cs.class_num_
        <where>
            <if test="courseGroupId != null">
                and #{courseGroupId} = cs.course_group_id_
            </if>
        </where>
        order by  cs.class_num_
    </select>

    <select id="selectAdminLiveCourseGroup" resultType="com.yonge.cooleshow.biz.dal.vo.LiveCourseGroupVo">
        select distinct
            cg.id_ as courseGroupId,
            cg.name_ as name,
            cg.complete_course_num_ as endCourseNum,
            if(cg.complete_course_num_ = 0 ,cg.course_num_,cg.course_num_ - cg.complete_course_num_) as noStartCourseNum,
            cg.course_price_ as coursePrice,
            cg.pre_student_num_ as preStudentNum,
            cg.status_ as status,
            cg.course_num_ as courseNum,
            cg.created_time_ as createTime,
            cg.course_introduce_ as courseIntroduce,
            s.name_ as subjectName
        from course_group cg
         left join course_schedule_student_payment cssp on cg.id_ = cssp.course_group_id_
        left join subject s on cg.subject_id_ = s.id_
        <where>
            <if test="courseGroupId != null">
                and #{courseGroupId} = cg.id_
            </if>
        </where>
    </select>

    <select id="selectAdminLiveStudentCoursePage"
            resultType="com.yonge.cooleshow.biz.dal.vo.LiveCourseGroupStudentCourseVo">
        select distinct
        cs.id_ as courseId,
        cs.class_date_ as classDate,
        cs.start_time_ as startTime,
        cs.end_time_ as endTime,
        cs.status_ as courseStatue,
        cssp.order_no_ as orderNo,
        sa.id_ as studentAttendanceId
        from course_schedule cs
        left join course_schedule_student_payment cssp on cs.id_ = cssp.course_id_
        left join student_attendance sa on cssp.course_id_ = sa.course_schedule_id_
        <where>
            cs.status_ !=  '${@ com.yonge.cooleshow.biz.dal.enums.CourseScheduleEnum@CANCEL}'
            <if test="param.studentId != null">
                and cssp.user_id_ = #{param.studentId}
            </if>
            <if test="param.status != null">
                <choose>
                    <when test="param.status == @com.yonge.cooleshow.biz.dal.enums.StudentCourseEnum@TRUANT">
                        and sa.id_ is null
                    </when>
                    <when test="param.status == @com.yonge.cooleshow.biz.dal.enums.StudentCourseEnum@ATTENDCLASS">
                        and sa.id_ is not null
                        and #{param.studentId} = sa.student_id_
                    </when>
                    <when test="param.status == @com.yonge.cooleshow.biz.dal.enums.StudentCourseEnum@NOTSTART">
                        and cs.status_ = '${@ com.yonge.cooleshow.biz.dal.enums.CourseScheduleEnum@NOT_START}'
                    </when>
                </choose>
            </if>
            <if test="param.courseGroupId != null">
                and #{param.courseGroupId} = cs.course_group_id_
            </if>
            <if test="param.courseId != null">
                and cs.id_ like concat('%',#{param.courseId},'%')
            </if>

        </where>
        order by  cs.id_ desc
    </select>
    <select id="selectPracticeGroup" resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupPracticeVo">
        SELECT
            cg.id_ AS groupId,
            cg.name_ AS groupName,
            cg.subject_id_ AS subjectId,
            sub.name_ AS subjectName,
            cg.course_num_ AS courseNum,
            IFNULL(cs.count_ ,0)AS completeCount,
            cg.teacher_id_ AS teacherId,
            tu.username_ AS teacherName,
            tu.real_name_ AS teacherRealName,
            sp.user_id_ AS studentId,
            su.username_ AS studentName,
            su.real_name_ AS studentRealName,
            cg.status_ AS `status`,
            cst.start_time_ AS startTime
        FROM course_group cg
        LEFT JOIN `subject` sub ON cg.subject_id_=sub.id_
        LEFT JOIN sys_user tu ON cg.teacher_id_=tu.id_
        LEFT JOIN (SELECT user_id_,course_group_id_ FROM course_schedule_student_payment WHERE course_type_='PRACTICE' GROUP BY course_group_id_) sp ON cg.id_=sp.course_group_id_
        LEFT JOIN sys_user su ON sp.user_id_=su.id_
        LEFT JOIN (SELECT course_group_id_,COUNT(1) AS count_ FROM course_schedule WHERE type_='PRACTICE' AND status_='COMPLETE' GROUP BY course_group_id_) cs ON cg.id_=cs.course_group_id_
        LEFT JOIN (SELECT course_group_id_,start_time_ FROM course_schedule WHERE type_='PRACTICE' GROUP BY course_group_id_) cst ON cg.id_=cst.course_group_id_
        WHERE cg.type_='PRACTICE' AND cg.status_ IN('ING','COMPLETE')
        <if test="param.search != null and param.search != ''">
            AND (
            cg.id_ LIKE concat('%',#{param.search},'%') OR
            cg.teacher_id_ LIKE concat('%',#{param.search},'%') OR
            sp.user_id_ LIKE concat('%',#{param.search},'%') OR
            cg.name_ LIKE concat('%',#{param.search},'%') OR
            tu.username_ LIKE concat('%',#{param.search},'%') OR
            su.username_ LIKE concat('%',#{param.search},'%')
            )
        </if>
        <if test="param.subjectId != null">
            AND cg.subject_id_ = #{param.subjectId}
        </if>
        <if test="param.status != null and param.status != ''">
            AND cg.status_ = #{param.status}
        </if>
        <if test="param.studentId != null">
            AND sp.user_id_ = #{param.studentId}
        </if>
        <if test="param.teacherId != null">
            AND cg.teacher_id_ = #{param.teacherId}
        </if>
    </select>
    <select id="selectPracticeGroupDetail"
            resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupPracticeDetailVo">
        SELECT * FROM
        (SELECT
        cs.id_ AS courseId,
        sb.name_ AS subjectName,
        cs.teacher_id_ AS teacherId,
        tu.username_ AS teacherName,
        tu.real_name_ AS teacherRealName,
        sp.user_id_ AS studentId,
        su.username_ AS studentName,
        su.real_name_ AS studentRealName,
        cs.status_ AS `status`,
        cs.start_time_ AS startTime,
        cs.end_time_ AS endTime,
        (CASE WHEN cs.start_time_ &gt;= ta.sign_in_time_ THEN 1 ELSE 0 END) AS teacherInSign,
        (CASE WHEN cs.end_time_ &lt;= ta.sign_out_time_ THEN 1 ELSE 0 END) AS teacherOutSign,
        (CASE WHEN cs.start_time_ &gt;= sa.sign_in_time_ THEN 1 ELSE 0 END) AS studentInSign,
        (CASE WHEN cs.end_time_ &lt;= sa.sign_out_time_ THEN 1 ELSE 0 END) AS studentOutSign,
        ts.status_ AS salaryStatus
        FROM course_schedule cs
        LEFT JOIN course_group cg ON cs.course_group_id_=cg.id_
        LEFT JOIN `subject` sb ON cg.subject_id_=sb.id_
        LEFT JOIN sys_user tu ON cs.teacher_id_=tu.id_
        LEFT JOIN course_schedule_student_payment sp ON cs.id_=sp.course_id_
        LEFT JOIN sys_user su ON sp.user_id_=su.id_
        LEFT JOIN teacher_attendance ta ON cs.id_=ta.course_schedule_id_
        LEFT JOIN student_attendance sa ON cs.id_=sa.course_schedule_id_
        LEFT JOIN course_schedule_teacher_salary ts ON cs.id_=ts.course_schedule_id_
        WHERE cs.type_='PRACTICE'
        AND cs.lock_=0
        AND cs.course_group_id_=#{param.courseGroupId}) a
        <where>
            <if test="param.search != null and param.search != ''">
                AND (
                courseId LIKE concat('%',#{param.search},'%') OR
                teacherName LIKE concat('%',#{param.search},'%') OR
                teacherId LIKE concat('%',#{param.search},'%') OR
                studentName LIKE concat('%',#{param.search},'%') OR
                studentId LIKE concat('%',#{param.search},'%')
                )
            </if>
            <if test="param.teacherSign == 1">
                AND teacherInSign = #{param.teacherSign}
            </if>
            <if test="param.teacherSign == 0">
                AND teacherOutSign = #{param.teacherSign}
            </if>
            <if test="param.studentSign == 1">
                AND studentInSign = #{param.studentSign}
            </if>
            <if test="param.studentSign == 0">
                AND studentOutSign = #{param.studentSign}
            </if>
            <if test="param.status != null and param.status != ''">
                AND `status` = #{param.status}
            </if>
            <if test="param.salaryStatus != null and param.salaryStatus != ''">
                AND salaryStatus = #{param.salaryStatus}
            </if>
            <if test="param.startTime != null">
                AND startTime &lt;= #{param.startTime}
            </if>
            <if test="param.endTime != null">
                AND endTime &gt;= #{param.endTime}
            </if>
            <if test="param.studentId != null">
                AND studentId = #{param.studentId}
            </if>
            <if test="param.teacherId != null">
                AND teacherId = #{param.teacherId}
            </if>
        </where>
    </select>
    <select id="selectLiveGroup" resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupLiveVo">
        SELECT
            cg.id_ AS courseGroupId,
            cg.name_ AS courseGroupName,
            cg.subject_id_ AS subjectId,
            sb.name_ AS subjectName,
            cg.teacher_id_ AS teacherId,
            su.username_ AS teacherName,
            su.real_name_ AS teacherRealName,
            cg.course_num_ AS courseNum,
            cg.pre_student_num_ AS studentNum,
            cg.status_ AS `status`,
            IFNULL(cm.count_,0) AS completeCount,
            st.start_time_ AS startTime
        FROM course_group cg
        LEFT JOIN `subject` sb ON cg.subject_id_=sb.id_
        LEFT JOIN sys_user su ON su.id_=cg.teacher_id_
        LEFT JOIN (SELECT course_group_id_,COUNT(1) AS count_ FROM course_schedule WHERE type_='LIVE' AND status_='COMPLETE' GROUP BY course_group_id_) cm ON cg.id_=cm.course_group_id_
        LEFT JOIN (SELECT course_group_id_,start_time_ FROM course_schedule WHERE type_='LIVE' GROUP BY course_group_id_) st ON cg.id_=st.course_group_id_
        WHERE cg.type_='LIVE'
        <if test="param.search != null and param.search != ''">
            AND (
            cg.id_ LIKE concat('%',#{param.search},'%') OR
            cg.teacher_id_ LIKE concat('%',#{param.search},'%') OR
            cg.name_ LIKE concat('%',#{param.search},'%') OR
            su.username_ LIKE concat('%',#{param.search},'%')
            )
        </if>
        <if test="param.subjectId != null">
            AND cg.subject_id_ = #{param.subjectId}
        </if>
        <if test="param.status != null and param.status != ''">
            AND cg.status_ = #{param.status}
        </if>
    </select>
    <select id="selectLiveGroupStudent" resultType="com.yonge.cooleshow.biz.dal.vo.CourseSchedulePaymentVo">
        SELECT
            p.user_id_ AS studentId,
            u.username_ AS studentName,
            u.real_name_ AS studentRealName,
            u.phone_ AS studentPhone,
            p.actual_price_ AS actualPrice,
            p.order_no_ AS orderNo,
            o.create_time_ AS createdOrderTime
        FROM course_schedule_student_payment p
        LEFT JOIN sys_user u ON p.user_id_=u.id_
        LEFT JOIN user_order o ON p.order_no_ = o.order_no_
        WHERE p.course_group_id_=#{param.groupId}
        <if test="param.search != null and param.search != ''">
            AND (
            p.course_id_ LIKE concat('%',#{param.search},'%') OR
            p.user_id_ LIKE concat('%',#{param.search},'%') OR
            u.username_ LIKE concat('%',#{param.search},'%') OR
            u.phone_ LIKE concat('%',#{param.search},'%')
            )
        </if>
        <if test="param.orderNo != null and param.orderNo != ''">
            AND p.order_no_ = #{param.orderNo}
        </if>
        <if test="param.startTime != null">
            AND o.create_time_ &lt;= #{param.startTime}
        </if>
        <if test="param.endTime != null">
            AND o.create_time_ &gt;= #{param.endTime}
        </if>
    </select>
    <select id="selectLiveGroupPlan" resultType="com.yonge.cooleshow.biz.dal.vo.CourseSchedulePlanVo">
        SELECT
        distinct
            cs.id_ AS courseId,
            g.course_introduce_ AS courseIntroduce,
            cs.class_num_ AS classNum,
            cs.class_date_ AS classDate,
            cs.start_time_ AS startTime,
            cs.end_time_ AS endTime,
            ts.status_ AS salaryStatus,
            (select group_concat(lrv.url_) from live_room_video lrv where lrv.course_id_ = cs.id_)AS url,
            cs.status_ AS courseStatus,
            (CASE WHEN cs.start_time_ &gt;= ta.sign_in_time_ THEN 1 ELSE 0 END) AS teacherInSign,
            (CASE WHEN cs.end_time_ &lt;= ta.sign_out_time_ THEN 1 ELSE 0 END) AS teacherOutSign
        FROM course_schedule cs
        LEFT JOIN course_group g ON cs.course_group_id_=g.id_
        LEFT JOIN course_schedule_teacher_salary ts ON cs.id_=ts.course_schedule_id_
        LEFT JOIN teacher_attendance ta ON cs.id_=ta.course_schedule_id_
        WHERE cs.course_group_id_= #{param.groupId}
        <if test="param.salaryStatus != null and param.salaryStatus != ''">
            AND ts.status_ = #{param.salaryStatus}
        </if>
        <if test="param.startTime != null">
            AND cs.start_time_ &lt;= #{param.startTime}
        </if>
        <if test="param.endTime != null">
            AND cs.end_time_ &gt;= #{param.endTime}
        </if>
        <if test="param.endTime != null">
            AND cs.id_ LIKE concat('%',#{param.courseId},'%')
        </if>
    </select>
    <select id="selectVideoGroup" resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupVideoVo">
        SELECT
            g.id_ AS groupId,
            g.lesson_name_ AS lessonName,
            g.lesson_subject_ AS subjectId,
            s.name_ AS subjectName,
            g.lesson_count_ AS lessonCount,
            IFNULL(p.count_,0)  AS buyCount,
            g.teacher_id_ AS teacherId,
            u.username_ AS teacherName,
            u.real_name_ AS teacherRealName,
            g.create_time_ AS createTime,
            r.create_time_ AS recordTime
        FROM video_lesson_group g
        LEFT JOIN `subject` s ON g.lesson_subject_=s.id_
        LEFT JOIN sys_user u ON g.teacher_id_=u.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_) p ON g.id_=p.video_lesson_group_id_
        LEFT JOIN (
            SELECT video_group_id_,create_time_ FROM video_lesson_auth_record
            WHERE audit_status_='PASS') r ON g.id_=r.video_group_id_
        <where>
            <if test="param.search != null and param.search != ''">
                AND (
                g.id_ LIKE concat('%',#{param.search},'%') OR
                g.lesson_name_ LIKE concat('%',#{param.search},'%') OR
                g.teacher_id_ LIKE concat('%',#{param.search},'%') OR
                u.username_ LIKE concat('%',#{param.search},'%')
                )
            </if>
            <if test="param.subjectId != null and param.subjectId != ''">
                AND g.lesson_subject_ = #{param.subjectId}
            </if>
            <if test="param.status != null and param.status != ''">
                AND g.audit_status_ = #{param.status}
            </if>
        </where>
    </select>
    <select id="selectVideoGroupStudent" resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupStudentVo">
        SELECT
            r.student_id_ AS studentId,
            u.username_ AS studentName,
            u.real_name_ AS studentRealName,
            u.phone_ AS studentPhone,
            r.order_no_ AS orderNo,
            r.pay_money_ AS payMoney,
            o.create_time_ AS createTime
        FROM video_lesson_purchase_record r
        LEFT JOIN sys_user u ON r.student_id_=u.id_
        LEFT JOIN user_order o ON r.order_no_=o.order_no_
        WHERE r.order_status_='PAID'
        AND video_lesson_group_id_ = #{param.groupId}
        <if test="param.search != null and param.search != ''">
            AND (
            r.student_id_ LIKE concat('%',#{param.search},'%') OR
            u.username_ LIKE concat('%',#{param.search},'%') OR
            u.phone_ LIKE concat('%',#{param.search},'%')
            )
        </if>
        <if test="param.orderNo != null and param.orderNo != ''">
            AND r.order_no_ = #{param.orderNo}
        </if>
        <if test="param.startTime != null">
            AND o.create_time_ &lt;= #{param.startTime}
        </if>
        <if test="param.endTime != null">
            AND o.create_time_ &gt;= #{param.endTime}
        </if>
    </select>
    <select id="selectVideoGroupPlan" resultType="com.yonge.cooleshow.biz.dal.entity.VideoLessonGroupDetail">
        SELECT
            id_ AS id,
            video_title_ AS videoTitle,
            video_content_ AS videoContent,
            video_url_ AS videoUrl,
            cover_url_ AS coverUrl
        FROM video_lesson_group_detail
        WHERE video_lesson_group_id_=#{param.groupId}
    </select>
    <select id="selectGroupInfoVo" resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupInfoVo"
            parameterType="java.lang.Long">
        SELECT
            g.id_ AS id,
            g.name_ AS name,
            g.subject_id_ AS subjectId,
            s.name_ AS subjectName,
            g.course_num_ AS courseNum,
            g.course_price_ AS coursePrice,
            g.status_ AS `status`,
            g.course_introduce_ AS courseIntroduce,
            g.created_time_ AS createdTime
        FROM course_group g
        LEFT JOIN `subject` s ON g.subject_id_=s.id_
        WHERE g.id_ =#{groupId}
    </select>
    <update id="opsPreStudentNum">
        update course_group
        set
            pre_student_num_ = pre_student_num_ + #{num}
        where id_ = #{id}
    </update>
    <update id="updateBatch">
        UPDATE course_group
        SET status_='COMPLETE'
        WHERE id_ IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <select id="selectPianoGroup" resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupPianoVo">
        SELECT
        cg.id_ AS courseGroupId,
        cg.name_ AS courseGroupName,
        cg.subject_id_ AS subjectId,
        sb.name_ AS subjectName,
        cg.teacher_id_ AS teacherId,
        su.username_ AS teacherName,
        su.real_name_ AS teacherRealName,
        cg.course_num_ AS courseNum,
        cg.status_ AS `status`,
        IFNULL(cm.count_,0) AS completeCount,
        st.start_time_ AS startTime
        FROM course_group cg
        LEFT JOIN `subject` sb ON cg.subject_id_=sb.id_
        LEFT JOIN sys_user su ON su.id_=cg.teacher_id_
        LEFT JOIN (SELECT course_group_id_,COUNT(1) AS count_ FROM course_schedule WHERE type_='PIANO_ROOM_CLASS' AND status_='COMPLETE' GROUP BY course_group_id_) cm ON cg.id_=cm.course_group_id_
        LEFT JOIN (SELECT course_group_id_,start_time_ FROM course_schedule WHERE type_='PIANO_ROOM_CLASS' GROUP BY course_group_id_) st ON cg.id_=st.course_group_id_
        WHERE cg.type_='PIANO_ROOM_CLASS'
        <if test="param.search != null and param.search != ''">
            AND (
            cg.id_ LIKE concat('%',#{param.search},'%') OR
            cg.teacher_id_ LIKE concat('%',#{param.search},'%') OR
            cg.name_ LIKE concat('%',#{param.search},'%') OR
            su.username_ LIKE concat('%',#{param.search},'%')
            )
        </if>
        <if test="param.subjectId != null">
            AND cg.subject_id_ = #{param.subjectId}
        </if>
        <if test="param.status != null and param.status != ''">
            AND cg.status_ = #{param.status}
        </if>
    </select>
    <select id="selectPianoGroupDetail"
            resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupPianoDetailVo">
        SELECT
        cs.id_ AS courseId,
        sb.id_ AS subjectId,
        sb.name_ AS subjectName,
        cs.teacher_id_ AS teacherId,
        tu.username_ AS teacherName,
        tu.real_name_ AS teacherRealName,
        cs.status_ AS `status`,
        a.studentCount AS studentCount,
        (cs.single_course_time_ * (a.studentCount+1)*(a.studentCount+1-1)) AS courseTime,
        cs.start_time_ AS startTime,
        cs.end_time_ AS endTime,
        b.teacherInSign AS teacherInSign,
        b.teacherOutSign AS teacherOutSign
        FROM course_schedule cs
        LEFT JOIN course_group cg ON cs.course_group_id_ = cg.id_
        LEFT JOIN `subject` sb ON cg.subject_id_ = sb.id_
        LEFT JOIN sys_user tu ON cs.teacher_id_ = tu.id_
        LEFT JOIN (SELECT course_id_,COUNT(1) AS studentCount FROM course_schedule_student_payment WHERE course_type_='PIANO_ROOM_CLASS' GROUP BY course_id_) a ON cs.id_=a.course_id_
        LEFT JOIN (SELECT cs.id_ AS cid,( CASE WHEN cs.start_time_ &gt;= ta.sign_in_time_ THEN 1 ELSE 0 END ) AS teacherInSign,( CASE WHEN cs.end_time_ &lt;= ta.sign_out_time_ THEN 1 ELSE 0 END ) AS teacherOutSign
        FROM course_schedule cs LEFT JOIN teacher_attendance ta ON cs.id_=ta.course_schedule_id_ WHERE cs.type_='PIANO_ROOM_CLASS') b ON cs.id_=b.cid
        WHERE cs.type_='PIANO_ROOM_CLASS'
        AND cs.course_group_id_=#{param.courseGroupId}
        <if test="param.search != null and param.search != ''">
            AND cs.id_ LIKE concat('%',#{param.search},'%')
        </if>
        <if test="param.status != null and param.status != ''">
            AND cs.status_ = #{param.status}
        </if>
        <if test="param.startTime != null">
            AND cs.start_time_ &gt;= #{param.startTime}
        </if>
        <if test="param.endTime != null">
            AND cs.start_time_ &lt;= #{param.endTime}
        </if>
        <if test="param.teacherSign == 1">
            AND b.teacherInSign = #{param.teacherSign}
        </if>
        <if test="param.teacherSign == 0">
            AND b.teacherOutSign = #{param.teacherSign}
        </if>
        <if test="param.studentSign != null">
            AND cs.id_ IN
            <foreach collection="param.courseIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>
    <select id="studentSign" resultType="com.yonge.cooleshow.biz.dal.vo.StudentSignVo">
        SELECT DISTINCT
        p.course_id_ AS courseId,
        p.user_id_ AS userId,
        u.username_ AS userName,
        u.real_name_ AS realName,
        ( CASE WHEN c.start_time_ &gt;= a.sign_in_time_ THEN 1 ELSE 0 END ) AS studentInSign,
        ( CASE WHEN c.end_time_ &lt;= a.sign_out_time_ THEN 1 ELSE 0 END ) AS studentOutSign
        FROM course_schedule_student_payment p
        LEFT JOIN sys_user u ON p.user_id_=u.id_
        LEFT JOIN course_schedule c ON p.course_id_=c.id_
        LEFT JOIN student_attendance a ON p.user_id_=a.student_id_
        <where>
            <if test="courseId !=null">
                AND p.course_id_=#{courseId}
            </if>
            <if test="type !=null and type!=''">
                AND p.course_type_=#{type}
            </if>
        </where>
    </select>
    <select id="selectPianoGroupStudent" resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupPianoVo">
        SELECT DISTINCT
            g.id_ AS courseGroupId,
            g.name_ AS courseGroupName,
            s.id_ AS subjectId,
            s.name_ AS subjectName,
            g.teacher_id_ AS teacherId,
            u.username_ AS teacherName,
            u.real_name_ AS teacherRealName,
            g.course_num_ AS courseNum,
            IFNULL(cm.count_,0) AS completeCount,
            g.status_ AS `status`,
            st.start_time_ AS startTime
        FROM course_schedule_student_payment p
        LEFT JOIN course_group g ON p.course_group_id_=g.id_
        LEFT JOIN `subject` s ON g.subject_id_=s.id_
        LEFT JOIN sys_user u ON g.teacher_id_=u.id_
        LEFT JOIN (SELECT course_group_id_,COUNT(1) AS count_ FROM course_schedule WHERE type_='PIANO_ROOM_CLASS' AND status_='COMPLETE' GROUP BY course_group_id_) cm ON p.course_group_id_=cm.course_group_id_
        LEFT JOIN (SELECT course_group_id_,start_time_ FROM course_schedule WHERE type_='PIANO_ROOM_CLASS' GROUP BY course_group_id_) st ON p.course_group_id_=st.course_group_id_
        WHERE p.course_type_='PIANO_ROOM_CLASS'
        AND p.user_id_=#{param.studentId}
        <if test="param.search != null and param.search != ''">
            AND (
            g.id_ LIKE concat('%',#{param.search},'%') OR
            g.teacher_id_ LIKE concat('%',#{param.search},'%') OR
            u.username_ LIKE concat('%',#{param.search},'%') OR
            g.name_ LIKE concat('%',#{param.search},'%')
            )
        </if>
        <if test="param.subjectId != null">
            AND s.id_ = #{param.subjectId}
        </if>
        <if test="param.status != null and param.status != ''">
            AND g.status_ = #{param.status}
        </if>
    </select>
    <select id="selectPianoGroupStudentDetail"
            resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupPianoDetailVo">
        SELECT * FROM (
        SELECT
        cs.id_ AS courseId,
        sb.id_ AS subjectId,
        sb.name_ AS subjectName,
        cs.teacher_id_ AS teacherId,
        tu.username_ AS teacherName,
        tu.real_name_ AS teacherRealName,
        cs.status_ AS `status`,
        a.studentCount AS studentCount,
        (cs.single_course_time_ * (a.studentCount+1)*(a.studentCount+1-1)) AS courseTime,
        cs.start_time_ AS startTime,
        cs.end_time_ AS endTime,
        p.user_id_ AS studentId,
        su.username_ AS studentName,
        su.real_name_ AS studentRealName,
        (CASE WHEN cs.start_time_ &gt;= ta.sign_in_time_ THEN 1 ELSE 0 END) AS teacherInSign,
        (CASE WHEN cs.end_time_ &lt;= ta.sign_out_time_ THEN 1 ELSE 0 END) AS teacherOutSign,
        (CASE WHEN cs.start_time_ &gt;= sa.sign_in_time_ THEN 1 ELSE 0 END) AS studentInSign,
        (CASE WHEN cs.end_time_ &lt;= sa.sign_out_time_ THEN 1 ELSE 0 END) AS studentOutSign,
        (CASE WHEN cs.start_time_ &gt;= sa.sign_in_time_ AND cs.end_time_ &lt;= sa.sign_out_time_ THEN 1 ELSE 0 END) AS studentSign,
        (CASE WHEN cs.start_time_ &gt;= ta.sign_in_time_ AND cs.end_time_ &lt;= ta.sign_out_time_ THEN 1 ELSE 0 END) AS teacherSign
        FROM course_schedule cs
        LEFT JOIN course_group cg ON cs.course_group_id_ = cg.id_
        LEFT JOIN `subject` sb ON cg.subject_id_ = sb.id_
        LEFT JOIN sys_user tu ON cs.teacher_id_ = tu.id_
        LEFT JOIN (SELECT course_id_,COUNT(1) AS studentCount FROM course_schedule_student_payment WHERE course_type_='PIANO_ROOM_CLASS' GROUP BY course_id_) a ON cs.id_=a.course_id_
        LEFT JOIN teacher_attendance ta ON cs.id_=ta.course_schedule_id_
        LEFT JOIN student_attendance sa ON cs.id_=sa.course_schedule_id_
        LEFT JOIN course_schedule_student_payment p ON cs.id_=p.course_id_
        LEFT JOIN sys_user su ON p.user_id_ = su.id_
        WHERE cs.type_='PIANO_ROOM_CLASS'
        AND p.user_id_=#{param.studentId}
        AND p.course_group_id_=#{param.courseGroupId}) m
        <where>
        <if test="param.search != null and param.search != ''">
            AND courseId LIKE concat('%',#{param.search},'%')
        </if>
        <if test="param.studentSign != null">
            AND studentSign = #{param.studentSign}
        </if>
        <if test="param.teacherSign != null">
            AND teacherSign = #{param.teacherSign}
        </if>
        <if test="param.status != null and param.status != ''">
            AND `status` = #{param.status}
        </if>
        <if test="param.startTime != null">
            AND startTime &gt;= #{param.startTime}
        </if>
        <if test="param.endTime != null">
            AND startTime &lt;= #{param.endTime}
        </if>
        </where>
    </select>
    <select id="selectPianoGroupTeacher" resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupPianoVo">
        SELECT
        cg.id_ AS courseGroupId,
        cg.name_ AS courseGroupName,
        cg.subject_id_ AS subjectId,
        sb.name_ AS subjectName,
        cg.teacher_id_ AS teacherId,
        su.username_ AS teacherName,
        su.real_name_ AS teacherRealName,
        cg.course_num_ AS courseNum,
        cg.status_ AS `status`,
        IFNULL(cm.count_,0) AS completeCount,
        st.start_time_ AS startTime
        FROM course_group cg
        LEFT JOIN `subject` sb ON cg.subject_id_=sb.id_
        LEFT JOIN sys_user su ON su.id_=cg.teacher_id_
        LEFT JOIN (SELECT course_group_id_,COUNT(1) AS count_ FROM course_schedule WHERE type_='PIANO_ROOM_CLASS' AND status_='COMPLETE' GROUP BY course_group_id_) cm ON cg.id_=cm.course_group_id_
        LEFT JOIN (SELECT course_group_id_,start_time_ FROM course_schedule WHERE type_='PIANO_ROOM_CLASS' GROUP BY course_group_id_) st ON cg.id_=st.course_group_id_
        WHERE cg.type_='PIANO_ROOM_CLASS'
        AND cg.teacher_id_=#{param.teacherId}
        <if test="param.search != null and param.search != ''">
            AND (
            cg.name_ LIKE concat('%',#{param.search},'%') OR
            cg.id_ LIKE concat('%',#{param.search},'%')
            )
        </if>
        <if test="param.subjectId != null">
            AND cg.subject_id_ = #{param.subjectId}
        </if>
        <if test="param.status != null and param.status != ''">
            AND cg.status_ = #{param.status}
        </if>
    </select>
    <select id="selectPianoGroupTeacherDetail"
            resultType="com.yonge.cooleshow.biz.dal.vo.CourseGroupPianoDetailVo">
        SELECT * FROM (
        SELECT
        cs.id_ AS courseId,
        sb.id_ AS subjectId,
        sb.name_ AS subjectName,
        cs.teacher_id_ AS teacherId,
        tu.username_ AS teacherName,
        tu.real_name_ AS teacherRealName,
        cs.status_ AS `status`,
        a.studentCount AS studentCount,
        (cs.single_course_time_ * (a.studentCount+1)*(a.studentCount+1-1)) AS courseTime,
        cs.start_time_ AS startTime,
        cs.end_time_ AS endTime,
        p.user_id_ AS studentId,
        su.username_ AS studentName,
        su.real_name_ AS studentRealName,
        (CASE WHEN cs.start_time_ &gt;= ta.sign_in_time_ THEN 1 ELSE 0 END) AS teacherInSign,
        (CASE WHEN cs.end_time_ &lt;= ta.sign_out_time_ THEN 1 ELSE 0 END) AS teacherOutSign,
        (CASE WHEN cs.start_time_ &gt;= ta.sign_in_time_ AND cs.end_time_ &lt;= ta.sign_out_time_ THEN 1 ELSE 0 END) AS teacherSign
        FROM course_schedule cs
        LEFT JOIN course_group cg ON cs.course_group_id_ = cg.id_
        LEFT JOIN `subject` sb ON cg.subject_id_ = sb.id_
        LEFT JOIN sys_user tu ON cs.teacher_id_ = tu.id_
        LEFT JOIN (SELECT course_id_,COUNT(1) AS studentCount FROM course_schedule_student_payment WHERE course_type_='PIANO_ROOM_CLASS' GROUP BY course_id_) a ON cs.id_=a.course_id_
        LEFT JOIN teacher_attendance ta ON cs.id_=ta.course_schedule_id_
        LEFT JOIN student_attendance sa ON cs.id_=sa.course_schedule_id_
        LEFT JOIN course_schedule_student_payment p ON cs.id_=p.course_id_
        LEFT JOIN sys_user su ON cs.teacher_id_ = su.id_
        WHERE cs.type_='PIANO_ROOM_CLASS'
        AND cs.teacher_id_=#{param.teacherId}
        AND p.course_group_id_=#{param.courseGroupId}
        <if test="param.studentSign != null">
            AND cs.id_ IN
            <foreach collection="param.courseIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>) m
        <where>
            <if test="param.search != null and param.search != ''">
                AND (
                courseId LIKE concat('%',#{param.search},'%') OR
                studentName LIKE concat('%',#{param.search},'%') OR
                studentId LIKE concat('%',#{param.search},'%')
                )
            </if>
            <if test="param.teacherSign != null">
                AND teacherSign = #{param.teacherSign}
            </if>
            <if test="param.status != null and param.status != ''">
                AND `status` = #{param.status}
            </if>
            <if test="param.startTime != null">
                AND startTime &gt;= #{param.startTime}
            </if>
            <if test="param.endTime != null">
                AND endTime &lt;= #{param.endTime}
            </if>
        </where>
        GROUP BY courseId
    </select>
</mapper>
