package com.gyee.generation.processors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import tech.powerjob.official.processors.CommonBasicProcessor;
import tech.powerjob.worker.core.processor.ProcessResult;
import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.core.processor.WorkflowContext;
import tech.powerjob.worker.log.OmsLogger;

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

/**
 * @description: 工作流调度任务测试
 * @author: shilin
 * @date: 2024/5/29 9:19
 * @param:
 * @return:
 **/
@Slf4j
@Component("testBaseProcessor")
public class StandaloneProcessorDemo extends CommonBasicProcessor {

    @Override
    protected ProcessResult process0(TaskContext taskContext) {

        String jobParams = taskContext.getJobParams();
        OmsLogger omsLogger = taskContext.getOmsLogger();
        try {
            HashMap<String, Object> data = JSON.parseObject(jobParams, new TypeReference<HashMap<String, Object>>() {
            });
            WorkflowContext workflowContext = taskContext.getWorkflowContext();
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                workflowContext.appendData2WfContext(entry.getKey(), entry.getValue());
                omsLogger.info("inject context, {}:{}", entry.getKey(), entry.getValue());
            }

            workflowContext.appendData2WfContext("test1", 1);
//            workflowContext.appendData2WfContext("test2", "2");
//            workflowContext.appendData2WfContext("test3", new Date());
//            workflowContext.appendData2WfContext("test4", true);
        } catch (Exception e) {
            omsLogger.error("Fail to parse job params:{},it is not a valid json string!", jobParams, e);
            return new ProcessResult(false);
        }
        return new ProcessResult(true);
    }
}
