package com.yfd.platform.component;

import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class TaskStatusHolder {

    private final ConcurrentHashMap<String, String> taskStatusMap = new ConcurrentHashMap<>();

    // 生成唯一Key：taskId + nodeId
    public String generateKey(String taskId, String nodeId) {
        return taskId + ":" + nodeId;
    }

    public String generateKeybyId(List<String> dataset) {
        return dataset + ":" ;
    }

    // 生成专项扫描唯一Key：project_id
    public String specialGenerateKey(String id) {
        return id + "扫描:";
    }
    // 生成专项文档上传唯一Key：project_id+上传:
    public String documentUploadKey(String id) {
        return id + "上传:";
    }

    // 生成试验任务扫描唯一Key：task_id
    public String testDatascanKey(String id) {
        return id + ":";
    }



    // 原子性检查并标记任务开始
    public boolean startTaskIfAbsent(String key) {
        return taskStatusMap.putIfAbsent(key, "IN_PROGRESS") == null;
    }
    // 标记任务结束
    public void finishTask(String key) {
        taskStatusMap.remove(key);
    }
    // 获取任务状态
    public String getStatus(String key) {
        return taskStatusMap.getOrDefault(key, "TASK_NOT_FOUND");
    }
}
