parent
4f2a3dd8d1
commit
f4af67b116
@ -1,198 +0,0 @@ |
||||
package com.zilber.boot.activiti.controller; |
||||
|
||||
|
||||
import com.github.pagehelper.PageInfo; |
||||
import com.zilber.boot.activiti.dto.*; |
||||
import com.zilber.boot.activiti.service.*; |
||||
import com.zilber.boot.activiti.vo.DefinitionVO; |
||||
import com.zilber.boot.activiti.vo.HistoricTaskVO; |
||||
import com.zilber.boot.activiti.vo.InstanceVO; |
||||
import com.zilber.boot.activiti.vo.TaskVO; |
||||
import com.zilber.boot.utils.AjaxResult; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import io.swagger.annotations.ApiParam; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.io.IOUtils; |
||||
import org.springframework.validation.annotation.Validated; |
||||
import org.springframework.web.bind.annotation.*; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
|
||||
import javax.annotation.Resource; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import javax.validation.Valid; |
||||
import javax.validation.constraints.NotBlank; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.io.OutputStream; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
|
||||
@Validated |
||||
@RestController |
||||
@RequestMapping("/bpmn") |
||||
@Api(tags = "工作流接口") |
||||
@Slf4j |
||||
public class BpmnController { |
||||
|
||||
@Resource |
||||
private DefinitionService definitionService; |
||||
|
||||
@Resource |
||||
private InstanceService instanceService; |
||||
|
||||
@Resource |
||||
private TaskService taskService; |
||||
|
||||
@Resource |
||||
private HistoryService historyService; |
||||
|
||||
@PostMapping("/definition/upload") |
||||
@ApiOperation("上传流程图") |
||||
public AjaxResult uploadAndDeployment( |
||||
@RequestParam @ApiParam("名称") String name, |
||||
@RequestParam @ApiParam("上传的文件") MultipartFile file) { |
||||
definitionService.uploadAndDeployment(name, file); |
||||
return AjaxResult.success(); |
||||
} |
||||
|
||||
@PostMapping("/definition") |
||||
@ApiOperation("保存流程图") |
||||
public AjaxResult saveAndDeployment(@RequestBody @Valid DeploymentDTO dto) { |
||||
definitionService.saveAndDeployment(dto.getName(), dto.getXml()); |
||||
return AjaxResult.success(); |
||||
} |
||||
|
||||
@GetMapping("/definition/page") |
||||
@ApiOperation("查询流程定义列表") |
||||
public AjaxResult getDefinitionPage(@Valid PageQueryDTO dto) { |
||||
PageInfo<DefinitionVO> page = new PageInfo<>(definitionService.getDefinitionPage(dto)); |
||||
return AjaxResult.success(page); |
||||
} |
||||
|
||||
@GetMapping("/definition/list/by/{key}") |
||||
@ApiOperation("查询指定Key的流程定义所有列表") |
||||
public AjaxResult getVersionList( |
||||
@PathVariable @NotBlank String key) { |
||||
return AjaxResult.success(definitionService.getVersionList(key)); |
||||
} |
||||
|
||||
@GetMapping("/definition/by/key/{key}") |
||||
@ApiOperation("查询流程图,XML字符串格式") |
||||
public AjaxResult getDeploymentXMLByKey( |
||||
@PathVariable @ApiParam("流程定义Key") @NotBlank String key) { |
||||
String xmlString = definitionService.getDeploymentXML(key); |
||||
HashMap<String, String> map = new HashMap<>(); |
||||
map.put("xml", xmlString); |
||||
return AjaxResult.success(map); |
||||
} |
||||
|
||||
@GetMapping("/definition/by/{id}") |
||||
@ApiOperation("查询流程图,XML字符串格式") |
||||
public AjaxResult getDeploymentXML( |
||||
@PathVariable @ApiParam("流程部署ID") @NotBlank String id, |
||||
@RequestParam @ApiParam("资源文件名") @NotBlank String name) { |
||||
String xmlString = definitionService.getDeploymentXML(id, name); |
||||
HashMap<String, String> map = new HashMap<>(); |
||||
map.put("xml", xmlString); |
||||
return AjaxResult.success(map); |
||||
} |
||||
|
||||
@GetMapping("/definition/file/{id}") |
||||
@ApiOperation("查询流程图,XML文件格式") |
||||
public void getDeploymentFile( |
||||
HttpServletResponse response, |
||||
@PathVariable @ApiParam("流程部署ID") @NotBlank String id, |
||||
@RequestParam @ApiParam("资源文件名") @NotBlank String name) { |
||||
InputStream is = definitionService.getDeploymentStream(id, name); |
||||
OutputStream os; |
||||
try { |
||||
os = response.getOutputStream(); |
||||
IOUtils.copy(is, os); |
||||
|
||||
is.close(); |
||||
os.close(); |
||||
} catch (IOException e) { |
||||
log.error("查看流程文件失败", e); |
||||
} |
||||
} |
||||
|
||||
@DeleteMapping("/definition/by/{id}") |
||||
@ApiOperation("删除流程定义") |
||||
public AjaxResult deleteDefinitionById( |
||||
@PathVariable @ApiParam("流程部署ID") @NotBlank String id) { |
||||
definitionService.deleteById(id); |
||||
return AjaxResult.success(); |
||||
} |
||||
|
||||
@GetMapping("/instance/page") |
||||
@ApiOperation("查询流程实例列表") |
||||
public AjaxResult getInstancePage(@Valid PageQueryDTO dto) { |
||||
PageInfo<InstanceVO> page = new PageInfo<>(instanceService.getInstancePage(dto)); |
||||
return AjaxResult.success(page); |
||||
} |
||||
|
||||
@PostMapping("/instance") |
||||
@ApiOperation("启动流程实例") |
||||
public AjaxResult startInstance(@RequestBody @Valid InstanceDTO dto) { |
||||
instanceService.start(dto); |
||||
return AjaxResult.success(); |
||||
} |
||||
|
||||
@PutMapping("/instance/suspend/by/{id}") |
||||
@ApiOperation("挂起流程实例") |
||||
public AjaxResult suspendInstance(@PathVariable @ApiParam("流程实例ID") @NotBlank String id) { |
||||
instanceService.suspend(id); |
||||
return AjaxResult.success(); |
||||
} |
||||
|
||||
@PutMapping("/instance/resume/by/{id}") |
||||
@ApiOperation("激活/重启流程实例") |
||||
public AjaxResult resumeInstance(@PathVariable @ApiParam("流程实例ID") @NotBlank String id) { |
||||
instanceService.resume(id); |
||||
return AjaxResult.success(); |
||||
} |
||||
|
||||
@DeleteMapping("/instance/by/{id}") |
||||
@ApiOperation("取消流程实例") |
||||
public AjaxResult deleteInstanceById(@PathVariable @ApiParam("流程实例ID") @NotBlank String id) { |
||||
instanceService.cancelById(id); |
||||
return AjaxResult.success(); |
||||
} |
||||
|
||||
@GetMapping("/instance/variables/by/{id}") |
||||
@ApiOperation("查询流程参数") |
||||
public AjaxResult instanceVariables(@PathVariable @ApiParam("流程实例ID") @NotBlank String id) { |
||||
return AjaxResult.success(instanceService.getVariables(id)); |
||||
} |
||||
|
||||
@GetMapping("/task/page") |
||||
@ApiOperation("查看待办任务") |
||||
public AjaxResult taskPage(@Valid TaskQueryDTO dto) { |
||||
PageInfo<TaskVO> page = new PageInfo<>(taskService.taskPage(dto)); |
||||
return AjaxResult.success(page); |
||||
} |
||||
|
||||
@PutMapping("/task/{id}") |
||||
@ApiOperation("完成任务") |
||||
public AjaxResult completeTask( |
||||
@PathVariable @NotBlank @ApiParam("流程任务ID") String id, |
||||
@RequestBody @Valid TaskCompleteDTO dto) { |
||||
taskService.complete(dto.getUserCode(), id, dto); |
||||
return AjaxResult.success(); |
||||
} |
||||
|
||||
@GetMapping("/historic/task/page") |
||||
@ApiOperation("查看历史任务") |
||||
public AjaxResult historicTaskPage(@Valid TaskQueryDTO dto) { |
||||
PageInfo<HistoricTaskVO> page = new PageInfo<>(historyService.historicTaskPage(dto)); |
||||
return AjaxResult.success(page); |
||||
} |
||||
|
||||
@GetMapping("/historic/task/list") |
||||
@ApiOperation("任务实例历史") |
||||
public List<HistoricTaskVO> getHistoryListByInstanceId( |
||||
@RequestParam @ApiParam("流程实例ID") @NotBlank String instanceId) { |
||||
return historyService.getListOfInstance(instanceId); |
||||
} |
||||
} |
@ -1,23 +0,0 @@ |
||||
package com.zilber.boot.activiti.dto; |
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
import javax.validation.constraints.NotBlank; |
||||
|
||||
@Getter |
||||
@Setter |
||||
@ApiModel("流程部署提交") |
||||
public class DeploymentDTO { |
||||
|
||||
@NotBlank(message = "名称不能为空") |
||||
@ApiModelProperty(value = "名称", required = true) |
||||
private String name; |
||||
|
||||
@NotBlank(message = "bpmn文件xml不能为空") |
||||
@ApiModelProperty(value = "bpmn文件xml", required = true) |
||||
private String xml; |
||||
|
||||
} |
@ -1,33 +0,0 @@ |
||||
package com.zilber.boot.activiti.dto; |
||||
|
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.*; |
||||
import org.hibernate.validator.constraints.Length; |
||||
|
||||
import javax.validation.constraints.NotBlank; |
||||
import javax.validation.constraints.NotNull; |
||||
import java.util.Map; |
||||
|
||||
@Getter |
||||
@Setter |
||||
@AllArgsConstructor |
||||
@NoArgsConstructor |
||||
@Builder |
||||
@ApiModel("启动流程实例") |
||||
public class InstanceDTO { |
||||
|
||||
@NotBlank |
||||
@ApiModelProperty(value = "流程定义Key") |
||||
@Length(max = 32) |
||||
private String procDefKey; |
||||
|
||||
@Length(max = 24) |
||||
@ApiModelProperty(value = "发起人") |
||||
private String createBy; |
||||
|
||||
@ApiModelProperty(value = "流程变量") |
||||
private Map<String,Object> variables; |
||||
|
||||
} |
@ -1,23 +0,0 @@ |
||||
package com.zilber.boot.activiti.dto; |
||||
|
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
import javax.validation.constraints.Max; |
||||
import javax.validation.constraints.Positive; |
||||
import javax.validation.constraints.PositiveOrZero; |
||||
import java.io.Serializable; |
||||
|
||||
@Getter |
||||
@Setter |
||||
public class PageQueryDTO implements Serializable { |
||||
|
||||
@ApiModelProperty("每页记录数") |
||||
@Positive(message = "分页数量必须为正整数") |
||||
private Integer pageSize = 20; |
||||
|
||||
@ApiModelProperty("当前页数") |
||||
@PositiveOrZero(message = "分页数不正确") |
||||
private Integer pageNo = 0; |
||||
} |
@ -1,30 +0,0 @@ |
||||
package com.zilber.boot.activiti.dto; |
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
import org.hibernate.validator.constraints.Length; |
||||
|
||||
import javax.validation.constraints.NotNull; |
||||
import java.util.Map; |
||||
|
||||
@Getter |
||||
@Setter |
||||
@ApiModel("完成流程任务") |
||||
public class TaskCompleteDTO { |
||||
|
||||
@NotNull(message = "请选择审批结果") |
||||
@ApiModelProperty("审批状态:1-通过 0-拒绝") |
||||
private Integer status; |
||||
|
||||
@Length(max = 512) |
||||
@ApiModelProperty("意见") |
||||
private String remark; |
||||
|
||||
@ApiModelProperty(value = "流程变量") |
||||
private Map<String, Object> variables; |
||||
|
||||
@ApiModelProperty("用户") |
||||
private String userCode; |
||||
} |
@ -1,22 +0,0 @@ |
||||
package com.zilber.boot.activiti.dto; |
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
@Getter |
||||
@Setter |
||||
@ApiModel("任务查询") |
||||
public class TaskQueryDTO extends PageQueryDTO { |
||||
|
||||
@ApiModelProperty("流程定义Key") |
||||
private String procDefKey; |
||||
|
||||
@ApiModelProperty("任务执行人") |
||||
private String assignee; |
||||
|
||||
@ApiModelProperty("流程实例ID") |
||||
private String procInstId; |
||||
|
||||
} |
@ -1,17 +0,0 @@ |
||||
package com.zilber.boot.activiti.listen; |
||||
|
||||
import org.activiti.engine.delegate.DelegateExecution; |
||||
import org.activiti.engine.delegate.ExecutionListener; |
||||
|
||||
public class ApplyListener implements ExecutionListener { |
||||
|
||||
@Override |
||||
public void notify(DelegateExecution execution) { |
||||
String eventName = execution.getEventName(); |
||||
if ("start".endsWith(eventName)) { |
||||
System.out.println("流程 start==="); |
||||
} else if ("end".endsWith(eventName)) { |
||||
System.out.println("流程 end==="); |
||||
} |
||||
} |
||||
} |
@ -1,23 +0,0 @@ |
||||
package com.zilber.boot.activiti.listen; |
||||
|
||||
import org.activiti.engine.delegate.DelegateTask; |
||||
import org.activiti.engine.delegate.TaskListener; |
||||
|
||||
public class ApplyTaskListener implements TaskListener { |
||||
|
||||
@Override |
||||
public void notify(DelegateTask delegateTask) { |
||||
System.out.println("===========进来了=============="); |
||||
String eventName = delegateTask.getEventName(); |
||||
if ("create".endsWith(eventName)) { |
||||
System.out.println("任务create===" + delegateTask.getName()); |
||||
} else if ("complete".endsWith(eventName)) { |
||||
System.out.println("任务task complete===" + delegateTask.getName()); |
||||
} else if ("delete".endsWith(eventName)) { |
||||
System.out.println("任务task delete===" + delegateTask.getName()); |
||||
} else if ("assignment".endsWith(eventName)) { |
||||
System.out.println("任务task assignment===" + delegateTask.getName()); |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,164 +0,0 @@ |
||||
package com.zilber.boot.activiti.service; |
||||
|
||||
import com.zilber.boot.activiti.dto.PageQueryDTO; |
||||
import com.zilber.boot.activiti.vo.DefinitionVO; |
||||
import com.zilber.boot.exception.ServiceException; |
||||
import org.activiti.engine.RepositoryService; |
||||
import org.activiti.engine.repository.ProcessDefinition; |
||||
import org.apache.commons.io.FilenameUtils; |
||||
import org.apache.commons.io.IOUtils; |
||||
|
||||
import org.springframework.beans.BeanUtils; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.io.StringWriter; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
import java.util.zip.ZipInputStream; |
||||
|
||||
@Service |
||||
public class DefinitionService { |
||||
|
||||
@Autowired |
||||
private RepositoryService repositoryService; |
||||
|
||||
/** |
||||
* 上传并部署流程定义 |
||||
* |
||||
* @param name 流程定义名称 |
||||
* @param file 上传的文件 |
||||
*/ |
||||
public void uploadAndDeployment(String name, MultipartFile file) { |
||||
try { |
||||
String filename = file.getOriginalFilename(); |
||||
String extension = FilenameUtils.getExtension(filename); |
||||
InputStream fileInputStream = file.getInputStream(); |
||||
if ("zip".equals(extension)) { |
||||
ZipInputStream zip = new ZipInputStream(fileInputStream); |
||||
repositoryService.createDeployment() |
||||
.addZipInputStream(zip) |
||||
.name(name) |
||||
.deploy(); |
||||
} else { |
||||
repositoryService.createDeployment() |
||||
.addInputStream(filename, fileInputStream) |
||||
.name(name) |
||||
.deploy(); |
||||
} |
||||
} catch (Exception e) { |
||||
throw new ServiceException(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
public List<DefinitionVO> getDefinitionPage(PageQueryDTO dto) { |
||||
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery() |
||||
.latestVersion().listPage(dto.getPageNo() * dto.getPageSize(), dto.getPageSize()); |
||||
list.sort((y, x) -> x.getVersion() - y.getVersion()); |
||||
|
||||
return list.stream().map(processDefinition -> { |
||||
DefinitionVO vo = new DefinitionVO(); |
||||
BeanUtils.copyProperties(processDefinition, vo); |
||||
return vo; |
||||
}).collect(Collectors.toList()); |
||||
} |
||||
|
||||
/** |
||||
* 根据流程定义Key获取历史版本 |
||||
* |
||||
* @param key 流程定义Key |
||||
* @return 历史版本列表 |
||||
*/ |
||||
public List<DefinitionVO> getVersionList(String key) { |
||||
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery() |
||||
.processDefinitionKey(key) |
||||
.orderByProcessDefinitionVersion().desc() |
||||
.list(); |
||||
return list.stream().map(processDefinition -> { |
||||
DefinitionVO vo = new DefinitionVO(); |
||||
BeanUtils.copyProperties(processDefinition, vo); |
||||
return vo; |
||||
}).collect(Collectors.toList()); |
||||
} |
||||
|
||||
/** |
||||
* 在线绘制流程图保存 |
||||
* |
||||
* @param name 流程名称 |
||||
* @param xml xml数据 |
||||
*/ |
||||
public void saveAndDeployment(String name, String xml) { |
||||
repositoryService.createDeployment() |
||||
.addString("bpmnjs.bpmn", xml) |
||||
.name(name) |
||||
.deploy(); |
||||
} |
||||
|
||||
/** |
||||
* 获取流程图(流) |
||||
* |
||||
* @param deploymentId 流程部署ID |
||||
* @param resourceName 资源文件名称 |
||||
* @return 数据流 |
||||
*/ |
||||
public InputStream getDeploymentStream(String deploymentId, String resourceName) { |
||||
return repositoryService.getResourceAsStream(deploymentId, resourceName); |
||||
} |
||||
|
||||
/** |
||||
* 获取流程图xml |
||||
* |
||||
* @param definitionKey 流程定义Key |
||||
* @return xml字符串 |
||||
*/ |
||||
public String getDeploymentXML(String definitionKey) { |
||||
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery() |
||||
.processDefinitionKey(definitionKey) |
||||
.latestVersion() |
||||
.singleResult(); |
||||
return getDeploymentXML(processDefinition.getDeploymentId(), processDefinition.getResourceName()); |
||||
} |
||||
|
||||
/** |
||||
* 获取流程图xml |
||||
* |
||||
* @param deploymentId 流程部署ID |
||||
* @param resourceName 资源文件名称 |
||||
* @return xml字符串 |
||||
*/ |
||||
public String getDeploymentXML(String deploymentId, String resourceName) { |
||||
try { |
||||
InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, resourceName); |
||||
StringWriter writer = new StringWriter(); |
||||
IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8.name()); |
||||
return writer.toString(); |
||||
} catch (IOException e) { |
||||
throw new ServiceException(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 获取流程定义 |
||||
* |
||||
* @param processDefinitionId 流程定义ID |
||||
* @return |
||||
*/ |
||||
public ProcessDefinition getById(String processDefinitionId) { |
||||
return repositoryService.createProcessDefinitionQuery() |
||||
.processDefinitionId(processDefinitionId) |
||||
.singleResult(); |
||||
} |
||||
|
||||
/** |
||||
* 根据流程定义id删除流程定义 |
||||
* |
||||
* @param id 流程定义id |
||||
*/ |
||||
public void deleteById(String id) { |
||||
repositoryService.deleteDeployment(id); |
||||
} |
||||
} |
@ -1,54 +0,0 @@ |
||||
package com.zilber.boot.activiti.service; |
||||
|
||||
|
||||
import com.zilber.boot.activiti.dto.TaskQueryDTO; |
||||
import com.zilber.boot.activiti.vo.HistoricTaskVO; |
||||
import org.activiti.engine.history.HistoricTaskInstance; |
||||
import org.activiti.engine.history.HistoricTaskInstanceQuery; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.List; |
||||
|
||||
import java.util.stream.Collectors; |
||||
|
||||
|
||||
@Service(value = "HistoryService_") |
||||
public class HistoryService { |
||||
|
||||
@Resource |
||||
private org.activiti.engine.HistoryService historyService; |
||||
|
||||
public List<HistoricTaskVO> historicTaskPage(TaskQueryDTO dto) { |
||||
HistoricTaskInstanceQuery taskInstanceQuery = historyService.createHistoricTaskInstanceQuery() |
||||
.finished() |
||||
.orderByHistoricTaskInstanceEndTime() |
||||
.desc(); |
||||
if (!StringUtils.isEmpty(dto.getAssignee())) { |
||||
taskInstanceQuery.taskAssignee(dto.getAssignee()); |
||||
} |
||||
if (!StringUtils.isEmpty(dto.getProcDefKey())) { |
||||
taskInstanceQuery.processDefinitionKey(dto.getProcDefKey()); |
||||
} |
||||
if (!StringUtils.isEmpty(dto.getProcInstId())) { |
||||
taskInstanceQuery.processInstanceId(dto.getProcInstId()); |
||||
} |
||||
List<HistoricTaskInstance> tasks = taskInstanceQuery.listPage(dto.getPageNo() * dto.getPageSize(), dto.getPageSize()); |
||||
return HistoricTaskVO.merge(tasks); |
||||
|
||||
} |
||||
|
||||
public List<HistoricTaskVO> getListOfInstance(String instanceId) { |
||||
List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery() |
||||
.orderByHistoricTaskInstanceStartTime() |
||||
.asc() |
||||
.processInstanceId(instanceId) |
||||
.list(); |
||||
return list.stream() |
||||
.map(HistoricTaskVO::merge) |
||||
.collect(Collectors.toList()); |
||||
|
||||
} |
||||
} |
@ -1,167 +0,0 @@ |
||||
package com.zilber.boot.activiti.service; |
||||
|
||||
|
||||
import com.zilber.boot.activiti.dto.InstanceDTO; |
||||
import com.zilber.boot.activiti.dto.PageQueryDTO; |
||||
import com.zilber.boot.activiti.vo.InstanceProgressVO; |
||||
import com.zilber.boot.activiti.vo.InstanceVO; |
||||
import org.activiti.engine.RuntimeService; |
||||
import org.activiti.engine.TaskService; |
||||
import org.activiti.engine.impl.persistence.entity.EntityManager; |
||||
import org.activiti.engine.runtime.ProcessInstance; |
||||
import org.activiti.engine.runtime.ProcessInstanceQuery; |
||||
import org.activiti.engine.task.Task; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
@Service |
||||
public class InstanceService { |
||||
|
||||
@Autowired |
||||
private RuntimeService runtimeService; |
||||
|
||||
@Autowired |
||||
private TaskService taskService; |
||||
|
||||
public ProcessInstance getById(String processInstanceId) { |
||||
return runtimeService.createProcessInstanceQuery() |
||||
.processInstanceId(processInstanceId) |
||||
.singleResult(); |
||||
} |
||||
|
||||
public List<InstanceVO> getInstancePage(PageQueryDTO dto) { |
||||
ProcessInstanceQuery query = runtimeService.createProcessInstanceQuery(); |
||||
return InstanceVO.merge(query.listPage(dto.getPageNo() * dto.getPageSize(), dto.getPageSize())); |
||||
} |
||||
|
||||
/** |
||||
* 启动流程实例并完成第一个任务 |
||||
* |
||||
* @param dto 启动流程实例必要参数 |
||||
*/ |
||||
public void start(InstanceDTO dto) { |
||||
Map<String, Object> variables = dto.getVariables(); |
||||
if (variables == null) { |
||||
variables = new HashMap<>(); |
||||
} |
||||
|
||||
// 设置默认的流程变量
|
||||
variables.put("assignee0", dto.getCreateBy()); |
||||
//variables.put("procDefKey", dto.getProcDefKey());
|
||||
|
||||
// 启动流程实例
|
||||
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey( |
||||
dto.getProcDefKey(), /*dto.getBusinessId() +*/ "", variables); |
||||
|
||||
// 执行第一个任务
|
||||
Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult(); |
||||
if (task != null) { |
||||
if (task.getAssignee() == null) { |
||||
taskService.claim(task.getId(), dto.getCreateBy()); |
||||
} |
||||
taskService.complete(task.getId()); |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 查询指定流程进度 |
||||
* |
||||
* @param instanceId 流程实例ID |
||||
* @return 进度列表 |
||||
*/ |
||||
public List<InstanceProgressVO> getInstanceProgress(String instanceId) { |
||||
String sql = "select\n" + |
||||
" t1.ACT_ID_ as actId,\n" + |
||||
" t1.TASK_ID_ as taskId,\n" + |
||||
" t1.ACT_NAME_ as actName,\n" + |
||||
" t1.ACT_TYPE_ as actType,\n" + |
||||
" t1.PROC_INST_ID_ as procInstId,\n" + |
||||
" t1.DURATION_ as duration,\n" + |
||||
" t1.ACT_INST_STATE_ as state,\n" + |
||||
" t1.START_TIME_ as startTime,\n" + |
||||
" t1.END_TIME_ as endTime,\n" + |
||||
" t1.ASSIGNEE_ as assignee,\n" + |
||||
" t2.MESSAGE_ as remark,\n" + |
||||
" t3.TEXT_ as operators\n" + |
||||
"from ACT_HI_ACTINST as t1\n" + |
||||
" left join ACT_HI_COMMENT as t2 on t1.TASK_ID_ = t2.TASK_ID_\n" + |
||||
" left join ACT_HI_VARINST as t3 on t1.ID_ = t3.ACT_INST_ID_ and t3.NAME_ = ?2\n" + |
||||
"where t1.PROC_INST_ID_ = ?1\n" + |
||||
" " + |
||||
"and t1.ACT_TYPE_ = 'userTask'\n" + |
||||
"order by t1.START_TIME_ desc, NOT ISNULL(t1.END_TIME_), t1" + |
||||
".END_TIME_ desc"; |
||||
|
||||
/*Query nativeQuery = entityManager.createNativeQuery(sql); |
||||
nativeQuery.setParameter(1, instanceId); |
||||
nativeQuery.setParameter(2, CamundaConstants.OPERATORS); |
||||
return nativeQuery |
||||
.unwrap(NativeQueryImpl.class) |
||||
.setResultTransformer(Transformers.aliasToBean(InstanceProgressVO.class)) |
||||
.list();*/ |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 挂起流程实例 |
||||
* |
||||
* @param id 流程实例id |
||||
*/ |
||||
public void suspend(String id) { |
||||
runtimeService.suspendProcessInstanceById(id); |
||||
} |
||||
|
||||
/** |
||||
* 激活流程实例 |
||||
* |
||||
* @param id 流程实例id |
||||
*/ |
||||
public void resume(String id) { |
||||
runtimeService.activateProcessInstanceById(id); |
||||
} |
||||
|
||||
/** |
||||
* 结束流程实例(审批失败) |
||||
* |
||||
* @param id 流程实例id |
||||
* @param reason 失败原因 |
||||
*/ |
||||
public void failEndById(String id, String reason) { |
||||
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery() |
||||
.processInstanceId(id).singleResult(); |
||||
if (processInstance != null) { |
||||
// 审批失败,状态为 INTERNALLY_TERMINATED
|
||||
runtimeService.deleteProcessInstance(processInstance.getId(), reason); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 取消流程实例 |
||||
* |
||||
* @param id 流程实例id |
||||
*/ |
||||
public void cancelById(String id) { |
||||
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery() |
||||
.processInstanceId(id).singleResult(); |
||||
if (processInstance != null) { |
||||
// 手动取消,状态为 EXTERNALLY_TERMINATED
|
||||
runtimeService.deleteProcessInstance(processInstance.getId(), "手动取消"); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 获取流程变量 |
||||
* |
||||
* @param id 流程实例id |
||||
* @return 流程变量map |
||||
*/ |
||||
public Map<String, Object> getVariables(String id) { |
||||
return runtimeService.getVariables(id); |
||||
} |
||||
|
||||
} |
@ -1,124 +0,0 @@ |
||||
package com.zilber.boot.activiti.service; |
||||
|
||||
import com.zilber.boot.activiti.dto.TaskCompleteDTO; |
||||
import com.zilber.boot.activiti.dto.TaskQueryDTO; |
||||
import com.zilber.boot.activiti.vo.TaskVO; |
||||
import com.zilber.boot.exception.ServiceException; |
||||
import org.activiti.engine.task.Task; |
||||
import org.activiti.engine.task.TaskQuery; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
@Service(value = "TaskService_") |
||||
public class TaskService { |
||||
|
||||
@Resource |
||||
private org.activiti.engine.TaskService taskService; |
||||
|
||||
@Resource |
||||
private InstanceService instanceService; |
||||
|
||||
public List<TaskVO> taskPage(TaskQueryDTO dto) { |
||||
TaskQuery taskQuery = taskService.createTaskQuery() |
||||
.active() |
||||
.orderByTaskCreateTime() |
||||
.desc(); |
||||
if (!StringUtils.isEmpty(dto.getAssignee())) { |
||||
taskQuery.taskAssignee(dto.getAssignee()); |
||||
} |
||||
if (!StringUtils.isEmpty(dto.getProcDefKey())) { |
||||
taskQuery.processDefinitionKey(dto.getProcDefKey()); |
||||
} |
||||
if (!StringUtils.isEmpty(dto.getProcInstId())) { |
||||
taskQuery.processInstanceId(dto.getProcInstId()); |
||||
} |
||||
List<Task> tasks = taskQuery.listPage(dto.getPageNo() * dto.getPageSize(), dto.getPageSize()); |
||||
return TaskVO.merge(tasks); |
||||
} |
||||
|
||||
public void complete(String userCode, String taskId, TaskCompleteDTO dto) { |
||||
Task task = taskService.createTaskQuery() |
||||
.taskAssignee(userCode) |
||||
.taskId(taskId) |
||||
.singleResult(); |
||||
|
||||
if (task == null) { |
||||
throw new ServiceException("没有任务"); |
||||
} |
||||
|
||||
if (task.getAssignee() == null) { |
||||
taskService.claim(taskId, userCode); |
||||
} |
||||
if (!StringUtils.isEmpty(dto.getRemark())) { |
||||
//taskService.createComment(taskId, task.getProcessInstanceId(), dto.getRemark());
|
||||
taskService.addComment(taskId, task.getProcessInstanceId(), dto.getRemark()); |
||||
} |
||||
Map<String, Object> variables = dto.getVariables(); |
||||
if (dto.getVariables() == null) { |
||||
variables = new HashMap<>(); |
||||
} |
||||
if (dto.getStatus() == 0) { |
||||
// TODO: 多实例并行任务,需要按照业务处理,是否一个人审批不通过,就都不通过
|
||||
taskService.setVariable(taskId, "status", 0); |
||||
instanceService.failEndById(task.getProcessInstanceId(), dto.getRemark()); |
||||
} else { |
||||
variables.put("status", dto.getStatus()); |
||||
taskService.complete(taskId, variables); |
||||
} |
||||
} |
||||
|
||||
/*public List<io.izn.iec.camunda.vo.SimpleTaskVO> getSimpleTasks(List<Integer> businessIds, DefinitionKey definitionKey) { |
||||
String sql = "SELECT\n" + |
||||
" t2.ID_ as taskId,\n" + |
||||
" t2.NAME_ as taskName,\n" + |
||||
" t2.DESCRIPTION_ as taskDescription,\n" + |
||||
" t2.TASK_DEF_KEY_ as taskDefinitionKey,\n" + |
||||
" t2.ASSIGNEE_ as assignee,\n" + |
||||
" t2.CREATE_TIME_ as taskTime,\n" + |
||||
" t2.PRIORITY_ as priority,\n" + |
||||
" t2.SUSPENSION_STATE_ as suspensionState,\n" + |
||||
" t1.business_id as businessId\n" + |
||||
"FROM bpm_business as t1\n" + |
||||
" LEFT JOIN ACT_RU_TASK as t2 on t1.proc_inst_id = t2.PROC_INST_ID_\n" + |
||||
"WHERE t1.business_id in (?1) and t1.proc_def_key = ?2"; |
||||
|
||||
Query nativeQuery = entityManager.createNativeQuery(sql); |
||||
nativeQuery.setParameter(1, businessIds); |
||||
nativeQuery.setParameter(2, definitionKey.getValue()); |
||||
return nativeQuery |
||||
.unwrap(NativeQueryImpl.class) |
||||
.setResultTransformer(Transformers.aliasToBean(io.izn.iec.camunda.vo.SimpleTaskVO.class)) |
||||
.list(); |
||||
} |
||||
|
||||
public List<io.izn.iec.camunda.vo.SimpleTaskVO> getSimpleTasks(List<Integer> businessIds, List<DefinitionKey> definitionKey) { |
||||
List<String> collect = definitionKey.stream().map(DefinitionKey::getValue).collect(Collectors.toList()); |
||||
String sql = "SELECT\n" + |
||||
" t2.ID_ as taskId,\n" + |
||||
" t2.NAME_ as taskName,\n" + |
||||
" t2.DESCRIPTION_ as taskDescription,\n" + |
||||
" t2.TASK_DEF_KEY_ as taskDefinitionKey,\n" + |
||||
" t2.ASSIGNEE_ as assignee,\n" + |
||||
" t2.CREATE_TIME_ as taskTime,\n" + |
||||
" t2.PRIORITY_ as priority,\n" + |
||||
" t2.SUSPENSION_STATE_ as suspensionState,\n" + |
||||
" t1.business_id as businessId\n" + |
||||
"FROM bpm_business as t1\n" + |
||||
" LEFT JOIN ACT_RU_TASK as t2 on t1.proc_inst_id = t2.PROC_INST_ID_\n" + |
||||
"WHERE t1.business_id in (?1) and t1.proc_def_key in (?2)"; |
||||
|
||||
Query nativeQuery = entityManager.createNativeQuery(sql); |
||||
nativeQuery.setParameter(1, businessIds); |
||||
nativeQuery.setParameter(2, collect); |
||||
return nativeQuery |
||||
.unwrap(NativeQueryImpl.class) |
||||
.setResultTransformer(Transformers.aliasToBean(io.izn.iec.camunda.vo.SimpleTaskVO.class)) |
||||
.list(); |
||||
}*/ |
||||
} |
@ -1,34 +0,0 @@ |
||||
package com.zilber.boot.activiti.vo; |
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
@Getter |
||||
@Setter |
||||
@ApiModel("流程定义") |
||||
public class DefinitionVO { |
||||
|
||||
@ApiModelProperty("流程定义ID") |
||||
private String id; |
||||
|
||||
@ApiModelProperty("流程定义名称") |
||||
private String name; |
||||
|
||||
@ApiModelProperty("流程定义描述") |
||||
private String description; |
||||
|
||||
@ApiModelProperty("流程定义Key") |
||||
private String key; |
||||
|
||||
@ApiModelProperty("流程定义资源名称") |
||||
private String resourceName; |
||||
|
||||
@ApiModelProperty("流程部署ID") |
||||
private String deploymentId; |
||||
|
||||
@ApiModelProperty("版本号") |
||||
private Integer version; |
||||
|
||||
} |
@ -1,74 +0,0 @@ |
||||
package com.zilber.boot.activiti.vo; |
||||
|
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
import org.activiti.engine.history.HistoricTaskInstance; |
||||
import org.springframework.beans.BeanUtils; |
||||
|
||||
import java.util.Date; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.stream.Collectors; |
||||
|
||||
@Getter |
||||
@Setter |
||||
@ApiModel("历史任务") |
||||
public class HistoricTaskVO { |
||||
|
||||
@ApiModelProperty("任务ID") |
||||
private String taskId; |
||||
|
||||
@ApiModelProperty("任务名称") |
||||
private String taskName; |
||||
|
||||
@ApiModelProperty("任务描述") |
||||
private String taskDescription; |
||||
|
||||
@ApiModelProperty("任务定义Key") |
||||
private String taskDefinitionKey; |
||||
|
||||
@ApiModelProperty("执行人") |
||||
private String assignee; |
||||
|
||||
@ApiModelProperty("任务开始时间") |
||||
private Date startTime; |
||||
|
||||
@ApiModelProperty("任务结束时间") |
||||
private Date endTime; |
||||
|
||||
@ApiModelProperty("任务优先级") |
||||
private Integer priority; |
||||
|
||||
@ApiModelProperty("任务执行时长") |
||||
private Long duration; |
||||
|
||||
@ApiModelProperty("任务删除原因") |
||||
private String deleteReason; |
||||
|
||||
public static List<HistoricTaskVO> merge(List<HistoricTaskInstance> tasks) { |
||||
return tasks.stream() |
||||
.map(HistoricTaskVO::merge) |
||||
.collect(Collectors.toList()); |
||||
} |
||||
|
||||
public static HistoricTaskVO merge(HistoricTaskInstance task) { |
||||
HistoricTaskVO vo = new HistoricTaskVO(); |
||||
vo.setTaskId(task.getId()); |
||||
vo.setTaskName(task.getName()); |
||||
vo.setTaskDescription(task.getDescription()); |
||||
vo.setTaskDefinitionKey(task.getTaskDefinitionKey()); |
||||
vo.setAssignee(task.getAssignee()); |
||||
vo.setStartTime(task.getStartTime()); |
||||
vo.setEndTime(task.getEndTime()); |
||||
vo.setPriority(task.getPriority()); |
||||
vo.setDuration(task.getDurationInMillis()); |
||||
vo.setDeleteReason(task.getDeleteReason()); |
||||
return vo; |
||||
} |
||||
|
||||
|
||||
} |
@ -1,54 +0,0 @@ |
||||
package com.zilber.boot.activiti.vo; |
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
import java.io.Serializable; |
||||
import java.math.BigInteger; |
||||
import java.util.Date; |
||||
|
||||
@Getter |
||||
@Setter |
||||
@ApiModel("审批进度") |
||||
public class InstanceProgressVO implements Serializable { |
||||
|
||||
@ApiModelProperty("活动ID") |
||||
private String actId; |
||||
|
||||
@ApiModelProperty("任务ID") |
||||
private String taskId; |
||||
|
||||
@ApiModelProperty("活动名称") |
||||
private String actName; |
||||
|
||||
// 'userTask', 'startEvent', 'noneEndEvent'
|
||||
@ApiModelProperty("活动类型") |
||||
private String actType; |
||||
|
||||
@ApiModelProperty("流程实例ID") |
||||
private String procInstId; |
||||
|
||||
@ApiModelProperty("任务执行时长") |
||||
private BigInteger duration; |
||||
|
||||
// ActivityInstanceState
|
||||
@ApiModelProperty("活动状态:0-default 1-scopeComplete 2-canceled 3-starting 4-ending") |
||||
private Integer state; |
||||
|
||||
@ApiModelProperty("开始时间") |
||||
private Date startTime; |
||||
|
||||
@ApiModelProperty("结束时间") |
||||
private Date endTime; |
||||
|
||||
@ApiModelProperty("执行人") |
||||
private String assignee; |
||||
|
||||
@ApiModelProperty("审批意见") |
||||
private String remark; |
||||
|
||||
@ApiModelProperty("额外操作项") |
||||
private String operators; |
||||
} |
@ -1,48 +0,0 @@ |
||||
package com.zilber.boot.activiti.vo; |
||||
|
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
import org.activiti.engine.runtime.ProcessInstance; |
||||
|
||||
import java.util.List; |
||||
|
||||
import java.util.stream.Collectors; |
||||
|
||||
@Getter |
||||
@Setter |
||||
@ApiModel("流程实例") |
||||
public class InstanceVO { |
||||
|
||||
@ApiModelProperty("是否挂起:1-正常 2-挂起") |
||||
private Integer suspensionState; |
||||
|
||||
@ApiModelProperty("流程定义Key") |
||||
private String procDefKey; |
||||
|
||||
@ApiModelProperty("流程定义ID") |
||||
private String procDefId; |
||||
|
||||
@ApiModelProperty("流程实例ID") |
||||
private String procInstId; |
||||
|
||||
@ApiModelProperty("备注") |
||||
private String remark; |
||||
|
||||
@ApiModelProperty("发起人") |
||||
private String createBy; |
||||
|
||||
public static List<InstanceVO> merge(List<ProcessInstance> instances) { |
||||
return instances.stream().map(instance -> { |
||||
InstanceVO vo = new InstanceVO(); |
||||
vo.setSuspensionState(instance.isSuspended() ? 2 : 1); |
||||
vo.setProcDefKey(instance.getProcessDefinitionKey()); |
||||
vo.setProcDefId(instance.getProcessDefinitionId()); |
||||
vo.setProcInstId(instance.getProcessInstanceId()); |
||||
vo.setCreateBy(instance.getStartUserId()); |
||||
return vo; |
||||
}).collect(Collectors.toList()); |
||||
} |
||||
} |
@ -1,43 +0,0 @@ |
||||
package com.zilber.boot.activiti.vo; |
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.Date; |
||||
|
||||
@Getter |
||||
@Setter |
||||
@ApiModel("流程任务(简单数据结构)") |
||||
public class SimpleTaskVO implements Serializable { |
||||
|
||||
@ApiModelProperty("任务ID") |
||||
private String taskId; |
||||
|
||||
@ApiModelProperty("任务名称") |
||||
private String taskName; |
||||
|
||||
@ApiModelProperty("任务描述") |
||||
private String taskDescription; |
||||
|
||||
@ApiModelProperty("任务定义Key") |
||||
private String taskDefinitionKey; |
||||
|
||||
@ApiModelProperty("执行人") |
||||
private String assignee; |
||||
|
||||
@ApiModelProperty("任务开始时间") |
||||
private Date taskTime; |
||||
|
||||
@ApiModelProperty("任务优先级") |
||||
private Integer priority; |
||||
|
||||
@ApiModelProperty("是否为挂起状态:1-否 2-是") |
||||
private Integer suspensionState; |
||||
|
||||
@ApiModelProperty("业务ID") |
||||
private Integer businessId; |
||||
|
||||
} |
@ -1,62 +0,0 @@ |
||||
package com.zilber.boot.activiti.vo; |
||||
|
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
import org.activiti.engine.task.Task; |
||||
import org.springframework.beans.BeanUtils; |
||||
|
||||
import java.util.Date; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.stream.Collectors; |
||||
|
||||
@Getter |
||||
@Setter |
||||
@ApiModel("流程任务") |
||||
public class TaskVO { |
||||
|
||||
@ApiModelProperty("任务ID") |
||||
private String taskId; |
||||
|
||||
@ApiModelProperty("任务名称") |
||||
private String taskName; |
||||
|
||||
@ApiModelProperty("任务描述") |
||||
private String taskDescription; |
||||
|
||||
@ApiModelProperty("任务定义Key") |
||||
private String taskDefinitionKey; |
||||
|
||||
@ApiModelProperty("执行人") |
||||
private String assignee; |
||||
|
||||
@ApiModelProperty("任务开始时间") |
||||
private Date taskTime; |
||||
|
||||
@ApiModelProperty("任务优先级") |
||||
private Integer priority; |
||||
|
||||
@ApiModelProperty("是否为挂起状态:1-否 2-是") |
||||
private Integer suspensionState; |
||||
|
||||
public static List<TaskVO> merge(List<Task> tasks) { |
||||
return tasks.stream().map(task -> { |
||||
TaskVO vo = new TaskVO(); |
||||
vo.setTaskId(task.getId()); |
||||
vo.setTaskName(task.getName()); |
||||
vo.setTaskDescription(task.getDescription()); |
||||
vo.setTaskDefinitionKey(task.getTaskDefinitionKey()); |
||||
vo.setAssignee(task.getAssignee()); |
||||
vo.setTaskTime(task.getCreateTime()); |
||||
vo.setPriority(task.getPriority()); |
||||
vo.setSuspensionState(task.isSuspended() ? 2 : 1); |
||||
return vo; |
||||
}).collect(Collectors.toList()); |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue