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.IncomeRecord;
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;
import java.util.List;

@Mapper
public interface IncomeRecordRepository extends BaseMapper<IncomeRecord> {

    IPage<IncomeRecord> selectPageList(Page<IncomeRecord> page,
                                      @Param("orderNo") String orderNo,
                                      @Param("shortId") String shortId,
                                      @Param("platform") String platform,
                                      @Param("shopId") Long shopId,
                                      @Param("status") String status,
                                      @Param("startDate") LocalDateTime startDate,
                                      @Param("endDate") LocalDateTime endDate);

    IncomeRecord findByIncomeNo(@Param("incomeNo") String incomeNo);

    IncomeRecord findByOrderNo(@Param("orderNo") String orderNo);

    List<IncomeRecord> findBySourceId(@Param("sourceId") Long sourceId, @Param("source") String source);

    @Select("SELECT SUM(actual_amount) FROM fin_income_record WHERE status = 'confirmed' AND payment_time >= #{startTime} AND payment_time <= #{endTime}")
    BigDecimal sumConfirmedAmount(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    @Select("SELECT SUM(actual_amount) FROM fin_income_record WHERE status = 'confirmed' AND shop_id = #{shopId} AND payment_time >= #{startTime} AND payment_time <= #{endTime}")
    BigDecimal sumConfirmedAmountByShop(@Param("shopId") Long shopId, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
}
