<?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.StudentDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.Student">
        <result column="user_id_" property="userId"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="member_rank_setting_id_" property="memberRankSettingId"/>
        <result column="membership_start_time_" property="membershipStartTime"/>
        <result column="membership_end_time_" property="membershipEndTime"/>
        <result column="cloud_study_sequence_days_" property="cloudStudySequenceDays"/>
        <result column="cloud_study_use_last_day_" property="cloudStudyUseLastDay"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.user_id_ as "userId"
        , t.subject_id_ as "subjectId"
        , t.current_grade_num_ as "currentGradeNum"
        , t.member_rank_setting_id_ as "memberRankSettingId"
        , t.membership_start_time_ as "membershipStartTime"
        , t.membership_end_time_ as "membershipEndTime"
        , t.cloud_study_sequence_days_ as "cloudStudySequenceDays"
        , t.cloud_study_use_last_day_ as "cloudStudyUseLastDay"
        , t.create_time_ as "createTime"
        , t.update_time_ as "updateTime"
        </sql>
    <update id="setSubject">
        update student set subject_id_ = #{subjectIds},update_time_ = now() where user_id_ = #{id}
    </update>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.StudentVo">
        SELECT
            <include refid="baseColumns"/>,
            u.avatar_ as avatar,
            u.real_name_ as realName,
            u.id_card_no_ as idCardNo,
            u.username_ as username,
            u.gender_ as gender,
            u.birthdate_ as birthdate,
            TIMESTAMPDIFF(YEAR, u.birthdate_, CURDATE()) as age,
            u.phone_ as phone,
            (case when isnull(u.id_card_no_) then 0 else 1 end) as isReal,
            (!isnull(membership_end_time_) and membership_end_time_ > now()) as isVip,
            (
            SELECT GROUP_CONCAT(name_ ORDER by locate(id_,t.subject_id_)) FROM subject WHERE FIND_IN_SET(id_,t.subject_id_)
            ) as subjectName,
            u.user_type_ as userType
        FROM student t
        left join sys_user u on t.user_id_ = u.id_
        where u.del_flag_ = 0 and t.user_id_ = #{userId}
    </select>
    <select id="detailByPhone" resultType="com.yonge.cooleshow.biz.dal.vo.StudentVo">
        SELECT
            <include refid="baseColumns"/>,
            u.avatar_ as avatar,
            u.real_name_ as realName,
            u.id_card_no_ as idCardNo,
            u.username_ as username,
            u.gender_ as gender,
            u.birthdate_ as birthdate,
            TIMESTAMPDIFF(YEAR, u.birthdate_, CURDATE()) as age,
            u.phone_ as phone,
            (case when isnull(u.id_card_no_) then 0 else 1 end) as isReal,
            (!isnull(membership_end_time_) and membership_end_time_ > now()) as isVip,
            (
                SELECT GROUP_CONCAT(name_ ORDER by locate(id_,t.subject_id_)) FROM subject WHERE FIND_IN_SET(id_,t.subject_id_)
            ) as subjectName,
            u.user_type_ as userType
        FROM student t
        left join sys_user u on t.user_id_ = u.id_
        where u.del_flag_ = 0 and u.phone_ = #{phone}
    </select>
    <!-- 分页查询 -->
    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.StudentVo">
        SELECT
            <include refid="baseColumns"/>,
            u.username_ as username,
            u.gender_ as gender,
            u.birthdate_ as birthdate,
            TIMESTAMPDIFF(YEAR, u.birthdate_, CURDATE()) as age,
            u.phone_ as phone,
            !isnull(birthdate_) as isReal,
            (!isnull(membership_end_time_) and membership_end_time_ > now()) as isVip,
            (
            SELECT GROUP_CONCAT(name_ ORDER by locate(id_,t.subject_id_)) FROM subject WHERE FIND_IN_SET(id_,t.subject_id_)
            ) as subjectName
        FROM student t
        left join sys_user u on t.user_id_ = u.id_
        <where>
            u.del_flag_ = 0
            <if test="null != param.search and '' != param.search">
                AND (
                    t.user_id_ LIKE CONCAT('%', #{param.search}, '%') or
                    u.username_ LIKE CONCAT('%', #{param.search}, '%') or
                    u.phone_ LIKE CONCAT('%', #{param.search}, '%')
                )
            </if>
            <if test="null != param.gender">
                and u.gender_ = #{param.gender}
            </if>
            <if test="null != param.subjectId and '' != param.subjectId">
                and INTE_ARRAY(t.subject_id_,#{param.subjectId})
            </if>
            <if test="null != param.isVip">
                <if test="1 == param.isVip.code">
                    and t.membership_end_time_ &gt; now()
                </if>
                <if test="0 == param.isVip.code">
                    and (t.membership_end_time_ is null or t.membership_end_time_ &lt;= now())
                </if>
            </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>
        </where>
    </select>

    <select id="querySubjectItem" resultType="com.yonge.cooleshow.biz.dal.entity.Subject">
        select * from subject t
        where exists(
            select 1 from student s where s.user_id_ = #{userId} and find_in_set(t.id_,s.subject_id_)
            union all
            select 1 from music_sheet a
            left join music_sheet_purchase_record a1 on a.id_ = a1.music_sheet_id_
            where a1.student_id_ = #{userId} and a1.order_status_ = 'PAID' and find_in_set(t.id_,a.music_subject_)
            union all
            select 1 from course_group b
            left join course_schedule_student_payment b1 on b.id_ = b1.course_group_id_
            where b1.user_id_ = #{userId} and find_in_set(t.id_,b.subject_id_)
            union all
            select 1 from video_lesson_group c
            left join video_lesson_purchase_record c1 on c.id_ = c1.video_lesson_group_id_
            where c1.student_id_ = #{userId} and find_in_set(t.id_,c.lesson_subject_)
        )
    </select>

    <select id="queryMyFollow" resultType="com.yonge.cooleshow.biz.dal.vo.MyFollow">
        SELECT
            s.teacher_id_ AS userId,
            u.avatar_ AS avatar,
            u.username_ AS userName,
            u.real_name_ AS realName,
            u.gender_ AS gender,
            t.star_grade_ AS starGrade,
            t.fans_num_ AS fansNum,
            t.live_flag_ AS liveFlag,
            t.exp_time_ AS expTime,
            tr.musician_flag_ AS musicianFlag,
            tr.entry_flag_ AS entryFlag,
            (SELECT group_concat( name_ ) FROM `subject` WHERE find_in_set( id_, tr.subject_id_ )) AS subjectName,
            r.room_uid_ AS roomUid
        FROM student_star s
        LEFT JOIN sys_user u ON s.teacher_id_ = u.id_
        LEFT JOIN teacher_total t ON s.teacher_id_ = t.user_id_
        LEFT JOIN teacher tr ON s.teacher_id_ = tr.user_id_
        LEFT JOIN (SELECT room_uid_,speaker_id_ FROM live_room WHERE live_state_=1) r ON t.user_id_= r.speaker_id_
        WHERE s.student_id_ = #{studentId}
    </select>
</mapper>