package com.red.circle.other.app.command.activity;

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.app.dto.clientobject.activity.MyTotalDrawCountCO;
import com.red.circle.other.infra.database.rds.entity.activity.LotteryTicket;
import com.red.circle.other.infra.database.rds.service.activity.LotteryTicketService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 查询我的总抽奖次数
 */
@Component
@RequiredArgsConstructor
public class MyTotalDrawCountQryExe {

    private final LotteryTicketService lotteryTicketService;

    public MyTotalDrawCountCO execute(AppExtCommand cmd, Long activityId) {
        Long userId = cmd.getReqUserId();

        List<LotteryTicket> tickets = lotteryTicketService.query()
            .eq(LotteryTicket::getUserId, userId)
            .eq(LotteryTicket::getActivityId, activityId)
            .eq(LotteryTicket::getStatus, 1)
            .list();

        int totalCount = tickets.stream()
            .mapToInt(LotteryTicket::getTotalCount)
            .sum();

        int usedCount = tickets.stream()
            .mapToInt(LotteryTicket::getUsedCount)
            .sum();

        int remainingCount = tickets.stream()
            .mapToInt(LotteryTicket::getRemainingCount)
            .sum();

        return new MyTotalDrawCountCO()
            .setTotalCount(totalCount)
            .setUsedCount(usedCount)
            .setRemainingCount(remainingCount);
    }
}
