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

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

import javax.transaction.Transactional;

public interface OfflineConsultationRepository extends JpaRepository<OfflineConsultation, Integer> {

    @Modifying
    @Transactional
    @Query(value = "update offline_consultation set order_id = ?1, order_no = ?2 where id = ?3", nativeQuery = true)
    void updateOrderIdAndOrderNoById(int orderId, String orderNo, int id);

    OfflineConsultation getFirstByOrderIdAndDeletedFalse(int orderId);

    @Query(value = "select * from ywt_center.offline_consultation where doctor_id = ? and user_id = ? and payment_status = 2 order by create_time desc limit 1;", nativeQuery = true)
    OfflineConsultation getLatestOfflineConsultation(int doctorId, int userId);

    @Query(value = "select * from ywt_center.offline_consultation where doctor_id = ? and user_id = ? and hospital_id = ? and payment_status = 2 order by create_time desc limit 1;", nativeQuery = true)
    OfflineConsultation getLatestOfflineConsultation(int doctorId, int userId, int hospitalId);

    OfflineConsultation getByRefundNo(String refundNo);

    @Query(value = "select * from ywt_center.offline_consultation where reg_id = ? and deleted = 0 order by create_time desc limit 1;", nativeQuery = true)
    OfflineConsultation getByRegId(int regId);
}
