/** * 版权所有,侵权必究! */ package com.zt.modules.workflow.service; import cn.hutool.core.convert.Convert; import com.zt.common.entity.BaseEntity; import com.zt.common.entity.BusiEntity; import com.zt.common.entity.FlowInfo; import com.zt.common.service.BaseService; import com.zt.common.utils.UUIDUtil; import com.zt.core.context.UserContext; import com.zt.core.sys.model.SysUser; import com.zt.modules.workflow.dao.WfRunTaskDao; import com.zt.modules.workflow.dto.BizInfoDto; import com.zt.modules.workflow.dto.QueryWfInfoDto; import com.zt.modules.workflow.dto.TaskParamDto; import com.zt.modules.workflow.model.WfRunInstance; import com.zt.modules.workflow.model.WfRunTask; import com.zt.modules.workflowconfig.dao.WfDefDao; import com.zt.modules.workflowconfig.dao.WfDefStepDao; import com.zt.modules.workflowconfig.model.WfDef; import com.zt.modules.workflowconfig.model.WfDefStep; import com.zt.modules.workflowconfig.service.WorkflowConfigService; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; import java.util.stream.Collectors; /** * 工作流服务 * * @author 朱曙光 * @since 1.0.0 */ @Service public class WorkflowService extends BaseService { @Autowired private WfDefStepDao wfDefStepDao; @Autowired private WfDefDao wfDefDao; @Autowired private WorkflowConfigService workflowConfigService; @Autowired private WfRunInstanceService wfRunInstanceService; @Value("${zt.oss.local-server}") private String localServer; public QueryWfInfoDto queryWfStepInfo(String wfCode, String wfStepId) { QueryWfInfoDto dto = new QueryWfInfoDto(); dto.setIsMyStep(0); dto.setIsFirstStep(0); dto.setIsFinallyStep(0); WfDefStep firstStep = this.queryWfDefFirstStep(wfCode); WfDefStep finallyStep = this.queryWfDefFinallyStep(wfCode); WfDefStep currentStep; if (wfStepId == null) { currentStep = firstStep; } else { currentStep = wfDefStepDao.selectById(Convert.toLong(wfStepId)); } if (currentStep == null) currentStep = firstStep; dto.setWfStepId(currentStep.getId()); dto.setIsCounterSign(currentStep.getIsCounterSign()); dto.setCanRefuse(currentStep.getCanRefuse()); dto.setStepName(currentStep.getName()); dto.setApproverIds(currentStep.getApproverIds()); if (firstStep.getId() == currentStep.getId()) { dto.setIsFirstStep(1); } if (finallyStep.getId() == currentStep.getId()) { dto.setIsFinallyStep(1); } if (("," + currentStep.getApproverIds() + ",").contains("," + UserContext.getUserId().toString() + ",")) { dto.setIsMyStep(1); } return dto; } public WfDefStep queryWfDefStep(String wfIdCode, String stepIdMarker) { List list = wfDefStepDao.queryWfDefStep(wfIdCode, stepIdMarker); if (list.isEmpty()) { return null; } else { WfDefStep step = list.get(0); // step.setCurrentUserId(UserContext.getUserId().toString()); return step; } } public WfDefStep queryWfDefFirstStep(String wfIdCode) { List list = wfDefStepDao.queryWfDefFirstStep(wfIdCode); if (list.isEmpty()) { return null; } else { WfDefStep step = list.get(0); // step.setCurrentUserId(UserContext.getUserId().toString()); return step; } } public WfDefStep queryWfDefFinallyStep(String wfIdCode) { List list = wfDefStepDao.queryWfDefFinallyStep(wfIdCode); if (list.isEmpty()) { return null; } else { WfDefStep step = list.get(0); // step.setCurrentUserId(UserContext.getUserId().toString()); return step; } } public WfDefStep queryWfDefNextStep(WfDefStep currentStep) { if (currentStep == null) { throw new RuntimeException("Null WfDefStep object."); } List list = wfDefStepDao.queryWfDefNextStep(currentStep); if (list.isEmpty()) { } else { return list.get(0); } return null; } public WfDefStep queryWfDefPreStep(WfDefStep currentStep) { if (currentStep == null) { throw new RuntimeException("Null WfDefStep object."); } List list = wfDefStepDao.queryWfDefPreStep(currentStep); if (list.isEmpty()) { } else { return list.get(0); } return null; } public WfDefStep queryCurrentTaskStep(String wfIdCode, Long bizId) { List list = baseDao.queryCurrentTaskStep(wfIdCode, bizId); if (list.size() > 0) return list.get(0); else return null; } public WfDefStep queryCurrentTaskStep100(String wfIdCode, Long bizId) { List list = baseDao.queryCurrentTaskStep100(wfIdCode, bizId); if (list.size() > 0) return list.get(0); else return null; } public Boolean isFlowFinish(String wfIdCode, Long bizId) { Boolean result = true; String[] wfIdCodeArr = wfIdCode.split(","); for (String code : wfIdCodeArr) { List list = baseDao.getFlowFinish(wfIdCode, bizId); if (list.size() == 0) { result = false; break; } } return result; } // 按task分組設置上一步狀體,並獲取設置狀態后縂的步驟狀態 public Boolean setMyStepStatus(Long prevStepId, Long currentStepId, Integer status, Long bizId, Integer option) { Boolean result = false; WfDefStep prevStep = workflowConfigService.get(prevStepId); if (prevStep != null) { String taskGroup = prevStep.getTaskGroup(); status = status == 0 ? 20 : 10; // (0:20 pass, 1:10 refuse) // 设置状态 本人status 别人status+1 taskGroup baseDao.setTaskStatus(bizId, prevStepId, status, UserContext.getUserId(), UserContext.getUser().getRealName(), taskGroup, option); if (prevStep.getStepType() != 0 && currentStepId == 0L) return false; if (option == 1) { // 1 refuse 2 pass 已办任务不显示 施工清单已办 // 设置状态 是否是驳回的(撤回?) // 如果是駁回的,則後面的步驟都設置 status2 =1, 查詢狀態或待辦任務的時候要剔出 baseDao.setTaskRefuse(bizId, currentStepId, 1); } if (status == 10) { // 驳回肯定执行(如果是按专业、部门分组的话,都驳回) result = true; } else { // 前进的话检查,是否所有的都完成了(taskGroup) result = this.getFlowStepFinish(prevStep.getWfDefId().toString(), bizId, prevStep.getStepMarker()); } } return result; } public void setTaskPartFinish(Long bizId, Long currentStepId, Integer status) { baseDao.setTaskPartFinish(bizId, currentStepId, status); } public void setTaskPartFinish2(Long bizId, Long receiveId, Integer status) { baseDao.setTaskPartFinish2(bizId, receiveId, status); } public void setTaskPartFinish3(Long bizId, Long receiveDeptId, Integer status) { baseDao.setTaskPartFinish3(bizId, receiveDeptId, status); } public void setFlowTask(Long prevStepId, Long currentStepId, Integer status, Long bizId, String deptIds, Integer direct) { Boolean result = true; if (prevStepId > 0) { // 设置上一步骤的分組状态,並獲取上一步骤縂的狀態 result = setMyStepStatus(prevStepId, currentStepId, status, bizId, direct); } if (result) { /* * int num = baseDao.isExistsStep(null, currentStepId, bizId); if (num == 0) { * setNextFlowTask(prevStepId, currentStepId, status, bizId, deptIds); } */ setNextFlowTask(prevStepId, currentStepId, status, bizId, deptIds); } } public int isExistsStep(String currentStepMarker, Long currentStepId, Long bizId) { int num = baseDao.isExistsStep(currentStepMarker, currentStepId, bizId); System.out.println("num:" + num); return num; } public void setNextFlowTask(Long prevStepId, Long currentStepId, Integer status, Long bizId, String deptIds) { int num = baseDao.isExistsRunningStep(null, currentStepId, bizId); if (num > 0) { // 已經存在該步驟了,不用繼續 return; } WfDefStep currentStep = workflowConfigService.get(currentStepId); WfDefStep finallyStep = this.queryWfDefFinallyStep(currentStep.getWfDefId().toString()); WfDef wfDef = wfDefDao.queryWfDef(currentStep.getWfDefId(), null).get(0); Long instanceId = UUIDUtil.generateId(); WfRunInstance wfRunInstanceOld = wfRunInstanceService.getFlowInstance(wfDef.getCode(), bizId); Boolean isNewInstance = true; if (wfRunInstanceOld != null) { isNewInstance = false; instanceId = wfRunInstanceOld.getId(); } String sqlStr = wfDef.getBizSql(); sqlStr = sqlStr.replace("${bizId}", bizId.toString()); List bizInfoList = baseDao.getSqlResult(sqlStr); BizInfoDto bizInfoDto = bizInfoList.size() > 0 ? bizInfoList.get(0) : null; Long preTaskId = 0L; WfRunTask task = getFlowStepStatus(wfDef.getCode(), bizId, currentStep.getStepMarker()); if (task != null) preTaskId = task.getId(); // sqlStr = "update project set subject='aaa' where id=${bizId}"; // sqlStr = sqlStr.replace("${bizId}",bizId.toString()); // baseDao.exeSql(sqlStr); Map existsUsers = new HashMap(); TaskParamDto taskParamDto = new TaskParamDto(); taskParamDto.setInstanceId(instanceId); taskParamDto.setPreTaskId(preTaskId); taskParamDto.setPrevStepId(prevStepId); taskParamDto.setBizId(bizId); taskParamDto.setStatus(status); taskParamDto.setStatus2(0); taskParamDto.setGroupId(UUIDUtil.generateId()); taskParamDto.setGroupId2(null); taskParamDto.setEventDate(new Date()); taskParamDto.setWfDef(wfDef); taskParamDto.setCurrentStep(currentStep); taskParamDto.setBizInfoDto(bizInfoDto); if (finallyStep.getStepNo() > currentStep.getStepNo()) { Integer isSelfDept = currentStep.getIsSelfDept(); if (isSelfDept > 0 && bizInfoDto!=null) { deptIds = bizInfoDto.getDeptIds(); } if (isSelfDept == 0) deptIds = null; String bizGroupId = bizInfoList.get(0).getBizId(); if ("br".equals(currentStep.getTaskGroup())) { List listUser = baseDao.getAssignUser(wfDef.getCode(), currentStep.getStepMarker(), bizId); for (SysUser user : listUser) { if (existsUsers.containsKey(user.getId())) { continue; } existsUsers.put(user.getId(), ""); taskParamDto.setUser(user); taskParamDto.setGroupId2(user.getId()); insertFlowTaskData(taskParamDto); } } else if (StringUtils.isNotBlank(currentStep.getTaskGroup()) && "pall,zrr,csr,shr,pzr".contains(currentStep.getTaskGroup())) { String userList = null; if ("zrr".equals(currentStep.getTaskGroup())) { userList = bizInfoDto.getZrr(); } if ("csr".equals(currentStep.getTaskGroup())) { userList = bizInfoDto.getCsr(); } if ("shr".equals(currentStep.getTaskGroup())) { userList = bizInfoDto.getShr(); } if ("pzr".equals(currentStep.getTaskGroup())) { userList = bizInfoDto.getPzr(); } if ("pall".equals(currentStep.getTaskGroup())) { userList = bizInfoDto.getPall(); } if (userList != null) { String[] userArr = userList.split(","); Long groupId2 = UUIDUtil.generateId(); for (String userStr : userArr) { Long userId = Convert.toLong(userStr); SysUser user = baseDao.getUserById(userId); existsUsers.put(userId, ""); taskParamDto.setUser(user); taskParamDto.setGroupId2(groupId2); insertFlowTaskData(taskParamDto); } } } else if ("sqr".equals(currentStep.getTaskGroup())) { SysUser user = baseDao.getUserById(bizInfoList.get(0).getApplyUserId()); existsUsers.put(user.getId(), ""); taskParamDto.setUser(user); taskParamDto.setGroupId2(user.getId()); insertFlowTaskData(taskParamDto); } else { String roleIds = currentStep.getApproverRoleIds(); if (StringUtils.isBlank(roleIds)) { throw new RuntimeException("没有设置下一步流程角色!"); } String[] roleIdArr = roleIds.split(","); for (String roleId : roleIdArr) { if (StringUtils.isBlank(roleId)) continue; List listUser = baseDao.getTaskUser(roleId, deptIds); for (SysUser user : listUser) { if (existsUsers.containsKey(user.getId())) { continue; } existsUsers.put(user.getId(), ""); taskParamDto.setUser(user); taskParamDto.setGroupId2(Convert.toLong(roleId)); insertFlowTaskData(taskParamDto); } } } if (existsUsers.size() == 0) { throw new RuntimeException("没有符合(" + wfDef.getName() + ")流程的下一步(" + currentStep.getName() + ")执行人!"); } WfDefStep prevStep = workflowConfigService.get(prevStepId); if (prevStep!=null) { String updateSql = prevStep.getUpdateSql(); if (org.apache.commons.lang3.StringUtils.isNotBlank(updateSql)) { updateSql = updateSql.replace("${bizId}", bizId.toString()); updateSql = updateSql.replace("${bizId}", bizId.toString()); updateSql = updateSql.replace("${nickName}", UserContext.getUser().getRealName()); updateSql = updateSql.replace("${userId}", UserContext.getUser().getId().toString()); baseDao.exeSql(updateSql); } } } else { taskParamDto.setUser(null); taskParamDto.setGroupId2(null); status = 100; taskParamDto.setFinishTime(new Date()); taskParamDto.setStatus(status); insertFlowTaskData(taskParamDto); String updateSql = currentStep.getUpdateSql(); if (org.apache.commons.lang3.StringUtils.isNotBlank(updateSql)) { updateSql = updateSql.replace("${bizId}", bizId.toString()); updateSql = updateSql.replace("${bizId}", bizId.toString()); updateSql = updateSql.replace("${nickName}", UserContext.getUser().getRealName()); updateSql = updateSql.replace("${userId}", UserContext.getUser().getId().toString()); baseDao.exeSql(updateSql); } WfDefStep prevStep = workflowConfigService.get(prevStepId); if (prevStep!=null) { updateSql = prevStep.getUpdateSql(); if (org.apache.commons.lang3.StringUtils.isNotBlank(updateSql)) { updateSql = updateSql.replace("${bizId}", bizId.toString()); updateSql = updateSql.replace("${bizId}", bizId.toString()); updateSql = updateSql.replace("${nickName}", UserContext.getUser().getRealName()); updateSql = updateSql.replace("${userId}", UserContext.getUser().getId().toString()); baseDao.exeSql(updateSql); } } if (!StringUtils.isBlank(currentStep.getNextFlows())) { boolean canNext = true; if (!StringUtils.isBlank(currentStep.getPreFlows())) { if (!this.isFlowFinish(currentStep.getPreFlows(), bizId)) { canNext = false; } } if (canNext) { String[] nextFlowArr = currentStep.getNextFlows().split(","); for (String nextFlowCode : nextFlowArr) { this.startFlow(nextFlowCode, bizId); } } } } if (currentStep.getStepType() == 0) { WfRunInstance wfRunInstance = new WfRunInstance(); wfRunInstance.setFlowId(wfDef.getId()); wfRunInstance.setFlowCode(wfDef.getCode()); wfRunInstance.setBizId(bizId); wfRunInstance.setRunStatus(1); wfRunInstance.setHasOut(0); wfRunInstance.setStepSite(currentStep.getStepSite()); wfRunInstance.setStepId(currentStep.getStepId()); wfRunInstance.setStepMarker(currentStep.getStepMarker()); wfRunInstance.setStepName(currentStep.getName()); wfRunInstance.setStatus(status); wfRunInstance.setPrevId(preTaskId); wfRunInstance.setPrevStepId(prevStepId); wfRunInstance.setSenderId(UserContext.getUser().getId()); wfRunInstance.setSenderName(UserContext.getUser().getRealName()); wfRunInstance.setSenderTime(taskParamDto.getEventDate()); String param = bizInfoDto == null ? "" : bizInfoDto.getParam(); param = "".equals(param) ? "" : " (" + param + ")"; wfRunInstance.setRemark(wfDef.getRemark() + param); wfRunInstance.setTitle(wfDef.getName()); if (bizInfoList.size() > 0) { wfRunInstance.setBizGroupId(taskParamDto.getBizInfoDto().getBizGroupId()); wfRunInstance.setApplyUserId(taskParamDto.getBizInfoDto().getApplyUserId()); wfRunInstance.setApplyUser(taskParamDto.getBizInfoDto().getApplyUser()); wfRunInstance.setApplyTime(taskParamDto.getBizInfoDto().getApplyTime()); wfRunInstance.setTopic(taskParamDto.getBizInfoDto().getTopic()); } if (isNewInstance) { // wfRunInstance = new WfRunInstance(); wfRunInstance.setId(instanceId); wfRunInstanceService.insert(wfRunInstance); } else { wfRunInstance.setId(wfRunInstanceOld.getId()); wfRunInstanceService.update(wfRunInstance); } } WfDefStep prevStep = workflowConfigService.get(prevStepId); if (prevStep != null && currentStep.getStepType() == 0) { if (prevStep.getCopyTo() != null) { String[] copyStepArr = prevStep.getCopyTo().split(","); for (String stepMarker : copyStepArr) { WfDefStep wfDefStep = this.queryWfDefStep(null, stepMarker); if (wfDefStep != null) setNextFlowTask(prevStep.getId(), wfDefStep.getId(), 0, bizId, null); } } } } public void insertFlowTaskData(TaskParamDto taskParamDto) { WfRunTask wfRunTask = new WfRunTask(); wfRunTask.setInstanceId(taskParamDto.getInstanceId()); wfRunTask.setPrevId(taskParamDto.getPreTaskId()); wfRunTask.setPrevStepId(taskParamDto.getPrevStepId()); wfRunTask.setFlowCode(taskParamDto.getWfDef().getCode()); wfRunTask.setFlowId(taskParamDto.getCurrentStep().getWfDefId()); wfRunTask.setStepId(taskParamDto.getCurrentStep().getId()); wfRunTask.setStepName(taskParamDto.getCurrentStep().getName()); wfRunTask.setStepMarker(taskParamDto.getCurrentStep().getStepMarker()); wfRunTask.setHasOut(0); wfRunTask.setStepSite(taskParamDto.getCurrentStep().getStepSite()); wfRunTask.setBizId(taskParamDto.getBizId()); wfRunTask.setGroupId(taskParamDto.getGroupId()); wfRunTask.setGroupId2(taskParamDto.getGroupId2()); wfRunTask.setSenderId(UserContext.getUser().getId()); wfRunTask.setSenderName(UserContext.getUser().getRealName()); wfRunTask.setSenderTime(taskParamDto.getEventDate()); if (taskParamDto.getUser() != null) { wfRunTask.setReceiveId(taskParamDto.getUser().getId()); wfRunTask.setReceiveName(taskParamDto.getUser().getRealName()); } wfRunTask.setReceiveTime(taskParamDto.getEventDate()); wfRunTask.setOpenTime(null); wfRunTask.setFinishTime(null); wfRunTask.setFinishTime2(null); wfRunTask.setStatus(taskParamDto.getStatus()); String param = ""; if (taskParamDto.getBizInfoDto() != null) { param = taskParamDto.getBizInfoDto().getParam(); wfRunTask.setBizGroupId(taskParamDto.getBizInfoDto().getBizGroupId()); wfRunTask.setApplyUserId(taskParamDto.getBizInfoDto().getApplyUserId()); wfRunTask.setApplyUser(taskParamDto.getBizInfoDto().getApplyUser()); wfRunTask.setApplyTime(taskParamDto.getBizInfoDto().getApplyTime()); wfRunTask.setTopic(taskParamDto.getBizInfoDto().getTopic()); } if (StringUtils.isBlank(taskParamDto.getCurrentStep().getStepTitle())) { wfRunTask.setTitle(taskParamDto.getWfDef().getName()); } else { wfRunTask.setTitle(taskParamDto.getCurrentStep().getStepTitle()); } param = "".equals(param) ? "" : " (" + param + ")"; wfRunTask.setRemark(taskParamDto.getWfDef().getRemark() + param); baseDao.insert(wfRunTask); } @Transactional(rollbackFor = Exception.class) public Boolean writeFlowTask(WfRunTask wfRunTask) { Date eventDate = new Date(); Long groupId = UUIDUtil.generateId(); // wfRunTask.setPrevId(0L); /// wfRunTask.setPrevStepId(0L); // wfRunTask.setFlowId(currentStep.getWfDefId()); // wfRunTask.setFlowCode(wfDef.get(0).getCode()); // wfRunTask.setStepId(currentStep.getId()); // wfRunTask.setStepName(currentStep.getName()); // wfRunTask.setStepMarker(currentStep.getStepMarker()); // wfRunTask.setBizId(bizId); // wfRunTask.setTitle(currentStep.getStepTitle()); wfRunTask.setSenderId(UserContext.getUser().getId()); wfRunTask.setSenderName(UserContext.getUser().getRealName()); wfRunTask.setSenderTime(eventDate); wfRunTask.setGroupId(groupId); wfRunTask.setGroupId2(groupId); // wfRunTask.setReceiveId(UserContext.getUser().getId()); // wfRunTask.setReceiveName(UserContext.getUser().getRealName()); wfRunTask.setReceiveTime(eventDate); wfRunTask.setOpenTime(null); wfRunTask.setFinishTime(null); wfRunTask.setFinishTime2(null); // wfRunTask.setStatus(100); wfRunTask.setApplyUserId(UserContext.getUser().getId()); wfRunTask.setApplyUser(UserContext.getUser().getRealName()); wfRunTask.setApplyTime(eventDate); baseDao.insert(wfRunTask); return true; } @Transactional(rollbackFor = Exception.class) public Boolean startFlow(String wfCode, Long bizId) { if (hasExistFlow(wfCode, bizId)) { return false; } WfDefStep firstStep = this.queryWfDefFirstStep(wfCode); if (firstStep == null) { throw new RuntimeException("没有定义流程" + wfCode); } // this.createFlowInstance(wfCode,bizId); this.setFlowTask(0L, firstStep.getId(), 0, bizId, null, 0); return true; } public void createFlowInstance(String wfCode, Long bizId) { WfDef wfDef = wfDefDao.queryWfDef(null, wfCode).get(0); WfRunInstance wfRunInstance = new WfRunInstance(); wfRunInstance.setFlowId(wfDef.getId()); wfRunInstance.setFlowCode(wfDef.getCode()); wfRunInstance.setBizId(bizId); wfRunInstanceService.insert(wfRunInstance); } public void deleteFlowTask(String wfIdCodes, Long bizId) { baseDao.deleteFlowTask(wfIdCodes, bizId); } @Transactional(rollbackFor = Exception.class) public void approvePass(String wfIdCode, Long bizId, String stepIdMark) { WfDefStep taskDbStep = this.queryCurrentTaskStep(wfIdCode, bizId); if (taskDbStep == null) { throw new RuntimeException("没有当前任务"); } WfDefStep currentStep = this.queryWfDefStep(wfIdCode, stepIdMark); if (currentStep == null) { throw new RuntimeException("该流程没有该步骤"); } WfDefStep endStep = this.queryWfDefFinallyStep(wfIdCode); if (currentStep.getStepNo() == endStep.getStepNo()) { throw new RuntimeException("已经完成了,不能继续提交"); } if (taskDbStep.getStepNo() != currentStep.getStepNo()) { throw new RuntimeException("其他用户已经提交"); } else { Long nextStepId = 0L; if (currentStep.getStepType() == 0) { WfDefStep nextStep = this.queryWfDefNextStep(currentStep); nextStepId = nextStep.getId(); } this.setFlowTask(currentStep.getId(), nextStepId, 0, bizId, null, 2); } } @Transactional(rollbackFor = Exception.class) public void approvePass2(String wfIdCode, Long bizId, String stepIdMark, String nextStepIdMark) { WfDefStep currentStep = this.queryWfDefStep(wfIdCode, stepIdMark); if (currentStep == null) { throw new RuntimeException("该流程没有该当前步骤"); } Long nextStepId = 0L; if (currentStep.getStepType() == 0) { WfDefStep nextStep = this.queryWfDefStep(wfIdCode, nextStepIdMark); nextStepId = nextStep.getId(); if (currentStep == null) { throw new RuntimeException("该流程没有指定下一步步骤"); } } WfDefStep endStep = this.queryWfDefFinallyStep(wfIdCode); if (currentStep.getStepNo() == endStep.getStepNo()) { throw new RuntimeException("已经完成了,不能继续提交"); } this.setFlowTask(currentStep.getId(), nextStepId, 0, bizId, null, 2); } @Transactional(rollbackFor = Exception.class) public void approveDirectPass(String wfIdCode, Long bizId, String stepIdMark, String nextStepIdMark) { WfDefStep currentStep = this.queryWfDefStep(wfIdCode, stepIdMark); if (currentStep == null) { throw new RuntimeException("该流程没有该当前步骤"); } Long nextStepId = 0L; if (currentStep.getStepType() == 0) { WfDefStep nextStep = this.queryWfDefStep(wfIdCode, nextStepIdMark); nextStepId = nextStep.getId(); if (currentStep == null) { throw new RuntimeException("该流程没有指定下一步步骤"); } } WfDefStep endStep = this.queryWfDefFinallyStep(wfIdCode); if (currentStep.getStepNo() == endStep.getStepNo()) { throw new RuntimeException("已经完成了,不能继续提交"); } this.setNextFlowTask(currentStep.getId(), nextStepId, 0, bizId, null); } @Transactional(rollbackFor = Exception.class) // 驳回到上一步 public void approveRefuse(String wfIdCode, Long bizId, String stepIdMark) { WfDefStep taskDbStep = this.queryCurrentTaskStep(wfIdCode, bizId); if (taskDbStep == null) { throw new RuntimeException("没有当前任务"); } WfDefStep currentStep = this.queryWfDefStep(wfIdCode, stepIdMark); if (currentStep == null) { throw new RuntimeException("该流程没有该步骤"); } WfDefStep firstStep = this.queryWfDefFirstStep(wfIdCode); if (currentStep.getStepNo() == firstStep.getStepNo()) { throw new RuntimeException("已经第一步了,不能驳回"); } if (taskDbStep.getStepNo() != currentStep.getStepNo()) { throw new RuntimeException("其他用户已经提交"); } else { WfDefStep preStep = this.queryWfDefPreStep(currentStep); this.setFlowTask(currentStep.getId(), preStep.getId(), 1, bizId, null, 1); } } @Transactional(rollbackFor = Exception.class) // 驳回到指定步骤 public void approveRefuse2(String wfIdCode, Long bizId, String stepIdMark, String prevStepIdMark) { WfDefStep taskDbStep = this.queryCurrentTaskStep100(wfIdCode, bizId); if (taskDbStep == null) { throw new RuntimeException("没有当前任务"); } WfDefStep currentStep = this.queryWfDefStep(wfIdCode, stepIdMark); if (currentStep == null) { throw new RuntimeException("该流程没有该当前步骤"); } WfDefStep firstStep = this.queryWfDefFirstStep(wfIdCode); if (currentStep.getStepNo() == firstStep.getStepNo()) { throw new RuntimeException("已经第一步了,不能驳回"); } WfDefStep preStep = this.queryWfDefStep(wfIdCode, prevStepIdMark); if (preStep == null) { throw new RuntimeException("该流程没有指定前一步步骤"); } this.setFlowTask(currentStep.getId(), preStep.getId(), 1, bizId, null, 1); } @Transactional(rollbackFor = Exception.class) // 重新从某一步开始 public void reStartFlow(String wfIdCode, Long bizId, String stepIdMark, String prevStepIdMark) { WfDefStep taskDbStep = this.queryCurrentTaskStep100(wfIdCode, bizId); if (taskDbStep == null) { throw new RuntimeException("没有当前任务"); } WfDefStep currentStep = this.queryWfDefStep(wfIdCode, stepIdMark); if (currentStep == null) { throw new RuntimeException("该流程没有该当前步骤"); } WfDefStep firstStep = this.queryWfDefFirstStep(wfIdCode); if (currentStep.getStepNo() == firstStep.getStepNo()) { throw new RuntimeException("已经第一步了,不能驳回"); } WfDefStep preStep = this.queryWfDefStep(wfIdCode, prevStepIdMark); if (preStep == null) { throw new RuntimeException("该流程没有指定前一步步骤"); } baseDao.setReStartStatus(wfIdCode, bizId); this.setFlowTask(currentStep.getId(), preStep.getId(), 0, bizId, null, 1); } public Map getSingleFlowStatus(String wfIdCodes, Long bizId) { int lastStepStatus = 100; Map result = new HashMap<>(); if ("yearPlanFlow".equals(wfIdCodes)) { WfRunTask item_zl = new WfRunTask(); WfRunTask item_cz = new WfRunTask(); int count = baseDao.getRoleCount(UserContext.getUser().getId(), "zlbm"); if (count > 1) { item_zl.setStatus(1); item_cz.setStatus(0); } else { item_zl.setStatus(20); item_cz.setStatus(1); } result.put("yearPlanFlow_zl", item_zl); result.put("yearPlanFlow_cz", item_cz); } else if ("yearPlanFlow2".equals(wfIdCodes)) { WfRunTask item_zl = new WfRunTask(); WfRunTask item_cz = new WfRunTask(); int status = baseDao.getPlanStatus(bizId); if (status == 10) { item_zl.setStatus(20); item_cz.setStatus(20); } else if (status == 5) { item_zl.setStatus(20); item_cz.setStatus(1); } else { item_zl.setStatus(1); item_cz.setStatus(0); } result.put("yearPlanFlow_zl", item_zl); result.put("yearPlanFlow_cz", item_cz); } else { List list = baseDao.getSingleFlowStatus(wfIdCodes, bizId); for (WfRunTask item : list) { lastStepStatus = checkStaus(lastStepStatus, item.getStatus(), item); item.setStatus(lastStepStatus); result.put(item.getStepMarker(), item); } } return result; } public int checkStaus(int lastStepStatus, int currentStepStatus, WfRunTask item) { if (currentStepStatus > lastStepStatus && !"jzjys_sc".equals(item.getStepMarker())) { return 0; } else { return currentStepStatus; } } public Map getFlowStatus(String wfIdCodes, Long bizId) { int lastStepStatus = 0; String lastflowCode = ""; Map result = new HashMap<>(); List list = baseDao.getFlowStatus(wfIdCodes, bizId); for (WfRunTask item : list) { if (!lastflowCode.equals(item.getFlowCode())) { lastflowCode = item.getFlowCode(); lastStepStatus = item.getStatus(); result.put(item.getStepMarker(), item); } else { lastStepStatus = checkStaus(lastStepStatus, item.getStatus(), item); item.setStatus(lastStepStatus); result.put(item.getStepMarker(), item); } } List list2 = baseDao.getFlowStatus2(wfIdCodes, bizId); for (WfRunTask item : list2) { if (!lastflowCode.equals(item.getFlowCode())) { lastflowCode = item.getFlowCode(); lastStepStatus = item.getStatus(); result.put(item.getStepMarker(), item); } else { lastStepStatus = checkStaus(lastStepStatus, item.getStatus(), item); item.setStatus(lastStepStatus); result.put(item.getStepMarker(), item); } } return result; } public Map getFlowStatus2(String wfIdCodes, Long bizId) { Map result = new HashMap<>(); List list = baseDao.getFlowStatus3(wfIdCodes, bizId); for (WfRunTask item : list) { result.put(item.getFlowCode(), item); } return result; } // 弃用 public Map getPlanFlowStatus(Long bizId) { Map result = new HashMap<>(); /* * List list = baseDao.getPLanFlowStatus(bizId); for (WfRunTask item * : list) { result.put(item.getFlowCode(), item.getStatus()); } */ return result; } // 准备弃用 public Map getPlanFlowStatus2(Long bizId) { Map result = new HashMap<>(); /* * List list = baseDao.getPLanFlowStatus(bizId); for (WfRunTask item * : list) { if ("planFlow".equals(item.getFlowCode())) * result.put("gcjd_plan_tybx", item); else if * ("sgmxFlow".equals(item.getFlowCode())) result.put("gcjd_sgmx_bb", item); * else if ("sgqdFlow".equals(item.getFlowCode())) result.put("gcjd_sgqd_cz", * item); else if ("gckyFlow".equals(item.getFlowCode())) * result.put("gcjd_gcky_bb", item); else if * ("gywjFlow".equals(item.getFlowCode())) result.put("gcjd_gywj_sc", item); * else if ("jzjFlow".equals(item.getFlowCode())) result.put("gcjd_jzj_bb", * item); else if ("wltFlow".equals(item.getFlowCode())) * result.put("gcjd_wlt_pz", item); else if * ("wxsbFlow".equals(item.getFlowCode())) result.put("gcjd_wxsb_wxsb", item); * else if ("jsfaFlow".equals(item.getFlowCode())) result.put("gcjd_jsfa_sc", * item); else if ("yxscFlow".equals(item.getFlowCode())) * result.put("gcjd_yxsc_bb", item); } */ return result; } public Map getPhaseFlowStatus(String wfIdCode, Long bizId) { Map result = new HashMap<>(); List list = baseDao.getPhaseFlowStatus(wfIdCode, bizId); for (WfRunTask item : list) { result.put(item.getFlowCode(), item); } return result; } public Boolean getFlowStepFinish(String wfIdCode, Long bizId, String stepIdMark) { Integer finishNum = baseDao.getFlowStepFinish(wfIdCode, bizId, stepIdMark); if (finishNum > 0) return false; else return true; } public Boolean getFlowStepFinish2(String wfIdCode, Long bizId, String stepIdMark) { Integer finishNum = baseDao.getFlowStepFinish2(wfIdCode, bizId, stepIdMark); if (finishNum > 0) return true; else return false; } public Boolean isMyStepFinish(String wfIdCode, Long bizId, String stepIdMark) { Integer finishNum = baseDao.isMyStepFinish(wfIdCode, bizId, stepIdMark, UserContext.getUser().getId()); if (finishNum > 0) { // 我的完成了 return true; } else { // 我的未完成了 return false; } } /* * myStatus 0: 不是我的步骤 1: 是我的步骤,当前正需要我执行 2:其他情况 5:步骤执行过了 */ public WfRunTask getFlowStepStatus(String wfIdCode, Long bizId, String stepIdMark) { WfRunTask result = null; WfDefStep requestStep = this.queryWfDefStep(wfIdCode, stepIdMark); if (requestStep != null) { List list = baseDao.getFlowStepStatus(wfIdCode, bizId); if (list.size() > 0) { result = list.get(0); result.setMyStatus(0); WfDefStep currentStep = this.queryWfDefStep(wfIdCode, list.get(0).getStepMarker()); Integer myStep = baseDao.isMyStep(wfIdCode, bizId, requestStep.getStepMarker(), UserContext.getUser().getId()); if (myStep > 0) result.setMyStatus(1); else if (currentStep.getStepNo() > requestStep.getStepNo()) { result.setMyStatus(5); // 执行过了 } else if (currentStep.getStepNo() == requestStep.getStepNo()) { result.setMyStatus(2); } } } return result; } public WfRunTask getFlowStepStatus2(String wfIdCode, Long bizId, String stepIdMark) { List list = baseDao.getFlowStepStatus(wfIdCode, bizId); WfRunTask result = null; if (list.size() > 0) { result = list.get(0); result.setMyStatus(0); if (result.getStatus() == 0 || result.getStatus() == 1) { Integer myStep = baseDao.isMyStep(wfIdCode, bizId, result.getStepMarker(), UserContext.getUser().getId()); if (myStep > 0) result.setMyStatus(1); else result.setMyStatus(2); } } return result; } public List> getStepCount(String wfIdCode, Long bizId) { List list = baseDao.getStepCount(wfIdCode, bizId); List> result = new ArrayList<>(); Map map = new HashMap<>(); map.put("name", "数量"); for (WfRunTask item : list) { map.put(item.getStepMarker(), item.getStatus()); } result.add(map); return result; } public Boolean hasExistFlow(String wfIdCode, Long bizId) { Integer num = baseDao.getBizTaskCount(wfIdCode, bizId); return num > 0 ? true : false; } public String recallFlow(String wfIdCode, Long bizId, String stepMarker, Long taskId, Long taskPrevId) { WfRunTask task = getFlowStepStatus(wfIdCode, bizId, stepMarker); if (task != null) { if (task.getStatus() == 0 || task.getStatus() == 1) { if (localServer.equals(task.getStepSite()) || localServer.equals("yjs") || task.getHasOut() == 0) { baseDao.setTaskRecall(taskId, wfIdCode); WfRunTask task2 = baseDao.selectById(taskId); if (task2 != null) { Long stepId = task2.getStepId(); WfDefStep wfDefStep = workflowConfigService.get(stepId); this.refuseExecSql(wfDefStep, bizId); } return "OK"; } else { return "已经导出到其他服务器执行,不能撤回了"; } } } return "任务已经执行完毕,不能撤回了"; } public void refuseExecSql(WfDefStep wfDefStep, Long bizId) { if (wfDefStep != null) { String sqlStr = wfDefStep.getRecallSql(); if (StringUtils.isNotEmpty(sqlStr)) { sqlStr = sqlStr.replace("${bizId}", bizId.toString()); baseDao.exeSql(sqlStr); } } } public Map> getAllSteps() { Map bean = new HashMap<>(); bean.put("status", ""); bean.put("receiveName", ""); Map> result = new HashMap<>(); List list = wfDefStepDao.getAllSteps(); for (String item : list) { result.put(item, bean); } return result; } public List getConnectInformation(String oldShipTeam) { return baseDao.getConnectInformation(oldShipTeam); } public void updateConnect(String oldShipTeam) { baseDao.updateConnect(oldShipTeam); } public List getFlowTrack(String flowCode, Long bizId) { List flowTrack = baseDao.getFlowTrack(flowCode, bizId); return flowTrack; } public String getFlowStepName(String wfIdCodes, Long bizId) { return baseDao.getFlowStepName(wfIdCodes, bizId); } public void deleteFlowStep(String wfIdCode, String stepIdMark, Long bizId){ baseDao.deleteFlowStep(wfIdCode,stepIdMark,bizId); } public void getRunFlow(List dataList, String flowCode) { List ids = dataList.stream().map(f -> f.getId()).collect(Collectors.toList()); Map params = new HashMap(); params.put("bizIds", ids); params.put("flowCode", flowCode); List list = baseDao.getRunFlow(params); for (BaseEntity item : dataList) { List list2 = list.stream().filter(item2->item2.getBizId().equals(item.getId())).collect(Collectors.toList()); if (list2.size()>0) { item.setFlowInfo(list2.get(0)); }else{ item.setFlowInfo(new FlowInfo()); } } } }