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

import com.google.common.collect.Lists;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.inner.endpoint.material.gift.GiftConfigClient;
import com.red.circle.other.inner.model.dto.gift.UserGiftWallDTO;
import com.red.circle.other.inner.model.dto.material.GiftConfigDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户获得礼物墙 服务实现类
 * </p>
 *
 * @author houyong
 * @since 2021-01-14
 */
@Service
@RequiredArgsConstructor
public class UserGiftWallServiceImpl implements UserGiftWallService {

  private final GiftConfigClient giftConfigClient;

  @Override
  public List<UserGiftWallDTO> getGiftWall(Long userId) {
    List<UserGiftWallDTO> userGiftWalls = ResponseAssert.requiredSuccess(
        giftConfigClient.listGiftWallByUser(userId));
    if (CollectionUtils.isEmpty(userGiftWalls)) {
      return Lists.newArrayList();
    }
    Set<Long> giftIds = userGiftWalls.stream().map(UserGiftWallDTO::getGiftId)
        .collect(Collectors.toSet());
    Map<Long, GiftConfigDTO> giftConfigMap = ResponseAssert.requiredSuccess(
        giftConfigClient.mapByIds(giftIds));
    return userGiftWalls.stream().map(userGiftWall -> {
      UserGiftWallDTO vo = new UserGiftWallDTO();
      vo.setQuantity(userGiftWall.getQuantity());
      GiftConfigDTO sysGiftConfig = giftConfigMap.get(userGiftWall.getGiftId());
      if (Objects.nonNull(sysGiftConfig)) {
        vo.setGiftPhoto(sysGiftConfig.getGiftPhoto());
      }
      return vo;
    }).collect(Collectors.toList());
  }
}
