<?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.unilife.mapper.CourseMapper">
    <resultMap id="courseResultMap" type="com.unilife.model.entity.Course">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="name" property="name"/>
        <result column="teacher" property="teacher"/>
        <result column="location" property="location"/>
        <result column="day_of_week" property="dayOfWeek"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="start_week" property="startWeek"/>
        <result column="end_week" property="endWeek"/>
        <result column="color" property="color"/>
        <result column="status" property="status"/>
        <result column="created_at" property="createdAt"/>
        <result column="updated_at" property="updatedAt"/>
    </resultMap>
    
    <insert id="insert" parameterType="com.unilife.model.entity.Course" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO courses (
            user_id, name, teacher, location, day_of_week, start_time, end_time, 
            start_week, end_week, color, status, created_at, updated_at
        ) VALUES (
            #{userId}, #{name}, #{teacher}, #{location}, #{dayOfWeek}, #{startTime}, #{endTime}, 
            #{startWeek}, #{endWeek}, #{color}, #{status}, NOW(), NOW()
        )
    </insert>
    
    <select id="getById" resultMap="courseResultMap">
        SELECT id, user_id, name, teacher, location, day_of_week, start_time, end_time, 
               start_week, end_week, color, status, created_at, updated_at
        FROM courses
        WHERE id = #{id} AND status != 0
    </select>
    
    <select id="getListByUserId" resultMap="courseResultMap">
        SELECT id, user_id, name, teacher, location, day_of_week, start_time, end_time, 
               start_week, end_week, color, status, created_at, updated_at
        FROM courses
        WHERE user_id = #{userId} AND status != 0
        ORDER BY day_of_week ASC, start_time ASC
    </select>
    
    <update id="update" parameterType="com.unilife.model.entity.Course">
        UPDATE courses
        SET name = #{name},
            teacher = #{teacher},
            location = #{location},
            day_of_week = #{dayOfWeek},
            start_time = #{startTime},
            end_time = #{endTime},
            start_week = #{startWeek},
            end_week = #{endWeek},
            color = #{color},
            updated_at = NOW()
        WHERE id = #{id} AND user_id = #{userId}
    </update>
    
    <update id="delete">
        UPDATE courses
        SET status = 0,
            updated_at = NOW()
        WHERE id = #{id} AND user_id = #{userId}
    </update>
    
    <select id="getListByUserIdAndDayOfWeek" resultMap="courseResultMap">
        SELECT id, user_id, name, teacher, location, day_of_week, start_time, end_time, 
               start_week, end_week, color, status, created_at, updated_at
        FROM courses
        WHERE user_id = #{userId} AND day_of_week = #{dayOfWeek} AND status != 0
        ORDER BY start_time ASC
    </select>
    
    <select id="checkConflict" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM courses
        WHERE user_id = #{userId} 
          AND day_of_week = #{dayOfWeek} 
          AND status != 0
          AND (
              (start_time &lt; #{endTime} AND end_time &gt; #{startTime})
          )
          <if test="excludeCourseId != null">
              AND id != #{excludeCourseId}
          </if>
    </select>
</mapper>