package com.red.circle.external.inner.service.message.impl;

import com.red.circle.component.instant.msg.tencet.notice.newsletter.NewsletterResult;
import com.red.circle.component.instant.msg.tencet.notice.newsletter.NewsletterService;
import com.red.circle.external.inner.model.cmd.message.NewsletterResultCmd;
import com.red.circle.external.inner.service.message.NewsletterClientService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 通讯消息服务 实现.
 *
 * @author pengliang on 2023/8/15
 */
@Service
@RequiredArgsConstructor
public class NewsletterClientServiceImpl implements NewsletterClientService {

  private final NewsletterService newsletterService;

  @Override
  public void send(NewsletterResultCmd cmd) {
    if (CollectionUtils.isEmpty(cmd.getUserIds())) {
      throw new IllegalArgumentException("userIds isEmpty");
    }

    if (Objects.equals(cmd.getUserIds().size(), 1)) {
      newsletterService.send(cmd.getUserIds().get(0),
          NewsletterResult.of(Objects.toString(cmd.getEvent()), cmd.getData()));
      return;
    }

    newsletterService.sendBatch(cmd.getUserIds(),
        NewsletterResult.of(Objects.toString(cmd.getEvent()), cmd.getData()));
  }
}
