package com.ywt.mg.domain.entities;

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

public interface OrderRepository extends JpaRepository<Order, Integer> {

    /**
     * 查看某个患者当前有没有正在进行的订单（订单状态，10-待付款,20-已付款）
     * @param doctorId
     * @param patient
     * @param type
     * @return
     */
    @Query(value = "SELECT  COUNT(1) FROM orders o,patient p WHERE o.user_id =  p.user_id AND o.doctor_id = ? AND p.id = ? and o.order_type = ? AND o.is_deleted =0 AND o.order_status in (10,20) limit 1",nativeQuery = true)
    int checkPresentOrder(int doctorId, int patient, int type);

    /**
     * 根据订单业务id和订单类型 查找订单对象
     * @param bussinessId
     * @param type
     * @return
     */
    @Query(value = "SELECT * FROM orders o WHERE o.business_id = ? and o.order_type = ? limit 1",nativeQuery = true)
    Order findByBussinessId(int bussinessId, int type);


}
