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

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.component.redis.service.RedisService;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.command.activity.CumulativeRechargeLotteryExe;
import com.red.circle.other.app.command.activity.CumulativeRechargeReceiveRewardCmdExe;
import com.red.circle.other.app.command.activity.UserCustomGiftUploadExe;
import com.red.circle.other.app.command.activity.query.CumulativeRechargeRewardQueryExe;
import com.red.circle.other.app.command.activity.query.MonthlyCumulativeRechargeRewardQueryExe;
import com.red.circle.other.app.command.activity.query.UserCustomGiftRecordQryExe;
import com.red.circle.other.app.dto.clientobject.activity.ActivityCumulativeRechargeCO;
import com.red.circle.other.app.dto.clientobject.activity.ActivityCumulativeRewardPropsCO;
import com.red.circle.other.inner.model.dto.activity.props.ActivityRewardProps;
import com.red.circle.other.inner.model.cmd.user.CumulativeRechargeCmd;
import com.red.circle.other.inner.model.dto.user.UserCustomGiftRecordDTO;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 累计充值抽奖 服务实现.
 *
 * @author pengshigang on 2022/3/29
 */
@Service
@RequiredArgsConstructor
public class CumulativeRechargeServiceImpl implements CumulativeRechargeService {

  private final RedisService redisService;
  private final UserCustomGiftUploadExe userCustomGiftUploadExe;
  private final UserCustomGiftRecordQryExe userCustomGiftRecordQryExe;
  private final CumulativeRechargeLotteryExe cumulativeRechargeLotteryExe;
  private final CumulativeRechargeRewardQueryExe cumulativeRechargeRewardQueryExe;
  private final CumulativeRechargeReceiveRewardCmdExe cumulativeRechargeReceiveRewardCmdExe;
  private final MonthlyCumulativeRechargeRewardQueryExe monthlyCumulativeRechargeRewardQueryExe;

  @Override
  public void receiveAward(AppIdLongCmd cmd) {

    // 上锁
    String key = "receiveAward:" + cmd.getReqUserId();
    try {
      ResponseAssert.isTrue(CommonErrorCode.REQUEST_LIMITING, redisService.lock(key, 60));
      cumulativeRechargeReceiveRewardCmdExe.execute(cmd);
    } finally {
      redisService.unlock(key);
    }
  }

  @Override
  public ActivityCumulativeRechargeCO listRechargeReward(AppExtCommand cmd) {
    return monthlyCumulativeRechargeRewardQueryExe.execute(cmd);
  }

  @Override
  public List<ActivityCumulativeRewardPropsCO> listRewardPool(AppExtCommand cmd) {
    return cumulativeRechargeRewardQueryExe.execute(cmd);
  }

  @Override
  public ActivityRewardProps lottery(AppIdLongCmd cmd) {
    return cumulativeRechargeLotteryExe.execute(cmd);
  }

  @Override
  public UserCustomGiftRecordDTO userCustomGift(Long id) {
    return userCustomGiftRecordQryExe.execute(id);
  }

  @Override
  public void uploadResource(CumulativeRechargeCmd cmd) {
    userCustomGiftUploadExe.execute(cmd);
  }

}
