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.RefundRecord;
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 RefundRecordRepository extends BaseMapper<RefundRecord> {

    IPage<RefundRecord> selectPageList(Page<RefundRecord> page,
                                       @Param("orderNo") String orderNo,
                                       @Param("status") String status,
                                       @Param("type") String type,
                                       @Param("startDate") LocalDateTime startDate,
                                       @Param("endDate") LocalDateTime endDate);

    RefundRecord findByRefundNo(@Param("refundNo") String refundNo);

    RefundRecord findBySourceId(@Param("sourceId") Long sourceId, @Param("source") String source);

    @Select("SELECT SUM(refund_amount) FROM fin_refund_record WHERE status = 'completed' AND refund_time >= #{startTime} AND refund_time <= #{endTime}")
    BigDecimal sumCompletedRefundAmount(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    @Select("SELECT COUNT(*) FROM fin_refund_record WHERE status = 'pending'")
    long countPendingRefunds();
}
