SpringBoot2.X集成activiti6.0.0

写在前面

activiti主要逻辑:
当我们画完流程图后,我们会给流程图起一个唯一的id,如下:
流程图文件讲解
此id将会作为这个流程的唯一标识,在代码中,我将其命名为workflowId,大致意思即为流程文件的id。
在activiti中,发起一个流程比较简单,发起一个流程之后,每个环节会变成一个任务,完成当前任务才会生成下一个任务,再非分支任务的情况下(分支任务即一次同时生成多个任务,可以生成多个taskId),此时taskId较难以维护,在activiti中也不建议采用此方式绘制流程图。单一分支情况下,我们可以通过流程实例id获取到任务id,从而使用任务id来完成任务,直至任务办结为止。
参数讲解:
processInstanceId:流程实例id,流程发起后会返回给你一个流程实例对象,此时最重要的参数是此对象的id。可以用此id获取到当前流程的任务信息,查看流程图信息等。
taskId:任务id,完成任务最重要的参数,如果不进行taskId的维护,且不是并行流程的情况下,我们可以在完成任务时通过流程实例id查询到taskId,用于正确的完成任务。而并行流程情况下,我们需要用流程实例id和执行器id(executionId)联合查询,才能确定一个唯一的任务。

activiti依赖引入

下面展示一些 内联代码片

 <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-basic</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-jpa</artifactId>
            <version>6.0.0</version>
 </dependency>

activiti配置文件

在resource文件夹下新建activiti.cfg.xml,内容如下:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <!-- 配置 ProcessEngineConfiguration  -->
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <!-- 配置数据库连接 -->
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/activiti?autoReconnect=true&amp;failOverReadOnly=false&amp;characterEncoding=utf-8&amp;useUnicode=true&amp;zeroDateTimeBehavior=convertToNull&amp;allowMultiQueries=true"></property>
        <property name="jdbcUsername" value="root"></property>
        <property name="jdbcPassword" value="123456"></property>
        <!-- 配置创建表策略 :没有表时,自动创建 -->
        <property name="databaseSchemaUpdate" value="true"></property>
    </bean>
</beans>

Application.yml中添加activiti配置

spring:
	activiti:
    	database-schema-update: true
    	check-process-definitions: true
    	process-definition-location-prefix: classpath:/processes/
    	history-level: full
    	db-history-used: true

注:此时添加完配置,启动会报错,因为需要在resource文件夹下新增processes文件夹,此文件夹用于存储流程图文件

SpringBoot相关依赖

 <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>${tk.mybatis.boot.version}</version>
</dependency>

注:由于activiti和tk.mybatis中都添加了jpa的依赖,因此我们需要去掉一个,此处我们去掉tk.mybatis中的jpa依赖,未引入tk.mybatis或mybatisPlus的可以不做这一步,如下:

 <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>${tk.mybatis.boot.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>persistence-api</artifactId>
                    <groupId>javax.persistence</groupId>
                </exclusion>
            </exclusions>
</dependency>

流程图绘制

在idea中需要下载actiBpm插件,插件可以去idea官网插件市场下载,但是此插件只支持idea2020.2.2以下版本安装。

此处我们绘制以下流程图:
请假流程图

流程相关方法封装

@Autowired
	RepositoryService rep;


	@Autowired
	ProcessEngine processEngine;


	@Autowired
	ITicketService ticketService;

	@Autowired
	IdentityService identityservice;

	@Autowired
	RuntimeService runtimeService;


	@Autowired
	TaskService taskService;



	@Autowired
	HistoryService historyService;


	@Resource
	BusinessTableInfoMapper businessTableInfoMapper;

	@Resource
	WorkflowTaskInfoMapper workflowTaskInfoMapper;











	/**************************通用***************************/

	/**
	 * 任务流转
	 * @param completeTaskVO 任务流转接收实体
	 * @return
	 */
	@Override
	public TaskRepDto circulationTask(CirculationTaskDto completeTaskVO){
		if(StringUtils.isEmpty(completeTaskVO.getUsername())){
			throw new BusinessException("username不存在!");
		}
		if(StringUtils.isEmpty(completeTaskVO.getWorkflowId())){
			throw new BusinessException("workflowId不能为空!");
		}
		log.info("任务id{}",completeTaskVO.getTaskId());
		Task task = taskService.createTaskQuery().taskId(completeTaskVO.getTaskId()).singleResult();
		if (null == task) {
			throw new BusinessException("任务不存在或已完成!");
		}
		String statusCode=null;
		//任务对象  获取流程实例Id
		String processInstanceId = task.getProcessInstanceId();
		//通过流程实例id 拿到业务主键
		ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
		String businessKey= pi.getBusinessKey();
		//设置审批人的userId
		Authentication.setAuthenticatedUserId(completeTaskVO.getUsername());
		//添加评论记录
		taskService.addComment(completeTaskVO.getTaskId(), processInstanceId, completeTaskVO.getMsg());
		log.info("-----------完成任务操作 开始----------");
		log.info("任务Id=" + completeTaskVO.getTaskId());
		log.info("负责人用户名=" + completeTaskVO.getUsername());
		log.info("流程实例id=" + processInstanceId);
		//完成办理
		//有分支情况 需要传参数
		Map<String, Object> map = new HashMap<>();
		map.put("isAgree", completeTaskVO.getIsAgree());
		try {
			taskService.claim(completeTaskVO.getTaskId(),completeTaskVO.getUsername());
			taskService.complete(completeTaskVO.getTaskId(), map);
		}catch (Exception e){
			throw new BusinessException("您无权限对此流程进行审批!");
		}

		log.info("-----------完成任务操作 结束----------");
		Task nextTask= taskService.createTaskQuery().processInstanceId(processInstanceId).executionId(task.getExecutionId()).singleResult();

		//todo 判断是否为自己能审批,第一思路,添加下一步审批人角色id做判断 解决bug:单人审批多步
		TaskRepDto taskRepDto = new TaskRepDto();
		taskRepDto.setTaskId(nextTask.getId());
		taskRepDto.setTaskName(nextTask.getName());
		taskRepDto.setDescription(nextTask.getDescription());
		taskRepDto.setAssignee(nextTask.getAssignee());
		taskRepDto.setProcessInstanceId(nextTask.getProcessInstanceId());
		taskRepDto.setWorkflowId(nextTask.getProcessDefinitionId());
		taskRepDto.setBeforeTaskName(task.getName());
		return taskRepDto;
	}




	/**
	 * 启动流程
	 * @param username 用户名
	 * @param workFlowId 流程id
	 * @return
	 */
	@Override
	public TaskRepDto startFlow(String username,String businessKey, String workFlowId,Map<String, Object> param) {
		//先对workFlowId判空
		if(StringUtils.isEmpty(workFlowId)){
			throw new BusinessException("workflowId不能为空!");
		}
		//在对用户名判空
		if(StringUtils.isEmpty(username)){
			throw new BusinessException("用户名不能为空!");
		}
		//对业务主键判空
		if(StringUtils.isEmpty(businessKey)){
			throw new BusinessException("业务主键不能为空!");
		}
		//设置流程起草人
		identityservice.setAuthenticatedUserId(username);
		//防止传空
		if(null == param){
			param= new HashMap<>();
		}
		param.put("draft", username);
		//开启流程
		ProcessInstance instance = null;
		try {
			 instance=runtimeService.startProcessInstanceByKey(workFlowId,businessKey,param);
		}catch (Exception e){
			throw new BusinessException("workFlowId不存在,流程发起失败!");
		}
		Task nextTask= taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();
		String statusCode=null;
		TaskRepDto taskRepDto =getTaskInfoByProcessInstanceId(instance.getId());
		//调用通过流程实例id拿到下个任务实体数据的方法

		return taskRepDto;
	}

	/**
	 * 启动流程
	 * @param username 用户名
	 * @param workFlowId 流程id
	 * @return
	 */
	@Override
	public TaskRepDto startFlow(String username, String businessKey, String workFlowId) {
		return startFlow(username, businessKey, workFlowId, new HashMap<String, Object>());
	}


	/**
	 * 获取我的待办列表
	 * @param username 用户名
	 * @param workflowId 流程id
	 * @return
	 */
	@Override
	public List<TaskInfoDetailDto> myFlowList(String username, String workflowId) {
		//先对workFlowId判空
		if(StringUtils.isEmpty(workflowId)){
			throw new BusinessException("workflowId不能为空!");
		}
		//在对用户名判空
		if(StringUtils.isEmpty(username)){
			throw new BusinessException("用户名不能为空!");
		}
		List<Task> taskList=null;
		// 查询我的待办任务
		try{
			taskList = taskService
					//创建任务查询对象
					.createTaskQuery()
					/** 查询条件(where部分) */
					.processDefinitionKey(workflowId)
					.taskCandidateOrAssigned(username)
					/** 排序 */
					.orderByTaskCreateTime().asc()// 使用创建时间的升序排列
					/** 返回结果集 */
					.list();// 返回列表
		}catch (Exception e){
			throw new BusinessException("workflowId不存在");
		}

		List<TaskInfoDetailDto> taskInfoDetailDtoList = new ArrayList<>();
		if (taskList != null && taskList.size() > 0) {
			for (Task task : taskList) {
				TaskInfoDetailDto taskInfoDetailDto = new TaskInfoDetailDto();
				taskInfoDetailDto.setTaskId(task.getId());
				taskInfoDetailDto.setTaskName(task.getName());
				taskInfoDetailDto.setDescription(task.getDescription());
				taskInfoDetailDto.setAssignee(task.getAssignee());
				taskInfoDetailDto.setCreateTime(task.getCreateTime());
				taskInfoDetailDto.setProcessInstanceId(task.getProcessInstanceId());
				taskInfoDetailDto.setWorkflowId(task.getProcessDefinitionId());
				taskInfoDetailDtoList.add(taskInfoDetailDto);
			}
		}
		return taskInfoDetailDtoList;

	}

	@Override
	public List<TaskInfoDetailDto> myFlowList(String username, List<String> workflowIdList) {
		//先对workFlowId判空
		if(null==workflowIdList||workflowIdList.size()==0){
			throw new BusinessException("workflowId不能为空!");
		}
		//在对用户名判空
		if(StringUtils.isEmpty(username)){
			throw new BusinessException("用户名不能为空!");
		}
		List<Task> taskList=null;
		// 查询我的待办任务
		try{
			taskList = taskService
					//创建任务查询对象
					.createTaskQuery()
					/** 查询条件(where部分) */
					.processDefinitionKeyIn(workflowIdList)
					.taskCandidateOrAssigned(username)
					/** 排序 */
					.orderByTaskCreateTime().asc()// 使用创建时间的升序排列
					/** 返回结果集 */
					.list();// 返回列表
		}catch (Exception e){
			throw new BusinessException("workflowId不存在");
		}

		List<TaskInfoDetailDto> taskInfoDetailDtoList = new ArrayList<>();
		if (taskList != null && taskList.size() > 0) {
			for (Task task : taskList) {
				TaskInfoDetailDto taskInfoDetailDto = new TaskInfoDetailDto();
				taskInfoDetailDto.setTaskId(task.getId());
				taskInfoDetailDto.setTaskName(task.getName());
				taskInfoDetailDto.setDescription(task.getDescription());
				taskInfoDetailDto.setAssignee(task.getAssignee());
				taskInfoDetailDto.setCreateTime(task.getCreateTime());
				taskInfoDetailDto.setProcessInstanceId(task.getProcessInstanceId());
				taskInfoDetailDto.setWorkflowId(task.getProcessDefinitionId());
				taskInfoDetailDtoList.add(taskInfoDetailDto);
			}
		}
		return taskInfoDetailDtoList;
	}

	/**
	 * 我的待办列表 分页
	 * @param username 用户名
	 * @param workflowIdList 流程id列表
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public PageResult<TaskInfoDetailDto> myFlowList(String username, List<String> workflowIdList, Integer pageNum, Integer pageSize) {
		//分页 给pageNum和pageSize默认值
		if(null==pageNum||pageNum==0){
			pageNum=1;
		}
		if(null==pageSize||pageSize==0){
			pageSize=10;
		}
		//先对workFlowId判空
		if(null==workflowIdList||workflowIdList.size()==0){
			throw new BusinessException("workflowId不能为空!");
		}
		//在对用户名判空
		if(StringUtils.isEmpty(username)){
			throw new BusinessException("用户名不能为空!");
		}
		int startIndex=(pageNum-1)*pageSize;
		List<Task> taskList=null;
		// 查询我的待办任务

		taskList = taskService
				//创建任务查询对象
				.createTaskQuery()
				/** 查询条件(where部分) */
				.processDefinitionKeyIn(workflowIdList)
				.taskCandidateOrAssigned(username)
				/** 排序 */
				.orderByTaskCreateTime().asc()// 使用创建时间的升序排列
				/** 返回结果集 */
				.listPage(startIndex, pageSize);// 返回列表
		long count = taskService
				//创建任务查询对象
				.createTaskQuery()
				/** 查询条件(where部分) */
				.processDefinitionKeyIn(workflowIdList)
				.taskCandidateOrAssigned(username)
				/** 排序 */
				.orderByTaskCreateTime().asc()// 使用创建时间的升序排列
				/** 返回结果集 */
				.count();
		List<TaskInfoDetailDto> taskInfoDetailDtoList = new ArrayList<>();
		if (taskList != null && taskList.size() > 0) {
			for (Task task : taskList) {
				TaskInfoDetailDto taskInfoDetailDto = new TaskInfoDetailDto();
				taskInfoDetailDto.setTaskId(task.getId());
				taskInfoDetailDto.setTaskName(task.getName());
				taskInfoDetailDto.setDescription(task.getDescription());
				taskInfoDetailDto.setAssignee(task.getAssignee());
				taskInfoDetailDto.setCreateTime(task.getCreateTime());
				taskInfoDetailDto.setProcessInstanceId(task.getProcessInstanceId());
				taskInfoDetailDto.setWorkflowId(task.getProcessDefinitionId());
				taskInfoDetailDtoList.add(taskInfoDetailDto);
			}
		}
		PageResult pageResult = new PageResult(count,taskInfoDetailDtoList,pageSize,pageNum);
		return pageResult;
	}

	/**
	 * 我审批完成的流程列表查看
	 * @param username 用户名
	 * @param workflowId 流程id
	 * @return
	 */
	@Override
	public List<String> myApprovalFlowList(String username, String workflowId) {
		//先对workFlowId判空
		if(StringUtils.isEmpty(workflowId)){
			throw new BusinessException("workflowId不能为空!");
		}
		//在对用户名判空
		if(StringUtils.isEmpty(username)){
			throw new BusinessException("用户名不能为空!");
		}
		List<HistoricTaskInstance> historicTaskInstanceList = processEngine.getHistoryService()
				.createHistoricTaskInstanceQuery()
				.taskAssignee(username)
				.processDefinitionKey(workflowId)
				.finished()
				.list();
		List<String> processInstanceIdList = new ArrayList<>();
		if (historicTaskInstanceList != null && historicTaskInstanceList.size() > 0) {
			for (HistoricTaskInstance historicTaskInstance : historicTaskInstanceList) {
				if(!processInstanceIdList.contains(historicTaskInstance.getProcessInstanceId())){
					processInstanceIdList.add(historicTaskInstance.getProcessInstanceId());
				}
			}
		}
		return processInstanceIdList;
	}

	/**
	 * 查询
	 * @param username 用户名
	 * @return
	 */
	@Override
	public List<String> myApprovalFlowList(String username,List<String> workflowIdList) {
		//先对workFlowId判空
		if(null == workflowIdList ||  workflowIdList.size()==0){
			throw new BusinessException("workflowId不能为空!");
		}
		//在对用户名判空
		if(StringUtils.isEmpty(username)){
			throw new BusinessException("用户名不能为空!");
		}
		List<HistoricTaskInstance> historicTaskInstanceList = processEngine.getHistoryService()
				.createHistoricTaskInstanceQuery()
				.taskAssignee(username)
				.processDefinitionKeyIn(workflowIdList)
				.finished()
				.list();
		List<String> processInstanceIdList = new ArrayList<>();
		if (historicTaskInstanceList != null && historicTaskInstanceList.size() > 0) {
			for (HistoricTaskInstance historicTaskInstance : historicTaskInstanceList) {
				if(!processInstanceIdList.contains(historicTaskInstance.getProcessInstanceId())){
					processInstanceIdList.add(historicTaskInstance.getProcessInstanceId());
				}
			}
		}
		return processInstanceIdList;
	}



	/**
	 * 查询已办列表 分页
	 * @param username 用户名
	 * @return
	 */
	@Override
	public PageResult<String> myApprovalFlowList(String username,List<String> workflowIdList,Integer pageNum,Integer pageSize) {
		if(null==pageNum||pageNum==0){
			pageNum=1;
		}
		if(null==pageSize||pageSize==0){
			pageSize=10;
		}
		int startIndex=(pageNum-1)*pageSize;
		//先对workFlowId判空
		if(null == workflowIdList ||  workflowIdList.size()==0){
			throw new BusinessException("workflowId不能为空!");
		}
		//在对用户名判空
		if(StringUtils.isEmpty(username)){
			throw new BusinessException("用户名不能为空!");
		}
		List<HistoricTaskInstance> historicTaskInstanceList = processEngine.getHistoryService()
				.createHistoricTaskInstanceQuery()
				.taskAssignee(username)
				.processDefinitionKeyIn(workflowIdList)
				.finished()
				.listPage(startIndex,pageSize);
		long count = processEngine.getHistoryService()
				.createHistoricTaskInstanceQuery()
				.taskAssignee(username)
				.processDefinitionKeyIn(workflowIdList)
				.finished()
				.count();
		List<String> processInstanceIdList = new ArrayList<>();
		if (historicTaskInstanceList != null && historicTaskInstanceList.size() > 0) {
			for (HistoricTaskInstance historicTaskInstance : historicTaskInstanceList) {
				if(!processInstanceIdList.contains(historicTaskInstance.getProcessInstanceId())){
					processInstanceIdList.add(historicTaskInstance.getProcessInstanceId());
				}
			}
		}
		PageResult pageResult = new PageResult(count,processInstanceIdList,pageSize,pageNum);
		return pageResult;
	}

	/**
	 * 通过流程实例id 拿到任务信息
	 * @param instanceId
	 * @return
	 */
	@Override
	public TaskRepDto getTaskInfoByProcessInstanceId(String instanceId) {
		Task nextTask = taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
		if(null == nextTask){
			return null;
		}
		TaskRepDto taskRepDto = new TaskRepDto();
		taskRepDto.setTaskId(nextTask.getId());
		taskRepDto.setTaskName(nextTask.getName());
		taskRepDto.setDescription(nextTask.getDescription());
		taskRepDto.setAssignee(nextTask.getAssignee());
		taskRepDto.setProcessInstanceId(nextTask.getProcessInstanceId());
		taskRepDto.setWorkflowId(nextTask.getProcessDefinitionId());
		return taskRepDto;

	}

	/**
	 * 根据流程实例id查询任务id
	 * @param instanceId
	 * @return
	 */
	@Override
	public String getTaskIdByInstanceId(String instanceId) {
		return taskService.createTaskQuery().processInstanceId(instanceId).singleResult().getId();
	}


	/**
	 * 根据任务id获取任务信息
	 * @param taskId 任务id
	 * @return
	 */
	@Override
	public TaskRepDto getTaskInfoByTaskId(String taskId) {
		if(StringUtils.isEmpty(taskId)){
			return null;
		}
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		TaskRepDto taskRepDto = new TaskRepDto();
		taskRepDto.setTaskId(task.getId());
		taskRepDto.setTaskName(task.getName());
		taskRepDto.setDescription(task.getDescription());
		taskRepDto.setAssignee(task.getAssignee());
		taskRepDto.setProcessInstanceId(task.getProcessInstanceId());
		taskRepDto.setWorkflowId(task.getProcessDefinitionId());
		return taskRepDto;
	}

	/**
	 * 根据实例id查询评论数据
	 * @param processInstanceId 流程实例id
	 * @return
	 */
	@Override
	public List<CommentDto> getCommentByInstanceId(String processInstanceId) {
		List<Comment> commentList=taskService.getProcessInstanceComments(processInstanceId);
		List<CommentDto> commentDtoList = new ArrayList<>();
		for(Comment comment : commentList){
			CommentDto commentDto = new CommentDto();
			commentDto.setMsg(comment.getFullMessage());
			commentDto.setUsername(comment.getUserId());
			/**
			 * 然后用用户名查询用户信息 这一步在工作流中做 还是在业务中做
			 */
			commentDtoList.add(commentDto);
		}
		return commentDtoList;
	}

	/**
	 * 通过流程实例id查询历史任务
	 * @param processInstanceId 流程实例id
	 * @return
	 */
	@Override
	public List<TaskInfoDetailDto> getHisTaskListByProcessInstanceId(String processInstanceId) {
		List<HistoricTaskInstance> historicTaskInstanceList = processEngine.getHistoryService()
				.createHistoricTaskInstanceQuery()
				.processInstanceId(processInstanceId)
				.list();
		List<TaskInfoDetailDto> taskInfoDetailDtoList = new ArrayList<>();
		if (historicTaskInstanceList != null && historicTaskInstanceList.size() > 0) {
			for (HistoricTaskInstance historicTaskInstance : historicTaskInstanceList) {
					TaskInfoDetailDto taskInfoDetailDto = new TaskInfoDetailDto();
					taskInfoDetailDto.setTaskId(historicTaskInstance.getId());
					taskInfoDetailDto.setProcessInstanceId(historicTaskInstance.getProcessInstanceId());
					taskInfoDetailDto.setTaskName(historicTaskInstance.getName());
					taskInfoDetailDto.setAssignee(historicTaskInstance.getAssignee());
					taskInfoDetailDto.setCreateTime(historicTaskInstance.getEndTime());
					taskInfoDetailDto.setDescription(historicTaskInstance.getDescription());
					taskInfoDetailDto.setWorkflowId(historicTaskInstance.getProcessDefinitionId());
					taskInfoDetailDtoList.add(taskInfoDetailDto);
			}

		}
		return taskInfoDetailDtoList;
	}

	/**
	 * 获取我的所有流程数据
	 * @param userName
	 * @param workflowIdList
	 * @return
	 */
	@Override
	public List<String> getMyAllProcessInstanceIdList(String userName, List<String> workflowIdList) {
		List<HistoricProcessInstance> historicProcessInstanceList = historyService.createHistoricProcessInstanceQuery().involvedUser(userName).processDefinitionKeyIn(workflowIdList).list();
		List<String> processInstanceIdList = new ArrayList<>();
		if(null != historicProcessInstanceList && historicProcessInstanceList.size()>0){
			for(HistoricProcessInstance historicProcessInstance : historicProcessInstanceList){
				String id = historicProcessInstance.getId();
				processInstanceIdList.add(id);
			}
		}
		return processInstanceIdList;
	}

	/**
	 * 获取我的所有流程数据 分页
	 * @param userName 用户名
	 * @param workflowIdList 流程文件id列表
	 * @param pageNum 页码
	 * @param pageSize 页大小
	 * @return
	 */
	@Override
	public PageResult<String> getMyAllProcessInstanceIdList(String userName, List<String> workflowIdList, Integer pageNum, Integer pageSize) {
		if(null==pageNum||pageNum==0){
			pageNum=1;
		}
		if(null==pageSize||pageSize==0){
			pageSize=10;
		}
		int startIndex=(pageNum-1)*pageSize;
		Set set = new HashSet(workflowIdList);
		//List<ProcessInstance> processInstanceList = runtimeService.createProcessInstanceQuery().processDefinitionKeys(set).involvedUser(userName).listPage(startIndex,pageSize);
		List<HistoricProcessInstance> historicProcessInstanceList = historyService.createHistoricProcessInstanceQuery().involvedUser(userName).processDefinitionKeyIn(workflowIdList).listPage(startIndex,pageSize);
		long count=historyService.createHistoricProcessInstanceQuery().involvedUser(userName).processDefinitionKeyIn(workflowIdList).count();
		// long count = runtimeService.createProcessInstanceQuery().processDefinitionKeys(set).involvedUser(userName).count();
		List<String> processInstanceIdList = new ArrayList<>();
		if(null != historicProcessInstanceList && historicProcessInstanceList.size()>0){
			for(HistoricProcessInstance processInstance : historicProcessInstanceList){
				String id = processInstance.getId();
				processInstanceIdList.add(id);
			}
		}
		return new PageResult<String>(count,processInstanceIdList,pageSize,pageNum);
	}

	/**
	 * 通过流程实例id获取业务主键
	 * @param processInstanceId
	 * @return
	 */
	@Override
	public String getBusinessKeyByProcessInstanceId(String processInstanceId) {
		String businessKey=workflowTaskInfoMapper.getBusinessKeyByProcessInstanceId(processInstanceId);
		return businessKey;
	}

	/**
	 * 我的已办列表 分页
	 * @param userName
	 * @param workflowIdList
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public PageResult<TaskInfoDetailDto> myApprovalList(String userName, List<String> workflowIdList, Integer pageNum, Integer pageSize) {
		if(null==pageNum||pageNum==0){
			pageNum=1;
		}
		if(null==pageSize||pageSize==0){
			pageSize=10;
		}
		int startIndex=(pageNum-1)*pageSize;
		//先对workFlowId判空
		if(null == workflowIdList ||  workflowIdList.size()==0){
			throw new BusinessException("workflowId不能为空!");
		}
		//在对用户名判空
		if(StringUtils.isEmpty(userName)){
			throw new BusinessException("用户名不能为空!");
		}
		List<HistoricTaskInstance> historicTaskInstanceList = processEngine.getHistoryService()
				.createHistoricTaskInstanceQuery()
				.taskAssignee(userName)
				.processDefinitionKeyIn(workflowIdList)
				.finished()
				.listPage(startIndex,pageSize);
		long count = processEngine.getHistoryService()
				.createHistoricTaskInstanceQuery()
				.taskAssignee(userName)
				.processDefinitionKeyIn(workflowIdList)
				.finished()
				.count();
		List<TaskInfoDetailDto> taskInfoDetailDtoList = new ArrayList<>();
		if (historicTaskInstanceList != null && historicTaskInstanceList.size() > 0) {
			for (HistoricTaskInstance historicTaskInstance : historicTaskInstanceList) {
				TaskInfoDetailDto taskInfoDetailDto = new TaskInfoDetailDto();
				taskInfoDetailDto.setTaskId(historicTaskInstance.getId());
				taskInfoDetailDto.setTaskName(historicTaskInstance.getName());
				taskInfoDetailDto.setDescription(historicTaskInstance.getDescription());
				taskInfoDetailDto.setAssignee(historicTaskInstance.getAssignee());
				taskInfoDetailDto.setCreateTime(historicTaskInstance.getCreateTime());
				taskInfoDetailDto.setProcessInstanceId(historicTaskInstance.getProcessInstanceId());
				taskInfoDetailDto.setWorkflowId(historicTaskInstance.getProcessDefinitionId());
				taskInfoDetailDtoList.add(taskInfoDetailDto);
			}
		}
		PageResult pageResult = new PageResult(count,taskInfoDetailDtoList,pageSize,pageNum);
		return pageResult;
	}

	/**
	 * 我的所有流程列表 分页
	 * @param userName
	 * @param workflowIdList
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public PageResult<TaskInfoDetailDto> myAllFlowList(String userName, List<String> workflowIdList, Integer pageNum, Integer pageSize) {
		//分页 给pageNum和pageSize默认值
		if(null==pageNum||pageNum==0){
			pageNum=1;
		}
		if(null==pageSize||pageSize==0){
			pageSize=10;
		}
		//先对workFlowId判空
		if(null==workflowIdList||workflowIdList.size()==0){
			throw new BusinessException("workflowId不能为空!");
		}
		//在对用户名判空
		if(StringUtils.isEmpty(userName)){
			throw new BusinessException("用户名不能为空!");
		}
		int startIndex=(pageNum-1)*pageSize;
		List<HistoricProcessInstance> historicProcessInstanceList = historyService
				.createHistoricProcessInstanceQuery()
				.involvedUser(userName)
				.processDefinitionKeyIn(workflowIdList)
				.listPage(startIndex,pageSize);
		long count = historyService
				.createHistoricProcessInstanceQuery()
				.involvedUser(userName)
				.processDefinitionKeyIn(workflowIdList)
				.count();
		List<TaskInfoDetailDto> taskInfoDetailDtoList = new ArrayList<>();
		if (historicProcessInstanceList != null && historicProcessInstanceList.size() > 0) {
			for (HistoricProcessInstance historicProcessInstance : historicProcessInstanceList) {
				Task task = taskService.createTaskQuery().processInstanceId(historicProcessInstance.getId()).singleResult();
				TaskInfoDetailDto taskInfoDetailDto = new TaskInfoDetailDto();
				if(null!=task){
					taskInfoDetailDto.setTaskId(task.getId());
					taskInfoDetailDto.setTaskName(task.getName());
					taskInfoDetailDto.setDescription(task.getDescription());
					taskInfoDetailDto.setAssignee(task.getAssignee());
					taskInfoDetailDto.setCreateTime(task.getCreateTime());
					taskInfoDetailDto.setProcessInstanceId(task.getProcessInstanceId());
					taskInfoDetailDto.setWorkflowId(task.getProcessDefinitionId());
					taskInfoDetailDtoList.add(taskInfoDetailDto);
				}
			}
		}
		PageResult pageResult = new PageResult(count,taskInfoDetailDtoList,pageSize,pageNum);
		return pageResult;
	}

	/**
	 * 通过任务id获取活动id
	 * @param taskId
	 * @return
	 */
	@Override
	public String getActivityIdByTaskId(String taskId) {
		if(StringUtils.isEmpty(taskId)){
			return null;
		}
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		if(null==task){
			return null;
		}
		return task.getTaskDefinitionKey();
	}

	/**
	 * 根据任务id拿到起草人
	 * @param taskId
	 * @return
	 */
	@Override
	public String getDraftByTaskId(String taskId) {
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		if(null==task){
			return null;
		}
		String processInstanceId = task.getProcessInstanceId();
		//根据流程实例id拿到起草人
		String startUserId = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult().getStartUserId();
		return startUserId;
	}
	/**************************通用************************************/

