<?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.TeacherDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.Teacher">
        <result column="user_id_" property="userId" />
        <result column="education_background_" property="educationBackground" />
        <result column="graduate_school_" property="graduateSchool" />
        <result column="technical_titles_" property="technicalTitles" />
        <result column="work_unit_" property="workUnit" />
        <result column="subject_id_" property="subjectId" />
        <result column="introduction_" property="introduction" />
        <result column="subject_" property="subject" />
        <result column="grad_certificate_" property="gradCertificate" />
        <result column="degree_certificate_" property="degreeCertificate" />
        <result column="teacher_certificate_" property="teacherCertificate" />
        <result column="entry_flag_" property="entryFlag" />
        <result column="entry_auth_date_" property="entryAuthDate" />
        <result column="musician_flag_" property="musicianFlag" />
        <result column="musician_date_" property="musicianDate" />
        <result column="member_rank_setting_id_" property="memberRankSettingId"/>
        <result column="membership_start_time_" property="membershipStartTime"/>
        <result column="membership_end_time_" property="membershipEndTime"/>
        <result column="live_flag_" property="liveFlag" />
        <result column="live_date_" property="liveDate" />
        <result column="browse_" property="browse" />
        <result column="memo_" property="memo" />
        <result column="create_time_" property="createTime" />
        <result column="update_time_" property="updateTime" />
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.user_id_ as "userId"
        , t.education_background_ as "educationBackground"
        , t.graduate_school_ as "graduateSchool"
        , t.technical_titles_ as "technicalTitles"
        , t.work_unit_ as "workUnit"
        , t.subject_id_ as "subjectId"
        , t.introduction_ as "introduction"
        , t.subject_ as "subject"
        , t.grad_certificate_ as "gradCertificate"
        , t.degree_certificate_ as "degreeCertificate"
        , t.teacher_certificate_ as "teacherCertificate"
        , t.entry_flag_ as "entryFlag"
        , t.entry_auth_date_ as "entryAuthDate"
        , t.musician_flag_ as "musicianFlag"
        , t.musician_date_ as "musicianDate"
        , t.live_flag_ as "liveFlag"
        , t.live_date_ as "liveDate"
        , t.member_rank_setting_id_ as "memberRankSettingId"
        , t.membership_start_time_ as "membershipStartTime"
        , t.membership_end_time_ as "membershipEndTime"
        , t.browse_ as "browse"
        , t.memo_ as "memo"
        , t.tag_ as "tag"
        , t.create_time_ as "createTime"
        , t.update_time_ as "updateTime"
        </sql>

    <!-- 分页查询 -->
    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherVo" >
        SELECT
            <include refid="baseColumns"/>,
            u.username_ as username,
            u.phone_ as phone,
            u.lock_flag_ as lockFlag,
            (case when isnull(u.id_card_no_) then 0 else 1 end) as isReal,
            (case when isnull(b.user_id_) then 0 else 1 end) as isBank
        FROM teacher t
        left join sys_user u on t.user_id_ = u.id_
        left join (
            select distinct user_id_ from user_bank_card where del_flag_ = 0
        ) b on t.user_id_ = b.user_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.teacherType and '' != param.teacherType">
                and (
                    1=0
                    <if test='param.teacherType.contains("TOURIST")'>
                        or (t.entry_flag_ = 0 and t.musician_flag_ = 0)
                    </if>
                    <if test='param.teacherType.contains("ENTRY")'>
                        or t.entry_flag_ = 1
                    </if>
                    <if test='param.teacherType.contains("MUSICIAN")'>
                        or t.musician_flag_ = 1
                    </if>
                )
            </if>
        </where>
    </select>

	<resultMap id="BasicUserInfo" type="com.yonge.cooleshow.biz.dal.dto.BasicUserInfo">
		<result property="userId" column="user_id_"/>
		<result property="username" column="username_"/>
		<result property="realName" column="real_name_"/>
		<result property="avatar" column="avatar_"/>
	</resultMap>
	<select id="getBasicUserInfo" resultMap="BasicUserInfo">
		SELECT id_ user_id_,username_,real_name_,avatar_ FROM sys_user
		WHERE del_flag_ = 0 and id_ = #{userId} LIMIT 1
	</select>

	<select id="findBasicUserInfo" resultMap="BasicUserInfo">
		SELECT id_ user_id_,username_,real_name_,avatar_ FROM sys_user
		WHERE del_flag_ = 0 and id_ IN
		<foreach collection="studentIds" open="(" close=")" separator="," item="userId">
			#{userId}
		</foreach>
	</select>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherVo">
        SELECT
            <include refid="baseColumns"/>,
            u.avatar_ as avatar,
            u.username_ as username,
            u.gender_ as `gender`,
            u.birthdate_ as birthdate,
            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,
            u.real_name_ as realName,
            u.id_card_no_ as idCardNo,
            u.lock_flag_ as lockFlag,
            (case when isnull(b.user_id_) then 0 else 1 end) as isBank,
            (
                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 teacher t
        left join sys_user u on t.user_id_ = u.id_
        left join (
            select distinct user_id_ from user_bank_card where del_flag_ = 0 and user_id_ = #{userId}
        ) b on t.user_id_ = b.user_id_
        where u.del_flag_ = 0 and t.user_id_ = #{userId}
    </select>

    <select id="querySubject" resultMap="com.yonge.cooleshow.biz.dal.dao.SubjectDao.Subject">
        SELECT s.* FROM `subject` s
        LEFT JOIN teacher t ON FIND_IN_SET(s.id_,t.subject_id_)
        WHERE t.user_id_ = #{userId}
    </select>

    <update id="setSubject">
        update teacher set subject_id_ = #{subjectIds},update_time_ = now() where user_id_ = #{id}
    </update>

    <update id="addHomeBrowse">
        update teacher set browse_ = browse_ + 1 where user_id_ = #{userId}
    </update>

    <select id="querySubjectItem" resultType="com.yonge.cooleshow.biz.dal.entity.Subject">
        select * from subject t
        where t.parent_subject_id_ != 0
        and exists(
            select 1 from teacher s where s.user_id_ = #{userId} and find_in_set(t.id_,s.subject_id_)
            union all
            select 1 from music_sheet a where a.user_id_ = #{userId} and find_in_set(t.id_,a.music_subject_)
            union all
            select 1 from course_group b where b.teacher_id_ = #{userId} and find_in_set(t.id_,b.subject_id_)
            union all
            select 1 from video_lesson_group c where c.teacher_id_ = #{userId} and find_in_set(t.id_,c.lesson_subject_)
        )
    </select>

    <select id="queryMyFans" resultType="com.yonge.cooleshow.biz.dal.vo.MyFens">
        SELECT
            s.student_id_ AS userId,
            u.avatar_ AS avatar,
            u.username_ AS userName,
            u.real_name_ AS realName,
            u.gender_ AS gender,
            (SELECT group_concat(name_) FROM `subject` WHERE find_in_set(id_,sr.subject_id_)) AS subjectName
        FROM student_star s
        LEFT JOIN sys_user u ON s.student_id_ = u.id_
        LEFT JOIN student sr ON s.student_id_ = sr.user_id_
        WHERE u.del_flag_ = 0 and s.teacher_id_ =#{teacherId}
    </select>
</mapper>