package com.erp.finance.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.finance.entity.PaymentRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Mapper
public interface PaymentRecordRepository extends BaseMapper<PaymentRecord> {

    IPage<PaymentRecord> selectPageList(Page<PaymentRecord> page,
                                        @Param("apNo") String apNo,
                                        @Param("purchaseOrderNo") String purchaseOrderNo,
                                        @Param("supplierId") Long supplierId,
                                        @Param("status") String status,
                                        @Param("purpose") String purpose,
                                        @Param("startDate") LocalDateTime startDate,
                                        @Param("endDate") LocalDateTime endDate);

    PaymentRecord findByPaymentNo(@Param("paymentNo") String paymentNo);

    PaymentRecord findByApId(@Param("apId") Long apId);

    @Select("SELECT SUM(amount) FROM fin_payment_record WHERE status = 'completed' AND payment_time >= #{startTime} AND payment_time <= #{endTime}")
    BigDecimal sumCompletedPaymentAmount(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
}