实体封装

@Data
public class CirculationTaskDto implements Serializable {

	/**
	 * 评论
	 */
	private String msg;


	/**
	 * 待办任务id
	 */
	private String taskId;


	/**
	 * 用户名
	 */
	private String username;

	/**
	 * 是否同意 1为同意 0为驳回
	 */
	private String isAgree;

	/**
	 * 流程文件id
	 */
	private String workflowId;
}


public class TaskInfoDetailDto implements Serializable {

	/**
	 * 任务id
	 */
	private String taskId;

	/**
	 * 任务名称
	 */
	private String taskName;

	/**
	 * 任务创建时间
	 */
	private Date createTime;

	/**
	 * 办理人
	 */
	private String assignee;

	/**
	 * 流程实例id
	 */
	private String processInstanceId;


	/**
	 * 描述
	 */
	private String description;


	/**
	 * 流程类型
	 */
	private String workflowId;


	/**
	 * 业务主键
	 */
	private String businessKey;



}
public class TaskRepDto {

	/**
	 * 任务id
	 */
	private String taskId;


	/**
	 * 任务描述
	 */
	private String description;

	/**
	 * 流程实例id
	 */
	private String processInstanceId;

	/**
	 * 任务名称
	 */
	private String taskName;

	/**
	 * 上一步任务名称
	 */
	private String beforeTaskName;


