码迷,mamicode.com
首页 > 其他好文 > 详细

//多实例任务节点完成条件

时间:2021-05-24 01:47:35      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:anti   ret   table   等于   size   oba   总经理   action   class   

技术图片

 

 技术图片

 

 

package org.springblade.flow.engine.listener.common;

import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;

import java.io.Serializable;

//多实例任务节点完成条件
@Component("multiInstance")
public class MultiInstance implements Serializable {

	/**
	 * 一票否决
	 * 1、如果有驳回操作,则驳回当前任务节点。
	 * 2、若已审批人数不等于总人数,则多实例任务继续执行
	 * 3、若已审批人数等于总人数,则结束当前任务节点,进入下一个任务节点。
	 * @param execution 分配执行实例
	 */
	public boolean vetoPower(DelegateExecution execution) {
		return vetoPower(execution,"pass");
	}

	public boolean vetoPower(DelegateExecution execution,String agreeFlagText){
		//已完成的实例数
		int completedInstance = (int) execution.getVariable("nrOfCompletedInstances");
		//总实例数
		int nrOfInstances = (int) execution.getVariable("nrOfInstances");
		//否决判断,一票否决
		if (execution.getVariable(agreeFlagText) != null) {
			boolean pass = (boolean) execution.getVariable(agreeFlagText);
			if (!pass) {
				//输出方向为拒绝
				//一票否决其他实例没必要做,结束
				return true;
			}
		}
		//所有实例任务未全部做完则继续其他实例任务
		if (completedInstance != nrOfInstances) {
			return false;
		} else {
			//输出方向为赞同
			//所有都做完了没被否决,结束
			return true;
		}
	}

	/**
	 * 一票否决 + 少数服从多数
	 * 1、如果有驳回操作,则驳回当前任务节点。
	 * 2、若同意人数比例大于等于0.5,则结束当前任务节点,进入下一个任务节点。
	 * 3、若不同意人数比例大于0.5,则驳回当前任务节点。
	 * 4、否则多实例任务继续执行
	 * @param execution
	 * @return
	 */
	public boolean vetoPowerAndObeyMost(DelegateExecution execution) {
		return vetoPowerAndObeyMost(execution,"pass","rejectCount","agreeCount",0.5d);
	}

	public boolean vetoPowerAndObeyMost(DelegateExecution execution,String agreeFlagText,String rejectCountText,String agreeCountText,double agreeRate) {
		//否决判断,一票否决
		if (execution.getVariable(agreeFlagText) != null) {
			boolean pass = (boolean) execution.getVariable(agreeFlagText);
			if (!pass) {
				//输出方向为拒绝
				//一票否决其他实例没必要做,结束
				return true;
			}
		}
		//已完成的实例数
		int completedInstance = (int) execution.getVariable("nrOfCompletedInstances");
		//总实例数
		int nrOfInstances = (int) execution.getVariable("nrOfInstances");
		//获取不同意的次数
		int rejectCount = (int)execution.getVariable(rejectCountText);
		//获取同意人的次数
		int agreeCount = (int)execution.getVariable(agreeCountText);
		//所有实例任务未全部做完则继续其他实例任务
		if (completedInstance != nrOfInstances) {
			//不同意的人数大于设置比例*总人数
			if (rejectCount*1.00/nrOfInstances>(1-agreeRate)){
				execution.setVariable(agreeFlagText, false);
				return true;
			}
			if (agreeCount*1.00/nrOfInstances>=agreeRate){
				execution.setVariable(agreeFlagText, true);
				return true;
			}
			return false;
		} else {
			//输出方向为赞同
			//所有都做完了没被否决,结束
			return true;
		}
	}

	/**
	 * 少数服从多数
	 * 1、若同意人数比例大于等于0.5,则结束当前任务节点,进入下一个任务节点。
	 * 2、若不同意人数比例大于0.5,则驳回当前任务节点。
	 * 3、否则多实例任务继续执行
	 * @param execution
	 * @return
	 */
	public boolean obeyMost(DelegateExecution execution) {
		return obeyMost(execution,"pass","rejectCount","agreeCount",0.5);
	}

