<?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="free_minutes_" jdbcType="INTEGER" property="freeMinutes"/>
        <result column="course_minutes_" jdbcType="INTEGER" property="courseMinutes"/>
        <result column="skip_holiday_flag_" property="skipHolidayFlag"/>
        <result column="subject_price_" jdbcType="VARCHAR" property="subjectPrice"/>
        <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="default_flag_" property="defaultFlag"/>
        <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_, free_minutes_, course_minutes_, skip_holiday_flag_, subject_price_,
    monday_, tuesday_, wednesday_, thursday_, friday_, saturday_, sunday_,default_flag_,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_, free_minutes_, course_minutes_, skip_holiday_flag_, subject_price_,
        monday_, tuesday_, wednesday_, thursday_, friday_, saturday_, sunday_,default_flag_, create_time_, update_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.teacherId}, #{entity.freeMinutes}, #{entity.courseMinutes}, #{entity.skipHolidayFlag},
            #{entity.subjectPrice}, #{entity.monday}, #{entity.tuesday}, #{entity.wednesday}, #{entity.thursday},
            #{entity.friday}, #{entity.saturday}, #{entity.sunday}, #{entity.defaultFlag},NOW(),NOW())
        </foreach>
    </insert>
    <insert id="insertPrice" parameterType="java.util.List">
        INSERT INTO teacher_subject_price
            (teacher_free_time_id,teacher_id_,free_minutes_,course_minutes_,subject_id_,subject_name_,subject_price_)VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.teacherFreeTimeId},#{item.teacherId},#{item.freeMinutes},#{item.courseMinutes},#{item.subjectId},#{item.subjectName},#{item.subjectPrice})
        </foreach>
    </insert>
    <update id="updateDefaultFlag">
        UPDATE teacher_free_time SET default_flag_ = #{defaultFlag} WHERE teacher_id_ = #{teacherId}
    </update>
    <delete id="deleteSubjectPrice" parameterType="java.lang.Long">
        DELETE FROM teacher_subject_price WHERE teacher_free_time_id = #{id}
    </delete>
    <select id="findOne" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM teacher_free_time WHERE teacher_id_ = #{teacherId}
        AND free_minutes_ = #{freeMinutes}
        AND course_minutes_ = #{courseMinutes} LIMIT 1
    </select>
    <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>
            <if test="param.userId != null">
                AND teacher_id_ = #{param.userId}
            </if>
            <if test="param.freeMinutes != null">
                AND free_minutes_ = #{param.freeMinutes}
            </if>
            <if test="param.courseMinutes != null">
                AND course_minutes_ = #{param.courseMinutes}
            </if>
            <if test="param.courseMinutes != null">
                AND course_minutes_ = #{param.courseMinutes}
            </if>
            <if test="param.defaultFlag != null">
                AND default_flag_ = #{param.defaultFlag}
            </if>
        </where>
    </select>
    <select id="getPrice" resultType="com.yonge.cooleshow.biz.dal.entity.TeacherSubjectPrice"
            parameterType="java.lang.Long">
        SELECT * FROM teacher_subject_price WHERE teacher_free_time_id = #{id}
    </select>
    <select id="selectSubjectPrice" resultType="com.yonge.cooleshow.biz.dal.entity.TeacherSubjectPrice">
        SELECT
            p.id_ AS id,
            p.teacher_free_time_id AS teacherFreeTimeId,
            p.teacher_id_ AS teacherId,
            p.free_minutes_ AS freeMinutes,
            p.course_minutes_ AS courseMinutes,
            p.subject_id_ AS subjectId,
            p.subject_name_ AS subjectName,
            p.subject_price_ AS subjectPrice
        FROM teacher_free_time f
        LEFT JOIN teacher_subject_price p ON f.id_=p.teacher_free_time_id
        WHERE f.teacher_id_=#{teacherId} AND f.default_flag_=1 AND p.subject_id_=#{subjectId}
    </select>
    <select id="selectPriceByTeacherId" resultType="com.yonge.cooleshow.biz.dal.entity.TeacherSubjectPrice"
            parameterType="java.lang.Long">
        SELECT
            p.id_ AS id,
            p.teacher_free_time_id AS teacherFreeTimeId,
            p.teacher_id_ AS teacherId,
            p.free_minutes_ AS freeMinutes,
            p.course_minutes_ AS courseMinutes,
            p.subject_id_ AS subjectId,
            p.subject_name_ AS subjectName,
            p.subject_price_ AS subjectPrice
        FROM teacher_free_time f
        LEFT JOIN teacher_subject_price p ON f.id_=p.teacher_free_time_id
        WHERE f.teacher_id_=#{teacherId}
        AND f.default_flag_=1
        AND f.enable_flag_=1
    </select>
    <select id="getTeacherFreeTime" resultType="com.yonge.cooleshow.biz.dal.entity.TeacherFreeTime">
        SELECT * FROM teacher_free_time WHERE default_flag_=1
        AND teacher_id_ IN(SELECT user_id_ FROM teacher_auth_entry_record WHERE teacher_auth_status_='PASS' GROUP BY user_id_)
    </select>
</mapper>
