<?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.ruoyi.education.mapper.EduCourseScheduleMapper">
    
    <resultMap type="EduCourseSchedule" id="EduCourseScheduleResult">
        <result property="scheduleId"       column="schedule_id"       />
        <result property="classId"          column="class_id"          />
        <result property="weekday"          column="weekday"           />
        <result property="periodStart"      column="period_start"      />
        <result property="periodEnd"        column="period_end"        />
        <result property="weeks"            column="weeks"             />
        <result property="classroomId"      column="classroom_id"      />
        <result property="remark"           column="remark"            />
        <result property="createBy"         column="create_by"         />
        <result property="createTime"       column="create_time"       />
        <result property="updateBy"         column="update_by"         />
        <result property="updateTime"       column="update_time"       />
        <result property="classCode"        column="class_code"        />
        <result property="courseName"       column="course_name"       />
        <result property="courseCode"       column="course_code"       />
        <result property="teacherName"      column="teacher_name"      />
        <result property="semesterName"     column="semester_name"     />
        <result property="classroomName"    column="classroom_name"    />
        <result property="weekdayName"      column="weekday_name"      />
        <result property="periodName"       column="period_name"       />
    </resultMap>

    <sql id="selectEduCourseScheduleVo">
        select s.schedule_id, s.class_id, s.weekday, s.period_start, s.period_end, s.weeks, 
               s.classroom_id, s.remark, s.create_by, s.create_time, s.update_by, s.update_time,
               cc.class_code,
               c.course_name, c.course_code,
               t.real_name as teacher_name,
               sem.semester_name,
               CONCAT(cr.building, cr.room_no) as classroom_name,
               CASE s.weekday 
                   WHEN 1 THEN '周一'
                   WHEN 2 THEN '周二'
                   WHEN 3 THEN '周三'
                   WHEN 4 THEN '周四'
                   WHEN 5 THEN '周五'
                   WHEN 6 THEN '周六'
                   WHEN 7 THEN '周日'
               END as weekday_name,
               CONCAT('第', s.period_start, '-', s.period_end, '节') as period_name
        from edu_course_schedule s
        left join edu_course_class cc on s.class_id = cc.class_id
        left join edu_course c on cc.course_id = c.course_id
        left join edu_teacher t on cc.teacher_id = t.teacher_id
        left join edu_semester sem on cc.semester_id = sem.semester_id
        left join edu_classroom cr on s.classroom_id = cr.classroom_id
    </sql>

    <select id="selectEduCourseScheduleList" parameterType="EduCourseSchedule" resultMap="EduCourseScheduleResult">
        <include refid="selectEduCourseScheduleVo"/>
        <where>  
            <if test="classId != null "> and s.class_id = #{classId}</if>
            <if test="weekday != null "> and s.weekday = #{weekday}</if>
            <if test="periodStart != null "> and s.period_start = #{periodStart}</if>
            <if test="periodEnd != null "> and s.period_end = #{periodEnd}</if>
            <if test="weeks != null  and weeks != ''"> and s.weeks like concat('%', #{weeks}, '%')</if>
            <if test="classroomId != null "> and s.classroom_id = #{classroomId}</if>
            <if test="classCode != null  and classCode != ''"> and cc.class_code like concat('%', #{classCode}, '%')</if>
            <if test="courseName != null  and courseName != ''"> and c.course_name like concat('%', #{courseName}, '%')</if>
            <if test="courseCode != null  and courseCode != ''"> and c.course_code like concat('%', #{courseCode}, '%')</if>
            <if test="teacherName != null  and teacherName != ''"> and t.real_name like concat('%', #{teacherName}, '%')</if>
        </where>
        order by s.weekday, s.period_start, s.create_time
    </select>
    
    <select id="selectEduCourseScheduleByScheduleId" parameterType="Long" resultMap="EduCourseScheduleResult">
        <include refid="selectEduCourseScheduleVo"/>
        where s.schedule_id = #{scheduleId}
    </select>

    <select id="selectScheduleByClassId" parameterType="Long" resultMap="EduCourseScheduleResult">
        <include refid="selectEduCourseScheduleVo"/>
        where s.class_id = #{classId}
        order by s.weekday, s.period_start
    </select>

    <select id="selectScheduleByClassroom" resultMap="EduCourseScheduleResult">
        <include refid="selectEduCourseScheduleVo"/>
        where s.classroom_id = #{classroomId} 
        and cc.semester_id = #{semesterId}
        and cc.status in ('1', '2')
        order by s.weekday, s.period_start
    </select>

    <select id="selectScheduleByTeacher" resultMap="EduCourseScheduleResult">
        <include refid="selectEduCourseScheduleVo"/>
        where cc.teacher_id = #{teacherId} 
        and cc.semester_id = #{semesterId}
        and cc.status in ('1', '2')
        order by s.weekday, s.period_start
    </select>

    <select id="selectScheduleBySemester" parameterType="Long" resultMap="EduCourseScheduleResult">
        <include refid="selectEduCourseScheduleVo"/>
        where cc.semester_id = #{semesterId}
        order by s.weekday, s.period_start, c.course_name
    </select>

    <select id="checkClassroomTimeConflict" resultType="int">
        select count(*)
        from edu_course_schedule s
        inner join edu_course_class cc on s.class_id = cc.class_id
        where s.classroom_id = #{classroomId}
        and s.weekday = #{weekday}
        and s.period_start &lt;= #{periodEnd}
        and s.period_end &gt;= #{periodStart}
        and cc.status in ('1', '2')
        <if test="weeks != null and weeks != ''">
            and s.weeks is not null and s.weeks != ''
        </if>
        <if test="excludeScheduleId != null">
            and s.schedule_id != #{excludeScheduleId}
        </if>
    </select>

    <select id="checkTeacherTimeConflict" resultType="int">
        select count(*)
        from edu_course_schedule s
        inner join edu_course_class cc on s.class_id = cc.class_id
        where cc.teacher_id = #{teacherId}
        and s.weekday = #{weekday}
        and s.period_start &lt;= #{periodEnd}
        and s.period_end &gt;= #{periodStart}
        and cc.status in ('1', '2')
        <if test="weeks != null and weeks != ''">
            and s.weeks is not null and s.weeks != ''
        </if>
        <if test="excludeScheduleId != null">
            and s.schedule_id != #{excludeScheduleId}
        </if>
    </select>

    <select id="generateClassroomTimetable" resultMap="EduCourseScheduleResult">
        <include refid="selectEduCourseScheduleVo"/>
        where s.classroom_id = #{classroomId} 
        and cc.semester_id = #{semesterId}
        and cc.status in ('1', '2')
        order by s.weekday, s.period_start
    </select>

    <select id="generateTeacherTimetable" resultMap="EduCourseScheduleResult">
        <include refid="selectEduCourseScheduleVo"/>
        where cc.teacher_id = #{teacherId} 
        and cc.semester_id = #{semesterId}
        and cc.status in ('1', '2')
        order by s.weekday, s.period_start
    </select>
        
    <insert id="insertEduCourseSchedule" parameterType="EduCourseSchedule" useGeneratedKeys="true" keyProperty="scheduleId">
        insert into edu_course_schedule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="classId != null">class_id,</if>
            <if test="weekday != null">weekday,</if>
            <if test="periodStart != null">period_start,</if>
            <if test="periodEnd != null">period_end,</if>
            <if test="weeks != null">weeks,</if>
            <if test="classroomId != null">classroom_id,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            create_time
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="classId != null">#{classId},</if>
            <if test="weekday != null">#{weekday},</if>
            <if test="periodStart != null">#{periodStart},</if>
            <if test="periodEnd != null">#{periodEnd},</if>
            <if test="weeks != null">#{weeks},</if>
            <if test="classroomId != null">#{classroomId},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            sysdate()
         </trim>
    </insert>

    <insert id="batchInsertSchedule" parameterType="java.util.List">
        insert into edu_course_schedule (class_id, weekday, period_start, period_end, weeks, classroom_id, remark, create_by, create_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.classId}, #{item.weekday}, #{item.periodStart}, #{item.periodEnd}, #{item.weeks}, #{item.classroomId}, #{item.remark}, #{item.createBy}, sysdate())
        </foreach>
    </insert>

    <update id="updateEduCourseSchedule" parameterType="EduCourseSchedule">
        update edu_course_schedule
        <trim prefix="SET" suffixOverrides=",">
            <if test="classId != null">class_id = #{classId},</if>
            <if test="weekday != null">weekday = #{weekday},</if>
            <if test="periodStart != null">period_start = #{periodStart},</if>
            <if test="periodEnd != null">period_end = #{periodEnd},</if>
            <if test="weeks != null">weeks = #{weeks},</if>
            <if test="classroomId != null">classroom_id = #{classroomId},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = sysdate()
        </trim>
        where schedule_id = #{scheduleId}
    </update>

    <delete id="deleteEduCourseScheduleByScheduleId" parameterType="Long">
        delete from edu_course_schedule where schedule_id = #{scheduleId}
    </delete>

    <delete id="deleteEduCourseScheduleByScheduleIds" parameterType="String">
        delete from edu_course_schedule where schedule_id in 
        <foreach item="scheduleId" collection="array" open="(" separator="," close=")">
            #{scheduleId}
        </foreach>
    </delete>

    <delete id="deleteScheduleByClassId" parameterType="Long">
        delete from edu_course_schedule where class_id = #{classId}
    </delete>
</mapper>