package com.red.circle.console.app.service.app.team;

import com.google.common.collect.Lists;
import com.red.circle.console.app.convertor.team.TeamAppConvertor;
import com.red.circle.console.app.dto.clienobject.team.TeamExchangeTargetCO;
import com.red.circle.console.inner.error.ConsoleErrorCode;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.inner.endpoint.team.agency.AgentTeamClient;
import com.red.circle.other.inner.model.cmd.team.TeamExchangeTargetBackQryCmd;
import com.red.circle.other.inner.model.dto.agency.agency.TeamExchangeSurplusTargetDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.text.StringUtils;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 将主播剩余目标兑换成美元记录.
 *
 * @author pengshigang on 2023/5/25
 */
@Service
@RequiredArgsConstructor
public class TeamExchangeTargetBackServiceImpl implements TeamExchangeTargetBackService {

  private final AgentTeamClient agentTeamClient;
  private final TeamAppConvertor teamAppConvertor;
  private final UserProfileClient userProfileClient;

  @Override
  public List<TeamExchangeTargetCO> listByCondition(TeamExchangeTargetBackQryCmd query) {

    List<TeamExchangeSurplusTargetDTO> targetList = ResponseAssert.requiredSuccess(agentTeamClient
        .listByCondition(query.getLastId(), query.getUserId(), query.getSysOrigin(),
            query.getDateNumber()));

    if (CollectionUtils.isEmpty(targetList)) {
      return Lists.newArrayList();
    }
    Map<Long, UserProfileDTO> userProfileMap = getUserProfileMap(targetList);
    if (CollectionUtils.isEmpty(userProfileMap)) {
      return Lists.newArrayList();
    }

    return targetList.stream().map(target -> {
      TeamExchangeTargetCO vo = teamAppConvertor.toTeamExchangeTargetCO(target);
      if (Objects.isNull(vo)) {
        return null;
      }

      UserProfileDTO userProfile = userProfileMap.get(target.getUserId());
      if (Objects.isNull(userProfile)) {
        return null;
      }

      vo.setUserProfile(userProfile);
      return vo;
    }).filter(Objects::nonNull).collect(Collectors.toList());

  }

  @Override
  public String getExchangeUsdTotal(TeamExchangeTargetBackQryCmd query) {

    ResponseAssert.isTrue(ConsoleErrorCode.SYSTEM_NOT_SELECTED_ERROR,
        StringUtils.isNotBlank(query.getSysOrigin()));

    return ResponseAssert.requiredSuccess(agentTeamClient
        .getTotalAmountUsd(query.getUserId(), query.getSysOrigin(), query.getDateNumber()));

  }

  private Map<Long, UserProfileDTO> getUserProfileMap(
      List<TeamExchangeSurplusTargetDTO> targetList) {
    return ResponseAssert.requiredSuccess(userProfileClient.mapByUserIds(
        targetList.stream().map(TeamExchangeSurplusTargetDTO::getUserId)
            .collect(Collectors.toSet())));
  }

}
