package com.yn.bftl.thirdparty.common.repository;

import com.yn.bftl.common.modules.account.entity.Payment;
import com.yn.bftl.common.modules.order.enums.PayStatus;
import com.yn.bftl.common.modules.order.enums.PaymentMethod;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

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

@Repository
public interface PaymentRepository extends JpaRepository<Payment, Long> {

    /**
     * 通过第三方单号查询最后一条数据
     *
     * @param externalId
     * @return
     */
    Payment findFirstByExternalIdOrderByIdDesc(String externalId);

    Payment findFirstByRefundSnOrderByIdDesc(String refundSn);

    Payment findFirstBySnOrderByIdDesc(String sn);

    Payment findFirstByOrderIdAndPayStatusOrderByIdDesc(String orderId, PayStatus payStatus);

    List<Payment> findAllByOrderId(String orderId);

    Payment findFirstByOrderIdOrderByCreatedOnDesc(String orderId);

    /**
     * 支付单-判断该银联订单号是否存在
     *
     * @param unionpaySn:银联订单号
     * @return true:存在、false:未存在
     * @author huabiao
     * @create 2022/9/5  14:16
     */
    Boolean existsByUnionpaySn(String unionpaySn);

    /**
     * 支付单-根据银联订单号查询
     *
     * @param unionpaySn:银联订单号
     * @return 支付单
     * @author huabiao
     * @create 2022/9/6  19:10
     */
    Payment findByUnionpaySn(String unionpaySn);

    @Query("select p from Payment p inner join Order o on p.orderId = o.id where o.sn = :sn order by p.createdOn desc")
    List<Payment> findByOrderSn(String sn);

    @Query("select p from Payment p inner join TopUpOrder t on p.topUpOrderId = t.id where t.sn = :sn and p.paymentType = 'RECHARGE' order by p.createdOn desc")
    List<Payment> findByTopUpOrderSn(String sn);

    Payment findFirstByTopUpOrderIdAndPayStatusOrderByIdDesc(Long topUpOrderId, PayStatus payStatus);

    /**
     * 支付单-查询
     *
     * @param orderId       订单ID
     * @param payStatus     支付状态
     * @param paymentMethod 支付方式
     * @return 支付单
     * @author huabiao
     * @create 2023/3/1  2:37
     */
    Payment findFirstByOrderIdAndPayStatusAndPaymentMethod(String orderId, PayStatus payStatus, PaymentMethod paymentMethod);

    /**
     * 支付单-查询
     *
     * @param orderId       订单ID
     * @param paymentMethod 支付方式
     * @return 支付单
     */
    Payment findFirstByOrderIdAndPaymentMethod(String orderId, PaymentMethod paymentMethod);

    /**
     * 支付单-根据购买企业获取待处理支付金额
     *
     * @param buyerCompanyId 购买企业ID
     * @return 待处理支付总金额
     * @description
     * @author huabiao
     * @create 2023/8/21  10:51
     */
    @Query(value = "SELECT COALESCE(SUM(payment.amount),0) FROM Payment payment " +
            " WHERE payment.orderId in (SELECT  orders.id FROM Order orders WHERE orders.buyerCompany.id = ?1 AND orders.orderStatus = 'CREATED' AND orders.paymentStatus = 'UN_PAID')" +
            " AND payment.payStatus = 'PROGRESS' AND payment.paymentMethod = 'DEPOSIT' ")
    BigDecimal getProgressPaymentAmount(Long buyerCompanyId);

}


