activiti 工作流

最近用到jeesite工作流,记录一下

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;


import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.act.entity.Act;
import com.thinkgem.jeesite.modules.act.service.ActTaskService;
import com.thinkgem.jeesite.modules.act.utils.ActUtils;
import com.thinkgem.jeesite.modules.oa.entity.TestAudit;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

//获取流程历史

public List<Comment> getActHis(HgZsglInvestigation hgZsglInvestigation) {
// 获取历史
List<Comment> historyCommnets = new ArrayList<Comment>();
// 1) 获取流程实例的ID
Task task2 = this.taskService.createTaskQuery().taskId(hgZsglInvestigation.getAct().getTaskId()).singleResult();
ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(task2.getProcessInstanceId())
.singleResult();
// 2)通过流程实例查询所有的(用户任务类型)历史活动
List<HistoricActivityInstance> hais = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(pi.getId()).activityType("userTask").list();
// 3)查询每个历史任务的批注
for (HistoricActivityInstance hai : hais) {
String historytaskId = hai.getTaskId();
List<Comment> comments = taskService.getTaskComments(historytaskId);
// 4)如果当前任务有批注信息,添加到集合中
if (comments != null && comments.size() > 0) {
historyCommnets.addAll(comments);
}
}
sortClass sort = new sortClass();
Collections.sort(historyCommnets, sort);
// 5)返回
return historyCommnets;

}

// 启动流程
@Transactional(readOnly = false)
public void mkSubmit_kc(HgZsglInvestigation hgZsglInvestigation) {
// 登陆用户
String userid = UserUtils.getUser().getId();
hgZsglInvestigation.setTypesp("2");//// 下一个业务环节知管审批
hgZsglInvestigation.preUpdate();
super.save(hgZsglInvestigation);
// 启动流程 先给节点赋值
identityService.setAuthenticatedUserId(hgZsglInvestigation.getCurrentUser().getLoginName());
Map<String, Object> vars = Maps.newHashMap();
vars.put("usertask1", "知识管理审批");//知管
vars.put("usertask2", "人力审批");//人力审批
vars.put("usertask3", "财务审批");//财务
vars.put("usertask4", "安全审批");//安全审批
vars.put("usertask5", "常务副总审批");//常务副总
vars.put("usertask6", "提交");//创建人
vars.put("usertask7", "知识管理审核");//知管审核
vars.put("usertask8", "知管入库");//知管入库
vars.put("modify", hgZsglInvestigation.getCreateBy().getLoginName());//创建人修改
actTaskService.startProcess(ActUtils.PD_HG_INVESTIGATION_KC[0], ActUtils.PD_HG_INVESTIGATION_KC[1],

hgZsglInvestigation.getId(), hgZsglInvestigation.getRemarks(), vars);

}

//获取流程实例
public Act getAct(HgZsglInvestigation hgZsglInvestigation) {


Task task = taskService.createTaskQuery().processInstanceId(hgZsglInvestigation.getProcInsId()).active()
.singleResult();
// 工作流Entity
Act act = new Act();
// 任务定义Key(任务环节标识)
act.setTaskDefKey(task.getTaskDefinitionKey());
// 流程实例ID
act.setProcInsId(hgZsglInvestigation.getProcInsId());
// 任务编号
act.setTaskId(task.getId());
// 任务对象
act.setTask(task);


return act;

}

//审批流经各个节点

