| | |
| | | package org.jeecg.modules.flowable.service.impl; |
| | | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.map.MapUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.google.common.collect.Lists; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.collections4.CollectionUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.flowable.bpmn.model.Process; |
| | | import org.flowable.bpmn.model.*; |
| | | import org.flowable.bpmn.model.Process; |
| | | import org.flowable.common.engine.api.FlowableException; |
| | | import org.flowable.common.engine.api.FlowableObjectNotFoundException; |
| | | import org.flowable.common.engine.impl.identity.Authentication; |
| | |
| | | import org.flowable.task.api.Task; |
| | | import org.flowable.task.api.TaskQuery; |
| | | import org.flowable.task.api.history.HistoricTaskInstance; |
| | | import org.flowable.task.api.history.HistoricTaskInstanceQuery; |
| | | import org.jeecg.common.api.vo.Result; |
| | | import org.jeecg.common.system.vo.LoginUser; |
| | | import org.jeecg.common.util.SpringContextUtils; |
| | | import org.jeecg.modules.flowable.apithird.business.dto.FlowMyBusinessDto; |
| | | import org.jeecg.modules.flowable.apithird.business.entity.FlowMyBusiness; |
| | | import org.jeecg.modules.flowable.apithird.business.service.impl.FlowMyBusinessServiceImpl; |
| | | import org.jeecg.modules.flowable.apithird.entity.ActStatus; |
| | | import org.jeecg.modules.flowable.apithird.entity.SysUser; |
| | | import org.jeecg.modules.flowable.apithird.service.FlowCallBackServiceI; |
| | | import org.jeecg.modules.flowable.apithird.service.IFlowThirdService; |
| | | import org.jeecg.modules.flowable.apithird.common.constant.ProcessConstants; |
| | | import org.jeecg.modules.flowable.apithird.common.enums.FlowComment; |
| | | import org.jeecg.modules.flowable.apithird.common.exception.CustomException; |
| | | import org.jeecg.modules.flowable.apithird.entity.SysUser; |
| | | import org.jeecg.modules.flowable.apithird.service.FlowCallBackServiceI; |
| | | import org.jeecg.modules.flowable.apithird.service.IFlowThirdService; |
| | | import org.jeecg.modules.flowable.domain.dto.FlowCommentDto; |
| | | import org.jeecg.modules.flowable.domain.dto.FlowNextDto; |
| | | import org.jeecg.modules.flowable.domain.dto.FlowTaskDto; |
| | |
| | | taskService.saveTask(task); |
| | | } |
| | | SysUser loginUser = iFlowThirdService.getLoginUser(); |
| | | //判断用户是否有权限 |
| | | if (!task.getAssignee().equals(loginUser.getUsername())){ |
| | | return Result.error("当前用户无权限"); |
| | | } |
| | | if (DelegationState.PENDING.equals(task.getDelegationState())) { |
| | | taskService.addComment(taskVo.getTaskId(), taskVo.getInstanceId(), FlowComment.DELEGATE.getType(), taskVo.getComment()); |
| | | //taskService.resolveTask(taskVo.getTaskId(), taskVo.getValues()); |
| | |
| | | System.out.println("taskVo.getTaskId()--->taskVo.getInstanceId()--->FlowComment.NORMAL.getType()--->taskVo.getComment()"+taskVo.getTaskId() + "---" + taskVo.getInstanceId() + "---" + FlowComment.NORMAL.getType() + "---"+taskVo.getComment() ); |
| | | taskService.addComment(taskVo.getTaskId(), taskVo.getInstanceId(), FlowComment.NORMAL.getType(), taskVo.getComment()); |
| | | taskService.setAssignee(taskVo.getTaskId(), loginUser.getUsername()); |
| | | //taskService.complete(taskVo.getTaskId(), taskVo.getValues()); |
| | | } |
| | | /*======================审批结束 回调以及关键数据保存======================*/ |
| | | //业务数据id |
| | |
| | | |
| | | if (task2!=null && task.getTaskDefinitionKey().equals(task2.getTaskDefinitionKey())){ |
| | | // * 当前节点是会签节点,没有走完 |
| | | business.setActStatus(ActStatus.doing) |
| | | .setTaskId(task2.getId()) |
| | | business.setTaskId(task2.getId()) |
| | | // .setActStatus(ActStatus.doing) |
| | | .setDoneUsers(doneUserList.toJSONString()) |
| | | ; |
| | | String todoUsersStr = business.getTodoUsers(); |
| | |
| | | // 前端传入候选人 |
| | | collect_username = candidateUsers; |
| | | } |
| | | business.setActStatus(ActStatus.doing) |
| | | business |
| | | .setTaskId(task2.getId()) |
| | | // .setActStatus(ActStatus.doing) |
| | | .setTaskNameId(nextTask.getId()) |
| | | .setTaskName(nextTask.getName()) |
| | | .setPriority(nextTask.getPriority()) |
| | |
| | | |
| | | } else { |
| | | // **没有下一个节点,流程已经结束了 |
| | | business.setActStatus(ActStatus.pass) |
| | | business |
| | | .setDoneUsers(doneUserList.toJSONString()) |
| | | .setTodoUsers("") |
| | | .setTaskId("") |
| | |
| | | if (flowCallBackService!=null)flowCallBackService.afterFlowHandle(business); |
| | | return Result.OK(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result completeByDateId(FlowTaskVo flowTaskVo){ |
| | | return this.complete(flowTaskVo); |
| | | } |
| | | |
| | | @Override |
| | | public void taskRejectByDataId(FlowTaskVo flowTaskVo){ |
| | | FlowMyBusiness business = flowMyBusinessService.getByDataId(flowTaskVo.getDataId()); |
| | |
| | | if (!doneUserList.contains(loginUser.getUsername())){ |
| | | doneUserList.add(loginUser.getUsername()); |
| | | } |
| | | business.setActStatus(ActStatus.reject) |
| | | business |
| | | .setTaskId(task2.getId()) |
| | | // .setActStatus(ActStatus.reject) |
| | | .setTaskNameId(task2.getTaskDefinitionKey()) |
| | | .setTaskName(task2.getName()) |
| | | .setDoneUsers(doneUserList.toJSONString()) |
| | |
| | | //**跳转到目标节点 |
| | | List<Task> task2List = taskService.createTaskQuery().processInstanceId(business.getProcessInstanceId()).active().list(); |
| | | Task targetTask = task2List.get(0); |
| | | business.setActStatus(ActStatus.reject) |
| | | business |
| | | .setTaskId(targetTask.getId()) |
| | | // .setActStatus(ActStatus.reject) |
| | | .setTaskNameId(targetTask.getTaskDefinitionKey()) |
| | | .setTaskName(targetTask.getName()) |
| | | .setPriority(targetTask.getPriority()+"") |
| | |
| | | flowTaskVo.setTaskId(business.getTaskId()); |
| | | return findReturnTaskList(flowTaskVo); |
| | | } |
| | | |
| | | /** |
| | | * 结束流程 |
| | | * @param processInstanceId 流程实例 ID |
| | | * @param deleteReason 定义删除原因 |
| | | */ |
| | | public Result<?> end(String processInstanceId, String deleteReason) { |
| | | try { |
| | | // 强制结束流程实例 |
| | | runtimeService.deleteProcessInstance(processInstanceId, deleteReason); |
| | | // 删除关联流程的业务 |
| | | FlowMyBusiness flowMyBusiness=flowMyBusinessService.getFlowMyBusiness(processInstanceId); |
| | | flowMyBusinessService.removeById(flowMyBusiness.getId()); |
| | | System.out.println("Process instance with ID " + processInstanceId + " has been forcefully ended."); |
| | | } catch (Exception e) { |
| | | System.err.println("Failed to force end process instance: " + e.getMessage()); |
| | | } |
| | | |
| | | // 关闭流程引擎 |
| | | processEngine.close(); |
| | | return Result.OK("流程已结束"); |
| | | } |
| | | |
| | | /** |
| | | * 获取所有可回退的节点 |
| | | * |
| | |
| | | String myTaskId = null; |
| | | HistoricTaskInstance myTask = null; |
| | | for (HistoricTaskInstance hti : htiList) { |
| | | if (loginUser.getUsername().toString().equals(hti.getAssignee())) { |
| | | if (loginUser.getUsername().equals(hti.getAssignee())) { |
| | | myTaskId = hti.getId(); |
| | | myTask = hti; |
| | | break; |
| | |
| | | |
| | | |
| | | return Result.OK(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 判断多个任务是否处于同一流程节点 |
| | | * @param taskIds 逗号分隔的任务ID字符串 |
| | | * @return 统一结果封装 |
| | | */ |
| | | @Override |
| | | public Result isSameNode(String taskIds) { |
| | | // 1. 参数校验 |
| | | if (StringUtils.isBlank(taskIds)) { |
| | | return Result.error("任务ID不能为空"); |
| | | } |
| | | |
| | | // 2. 分割任务ID |
| | | String[] taskIdArray = taskIds.split(","); |
| | | if (taskIdArray.length == 0) { |
| | | return Result.error("未提供有效的任务ID"); |
| | | } |
| | | |
| | | // 3. 单任务直接返回true |
| | | if (taskIdArray.length == 1) { |
| | | return Result.ok(true); |
| | | } |
| | | |
| | | // 4. 多任务检查逻辑 |
| | | String referenceNodeId = null; |
| | | String currentNodeId = null; |
| | | for (String taskId : taskIdArray) { |
| | | // 4.1 查询任务实例 |
| | | Task task = taskService.createTaskQuery() |
| | | .taskId(taskId.trim()) |
| | | .singleResult(); |
| | | |
| | | // 4.2 任务不存在处理 |
| | | if (task == null) { |
| | | return Result.error("任务不存在: " + taskId); |
| | | } |
| | | |
| | | // 4.3 获取节点标识(TaskDefinitionKey) |
| | | currentNodeId = task.getTaskDefinitionKey(); |
| | | |
| | | // 4.4 首次遍历初始化参考节点 |
| | | if (referenceNodeId == null) { |
| | | referenceNodeId = currentNodeId; |
| | | continue; |
| | | } |
| | | |
| | | // 4.5 节点不一致直接返回 |
| | | if (!referenceNodeId.equals(currentNodeId)) { |
| | | return Result.ok("节点不一致"); |
| | | } |
| | | } |
| | | |
| | | // 5. 所有任务节点一致 |
| | | return Result.ok(currentNodeId); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result finishedList(Integer pageNum, Integer pageSize) { |
| | | Page<FlowTaskDto> page = new Page<>(); |
| | | public Result finishedList(Integer pageNum, Integer pageSize, FlowMyBusinessDto flowMyBusinessDto) { |
| | | //修改查询,添加查询条件 |
| | | Page page = new Page(pageNum, pageSize); |
| | | String username = iFlowThirdService.getLoginUser().getUsername(); |
| | | HistoricTaskInstanceQuery taskInstanceQuery = historyService.createHistoricTaskInstanceQuery() |
| | | .includeProcessVariables() |
| | | .finished() |
| | | .taskAssignee(username) |
| | | .orderByHistoricTaskInstanceEndTime() |
| | | .desc(); |
| | | List<HistoricTaskInstance> historicTaskInstanceList = taskInstanceQuery.listPage((pageNum - 1)*pageSize, pageSize); |
| | | List<FlowTaskDto> hisTaskList = Lists.newArrayList(); |
| | | for (HistoricTaskInstance histTask : historicTaskInstanceList) { |
| | | FlowTaskDto flowTask = new FlowTaskDto(); |
| | | // 当前流程信息 |
| | | flowTask.setTaskId(histTask.getId()); |
| | | // 审批人员信息 |
| | | flowTask.setCreateTime(histTask.getCreateTime()); |
| | | flowTask.setFinishTime(histTask.getEndTime()); |
| | | flowTask.setDuration(getDate(histTask.getDurationInMillis())); |
| | | flowTask.setProcDefId(histTask.getProcessDefinitionId()); |
| | | flowTask.setTaskDefKey(histTask.getTaskDefinitionKey()); |
| | | flowTask.setTaskName(histTask.getName()); |
| | | flowTask.setDescription(flowMyBusinessService.getByProcessInstanceId(histTask.getProcessInstanceId()).getTitle()); |
| | | // 流程定义信息 |
| | | ProcessDefinition pd = repositoryService.createProcessDefinitionQuery() |
| | | .processDefinitionId(histTask.getProcessDefinitionId()) |
| | | .singleResult(); |
| | | flowTask.setDeployId(pd.getDeploymentId()); |
| | | flowTask.setProcDefName(pd.getName()); |
| | | flowTask.setProcDefVersion(pd.getVersion()); |
| | | flowTask.setProcInsId(histTask.getProcessInstanceId()); |
| | | flowTask.setHisProcInsId(histTask.getProcessInstanceId()); |
| | | |
| | | flowMyBusinessDto.setCurrentUser(username); |
| | | List<FlowTaskDto> list = flowMyBusinessService.ListMyBusiness(flowMyBusinessDto); |
| | | list.forEach(flowTaskDto -> { |
| | | // 流程发起人信息 |
| | | HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery() |
| | | .processInstanceId(histTask.getProcessInstanceId()) |
| | | .processInstanceId(flowTaskDto.getProcInsId()) |
| | | .singleResult(); |
| | | SysUser startUser = iFlowThirdService.getUserByUsername(historicProcessInstance.getStartUserId()); |
| | | flowTask.setStartUserId(startUser.getUsername()); |
| | | flowTask.setStartUserName(startUser.getRealname()); |
| | | if (startUser!=null){ |
| | | flowTaskDto.setStartUserId(startUser.getUsername()); |
| | | flowTaskDto.setStartUserName(startUser.getRealname()); |
| | | } |
| | | List<String> departNamesByUsername = iFlowThirdService.getDepartNamesByUsername(historicProcessInstance.getStartUserId()); |
| | | flowTask.setStartDeptName(CollUtil.join(departNamesByUsername,",")); |
| | | hisTaskList.add(flowTask); |
| | | } |
| | | page.setTotal(hisTaskList.size()); |
| | | page.setRecords(hisTaskList); |
| | | // Map<String, Object> result = new HashMap<>(); |
| | | // result.put("result",page); |
| | | // result.put("finished",true); |
| | | return Result.OK(page); |
| | | flowTaskDto.setStartDeptName(CollUtil.join(departNamesByUsername,",")); |
| | | if (flowTaskDto.getTodoUsers() == null){ |
| | | flowTaskDto.setTodoUsers(""); |
| | | }else { |
| | | //去除[] |
| | | flowTaskDto.setTodoUsers(flowTaskDto.getTodoUsers().replaceAll("\\[", "").replaceAll("\\]", "")); |
| | | flowTaskDto.setTodoUsers(flowTaskDto.getTodoUsers().replaceAll("\"", "")); |
| | | } |
| | | }); |
| | | IPage<FlowTaskDto> flowTaskDtoIPage = new Page<>(); |
| | | flowTaskDtoIPage.setRecords(list); |
| | | flowTaskDtoIPage.setTotal(page.getTotal()); |
| | | return Result.OK(flowTaskDtoIPage); |
| | | } |
| | | |
| | | private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { |