1. 设置申请人,并通过key启动一个流程实例;
下图中:Authentication.setAuthenticatedUserId(userName);//直接调用底层实现,设置申请人,
但是需要定义流程定义时配置:activiti:initiator属性
Authentication.setAuthenticatedUserId(userName);//直接调用底层实现
Map<String,String> workOrder = (Map)params.get("workOrder");
String processDefinitionKey = workOrder.get("taskExample");
String nextHandleName = workOrder.get("nextHandleName");
Map<String, Object> variables = new HashMap<String, Object>();
//变量名是num,变量值是holiday.getNum(),变量名也可以是一个对象
variables.put("assigneeUserId", nextHandleName);
// 根据流程定义key启动流程
ProcessInstance processInstance = runtimeService
.startProcessInstanceByKey(processDefinitionKey,businessKey,variables);
2. 查询发起人发起的正在申请中的任务单
String userName = SecurityUtils.getUsername();
// 查询发起人的正在执行的任务列表
List<Execution> executionList = runtimeService.createExecutionQuery() //创建正在执行的流程查询对象
.processDefinitionKey(processKey) //根据流程定义的key查询
.startedBy(userName) // 根据发起人查询
.orderByProcessInstanceId() //根据流程实例id排序
.desc() //倒序
.listPage(firstIndex,MaxIndex); //查询出集合
// 查询发起人的正在执行的任务总量
long count = runtimeService.createExecutionQuery() //创建正在执行的流程查询对象
.processDefinitionKey(processKey) //根据流程定义的key查询
.startedBy(userName) // 根据发起人查询
.count();
3.查询自己发起的已完成的任务单
int firstIndex = (pageNumber-1) * pageSize;
int MaxIndex = pageNumber * pageSize - 1;
List<HistoricProcessInstance> finshedList = historyService.createHistoricProcessInstanceQuery()
.processDefinitionKey(processKey)
.startedBy(userName).finished().listPage(firstIndex, MaxIndex);
long count = historyService.createHistoricProcessInstanceQuery()
.processDefinitionKey(processKey)
.startedBy(userName).finished().count();
4. 查询自己的待办任务
int firstIndex = (pageNumber-1) * pageSize;
int MaxIndex = pageNumber * pageSize - 1;
List<Task> list = taskService.createTaskQuery()//
.processDefinitionKey(processKey).taskAssigneeIds(ids)
.includeProcessVariables().listPage(firstIndex,MaxIndex);
long count = taskService.createTaskQuery()//
.processDefinitionKey(processKey).taskAssigneeIds(ids)
.includeProcessVariables().count();
5. 办理/审批 自己的任务单: 通过/驳回,并添加批注
public Map<String,Object> handleWorkOrderServer(String taskId,String processId,String handleSelect,String handleIdea) {
Map<String,Object> result = new HashMap<>();
Long userId = SecurityUtils.getUserId();
//String roleIdByUserId = activitiCommonDao.findRoleIdByUserId(userId);
String userName = SecurityUtils.getUsername();
// 使用任务id,获取任务对象,获取流程实例id
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
if(task != null){
// 如果task为空,则可继续处理
String processInstancesId = task.getProcessInstanceId();
Authentication.setAuthenticatedUserId(userName); // 添加批注时候的审核人,通常应该从session获取
//变量名是num,变量值是holiday.getNum(),变量名也可以是一个对象
// 处理区域,先判断是驳回还是通过
if("accept".equals(handleSelect)) {
taskService.addComment(taskId, processInstancesId, handleIdea);
Map map = new HashMap();
map.put("checkState",true);
taskService.complete(taskId,map);
result.put("msg", "审批通过成功");
}else {
// 处理区域,是驳回,则结束整个任务单
taskService.addComment(taskId, processInstancesId, "驳回:"+handleIdea);
runtimeService.deleteProcessInstance(processId, "驳回");
result.put("msg", "任务驳回成功");
}
result.put("status", "1");
}else {
// 如果task为空,则已被处理
result.put("status", "0");
result.put("msg", "此任务已被处理");
}
6. 一般在任务单通过审批结束后,需要将业务入库到真正的业务表:
@Component
@Aspect
public class HandleWorkOrderAspect {
@Autowired
private HistoryService historyService;
@Autowired
private ActivitiCommonService activitiCommonService;
@Autowired
private ActivitiRuleDao activitiRuleDao;
@Pointcut("execution(* handleWorkOrderServer*(..))")
public void pointCut() {}
@After("pointCut()")
public void after(JoinPoint point){
Object[] args = point.getArgs(); //获取目标对象方法参数
String processId= String.valueOf(args[1]);
String handleSelect= String.valueOf(args[2]);
if("accept".equals(handleSelect)){
List<HistoricProcessInstance> finshedList = historyService.createHistoricProcessInstanceQuery().processInstanceId(processId).finished().list();
// 根据流程实例ID去查询它
// 如果finshedList集合不为空,则是通过审批并结束,而不是驳回导致的结束
if( !CollectionUtils.isEmpty(finshedList)){
// 以下是真正的业务入库
Map<String, String> workOrder = (Map)activitiCommonService.findWorkOrderByBusinessKey(finshedList.get(0).getBusinessKey());
workOrder.put("operType","handle");
Map<String, Object> taskBusiness = activitiCommonService.findTaskBusiness(workOrder);
String taskType = workOrder.get("taskType");
if ("规则流程".equals(taskType)) {
List ruleList = (List)taskBusiness.get("ruleList");
activitiRuleDao.saveRuleList(ruleList);
} else {
// Map commonBusiness = (Map)taskBusiness.get("commonBusiness");
// activitiCommonDao.findTaskDraftBusiness(commonBusiness);
}
}
}
}
}
7. 查询自己的 已办任务,并查询且展示当前节点信息
public Map<String,Object> findDoneOrderByName(int pageNumber,int pageSize ,String processKey ) {
Map<String,Object> resultMap = new HashMap<>();
List<Map<String,Object>> resultList = new ArrayList();
// 任务负责人
String userName = SecurityUtils.getUsername();
Long userId = SecurityUtils.getUserId();
List<String> ids = activitiCommonDao.findRoleIdByUserId(userId);
ids.add(userId.toString());
ids.add(SecurityUtils.getUsername());
// 创建TaskService
int firstIndex = (pageNumber-1) * pageSize;
int MaxIndex = pageNumber * pageSize - 1;
List<HistoricTaskInstance> doneList = historyService.createHistoricTaskInstanceQuery()
.taskAssigneeIds(ids)
.finished().listPage(firstIndex,MaxIndex);
long count = historyService.createHistoricTaskInstanceQuery()
.taskAssigneeIds(ids)
.finished().count();
for (HistoricTaskInstance task : doneList) {
// 查看任务对应的工单信息
List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).list();
Map<String, Object> workOrder = activitiCommonService.findWorkOrderByBusinessKey(list.get(0).getBusinessKey());
if( !CollectionUtils.isEmpty(workOrder) ) {
workOrder.put("processId",task.getProcessInstanceId());
}
//查询待办的审批节点信息
HistoricActivityInstance hai=historyService.createHistoricActivityInstanceQuery()//
.processInstanceId(task.getProcessInstanceId()).unfinished()
.singleResult();
// 不为null流程未结束,获取当前待办的审批节点信息
if(hai!=null){
workOrder.put("handleStatus", hai.getActivityName());// 流程状态
}else{
// 流程结束,查看状态是驳回还是完结,这个 task.getDeleteReason()的值是在驳回删除流程时设置的
if(org.apache.commons.lang3.StringUtils.isNotEmpty(task.getDeleteReason())){
workOrder.put("handleStatus", task.getDeleteReason());// 流程状态
}else{
workOrder.put("handleStatus", "完结");// 流程状态
}
}
resultList.add(workOrder);
}
resultMap.put("total",count);
resultMap.put("list",resultList);
return resultMap;
}
8.查询一个任务的所有审批节点的 节点名,审批人,审批意见(批注)
/**
* 查询一个任务所有的审批意见
* @param processInstanceId
* @return
*/
public List<Map<String,Object>> getProcessComments(String processInstanceId) {
List<Map<String,Object>> historyCommnets = new ArrayList<>();
// 2)通过流程实例查询所有的(用户任务类型)历史活动
List<HistoricActivityInstance> hais = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId).activityType("userTask").list();
// 3)查询每个历史任务的批注
for (HistoricActivityInstance hai : hais) {
String historytaskId = hai.getTaskId();
List<Comment> comments = taskService.getTaskComments(historytaskId);
// 4)如果当前任务有批注信息,添加到集合中
if( !CollectionUtils.isEmpty(comments)){
for(Comment comment: comments){
Map map = new HashMap();
map.put("userId",comment.getUserId());
map.put("message",comment.getFullMessage());
map.put("activityName",hai.getActivityName());
historyCommnets.add(map);
}
}
}
// 5)返回
return historyCommnets;