package com.ywt.mg.domain.ywtDrugEntities;

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

import java.util.List;

public interface PrescriptionInfoRepository extends JpaRepository<PrescriptionInfo, Integer>{

    /**
     * 根据处方编码得到处方对象
     * @param preBizNo
     * @return
     */
    @Query(value = "select * from ywt_drug.prescription_info where pre_biz_no = ? limit 1 ", nativeQuery = true)
    PrescriptionInfo findOneByPreBizNo(String preBizNo);


    /**
     * 判断患者是否有处方单记录
     *
     * @param patientName
     * @param patientNo
     * @return
     */
    @Query(value = "select count(1) from ywt_drug.prescription_info where patient_name = ? or patient_no = ? limit 1", nativeQuery = true)
    int checkCount(String patientName,String patientNo);

    /**
     *  药房药师点击“发药”，过了当天00：00后，患者端（自取）处方订单状态才变成已完成。（排除广三的）
     *  source 参考字段{@link com.ywt.mg.domain.models.enums.PrescriptionInfoSourceEnum}
     *  找到符合条件的记录，关键点：支付成功,自取，已发药，订单状态（配送中），小于当前时间
     * @return
     */
    @Query(value = "select * from ywt_drug prescription_info where source != 1 and payment_status = 2 and delivery_method = 2 and status = 2 and ((ext_status & 2) = 2) and start_send_time < now(); ", nativeQuery = true)
    List<PrescriptionInfo> getScheduledList();

    /**
     * 根据处方编码得到处方对象
     * @param orderNo
     * @return
     */
    @Query(value = "select * from ywt_drug.prescription_info where order_no = ? limit 1 ", nativeQuery = true)
    PrescriptionInfo findOneByOrderNo(String orderNo);

    @Query(value = "select * from ywt_drug.prescription_info where order_id = ? limit 1 ", nativeQuery = true)
    PrescriptionInfo findOneByOrderId(int orderId);
}
