<?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.xjrsoft.module.courseTable.mapper.CourseTableMapper">
    <select id="getList" parameterType="com.xjrsoft.module.schedule.dto.CourseTableDto" resultType="com.xjrsoft.module.schedule.vo.CourseDetailVo">
        SELECT t1.course_name, t1.teacher_name,CONCAT(t4.name,t3.name) AS classroom_name,t1.weeks,t1.time_period,t1.time_number,t5.name as class_name,t1.schedule_date,t1.adjust_type FROM course_table t1
        LEFT JOIN base_classroom t3 ON t1.site_id = t3.id
        LEFT JOIN base_office_build t4 ON t3.office_build_id = t4.id
        LEFT JOIN base_class t5 ON t1.class_id = t5.id
        LEFT JOIN base_class_major_set t7 ON t7.class_id = t5.id
        LEFT JOIN base_grade t6 ON t5.grade_id = t6.id
        where t1.status = 1
        <if test="dto.semesterId != null">
            and t1.base_semester_id = #{dto.semesterId}
        </if>
        <if test="dto.weekDay != null">
            and t1.weeks = #{dto.weekDay}
        </if>
        <if test="dto.courseType != null and dto.courseType != '' and dto.courseType == 'teacher' and dto.teacherId != null">
            and t1.teacher_id like concat('%', #{dto.teacherId}, '%')
        </if>

        <if test="dto.courseType != null and dto.courseType != '' and dto.courseType == 'class'">
            <if test="dto.gradeId != null">
                and t6.id = #{dto.gradeId}
            </if>
            <if test="dto.classId != null">
                and t5.id = #{dto.classId}
            </if>
            <if test="dto.majorSetId != null">
                and t7.major_set_id = #{dto.majorSetId}
            </if>
        </if>
        <if test="dto.startDate != null and dto.endDate != null">
            and t1.schedule_date between #{dto.startDate} and #{dto.endDate}
        </if>
        <if test="dto.toDay != null">
            and t1.schedule_date = #{dto.toDay}
        </if>
        <if test="dto.teacherId != null">
            and t1.teacher_id like concat('%', #{dto.teacherId}, '%')
        </if>
        <if test="dto.classId != null">
            and t5.id = #{dto.classId}
        </if>

    </select>
    <select id="getAdjustList" parameterType="com.xjrsoft.module.schedule.dto.CourseTableAdjustDto" resultType="com.xjrsoft.module.schedule.vo.CourseListVo">
        SELECT t1.time_period,t4.short_name as time_number,t1.course_name,t2.name AS class_name,t3.name AS classroom_name,t1.id,t2.id as class_id FROM course_table t1
        LEFT JOIN base_class t2 ON t1.class_id = t2.id
        LEFT JOIN base_classroom t3 ON t1.site_id = t3.id
        left join class_time t4 on t1.time_number = t4.number and t1.time_period = t4.time_period
        WHERE t1.status = 1
        <if test="dto.adjustType != null and dto.adjustType == 'course_exchange'">
            AND t1.teacher_id = #{dto.teacherId}
        </if>
        <if test="dto.adjustType != null and dto.adjustType == 'course_substitute'">
            AND t1.teacher_id like concat('%', #{dto.teacherId},'%')
        </if>
        AND t1.weeks = #{dto.week} AND t1.schedule_date = #{dto.adjustDate}
        <if test="dto.classId != null and dto.classId != ''">
            and t1.class_id = #{dto.classId}
        </if>
        <if test="dto.exceptCourseList != null and dto.exceptCourseList.size > 0">
            AND t1.id not in
            <foreach item="courseId" collection="dto.exceptCourseList" open="(" separator="," close=")">
                #{courseId}
            </foreach>
        </if>
        order by t4.number asc
    </select>
    <select id="getExceptCourseList" parameterType="java.lang.Long" resultType="com.xjrsoft.module.schedule.entity.WfCourseAdjust">
        SELECT a1.* FROM wf_course_adjust a1
        INNER JOIN xjr_workflow_form_relation a2 ON a1.id = a2.form_key_value
        WHERE a1.delete_mark = 0 and a1.enabled_mark = 1 AND a2.current_state IN ('COMPLETED','ACTIVE')
        and a1.user_id = #{userId}
    </select>

    <select id="getClassListByTeacherId" parameterType="com.xjrsoft.module.schedule.dto.ClassOptionDto" resultType="com.xjrsoft.module.schedule.vo.ClassOptionVo">
        SELECT id,name FROM base_class WHERE delete_mark = 0 AND id IN (
            SELECT DISTINCT class_id FROM course_table WHERE teacher_id like concat('%', #{dto.userId},'%')
        )
    </select>

    <select id="getClassTeacherIds" parameterType="com.xjrsoft.module.courseTable.dto.ClassTeacherDto" resultType="com.xjrsoft.module.courseTable.vo.ClassTeacherVo">
        SELECT DISTINCT t1.class_id,t1.teacher_id FROM course_table t1
        WHERE t1.class_id IN (SELECT id FROM base_class WHERE grade_id = #{dto.gradeId})
        AND t1.schedule_date BETWEEN #{dto.startDate} AND #{dto.endDate}
        AND (t1.adjust_type = 'course_exchange' OR t1.adjust_type = 'course_replace' OR t1.adjust_type IS NULL)
        UNION
        SELECT DISTINCT t1.class_id,t1.teacher_id FROM course_table_bak t1
        WHERE t1.class_id IN (SELECT id FROM base_class WHERE grade_id = #{dto.gradeId})
        AND t1.schedule_date BETWEEN #{dto.startDate} AND #{dto.endDate}
        AND (t1.adjust_type = 'course_exchange' OR t1.adjust_type = 'course_replace' OR t1.adjust_type IS NULL)
    </select>


</mapper>