package com.red.circle.other.app.inner.service.live.impl;

import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.service.live.RoomContributionBalanceClientService;
import com.red.circle.other.infra.database.rds.entity.live.RoomContributionBalance;
import com.red.circle.other.infra.database.rds.service.live.RoomContributionBalanceService;
import com.red.circle.other.inner.model.cmd.live.RoomContributionBalancePageQryCmd;
import com.red.circle.other.inner.model.dto.live.RoomContributionBalanceDTO;
import java.math.BigDecimal;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 房间贡献余额，服务实现.
 *
 * @author pengliang on 2023/7/24
 */
@Service
@RequiredArgsConstructor
public class RoomContributionBalanceClientServiceImpl implements
    RoomContributionBalanceClientService {

  private final RoomContributionBalanceService roomContributionBalanceService;

  @Override
  public BigDecimal getTotalQuantityByUserId(Long userId) {
    return roomContributionBalanceService.getTotalQuantityByUserId(userId);
  }

  @Override
  public PageResult<RoomContributionBalanceDTO> pageRoomContributionOps(
      RoomContributionBalancePageQryCmd qryCmd) {
    return roomContributionBalanceService.query()
        .eq(Objects.nonNull(qryCmd.getRoomId()), RoomContributionBalance::getRoomId,
            qryCmd.getRoomId())
        .orderByDesc(RoomContributionBalance::getTotalQuantity,
            RoomContributionBalance::getUpdateTime)
        .page(qryCmd.getPageQuery())
        .convert(entity -> new RoomContributionBalanceDTO()
            .setId(entity.getId())
            .setUserId(entity.getUserId())
            .setRoomId(entity.getRoomId())
            .setTotalQuantity(entity.getTotalQuantity())
            .setWithdrawQuantity(entity.getWithdrawQuantity())
            .setCreateTime(entity.getCreateTime())
            .setUpdateTime(entity.getUpdateTime())
            .setCreateUser(entity.getCreateUser())
            .setUpdateUser(entity.getUpdateUser())
        );
  }

}
