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

import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskDelegateAssigner;
import cn.axzo.workflow.core.common.exception.WorkflowEngineException;
import cn.axzo.workflow.core.engine.operation.DeleteProcessInstanceOperation;
import cn.axzo.workflow.core.service.ExtAxHiTaskInstService;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.history.HistoricProcessInstanceQuery;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.task.api.Task;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static cn.axzo.workflow.common.constant.BpmnConstants.COMMENT_TYPE_OPERATION_DESC;
import static cn.axzo.workflow.common.constant.BpmnConstants.INTERNAL_DELETE_PROCESS_FLAG;
import static cn.axzo.workflow.common.constant.BpmnConstants.INTERNAL_END_TENANT_ID;
import static cn.axzo.workflow.common.constant.BpmnConstants.INTERNAL_END_USER_ID;
import static cn.axzo.workflow.common.constant.BpmnConstants.INTERNAL_END_USER_NAME;
import static cn.axzo.workflow.common.constant.BpmnConstants.INTERNAL_PROCESS_DELETE_REASON;
import static cn.axzo.workflow.common.constant.BpmnConstants.INTERNAL_PROCESS_TYPE_ABORT;
import static cn.axzo.workflow.common.constant.BpmnConstants.TASK_ASSIGNEE_SKIP_FLAT;
import static cn.axzo.workflow.common.constant.BpmnConstants.TASK_COMPLETE_OPERATION_TYPE;
import static cn.axzo.workflow.common.enums.BpmnFlowNodeType.NODE_ABORT;
import static cn.axzo.workflow.common.enums.BpmnProcessInstanceResultEnum.ABORTED;
import static cn.axzo.workflow.common.enums.BpmnProcessInstanceResultEnum.CANCELLED;
import static cn.axzo.workflow.core.common.code.BpmnInstanceRespCode.PROCESS_INSTANCE_CANCEL_FAIL_NOT_EXISTS;
import static cn.axzo.workflow.core.common.code.BpmnInstanceRespCode.PROCESS_INSTANCE_CANT_ABORT;
import static cn.axzo.workflow.core.common.code.BpmnInstanceRespCode.PROCESS_INSTANCE_NOT_EXISTS;
import static cn.axzo.workflow.core.engine.cmd.helper.CustomTaskHelper.addComment;
import static cn.axzo.workflow.core.engine.cmd.helper.CustomTaskHelper.createVirtualTask;

/**
 * 自定义的中止流程实例的命令实现
 *
 * @author wangli
 * @since 2024/1/2 18:19
 */
public class CustomAbortProcessInstanceCmd implements Command<Void>, Serializable {
    private final String processInstanceId;
    private final String tenantId;
    private final String reason;
    private final ExtAxHiTaskInstService extAxHiTaskInstService;

    public CustomAbortProcessInstanceCmd(String processInstanceId, String tenantId, String reason,
                                         ExtAxHiTaskInstService extAxHiTaskInstService) {
        this.processInstanceId = processInstanceId;
        this.tenantId = tenantId;
        this.reason = reason;
        this.extAxHiTaskInstService = extAxHiTaskInstService;
    }

    @Override
    public Void execute(CommandContext commandContext) {
        ProcessEngineConfigurationImpl processEngineConfiguration =
                CommandContextUtil.getProcessEngineConfiguration(commandContext);

        HistoryService historyService = processEngineConfiguration.getHistoryService();
        HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(processInstanceId);
        if (StringUtils.hasText(tenantId)) {
            query.processInstanceTenantId(tenantId);
        }
        HistoricProcessInstance instance = query.singleResult();
        if (Objects.isNull(instance)) {
            throw new WorkflowEngineException(PROCESS_INSTANCE_NOT_EXISTS);
        }

        if (Objects.nonNull(instance.getEndTime())) {
            throw new WorkflowEngineException(PROCESS_INSTANCE_CANCEL_FAIL_NOT_EXISTS);
        }

        if (Objects.equals(instance.getBusinessStatus(), CANCELLED.getStatus())) {
            throw new WorkflowEngineException(PROCESS_INSTANCE_CANT_ABORT);
        }

        Map<String, Object> variables = new HashMap<>();
        variables.put(INTERNAL_END_TENANT_ID, tenantId);
        variables.put(INTERNAL_END_USER_ID, "system");
        variables.put(INTERNAL_END_USER_NAME, "系统");
        variables.put(INTERNAL_DELETE_PROCESS_FLAG, INTERNAL_PROCESS_TYPE_ABORT);
        variables.put(INTERNAL_PROCESS_DELETE_REASON, reason);
        RuntimeService runtimeService = processEngineConfiguration.getRuntimeService();
        runtimeService.setVariables(instance.getId(), variables);

        CommandContextUtil.getAgenda(commandContext).planOperation(new DeleteProcessInstanceOperation(commandContext,
                processInstanceId, extAxHiTaskInstService));

        // 添加自定义的节点,用于展示最后的操作
        Task task = createVirtualTask(commandContext, extAxHiTaskInstService, processInstanceId,
                "系统中止", NODE_ABORT.getType(), null, BpmnTaskDelegateAssigner.buildDummyAssigner("system",
                        TASK_ASSIGNEE_SKIP_FLAT, "系统"), ABORTED.getStatus());
        addComment(commandContext, task, COMMENT_TYPE_OPERATION_DESC, reason);
        runtimeService.setVariable(task.getProcessInstanceId(), TASK_COMPLETE_OPERATION_TYPE + task.getId(), ABORTED);
        return null;
    }
}
