提交 5e81e604 编写于 作者: sq3536's avatar sq3536

工作流编程

上级 77e28278
package cn.ibizlab.core.extensions.service;
import cn.ibizlab.core.workflow.domain.WFProcessDefinition;
import cn.ibizlab.core.workflow.domain.WFProcessInstance;
import cn.ibizlab.core.workflow.domain.WFProcessNode;
import cn.ibizlab.core.workflow.domain.WFTaskWay;
import cn.ibizlab.core.workflow.service.IWFProcessDefinitionService;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.security.AuthenticationUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import java.sql.Timestamp;
import java.util.*;
public class WFCoreService
{
@Autowired
private ProcessRuntime processRuntime;
@Autowired
private TaskRuntime taskRuntime;
@Autowired
private RepositoryService repositoryService;
@Autowired
private HistoryService historyService;
@Autowired
private TaskService taskService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private WFModelService wfModelService;
public List<WFProcessDefinition> getWorkflow(String system,String appname,String entity) {
return wfModelService.getWorkflow(system,entity);
}
public List<String> getWorkflowKey(String system,String appname,String entity) {
List<String> definitionKeys=new ArrayList<>();
for(WFProcessDefinition wfdef:wfModelService.getWorkflow(system,entity))
definitionKeys.add(wfdef.getDefinitionkey());
return definitionKeys;
}
public List<WFProcessNode> getWFStep(String system,String appname, String entity) {
List<WFProcessNode> rt=new ArrayList<>();
for(WFProcessDefinition wfdef:wfModelService.getWorkflow(system,entity)) {
LinkedHashMap<String,UserTask> userTasks = wfModelService.getModelStepByKey(wfdef.getDefinitionkey());
for(UserTask userTask:userTasks.values()) {
WFProcessNode node=new WFProcessNode();
node.setUsertaskid(userTask.getId());
node.setUsertaskname(userTask.getName());
node.setProcessdefinitionkey(wfdef.getDefinitionkey());
node.setProcessdefinitionname(wfdef.getDefinitionname());
rt.add(node);
}
}
return rt;
}
public Map<String,Long> getWFStepCnt(String system,String appname, String entity,String processDefinitionKey,String wfstepcode)
{
Map<String,Long> wfStepCnt = new LinkedHashMap<String,Long>();
String userId=AuthenticationUser.getAuthenticationUser().getUserid();
if(StringUtils.isEmpty(userId))
return wfStepCnt;
TaskQuery query=taskService.createTaskQuery().taskCandidateOrAssigned(userId);
if(!StringUtils.isEmpty(processDefinitionKey))
query.processDefinitionKey(processDefinitionKey);
else {
query.processDefinitionKeyIn(this.getWorkflowKey(system,appname,entity));
}
if(!StringUtils.isEmpty(wfstepcode)) {
if(wfstepcode.indexOf("tid-")!=0) {
wfstepcode = "tid-" + wfstepcode + "-";
query.taskDefinitionKeyLike(wfstepcode);
}
else
query.taskDefinitionKey(wfstepcode);
}
Long total=query.count();
if(total==0)
return wfStepCnt;
if(!StringUtils.isEmpty(wfstepcode)) {
List<Task> list=query.listPage(0,1);
if(list.size()>0)
wfStepCnt.put(list.get(0).getTaskDefinitionKey(),total);
return wfStepCnt;
}
Long sumstep=0L;
for(WFProcessNode node:this.getWFStep(system,appname,entity)){
if((!StringUtils.isEmpty(processDefinitionKey))&&(!node.getProcessdefinitionkey().equals(processDefinitionKey)))
continue;
Long stepcnt=taskService.createTaskQuery().taskCandidateOrAssigned(userId).processDefinitionKey(node.getProcessdefinitionkey()).taskDefinitionKey(node.getUsertaskid()).count();
if(stepcnt>0)
wfStepCnt.put(node.getUsertaskid(),stepcnt);
sumstep+=stepcnt;
if(sumstep>=total)
break;
}
return wfStepCnt;
}
public WFProcessInstance wfStart(String system,String appname,
String entity,String businessKey,WFProcessInstance instance) {
String userId=AuthenticationUser.getAuthenticationUser().getUserid();
if(StringUtils.isEmpty(userId))
throw new BadRequestAlertException("未传入当前用户",entity,businessKey);
String processDefinitionKey=instance.getProcessdefinitionkey();
if(StringUtils.isEmpty(processDefinitionKey)){
List<String> definitionKeys=this.getWorkflowKey(system,appname,entity);
if(definitionKeys.size()==0)
throw new BadRequestAlertException("未找到对应的工作流配置",entity,businessKey);
processDefinitionKey=definitionKeys.get(0);
}
String processInstanceBusinessKey=system+":"+entity+":srfkey-"+businessKey;
Map<String, Object> variables = new LinkedHashMap<>();
variables.put("businessKey",businessKey);
variables.put("activedata",instance.get("activedata"));
variables.put("curuser",AuthenticationUser.getAuthenticationUser());
variables.put("cloud-serviceid",system+"-app-"+appname);
variables.put("entitys",entity);
//根据流程定义启动流程
Authentication.setAuthenticatedUserId(userId);
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey,processInstanceBusinessKey, variables);//流程定时标识、业务标识、变量
instance.setBusinesskey(processInstanceBusinessKey);
instance.setId(processInstance.getId());
instance.setName(processInstance.getName());
instance.setProcessdefinitionkey(processInstance.getProcessDefinitionKey());
instance.setProcessdefinitionname(processInstance.getProcessDefinitionName());
if(processInstance.getStartTime()!=null)
instance.setStarttime(new Timestamp(processInstance.getStartTime().getTime()));
instance.setStartuserid(processInstance.getStartUserId());
return instance;
}
public List<WFTaskWay> getWFLink(String system,String appname,
String entity, String businessKey,String taskDefinitionKey) {
List<WFTaskWay> taskWays=new ArrayList<>();
String processInstanceBusinessKey=system+":"+entity+":srfkey-"+businessKey;
String userId=AuthenticationUser.getAuthenticationUser().getUserid();
if(StringUtils.isEmpty(userId))
return taskWays;
TaskQuery query=taskService.createTaskQuery().taskCandidateOrAssigned(userId).processInstanceBusinessKey(processInstanceBusinessKey);
if(!StringUtils.isEmpty(taskDefinitionKey))
query.taskDefinitionKey(taskDefinitionKey);
List<Task> list=query.orderByTaskCreateTime().desc().listPage(0,1);
if(list.size()==0)
return taskWays;
Task task=list.get(0);
if((!StringUtils.isEmpty(task.getProcessDefinitionId()))&&(!StringUtils.isEmpty(task.getTaskDefinitionKey()))) {
UserTask userTask = wfModelService.getModelStepById(task.getProcessDefinitionId()).get(task.getTaskDefinitionKey());
if(userTask!=null&&userTask.getOutgoingFlows()!=null) {
for(SequenceFlow sequenceFlow:userTask.getOutgoingFlows()) {
WFTaskWay way=new WFTaskWay();
way.setSequenceflowid(sequenceFlow.getId());
way.setSequenceflowname(sequenceFlow.getName());
way.setProcessdefinitionkey(task.getProcessDefinitionId().split(":")[0]);
way.setTaskid(task.getId());
way.setProcessinstanceid(task.getProcessInstanceId());
way.setTaskprocessdefinitionkey(task.getTaskDefinitionKey());
way.setProcessinstancebusinesskey(processInstanceBusinessKey);
taskWays.add(way);
}
}
}
return taskWays;
}
public List<WFTaskWay> getTaskLink(String system,String appname, String entity, String businessKey,String taskId) {
List<WFTaskWay> taskWays=new ArrayList<>();
String processInstanceBusinessKey=system+":"+entity+":srfkey-"+businessKey;
String userId=AuthenticationUser.getAuthenticationUser().getUserid();
if(StringUtils.isEmpty(userId))
return taskWays;
TaskQuery query=taskService.createTaskQuery().taskCandidateOrAssigned(userId).taskId(taskId);
List<Task> list=query.orderByTaskCreateTime().desc().listPage(0,1);
if(list.size()==0)
return taskWays;
Task task=list.get(0);
if((!StringUtils.isEmpty(task.getProcessDefinitionId()))&&(!StringUtils.isEmpty(task.getTaskDefinitionKey()))) {
UserTask userTask = wfModelService.getModelStepById(task.getProcessDefinitionId()).get(task.getTaskDefinitionKey());
if(userTask!=null&&userTask.getOutgoingFlows()!=null) {
for(SequenceFlow sequenceFlow:userTask.getOutgoingFlows()) {
WFTaskWay way=new WFTaskWay();
way.setSequenceflowid(sequenceFlow.getId());
way.setSequenceflowname(sequenceFlow.getName());
way.setProcessdefinitionkey(task.getProcessDefinitionId().split(":")[0]);
way.setTaskid(task.getId());
way.setProcessinstanceid(task.getProcessInstanceId());
way.setTaskprocessdefinitionkey(task.getTaskDefinitionKey());
way.setProcessinstancebusinesskey(processInstanceBusinessKey);
taskWays.add(way);
}
}
}
return taskWays;
}
public List<String> getBusinessKeys(String system,String appname, String entity,String processDefinitionKey,String taskDefinitionKey)
{
List<String> businessKeys = new ArrayList<>();
String processInstanceBusinessKeyLike=system+":"+entity+":srfkey-";
String userId=AuthenticationUser.getAuthenticationUser().getUserid();
if(StringUtils.isEmpty(userId))
return businessKeys;
TaskQuery query=taskService.createTaskQuery().taskCandidateOrAssigned(userId);
if(!StringUtils.isEmpty(processDefinitionKey))
query.processDefinitionKey(processDefinitionKey);
else {
query.processDefinitionKeyIn(this.getWorkflowKey(system,appname,entity));
}
if(!StringUtils.isEmpty(taskDefinitionKey))
query.taskDefinitionKey(taskDefinitionKey);
List<Task> tasks=query.includeProcessVariables().listPage(0,500);
for(Task task:tasks) {
Object key=task.getProcessVariables().get("businessKey");
if(key!=null) {
String str=key.toString();
if(str.indexOf(":srfkey-")>0)
str=str.split(":srfkey-")[1];
businessKeys.add(str);
}
}
return businessKeys;
}
public WFProcessInstance wfsubmit(String system,String appname,
String entity,String businessKey,String taskId,WFTaskWay taskWay) {
WFProcessInstance instance = new WFProcessInstance();
return instance;
}
public void execute(DelegateExecution delegateExecution,Object activedata) throws Exception {
String entity = (String)delegateExecution.getVariable("pass");
// Boolean val = (Boolean) pass.getValue(delegateExecution);
//logger.info("=ProductFactoryNewProductTask=>business:{} prossId:{} pass:{}",delegateExecution.getProcessInstanceBusinessKey(),delegateExecution.getProcessInstanceId(),val.toString());
// //修改产品状态为审核中
// ProductDto pactDto = new ProductDto();
// pactDto.setId(Long.valueOf(delegateExecution.getBusinessKey()));
// pactDto.setStatus(ProductStatusEnum.product_status__publishing.getStatus());
// ResponseResult<Integer> responseResult = productApi.update(pactDto);
//
// logger.info("ProductFactoryNewProductTask end!");
}
public Timestamp getnow()
{
return new Timestamp(new java.util.Date().getTime());
}
}
package cn.ibizlab.core.extensions.service;
import cn.ibizlab.core.workflow.domain.WFProcessDefinition;
import cn.ibizlab.core.workflow.domain.WFProcessNode;
import cn.ibizlab.core.workflow.service.IWFProcessDefinitionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import java.util.*;
public class WFModelService
{
@Autowired
private RepositoryService repositoryService;
@Autowired
private IWFProcessDefinitionService iwfProcessDefinitionService;
public List<WFProcessDefinition> getWorkflow(String system,String entity) {
return iwfProcessDefinitionService.list(new QueryWrapper<WFProcessDefinition>().
likeRight("definitionkey",system+"-"+entity+"-").eq("modelenable",1).orderByDesc("modelversion"));
}
public LinkedHashMap<String,UserTask> getModelStepByKey(String definitionkey)
{
LinkedHashMap<String, UserTask> allTask=new LinkedHashMap<String,UserTask>();
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().processDefinitionKey(definitionkey).orderByProcessDefinitionVersion().desc().list();
boolean blastest=true;
for (ProcessDefinition def : list) {
LinkedHashMap<String, UserTask> userTasks=getModelStepById(def.getId());
if(blastest){
allTask.putAll(userTasks);
blastest=false;
}
else {
for(String key:userTasks.keySet()) {
if(!allTask.containsKey(key)) {
String taskName = userTasks.get(key).getName() + "-历史版本v" +def.getVersion();
userTasks.get(key).setName(taskName);
allTask.put(key, userTasks.get(key));
}
}
}
}
return allTask;
}
public LinkedHashMap<String,UserTask> getModelStepById(String definitionid)
{
LinkedHashMap<String, UserTask> userTasks = new LinkedHashMap<String, UserTask>();
Map<String,UserTask> map=new HashMap<>();
for(FlowElement f:repositoryService.getBpmnModel(definitionid).getMainProcess().getFlowElements()) {
if(f instanceof UserTask) {
map.put(f.getId(),(UserTask)f);
}
}
List<Map.Entry<String, UserTask>> infoIds =new ArrayList<Map.Entry<String, UserTask>>(map.entrySet());
Collections.sort(infoIds, new Comparator<Map.Entry<String, UserTask>>() {
public int compare(Map.Entry<String, UserTask> o1, Map.Entry<String, UserTask> o2) {
String p1 = o1.getKey();
String p2 = o2.getKey();
return p1.compareTo(p2);
}
});
for(Map.Entry<String,UserTask> entity : infoIds){
userTasks.put(entity.getKey(), entity.getValue());
System.out.println(entity.getKey());
}
return userTasks;
}
}
package cn.ibizlab.api.rest.extensions;
import cn.ibizlab.core.workflow.domain.*;
import cn.ibizlab.core.extensions.service.WFCoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@Api(tags = {"wfcore" })
@RestController("api-wfcore")
@RequestMapping("")
public class WFCoreResource
{
@Autowired
private WFCoreService wfCoreService;
@ApiOperation(value = "getWFProcessDefinition", tags = {"WFProcessDefinition" }, notes = "根据系统实体查找当前适配的工作流模型")
@RequestMapping(method = RequestMethod.GET, value = "/wfcore/{system}-app-{appname}/{entity}/process-definitions")
public ResponseEntity<List<WFProcessDefinition>> getworkflow(@PathVariable("system") String system,@PathVariable("appname") String appname,
@PathVariable("entity") String entity) {
return ResponseEntity.status(HttpStatus.OK).body(null);
}
@ApiOperation(value = "getStepByEntity", tags = {"WFProcessNode" }, notes = "根据系统实体查找当前适配的工作流模型步骤")
@RequestMapping(method = RequestMethod.GET, value = "/wfcore/{system}-app-{appname}/{entity}/process-definitions-nodes")
public ResponseEntity<List<WFProcessNode>> getwfstep(@PathVariable("system") String system,@PathVariable("appname") String appname,
@PathVariable("entity") String entity) {
return ResponseEntity.status(HttpStatus.OK).body(null);
}
@PreAuthorize("hasPermission(#entity,'WFSTART',this.getEntity())")
@ApiOperation(value = "wfstart", tags = {"WFProcessInstance" }, notes = "启动工作流")
@RequestMapping(method = RequestMethod.POST, value = "/wfcore/{system}-app-{appname}/{entity}/{businessKey}/process-instances")
public ResponseEntity<WFProcessInstance> wfstart(@PathVariable("system") String system,@PathVariable("appname") String appname,
@PathVariable("entity") String entity,
@PathVariable("businessKey") String businessKey,@RequestBody WFProcessInstance instance) {
instance.setBusinesskey(businessKey);
return ResponseEntity.ok(instance);
}
@ApiOperation(value = "getWayByBusinessKey", tags = {"WFTaskWay" }, notes = "根据业务主键和当前步骤获取操作路径")
@RequestMapping(method = RequestMethod.GET, value = "/wfcore/{system}-app-{appname}/{entity}/{businessKey}/usertasks/{taskDefinitionKey}/ways")
public ResponseEntity<List<WFTaskWay>> getwflink(@PathVariable("system") String system,@PathVariable("appname") String appname,
@PathVariable("entity") String entity,
@PathVariable("businessKey") String businessKey,@PathVariable("taskDefinitionKey") String taskDefinitionKey) {
return ResponseEntity.status(HttpStatus.OK).body(null);
}
@ApiOperation(value = "getWayByTaskId", tags = {"WFTaskWay" }, notes = "根据业务主键和当前步骤获取操作路径")
@RequestMapping(method = RequestMethod.GET, value = "/wfcore/{system}-app-{appname}/{entity}/{businessKey}/tasks/{taskId}/ways")
public ResponseEntity<List<WFTaskWay>> gettasklink(@PathVariable("system") String system,@PathVariable("appname") String appname,
@PathVariable("entity") String entity,
@PathVariable("businessKey") String businessKey,@PathVariable("taskId") String taskId) {
return ResponseEntity.status(HttpStatus.OK).body(null);
}
@PreAuthorize("hasPermission(#entity,'WFSTART',this.getEntity())")
@ApiOperation(value = "wfsubmit", tags = {"WFProcessInstance" }, notes = "工作流执行步骤")
@RequestMapping(method = RequestMethod.POST, value = "/wfcore/{system}-app-{appname}/{entity}/{businessKey}/tasks/{taskId}")
public ResponseEntity<WFProcessInstance> wfsubmit(@PathVariable("system") String system,@PathVariable("appname") String appname,
@PathVariable("entity") String entity,
@PathVariable("businessKey") String businessKey,@PathVariable("taskId") String taskId,
@RequestBody WFTaskWay taskWay) {
WFProcessInstance instance = new WFProcessInstance();
return ResponseEntity.ok(instance);
}
}
......@@ -54,5 +54,19 @@
<artifactId>problem-spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-json-converter</artifactId>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-image-generator</artifactId>
</dependency>
</dependencies>
</project>
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册