<?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.EduSemesterMapper">
    
    <resultMap type="EduSemester" id="EduSemesterResult">
        <result property="semesterId"       column="semester_id"       />
        <result property="semesterCode"     column="semester_code"     />
        <result property="semesterName"     column="semester_name"     />
        <result property="startDate"        column="start_date"        />
        <result property="endDate"          column="end_date"          />
        <result property="enrollStartTime"  column="enroll_start_time" />
        <result property="enrollEndTime"    column="enroll_end_time"   />
        <result property="status"           column="status"            />
        <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"       />
    </resultMap>

    <sql id="selectEduSemesterVo">
        select semester_id, semester_code, semester_name, start_date, end_date, enroll_start_time, enroll_end_time, status, remark, create_by, create_time, update_by, update_time from edu_semester
    </sql>

    <select id="selectEduSemesterList" parameterType="EduSemester" resultMap="EduSemesterResult">
        <include refid="selectEduSemesterVo"/>
        <where>  
            <if test="semesterCode != null  and semesterCode != ''"> and semester_code like concat('%', #{semesterCode}, '%')</if>
            <if test="semesterName != null  and semesterName != ''"> and semester_name like concat('%', #{semesterName}, '%')</if>
            <if test="startDate != null "> and start_date = #{startDate}</if>
            <if test="endDate != null "> and end_date = #{endDate}</if>
            <if test="enrollStartTime != null "> and enroll_start_time = #{enrollStartTime}</if>
            <if test="enrollEndTime != null "> and enroll_end_time = #{enrollEndTime}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectEduSemesterBySemesterId" parameterType="Long" resultMap="EduSemesterResult">
        <include refid="selectEduSemesterVo"/>
        where semester_id = #{semesterId}
    </select>

    <select id="selectEduSemesterBySemesterCode" parameterType="String" resultMap="EduSemesterResult">
        <include refid="selectEduSemesterVo"/>
        where semester_code = #{semesterCode}
    </select>

    <select id="selectCurrentSemester" resultMap="EduSemesterResult">
        <include refid="selectEduSemesterVo"/>
        where status = '1'
        limit 1
    </select>

    <select id="selectEnrollableSemesters" resultMap="EduSemesterResult">
        <include refid="selectEduSemesterVo"/>
        where status = '1' 
        and NOW() BETWEEN enroll_start_time AND enroll_end_time
        order by create_time desc
    </select>
        
    <insert id="insertEduSemester" parameterType="EduSemester" useGeneratedKeys="true" keyProperty="semesterId">
        insert into edu_semester
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="semesterCode != null and semesterCode != ''">semester_code,</if>
            <if test="semesterName != null and semesterName != ''">semester_name,</if>
            <if test="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="enrollStartTime != null">enroll_start_time,</if>
            <if test="enrollEndTime != null">enroll_end_time,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            create_time
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="semesterCode != null and semesterCode != ''">#{semesterCode},</if>
            <if test="semesterName != null and semesterName != ''">#{semesterName},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="enrollStartTime != null">#{enrollStartTime},</if>
            <if test="enrollEndTime != null">#{enrollEndTime},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            sysdate()
         </trim>
    </insert>

    <update id="updateEduSemester" parameterType="EduSemester">
        update edu_semester
        <trim prefix="SET" suffixOverrides=",">
            <if test="semesterCode != null and semesterCode != ''">semester_code = #{semesterCode},</if>
            <if test="semesterName != null and semesterName != ''">semester_name = #{semesterName},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="enrollStartTime != null">enroll_start_time = #{enrollStartTime},</if>
            <if test="enrollEndTime != null">enroll_end_time = #{enrollEndTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = sysdate()
        </trim>
        where semester_id = #{semesterId}
    </update>

    <delete id="deleteEduSemesterBySemesterId" parameterType="Long">
        delete from edu_semester where semester_id = #{semesterId}
    </delete>

    <delete id="deleteEduSemesterBySemesterIds" parameterType="String">
        delete from edu_semester where semester_id in 
        <foreach item="semesterId" collection="array" open="(" separator="," close=")">
            #{semesterId}
        </foreach>
    </delete>
</mapper>