<?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.ServerTimeSectionMapper">
    
    <resultMap type="ServerTimeSectionVo" id="ServerTimeSectionResultVo" extends="ServerTimeSectionResult">

    </resultMap>

    <update id="updateServerTimeSectionVo">
        update server_time_section
        <trim prefix="SET" suffixOverrides=",">
            <if test="serverId != null and serverId != ''">server_id = #{serverId},</if>
            <if test="weeks != null">weeks = #{weeks},</if>
            <if test="hoursFromString != null">hours_from = #{hoursFromString},</if>
            <if test="hoursEndString != null">hours_end = #{hoursEndString},</if>
            <if test="isEnable != null">is_enable = #{isEnable},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="column1 != null">column1 = #{column1},</if>
            <if test="column2 != null">column2 = #{column2},</if>
            <if test="column3 != null">column3 = #{column3},</if>
        </trim>
        where section_id = #{sectionId}
    </update>
    <insert id="insertServerTimeSectionVo" >
        insert into server_time_section
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sectionId != null">section_id,</if>
            <if test="serverId != null and serverId != ''">server_id,</if>
            <if test="weeks != null">weeks,</if>
            <if test="hoursFromString != null">hours_from,</if>
            <if test="hoursEndString != null">hours_end,</if>
            <if test="isEnable != null">is_enable,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="column1 != null">column1,</if>
            <if test="column2 != null">column2,</if>
            <if test="column3 != null">column3,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sectionId != null">#{sectionId},</if>
            <if test="serverId != null and serverId != ''">#{serverId},</if>
            <if test="weeks != null">#{weeks},</if>
            <if test="hoursFromString != null">#{hoursFromString},</if>
            <if test="hoursEndString != null">#{hoursEndString},</if>
            <if test="isEnable != null">#{isEnable},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="column1 != null">#{column1},</if>
            <if test="column2 != null">#{column2},</if>
            <if test="column3 != null">#{column3},</if>
        </trim>
    </insert>
    <update id="deleteServerTimeSectionByServerId">
        update server_time_section
        SET
            del_flag = '1'
        where server_id = #{serverId}
        and is_enable = 1
    </update>
    <!--    根据员工工作日查询工作时间-->
    <select id="selectSectionWeek" resultMap="ServerTimeSectionResult" parameterType="com.younike.system.domain.ServerTimeSection">
        SELECT
        sts.section_id,
        sts.server_id,
        sts.weeks,
        sts.hours_from,
        sts.hours_end,
        sts.is_enable,
        sts.del_flag,
        sts.create_by,
        sts.create_time,
        sts.update_by,
        sts.update_time,
        sts.column1,
        sts.column2,
        sts.column3
        FROM
        server_time_section sts
        LEFT JOIN server_time st ON st.server_id = sts.server_id
        WHERE
        sts.del_flag = 0 AND st.stop_order = 0
        <if test="serverId != null  and serverId != ''"> and sts.server_id = #{serverId}</if>
        <if test="weeks != null  and weeks != ''"> and sts.weeks like concat('%',#{weeks},'%')</if>
        <if test="hoursFrom != null "> and sts.hours_from = #{hoursFrom}</if>
        <if test="hoursEnd != null "> and sts.hours_end = #{hoursEnd}</if>
        LIMIT 1
    </select>
    <select id="selectAllSectionBy" resultType="com.younike.system.domain.ServerTimeSection">
        <include refid="selectServerTimeSectionVo"/>
        <where>
            del_flag = '0'
            and find_in_set(#{dayOfWeek}, weeks)
        </where>
    </select>
    <select id="selectSectionBy" resultType="com.younike.system.domain.ServerTimeSection">
        <include refid="selectServerTimeSectionVo"/>
        <where>
            del_flag = '0'
            and find_in_set(#{dayOfWeek}, weeks)
            and server_id = #{serverId}
        </where>
        order by hours_from asc
    </select>
</mapper>