package com.purui.prims.dscheduler.service;

import com.purui.prims.common.config.DolphinSchedulerConfig;
import com.purui.prims.dscheduler.model.Project;
import com.purui.prims.headless.api.pojo.enums.DolphineSchedulerType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

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

/**
 * 类说明
 *
 * @author charlie
 * @date 2024/7/10
 */
@Service
@Slf4j
public class ProjectService extends AbstractSchedulerService {


    public ProjectService(DolphinSchedulerConfig config, RestTemplate restTemplate) {
        super(config, restTemplate);
    }

    private Project createProject() {
        Map<String, Object> data = new HashMap<>(2);
        data.put("projectName", "指标系统");
        data.put("description", "自动创建,禁止删除");
        Project project = runSchedulerX(0L, 0L, DolphineSchedulerType.PROJECT_CREATE, data, Project.class);
        log.info("create project: {}", project);
        return project;
    }

    public Project getProject() {
        // create at first whatever reason
        List<Project> projectList = runSchedulerListX(0L, 0L, DolphineSchedulerType.PROJECT_LIST, null, Project.class);
        log.info("project list: {}", projectList);
        Project project = projectList.stream().filter(p -> p.getName().equals("指标系统")).findFirst().orElse(null);
        if (project == null) {
            project = createProject();
        }
        return project;
    }

}
