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

import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskRemindDTO;
import cn.axzo.workflow.core.conf.SupportRefreshProperties;
import cn.axzo.workflow.core.engine.cmd.CustomRemindTaskCmd;
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;

/**
 * 异步的催办任务的处理器
 *
 * @author wangli
 * @since 2025-06-13 14:13
 */
@Slf4j
public class AsyncRemindTaskJobHandler extends AbstractExecuteWithLockJobHandler implements JobHandler {
    public static final String TYPE = "async-remind-task";
    private final SupportRefreshProperties refreshProperties;

    public AsyncRemindTaskJobHandler(SupportRefreshProperties refreshProperties) {
        this.refreshProperties = refreshProperties;
    }

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

    @Override
    public void executeInternal(JobEntity job, String configuration, VariableScope variableScope, CommandContext commandContext) {
        log.info("AsyncRejectTaskJobHandler executing...");
        log(job);
        ProcessEngineConfigurationImpl processEngineConfiguration =
                CommandContextUtil.getProcessEngineConfiguration(commandContext);
        BpmnTaskRemindDTO dto = JSONUtil.toBean(job.getCustomValues(), BpmnTaskRemindDTO.class);
        processEngineConfiguration.getCommandExecutor().execute(new CustomRemindTaskCmd(dto.getTerminalType(),
                dto.getProcessInstanceId(), dto.getTaskDefinitionKey(), dto.getRemindTypes(), refreshProperties));
    }
}
