package com.ywt.outpatient.domain.entity.center;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface FollowUpOrderRepository extends JpaRepository<FollowUpOrder, Integer> {
    @Query(value = "select * from follow_up_order where doctor_id = ? and patient_id = ? and deleted = 0 order by create_time desc limit 1;", nativeQuery = true)
    FollowUpOrder getLastFollowUpOrder(int doctorId, int patientId);

    /**
     * 得到随访中的随访订单
     */
    @Query(value = "select * from follow_up_order where doctor_id = ? and create_time > ? and deleted = 0 order by create_time desc ;", nativeQuery = true)
    List<FollowUpOrder> getUnderwayFollowUpORderList(int doctorId, String date);

    /**
     * 得到已结束的随访订单
     */
    @Query(value = "select * from follow_up_order where doctor_id = ? and create_time <= ? and deleted = 0 order by create_time desc ;", nativeQuery = true)
    List<FollowUpOrder> getFinishFollowUpORderList(int doctorId, String date);

    /**
     * 得到随访中的随访订单数量
     */
    @Query(value = "select count(0) from follow_up_order where doctor_id = ? and date_add(create_time, interval 1 day) > now() and follow_up_status = 1 and deleted = 0;", nativeQuery = true)
    int getAwaitFollowUpCountByDoctorId(int doctorId);

    @Query(value = "select * from follow_up_order where date_add(create_time, interval 1 day) < now() and follow_up_status = 1 and deleted = 0 order by create_time;", nativeQuery = true)
    List<FollowUpOrder> getEndingList();
}
