<?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.ruoyi.web.modules.booking.mapper.AppMyAppointmentMapper">

    <resultMap type="com.ruoyi.web.modules.booking.domain.vo.AppMyAppointmentListVo" id="AppMyAppointmentListVoResult">
        <id     property="id"                  column="id"/>
        <result property="providerType"        column="provider_type"/>
        <result property="providerId"          column="provider_id"/>
        <result property="providerName"        column="provider_name"/>
        <result property="appointDate"         column="appoint_date"/>
        <result property="timeStart"           column="time_start"/>
        <result property="timeEnd"             column="time_end"/>
        <result property="serviceAddress"      column="service_address"/>
        <result property="serviceRequirement"  column="service_requirement"/>
        <result property="status"              column="status"/>
        <result property="rejectReason"        column="reject_reason"/>
        <result property="hasReview"           column="has_review"/>
    </resultMap>

    <resultMap type="com.ruoyi.web.modules.diagnosis.domain.BizServiceAppointment" id="BizServiceAppointmentResult">
        <id     property="id"                  column="id"/>
        <result property="providerType"        column="provider_type"/>
        <result property="providerId"          column="provider_id"/>
        <result property="appointeeName"       column="appointee_name"/>
        <result property="appointDate"         column="appoint_date"/>
        <result property="timeStart"           column="time_start"/>
        <result property="timeEnd"             column="time_end"/>
        <result property="contactPhone"        column="contact_phone"/>
        <result property="serviceAddress"      column="service_address"/>
        <result property="serviceRequirement"  column="service_requirement"/>
        <result property="status"              column="status"/>
        <result property="rejectReason"        column="reject_reason"/>
        <result property="createBy"            column="create_by"/>
    </resultMap>

    <sql id="myAppointmentFrom">
        from biz_service_appointment a
        left join biz_medical_resource m
            on a.provider_type in (1, 2) and m.id = a.provider_id and m.del_flag = '0'
        left join biz_tech_resource t
            on a.provider_type = 3 and t.id = a.provider_id and t.del_flag = '0'
    </sql>

    <sql id="statusTabFilter">
        <choose>
            <when test="statusTab == 'notStarted'">
                and a.status in (0, 1, 5)
            </when>
            <when test="statusTab == 'pendingReview'">
                and a.status = 4
                and not exists (
                    select 1 from biz_service_appointment_review r
                    where r.appointment_id = a.id
                )
            </when>
            <when test="statusTab == 'completed'">
                and a.status = 4
            </when>
            <when test="statusTab == 'cancelled'">
                and a.status = 3
            </when>
        </choose>
    </sql>

    <select id="selectMyAppointmentList"
            parameterType="com.ruoyi.web.modules.booking.domain.AppMyAppointmentQuery"
            resultMap="AppMyAppointmentListVoResult">
        select a.id, a.provider_type, a.provider_id,
               coalesce(m.resource_name, t.resource_name) as provider_name,
               a.appoint_date, a.time_start, a.time_end,
               a.service_address, a.service_requirement, a.status, a.reject_reason,
               case when exists (
                   select 1 from biz_service_appointment_review r where r.appointment_id = a.id
               ) then 1 else 0 end as has_review
        <include refid="myAppointmentFrom"/>
        where a.create_by = #{createBy}
          and a.provider_type = #{providerType}
        <include refid="statusTabFilter"/>
        order by a.appoint_date desc, a.create_time desc, a.id desc
    </select>

    <select id="selectMyAppointmentByIdAndUser" resultMap="BizServiceAppointmentResult">
        select a.id, a.provider_type, a.provider_id, a.appointee_name, a.appoint_date,
               a.time_start, a.time_end, a.contact_phone, a.service_address,
               a.service_requirement, a.status, a.reject_reason, a.create_by
        from biz_service_appointment a
        where a.id = #{id}
          and a.create_by = #{createBy}
    </select>

    <update id="cancelMyAppointment">
        update biz_service_appointment
        set status = 3,
            update_by = #{updateBy},
            update_time = sysdate()
        where id = #{id}
          and create_by = #{createBy}
          and status in (0, 1, 5)
    </update>

</mapper>
