package com.ywt.mg.domain.entities;

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

public interface NatOrderRepository extends JpaRepository<NatOrder, Integer>, JpaSpecificationExecutor<NatOrder> {

    @Query(value = "select * from nat_order where deleted = 0 and id = ? limit 1;", nativeQuery = true)
    NatOrder findOneById(int id);

    @Query(value = "select * from nat_order where deleted = 0 and payment_code = ? order by id desc limit 1;", nativeQuery = true)
    NatOrder findOneByPaymentCode(String paymentCode);

    /**
     * 判断其他用户是否有相同的疫苗码（或者是判断疫苗码已被其他用户使用）
     * @param id 核酸检测缴费订单ID
     * @param vaccineCode 疫苗码
     * @return 1-存在，0-不存在
     */
    @Query(value = "select count(1) from nat_order where deleted = 0 and id != ? and vaccine_code = ? limit 1;", nativeQuery = true)
    int checkVaccineCodeExist(int id, String vaccineCode);
}