<?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.TeacherAuthEntryRecordDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.TeacherAuthEntryRecord">
        <result column="id_" property="id"/>
        <result column="user_id_" property="userId"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="introduction_" property="introduction"/>
        <result column="graduate_school_" property="graduateSchool"/>
        <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="style_video_json_" property="styleVideoJson"/>
        <result column="teacher_auth_status_" property="teacherAuthStatus"/>
        <result column="teacher_auth_type_" property="teacherAuthType"/>
        <result column="verify_user_id_" property="verifyUserId"/>
        <result column="reason_" property="reason"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ as "id"
        , t.user_id_ as "userId"
        , t.subject_id_ as "subjectId"
        , t.introduction_ as "introduction"
        , t.graduate_school_ as "graduateSchool"
        , t.subject_ as "subject"
        , t.grad_certificate_ as "gradCertificate"
        , t.degree_certificate_ as "degreeCertificate"
        , t.teacher_certificate_ as "teacherCertificate"
        , t.style_video_json_ as "styleVideoJson"
        , t.teacher_auth_status_ as "teacherAuthStatus"
        , t.teacher_auth_type_ as "teacherAuthType"
        , t.verify_user_id_ as "verifyUserId"
        , t.reason_ as "reason"
        , t.create_time_ as "createTime"
        , t.update_time_ as "updateTime"
        </sql>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherAuthEntryRecordVo">
        select
            <include refid="baseColumns"/>,
            ifnull(u.real_name_,u.username_) as realName,
            u.id_card_no_ as idCardNo,
            u.phone_ as phone,
            u.username_ as username,
            u.gender_ as gender,
            (
                SELECT GROUP_CONCAT(name_ ORDER by locate(id_,t.subject_id_)) FROM subject WHERE FIND_IN_SET(id_,t.subject_id_)
            ) as subjectName,
            (
                SELECT u.username_ FROM sys_user u WHERE u.id_ = t.verify_user_id_
            ) as verifyUser
        from teacher_auth_entry_record t
        left join sys_user u on t.user_id_ = u.id_
        where u.del_flag_ = 0 and t.id_ = #{id}
    </select>

    <select id="lastDetil" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherAuthEntryRecordVo">
        select
            <include refid="baseColumns"/>,
            ifnull(u.real_name_,u.username_) as realName,
            u.id_card_no_ as idCardNo,
            u.phone_ as phone,
            u.username_ as username,
            u.gender_ as gender,
            (
                SELECT GROUP_CONCAT(name_ ORDER by locate(id_,t.subject_id_)) FROM subject WHERE FIND_IN_SET(id_,t.subject_id_)
            ) as subjectName,
            (
                SELECT u.username_ FROM sys_user u WHERE u.id_ = t.verify_user_id_
            ) as verifyUser
        from teacher_auth_entry_record t
        left join sys_user u on t.user_id_ = u.id_
        where u.del_flag_ = 0 and t.user_id_ = #{userId}
            and t.id_ != #{id}
        order by t.create_time_ desc limit 1
    </select>

    <!-- 分页查询 -->
    <select id="selectPage" resultType = "com.yonge.cooleshow.biz.dal.vo.TeacherAuthEntryRecordVo">
        SELECT
            <include refid="baseColumns"/>,
            ifnull(u.real_name_,u.username_) as realName,
            u.id_card_no_ as idCardNo,
            u.phone_ as phone,
            u.username_ as username,
            u.gender_ as gender,
            v.username_ as verifyUser,
            (
                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 user_id_,max(id_) as id_ from teacher_auth_entry_record group by user_id_
        ) a
        left join teacher_auth_entry_record t on a.id_ = t.id_
        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
            <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.userId">
                AND t.user_id_ = #{param.userId}
            </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.teacher_auth_status_ = #{param.authStatus}
            </if>
            <if test="param.teacherAuthType != null">
                AND t.teacher_auth_type_ = #{param.teacherAuthType}
            </if>
        </where>
        order by field(t.teacher_auth_status_,'DOING') desc, t.create_time_ desc
    </select>

    <select id="historyPage" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherAuthEntryRecordVo">
        SELECT
            <include refid="baseColumns"/>,
            ifnull(u.real_name_,u.username_) as realName,
            u.id_card_no_ as idCardNo,
            u.phone_ as phone,
            u.username_ as username,
            u.gender_ as gender,
            v.username_ as verifyUser,
            (
                SELECT GROUP_CONCAT(name_ ORDER by locate(id_,t.subject_id_)) FROM subject WHERE FIND_IN_SET(id_,t.subject_id_)
            ) as subjectName
        FROM teacher_auth_entry_record 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.teacher_auth_status_ != 'DOING'
            <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.userId">
                AND t.user_id_ = #{param.userId}
            </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.teacher_auth_status_ = #{param.authStatus}
            </if>
            <if test="param.teacherAuthType != null">
                AND t.teacher_auth_type_ = #{param.teacherAuthType}
            </if>
        </where>
        order by field(t.teacher_auth_status_,'DOING') desc, t.create_time_ desc
    </select>

    <select id="getLastRecordByUserId" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherAuthEntryRecordVo">
        select
            <include refid="baseColumns"/>,
            ifnull(u.real_name_,u.username_) as realName,
            u.id_card_no_ as idCardNo,
            u.gender_ as gender,
            (
                SELECT GROUP_CONCAT(name_ ORDER by locate(id_,t.subject_id_)) FROM subject WHERE FIND_IN_SET(id_,t.subject_id_)
            ) as subjectName,
            (
                SELECT u.username_ FROM sys_user u WHERE u.id_ = t.verify_user_id_
            ) as verifyUser
        from teacher_auth_entry_record t
        left join sys_user u on t.user_id_ = u.id_
        where u.del_flag_ = 0 and t.user_id_ = #{userId}
		order by t.create_time_ desc limit 1
    </select>
</mapper>