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

import com.red.circle.common.business.core.level.LevelUtils;
import com.red.circle.common.business.core.level.UserLevelExperience;
import com.red.circle.other.app.dto.cmd.user.user.UserLevelCmd;
import com.red.circle.other.infra.database.rds.service.user.user.ConsumptionLevelService;
import java.math.BigDecimal;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 获取用户财富等级和魅力等级.
 *
 * @author pengliang on 2021/1/18
 */
@Component
@RequiredArgsConstructor
public class UserLevelExperienceQryExe {

  private final ConsumptionLevelService consumptionLevelService;

  public UserLevelExperience execute(UserLevelCmd cmd) {
    if (UserLevelCmd.LevelType.WEALTH.equals(cmd.getType())) {
      return LevelUtils.getWealthLevel(cmd.requireReqSysOriginEnum(), getQuantity(cmd).longValue());
    }
    return LevelUtils.getUserLevelV2(cmd.requireReqSysOriginEnum(), getQuantity(cmd).longValue());
  }

  private BigDecimal getQuantity(UserLevelCmd cmd) {
    if (Objects.equals(cmd.getType(), UserLevelCmd.LevelType.WEALTH)) {
      return consumptionLevelService.getConsumptionGolds(cmd.getUserId());
    }

    if (Objects.equals(cmd.getType(), UserLevelCmd.LevelType.CHARM)) {
      return consumptionLevelService.getConsumptionDiamond(cmd.getUserId());
    }
    return BigDecimal.ZERO;
  }

}
