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

import cn.axzo.workflow.core.service.ExtAxHiTaskInstService;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.impl.agenda.AbstractOperation;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

import static cn.axzo.workflow.common.constant.BpmnConstants.HIDDEN_ASSIGNEE_ID;

/**
 * 通用的在 Command 内执行删除流程实例时的额外操作
 *
 * @author wangli
 * @since 2024/1/3 20:03
 */
public class DeleteProcessInstanceOperation extends AbstractOperation {
    private final String processInstanceId;
    private final ExtAxHiTaskInstService extAxHiTaskInstService;
    private String customDeleteReason;

    public DeleteProcessInstanceOperation(CommandContext commandContext, String processInstanceId,
                                          ExtAxHiTaskInstService extAxHiTaskInstService) {
        super(commandContext, null);
        this.processInstanceId = processInstanceId;
        this.extAxHiTaskInstService = extAxHiTaskInstService;
    }

    public DeleteProcessInstanceOperation(CommandContext commandContext, String processInstanceId,
                                          ExtAxHiTaskInstService extAxHiTaskInstService,
                                          String customDeleteReason) {
        super(commandContext, null);
        this.processInstanceId = processInstanceId;
        this.extAxHiTaskInstService = extAxHiTaskInstService;
        this.customDeleteReason = customDeleteReason;
    }

    @Override
    public void run() {
        ProcessEngineConfigurationImpl processEngineConfiguration =
                CommandContextUtil.getProcessEngineConfiguration(commandContext);
        HistoryService historyService = processEngineConfiguration.getHistoryService();

        // 有一些节点是普通的 task,没有 ExecutionId,所以先主动去完成这些 node
        List<HistoricTaskInstance> historyTaskList =
                historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).unfinished().list();
        TaskService taskService = processEngineConfiguration.getTaskService();
        if (!CollectionUtils.isEmpty(historyTaskList)) {
            historyTaskList.stream().filter(i -> Objects.isNull(i.getExecutionId()))
                    .forEach(i -> {
                        taskService.setAssignee(i.getId(), HIDDEN_ASSIGNEE_ID);
                        taskService.complete(i.getId());
                        extAxHiTaskInstService.deleteStatusByTaskId(i.getId(), processInstanceId);
                    });

        }

        RuntimeService runtimeService = processEngineConfiguration.getRuntimeService();
        runtimeService.deleteProcessInstance(processInstanceId, StringUtils.hasText(customDeleteReason) ? customDeleteReason : HIDDEN_ASSIGNEE_ID);
        // 将结束流程实例的原因记录下来
//        runtimeService.deleteProcessInstance(processInstanceId, reason);
    }
}
