package com.bizzan.bitrade.dao;

import com.bizzan.bitrade.dao.base.BaseDao;
import com.bizzan.bitrade.entity.ExperienceGiftRecord;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

@Repository
public interface ExperienceGiftRecordDao extends BaseDao<ExperienceGiftRecord>, ExperienceGiftRecordDaoCustom {

    /** 累计领取面值（前端：累计领取体验金） */
    @Query(value = "SELECT COALESCE(SUM(amount), 0) FROM experience_gift_record WHERE member_id = :memberId", nativeQuery = true)
    BigDecimal sumTotalReceivedByMemberId(@Param("memberId") Long memberId);

    /** 累计平仓盈利（status=2 且 pnl>0 之和） */
    @Query(value = "SELECT COALESCE(SUM(pnl), 0) FROM experience_gift_record WHERE member_id = :memberId AND status = 2 AND pnl > 0", nativeQuery = true)
    BigDecimal sumProfitByMemberId(@Param("memberId") Long memberId);

    /** 邀请触发体验金累计领取面值（grant_type=1） */
    @Query(value = "SELECT COALESCE(SUM(amount), 0) FROM experience_gift_record WHERE member_id = :memberId AND grant_type = 1", nativeQuery = true)
    BigDecimal sumInviteReceivedByMemberId(@Param("memberId") Long memberId);

    /** 累计平仓亏损绝对值（pnl < 0） */
    @Query(value = "SELECT COALESCE(SUM(ABS(pnl)), 0) FROM experience_gift_record WHERE member_id = :memberId AND pnl < 0", nativeQuery = true)
    BigDecimal sumLossByMemberId(@Param("memberId") Long memberId);
}
