<?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.TeacherFreeTimeDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.TeacherFreeTime">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="teacher_id_" jdbcType="INTEGER" property="teacherId"/>
        <result column="skip_holiday_flag_" property="skipHolidayFlag"/>
        <result column="monday_" jdbcType="VARCHAR" property="monday"/>
        <result column="tuesday_" jdbcType="VARCHAR" property="tuesday"/>
        <result column="wednesday_" jdbcType="VARCHAR" property="wednesday"/>
        <result column="thursday_" jdbcType="VARCHAR" property="thursday"/>
        <result column="friday_" jdbcType="VARCHAR" property="friday"/>
        <result column="saturday_" jdbcType="VARCHAR" property="saturday"/>
        <result column="sunday_" jdbcType="VARCHAR" property="sunday"/>
        <result column="recent_free_time_" property="recentFreeTime"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time_" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_, teacher_id_, skip_holiday_flag_,
    monday_, tuesday_, wednesday_, thursday_, friday_, saturday_, sunday_,recent_free_time_,create_time_, update_time_
    </sql>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.yonge.cooleshow.biz.dal.entity.TeacherFreeTime">
        insert into teacher_free_time(teacher_id_, skip_holiday_flag_,
        monday_, tuesday_, wednesday_, thursday_, friday_, saturday_, sunday_,recent_free_time_, create_time_, update_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.teacherId},#{entity.skipHolidayFlag},
            #{entity.monday}, #{entity.tuesday}, #{entity.wednesday}, #{entity.thursday},
            #{entity.friday}, #{entity.saturday}, #{entity.sunday},#{entity.recentFreeTime},NOW(),NOW())
        </foreach>
    </insert>
    <select id="getDetail" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherFreeTimeVo"
            parameterType="com.yonge.cooleshow.biz.dal.dto.PracticeTimesSetting">
        SELECT * FROM teacher_free_time where teacher_id_ = #{userId}
    </select>
    <select id="getTeacherFreeTime" resultType="com.yonge.cooleshow.biz.dal.entity.TeacherFreeTime">
        SELECT * FROM teacher_free_time
        WHERE teacher_id_ IN(SELECT user_id_ FROM teacher_auth_entry_record WHERE teacher_auth_status_='PASS' GROUP BY user_id_)
    </select>
    <select id="queryTeacherList" resultType="com.yonge.cooleshow.biz.dal.vo.PracticeTeacherVo">
        select
            tsp.teacher_id_ AS teacherId,
            u.username_ AS userName,
            u.real_name_ AS realName,
            u.avatar_ AS avatar,
            tt.star_grade_ AS starGrade,
            tt.exp_time_ AS expTime,
            t.graduate_school_ AS school,
            t.subject_ AS schoolSubject,
            tsp.subject_name_ AS subjectName,
            tsp.subject_id_ AS subjectId,
            tsp.subject_price_ AS subjectPrice,
            tsp.course_minutes_ AS courseMinutes
        from teacher_free_time tft
        left join teacher_subject_price tsp ON tsp.teacher_id_ = tft.teacher_id_
        left join teacher t ON t.user_id_ = tsp.teacher_id_
        LEFT JOIN sys_user u ON t.user_id_ = u.id_
        LEFT JOIN teacher_total tt ON t.user_id_=tt.user_id_
        <where>
            t.lock_flag_ = 0
            <if test="param.subjectId != null">
                AND tsp.subject_id_ = #{param.subjectId}
            </if>
            <if test="param.recentFreeTime != null">
                AND tft.recent_free_time_ = #{param.recentFreeTime}
            </if>
            <if test="param.courseType != null and param.courseType != ''">
                AND tsp.course_type_ = #{param.courseType}
            </if>
            <if test="param.search != null and param.search != ''">
                AND u.username_ LIKE CONCAT('%',#{param.search},'%')
            </if>
        </where>
        <if test="param.sortField != null and param.sortField != ''">
            order by ${param.sortField} ${param.sortRule}
        </if>
        <if test="param.sortField == null or param.sortField == ''">
            order by tt.exp_time_ desc
        </if>
    </select>
</mapper>
