<?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.diagnosis.mapper.BizServiceAppointmentMapper">

    <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="confirmTime"         column="confirm_time"/>
        <result property="rejectTime"          column="reject_time"/>
        <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="remark"              column="remark"/>
    </resultMap>

    <sql id="selectVo">
        select id, provider_type, provider_id, appointee_name, appoint_date, time_start, time_end,
               contact_phone, service_address, service_requirement, status, reject_reason,
               confirm_time, reject_time, create_by, create_time, update_by, update_time, remark
        from biz_service_appointment
    </sql>

    <select id="selectBizServiceAppointmentById" parameterType="long" resultMap="BizServiceAppointmentResult">
        <include refid="selectVo"/>
        where id = #{id}
    </select>

    <select id="selectVetAppointmentByIdAndProvider" resultMap="BizServiceAppointmentResult">
        <include refid="selectVo"/>
        where id = #{id}
          and provider_type = 1
          and provider_id = #{providerId}
    </select>

    <select id="selectVetAppointmentList" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizServiceAppointment"
            resultMap="BizServiceAppointmentResult">
        <include refid="selectVo"/>
        <where>
            provider_type = #{providerType}
            and provider_id = #{providerId}
            <if test="appointeeName != null and appointeeName != ''">
                and appointee_name like concat('%', #{appointeeName}, '%')
            </if>
            <if test="startDate != null and startDate != ''">
                and appoint_date &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != ''">
                and appoint_date &lt;= #{endDate}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
        </where>
        order by appoint_date desc, create_time desc, id desc
    </select>

    <select id="selectOrgAppointmentByIdAndProvider" resultMap="BizServiceAppointmentResult">
        <include refid="selectVo"/>
        where id = #{id}
          and provider_type = 2
          and provider_id = #{providerId}
    </select>

    <select id="selectOrgAppointmentList" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizServiceAppointment"
            resultMap="BizServiceAppointmentResult">
        <include refid="selectVo"/>
        <where>
            provider_type = #{providerType}
            and provider_id = #{providerId}
            <if test="appointeeName != null and appointeeName != ''">
                and appointee_name like concat('%', #{appointeeName}, '%')
            </if>
            <if test="startDate != null and startDate != ''">
                and appoint_date &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != ''">
                and appoint_date &lt;= #{endDate}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
        </where>
        order by appoint_date desc, create_time desc, id desc
    </select>

    <select id="selectExpertAppointmentByIdAndProvider" resultMap="BizServiceAppointmentResult">
        <include refid="selectVo"/>
        where id = #{id}
          and provider_type = 3
          and provider_id = #{providerId}
    </select>

    <select id="selectExpertAppointmentList" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizServiceAppointment"
            resultMap="BizServiceAppointmentResult">
        <include refid="selectVo"/>
        <where>
            provider_type = #{providerType}
            and provider_id = #{providerId}
            <if test="appointeeName != null and appointeeName != ''">
                and appointee_name like concat('%', #{appointeeName}, '%')
            </if>
            <if test="startDate != null and startDate != ''">
                and appoint_date &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != ''">
                and appoint_date &lt;= #{endDate}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
        </where>
        order by appoint_date desc, create_time desc, id desc
    </select>

    <update id="updateConfirmExpertAppointment" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizServiceAppointment">
        update biz_service_appointment
        set status = 1,
            confirm_time = #{confirmTime},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where id = #{id}
          and provider_type = 3
          and provider_id = #{providerId}
          and status = 0
    </update>

    <update id="updateRejectExpertAppointment" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizServiceAppointment">
        update biz_service_appointment
        set status = 2,
            reject_reason = #{rejectReason},
            reject_time = #{rejectTime},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where id = #{id}
          and provider_type = 3
          and provider_id = #{providerId}
          and status = 0
    </update>

    <update id="updateCompleteExpertAppointment" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizServiceAppointment">
        update biz_service_appointment
        set status = 4,
            update_by = #{updateBy},
            update_time = #{updateTime}
        where id = #{id}
          and provider_type = 3
          and provider_id = #{providerId}
          and status = 1
    </update>

    <update id="updateConfirmVetAppointment" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizServiceAppointment">
        update biz_service_appointment
        set status = 1,
            confirm_time = #{confirmTime},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where id = #{id}
          and provider_type = 1
          and provider_id = #{providerId}
          and status = 0
    </update>

    <update id="updateRejectVetAppointment" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizServiceAppointment">
        update biz_service_appointment
        set status = 2,
            reject_reason = #{rejectReason},
            reject_time = #{rejectTime},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where id = #{id}
          and provider_type = 1
          and provider_id = #{providerId}
          and status = 0
    </update>

    <update id="updateCompleteVetAppointment" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizServiceAppointment">
        update biz_service_appointment
        set status = 4,
            update_by = #{updateBy},
            update_time = #{updateTime}
        where id = #{id}
          and provider_type = 1
          and provider_id = #{providerId}
          and status = 1
    </update>

    <update id="updateCompleteOrgAppointment" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizServiceAppointment">
        update biz_service_appointment
        set status = 4,
            update_by = #{updateBy},
            update_time = #{updateTime}
        where id = #{id}
          and provider_type = 2
          and provider_id = #{providerId}
          and status = 5
    </update>

    <insert id="insertBizServiceAppointment" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizServiceAppointment"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_service_appointment (
            provider_type, provider_id, appointee_name, appoint_date,
            time_start, time_end, contact_phone, service_address, service_requirement,
            status, create_by, create_time
        ) values (
            #{providerType}, #{providerId}, #{appointeeName}, #{appointDate},
            #{timeStart}, #{timeEnd}, #{contactPhone}, #{serviceAddress}, #{serviceRequirement},
            #{status}, #{createBy}, sysdate()
        )
    </insert>

    <select id="countActiveAppointmentsOnDate" resultType="int">
        select count(1)
        from biz_service_appointment
        where provider_type = #{providerType}
          and provider_id = #{providerId}
          and appoint_date = #{appointDate}
          and status not in (2, 3)
    </select>

    <select id="countMyActiveAppointmentOnDate" resultType="int">
        select count(1)
        from biz_service_appointment
        where provider_type = #{providerType}
          and provider_id = #{providerId}
          and appoint_date = #{appointDate}
          and create_by = #{createBy}
          and status not in (2, 3)
    </select>
</mapper>
