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

import com.alibaba.fastjson.JSON;
import com.red.circle.console.app.convertor.WalletAppConvertor;
import com.red.circle.console.app.dto.clienobject.user.UserDiamondBalanceCO;
import com.red.circle.console.app.dto.clienobject.user.UserDiamondRunWaterCO;
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.model.dto.user.UserProfileDTO;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.wallet.inner.endpoint.wallet.WalletDiamondClient;
import com.red.circle.wallet.inner.model.cmd.UserDiamondBalanceQryCmd;
import com.red.circle.wallet.inner.model.cmd.UserDiamondRunWaterQryCmd;
import com.red.circle.wallet.inner.model.dto.DiamondBalanceDTO;
import com.red.circle.wallet.inner.model.dto.UserDiamondRunWaterDTO;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户钻石余额 服务实现类
 * </p>
 *
 * @author pengshigang
 * @since 2023-7-24
 */
@RequiredArgsConstructor
@Service
public class UserDiamondServiceImpl implements
    UserDiamondService {

  private final UserProfileClient userProfileClient;
  private final WalletAppConvertor walletAppConvertor;
  private final WalletDiamondClient walletDiamondClient;

  @Override
  public PageResult<UserDiamondBalanceCO> pageUserDiamondBalance(UserDiamondBalanceQryCmd query) {
    PageResult<DiamondBalanceDTO> page = ResponseAssert.requiredSuccess(
        walletDiamondClient.pageUserDiamondBalance(query));

    PageResult<UserDiamondBalanceCO> pageResult = page.convert(
        walletAppConvertor::toUserDiamondBalanceCO);
    if (CollectionUtils.isEmpty(pageResult.getRecords())) {
      return pageResult;
    }

    Map<Long, UserProfileDTO> userMap = ResponseAssert.requiredSuccess(userProfileClient
        .mapByUserIds(pageResult.getRecords().stream()
            .map(UserDiamondBalanceCO::getUserId).collect(Collectors.toSet())));

    return pageResult.convert(diamondBalance -> {
      diamondBalance.setUserProfile(userMap.get(diamondBalance.getUserId()));
      return diamondBalance;
    });
  }

  @Override
  public PageResult<UserDiamondRunWaterCO> pageUserDiamondRunWater(
      UserDiamondRunWaterQryCmd query) {
    System.out.println("数据信息:{}"+ JSON.toJSONString(query));

    PageResult<UserDiamondRunWaterDTO> iPage = ResponseAssert.requiredSuccess(
        walletDiamondClient.pageUserDiamondRunWater(query));

    PageResult<UserDiamondRunWaterCO> pageResult = iPage.convert(
        walletAppConvertor::toUserDiamondRunWaterCO);

    if (CollectionUtils.isEmpty(iPage.getRecords())) {
      return pageResult;
    }

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

    pageResult.getRecords().forEach(runWater -> {

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

      runWater.setUserProfile(userProfile);
      runWater.setTypeDesc(Objects.equals(runWater.getType(), 1) ? "支出" : "收入");
    });

    return pageResult;
  }

  private Map<Long, UserProfileDTO> getUserProfileMap(PageResult<UserDiamondRunWaterCO> iPage) {
    return ResponseAssert.requiredSuccess(userProfileClient.mapByUserIds(
        iPage.getRecords().stream().map(UserDiamondRunWaterCO::getUserId)
            .collect(Collectors.toSet())));
  }


}
