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

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

/**
 * (OutpatientOrder)表Repository类
 *
 * @author johnson lin
 * @since 2019-05-29 15:51:59
 */
public interface OutpatientOrderRepository extends JpaRepository<OutpatientOrder, Integer> {

    /**
     * 根据订单ID得到门诊缴费订单
     *
     * @param orderId 订单ID
     * @return
     */
    OutpatientOrder getFirstByOrderIdAndDeletedFalse(int orderId);

    @Query(value = "select * from outpatient_order where his_order_no = ? and deleted = 0 limit 1", nativeQuery = true)
    OutpatientOrder getByHisOrderNo(String hisOrderNo);

    @Query(value = "select * from ywt_center.outpatient_order where doctor_code = ? and user_id = ? and payment_status = 2 order by create_time desc limit 1;", nativeQuery = true)
    OutpatientOrder getLatestOutpatientOrder(String doctorCode, int userId);
    
    @Query(value = "select * from ywt_center.outpatient_order where doctor_code = ? and user_id = ? and hospital_id = ? and payment_status = 2 order by create_time desc limit 1;", nativeQuery = true)
    OutpatientOrder getLatestOutpatientOrder(String doctorCode, int userId, int hospitalId);

    @Query(value = "select * from outpatient_order where invoice_no = ? and deleted = 0 limit 1", nativeQuery = true)
    OutpatientOrder getByInvoiceNo(String invoiceNo);

}