package com.izouma.nineth.repo;

import com.izouma.nineth.domain.PaymentInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.math.BigDecimal;
import java.util.Optional;

public interface PaymentInfoRepo extends JpaRepository<PaymentInfo, Long>, JpaSpecificationExecutor<PaymentInfo> {


    /**
     * 计数
     * @param orderNo
     * @return
     */
    Integer countByOrderNo(String orderNo);

    Optional<PaymentInfo> findByOrderNo(String orderNo);

    /**
     * 检查支付中的记录数量
     * @param orderId
     * @return
     */
    @Query("select count(pi) from PaymentInfo pi where pi.status = com.izouma.nineth.enums.PayStatus.PENDING and pi.orderId = ?1 ")
    Integer countPendingStatus(Long orderId);

    /**
     * 检查已支付的记录数量
     * @param orderId
     * @return
     */
    @Query("select count(pi) from PaymentInfo pi where pi.status = com.izouma.nineth.enums.PayStatus.HAVE_PAID and pi.orderId = ?1 ")
    Integer countHavePaidStatus(Long orderId);


    @Query(nativeQuery = true ,value = "select sum(pay_amt) from payment_info pi where date(pi.created_at) = date_sub(curdate(), interval 1 day) and pi.pay_method = 'WALLET' and pi.status = 'HAVE_PAID'")
    BigDecimal lastDayWalletPayNum();

    @Query(nativeQuery = true ,value = "select sum(pay_amt) from payment_info pi where date(pi.created_at) = date_sub(curdate(), interval 1 day) and pi.pay_method = 'BANK' and pi.status = 'HAVE_PAID'")
    BigDecimal lastDayBankPayNum();
}
