<?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.younike.system.mapper.ServerTimeBookMapper">

    <select id="selectServerTimeBookByServerId" parameterType="Long" resultMap="ServerTimeBookResult">
        <include refid="selectServerTimeBookVo"/>
        where server_id = #{serverId}
        and server_date &gt; now()
        order by server_date asc, start_time asc
    </select>

    <insert id="insertBatch">
        insert into server_time_book
            (server_id, server_date, week, start_time, end_time, booked, create_by,create_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.serverId}, #{item.serverDate}, #{item.week}, #{item.startTime},
             #{item.endTime}, #{item.booked}, #{item.createBy}, #{item.createTime})
        </foreach>;
    </insert>

    <select id="getBookedList" resultType="com.younike.system.domain.ServerTimeBook">
        <include refid="selectServerTimeBookVo"/>
        <where>
            server_id = #{serverId}
            and start_time >= DATE_ADD(CURDATE(),INTERVAL 1 DAY)
            and booked = 1
        </where>
    </select>

    <delete id="removeBookedList">
        delete from server_time_book
        <where>
            server_id = #{serverId}
            and start_time >= DATE_ADD(CURDATE(),INTERVAL 1 DAY)
        </where>
    </delete>
    <delete id="removeBookedListBefore">
        delete from server_time_book
        <where>
            server_date &lt;= #{deleteDay}
        </where>
    </delete>
    <delete id="removeBookedListAfter">
        delete from server_time_book
        <where>
            server_date &gt;= #{insertDay}
        </where>
    </delete>

    <select id="selectAvailableServerTimeBookList" resultMap="ServerTimeBookResult">
        <include refid="selectServerTimeBookVo"/>
        where server_id = #{serverId}
        and server_date = #{date}
        and start_time &gt;= #{start}
        and end_time &lt;= #{end}
    </select>

    <update id="updateServerTimeBooks">
        <foreach collection="list" item="item" separator=";">
            update server_time_book
            <trim prefix="SET" suffixOverrides=",">
                <if test="item.serverId != null">server_id = #{item.serverId},</if>
                <if test="item.serverDate != null">server_date = #{item.serverDate},</if>
                <if test="item.week != null">week = #{item.week},</if>
                <if test="item.startTime != null">start_time = #{item.startTime},</if>
                <if test="item.endTime != null">end_time = #{item.endTime},</if>
                <if test="item.booked != null">booked = #{item.booked},</if>
                <if test="item.createBy != null">create_by = #{item.createBy},</if>
                <if test="item.createTime != null">create_time = #{item.createTime},</if>
            </trim>
            where id = #{item.id}
        </foreach>
    </update>

</mapper>