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

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.other.inner.endpoint.user.user.UserCustomGiftClient;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.cmd.user.UserCustomGiftQryCmd;
import com.red.circle.other.inner.model.dto.user.UserCustomGiftRecordDTO;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 用户领取专属礼物记录
 *
 * @author zongpubin on 2024/1/31
 */
@Service
@RequiredArgsConstructor
public class UserCustomGiftServiceImpl implements UserCustomGiftService {

  private final UserProfileClient userProfileClient;
  private final UserCustomGiftClient userCustomGiftClient;

  @Override
  public PageResult<UserCustomGiftRecordDTO> getCustomGift(UserCustomGiftQryCmd query) {
    PageResult<UserCustomGiftRecordDTO> pageResult = ResponseAssert.requiredSuccess(
        userCustomGiftClient.getCustomGift(query));
    if (CollectionUtils.isEmpty(pageResult.getRecords())) {
      return pageResult;
    }
    Map<Long, UserProfileDTO> userProfileMap = ResponseAssert.requiredSuccess(
        userProfileClient.mapByUserIds(
            pageResult.getRecords().stream().map(UserCustomGiftRecordDTO::getUserId).collect(
                Collectors.toSet())));
    return pageResult.convert(userCustomGiftRecordDTO -> userCustomGiftRecordDTO.setUserBaseInfo(
        userProfileMap.get(userCustomGiftRecordDTO.getUserId())));
  }

  @Override
  public void switchStatus(Long id, Boolean status) {
    userCustomGiftClient.switchStatus(id, status);
  }
}
