package cn.axzo.workflow.core.engine.job;

import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskResetApproversDTO;
import cn.axzo.workflow.core.engine.cmd.CustomResetTaskApproversCmd;
import cn.axzo.workflow.core.service.ExtAxHiTaskInstService;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.job.service.JobHandler;
import org.flowable.job.service.impl.persistence.entity.JobEntity;
import org.flowable.variable.api.delegate.VariableScope;

@Slf4j
public class AsyncResetApproversUserTaskJobHandler extends AbstractJobHandler implements JobHandler {

    public static final String TYPE = "async-reset-approves-task";

    private final ExtAxHiTaskInstService extAxHiTaskInstService;

    public AsyncResetApproversUserTaskJobHandler(ExtAxHiTaskInstService extAxHiTaskInstService) {
        this.extAxHiTaskInstService = extAxHiTaskInstService;
    }

    @Override
    public String getType() {
        return TYPE;
    }

    @Override
    public void execute(JobEntity job, String configuration, VariableScope variableScope, CommandContext commandContext) {
        log.info("AsyncCountersignUserTaskJobHandler executing...");
        log(job);
        ProcessEngineConfigurationImpl processEngineConfiguration =
                CommandContextUtil.getProcessEngineConfiguration(commandContext);
        BpmnTaskResetApproversDTO dto = JSONUtil.toBean(job.getCustomValues(), BpmnTaskResetApproversDTO.class);
        processEngineConfiguration.getCommandExecutor().execute(new CustomResetTaskApproversCmd(dto.getTaskId(),
                dto.getAdvice(),
                dto.getAttachmentList(),
                dto.getOriginAssigner(),
                dto.getTargetAssignerList(),
                extAxHiTaskInstService));
    }
}