public void investigatione_kcsp(HgZsglInvestigation hgZsglInvestigation) {
// 登陆用户
String userid = UserUtils.getUser().getId();
// 设置意见
hgZsglInvestigation.getAct()
.setComment(("yes".equals(hgZsglInvestigation.getAct().getFlag()) ? "[同意] " : "[驳回] ")
+ hgZsglInvestigation.getAct().getComment());
hgZsglInvestigation.preUpdate();
// 对不同环节的业务逻辑进行操作
String taskDefKey = hgZsglInvestigation.getAct().getTaskDefKey();
String str = hgZsglInvestigation.getAct().getComment();
// 审核环节
// 知管审批
if ("usertask1".equals(taskDefKey)) {
hgZsglInvestigation.setKnowledgesp(hgZsglInvestigation.getAct().getComment());
dao.updateknowledgesp(hgZsglInvestigation);
if (str.indexOf("同意") >= 0) {
// 人力审批
hgZsglInvestigation.setTypesp("3");
} else {
// 被驳回
hgZsglInvestigation.setTypesp("0");
}
super.save(hgZsglInvestigation);
} else if ("usertask2".equals(taskDefKey)) {
hgZsglInvestigation.setHumansp(hgZsglInvestigation.getAct().getComment());
dao.updateHumansp(hgZsglInvestigation);
if (str.indexOf("同意") >= 0) {
// 财务审批
hgZsglInvestigation.setTypesp("4");
} else {
// 被驳回
hgZsglInvestigation.setTypesp("0");
}
super.save(hgZsglInvestigation);
}
// 安全审批
else if ("usertask3".equals(taskDefKey)) {
hgZsglInvestigation.setFinancesp(hgZsglInvestigation.getAct().getComment());
dao.updateFinancesp(hgZsglInvestigation);
if (str.indexOf("同意") >= 0) {
// 安全审批
hgZsglInvestigation.setTypesp("5");
} else {
// 被驳回
hgZsglInvestigation.setTypesp("0");
}
super.save(hgZsglInvestigation);
}
// 常务副总审批
else if ("usertask4".equals(taskDefKey)) {
hgZsglInvestigation.setSecuritysp(hgZsglInvestigation.getAct().getComment());
dao.updatesecuritysp(hgZsglInvestigation);
if (str.indexOf("同意") >= 0) {
// 常务副总审批
hgZsglInvestigation.setTypesp("6");
} else {
// 被驳回
hgZsglInvestigation.setTypesp("0");
}
super.save(hgZsglInvestigation);
} else if ("usertask5".equals(taskDefKey)) {
hgZsglInvestigation.setStandingsp(hgZsglInvestigation.getAct().getComment());
dao.updateStandingsp(hgZsglInvestigation);
if (str.indexOf("同意") >= 0) {
// 提交
hgZsglInvestigation.setTypesp("7");
} else {
// 被驳回
hgZsglInvestigation.setTypesp("0");
}
super.save(hgZsglInvestigation);


} else if ("usertask6".equals(taskDefKey)) {
hgZsglInvestigation.setStandingsp(hgZsglInvestigation.getAct().getComment());
dao.updateStandingsp(hgZsglInvestigation);
if (str.indexOf("同意") >= 0) {
// 知管审批
hgZsglInvestigation.setTypesp("8");

super.save(hgZsglInvestigation);


} else if ("usertask7".equals(taskDefKey)) {
hgZsglInvestigation.setStandingsp(hgZsglInvestigation.getAct().getComment());
dao.updateStandingsp(hgZsglInvestigation);
if (str.indexOf("同意") >= 0) {
// 知管入库
hgZsglInvestigation.setTypesp("9");
} else {
// 被驳回
hgZsglInvestigation.setTypesp("7");
}
super.save(hgZsglInvestigation);


} else if ("usertask8".equals(taskDefKey)) {
hgZsglInvestigation.setStandingsp(hgZsglInvestigation.getAct().getComment());
dao.updateStandingsp(hgZsglInvestigation);
if (str.indexOf("同意") >= 0) {
// 知管入库
hgZsglInvestigation.setTypesp("10");
}
super.save(hgZsglInvestigation);


} else if ("modify".equals(taskDefKey)) {
if (str.indexOf("同意") >= 0) {
// 审批流程结束
hgZsglInvestigation.setTypesp("2");
} else {
super.delete(hgZsglInvestigation);
}
super.save(hgZsglInvestigation);
}


// 未知环节,直接返回
else {
return;
}
// 提交流程任务
Map<String, Object> vars = Maps.newHashMap();
vars.put("pass", "yes".equals(hgZsglInvestigation.getAct().getFlag()) ? "0" : "1");
actTaskService.complete(hgZsglInvestigation.getAct().getTaskId(), hgZsglInvestigation.getAct().getProcInsId(),
hgZsglInvestigation.getAct().getComment(), vars);

}

// 比较器
class sortClass implements Comparator {
public int compare(Object arg0, Object arg1) {
Comment comment0 = (Comment) arg0;
Comment Comment1 = (Comment) arg1;
int flag = comment0.getTime().compareTo(Comment1.getTime());
return flag;
}
}

猜你喜欢

转载自blog.csdn.net/m0_37681654/article/details/80669242