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

import com.red.circle.console.app.dto.clienobject.room.RoomContributionBalanceCountTimeTableCO;
import com.red.circle.console.app.dto.clienobject.room.RoomContributionBalanceTableCO;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.inner.endpoint.activity.RoomContributionCountClient;
import com.red.circle.other.inner.endpoint.live.RoomContributionBalanceClient;
import com.red.circle.other.inner.endpoint.live.RoomManagerClient;
import com.red.circle.other.inner.model.cmd.live.RoomContributionBalancePageQryCmd;
import com.red.circle.other.inner.model.cmd.live.RoomContributionCountPageQryCmd;
import com.red.circle.other.inner.model.dto.activity.RoomContributionCountDTO;
import com.red.circle.other.inner.model.dto.live.RoomContributionBalanceDTO;
import com.red.circle.other.inner.model.dto.live.RoomProfileDTO;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 房间贡献余额.
 *
 * @author pengliang on 2023/11/23
 */
@Service
@RequiredArgsConstructor
public class RoomContributionServiceImpl implements RoomContributionService {

  private final RoomManagerClient roomManagerClient;
  private final RoomContributionCountClient roomContributionCountClient;
  private final RoomContributionBalanceClient roomContributionBalanceClient;

  @Override
  public PageResult<RoomContributionBalanceTableCO> pageRoomContributionOps(
      RoomContributionBalancePageQryCmd qryCmd) {

    PageResult<RoomContributionBalanceDTO> pageResult = roomContributionBalanceClient.pageRoomContributionOps(
        qryCmd).getBody();

    if (pageResult.checkRecordsEmpty()) {
      return pageResult.emptyRecords();
    }

    // 房间信息
    Map<Long, RoomProfileDTO> roomProfileMap = roomManagerClient.mapProfileByRoomIds(
            pageResult.getRecords().stream().map(RoomContributionBalanceDTO::getRoomId)
                .collect(Collectors.toSet()))
        .getBody();

    return pageResult.convert(roomBalance -> new RoomContributionBalanceTableCO()
        .setRoomId(roomBalance.getRoomId())
        .setTotalQuantity(roomBalance.getTotalQuantity())
        .setWithdrawQuantity(roomBalance.getWithdrawQuantity())
        .setRoomProfile(roomProfileMap.get(roomBalance.getRoomId()))
        .setCreateTime(roomBalance.getCreateTime())
        .setUpdateTime(roomBalance.getUpdateTime())
    );

  }

  @Override
  public PageResult<RoomContributionBalanceCountTimeTableCO> pageRoomContributionTimeOps(
      RoomContributionCountPageQryCmd qryCmd) {

    PageResult<RoomContributionCountDTO> pageResult = roomContributionCountClient.pageOps(
        qryCmd).getBody();

    if (pageResult.checkRecordsEmpty()) {
      return pageResult.emptyRecords();
    }

    // 房间信息
    Map<Long, RoomProfileDTO> roomProfileMap = roomManagerClient.mapProfileByRoomIds(
        pageResult.getRecords().stream().map(RoomContributionCountDTO::getRoomId)
            .collect(Collectors.toSet())).getBody();

    return pageResult.convert(count -> new RoomContributionBalanceCountTimeTableCO()
        .setRoomId(count.getRoomId())
        .setTotalQuantity(count.getTotalQuantity())
        .setDateNumber(Integer.parseInt(count.getDateNumber()))
        .setWithdrawQuantity(count.getWithdrawQuantity())
        .setCreateTime(count.getCreateTime())
        .setRoomProfile(roomProfileMap.get(count.getRoomId()))
    );
  }
}
