package cn.axzo.msg.center.message.service.todo.manage.event;

import static java.util.stream.Collectors.toSet;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.springframework.context.ApplicationEvent;

import com.alibaba.fastjson.JSON;

import cn.axzo.msg.center.domain.entity.Todo;
import cn.axzo.msg.center.message.domain.dto.TemplateModelV3;
import lombok.Getter;

/**
 * @author yanglin
 */
@Getter
public class NewTodoEvent extends ApplicationEvent {
    private final TemplateModelV3 templateModel;
    private final List<Todo> todos;

    public NewTodoEvent(Object source, TemplateModelV3 templateModel, List<Todo> todos) {
        super(source);
        this.templateModel = templateModel;
        this.todos = todos;
    }

    public Set<Long> executorPersonIds() {
        return todos.stream().map(Todo::getExecutorPersonId).collect(toSet());
    }

    @Override
    public String toString() {
        HashMap<String, Object> fields = new HashMap<>();
        fields.put("model", templateModel);
        fields.put("todos", todos);
        return JSON.toJSONString(fields);
    }
}