<?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.VideoLessonGroupDao">
	<resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.VideoLessonGroup">
		<id column="id_" jdbcType="BIGINT" property="id" />
		<result column="lesson_name_" jdbcType="VARCHAR" property="lessonName" />
		<result column="lesson_subject_" jdbcType="VARCHAR" property="lessonSubject" />
		<result column="lesson_desc_" jdbcType="VARCHAR" property="lessonDesc" />
		<result column="lesson_price_" jdbcType="INTEGER" property="lessonPrice" />
		<result column="lesson_cover_url_" jdbcType="VARCHAR" property="lessonCoverUrl" />
		<result column="teacher_id_" jdbcType="BIGINT" property="teacherId" />
		<result column="lesson_count_" jdbcType="INTEGER" property="lessonCount" />
		<result column="sort_number_" jdbcType="INTEGER" property="sortNumber" />
		<result column="lesson_tag_" jdbcType="VARCHAR" property="lessonTag" />
		<result column="top_flag_" jdbcType="TINYINT" property="topFlag" />
		<result column="hot_flag_" jdbcType="TINYINT" property="hotFlag" />
		<result column="shelves_flag_" jdbcType="TINYINT" property="shelvesFlag" />
		<result column="shelves_time_" property="shelvesTime" />
		<result column="shelves_reason_" property="shelvesReason" />
		<result column="shelves_id_" property="shelvesId" />
		<result column="virtual_number_" property="virtualNumber" />
		<result column="audit_status_" jdbcType="TINYINT" property="auditStatus" />
		<result column="audit_id_" jdbcType="INTEGER" property="auditId" />
		<result column="audit_name_" jdbcType="VARCHAR" property="auditName" />
		<result column="create_time_" jdbcType="TIMESTAMP" property="createTime" />
		<result column="update_time_" jdbcType="TIMESTAMP" property="updateTime" />
	</resultMap>

	<sql id="baseColumns">
		g.id_ AS id,
		g.lesson_name_ AS lessonName,
		g.lesson_subject_ AS lessonSubject,
		g.lesson_desc_ AS lessonDesc,
		g.lesson_price_ AS lessonPrice,
		g.lesson_cover_url_ AS lessonCoverUrl,
		g.teacher_id_ AS teacherId,
		g.lesson_count_ AS lessonCount,
		g.sort_number_ AS sortNumber,
		g.lesson_tag_ AS lessonTag,
		g.top_flag_ AS topFlag,
		g.hot_flag_ AS hotFlag,
		g.shelves_flag_ AS shelvesFlag,
		g.shelves_time_ as shelvesTime,
		g.shelves_reason_ as shelvesReason,
		g.audit_status_ AS auditStatus,
		g.audit_version_ as auditVersion,
		g.shelves_id_ as shelvesId,
		g.create_time_ AS createTime,
		g.update_time_ AS updateTime,
		g.audit_id_ AS auditId,
		g.audit_name_ AS auditName,
		g.relation_type_ AS relationType,
		g.virtual_number_ as virtualNumber,
	</sql>
	<sql id="recordColumns">
		g.id_ AS id,
		g.lesson_name_ AS lessonName,
		g.lesson_subject_ AS lessonSubject,
		g.lesson_desc_ AS lessonDesc,
		g.lesson_price_ AS lessonPrice,
		g.lesson_cover_url_ AS lessonCoverUrl,
		g.teacher_id_ AS teacherId,
		g.lesson_count_ AS lessonCount,
		g.sort_number_ AS sortNumber,
		g.lesson_tag_ AS lessonTag,
		g.top_flag_ AS topFlag,
		g.hot_flag_ AS hotFlag,
		g.shelves_flag_ AS shelvesFlag,
		g.shelves_time_ as shelvesTime,
		g.shelves_reason_ as shelvesReason,
		g.create_time_ AS createTime,
		g.update_time_ AS updateTime,
		r.audit_id_ AS auditId,
		r.audit_name_ AS auditName,
		r.audit_status_ AS auditStatus,
		g.audit_version_ as auditVersion,
		g.virtual_number_ as virtualNumber,
	</sql>

	<update id="updateGroup" parameterType="com.yonge.cooleshow.biz.dal.vo.VideoLessonGroupUpVo">
		UPDATE video_lesson_group
		<set>
			<if test="lessonName !=null and lessonName !=''">lesson_name_ = #{lessonName},</if>
			<if test="lessonSubject !=null and lessonSubject !=''">lesson_subject_ = #{lessonSubject},</if>
			<if test="lessonDesc !=null and lessonDesc !=''">lesson_desc_ = #{lessonDesc},</if>
			<if test="lessonPrice !=null and lessonPrice !=''">lesson_price_ = #{lessonPrice},</if>
			<if test="lessonCoverUrl !=null and lessonCoverUrl !=''">lesson_cover_url_ = #{lessonCoverUrl},</if>
			<if test="sortNumber !=null">sort_number_ = #{sortNumber},</if>
			<if test="lessonTag !=null and lessonTag !=''">lesson_tag_ = #{lessonTag},</if>
			<if test="topFlag !=null">top_flag_ = #{topFlag},</if>
			<if test="hotFlag !=null">hot_flag_ = #{hotFlag},</if>
			<if test="auditStatus !=null">audit_status_ = #{auditStatus},</if>
			update_time_ = SYSDATE(),
			lesson_count_ = (SELECT COUNT(1) FROM video_lesson_group_detail WHERE video_lesson_group_id_ = #{id})
		</set>
		WHERE id_ = #{id}
	</update>
	<update id="updateExamine" parameterType="com.yonge.cooleshow.biz.dal.vo.VideoLessonExamineVo">
		UPDATE video_lesson_group
		<set>
			<if test="shelvesFlag !=null">shelves_flag_ = #{shelvesFlag},</if>
			<if test="auditStatus !=null and auditStatus !=''">audit_status_ = #{auditStatus},</if>
			<if test="auditName !=null and auditName!=''">audit_name_ = #{auditName},</if>
			<if test="auditId !=null">audit_id_ = #{auditId},</if>
			<if test="shelvesTime !=null">shelves_time_ = #{shelvesTime},</if>
			<if test="remarks !=null and remarks !=''">remarks = #{remarks},</if>
			update_time_ = SYSDATE()
		</set>
		WHERE id_ = #{id}
	</update>

	<update id="updateShelves">
		UPDATE video_lesson_group
		<set>
			<if test="shelvesFlag !=null">shelves_flag_ = #{shelvesFlag},</if>
			<if test="shelvesTime !=null">shelves_time_ = #{shelvesTime},</if>
			<if test="shelvesReason !=null and shelvesReason!=''">shelves_reason_ = #{shelvesReason},</if>
			<if test="shelvesId !=null">shelves_id_ = #{shelvesId},</if>
			update_time_ = SYSDATE()
		</set>
		WHERE id_ = #{id}
	</update>
	<select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.VideoLessonGroupVo">
		SELECT
			<include refid="baseColumns"/>
			b.name_ AS lessonSubjectName,
			s.username_ AS username,
			s.avatar_ AS avatar,
		    s.real_name_ AS realName,
			(SELECT COUNT(1) FROM video_lesson_purchase_record r WHERE r.video_lesson_group_id_=g.id_ AND r.order_status_='PAID') AS countStudent
		FROM video_lesson_group g
		LEFT JOIN sys_user s ON g.teacher_id_ = s.id_
		LEFT JOIN `subject` b ON g.lesson_subject_=b.id_
		<where>
			<if test="param.shelvesFlag !=null">
				AND g.shelves_flag_ = #{param.shelvesFlag}
			</if>
			<if test="param.auditStatus !=null">
				AND g.audit_status_ = #{param.auditStatus}
			</if>
			<if test="param.groupId !=null">
				AND g.id_ = #{param.groupId}
			</if>
			<if test="param.teacherId !=null">
				AND g.teacher_id_ = #{param.teacherId}
			</if>
			<if test="param.auditVersion != null">
				AND g.audit_version_ = #{param.auditVersion}
			</if>
			<if	test="param.lessonSubject != null and param.lessonSubject != ''">
				AND g.lesson_subject_ = #{param.lessonSubject}
			</if>
			<if test="null != param.search and '' != param.search">
				AND (
				g.lesson_name_ LIKE CONCAT('%', #{param.search}, '%') OR
				s.username_ LIKE CONCAT('%', #{param.search}, '%')
				)
			</if>
		</where>
		ORDER BY g.top_flag_ desc,g.sort_number_ desc, countStudent DESC,g.id_ desc
	</select>
	<select id="selectStudentPage" resultType="com.yonge.cooleshow.biz.dal.vo.VideoLessonStudentDetailVo">
		SELECT
			g.id_ AS groupId,
			g.lesson_name_ AS lessonName,
			g.lesson_subject_ AS lessonSubject,
			j.name_ AS lessonSubjectName,
			g.lesson_count_ AS lessonCount,
			p.pay_money_ AS payMoney,
			p.order_no_ AS orderNo,
			p.purchase_time_ AS purchaseTime,
			g.audit_version_ as auditVersion
		FROM video_lesson_group g
		LEFT JOIN video_lesson_purchase_record p ON g.id_ = p.video_lesson_group_id_
		LEFT JOIN sys_user s ON g.teacher_id_ = s.id_
		LEFT JOIN subject j ON g.lesson_subject_ = j.id_
		<where>
			<if test="param.userId !=null">
				AND p.student_id_ = #{param.userId}
			</if>
			<if test="null != param.search and '' != param.search">
				AND (
				s.id_ LIKE CONCAT('%', #{param.search}, '%') OR
				s.username_ LIKE CONCAT('%', #{param.search}, '%') OR
				s.phone_ LIKE CONCAT('%', #{param.search}, '%') OR
				g.id_ LIKE CONCAT('%', #{param.search}, '%')
				)
			</if>
			<if test="null != param.orderNo and '' != param.orderNo">
				AND p.order_no_ = #{param.orderNo}
			</if>
			<if test="null != param.lessonSubject and '' != param.lessonSubject">
				AND g.lesson_subject_ = #{param.lessonSubject}
			</if>
			<if test="param.shelvesFlag !=null">
				AND g.shelves_flag_ = #{param.shelvesFlag}
			</if>
			<if test="param.auditStatus !=null">
				AND g.audit_status_ = #{param.auditStatus}
			</if>
			<if test="param.startTime !=null">
				<![CDATA[AND p.purchase_time_ >= #{param.startTime} ]]>
			</if>
			<if test="param.endTime !=null">
				<![CDATA[AND p.purchase_time_ <= #{param.endTime} ]]>
			</if>
		</where>
	</select>
	<select id="selectTeacherPage" resultType="com.yonge.cooleshow.biz.dal.vo.VideoLessonTeacherVo">
		SELECT
			g.id_ AS groupId,
			g.lesson_name_ AS lessonName,
			g.lesson_subject_ AS lessonSubject,
			j.name_ AS lessonSubjectName,
			g.lesson_count_ AS lessonCount,
			g.lesson_price_ AS lessonPrice,
			g.audit_status_ AS auditStatus,
			(SELECT COUNT(1) FROM video_lesson_purchase_record r WHERE r.video_lesson_group_id_=g.id_ AND r.order_status_='PAID') AS countStudent,
			g.create_time_ AS createTime,
			g.lesson_desc_ AS lessonDesc,
		g.virtual_number_ as virtualNumber
		FROM video_lesson_group g
		LEFT JOIN subject j ON g.lesson_subject_ = j.id_
		<where>
			<if test="param.userId !=null">
				AND g.teacher_id_ = #{param.userId}
			</if>
			<if test="null != param.search and '' != param.search">
				AND (
				g.id_ LIKE CONCAT('%', #{param.search}, '%') OR
				g.lesson_name_ LIKE CONCAT('%', #{param.search}, '%')
				)
			</if>
			<if test="null != param.lessonSubject and '' != param.lessonSubject">
				AND g.lesson_subject_ = #{param.lessonSubject}
			</if>
			<if test="param.auditStatus !=null">
				AND g.audit_status_ = #{param.auditStatus}
			</if>
			<if test="param.shelvesFlag !=null">
				AND g.shelves_flag_ = #{param.shelvesFlag}
			</if>
		</where>
	</select>
	<select id="selectPurchaseStudent" resultType="com.yonge.cooleshow.biz.dal.vo.VideoLessonPurchaseVo">
		SELECT
			p.student_id_ AS studentId,
			u.username_ AS username,
			u.real_name_ AS realName,
		    u.phone_ AS phone,
			p.pay_money_ AS payMoney,
			p.order_no_ AS orderNo,
			o.pay_time_ AS purchaseTime
		FROM video_lesson_purchase_record p
		LEFT JOIN sys_user u ON p.student_id_ = u.id_
		LEFT JOIN user_order o ON p.order_no_ = o.order_no_
		WHERE p.order_status_='PAID'
			<if test="param.videoLessonGroupId !=null">
				AND p.video_lesson_group_id_ = #{param.videoLessonGroupId}
			</if>
			<if test="null != param.search and '' != param.search">
				AND (
				u.id_ LIKE CONCAT('%', #{param.search}, '%') OR
				u.username_ LIKE CONCAT('%', #{param.search}, '%') OR
				u.phone_ LIKE CONCAT('%', #{param.search}, '%')
				)
			</if>
			<if test="null != param.orderNo and '' != param.orderNo">
				AND p.order_no_ = #{param.orderNo}
			</if>
			<if test="param.startTime !=null">
				<![CDATA[AND p.purchase_time_ >= #{param.startTime} ]]>
			</if>
			<if test="param.endTime !=null">
				<![CDATA[AND p.purchase_time_ <= #{param.endTime} ]]>
			</if>
	</select>
	<select id="selectLessonGroup" resultType="com.yonge.cooleshow.biz.dal.vo.LessonGroupVo">
		SELECT
			g.id_ AS id,
			g.lesson_name_ AS lessonName,
			g.lesson_subject_ AS lessonSubject,
			s.name_ AS lessonSubjectName,
			g.lesson_desc_ AS lessonDesc,
			g.lesson_price_ AS lessonPrice,
			g.lesson_cover_url_ AS lessonCoverUrl,
			g.lesson_count_ AS lessonCount,
			g.teacher_id_ AS teacherId,
			g.relation_type_ AS relationType,
			u.username_ AS userName,
			u.real_name_ AS realName,
			u.avatar_ AS avatar,
			g.virtual_number_ as virtualNumber,
			g.audit_version_ as auditVersion,
			(SELECT COUNT(1) FROM video_lesson_purchase_record r WHERE r.video_lesson_group_id_=g.id_ AND r.order_status_='PAID') AS countStudent
		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_
		<where>
			AND  u.del_flag_ = 0
			<if test="null != param.search and '' != param.search">
				AND (
				u.username_ LIKE CONCAT('%', #{param.search}, '%') OR
				g.lesson_name_ LIKE CONCAT('%', #{param.search}, '%')
				)
			</if>
			<if test="param.shelvesFlag !=null">
				AND g.shelves_flag_ = #{param.shelvesFlag}
			</if>
			<if test="param.auditStatus !=null">
				AND g.audit_status_ = #{param.auditStatus}
			</if>
			<if	test="param.studentId != null">
				AND g.id_ IN
				(SELECT video_lesson_group_id_ FROM video_lesson_purchase_record WHERE student_id_=#{param.studentId} AND order_status_='PAID')
			</if>
			<if	test="param.teacherId != null">
				AND g.teacher_id_ = #{param.teacherId}
			</if>
			<if test="param.auditVersion != null">
				AND g.audit_version_ = #{param.auditVersion}
			</if>
			<if test="param.lessonSubject != null">
				AND s.id_ = #{param.lessonSubject}
			</if>
		</where>
		ORDER BY g.top_flag_ desc,g.sort_number_ desc, countStudent DESC,g.id_ desc
	</select>
	
	<select id="queryMyLessonGroup" resultType="com.yonge.cooleshow.biz.dal.vo.LessonGroupVo">
		SELECT
			g.video_lesson_group_id_ AS id,
			g.lesson_name_ AS lessonName,
			g.lesson_subject_ AS lessonSubject,
			s.name_ AS lessonSubjectName,
			g.lesson_desc_ AS lessonDesc,
			g.lesson_price_ AS lessonPrice,
			g.lesson_cover_url_ AS lessonCoverUrl,
			g.lesson_count_ AS lessonCount,
			g.teacher_id_ AS teacherId,
			u.username_ AS userName,
			u.real_name_ AS realName,
			u.avatar_ AS avatar,
			g.audit_version_ as auditVersion,
			g.relation_type_ as relationType,
			(SELECT COUNT(1) FROM video_lesson_purchase_record r WHERE r.video_lesson_group_id_=g.video_lesson_group_id_ AND r.order_status_='PAID') AS countStudent
		FROM video_lesson_purchase_record pr
		left join (
		SELECT gg.* FROM video_lesson_auth_record ar left join video_lesson_group_log gg on ar.video_group_id_ = gg.video_lesson_group_id_ and ar.version_ = gg.version_
		where ar.id_ in (select max(id_) from video_lesson_auth_record where audit_status_ = 'PASS' group by video_group_id_)
		 ) g on g.video_lesson_group_id_ = pr.video_lesson_group_id_
		LEFT JOIN sys_user u ON g.teacher_id_=u.id_
		LEFT JOIN `subject` s ON g.lesson_subject_ = s.id_
		<where>
			AND  u.del_flag_ = 0
			<if test="null != param.search and '' != param.search">
				AND (
				u.username_ LIKE CONCAT('%', #{param.search}, '%') OR
				g.lesson_name_ LIKE CONCAT('%', #{param.search}, '%')
				)
			</if>
			<if test="param.shelvesFlag !=null">
				AND g.shelves_flag_ = #{param.shelvesFlag}
			</if>
			<if test="param.auditStatus !=null">
				AND g.audit_status_ = #{param.auditStatus}
			</if>
			<if	test="param.studentId != null">
				AND pr.student_id_ = #{param.studentId}
			</if>
			<if	test="param.teacherId != null">
				AND g.teacher_id_ = #{param.teacherId}
			</if>
			<if test="param.auditVersion != null">
				AND g.audit_version_ = #{param.auditVersion}
			</if>
			<if test="param.lessonSubject != null">
				AND s.id_ = #{param.lessonSubject}
			</if>
		</where>
		ORDER BY g.create_time_ DESC
	</select>
	
	<select id="queryGroupList" resultType="com.yonge.cooleshow.biz.dal.vo.VideoLessonAuthGroup">
		SELECT
			g.id_ AS id,
			g.lesson_name_ AS lessonName,
			g.lesson_subject_ AS lessonSubject,
			g.lesson_desc_ AS lessonDesc,
			g.lesson_price_ AS lessonPrice,
			g.lesson_cover_url_ AS lessonCoverUrl,
			g.teacher_id_ AS teacherId,
			g.lesson_count_ AS lessonCount,
			g.sort_number_ AS sortNumber,
			g.lesson_tag_ AS lessonTag,
			g.top_flag_ AS topFlag,
			g.hot_flag_ AS hotFlag,
			g.shelves_flag_ AS shelvesFlag,
			g.shelves_time_ as shelvesTime,
			g.shelves_reason_ as shelvesReason,
			g.create_time_ AS createTime,
			g.update_time_ AS updateTime,
			g.audit_id_ AS auditId,
			g.audit_name_ AS auditName,
			g.audit_status_ AS auditStatus,
			g.audit_version_ as auditVersion,
			g.remarks as remarks,
			s.name_ AS subjectName,
			t.username_ AS teacherName,
			t.real_name_ AS realName,
			t.phone_ AS teacherPhone
		FROM video_lesson_group g
		LEFT JOIN `subject` s ON g.lesson_subject_=s.id_
		LEFT JOIN sys_user t ON g.teacher_id_=t.id_
		<where>
			g.audit_status_ is not null and g.draft_flag_  = 0
			<if test="null != param.search and '' != param.search">
				AND (
					g.id_ LIKE CONCAT('%', #{param.search}, '%') OR
					g.lesson_name_ LIKE CONCAT('%', #{param.search}, '%') OR
					t.id_ LIKE CONCAT('%', #{param.search}, '%') OR
					t.username_ LIKE CONCAT('%', #{param.search}, '%') OR
					t.phone_ LIKE CONCAT('%', #{param.search}, '%')
				)
			</if>
			<if test="param.auditStatus !=null and param.auditStatus !=''">
				AND g.audit_status_ = #{param.auditStatus}
			</if>
			<if test="param.shelvesFlag !=null">
				AND g.shelves_flag_ = #{param.shelvesFlag}
			</if>
			<if test="param.subjectId !=null">
				AND g.lesson_subject_ = #{param.subjectId}
			</if>
			<if test="param.auditName !=null and param.auditName !=''">
				AND g.audit_name_ LIKE CONCAT('%', #{param.auditName}, '%')
			</if>
			<if test="param.startTime !=null">
				<![CDATA[AND g.create_time_ >= #{param.startTime} ]]>
			</if>
			<if test="param.endTime !=null">
				<![CDATA[AND g.create_time_ <= #{param.endTime} ]]>
			</if>
		</where>
		ORDER BY g.update_time_ desc, g.id_ desc
	</select>

	<select id="queryGroupHistoryList" resultType="com.yonge.cooleshow.biz.dal.vo.VideoLessonAuthGroup">
		SELECT
			g.id_ AS id,
			g.lesson_name_ AS lessonName,
			g.lesson_subject_ AS lessonSubject,
			g.lesson_desc_ AS lessonDesc,
			g.lesson_price_ AS lessonPrice,
			g.lesson_cover_url_ AS lessonCoverUrl,
			g.teacher_id_ AS teacherId,
			g.lesson_count_ AS lessonCount,
			g.sort_number_ AS sortNumber,
			g.lesson_tag_ AS lessonTag,
			g.top_flag_ AS topFlag,
			g.hot_flag_ AS hotFlag,
			g.shelves_flag_ AS shelvesFlag,
			g.shelves_time_ as shelvesTime,
			g.shelves_reason_ as shelvesReason,
			g.create_time_ AS createTime,
			g.update_time_ AS updateTime,
			g.relation_type_ as relationType,
			g.video_detail_json_ as videoDetailJson,
			r.audit_id_ AS auditId,
			r.audit_name_ AS auditName,
			r.audit_status_ AS auditStatus,
			g.audit_version_ as auditVersion,
			r.video_group_id_ as videoGroupId,
			r.audit_status_ AS auditStatus,
			r.remarks as remarks,
			r.version_ as version,
			s.name_ AS subjectName,
			t.username_ AS teacherName,
			t.real_name_ AS realName,
			t.phone_ AS teacherPhone
		FROM video_lesson_auth_record r
		LEFT JOIN video_lesson_group_log g ON g.video_lesson_group_id_=r.video_group_id_ and r.version_ = g.version_
		LEFT JOIN `subject` s ON g.lesson_subject_=s.id_
		LEFT JOIN sys_user t ON g.teacher_id_=t.id_
		<where>
			and r.audit_status_ != 'DOING'
			<if test="null != param.groupId">
				AND r.video_group_id_ = #{param.groupId}
			</if>
			<if test="null != param.search and '' != param.search">
				AND (
				g.id_ LIKE CONCAT('%', #{param.search}, '%') OR
				g.lesson_name_ LIKE CONCAT('%', #{param.search}, '%') OR
				t.id_ LIKE CONCAT('%', #{param.search}, '%') OR
				t.username_ LIKE CONCAT('%', #{param.search}, '%') OR
				t.phone_ LIKE CONCAT('%', #{param.search}, '%')
				)
			</if>
			<if test="param.auditStatus !=null and param.auditStatus !=''">
				AND r.audit_status_ = #{param.auditStatus}
			</if>
			<if test="param.shelvesFlag !=null">
				AND g.shelves_flag_ = #{param.shelvesFlag}
			</if>
			<if test="param.subjectId !=null">
				AND g.lesson_subject_ = #{param.subjectId}
			</if>
			<if test="param.auditName !=null and param.auditName !=''">
				AND r.audit_name_ LIKE CONCAT('%', #{param.auditName}, '%')
			</if>
			<if test="param.startTime !=null">
				<![CDATA[AND g.create_time_ >= #{param.startTime} ]]>
			</if>
			<if test="param.endTime !=null">
				<![CDATA[AND g.create_time_ <= #{param.endTime} ]]>
			</if>
		</where>
		ORDER BY g.create_time_ DESC
	</select>

	<select id="authGroupDetil" resultType="com.yonge.cooleshow.biz.dal.vo.VideoLessonAuthGroup">
		SELECT
			g.id_ AS id,
			g.lesson_name_ AS lessonName,
			g.lesson_subject_ AS lessonSubject,
			g.lesson_desc_ AS lessonDesc,
			g.lesson_price_ AS lessonPrice,
			g.lesson_cover_url_ AS lessonCoverUrl,
			g.teacher_id_ AS teacherId,
			g.lesson_count_ AS lessonCount,
			g.sort_number_ AS sortNumber,
			g.lesson_tag_ AS lessonTag,
			g.top_flag_ AS topFlag,
			g.hot_flag_ AS hotFlag,
			g.shelves_flag_ AS shelvesFlag,
			g.shelves_time_ as shelvesTime,
			g.shelves_reason_ as shelvesReason,
			g.create_time_ AS createTime,
			g.update_time_ AS updateTime,
			g.audit_version_ as auditVersion,
			g.audit_id_ AS auditId,
			g.audit_name_ AS auditName,
			g.audit_status_ AS auditStatus,
			g.relation_type_ as relationType,
			r.version_ as version,
			r.video_group_id_ as videoGroupId,
			r.remarks as remarks,
			s.name_ AS subjectName,
			t.username_ AS teacherName,
			t.real_name_ AS realName,
			t.phone_ AS teacherPhone
		FROM video_lesson_group g 
		LEFT JOIN video_lesson_auth_record r ON g.id_=r.video_group_id_
		LEFT JOIN `subject` s ON g.lesson_subject_=s.id_
		LEFT JOIN sys_user t ON g.teacher_id_=t.id_
		where g.id_ = #{videoGroupId}
		order by r.id_ desc limit 1
	</select>

	<select id="selectGroupById" resultType="com.yonge.cooleshow.biz.dal.entity.VideoLessonGroup"
            parameterType="java.lang.Long">
		SELECT
			<include refid="baseColumns"/>
			s.name_ AS lessonSubjectName
		FROM video_lesson_group g
		LEFT JOIN `subject` s ON g.lesson_subject_=s.id_
		WHERE g.id_=#{groupId}
		AND s.del_flag_ = 0
	</select>
	<select id="countVideoGroup" resultType="com.yonge.cooleshow.biz.dal.vo.CountVideoGroupVo">
		SELECT
			COUNT(1) AS videoLessonCount,
			(SELECT COUNT(1) FROM (SELECT COUNT(1) FROM video_lesson_purchase_record GROUP BY student_id_) a) AS payingUser,
			(SELECT COUNT(1) FROM video_lesson_group_detail WHERE video_lesson_group_id_ IN
											  (SELECT id_ FROM video_lesson_group WHERE audit_status_='PASS'  and shelves_flag_ = 1)) AS lessonCount
		FROM video_lesson_group WHERE audit_status_='PASS' and shelves_flag_ = 1
	</select>
	<select id="selectVideoLesson" resultType="com.yonge.cooleshow.biz.dal.vo.CountVideoGroupVo$VideoLesson">
		SELECT
			r.video_lesson_group_id_ AS videoLessonGroupId,
			g.lesson_name_ AS lessonName,
			g.lesson_count_ AS lessonNum,
			COUNT(1) AS payCount
		FROM video_lesson_purchase_record r
		LEFT JOIN video_lesson_group g ON r.video_lesson_group_id_=g.id_
		where g.shelves_flag_ = 1 and g.audit_status_ = 'PASS'
		GROUP BY r.video_lesson_group_id_
		ORDER BY payCount DESC
		LIMIT 5
	</select>
</mapper>