	public boolean obeyMost(DelegateExecution execution,String agreeFlagText,String rejectCountText,String agreeCountText,double agreeRate) {
		//已完成的实例数
		int completedInstance = (int) execution.getVariable("nrOfCompletedInstances");
		//总实例数
		int nrOfInstances = (int) execution.getVariable("nrOfInstances");
		//获取不同意的次数
		int rejectCount = (int)execution.getVariable(rejectCountText);
		//获取同意人的次数
		int agreeCount = (int)execution.getVariable(agreeCountText);
		//所有实例任务未全部做完则继续其他实例任务
		if (completedInstance != nrOfInstances) {
			//不同意的人数大于设置比例*总人数
			if (rejectCount*1.00/nrOfInstances>(1-agreeRate)){
				execution.setVariable(agreeFlagText, false);
				return true;
			}
			if (agreeCount*1.00/nrOfInstances>=agreeRate){
				execution.setVariable(agreeFlagText, true);
				return true;
			}
			return false;
		} else {
			//不同意的人数大于设置比例*总人数
			if (rejectCount*1.00/nrOfInstances>(1-agreeRate)){
				execution.setVariable(agreeFlagText, false);
				return true;
			}
			if (agreeCount*1.00/nrOfInstances>=agreeRate){
				execution.setVariable(agreeFlagText, true);
				return true;
			}
			return true;
		}
	}

	public boolean test(DelegateExecution execution,int i,String a){
		System.out.println("===========i====="+i);
		System.out.println("===========a====="+a);
		return false;
	}


}

  

/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.desk.service.impl;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.desk.entity.PurchaseRequest;
import org.springblade.desk.entity.PurchaseRequestLine;
import org.springblade.desk.entity.SapFormInfo;
import org.springblade.desk.mapper.PurchaseRequestMapper;
import org.springblade.desk.service.IPurchaseRequestService;
import org.springblade.desk.service.ISapFormInfoService;
import org.springblade.desk.utils.DeptUserUtil;
import org.springblade.desk.utils.DeskUtil;
import org.springblade.desk.vo.PurchaseRequestVO;
import org.springblade.flow.core.constant.ProcessConstant;
import org.springblade.flow.core.entity.BladeFlow;
import org.springblade.flow.core.feign.IFlowClient;
import org.springblade.flow.core.utils.FlowUtil;
import org.springblade.flow.core.utils.TaskUtil;
import org.springblade.system.entity.Dept;
import org.springblade.system.user.entity.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 服务实现类
 *
 * @author Chill
 */
@Slf4j
@Service
@AllArgsConstructor
public class PurchaseRequestServiceImpl extends BaseServiceImpl<PurchaseRequestMapper, PurchaseRequest> implements IPurchaseRequestService {

