package com.red.circle.other.app.command.user.query;

import com.google.common.collect.Lists;
import com.red.circle.common.business.dto.cmd.app.AppFlowCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.other.app.dto.clientobject.user.invite.InviteRedPacketUserLotteryDetailsCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.rds.entity.user.user.InviteRedPacketDrawLog;
import com.red.circle.other.infra.database.rds.service.user.user.InviteRedPacketDrawLogService;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 查询抽奖明细列表分页.
 * </p>
 *
 * @author pengshigang
 * @since 2023-12-23
 */
@Service
@AllArgsConstructor
public class InviteNewLotteryDetailsPageQryExe {

  private final UserProfileGateway userProfileGateway;
  private final InviteRedPacketDrawLogService inviteRedPacketDrawLogService;

  public List<InviteRedPacketUserLotteryDetailsCO> execute(AppFlowCmd cmd) {

    ResponseAssert.notNull(UserErrorCode.USER_INFO_NOT_FOUND,
        userProfileGateway.getByUserId(cmd.getReqUserId()));

    List<InviteRedPacketDrawLog> drawLogs = inviteRedPacketDrawLogService.pageLotteryDetails(
        cmd.getLastId(), cmd.getReqUserId());
    if (CollectionUtils.isEmpty(drawLogs)) {
      return Lists.newArrayList();
    }

    return drawLogs.stream().map(log -> {
      InviteRedPacketUserLotteryDetailsCO detailsCO = new InviteRedPacketUserLotteryDetailsCO();
      detailsCO.setId(log.getId());
      detailsCO.setQuantity(log.getAwardNumber());
      detailsCO.setType(log.getAwardType());
      detailsCO.setCreateTime(log.getCreateTime());
      return detailsCO;
    }).collect(Collectors.toList());

  }


}
