package com.starry.admin.modules.withdraw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.starry.admin.modules.withdraw.entity.EarningsLineEntity;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface EarningsLineMapper extends BaseMapper<EarningsLineEntity> {

    @Select("SELECT COALESCE(SUM(amount), 0) " +
            "FROM play_earnings_line " +
            "WHERE deleted = 0 " +
            "  AND clerk_id = #{clerkId} " +
            "  AND (status = 'available' OR (status = 'frozen' AND unlock_time <= #{now}))")
    BigDecimal sumWithdrawableAmount(@Param("clerkId") String clerkId, @Param("now") LocalDateTime now);

    @Select("SELECT COALESCE(SUM(amount), 0) " +
            "FROM play_earnings_line " +
            "WHERE deleted = 0 " +
            "  AND clerk_id = #{clerkId} " +
            "  AND status = 'frozen' " +
            "  AND unlock_time > #{now}")
    BigDecimal sumPendingAmount(@Param("clerkId") String clerkId, @Param("now") LocalDateTime now);

    @Select("SELECT * " +
            "FROM play_earnings_line " +
            "WHERE deleted = 0 " +
            "  AND clerk_id = #{clerkId} " +
            "  AND (status = 'available' OR (status = 'frozen' AND unlock_time <= #{now})) " +
            "ORDER BY unlock_time ASC")
    List<EarningsLineEntity> selectWithdrawableLines(@Param("clerkId") String clerkId, @Param("now") LocalDateTime now);
}
