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

import com.google.common.collect.Lists;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.app.convertor.sys.SysNoticeAppConvertor;
import com.red.circle.other.app.dto.clientobject.sys.NoticeMessageCO;
import com.red.circle.other.infra.database.rds.service.sys.NoticeMessageService;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 最近一个月系统通知-max 100
 *
 * @author pengliang on 2021/3/27
 */
@Component
@RequiredArgsConstructor
public class SysNoticeMessageLastOneMonthQryExe {

  private final SysNoticeAppConvertor sysNoticeAppConvertor;
  private final NoticeMessageService noticeMessageService;

  public List<NoticeMessageCO> execute(AppExtCommand cmd) {
    return Optional.ofNullable(noticeMessageService.listLastOneMonth(cmd.requireReqSysOrigin()))
        .map(noticeMessages -> noticeMessages.stream().map(noticeMessage -> {
          NoticeMessageCO co = sysNoticeAppConvertor.toNoticeMessageCO(noticeMessage);
          co.setDesc(noticeMessage.getDescription());
          return co;
        }).collect(Collectors.toList())).orElse(Lists.newArrayList());
  }
}
