<?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.ServerTimeMapper">

    <resultMap type="ServerTime" id="ServerTimeResult">
        <result property="serverId" column="server_id"/>
        <result property="stopOrder" column="stop_order"/>
        <result property="allowEdit" column="allow_edit"/>
        <result property="serverGrap" column="server_grap"/>
        <result property="loginIp" column="login_ip"/>
        <result property="loginDate" column="login_date"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="column1" column="column1"/>
        <result property="column2" column="column2"/>
        <result property="column3" column="column3"/>
    </resultMap>

    <resultMap type="com.younike.system.domain.vo.ServerTimeVo" id="ServerTimeVoResult">
        <result property="serverId" column="server_id"/>
        <result property="stopOrder" column="stop_order"/>
        <result property="allowEdit" column="allow_edit"/>
        <result property="serverGrap" column="server_grap"/>
        <result property="loginIp" column="login_ip"/>
        <result property="loginDate" column="login_date"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="column1" column="column1"/>
        <result property="column2" column="column2"/>
        <result property="column3" column="column3"/>

        <collection property="sectionList" select="serctionServerId" column="server_id"
                    ofType="com.younike.system.domain.ServerTimeSection">
        </collection>
    </resultMap>

    <sql id="selectServerTimeVo">
        select server_id, stop_order, allow_edit, server_grap, login_ip, login_date, del_flag, create_by, create_time, update_by, update_time, column1, column2, column3 from server_time
    </sql>

    <select id="selectServerTimeList" parameterType="ServerTime" resultMap="ServerTimeResult">
        <include refid="selectServerTimeVo"/>
        <where>
            <if test="stopOrder != null ">and stop_order = #{stopOrder}</if>
            <if test="allowEdit != null ">and allow_edit = #{allowEdit}</if>
            <if test="serverGrap != null ">and server_grap = #{serverGrap}</if>
            <if test="loginIp != null  and loginIp != ''">and login_ip = #{loginIp}</if>
            <if test="loginDate != null ">and login_date = #{loginDate}</if>
            <if test="column1 != null  and column1 != ''">and column1 = #{column1}</if>
            <if test="column2 != null  and column2 != ''">and column2 = #{column2}</if>
            <if test="column3 != null  and column3 != ''">and column3 = #{column3}</if>
        </where>
    </select>

    <select id="selectServerTimeByServerId" parameterType="Long" resultMap="ServerTimeResult">
        <include refid="selectServerTimeVo"/>
        where server_id = #{serverId}
    </select>

    <insert id="insertServerTime" parameterType="ServerTime" useGeneratedKeys="true" keyProperty="serverId">
        insert into server_time
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stopOrder != null">stop_order,</if>
            <if test="allowEdit != null">allow_edit,</if>
            <if test="serverGrap != null">server_grap,</if>
            <if test="loginIp != null">login_ip,</if>
            <if test="loginDate != null">login_date,</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="stopOrder != null">#{stopOrder},</if>
            <if test="allowEdit != null">#{allowEdit},</if>
            <if test="serverGrap != null">#{serverGrap},</if>
            <if test="loginIp != null">#{loginIp},</if>
            <if test="loginDate != null">#{loginDate},</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="updateServerTime" parameterType="ServerTime">
        update server_time
        <trim prefix="SET" suffixOverrides=",">
            <if test="stopOrder != null">stop_order = #{stopOrder},</if>
            <if test="allowEdit != null">allow_edit = #{allowEdit},</if>
            <if test="serverGrap != null">server_grap = #{serverGrap},</if>
            <if test="loginIp != null">login_ip = #{loginIp},</if>
            <if test="loginDate != null">login_date = #{loginDate},</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 server_id = #{serverId}
    </update>

    <delete id="deleteServerTimeByServerId" parameterType="Long">
        delete from server_time where server_id = #{serverId}
    </delete>

    <delete id="deleteServerTimeByServerIds" parameterType="String">
        delete from server_time where server_id in
        <foreach item="serverId" collection="array" open="(" separator="," close=")">
            #{serverId}
        </foreach>
    </delete>

    <!-- 根据服务人员id查询服务人员工作时间数据   -->
    <select id="getServerTime" resultMap="ServerTimeVoResult" parameterType="Long">
        select * from server_time where server_id=#{serverId} and del_flag=0
    </select>

    <!--    查询服务人员已经被预约的时间-->
    <select id="getServerOrderTime" resultMap="com.younike.system.mapper.OrderMapper.OrderResult" parameterType="Long">
        SELECT b.*
        FROM order_goods_server a
            LEFT JOIN `order` b
            ON a.order_id = b.order_id
                AND b.del_flag = 0
        WHERE a.del_flag = 0
            AND b.appointment_start_time > now()
            AND b.order_state=001
            AND a.server_id = #{serverId}

    </select>

    <select id="serctionServerId" resultType="com.younike.system.domain.vo.ServerTimeSectionVo">
        select * from server_time_section  where server_id=#{server_id} and del_flag=0
    </select>


</mapper>
