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

import com.google.common.collect.Lists;
import com.red.circle.common.business.dto.cmd.app.AppUserIdCmd;
import com.red.circle.tool.core.num.NumUtils;
import com.red.circle.other.app.dto.clientobject.user.relation.RelationshipCounterCO;
import com.red.circle.other.infra.database.rds.service.user.user.UserRelationshipCounterService;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 获取用户关系计数.
 *
 * @author pengliang on 2020/11/18
 */
@Component
@RequiredArgsConstructor
public class UserRelationshipCounterListQryExe {

  private final UserRelationshipCounterService userRelationshipCounterService;

  public List<RelationshipCounterCO> execute(AppUserIdCmd cmd) {
    return Optional
        .ofNullable(userRelationshipCounterService.getRelationshipCounter(cmd.getUserId()))
        .map(list -> list.stream()
            .map(counter -> new RelationshipCounterCO()
                .setCounterType(counter.getType())
                .setQuantity(counter.getQuantity())
                .setFormatQuantity(NumUtils.formatLong(counter.getQuantity()))
            ).collect(Collectors.toList()))
        .orElse(Lists.newArrayList());
  }
}
