package com.ywt.outpatient.taihe.rpc.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;

/**
 * (OfflineConsultation)表Repository类
 *
 * @author makejava
 * @since 2019-04-29 18:33:00
 */
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）
     *
     * @param paymentStatus 支付成功状态
     * @param paymentNo     支付流水号
     * @param id            OfflineConsultation主键
     * @return 更新行数
     */
    @Modifying
    @Transactional
    @Query(value =
            "update offline_consultation " +
                    "set payment_status = ?1" +
                    ", payment_no = ?2" +
                    ", pay_time = now() " +
                    "where id = ?3 and payment_status != ?1"
            , nativeQuery = true)
    int updatePaymentStatus(int paymentStatus, String paymentNo, int id);

    OfflineConsultation getFirstByOrderIdAndDeletedFalse(int orderId);

    OfflineConsultation getById(int id);

    @Query(value = "select * from offline_consultation where reg_id = ? ", nativeQuery = true)
    OfflineConsultation getByRegId(int regId);
}