TODO 批量申请的任务监听器

This commit is contained in:
wangliwen 2022-06-26 15:31:57 +08:00
parent e2036397cf
commit f5cc6c10b0
2 changed files with 400 additions and 0 deletions

View File

@ -0,0 +1,266 @@
package io.renren.modules.processForm.listener.v2;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import io.renren.common.annotation.ActivitiNoticeOperation;
import io.renren.modules.activiti.service.ActTaskService;
import io.renren.modules.processForm.dto.TAbilityApplicationDTO;
import io.renren.modules.processForm.service.ApiGatewayService;
import io.renren.modules.processForm.service.TAbilityApplicationService;
import io.renren.modules.resource.dto.ResourceDTO;
import io.renren.modules.resource.entity.ResourceEntity;
import io.renren.modules.resource.service.ResourceService;
import io.renren.modules.sys.dto.SysDeptDTO;
import io.renren.modules.sys.dto.SysRoleDTO;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysDeptService;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.service.SysRoleUserService;
import io.renren.modules.sys.service.SysUserService;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.*;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
/**
* 部门动态审批人 v2
*/
@Component
public class CorrectionListenerV2 implements TaskListener, ExecutionListener, ActivitiEventListener, JavaDelegate {
private static Logger logger = LoggerFactory.getLogger(CorrectionListenerV2.class);
@Value("${big_date.name}")
private String bigDateDeptName; // 大数据局名称
@Value("${big_date.assignee_role_name}")
private String roleName; // 具备审批的角色名称
@Autowired
private SysRoleService sysRoleService;
@Autowired
private TaskService taskService;
@Autowired
private SysUserService sysUserService;
@Autowired
private SysRoleUserService sysRoleUserService;
@Autowired
private SysDeptService sysDeptService;
@Autowired
private ApiGatewayService apiGatewayService;
@Autowired
private ResourceService resourceService;
@Autowired
private TAbilityApplicationService tAbilityApplicationService;
@Override
@ActivitiNoticeOperation(value = "资源部门负责人审批", process = "能力申请流程")
public void notify(DelegateTask delegateTask) {
logger.error("-------------------------进入部门动态审批人流程-------------------------------");
final String eventName = delegateTask.getEventName();
switch (eventName) {
case EVENTNAME_CREATE:
create(delegateTask);
break;
case EVENTNAME_COMPLETE:
complete(delegateTask);
break;
default:
}
logger.error("-------------------------结束部门动态审批人流程-------------------------------");
}
@Override
@ActivitiNoticeOperation(value = "流程结束", process = "能力申请流程", type = 2)
public void notify(DelegateExecution delegateExecution) throws Exception {
logger.error("----------------------进入部门审批结束节点---------------------------");
delegateExecution.getProcessBusinessKey();
final String eventName = delegateExecution.getEventName();
switch (eventName) {
case EVENTNAME_END:
endTake(delegateExecution.getVariables());
break;
}
}
@Override
public void onEvent(ActivitiEvent activitiEvent) { // 事件回调
logger.error("事件类型:" + activitiEvent.getType().toString());
logger.error("" + activitiEvent.getProcessDefinitionId());
}
@Override
public boolean isFailOnException() {
return true;
}
@Override
public void execute(DelegateExecution delegateExecution) throws Exception {
logger.error("异常", delegateExecution);
}
/**
* 结束审批
*
* @param kv
*/
private void endTake(Map<String, Object> kv) { // 进入最后结束节点
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong()));
Gson gson = builder.create();
JsonElement jsonElement = gson.toJsonTree(kv);
TAbilityApplicationDTO abilityApplicationDTO = gson.fromJson(jsonElement, TAbilityApplicationDTO.class);
if (abilityApplicationDTO != null) {
if (abilityApplicationDTO.getReject() != null && abilityApplicationDTO.getReject() == Boolean.TRUE) { // 存在被拒绝的节点
abilityApplicationDTO.setDelFlag(0);
abilityApplicationDTO.setApproveStatus("不通过");
tAbilityApplicationService.update(abilityApplicationDTO);
logger.error("审批不通过申请id:" + abilityApplicationDTO.getId());
} else {
abilityApplicationDTO.setDelFlag(0);
abilityApplicationDTO.setApproveStatus("通过");
tAbilityApplicationService.update(abilityApplicationDTO);
logger.error("审批通过申请id:" + abilityApplicationDTO.getId());
}
}
}
/**
* 节点创建时动态分配资源部门审核人
*
* @param delegateTask
*/
private void create(DelegateTask delegateTask) {
Map<String, Object> kv = delegateTask.getVariables();
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong()));
Gson gson = builder.create();
JsonElement jsonElement = gson.toJsonTree(kv);
TAbilityApplicationDTO abilityApplicationDTO = gson.fromJson(jsonElement, TAbilityApplicationDTO.class);
SysRoleDTO roleDTO = sysRoleService.getByName(roleName);
logger.error("roleDTOId:" + roleDTO.getId());
ResourceDTO resourceEntityDto = null;
Long deptId = null;
if (abilityApplicationDTO != null) {
logger.error("abilityApplicationDTO:" + abilityApplicationDTO.toString());
resourceEntityDto = resourceService.get(Long.valueOf(abilityApplicationDTO.getResourceId()));
}
if (resourceEntityDto != null && resourceEntityDto.getDeptId() != null) {
deptId = resourceEntityDto.getDeptId();
SysDeptDTO deptDTO =
sysDeptService.get(resourceEntityDto.getDeptId());
if (deptDTO != null && deptDTO.getName().equals(bigDateDeptName)) {
logger.error("第二级别审批仍然为 " + bigDateDeptName);
SysUserDTO userDTO = sysUserService.getByDeptIdAndRoleId(deptId, roleDTO.getId()); // 搜出审批人
if (userDTO != null) {
taskService.setAssignee(delegateTask.getId(), userDTO.getId().toString());
}
taskService.addComment(delegateTask.getId(), delegateTask.getProcessInstanceId(), "默认通过");
taskService.complete(delegateTask.getId(), delegateTask.getVariables());
return;
}
} else {
logger.error("表单内单位名称:" + abilityApplicationDTO.getUnit());
SysDeptDTO deptDTO = sysDeptService.getByName(abilityApplicationDTO.getUnit());
if (deptDTO != null) {
logger.error("deptDTOId:" + deptDTO.getId());
deptId = deptDTO.getId();
}
}
SysUserDTO userDTO = null;
if (deptId != null) {
userDTO = sysUserService.getByDeptIdAndRoleId(deptId, roleDTO.getId()); // 搜出审批人
}
if (userDTO != null) {
logger.error("审批人id:" + userDTO.getId());
taskService.setAssignee(delegateTask.getId(), userDTO.getId().toString());
} else {
logger.error("未查到该部门对应的 " + roleName);
taskService.setAssignee(delegateTask.getId(), "1516728698224427010");
}
mpComplete(abilityApplicationDTO, delegateTask);
}
private void mpComplete(TAbilityApplicationDTO abilityApplicationDTO, DelegateTask delegateTask) {
Optional<ResourceDTO> resourceDTOOptional = Optional.ofNullable(resourceService.get(Long.valueOf(abilityApplicationDTO.getResourceId())));
resourceDTOOptional.ifPresent(resource -> {
if (StringUtils.contains(resource.getShareCondition(), "免批")) { // 针对免批资源申请
taskService.addComment(delegateTask.getId(), delegateTask.getProcessInstanceId(), "免批资源申请默认通过");
taskService.complete(delegateTask.getId(), delegateTask.getVariables());
return;
}
if (abilityApplicationDTO.getUserId().equals(delegateTask.getAssignee())) {
taskService.addComment(delegateTask.getId(), delegateTask.getProcessInstanceId(), "申请人为资源提供方部门审批人,默认通过");
taskService.complete(delegateTask.getId(), delegateTask.getVariables());
return;
}
});
}
private void complete(DelegateTask delegateTask) {
Map<String, Object> kv = delegateTask.getVariables();
//如果有code说明已经注册过了,以及只有通过的流程申请
if (kv.get("gatewayCode") != null ||
!ActTaskService.Task_HANDLE_STATE_AGREE.equals(kv.get(ActTaskService.Task_HANDLE_STATE))) return;
try {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong()));
Gson gson = builder.create();
JsonElement jsonElement = gson.toJsonTree(kv);
TAbilityApplicationDTO abilityApplicationDTO = gson.fromJson(jsonElement, TAbilityApplicationDTO.class);
applyCode(delegateTask, abilityApplicationDTO);
} catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
}
}
/**
* 审批通过申请code
*
* @param delegateTask
* @param abilityApplicationDTO
*/
private void applyCode(DelegateTask delegateTask, TAbilityApplicationDTO abilityApplicationDTO) {
logger.info("-------能力申请code-------");
ResourceEntity resourceEntity = resourceService.selectById(abilityApplicationDTO.getResourceId());
//没有groupid当做没有接口直接跳过
if (resourceEntity.getGroupId() == null)
return;
String code = UUID.randomUUID().toString();
apiGatewayService.subscribeCode(String.valueOf(abilityApplicationDTO.getId()), code);
delegateTask.setVariable("gatewayCode", code);
String apiPrefix = "/juapi/" + abilityApplicationDTO.getResourceId();
TaskService taskService = ProcessEngines.getDefaultProcessEngine().getTaskService();
String msg = String.format("您的能力申请已通过接口认证code为%s, 接口公共前缀为:%s", code, apiPrefix);
taskService.addComment(delegateTask.getId(), delegateTask.getProcessInstanceId(), msg);
}
}