	/**
	 * 审批人
	 */
	private String assignee;


	/**
	 * 业务状态码
	 */
	private String businessStatusCode;

	/**
	 * 流程文件id 
	 */
	private String workflowId;


}

查看运行流程图

public void traceProcess(HttpServletResponse response, @RequestParam("processInstanceId")String instanceId) throws Exception{
		InputStream in = flowUtils.getResourceDiagramInputStream(instanceId);
		ServletOutputStream output = response.getOutputStream();
		IOUtils.copy(in, output);
	}
@Component
public class FlowUtils {
	@Autowired
	RuntimeService runservice;
	@Autowired
	private HistoryService historyService;
	@Autowired
	private RepositoryService repositoryService;
	@Autowired
	private ProcessEngineFactoryBean processEngine;

	/**
	 * 获取历史节点流程图
	 * @param id
	 * @return
	 */
	public InputStream getResourceDiagramInputStream(String id) {
		try {
			// 获取历史流程实例
			HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(id).singleResult();
			// 获取流程中已经执行的节点,按照执行先后顺序排序
			List<HistoricActivityInstance> historicActivityInstanceList = historyService.createHistoricActivityInstanceQuery().processInstanceId(id).orderByHistoricActivityInstanceId().asc().list();
			// 构造已执行的节点ID集合
			List<String> executedActivityIdList = new ArrayList<String>();
			for (HistoricActivityInstance activityInstance : historicActivityInstanceList) {
				executedActivityIdList.add(activityInstance.getActivityId());
			}
			// 获取bpmnModel
			BpmnModel bpmnModel = repositoryService.getBpmnModel(historicProcessInstance.getProcessDefinitionId());
			// 获取流程已发生流转的线ID集合
			List<String> flowIds = this.getExecutedFlows(bpmnModel, historicActivityInstanceList);
			// 使用默认配置获得流程图表生成器,并生成追踪图片字符流
			ProcessDiagramGenerator processDiagramGenerator = processEngine.getProcessEngineConfiguration().getProcessDiagramGenerator();
			//你也可以 new 一个
			//DefaultProcessDiagramGenerator processDiagramGenerator = new DefaultProcessDiagramGenerator();
			InputStream imageStream = processDiagramGenerator.generateDiagram(bpmnModel, "png", executedActivityIdList, flowIds, "宋体", "微软雅黑", "黑体", null, 2.0);
			return imageStream;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	private List<String> getExecutedFlows(BpmnModel bpmnModel,
										  List<HistoricActivityInstance> historicActivityInstanceList) {
		List<String> executedFlowIdList = new ArrayList<>();
		for (int i = 0; i < historicActivityInstanceList.size() - 1; i++) {
			HistoricActivityInstance hai = historicActivityInstanceList.get(i);
			FlowNode flowNode = (FlowNode) bpmnModel.getFlowElement(hai.getActivityId());
			List<SequenceFlow> sequenceFlows = flowNode.getOutgoingFlows();
			if (sequenceFlows.size() > 1) {
				HistoricActivityInstance nextHai = historicActivityInstanceList.get(i + 1);
				sequenceFlows.forEach(sequenceFlow -> {
					if (sequenceFlow.getTargetFlowElement().getId().equals(nextHai.getActivityId())) {
						executedFlowIdList.add(sequenceFlow.getId());
					}
				});
			} else if (sequenceFlows.size() == 1) {
				executedFlowIdList.add(sequenceFlows.get(0).getId());
			}
		}
		return executedFlowIdList;
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!感谢您的问题。以下是安装 Activiti 的一般步骤: 1. 首先,您需要下载 Activiti 的安装文件。您可以从 Activiti 官方网站或 GitHub 上找到最新版本的安装包。 2. 解压下载的文件到您选择的目录。您将获得一个包含 Activiti 运行时所需的所有文件和文件夹的文件夹。 3. 安装 Java 开发工具包(JDK),确保您的计算机上已经安装了 JDK。Activiti 运行时需要 Java 环境来运行。 4. 设置 Java 环境变量。将 JDK 的安装路径添加到系统的 PATH 环境变量中。这样,您就可以从任何位置运行 Java 命令。 5. 配置 Activiti 运行时环境。打开 Activiti 安装文件夹中的 `activiti.cfg.xml` 文件,并根据您的需求进行必要的配置,例如数据库连接等。 6. 启动 Activiti。在命令行或终端中导航到 Activiti 安装文件夹,并运行启动命令,如 `./activiti.sh start` 或 `./activiti.bat start`。 7. 验证安装。打开您的 Web 浏览器,输入 Activiti 的 URL(通常是 `http://localhost:8080/activiti`)来访问 Activiti 的管理控制台。如果一切顺利,您将看到 Activiti 的登录页面。 请注意,这只是一个大致的安装过程概述。具体的安装步骤可能因所使用的操作系统、Java 版本和其他因素而有所不同。在安装 Activiti 之前,建议您查阅 Activiti 的官方文档或社区资源以获取更详细的安装指南和最新的信息。 希望对您有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值