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;
import java.util.List;

public interface ConsultOrderRepository extends JpaRepository<ConsultOrder, Integer> {
    @Query(value = "select * from consult_order where id = ? and deleted = 0 limit 1;", nativeQuery = true)
    ConsultOrder getConsultOrderById(int id);

    /**
     * 待接诊数量
     *
     * @param doctorId
     * @return
     */
    @Query(value = "select count(0) from consult_order where doctor_id = ? and payment_status = ? and consult_status >= ? and consult_status < ? and ( update_time >= ? or create_time >= ?  ) and deleted = 0  ", nativeQuery = true)
    int getCountByDoctorId(int doctorId, int pamentStatus, int consultStatus, int doubleConsultStatus, String dateTime1, String dateTime2);

    /**
     * 根据支付状态还咨询订单状态查询订单记录
     *
     * @param doctorId
     * @return
     */
    @Query(value = "select * from consult_order where doctor_id = ? and payment_status = ? and consult_status >= ? and consult_status < ?  and deleted = 0  ", nativeQuery = true)
    List<ConsultOrder> getConsultOrderListByDoctorId(int doctorId, int pamentStatus, int consultStatus, int doubleConsultStatus);


    /**
     * 接诊中
     *
     * @param doctorId
     * @return
     */
    @Query(value = "select * from consult_order where doctor_id = ? and payment_status = ? and consult_status >= ? and consult_status < ? and accept_time  >= ? and deleted = 0  ", nativeQuery = true)
    List<ConsultOrder> getConsultOrderListByDoctorId(int doctorId, int pamentStatus, int consultStatus, int doubleConsultStatus, String dataTime);


    /**
     * 接诊中 (两天之内，则接诊的时间要大于两天之前的时间)
     *
     * @param doctorId
     * @return
     */
    @Query(value = "select count(1) from consult_order where doctor_id = ? and payment_status = ? and consult_status >= ? and consult_status < ? and accept_time >= ? and deleted = 0  ", nativeQuery = true)
    int getConsultOrderCountByDoctorId(int doctorId, int pamentStatus, int consultStatus, int doubleConsultStatus, String dataTime);


    /**
     * 已结束但未结算的咨询订单
     *
     * @return
     */
    @Query(value = "select * from consult_order where consult_status & 128 <> 128 and consult_status & 64 <> 64 and (consult_status & 16 = 16 or (consult_status = 5 and TO_DAYS(NOW()) - TO_DAYS(accept_time) >= 2)) and deleted = 0;", nativeQuery = true)
    List<ConsultOrder> getConsultOrderList();

    List<ConsultOrder> getListByDoctorIdAndUserId(int doctorId, int userId);

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

    @Query(value = "select count(0) from consult_order where activity_id = ? and user_id = ?", nativeQuery = true)
    int countByActivityIdAndUserId(int activityId, int userId);

    @Query(value = "select count(0) from consult_order where activity_id = ?", nativeQuery = true)
    int countByActivityId(int activityId);

    /**
     * 根据 orderId 获取 ConsultOrder
     *
     * @param orderId 订单ID
     * @return
     */
    ConsultOrder getByOrderIdAndDeletedFalse(int orderId);

    @Query(value = "select * from consult_order where refund_no = ? and deleted = 0 limit 1;", nativeQuery = true)
    ConsultOrder getConsultOrderByRefundNo(String refundNo);
}
