package com.friday.distributor.common;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.friday.blank.api.constants.ExecuteStateEnum;
import com.friday.distributor.constant.States;
import com.friday.distributor.queue.TaskPersistHelper;
import com.friday.distributor.po.JobInvoke;
import com.friday.operation.api.distributor.dto.TaskResultStateDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author YaoYao
 * @Date Created In 14:34 2023/3/7
 * @Description 运行数据相关服务
 * @Modified By
 */
@Service
@Slf4j
public class ExecDataService {
    @Autowired
    private TaskPersistHelper taskPersistHelper;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    public void sendKafkaWhenTaskEnded(String taskId){
        List<JobInvoke> jobs = taskPersistHelper.getJobsByTaskId(taskId);
        if(CollectionUtils.isNotEmpty(jobs)){
         this.sendMessage("FRIDAY_OPERATION_DISTRIBUTOR_DEFAULT", JSON.toJSONString(jobs));
        }
    }

    private void sendMessage(String topic, String message) {
        try {
            kafkaTemplate.send(topic, message);
        } catch (Exception e){
            log.error("kafka发送消息异常", e);
        }
    }

    public TaskResultStateDto getTaskState(String taskId) {
        List<JobInvoke> jobs = taskPersistHelper.getJobsByTaskId(taskId);
        TaskResultStateDto state = new TaskResultStateDto();
        if(CollectionUtils.isEmpty(jobs)){
            state.setExecState(ExecuteStateEnum.SUCCESS.getKey());
            return state;
        }
        List<String> jobStates = jobs.stream().map(JobInvoke::getJobState).collect(Collectors.toList());
        if(jobStates.contains(States.JobState.UNSTART.name()) || jobStates.contains(States.JobState.RUNNING.name()) || jobStates.contains(States.JobState.RUNNING_TIMEOUT.name())){
            state.setExecState(ExecuteStateEnum.RUNNING.getKey());
        } else if(jobStates.contains(States.JobState.FAIL.name())){
            state.setExecState(ExecuteStateEnum.FAIL.getKey());
            List<String> failJobMsg = jobs.stream().filter(j -> States.JobState.FAIL.name().equals(j.getJobState())).map(JobInvoke::getFailMsg).collect(Collectors.toList());
            String failMsg = String.join(";", failJobMsg);
            state.setFailMag(failMsg);
        } else if(jobStates.contains(States.JobState.START_TIMEOUT.name())){
            state.setExecState(ExecuteStateEnum.FAIL.getKey());
            state.setFailMag("任务启动超时");
        } else if(jobStates.contains(States.JobState.TERMINATE.name())){
            state.setExecState(ExecuteStateEnum.TERMINATE.getKey());
            state.setFailMag("任务异常终止");
        } else {
            state.setExecState(ExecuteStateEnum.SUCCESS.getKey());
        }
        return state;
    }

}
