package com.ywt.mg.domain.entities;

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

import java.util.List;

public interface OutpatientOrderRepository extends JpaRepository<OutpatientOrder, Integer> {
    @Query(value = "select count(0) from outpatient_order where his_order_no = ? and deleted = 0", nativeQuery = true)
    int count(String hisOrderNo);

    @Query(value = "select * from outpatient_order where id >= ? and id <= ? and deleted = 0 order by id;", nativeQuery = true)
    List<OutpatientOrder> getList(int minId, int maxId);

    /**
     * 获取昨日门诊缴费单
     *
     * @return {@link List< OutpatientOrder >}
     */
    @Query(value = "select * from outpatient_order where to_days(now()) - to_days(create_time) = 1 and deleted = 0 order by id;", nativeQuery = true)
    List<OutpatientOrder> getYesterdayList();

    /**
     * 根据订单ID得到门诊缴费订单
     *
     * @param orderId 订单表ID
     * @return {@link OutpatientOrder}
     */
    @Query(value = "select oo.* from orders o , outpatient_order oo where o.id = ? and o.order_type = 8 and o.business_id = oo.id  and oo.deleted = 0 limit 1 ;", nativeQuery = true)
    OutpatientOrder getOneByOrderId(int orderId);


    /**
     * 根据门诊缴费订单ID得到门诊缴费订单
     *
     * @param outId 门诊缴费订单ID
     * @return {@link OutpatientOrder}
     */
    @Query(value = "select * from outpatient_order where id = ? and deleted = 0 limit 1;", nativeQuery = true)
    OutpatientOrder findOutpatientOrderByOutId(int outId);


}
