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

import com.google.common.collect.Lists;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.inner.model.dto.material.BadgeConfigDetailsDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 佩戴使用的徽章.
 *
 * @author pengliang on 2021/4/8
 */
@Service
@RequiredArgsConstructor
public class UseWearBadgeQryExe {

  private final UserProfileGateway userProfileGateway;
  private final UserProfileAppConvertor userProfileAppConvertor;

  public List<BadgeConfigDetailsDTO> execute(AppExtCommand cmd) {
    var userProfile = userProfileGateway.getByUserId(cmd.requiredReqUserId());
    if (Objects.isNull(userProfile)) {
      return Lists.newArrayList();
    }
    HashMap<Object, Boolean> map = new HashMap<>();
    return CollectionUtils.isEmpty(userProfile.getWearBadge())
        ? Lists.newArrayList()
        : userProfileAppConvertor.toListBadgeConfigDetailsDTO(userProfile.getWearBadge().stream()
            .filter(e-> map.putIfAbsent(e.getId(), Boolean.TRUE) == null).collect(Collectors.toList()));
  }
}
