package com.plan.api.model;


import com.google.common.collect.Lists;
import com.base.orbit.utils.DateUtils;

import java.util.*;
import java.util.stream.Collectors;

public class StepDtoMapTool {

    /**
     * key为任务id，value为执行状态步骤信息
     */
    public static List<PlanIndicatorStepDTO> stepInfo = new ArrayList<>();

    public static Map<String, List<PlanIndicatorStepDTO>> stepInfoMap = new HashMap<>();


    public static void add(String taskId, List<PlanIndicatorStepDTO> strpDTOS) {
        strpDTOS = strpDTOS.stream().sorted(Comparator.comparing(PlanIndicatorStepDTO::getStepVal)).collect(Collectors.toList());
        strpDTOS.stream().forEach(strpDTO->{
            strpDTO.setTimeStamp(DateUtils.dateTimeNow());
        });
        stepInfoMap.put(taskId, strpDTOS);
    }

    public static void remove(String taskId) {
        Iterator<String> iter = stepInfoMap.keySet().iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            if (taskId.equals(key)) {
                iter.remove();
            }
        }
    }

    public static List<PlanIndicatorStepDTO> getStepInfosByTaskId(String taskId) {
        if (Objects.isNull(stepInfoMap.get(taskId))) {
            return Lists.newArrayList();
        }
        return stepInfoMap.get(taskId);
    }
}
