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

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.app.dto.clientobject.task.CheckInCO;
import com.red.circle.other.domain.gateway.props.PropsNobleVipGateway;
import com.red.circle.other.infra.database.cache.service.other.CheckInCacheService;
import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService;
import com.red.circle.other.inner.enums.task.CheckInEnum;
import com.red.circle.other.inner.model.dto.material.props.PropsNobleVipAbilityDTO;
import com.red.circle.tool.core.num.ArithmeticUtils;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 获取打卡信息记录.
 *
 * @author pengliang on 2020/11/27
 */
@Component
@RequiredArgsConstructor
public class SevenCheckInGetQryExe {

  private final CheckInCacheService checkInCacheService;
  private final PropsNobleVipGateway propsNobleVipGateway;
  private final EnumConfigCacheService enumConfigCacheService;


  public CheckInCO execute(AppExtCommand cmd) {
    PropsNobleVipAbilityDTO nobleVip = getUserNobleVip(cmd);
    return new CheckInCO()
        .setType(getType(nobleVip))
        .setClockedIn(checkInCacheService.getMarkDaySevenCheckIn(cmd.getReqUserId()))
        .setConsecutiveDays(checkInCacheService.getCheckInDays("cmd.getReqUserId()"))
        .putNobleRewardQuantity(Optional.ofNullable(nobleVip)
            .map(nobleVipCache -> enumConfigCacheService
                .goldsToDiamondQuantity(nobleVipCache.getLoginRewardsAmount(),
                    cmd.requireReqSysOrigin())).orElse(null));
  }

  private CheckInEnum getType(PropsNobleVipAbilityDTO nobleVipAbility) {
    if (Objects.isNull(nobleVipAbility) || ArithmeticUtils.lte(
        nobleVipAbility.getLoginRewardsAmount(), BigDecimal.ZERO)) {
      return CheckInEnum.ORDINARY;
    }
    return CheckInEnum.NOBLE_VIP;
  }

  private PropsNobleVipAbilityDTO getUserNobleVip(AppExtCommand cmd) {
    return propsNobleVipGateway.getUserMaxAbilityDTO(cmd.getReqUserId());
  }
}
