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.LotteryTicketCO;
import com.red.circle.other.infra.database.rds.entity.activity.LotteryTicket;
import com.red.circle.other.infra.database.rds.service.activity.LotteryTicketService;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * 查询我的抽奖券列表.
 *
 * @author system
 * @since 2025-10-18
 */
@Component
@RequiredArgsConstructor
public class LotteryTicketQryExe {

  private final LotteryTicketService lotteryTicketService;

  public List<LotteryTicketCO> execute(AppExtCommand cmd, Long activityId) {
    Long userId = cmd.requiredReqUserId();

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

    return tickets.stream()
        .map(this::convertToCO)
        .collect(Collectors.toList());
  }

  private LotteryTicketCO convertToCO(LotteryTicket ticket) {
    LotteryTicketCO co = new LotteryTicketCO();
    BeanUtils.copyProperties(ticket, co);
    return co;
  }

}
