前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >flowable 实现投票功能

flowable 实现投票功能

作者头像
星痕
发布2019-05-05 11:33:08
2.3K0
发布2019-05-05 11:33:08
举报
文章被收录于专栏:JAVA后端开发

最近想做一个投票节点,具体需求是这样的

  • 流程同意到一定比例时,往下走
  • 如果流程不同意时,即返回指定的任何节点. 注意:流程引擎是我用flowable进行改造的,有些是需要重写节点的行为,现在只说关键的地方,供大家参考.
  1. 流程定义-投票的行为 生成的流程定义如下:
代码语言:javascript
复制
<userTask id="N5" name="审批" flowable:async="true" flowable:assignee="1,2,3">
      <multiInstanceLoopCharacteristics isSequential="false" flowable:collection="{starmark_emptycollection}" flowable:elementVariable="starmark_atuser">
        <completionCondition>${voteMultiInstanceService.completeTask(execution,'22','N6')}</completionCondition>
      </multiInstanceLoopCharacteristics>
    </userTask>

其中voteMultiInstanceService是一个决策判断的bean类

  1. 流程判断是否达到同意或不同意的标准
代码语言:javascript
复制
@Service("voteMultiInstanceService")
public class VoteMultiInstanceService implements Serializable {

    @Autowired
    private RuntimeService runtimeService;

    public boolean completeTask(DelegateExecution execution, String voteRate, String voteRefuseNode) {
        //所有人员审批
        int nrOfInstances = (Integer) execution.getVariable("nrOfInstances"); //总的会签任务数量
        int nrOfCompletedInstances = (Integer) execution.getVariable("nrOfCompletedInstances"); //总的会签任务数量---已执行
        int vote_pass_count = execution.getVariable("starmark_vote_pass_count") == null ? 0 : (Integer) execution.getVariable("starmark_vote_pass_count"); //总的会签任务数量---已执行的
        int voteRateInteger = Integer.parseInt(voteRate);

        if (((float) vote_pass_count / nrOfInstances * 100.0) >= voteRateInteger) {
            return true;
        }
        //不同意
        if ( ((float)(nrOfCompletedInstances - vote_pass_count) / nrOfInstances) * 100.0 > (100 - voteRateInteger)) {
            execution.setVariableLocal("starmark_voteRefuseNode",voteRefuseNode);
            return true;
        }
        return false;
    }
}
  1. 流程不同意时,返回指定的节点 按上所示,流程不同意时,流程设置了一个变量starmark_voteRefuseNode,如果该变量有值,就需要流转到指定的节点 这个时候,需要重写ParallelMultiInstanceBehavior的cleanupMiRoot,具体重写如下:
代码语言:javascript
复制
  protected void cleanupMiRoot(DelegateExecution execution) {
        // Delete multi instance root and all child executions.
        // Create a fresh execution to continue

        ExecutionEntity multiInstanceRootExecution = (ExecutionEntity) getMultiInstanceRootExecution(execution);
        String voteRefuseNode=execution.getVariableLocal("starmark_voteRefuseNode")==null?"":execution.getVariableLocal("starmark_voteRefuseNode")+"";
        FlowElement flowElement = multiInstanceRootExecution.getCurrentFlowElement();
        ExecutionEntity parentExecution = multiInstanceRootExecution.getParent();

        ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();
        Collection<String> executionIdsNotToSendCancelledEventsFor = execution.isMultiInstanceRoot() ? null : Collections.singletonList(execution.getId());
        executionEntityManager.deleteChildExecutions(multiInstanceRootExecution, null, executionIdsNotToSendCancelledEventsFor, DELETE_REASON_END, true, flowElement);
        executionEntityManager.deleteRelatedDataForExecution(multiInstanceRootExecution, DELETE_REASON_END);
        executionEntityManager.delete(multiInstanceRootExecution);

        ExecutionEntity newExecution = executionEntityManager.createChildExecution(parentExecution);
        //投票节点
        if(StringUtils.isEmpty(voteRefuseNode)) {
            //正常流转
            newExecution.setCurrentFlowElement(flowElement);
            super.leave(newExecution);
        } else {
            //不同意时,返回指定节点
            org.flowable.bpmn.model.Process process = ProcessDefinitionUtil.getProcess(newExecution.getProcessDefinitionId());
            FlowElement targetFlowElement = process.getFlowElement(voteRefuseNode);
            newExecution.setCurrentFlowElement(targetFlowElement);
            FlowableEngineAgenda agenda = CommandContextUtil.getAgenda();
            agenda.planContinueProcessInCompensation(newExecution);
        }
    }

至此,投票功能已完成

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.04.18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档