package com.ibiz.activiti.util;

import org.activiti.bpmn.model.FlowNode;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.persistence.entity.TaskEntityImpl;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 工作流节点跳转辅助类
 */
@Component
public class WFGoToUtil {

    @Autowired
    ManagementService managementService;

    @Autowired
    TaskService taskService;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    HistoryService historyService;

    //跳转方法
    public void jump(String sourceTaskId,String targetTaskId){
        //跳转源任务
        Task sourceTask = taskService.createTaskQuery().taskId(sourceTaskId).singleResult();
        //跳转目标任务
        HistoricTaskInstance targetTask = historyService.createHistoricTaskInstanceQuery().taskId(targetTaskId).singleResult();
        //获取流程定义
        Process process = repositoryService.getBpmnModel(sourceTask.getProcessDefinitionId()).getMainProcess();
        //获取目标节点定义
        FlowNode targetNode = (FlowNode)process.getFlowElement(targetTask.getTaskDefinitionKey());
        //删除当前运行任务
        String executionEntityId = managementService.executeCommand(new WFDeleteCmd(sourceTask.getId()));
        //流程执行到来源节点
        managementService.executeCommand(new WFGoToCmd(targetNode, executionEntityId));
    }


    //跳转方法
    public void jump(Task sourceTask, Task targetTask) {

        if(ObjectUtils.isEmpty(sourceTask) || ObjectUtils.isEmpty(targetTask))
            return ;

        //获取流程定义
        Process process = repositoryService.getBpmnModel(sourceTask.getProcessDefinitionId()).getMainProcess();
        //获取目标节点定义
        FlowNode targetNode = (FlowNode)process.getFlowElement(targetTask.getTaskDefinitionKey());
        //删除当前运行任务
        String executionEntityId = managementService.executeCommand(new WFDeleteCmd(sourceTask.getId()));
        //流程执行到来源节点
        managementService.executeCommand(new WFGoToCmd(targetNode, executionEntityId));
    }


    /**
     * Task转换
     * @param historicTaskInstance
     * @return
     */
    public Task historicTask2Task(HistoricTaskInstance historicTaskInstance){

        Task task =new TaskEntityImpl();

        if(ObjectUtils.isEmpty(historicTaskInstance))
            return task;

        BeanCopier copier=BeanCopier.create(historicTaskInstance.getClass(), task.getClass(), false);
        copier.copy(historicTaskInstance, task, null);

        return task;
    }


    /**
     * 获取下一步路径
     * @param curTask
     * @return
     */
    public List<SequenceFlow>  getNextFlows(Task curTask){

        //获取流程定义
        Process process = repositoryService.getBpmnModel(curTask.getProcessDefinitionId()).getMainProcess();

        //获取目标节点定义
        FlowNode flowElement = (FlowNode)process.getFlowElement(curTask.getTaskDefinitionKey());

        //获取当前节点对外的连线
        List<SequenceFlow> flows = flowElement.getOutgoingFlows();

        if(flows==null || flows.size()<1){
            throw new ActivitiException("当前步骤没有对外的连线!");
        }

        return flows;

    }
}