	private IFlowClient flowClient;
	private PurchaseRequestLineServiceImpl lineService;
	private DeptUserUtil deptUserUtil;
	private OaSerialServiceImpl oaSerialService;
	private OaAttachmentServiceImpl oaAttachmentService;
	private ISapFormInfoService infoService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	// @GlobalTransactional
	public boolean startProcess(PurchaseRequest bean) {

		//是否启动流程
		boolean isStartProcess = false;
		//是否新增数据
		boolean isAdd = false;

		String tenantId = "000000";
		if(!Func.isEmpty(AuthUtil.getTenantId())){
			tenantId = AuthUtil.getTenantId();
		}

		if(bean!=null){
			if(Func.isEmpty(bean.getProcessInstanceId())){
				isStartProcess = true;
			}
			if(Func.isEmpty(bean.getId())){
				isAdd = true;
			}
			//更新业务数据
			if(bean.getApplyTime()==null){
				bean.setApplyTime(DateUtil.now());
			}
			if(isStartProcess){
				Date now = new Date();
				SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
				String dateStr = format.format(now);
				bean.setOrderCode(oaSerialService.getSerial(Func.toStr(AuthUtil.getTenantId(),"000000"),"purchaseRequest",dateStr,dateStr,5));
			}
			saveOrUpdate(bean);
			PurchaseRequestLine lineEntity = new PurchaseRequestLine();
			lineEntity.setPurchaseRequestId(bean.getId());
			lineService.remove(Condition.getQueryWrapper(lineEntity));
			if(bean.getLineList()!=null&&!bean.getLineList().isEmpty()){
				List<PurchaseRequestLine> list = new ArrayList<>();
				for(PurchaseRequestLine line : bean.getLineList()){
					line.setPurchaseRequestId(bean.getId());
					line.setNotUsedQuantity(line.getQuantity());
					line.setUsedQuantity(new BigDecimal(0));
					line.setTenantId(tenantId);
					list.add(line);
					//lineService.saveOrUpdate(line);
				}
				if(!Func.isNull(list)){
					lineService.saveOrUpdateBatch(list);
				}
			}
			//更新附件信息
			oaAttachmentService.saveOrUpdateOaAttachment(bean.getAttachments(),bean.getId(),bean.getProcessInstanceId(),bean.getTenantId(),"purchaseRequest");
		}

		Long curDeptId = DeskUtil.getCurDeptId(bean.getCreateDept());

		/*
		//经理
		String roleId = deptUserUtil.getRoleId(AuthUtil.getTenantId(),"经理");
		List<User> users = deptUserUtil.getUserListByRoleIdAndDeptId(roleId,String.valueOf(curDeptId),true,AuthUtil.getUserId());
		Long managerUserId = Func.isEmpty(users)?null:users.get(0).getId();

		//总监
		roleId = deptUserUtil.getRoleId(AuthUtil.getTenantId(),"总监");
		users = deptUserUtil.getUserListByRoleIdAndDeptId(roleId,String.valueOf(curDeptId),true,AuthUtil.getUserId());
		Long directorUserId = Func.isEmpty(users)?null:users.get(0).getId();

		//总经理
		roleId =deptUserUtil.getRoleId(AuthUtil.getTenantId(),"总经理");
		users = deptUserUtil.getUserListByRoleIdAndDeptId(roleId,String.valueOf(curDeptId),true,AuthUtil.getUserId());
		Long presidentId = Func.isEmpty(users)?null:users.get(0).getId();

		//采购主管
		Long purchaseManagerUserId ;
		if("茶叶".equals(bean.getType())){
			roleId = deptUserUtil.getRoleId(AuthUtil.getTenantId(), "采购主管(茶叶)");
		}
		else{
			roleId = deptUserUtil.getRoleId(AuthUtil.getTenantId(), "采购主管(其他)");
		}
		users = deptUserUtil.getUserListByRoleId(roleId);
		purchaseManagerUserId = (users.isEmpty() ||users==null)?null:users.get(0).getId() ;

		//IT采购人员
		roleId = deptUserUtil.getRoleId(AuthUtil.getTenantId(), "IT(采购申请)");
		users = deptUserUtil.getUserListByRoleId(roleId);
		Long internetUserId = (users.isEmpty() ||users==null)?null:users.get(0).getId() ;

		//上级主管
		roleId = deptUserUtil.getRoleId(AuthUtil.getTenantId(),"主管");
		boolean isManager = Func.isNotEmpty(AuthUtil.getUserRole())&&AuthUtil.getUserRole().contains("主管")?true:false;
		Long lastManagerUserId = null;
		if(isManager){
			users = deptUserUtil.getUserListByRoleIdAndDeptId(roleId,String.valueOf(curDeptId),true,AuthUtil.getUserId());
			lastManagerUserId = Func.isEmpty(users)?null:users.get(0).getId();
		}
		 */

		//总经理
		String presidentRoleId =deptUserUtil.getRoleId(tenantId,"总经理");
		List<User> users = deptUserUtil.getUserListByRoleIdAndDeptId(presidentRoleId,String.valueOf(curDeptId),true,AuthUtil.getUserId());
		Long presidentId = Func.isEmpty(users)?null:users.get(0).getId();

		//上级主管
		String roleId = deptUserUtil.getRoleId(tenantId,"主管");
		boolean isManager = Func.isNotEmpty(AuthUtil.getUserRole())&&AuthUtil.getUserRole().contains("主管")?true:false;
		users = deptUserUtil.getUserListByRoleIdAndUntilRoleIdAndDeptId(roleId,presidentRoleId,String.valueOf(curDeptId),true);
		List<String> managerUser = new ArrayList<>();
		for (User  u: users ) {
			if(!AuthUtil.getUserId().equals(u.getId())  ){
				managerUser.add(TaskUtil.getTaskUser(String.valueOf(u.getId())));
			}
		}

		// 启动流程
		Kv variables =DeskUtil.createKV(curDeptId,"采购申请",bean.getOrderCode(),bean.getOrigin(),isManager,bean.getCcUser(),bean.getCcUserName())
			.set("type",bean.getType())
			.set("managerSkip",Func.isEmpty(managerUser))
			.set("managerUser",managerUser)
			.set("presidentSkip",Func.isEmpty(presidentId))
			.set("presidentUser",TaskUtil.getTaskUser(String.valueOf(presidentId)))
			/*
			.set("managerSkip",Func.isNull(managerUserId))
			.set("managerUser",TaskUtil.getTaskUser(String.valueOf(managerUserId)))
			.set("directorSkip",Func.isNull(directorUserId))
			.set("directorUser",TaskUtil.getTaskUser(String.valueOf(directorUserId)))
			.set("presidentSkip",Func.isEmpty(presidentId))
			.set("presidentUser",TaskUtil.getTaskUser(String.valueOf(presidentId)))
			.set("purchaseManagerSkip",Func.isEmpty(purchaseManagerUserId))
			.set("purchaseManagerUser",TaskUtil.getTaskUser(String.valueOf(purchaseManagerUserId)))
			.set("purchaseSkip",Func.isEmpty(bean.getPurchaseUser()))
			.set("purchaseUser",TaskUtil.getTaskUser(String.valueOf(bean.getPurchaseUser())))
			.set("internetSkip",Func.isEmpty(internetUserId))
			.set("internetUser",TaskUtil.getTaskUser(String.valueOf(internetUserId)))
			.set("lastManagerSkip",Func.isEmpty(lastManagerUserId))
			.set("lastManagerUser",TaskUtil.getTaskUser(String.valueOf(lastManagerUserId)))
			*/ ;

		//启动流程
		if (isStartProcess) {
			String businessTable = FlowUtil.getBusinessTable(ProcessConstant.PURCHASE_REQUEST_KEY);
			R<BladeFlow> result = new R<>();
			if(Func.isEmpty(AuthUtil.getUserId()) || AuthUtil.getUserId() == -1L ){
				String processDefinitionId = "";
				SapFormInfo SapFormInfo = new SapFormInfo();
				SapFormInfo.setName("采购申请单");
				SapFormInfo.setIsEnable(1);
				List<SapFormInfo> infoList = infoService.list(Condition.getQueryWrapper(SapFormInfo)) ;
				if(infoList!=null && infoList.size() > 0 ){
					processDefinitionId = infoList.get(0).getProcessDefinitionId() ;
				}
				result = flowClient.startProcessInstanceByIdDing( !Func.isEmpty(processDefinitionId) ? processDefinitionId: bean.getProcessDefinitionId() ,
					FlowUtil.getBusinessKey(businessTable, String.valueOf(bean.getId())),
					String.valueOf(bean.getCreateUser()),variables);
			}
			else{
				result = flowClient.startProcessInstanceById(bean.getProcessDefinitionId(), FlowUtil.getBusinessKey(businessTable, String.valueOf(bean.getId())), variables);

			}
			if (result.isSuccess()) {
				log.debug("流程已启动,流程ID:" + result.getData().getProcessInstanceId());
				// 返回流程id写入业务表中
				bean.setProcessInstanceId(result.getData().getProcessInstanceId());
				updateById(bean);
			} else {
				throw new ServiceException("开启流程失败");
			}
		}
		else{
			if(!Func.isEmpty(bean.getFlow())&&!Func.isEmpty(bean.getFlow().getTaskId())&&!Func.isEmpty(bean.getProcessInstanceId())){
				variables.put(ProcessConstant.PASS_KEY, true);
				flowClient.completeTask(bean.getFlow().getTaskId(),
					bean.getProcessInstanceId(),Func.toStr(bean.getFlow().getComment(),ProcessConstant.PASS_COMMENT),variables);
			}
		}
		return true;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	// @GlobalTransactional
	public boolean saveDraft(PurchaseRequest bean) {
		//是否新增数据
		boolean isAdd = false;
		if(bean!=null){
			if(Func.isEmpty(bean.getId())){
				isAdd = true;
			}
			//更新业务数据
			saveOrUpdate(bean);
			PurchaseRequestLine lineEntity = new PurchaseRequestLine();
			lineEntity.setPurchaseRequestId(bean.getId());
			lineService.remove(Condition.getQueryWrapper(lineEntity));
			if(bean.getLineList()!=null&&!bean.getLineList().isEmpty()){
				for(PurchaseRequestLine line : bean.getLineList()){
					line.setPurchaseRequestId(bean.getId());
					line.setNotUsedQuantity(line.getQuantity());
					line.setUsedQuantity(new BigDecimal(0));
					lineService.saveOrUpdate(line);
				}
			}
			//更新附件信息
			oaAttachmentService.saveOrUpdateOaAttachment(bean.getAttachments(),bean.getId(),bean.getProcessInstanceId(),bean.getTenantId(),"purchaseRequest");
		}
		return true;
	}

	/**
	 * 获取草稿列表
	 */
	@Override
	public List<PurchaseRequest> getDraftList(PurchaseRequest bean){
		bean.setTenantId(AuthUtil.getTenantId());
		return baseMapper.getDraftList(bean);
	};

	/**
	 * 列表搜索
	 * @param bean
	 * @return
	 */
	@Override
	public List<PurchaseRequestVO> search(PurchaseRequestVO bean){
		bean.setTenantId(AuthUtil.getTenantId());
 		return baseMapper.search(bean);
	}

	/**
	 * 同意任务
	 * @param flow
	 * @return
	 */
	@Override
	public boolean completeTask(BladeFlow flow,PurchaseRequest bean) {
		String taskId = flow.getTaskId();
		String processInstanceId = flow.getProcessInstanceId();
		String comment = Func.toStr(flow.getComment(), ProcessConstant.PASS_COMMENT);
		// 创建变量
		Map<String, Object> variables = flow.getVariables();
		if (variables == null) {
			variables = Kv.create();
		}
		variables.put(ProcessConstant.PASS_KEY, flow.isPass());
		if("purchaseManagerTask".equals(flow.getTaskDefinitionKey())){
			PurchaseRequest entity = new PurchaseRequest();
			entity.setId( bean.getId());
			entity.setPurchaseUser(bean.getPurchaseUser());
			entity.setPurchaseUserName(bean.getPurchaseUserName());
			updateById(entity);
			variables.put("purchaseSkip",Func.isEmpty(bean.getPurchaseUser()));
			variables.put("purchaseUser",TaskUtil.getTaskUser(String.valueOf(bean.getPurchaseUser())));
		}
		flowClient.completeTask(taskId,processInstanceId,comment,variables);
		return true;
	}

	/**
	 * 获取流程任务信息
	 * @param taskUser 当前流程用户
	 * @param processInstanceId 流程实例id
	 */
	public BladeFlow taskInfo(String taskUser,String processInstanceId){
		R<BladeFlow> rFlow = flowClient.taskInfo(taskUser,processInstanceId);
		if(rFlow!=null){
			return rFlow.getData();
		}
		return null;
	}
	/**
	 * 作废任务
	 * @param businessId
	 * @param processInstanceId
	 * @param reason 作废原因
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean cancellation(String businessId, String processInstanceId, String reason) {
		log.info("采购申请单作废businessId===="+businessId+"--processInstanceId===="+processInstanceId+"---reason==="+reason);
		boolean flag=removeById(Long.valueOf(businessId));
		log.info("flag======"+flag);
		if(flag){
			flag=flowClient.deleteTask(processInstanceId,reason).isSuccess();
		}else {
			flag=false;
		}
		return flag;
	}
}

  

//多实例任务节点完成条件

标签:anti   ret   table   等于   size   oba   总经理   action   class   

原文地址:https://www.cnblogs.com/xianz666/p/14744573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!