diff --git a/renren-admin/src/main/java/io/renren/modules/processForm/listener/v3/CorrectionListenerV3.java b/renren-admin/src/main/java/io/renren/modules/processForm/listener/v3/CorrectionListenerV3.java new file mode 100644 index 00000000..c1774814 --- /dev/null +++ b/renren-admin/src/main/java/io/renren/modules/processForm/listener/v3/CorrectionListenerV3.java @@ -0,0 +1,351 @@ +package io.renren.modules.processForm.listener.v3; + +import com.alibaba.fastjson.JSON; +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.common.dto.AuditingBaseDTO; +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.activiti.engine.task.Task; +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.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * 部门动态审批人 v3 + */ +@Component +public class CorrectionListenerV3 implements TaskListener, ExecutionListener, ActivitiEventListener, JavaDelegate { + private static Logger logger = LoggerFactory.getLogger(CorrectionListenerV3.class); + @Value("${big_date.name}") + private String bigDateDeptName; // 大数据局名称 + @Value("${big_date.default_assignee_role_id}") + private String defaultAssigneeRoleId; // 当某部门未设置部门审批人时,将使用该用户审批 + @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 JdbcTemplate jdbcTemplate; + @Autowired + private TAbilityApplicationService tAbilityApplicationService; + + @Override + @ActivitiNoticeOperation(value = "流程结束", process = "能力申请流程v2", type = 2) + public void notify(DelegateExecution delegateExecution) throws Exception { + logger.error("----------------------进入部门审批结束节点---------------------------"); + delegateExecution.getProcessBusinessKey(); + final String eventName = delegateExecution.getEventName(); + if (EVENTNAME_END.equals(eventName)) { + endTake(delegateExecution); + } + } + + @Override + public void execute(DelegateExecution execution) throws Exception { + + } + + @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("-------------------------结束部门动态审批人流程-------------------------------"); + } + + /** + * Called when an event has been fired + * + * @param event the event + */ + @Override + public void onEvent(ActivitiEvent event) { + + } + + /** + * @return whether or not the current operation should fail when this listeners execution + * throws an exception. + */ + @Override + public boolean isFailOnException() { + return false; + } + + /** + * 结束审批 + * + * @param delegateExecution + */ + private void endTake(DelegateExecution delegateExecution) { // 进入最后结束节点 + Map kv = delegateExecution.getVariables(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, (JsonDeserializer) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong())); + + Gson gson = builder.create(); + JsonElement jsonElement = gson.toJsonTree(kv); + AuditingBaseDTO auditingBaseDTO = gson.fromJson(jsonElement, AuditingBaseDTO.class); + if (auditingBaseDTO != null) { + List dtoList = new ArrayList<>(); + if (kv.containsKey("tAbilityApplicationDTOList")) { + dtoList = (List) kv.get("tAbilityApplicationDTOList"); + } + String[] sqls; + if (auditingBaseDTO.getReject() != null && auditingBaseDTO.getReject() == Boolean.TRUE) { // 存在被拒绝的节点 + sqls = dtoList.stream().map(i -> String.format("UPDATE t_ability_application SET del_flag = 0,approve_status = '不通过' WHERE id = %s", i.getId())) + .collect(Collectors.toList()).toArray(new String[dtoList.size()]); + } else { + sqls = dtoList.stream().map(i -> String.format("UPDATE t_ability_application SET del_flag = 0,approve_status = '通过' WHERE id = %s", i.getId())) + .collect(Collectors.toList()).toArray(new String[dtoList.size()]); + } + jdbcTemplate.batchUpdate(sqls); + + if (auditingBaseDTO.getReject() == null || auditingBaseDTO.getReject() != Boolean.TRUE) { // 都是同意 + try { + batchApplyCode(delegateExecution, dtoList); + } catch (Exception exception) { + logger.error("上架网关失败", exception); + } + } + + } + } + + /** + * 节点创建时动态分配资源部门审核人 + * + * @param delegateTask + */ + private void create(DelegateTask delegateTask) { + String assignee; + Map kv = delegateTask.getVariables(); + SysRoleDTO roleDTO = sysRoleService.getByName(roleName); + logger.error("roleDTOId:{}", roleDTO.getId()); + Long deptId; + if (kv.containsKey("deptId")) { // 存在部门id + deptId = Long.valueOf(kv.get("deptId").toString()); + SysDeptDTO deptDTO = sysDeptService.get(deptId); +// 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; +// } // 二级审批依然为大数据局 + + 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()); + assignee = userDTO.getId().toString(); + } else { + logger.error("未查到该部门 {} 对应的 {}", deptId, roleName); + taskService.setAssignee(delegateTask.getId(), defaultAssigneeRoleId); + assignee = defaultAssigneeRoleId; + } + if (kv.containsKey("tAbilityApplicationDTOList")) { + List dtoList = (List) kv.get("tAbilityApplicationDTOList"); + mpComplete(dtoList, delegateTask, assignee); + } + } else { + try { + logger.error("流程参数异常" + JSON.toJSONString(kv)); + } catch (Exception exception) { + logger.error("流程参数异常", exception); + } + } + } + + private void mpComplete(List abilityApplicationDTO, DelegateTask delegateTask, final String assignee) { // 处理免批或申请人为资源提供方部门审批人 + Optional abilityApplicationDTOOptional = abilityApplicationDTO.stream().findAny(); + abilityApplicationDTOOptional.ifPresent(index -> { + if (abilityApplicationDTOOptional.get().getUserId().equals(delegateTask.getAssignee())) { + taskService.addComment(delegateTask.getId(), delegateTask.getProcessInstanceId(), "申请人为资源提供方部门审批人,默认通过"); + taskService.complete(delegateTask.getId(), delegateTask.getVariables()); + return; + } + }); + if (StringUtils.isNotEmpty(assignee) + && StringUtils.isNotEmpty(delegateTask.getVariable("creator", String.class)) + && delegateTask.getVariable("creator", String.class).equals(assignee)) { + taskService.addComment(delegateTask.getId(), delegateTask.getProcessInstanceId(), "审批人为发起人,默认通过"); + taskService.setVariable(delegateTask.getId(), ActTaskService.Task_HANDLE_STATE, ActTaskService.Task_HANDLE_STATE_AGREE); + taskService.setVariable(delegateTask.getId(), "backToFirst", Boolean.FALSE); + taskService.complete(delegateTask.getId(), delegateTask.getVariables()); + return; + } + + List dtoList = delegateTask.getVariable("tAbilityApplicationDTOList", List.class); + Optional tAbilityApplicationDTO = dtoList.stream().filter(index -> { + Optional resourceDTOOptional = Optional.ofNullable(resourceService.get(Long.valueOf(index.getResourceId()))); + if (resourceDTOOptional.isPresent()) { + return !StringUtils.contains(resourceDTOOptional.get().getShareCondition(), "免批"); // 存在非免批 + } else { + return false; + } + }).findAny(); + if (!tAbilityApplicationDTO.isPresent()) { // 全是免批 + taskService.addComment(delegateTask.getId(), delegateTask.getProcessInstanceId(), "免批资源申请默认通过"); + taskService.complete(delegateTask.getId(), delegateTask.getVariables()); + return; + } + } + + private void complete(DelegateTask delegateTask) { + Map kv = delegateTask.getVariables(); + + //如果有code说明已经注册过了,以及只有通过的流程申请 +// if (kv.get("gatewayCode") != null || +// !ActTaskService.Task_HANDLE_STATE_AGREE.equals(kv.get(ActTaskService.Task_HANDLE_STATE))) return; +// try { +// List dtoList = new ArrayList<>(); +// if (kv.containsKey("tAbilityApplicationDTOList")) { +// dtoList = (List) kv.get("tAbilityApplicationDTOList"); +// } +// dtoList.stream().forEach(index -> { +// applyCode(delegateTask, index); // 列表内都执行 +// }); +// } 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); + + + } + + private void batchApplyCode(DelegateExecution delegateExecution, List dtoList) { + + logger.info("-------能力申请code-------"); + + StringBuilder allMsg = new StringBuilder(); + allMsg.append("您的能力申请已通过,访问令牌如下:"); + allMsg.append('\n'); + + boolean hasData = false; + for (TAbilityApplicationDTO abilityApplicationDTO : dtoList) { + ResourceEntity resourceEntity = resourceService.selectById(abilityApplicationDTO.getResourceId()); + if (resourceEntity == null) { + continue; + } + //没有groupid当做没有接口,直接跳过 + if (resourceEntity == null || resourceEntity.getGroupId() == null) + continue; + + hasData = true; + + String code = UUID.randomUUID().toString(); + apiGatewayService.subscribeCode(String.valueOf(abilityApplicationDTO.getId()), code); + String apiPrefix = "/juapi/" + abilityApplicationDTO.getResourceId(); + String msg = String.format("能力名称:%s,接口认证code为:%s, 接口公共前缀为:%s", resourceEntity.getName(), code, apiPrefix); + allMsg.append(msg); + allMsg.append('\n'); + + } + //一条也没有跳过 + if (!hasData) return; + + + TaskService taskService = ProcessEngines.getDefaultProcessEngine().getTaskService(); + //先用不正经方法找 + List tasks = taskService.createTaskQuery(). + processInstanceId(delegateExecution.getProcessInstanceId()). + list(); + Task bigDataTask = null; + for (Task task : tasks) { + if ("大数据部门负责人审批".equals(task.getName())) { + bigDataTask = task; + break; + } + } + + if (bigDataTask != null) { + taskService.addComment(bigDataTask.getId(), delegateExecution.getProcessInstanceId(), allMsg.toString()); + } + + + } +}