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

import cn.axzo.workflow.common.model.request.bpmn.activity.BpmnActivityTimeoutTriggerDTO;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnActivityTriggerDTO;
import cn.axzo.workflow.core.service.BpmnProcessActivityService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.job.service.JobHandler;
import org.flowable.job.service.impl.persistence.entity.JobEntity;
import org.flowable.variable.api.delegate.VariableScope;

/**
 * 业务节点的触发离开的任务处理器
 *
 * @author wangli
 * @since 2024-08-16 16:29
 */
@Slf4j
public class AsyncActivityLeaveJobHandler extends AbstractJobHandler implements JobHandler {
    private final BpmnProcessActivityService bpmnProcessActivityService;
    public static final String TYPE = "business-node-leave-task";

    public AsyncActivityLeaveJobHandler(BpmnProcessActivityService bpmnProcessActivityService) {
        this.bpmnProcessActivityService = bpmnProcessActivityService;
    }

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

    @Override
    public void execute(JobEntity job, String configuration, VariableScope variableScope, CommandContext commandContext) {
        log.warn("AsyncActivityLeaveJobHandler exec start...");
        BpmnActivityTimeoutTriggerDTO dto = JSON.parseObject(job.getCustomValues(), BpmnActivityTimeoutTriggerDTO.class);
        bpmnProcessActivityService.trigger(BpmnActivityTriggerDTO.builder().async(false).triggerId(dto.getTriggerId()).build());
    }
}
