package easydo.technology.service;

import easydo.technology.components.JdbcClient;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

@EnableAsync
@Service
public class AsyncService {
    @Resource
    JdbcClient jdbcClient;
    @Resource
    DataSource dataSource;

    private Map<String, CompletableFuture<String>> taskMap = new ConcurrentHashMap<>();


    @Async
    public void stopProcessingTask(String taskId) throws Exception {
        CompletableFuture<String> future = taskMap.get(taskId);
        if (future != null) {
            future.cancel(true);
            taskMap.remove(taskId);
        }
    }

    public void putMapTask(String taskId, CompletableFuture<String> future) {
        taskMap.put(taskId, future);
    }

    public void removeMapTask(String taskId) {
        taskMap.remove(taskId);
    }


}