<?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.TeacherStyleVideoDao">
	<resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.TeacherStyleVideo">
		<result column="id_" property="id" />
		<result column="user_id_" property="userId" />
		<result column="video_url_" property="videoUrl" />
		<result column="cover_" property="cover" />
		<result column="describe_" property="describe" />
		<result column="browse_" property="browse" />
		<result column="audit_version_" property="auditVersion" />
		<result column="auth_status_" property="authStatus" />
		<result column="virtual_number_" property="virtualNumber" />
		<result column="verify_user_id_" property="verifyUserId" />
		<result column="reason_" property="reason" />
		<result column="entry_flag_" property="entryFlag" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="del_flag_" property="delFlag" />
	</resultMap>

	<!-- 表字段 -->
	<sql id="baseColumns">
		t.id_ as id
        , t.user_id_ as userId
        , t.video_url_ as videoUrl
        , t.cover_ as cover
        , t.describe_ as `describe`
        , t.browse_  as browse
        , t.audit_version_ as auditVersion
        , t.auth_status_ as authStatus
		, t.virtual_number_ as virtualNumber
        , t.verify_user_id_ as verifyUserId
        , t.reason_ as reason
		, t.entry_flag_ as entryFlag
        , t.create_time_ as createTime
        , t.update_time_ as updateTime
        , t.del_flag_ as delFlag
	</sql>

	<select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherStyleVideoVo">
		SELECT
			<include refid="baseColumns"/>,
		       u.real_name_ as realName,
		       u.phone_ as phone,
			   v.username_ as verifyUser
		FROM teacher_style_video t
		left join sys_user u on t.user_id_ = u.id_
		left join sys_user v on t.verify_user_id_ = v.id_
		where u.del_flag_ = 0 and t.id_ = #{id}
	</select>

	<select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherStyleVideoVo">
		SELECT
			<include refid="baseColumns" />,
			u.real_name_ as realName,
			u.phone_ as phone,
			v.username_ as verifyUser
		FROM teacher_style_video t
		left join sys_user u on t.user_id_ = u.id_
		left join sys_user v on t.verify_user_id_ = v.id_
		where t.del_flag_ = 0 and u.del_flag_ = 0
		<if test="null != param.search and '' != param.search">
			AND (
			t.user_id_ LIKE CONCAT('%', #{param.search}, '%') or
			u.real_name_ LIKE CONCAT('%', #{param.search}, '%') or
			u.phone_ LIKE CONCAT('%', #{param.search}, '%')
			)
		</if>
		<if test="null != param.verifyUser and '' != param.verifyUser">
			AND v.username_ LIKE CONCAT('%', #{param.verifyUser}, '%')
		</if>
		<if test="param.startTime !=null">
			<![CDATA[AND t.create_time_ >= #{param.startTime} ]]>
		</if>
		<if test="param.endTime !=null">
			<![CDATA[AND t.create_time_ < #{param.endTime} ]]>
		</if>
		<if test="param.authStatus !=null">
			AND t.auth_status_ = #{param.authStatus}
		</if>
		<if test="param.entryFlag !=null">
			AND t.entry_flag_ = #{param.entryFlag}
		</if>
		order by field(t.auth_status_,'DOING') desc, t.create_time_ desc
	</select>

	<!-- 根据老师id查询 -->
    <select id="selectListByUserId" resultType="com.yonge.cooleshow.biz.dal.entity.TeacherStyleVideo">
		SELECT
			t.id_ as id
			 , t.user_id_ as userId
			 , t.video_url_ as videoUrl
			 , t.cover_ as cover
			 , t.describe_ as `describe`
			 , t.browse_ + t.virtual_number_  as browse
			 , t.audit_version_ as auditVersion
			 , t.auth_status_ as authStatus
			 , t.virtual_number_ as virtualNumber
			 , t.verify_user_id_ as verifyUserId
			 , t.reason_ as reason
			 , t.entry_flag_ as entryFlag
			 , t.create_time_ as createTime
			 , t.update_time_ as updateTime
			 , t.del_flag_ as delFlag
		FROM teacher_style_video t
		WHERE t.del_flag_ = 0 and t.user_id_ = #{userId}
	</select>

	<update id="removeByUserIdAndOldIds">
		UPDATE teacher_style_video t SET t.del_flag_ = '1'
		WHERE t.user_id_ = #{userId}
		<if test="oldIds!=null and oldIds.size()>0">
			AND t.id_ NOT IN
			<foreach collection="oldIds" item="oldId" open="(" close=")" separator=",">
				#{oldId}
			</foreach>
		</if>
	</update>

	<select id="stylePage" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherStyleVideoVo">
		SELECT
			<include refid="baseColumns"/>,
			ifnull(ta.live_flag_,0) as liveing,
			u.avatar_ as avatar,
			u.username_ as username
		FROM teacher_style_video t
		LEFT JOIN teacher a on t.user_id_ = a.user_id_
		LEFT JOIN sys_user u on t.user_id_ = u.id_
		LEFT JOIN teacher_total ta on t.user_id_ = ta.user_id_
		where t.del_flag_ = 0 and a.is_test_user_ = 0 and u.del_flag_ = 0 and t.auth_status_ = 'PASS'
		<if test="param.username != null and param.username != ''">
			AND u.username_ LIKE CONCAT('%', #{param.username}, '%')
		</if>
		<if test="param.subjectId != null and param.subjectId != ''">
			AND INTE_ARRAY(a.subject_id_,#{param.subjectId})
		</if>
		<if test="param.auditVersion != null">
			and #{param.auditVersion} = t.audit_version_
		</if>
		order by (ta.fans_num_ * 0.3 + t.browse_ * 0.3 + ta.exp_time_ * 0.4) desc
	</select>

	<update id="addVideoBrowse">
		update teacher_style_video set browse_ = browse_ +1 where id_ = #{videoId}
	</update>

</mapper>