package com.zzdy.second.controller;
import com.zzdy.second.repository.Flow_R;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.util.IoUtil;
import org.activiti.engine.task.Task;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
// 獲取 當前任務流程圖 高亮
@RestController
@Transactional
public class Flow_Get_Img_C {
@Autowired
RepositoryService repositoryService;
@Autowired
protected RuntimeService runtimeService;
@Autowired
ProcessEngineConfiguration processEngineConfiguration;
@Autowired
ProcessEngineFactoryBean processEngine;
@Autowired
HistoryService historyService;
@Autowired
TaskService taskService;
@Autowired
private Flow_R fr;
private String path = new File("/").getAbsolutePath();//或許當前磁盤路徑
/**
* 讀取帶跟蹤的圖片
*/
@RequestMapping("/Flow_Get_Img/{id}/{tableId}")
public String test(@PathVariable("id") Integer id,@PathVariable("tableId") String tableId) throws Exception {
//獲取歷史流程實例
String processInstanceId = fr.getFlowId(id, tableId);
//根據實例Id獲取流程部署Id
String FlowKey = fr.find_Flow_Name(processInstanceId);
HistoricProcessInstance processInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
//獲取流程圖
BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
processEngineConfiguration = processEngine.getProcessEngineConfiguration();
Context.setProcessEngineConfiguration((ProcessEngineConfigurationImpl) processEngineConfiguration);
ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
ProcessDefinitionEntity definitionEntity = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(processInstance.getProcessDefinitionId());
List<HistoricActivityInstance> highLightedActivitList = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId).list();
//高亮環節id集合
List<String> highLightedActivitis = new ArrayList<String>();
//高亮線路id集合
List<String> highLightedFlows = getHighLightedFlows(definitionEntity, highLightedActivitList);
//當前流程實例執行到哪個節點
ExecutionEntity execution = (ExecutionEntity) runtimeService.createExecutionQuery().executionId(processInstanceId).singleResult();// 執行實例
highLightedActivitis.add(execution.getActivityId());
String nowId=execution.getActivityId().replaceAll("-","");
String file = path + "img\\" + FlowKey + nowId + ".png";
boolean flag = judeFileExists(new File(file));
if (flag){
file = file.replaceAll(":", "%3A");
file = file.replaceAll("\\\\", "%5C");
return file;
} else {
//中文顯示的是口口口,設置字體就好了
InputStream imageStream = diagramGenerator.generateDiagram(bpmnModel, "png", highLightedActivitis, highLightedFlows, "宋體", "宋體", null, null, 1.0);
//單獨返回流程圖,不高亮顯示
// InputStream imageStream = diagramGenerator.generatePngDiagram(bpmnModel);
// 輸出資源內容到相應對象
try {
File File = new File(file);
FileUtils.copyInputStreamToFile(imageStream, File);
file = file.replaceAll(":", "%3A");
file = file.replaceAll("\\\\", "%5C");
return file;
}catch (Exception e) {
throw new RuntimeException("生成流程圖異常!", e);
} finally {
IOUtils.closeQuietly(imageStream);
}
}
}
/**
* 獲取需要高亮的線
* @param processDefinitionEntity
* @param historicActivityInstances
* @return
*/
private List<String> getHighLightedFlows(
ProcessDefinitionEntity processDefinitionEntity,
List<HistoricActivityInstance> historicActivityInstances) {
List<String> highFlows = new ArrayList<String>();// 用以保存高亮的線flowId
for (int i = 0; i < historicActivityInstances.size() - 1; i++) {// 對歷史流程節點進行遍歷
ActivityImpl activityImpl = processDefinitionEntity
.findActivity(historicActivityInstances.get(i)
.getActivityId());// 得到節點定義的詳細信息
List<ActivityImpl> sameStartTimeNodes = new ArrayList<ActivityImpl>();// 用以保存後需開始時間相同的節點
ActivityImpl sameActivityImpl1 = processDefinitionEntity
.findActivity(historicActivityInstances.get(i + 1)
.getActivityId());
// 將後面第一個節點放在時間相同節點的集合裏
sameStartTimeNodes.add(sameActivityImpl1);
for (int j = i + 1; j < historicActivityInstances.size() - 1; j++) {
HistoricActivityInstance activityImpl1 = historicActivityInstances
.get(j);// 後續第一個節點
HistoricActivityInstance activityImpl2 = historicActivityInstances
.get(j + 1);// 後續第二個節點
if (activityImpl1.getStartTime().equals(
activityImpl2.getStartTime())) {
// 如果第一個節點和第二個節點開始時間相同保存
ActivityImpl sameActivityImpl2 = processDefinitionEntity
.findActivity(activityImpl2.getActivityId());
sameStartTimeNodes.add(sameActivityImpl2);
} else {
// 有不相同跳出循環
break;
}
}
List<PvmTransition> pvmTransitions = activityImpl
.getOutgoingTransitions();// 取出節點的所有出去的線
for (PvmTransition pvmTransition : pvmTransitions) {
// 對所有的線進行遍歷
ActivityImpl pvmActivityImpl = (ActivityImpl) pvmTransition
.getDestination();
// 如果取出的線的目標節點存在時間相同的節點裏,保存該線的id,進行高亮顯示
if (sameStartTimeNodes.contains(pvmActivityImpl)) {
highFlows.add(pvmTransition.getId());
}
}
}
return highFlows;
}
// 判斷文件是否存在
public static boolean judeFileExists(File file) {
if (file.exists()) {
return true;
} else {
return false;
}
}
}