View File

@ -0,0 +1,134 @@
package io.renren.modules.processForm.listener.v2;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import io.renren.common.annotation.ActivitiNoticeOperation;
import io.renren.modules.resource.service.ResourceService;
import io.renren.modules.sys.dto.SysDeptDTO;
import io.renren.modules.sys.dto.SysRoleDTO;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysDeptService;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.service.SysRoleUserService;
import io.renren.modules.sys.service.SysUserService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.*;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.Map;
/**
* 大数据局动态审批人 v2
*/
@Component
public class DataCenterListenerV2 implements TaskListener, ExecutionListener, ActivitiEventListener, JavaDelegate {
private static Logger logger = LoggerFactory.getLogger(DataCenterListenerV2.class);
@Value("${big_date.name}")
private String bigDateDeptName; // 大数据局名称
@Value("${big_date.assignee_role_name}")
private String roleName; // 具备审批的角色名称
@Autowired
private SysRoleService sysRoleService;
@Autowired
private TaskService taskService;
@Autowired
private SysUserService sysUserService;
@Autowired
private SysRoleUserService sysRoleUserService;
@Autowired
private SysDeptService sysDeptService;
@Autowired
private ResourceService resourceService;
@Override
public void notify(DelegateExecution delegateExecution) throws Exception {
}
@Override
public void execute(DelegateExecution delegateExecution) throws Exception {
}
@Override
@ActivitiNoticeOperation(value = "大数据局负责人审批", process = "能力申请流程")
public void notify(DelegateTask delegateTask) {
logger.error("事件类型:" + delegateTask.getEventName());
final String eventName = delegateTask.getEventName();
switch (eventName) {
case EVENTNAME_CREATE:
createEvent(delegateTask);
break;
case EVENTNAME_COMPLETE:
logger.error("----------------------大数据审批通过-complete------------------------------");
break;
default:
logger.error("未处理该事件:" + eventName);
}
}
@Override
public void onEvent(ActivitiEvent activitiEvent) {
}
@Override
public boolean isFailOnException() {
return false;
}
/**
* 节点创建时动态分配大数据局审批人
*
* @param delegateTask
*/
private void createEvent(DelegateTask delegateTask) {
logger.error("大数据局名称:" + bigDateDeptName);
SysDeptDTO deptDTO = sysDeptService.getByName(bigDateDeptName);
logger.error("deptDTOId:" + deptDTO.getId());
SysRoleDTO roleDTO = sysRoleService.getByName(roleName);
logger.error("roleDTOId:" + roleDTO.getId());
SysUserDTO userDTO = sysUserService.getByDeptIdAndRoleId(deptDTO.getId(), roleDTO.getId());
if (userDTO != null) {
logger.error("审批人id:" + userDTO.getId());
taskService.setAssignee(delegateTask.getId(), userDTO.getId().toString());
} else {
delegateTask.setAssignee("1516728698224427010");
logger.error("未查到该部门对应 " + roleName);
}
Map<String, Object> kv = delegateTask.getVariables();
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong()));
Gson gson = builder.create();
JsonElement jsonElement = gson.toJsonTree(kv);
// TAbilityApplicationDTO abilityApplicationDTO = gson.fromJson(jsonElement, TAbilityApplicationDTO.class);
// Optional<ResourceDTO> resourceDTOOptional = Optional.ofNullable(resourceService.get(Long.valueOf(abilityApplicationDTO.getResourceId())));
// resourceDTOOptional.ifPresent(resource -> {
// if (StringUtils.contains(resource.getShareCondition(), "免批")) { // 针对免批资源申请
// taskService.addComment(delegateTask.getId(), delegateTask.getProcessInstanceId(), "免批资源申请默认通过");
// taskService.complete(delegateTask.getId(), delegateTask.getVariables());
// return;
// }
// if (userDTO != null && abilityApplicationDTO.getUserId().equals(userDTO.getId().toString())) {
// taskService.addComment(delegateTask.getId(), delegateTask.getProcessInstanceId(), "申请人为大数据部门审批人,默认通过");
// taskService.complete(delegateTask.getId(), delegateTask.getVariables());
// return;
// }
// });
}
}