package com.purui.prims.dscheduler.model;

import com.purui.prims.dscheduler.enums.Flag;
import com.purui.prims.dscheduler.enums.Priority;
import com.purui.prims.dscheduler.enums.TaskExecuteType;
import com.purui.prims.dscheduler.enums.TimeoutFlag;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Data
public class DSTaskRequest extends TaskRequest {

    public static final String TASK_TYPE = "DATAX";
    public static final String DW_TYPE = "DORIS";

    // 自定义参数
    private List<Property> localParams;

    // 上游任务编号
    List<String> upstreamCodes = Arrays.asList();

    // 调度周期
    private String scheduleCycle;


    private Long projectCode;

    // 任务id
    @NotNull
    private String taskId;

    public DSTaskRequest create(String name, String sql, List<String> preSql, String postSql) {
        return this.create(name, sql, preSql, postSql, 1, 1, DW_TYPE, DW_TYPE);
    }

    public DSTaskRequest create(String name, String sql, List<String> preSql, String postSql, int sourceId, int targetId, String sourceType,
                                String targetType) {
        this.setName(name);
        this.setTaskParams(createDataXTaskParams(sql, preSql, postSql, name, sourceId, targetId, sourceType, targetType));
        this.setTaskType(TASK_TYPE);
        this.setTaskExecuteType(TaskExecuteType.BATCH);
        this.setTaskPriority(Priority.MEDIUM.toString());
        this.setFlag(Flag.YES.toString());
        this.setIsCache(Flag.NO.toString());
        this.setWorkerGroup("default");
        this.setTimeout(0);
        this.setTimeoutFlag(TimeoutFlag.CLOSE.toString());
        this.setDelayTime(0);
        this.setFailRetryTimes(0);
        this.setFailRetryInterval(1);
        return this;
    }

    // 创建SQL任务参数
    private TaskParams createSQLTaskParams(String sql, String preSql, String postSql) {
        TaskParams taskParams = new TaskParams();
        taskParams.setSql(sql);
        taskParams.setSqlType("1");
        taskParams.setType(DW_TYPE);
        taskParams.setDatasource(1);
        taskParams.setLocalParams(new ArrayList<>());
        if (preSql != null) {
            taskParams.setPreStatements(Arrays.asList(preSql));
        }
        if (postSql != null) {
            taskParams.setPostStatements(Arrays.asList(postSql));
        }
        return taskParams;
    }

    // 创建DataX任务参数
    private DataXParams createDataXTaskParams(String sql, List<String> preSql, String postSql, String tableName, int sourceId, int targetId,
                                              String sourceType, String targetType) {
        if (StringUtils.isEmpty(sourceType)) {
            sourceType = DW_TYPE;
        }
        if (StringUtils.isEmpty(targetType)) {
            targetType = DW_TYPE;
        }
        DataXParams taskParams = new DataXParams();
        taskParams.setSql(sql);
        taskParams.setDataSource(sourceId);
        taskParams.setDsType(sourceType);
        taskParams.setDataTarget(targetId);
        taskParams.setDtType(targetType);
        taskParams.setTargetTable(tableName);
        taskParams.setLocalParams(new ArrayList<>());
        if (preSql != null) {
            taskParams.setPreStatements(preSql);
        }
        if (postSql != null) {
            taskParams.setPostStatements(Arrays.asList(postSql));
        }
        return taskParams;
    }

}
