package org.jebot.repository.xxpay;

import org.jebot.models.xxpay.PayOrder;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component
public interface PayOrderRepository extends JpaRepository<PayOrder, String> {

    //通过支付单号查询支付订单
    @Query("SELECT t FROM t_pay_order t WHERE t.payOrderId =:orderId  OR t.channelOrderNo =:orderId ")
    PayOrder findByPayOrderIdOrChannelOrderNo(@Param("orderId") String orderId);

    //通过支付单号查询支付订单
    @Query("SELECT t FROM t_pay_order t WHERE (t.payOrderId =:orderId OR t.mchOrderNo =:orderId) and t.mchId =:mchId")
    PayOrder findByPayOrderIdOrMchOrderNo(@Param("orderId") String orderId, @Param("mchId") Long mchId);

    //通过支付单号查询支付订单
//    @Query("SELECT t FROM t_pay_order t WHERE t.payOrderId =:orderId OR t.mchOrderNo =:orderId OR t.channelOrderNo =:orderId ")
//    PayOrder findByPayOrderIdOrMchOrderNoOrChannelOrderNo(@Param("orderId") String orderId);

    @Query("SELECT new org.jebot.models.xxpay.PayOrder( tpo.amount,tpo.mchId,tpo.mchRate,tpo.passageId,tpo.productId,tpo.status,tpo.createTime) FROM t_pay_order as tpo WHERE tpo.mchId =:mchId and tpo.createTime >= :startTime and tpo.createTime <= :endTime")
    List<PayOrder> findPayOrderByMchIdAndCreateTime(Long mchId, Date startTime, Date endTime);

    @Query("SELECT new org.jebot.models.xxpay.PayOrder( tpo.amount,tpo.mchId,tpo.mchRate,tpo.passageId,tpo.status,tpo.createTime) FROM t_pay_order as tpo WHERE  tpo.mchId =:mchId and tpo.status in (2,3) and tpo.createTime >= :startTime and tpo.createTime <= :endTime")
    List<PayOrder> findSuccessPayOrderByMchIdAndCreateTime(Long mchId, Date startTime, Date endTime);

    @Query("SELECT new org.jebot.models.xxpay.PayOrder( tpo.amount,tpo.mchId,tpo.mchRate,tpo.passageId,tpo.status,tpo.createTime) FROM t_pay_order as tpo WHERE  tpo.passageId =:passageId and tpo.status in (2,3) and tpo.createTime >= :startTime and tpo.createTime <= :endTime")
    List<PayOrder> findSuccessPayOrderByPassageIdAndCreateTime(Long passageId, Date startTime, Date endTime);

    @Query("SELECT new org.jebot.models.xxpay.PayOrder( tpo.amount,tpo.mchId,tpo.mchRate,tpo.payPassageRate,tpo.passageId,tpo.status,tpo.createTime) FROM t_pay_order as tpo WHERE tpo.passageId in (:passageIds) and tpo.createTime >= :startTime and tpo.createTime <= :endTime and tpo.status in(2,3)")
    List<PayOrder> findPayOrderByPassageAndCreateTime(List<Long> passageIds, Date startTime, Date endTime);

    @Query("SELECT new org.jebot.models.xxpay.PayOrder( tpo.amount,tpo.mchId,tpo.mchRate,tpo.payPassageRate,tpo.passageId,tpo.status,tpo.createTime) FROM t_pay_order as tpo WHERE tpo.passageId in (:passageIds) and tpo.createTime >= :startTime and tpo.createTime <= :endTime")
    List<PayOrder> findPayOrderByPassageAndCreateTimeAll(List<Long> passageIds, Date startTime, Date endTime);

    @Query("SELECT SUM(tpo.amount) FROM t_pay_order as tpo WHERE tpo.mchId =:mchId AND tpo.createTime >=:createTime and tpo.status in(2,3)")
    BigDecimal findPayOrderAmountByMchIdAndCreateTime(@Param("mchId") Long mchId, @Param("createTime") Date createTime);

}
