<?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.UserBindingTeacherDao">
    <select id="selectStudent" resultType="com.yonge.cooleshow.biz.dal.vo.CourseStudentVo">
        SELECT
            su.id_ AS userId,
            su.username_ AS userName,
            su.real_name_ AS realName,
            su.avatar_ AS avatar,
            su.phone_ AS phone,
            (SELECT group_concat(name_) FROM `subject` WHERE find_in_set(id_,s.subject_id_)) AS subjectName,
            DATEDIFF(NOW(),b.last_end_class_) AS lastEndClass
        FROM user_binding_teacher b
        LEFT JOIN sys_user su ON b.student_id_=su.id_
        LEFT JOIN student s ON b.student_id_=s.user_id_
        <where>
            su.del_flag_ = 0
            <if test="param.subjectId != null">
               AND find_in_set(#{param.subjectId},s.subject_id_)
            </if>
            <if test="param.teacherId !=null">
                AND b.teacher_id_ = #{param.teacherId}
            </if>
            <if test="param.userName !=null and param.userName !=''">
                AND su.username_ LIKE CONCAT('%', #{param.userName}, '%')
            </if>
            <if test="param.studentList !=null and param.studentList.size>0">
                AND su.id_ IN
                <foreach collection="param.studentList" item="item" open="(" separator="," close=")">
                    #{item.userId}
                </foreach>
            </if>
        </where>
    </select>

    <!-- 表字段 -->
    <sql id="teacherColumns">
        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.memo_ as "memo"
        , t.create_time_ as "createTime"
        , t.update_time_ as "updateTime"
    </sql>

    <select id="getBindTeacherByPhone" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherVo">
        SELECT
            <include refid="teacherColumns"/>,
            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,
            u.real_name_ as realName,
            u.id_card_no_ as idCardNo,
            u.lock_flag_ as lockFlag,
            (
                SELECT GROUP_CONCAT(name_ ORDER by locate(id_,t.subject_id_)) FROM subject WHERE FIND_IN_SET(id_,t.subject_id_)
            ) as subjectName
        FROM (
            select a.* from user_binding_teacher a
            left join sys_user b on a.student_id_ = b.id_
            where b.phone_ = #{phone}
        ) a
        left join teacher t on a.teacher_id_ = t.user_id_
        left join sys_user u on t.user_id_ = u.id_
        where u.del_flag_ = 0
    </select>


    <select id="queryUnbindList" resultType="com.yonge.cooleshow.biz.dal.entity.UserBindingTeacher">
        select
            t.*
        from user_binding_teacher t
        left join (
            SELECT
                a.teacher_id_,b.user_id_
            FROM course_schedule a
            left join course_schedule_student_payment b on a.id_ = b.course_id_
            WHERE a.end_time_ >= DATE_SUB(NOW(),INTERVAL #{days} DAY)
              AND a.type_ = 'PIANO_ROOM_CLASS'
            GROUP BY a.teacher_id_,b.user_id_
        ) v on t.teacher_id_ = v.teacher_id_ and t.student_id_ = v.user_id_
        where v.user_id_ is null and t.created_time_ &lt;= DATE_SUB(NOW(),INTERVAL #{days} DAY)
    </select>
</mapper>
