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 CheckResultOrderRepository extends JpaRepository<CheckResultOrder, Integer> {

    /**
     * 根据医院ID得到支付且尚未回传url的记录
     *
     * @return {@link  List<CheckResultOrder>}
     */
    @Query(value = "select * from check_result_order where hospital_id = ? and payment_status = 2  and deleted = 0 and (pacs_view_url is null or pacs_view_url = ''); ", nativeQuery = true)
    List<CheckResultOrder> getPaymentAndNoUrlList(int hospitalId);

    /**
     * 根据accessionNumber（检查单号）得到相应的记录
     *
     * @return {@link  CheckResultOrder}
     */
    @Query(value = "select * from check_result_order where accession_number = ? and deleted = 0 limit 1; ", nativeQuery = true)
    CheckResultOrder findOneByAccessionNumber(String accessionNumber);

    @Query(value = "select * from check_result_order where check_result_id = ? and (status = 2 or status = 3) and deleted = 0 order by create_time desc limit 1; ", nativeQuery = true)
    CheckResultOrder findOnePaidOrderByReportId(String reportId);

    @Query(value = "select * from check_result_order where check_result_id = ? and (status = 2 or status = 3) and hospital_id = ? and deleted = 0 order by create_time desc limit 1; ", nativeQuery = true)
    CheckResultOrder findOnePaidOrderByReportIdAndHospitalId(String reportId, int hospitalId);
}