package cn.axzo.msg.center.notices.client.converters;


import cn.axzo.msg.center.notices.http.api.request.SendBatchMessageRequest;
import cn.axzo.msg.center.notices.http.api.request.SmsContent;
import cn.axzo.msg.center.notices.manager.api.dto.request.SendBatchMessageRequestDto;
import cn.axzo.msg.center.notices.manager.api.dto.request.SmsContentDto;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * SendBatchMessageRequest 转换器
 *
 * @author zhaoyong
 * @see SendBatchMessageRequestToDtoConverter
 * @since 2021-08-07 16:37
 */
@Component
public class SendBatchMessageRequestToDtoConverter implements
    Converter<SendBatchMessageRequest, SendBatchMessageRequestDto> {

  @Override
  public SendBatchMessageRequestDto convert(SendBatchMessageRequest source) {
    Assert.notNull(source, "source must not be null");
    SendBatchMessageRequestDto dto = new SendBatchMessageRequestDto();
    dto.setRequestNo(source.getRequestNo());
    dto.setAppCode(source.getAppCode());
    dto.setSendType(source.getSendType());
    dto.setTemplateNo(source.getTemplateNo());
    List<SmsContent> smsContents = source.getSmsContents();
    if(CollectionUtils.isNotEmpty(smsContents)) {
      List<SmsContentDto> smsDtos = new ArrayList<>();
      for (SmsContent content : smsContents) {
        SmsContentDto smsDto = new SmsContentDto();
        smsDto.setPhoneNo(content.getPhoneNo());
        smsDto.setTemplateParams(content.getTemplateParams());
        smsDtos.add(smsDto);
      }
      dto.setSmsContentDtos(smsDtos);
    }
    return dto;
  }
}
