package com.purui.prims.dscheduler.service;

import com.alibaba.fastjson.JSON;
import com.purui.prims.common.config.DolphinSchedulerConfig;
import com.purui.prims.dscheduler.model.TaskDefinition;
import com.purui.prims.dscheduler.model.TaskDefinitionLog;
import com.purui.prims.dscheduler.model.TaskDefinitionVO;
import com.purui.prims.dscheduler.model.TaskInstance;
import com.purui.prims.dscheduler.model.TaskLog;
import com.purui.prims.dscheduler.model.TaskMainInfo;
import com.purui.prims.dscheduler.result.PageInfo;
import com.purui.prims.dscheduler.utils.ParamUtil;
import com.purui.prims.headless.api.pojo.enums.DolphineSchedulerType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务服务
 *
 * @author charlie
 * @date 2024/7/10
 */
@Service
@Slf4j
public class TaskService extends AbstractSchedulerService {
    public TaskService(DolphinSchedulerConfig config, RestTemplate restTemplate) {
        super(config, restTemplate);
    }

    /**
     * 创建任务定义
     *
     * @param projectCode 项目编号
     * @param processCode 工作流定义编号
     * @return {@link TaskDefinitionLog}
     */
    public TaskDefinitionLog createTaskDefinition(
        Long projectCode, Long processCode, List<String> upstreamCodes, String taskDefinitionJsonObj) {
        Map<String, Object> params = new HashMap<>();
        params.put("processDefinitionCode", processCode);
        if (CollectionUtils.isNotEmpty(upstreamCodes)) {
            params.put("upstreamCodes", StringUtils.join(upstreamCodes, ","));
        }

        params.put("taskDefinitionJsonObj", taskDefinitionJsonObj);

        return runSchedulerX(projectCode, 0L, DolphineSchedulerType.TASK_CREATE, params, TaskDefinitionLog.class);
    }

    /**
     * 查询任务定义集合
     *
     * @param projectCode 项目编号
     * @param processName 工作流定义名称
     * @param taskName    任务定义名称
     * @return {@link List<TaskMainInfo>}
     */
    public List<TaskMainInfo> getTaskMainInfos(Long projectCode, String processName, String taskName, String taskType) {
        Map<String, Object> pageParams = ParamUtil.getPageParams();
        pageParams.put("searchTaskName", taskName);
        pageParams.put("searchWorkflowName", processName);
        pageParams.put("taskType", taskType);

        PageInfo data = runSchedulerX(projectCode, 0L, DolphineSchedulerType.TASK_LIST, pageParams, PageInfo.class);

        List<TaskMainInfo> lists = new ArrayList<>();
        if (data == null || data.getTotalList() == null) {
            return lists;
        }
        lists = JSON.parseArray(JSON.toJSONString(data.getTotalList()), TaskMainInfo.class);
        return lists;
    }

    /**
     * 根据编号查询
     *
     * @param projectCode 项目编号
     * @param taskCode    任务编号
     * @return {@link TaskDefinition}
     */
    public TaskDefinitionVO getTaskDefinition(Long projectCode, Long taskCode) {
        return runSchedulerX(projectCode, taskCode, DolphineSchedulerType.TASK_DETAIL, null, TaskDefinitionVO.class);

    }

    // 查询任务代码
    public Long getTaskCode(Long projectCode, String taskName) {
        List<TaskMainInfo> lists = getTaskMainInfos(projectCode, "", taskName, "");
        if (CollectionUtils.isNotEmpty(lists)) {
            return lists.get(0).getTaskCode();
        }
        return 0L;
    }

    /**
     * 根据任务名称查询任务实例
     *
     * @param projectCode
     * @param taskCode
     * @return
     */
    public List<TaskInstance> getTaskInstances(Long projectCode, Long taskCode, String startDate, String endDate) {
        Map<String, Object> pageParams = ParamUtil.getPageParams();
        pageParams.put("taskCode", taskCode);
        pageParams.put("startDate", startDate);
        pageParams.put("endDate", endDate);
        PageInfo data = runSchedulerX(projectCode, taskCode, DolphineSchedulerType.TASK_INSTANCE_LIST,
            pageParams, PageInfo.class);
        if (data != null && data.getTotalList() != null) {
            return JSON.parseArray(JSON.toJSONString(data.getTotalList()), TaskInstance.class);
        }
        return null;
    }

    /**
     * 查询指标任务执行日志
     *
     * @param projectCode
     * @param instanceId
     * @return
     */
    public TaskLog queryTaskLog(Long projectCode, int instanceId) {
        if (instanceId > 0) {
            Map<String, Object> params = new HashMap<>(4);
            params.put("taskInstanceId", instanceId);
            params.put("skipLineNum", 0);
            params.put("limit", 2000);
            return runSchedulerX(projectCode, 0L, DolphineSchedulerType.TASK_LOG,
                params, TaskLog.class);
        }
        return null;
    }

    /**
     * 修改任务定义
     *
     * @param projectCode           项目编号
     * @param taskCode              任务定义编号
     * @param taskDefinitionJsonObj 修改参数
     * @return {@link Long}
     */
    public Long updateTaskDefinition(
        long projectCode, long taskCode, List<String> upstreamCodes, String taskDefinitionJsonObj) {
        Map<String, Object> params = new HashMap<>();
        if (CollectionUtils.isNotEmpty(upstreamCodes)) {
            params.put("upstreamCodes", StringUtils.join(upstreamCodes, ","));
        }
        params.put("taskDefinitionJsonObj", taskDefinitionJsonObj);

        return runSchedulerX(projectCode, taskCode, DolphineSchedulerType.TASK_UPDATE, params, Long.class);
    }

    /**
     * 生成任务定义编号
     *
     * @param projectCode 项目编号
     * @param genNum      生成个数
     * @return {@link List}
     */
    public List<Long> genTaskCodes(Long projectCode, int genNum) {
        Map<String, Object> params = new HashMap<>();
        params.put("genNum", genNum);
        return runSchedulerX(projectCode, 0L, DolphineSchedulerType.TASK_GEN_CODE, params, List.class);
    }

    /**
     * 运行任务
     *
     * @param projectCode
     * @param taskCode
     * @return
     */
    public boolean runTaskNow(Long projectCode, Long taskCode) {
        TaskDefinitionVO taskDefinitionVO = getTaskDefinition(projectCode, taskCode);
        if (taskDefinitionVO == null) {
            return false;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("version", taskDefinitionVO.getVersion());
        // 失败策略
        params.put("failureStrategy", "END");
        // 告警策略
        params.put("warningType", "NONE");

        return runSchedulerX(projectCode, taskCode, DolphineSchedulerType.TASK_RUN_NOW, params, Boolean.class);
    }

}
