<?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.api.app.dao.butler.AppFacilitiesAppointmentDao">

    <select id="list" parameterType="com.api.model.app.SearchAppFacilitiesAppointment" resultType="com.api.vo.app.AppFacilitiesAppointmentVo">
        select * from (
            select sfa.id,sfa.code,sfm.name as facilities_name,sfm.address,sfa.appointment_start_date,sfa.appointment_end_date,sfa.use_end_date,
            (
                case sfa.status = 1 and sfa.appointment_start_date <![CDATA[ < ]]> NOW() when true then 3 else
                (
                    case sfa.status = 2 and sfa.appointment_end_date <![CDATA[ < ]]> NOW() when true then 5 else sfa.status end
                )
            end
            ) as status
            from sys_facilities_appointment as sfa,sys_facilities_manage as sfm
            where sfa.facilities_manage_id = sfm.id
            order by sfa.create_date desc
        ) as T
        <where>
            <if test="facilitiesType == 1">
                and T.status <![CDATA[ < ]]> 3
            </if>
            <if test="facilitiesType == 2">
                and T.status <![CDATA[ >= ]]> 3
            </if>
        </where>
    </select>
    <select id="findCategoryList" resultType="com.api.vo.app.AppFacilitiesCategoryVo">
        select id,name,open_start_date,open_end_date,num from sys_facilities_category where is_delete = 1
    </select>
    <select id="findFacilitiesByCategoryId" parameterType="java.lang.Integer" resultType="com.api.vo.app.IdAndName">
        select id,name from sys_facilities_manage where facilities_category_id = #{categoryId}
    </select>
    <select id="findStatusById" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select status from sys_facilities_appointment where id = #{facilitiesAppointmentId}
    </select>

    <update id="useStop" parameterType="com.api.model.app.AppointmentStopUseFactor">
        update sys_facilities_appointment
        set status = 5,
        use_end_date = #{useEndDate}
        where id = #{facilitiesAppointmentId} and appointment_id = #{id}
    </update>

    <update id="cancel" parameterType="com.api.model.app.AppointmentStopUseFactor">
        update sys_facilities_appointment
        set status = 4
        where id = #{facilitiesAppointmentId}
    </update>

    <select id="findAppointmentStartDateByACAUI" parameterType="com.api.model.app.AppointmentCodeAndUserId" resultType="java.util.Date">
        select appointment_start_date from sys_facilities_appointment where code = #{appointmentCode} and appointment_id = #{id}
    </select>

    <update id="signId" parameterType="com.api.model.app.AppointmentCodeAndUserId">
        update sys_facilities_appointment
        set status = 2
        where code = #{appointmentCode} and appointment_id = #{id} and status = 1
    </update>

    <select id="findFacilitiesAppointmentDate" parameterType="com.api.model.app.AppFacilitiesIdAndNowDate" resultType="com.api.vo.app.AppAppointmentDateVo">
        select sfa.id,sfa.appointment_start_date,sfa.appointment_end_date,ur.name as appointment_name
        from sys_facilities_appointment as sfa,user_resident as ur
        <where>
            sfa.appointment_id = ur.id
            and sfa.facilities_manage_id = #{facilitiesId}
            and sfa.appointment_end_date <![CDATA[ > ]]> #{nowDate}
        </where>
        order by sfa.appointment_start_date
    </select>



</mapper>