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

import com.google.common.collect.Lists;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.common.props.GroupRewardPoolCommon;
import com.red.circle.other.app.dto.clientobject.activity.ActivityCumulativeRewardPropsCO;
import com.red.circle.other.infra.database.cache.service.other.CumulativeRechargeCacheService;
import com.red.circle.other.infra.database.rds.entity.activity.PropsActivityRuleConfig;
import com.red.circle.other.inner.enums.activity.PropsActivityTypeEnum;
import com.red.circle.other.inner.model.dto.activity.props.ActivityRewardProps;
import com.red.circle.other.inner.model.dto.activity.rule.CumulativeRechargeContent;
import com.red.circle.tool.core.num.ArithmeticUtils;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 累计充值奖励查询.
 *
 * @author pengshigang on 2022/3/30
 */
@Component
@RequiredArgsConstructor
public class CumulativeRechargeRewardQueryExe {

  private final GroupRewardPoolCommon groupRewardPoolCommon;
  private final CumulativeRechargeCacheService cumulativeRechargeCacheService;


  public List<ActivityCumulativeRewardPropsCO> execute(AppExtCommand cmd) {

    List<ActivityCumulativeRewardPropsCO> result = Lists.newArrayList();

    //今天充值金额
    BigDecimal todayRecharge = BigDecimal.valueOf(cumulativeRechargeCacheService
        .getTodayRechargeScore(cmd.getReqUserId()));

    List<PropsActivityRuleConfig> activityRuleConfigs = getActivityRuleConfigs(cmd);

    Map<Long, List<ActivityRewardProps>> mapRewardProps = groupRewardPoolCommon.mapRewardProps(
        cmd.requireReqSysOriginEnum(),
        activityRuleConfigs.stream().map(PropsActivityRuleConfig::getResourceGroupId)
            .collect(Collectors.toSet())
    );

    for (int index = 0; index < activityRuleConfigs.size(); index++) {
      ActivityCumulativeRewardPropsCO rewardPropsCO = new ActivityCumulativeRewardPropsCO();
      CumulativeRechargeContent cumulativeRechargeContent = CumulativeRechargeContent
          .serializable(activityRuleConfigs.get(index).getJsonData());
      // 2040 配置错误
      ResponseAssert.isTrue(CommonErrorCode.CONFIGURATION_ERROR,
          Objects.nonNull(cumulativeRechargeContent) && ArithmeticUtils
              .gt(cumulativeRechargeContent.getQuantity(), BigDecimal.ZERO));

      rewardPropsCO.setActivityRewardPropsList(
          mapRewardProps.get(activityRuleConfigs.get(index).getResourceGroupId()));

      rewardPropsCO.setQuantity(cumulativeRechargeContent.getQuantity());

      //不够资格显示要求金额
      if (todayRecharge.compareTo(cumulativeRechargeContent.getQuantity()) < 0) {
        rewardPropsCO.setState(0);
        result.add(rewardPropsCO);
        continue;
      }
      if (Boolean.TRUE.equals(cumulativeRechargeCacheService
          .checkTodayAwarded(cmd.getReqUserId(), (long) index + 1))) {
        rewardPropsCO.setState(2);
        result.add(rewardPropsCO);
        continue;
      }
      rewardPropsCO.setState(1);
      result.add(rewardPropsCO);
    }
    return result;
  }


  private List<PropsActivityRuleConfig> getActivityRuleConfigs(AppExtCommand cmd) {
    List<PropsActivityRuleConfig> configs = groupRewardPoolCommon
        .listByActivityTypeLimit(cmd.requireReqSysOriginEnum(),
            PropsActivityTypeEnum.CUMULATIVE_RECHARGE_LOTTERY);
    ResponseAssert.notEmpty(CommonErrorCode.DATA_ERROR, configs);
    ResponseAssert.isTrue(CommonErrorCode.DATA_ERROR, configs.size() >= 3);
    return configs;
  }


}
