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

import com.red.circle.other.app.inner.convertor.sys.SysMessageCopywritingContentInnerConvertor;
import com.red.circle.other.app.inner.service.sys.SysMessageCopywritingContentClientService;
import com.red.circle.other.infra.database.rds.entity.sys.SysMessageCopywritingContent;
import com.red.circle.other.infra.database.rds.service.sys.SysMessageCopywritingContentService;
import com.red.circle.other.inner.model.dto.sys.SysMessageCopywritingContentDTO;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author zongpubin on 2023/11/8
 */
@Service
@RequiredArgsConstructor
public class SysMessageCopywritingContentClientServiceImpl implements
    SysMessageCopywritingContentClientService {

  private final SysMessageCopywritingContentService messageCopywritingContentService;
  private final SysMessageCopywritingContentInnerConvertor sysMessageCopywritingContentInnerConvertor;

  @Override
  public List<SysMessageCopywritingContentDTO> listByTextTypeId(Long textTypeId) {
    List<SysMessageCopywritingContent> sysMessageCopywritingContents = messageCopywritingContentService.listByTextTypeId(
        textTypeId);
    return Optional.ofNullable(sysMessageCopywritingContents)
        .map(sysPushTextContentsList ->
            sysMessageCopywritingContents.stream().map(
                    sysMessageCopywritingContentInnerConvertor::toSysMessageCopywritingContentDTO)
                .collect(Collectors.toList()))
        .orElse(null);
  }

  @Override
  public void synchronizeTemplateMessage(List<SysMessageCopywritingContentDTO> list) {
    messageCopywritingContentService.saveBatch(
        sysMessageCopywritingContentInnerConvertor.toListSysMessageCopywritingContent(list));

  }

  @Override
  public List<SysMessageCopywritingContentDTO> listByLanguage(String language) {
    return sysMessageCopywritingContentInnerConvertor.toListSysMessageCopywritingContentDTO(
        messageCopywritingContentService.listByLanguage(language));
  }

  @Override
  public void save(SysMessageCopywritingContentDTO sysMessageCopywritingContent) {
    messageCopywritingContentService.save(
        sysMessageCopywritingContentInnerConvertor.toSysMessageCopywritingContent(
            sysMessageCopywritingContent));
  }
}
