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

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.command.activity.LotteryActivityDetailQryExe;
import com.red.circle.other.app.command.activity.LotteryActivityListQryExe;
import com.red.circle.other.app.command.activity.LotteryDrawExe;
import com.red.circle.other.app.command.activity.LotteryMultiDrawExe;
import com.red.circle.other.app.command.activity.LotteryRankQryExe;
import com.red.circle.other.app.command.activity.LotteryRecordQryExe;
import com.red.circle.other.app.command.activity.LotteryTicketCountQryExe;
import com.red.circle.other.app.command.activity.LotteryTicketQryExe;
import com.red.circle.other.app.command.activity.LotteryWinnerHistoryQryExe;
import com.red.circle.other.app.command.activity.LotteryWithdrawAmountQryExe;
import com.red.circle.other.app.command.activity.LotteryWithdrawApplyExe;
import com.red.circle.other.app.command.activity.LotteryWithdrawQryExe;
import com.red.circle.other.app.command.activity.LotteryExchangeGoldExe;
import com.red.circle.other.app.command.activity.LotteryTransferExe;
import com.red.circle.other.app.dto.clientobject.activity.*;
import com.red.circle.other.app.dto.cmd.activity.*;

import java.util.List;

import com.red.circle.other.app.util.DistributedLockUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 抽奖活动服务实现.
 *
 * @author system
 * @since 2025-10-18
 */
@Service
@RequiredArgsConstructor
public class LotteryActivityRestServiceImpl implements LotteryActivityRestService {

  private final LotteryActivityListQryExe lotteryActivityListQryExe;
  private final LotteryActivityDetailQryExe lotteryActivityDetailQryExe;
  private final LotteryDrawExe lotteryDrawExe;
  private final LotteryMultiDrawExe lotteryMultiDrawExe;
  private final LotteryRecordQryExe lotteryRecordQryExe;
  private final LotteryTicketQryExe lotteryTicketQryExe;
  private final LotteryTicketCountQryExe lotteryTicketCountQryExe;
  private final LotteryRankQryExe lotteryRankQryExe;
  private final LotteryWinnerHistoryQryExe lotteryWinnerHistoryQryExe;
  private final LotteryWithdrawApplyExe lotteryWithdrawApplyExe;
  private final LotteryWithdrawQryExe lotteryWithdrawQryExe;
  private final LotteryWithdrawAmountQryExe lotteryWithdrawAmountQryExe;
  private final LotteryExchangeGoldExe lotteryExchangeGoldExe;
  private final LotteryTransferExe lotteryTransferExe;
  private final DistributedLockUtil distributedLockUtil;
  private static final String LOCK_KEY_PREFIX = "activity:lottery:draw:lock";

  @Override
  public List<LotteryActivityCO> listValidActivities(AppExtCommand cmd) {
    return lotteryActivityListQryExe.execute(cmd);
  }

  @Override
  public LotteryActivityDetailCO getActivityDetail(String activityCode, AppExtCommand cmd) {
    return lotteryActivityDetailQryExe.execute(activityCode, cmd);
  }

  @Override
  public LotteryDrawResultCO draw(LotteryDrawCmd cmd) {
    return distributedLockUtil.executeWithLock(
        DistributedLockUtil.buildLockKey(LOCK_KEY_PREFIX, cmd.requiredReqUserId()),
        () -> lotteryDrawExe.execute(cmd));
  }

  @Override
  public LotteryMultiDrawResultCO multiDraw(LotteryMultiDrawCmd cmd) {
    return distributedLockUtil.executeWithLock(
        DistributedLockUtil.buildLockKey(LOCK_KEY_PREFIX, cmd.requiredReqUserId()),
        () -> lotteryMultiDrawExe.execute(cmd));
  }

  @Override
  public PageResult<LotteryRecordCO> listMyRecords(LotteryRecordQryCmd cmd) {
    return lotteryRecordQryExe.execute(cmd);
  }

  @Override
  public List<LotteryTicketCO> listMyTickets(AppExtCommand cmd) {
    return lotteryTicketQryExe.execute(cmd);
  }

  @Override
  public Integer getMyTicketCount(AppExtCommand cmd) {
    return lotteryTicketCountQryExe.execute(cmd);
  }

  @Override
  public List<LotteryRankCO> getRankList(LotteryRankQryCmd cmd) {
    return lotteryRankQryExe.execute(cmd);
  }

  @Override
  public List<LotteryWinnerHistoryCO> getWinnerHistory(LotteryWinnerHistoryQryCmd cmd) {
    return lotteryWinnerHistoryQryExe.execute(cmd);
  }

  @Override
  public void applyWithdraw(LotteryWithdrawCmd cmd) {
    lotteryWithdrawApplyExe.execute(cmd);
  }

  @Override
  public PageResult<LotteryWithdrawCO> listWithdrawRecords(LotteryWithdrawQryCmd cmd) {
    return lotteryWithdrawQryExe.execute(cmd);
  }

  @Override
  public LotteryWithdrawAmountCO getWithdrawAmount(Long userId, Long activityId) {
    return lotteryWithdrawAmountQryExe.execute(userId, activityId);
  }

  @Override
  public void exchangeGold(LotteryExchangeGoldCmd cmd) {
    lotteryExchangeGoldExe.execute(cmd);
  }

  @Override
  public void transfer(LotteryTransferCmd cmd) {
    lotteryTransferExe.execute(cmd);
  }

}
