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 AttendingOrderViewRepository extends JpaRepository<AttendingOrderView, Integer> {

    /**
     * 查看某个患者当前有没有正在进行的订单（订单状态，10-待付款,20-已付款），主诊
     * @param doctorId
     * @param patientId
     * @return
     */
    @Query(value = "SELECT  * FROM attending_order_view o WHERE  o.from_doctor_id = ? AND o.patient_id = ? and o.order_type = 3 AND o.is_deleted = 0 AND o.order_status in (10,20) ORDER BY o.create_time DESC limit 1",nativeQuery = true)
    List<AttendingOrderView> checkAttendingPatientOrder(int doctorId, int patientId);

    /**
     * 查看当前某个患者是否有正在进行的订单
     *
     * @param doctorId
     * @param patientId
     * @return
     */
    @Query(value = "SELECT  COUNT(1) FROM attending_order_view o WHERE  o.from_doctor_id = ? AND o.patient_id = ? and o.order_type = 3 AND o.is_deleted = 0 AND o.order_status in (10,20) ORDER BY o.create_time DESC limit 1",nativeQuery = true)
    int checkPresentAttendingTreatmentOrder(int doctorId, int patientId);

}
