<?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.srs.dormitory.mapper.DmsSemesterMapper">

    <resultMap type="DmsSemester" id="DmsSemesterResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="totalMonths"    column="total_months"    />
        <result property="semesterType"    column="semester_type"    />
        <result property="isCurrentSemester"    column="is_current_semester"    />
    </resultMap>

    <sql id="selectDmsSemesterVo">
        select id, name, start_time, end_time, total_months, semester_type, is_current_semester,`year` from dms_semester
    </sql>

    <select id="selectDmsSemesterList" parameterType="DmsSemester" resultMap="DmsSemesterResult">
        <include refid="selectDmsSemesterVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="totalMonths != null "> and total_months = #{totalMonths}</if>
            <if test="semesterType != null "> and semester_type = #{semesterType}</if>
            <if test="isCurrentSemester != null "> and is_current_semester = #{isCurrentSemester}</if>
        </where>
        order by id desc
    </select>

    <select id="selectDmsSemesterById" parameterType="Long" resultMap="DmsSemesterResult">
        <include refid="selectDmsSemesterVo"/>
        where id = #{id}
    </select>

    <insert id="insertDmsSemester" parameterType="DmsSemester" useGeneratedKeys="true" keyProperty="id">
        insert into dms_semester
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">`name`,</if>
            <if test="year != null and year != ''">`year`,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="totalMonths != null">total_months,</if>
            <if test="semesterType != null">semester_type,</if>
            <if test="isCurrentSemester != null">is_current_semester,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="year != null and year != ''">#{year},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="totalMonths != null">#{totalMonths},</if>
            <if test="semesterType != null">#{semesterType},</if>
            <if test="isCurrentSemester != null">#{isCurrentSemester},</if>
        </trim>
    </insert>

    <update id="updateDmsSemester" parameterType="DmsSemester">
        update dms_semester
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="year != null and year != ''">`year` = #{year},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="totalMonths != null">total_months = #{totalMonths},</if>
            <if test="semesterType != null">semester_type = #{semesterType},</if>
            <if test="isCurrentSemester != null">is_current_semester = #{isCurrentSemester},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDmsSemesterById" parameterType="Long">
        delete from dms_semester where id = #{id}
    </delete>

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