package com.iailab.module.bpm.service.task; import cn.hutool.core.util.ObjectUtil; import com.iailab.framework.common.pojo.PageResult; import com.iailab.module.bpm.controller.admin.task.vo.instance.BpmProcessInstanceCopyPageReqVO; import com.iailab.module.bpm.dal.dataobject.task.BpmProcessInstanceCopyDO; import com.iailab.module.bpm.dal.mysql.task.BpmProcessInstanceCopyMapper; import com.iailab.module.bpm.enums.ErrorCodeConstants; import com.iailab.module.bpm.service.definition.BpmProcessDefinitionService; import lombok.extern.slf4j.Slf4j; import org.flowable.engine.repository.ProcessDefinition; import org.flowable.engine.runtime.ProcessInstance; import org.flowable.task.api.Task; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.util.Collection; import java.util.List; import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; import static com.iailab.framework.common.util.collection.CollectionUtils.convertList; /** * 流程抄送 Service 实现类 * * @author kyle */ @Service @Validated @Slf4j public class BpmProcessInstanceCopyServiceImpl implements BpmProcessInstanceCopyService { @Resource private BpmProcessInstanceCopyMapper processInstanceCopyMapper; @Resource @Lazy // 延迟加载,避免循环依赖 private BpmTaskService taskService; @Resource @Lazy // 延迟加载,避免循环依赖 private BpmProcessInstanceService processInstanceService; @Resource @Lazy // 延迟加载,避免循环依赖 private BpmProcessDefinitionService processDefinitionService; @Override public void createProcessInstanceCopy(Collection userIds, String reason, String taskId) { Task task = taskService.getTask(taskId); if (ObjectUtil.isNull(task)) { throw exception(ErrorCodeConstants.TASK_NOT_EXISTS); } // 执行抄送 createProcessInstanceCopy(userIds, reason, task.getProcessInstanceId(), task.getTaskDefinitionKey(), task.getId(), task.getName()); } @Override public void createProcessInstanceCopy(Collection userIds, String reason, String processInstanceId, String activityId, String activityName, String taskId) { // 1.1 校验流程实例存在 ProcessInstance processInstance = processInstanceService.getProcessInstance(processInstanceId); if (processInstance == null) { throw exception(ErrorCodeConstants.PROCESS_INSTANCE_NOT_EXISTS); } // 1.2 校验流程定义存在 ProcessDefinition processDefinition = processDefinitionService.getProcessDefinition( processInstance.getProcessDefinitionId()); if (processDefinition == null) { throw exception(ErrorCodeConstants.PROCESS_DEFINITION_NOT_EXISTS); } // 2. 创建抄送流程 List copyList = convertList(userIds, userId -> new BpmProcessInstanceCopyDO() .setUserId(userId).setReason(reason).setStartUserId(Long.valueOf(processInstance.getStartUserId())) .setProcessInstanceId(processInstanceId).setProcessInstanceName(processInstance.getName()) .setCategory(processDefinition.getCategory()).setTaskId(taskId) .setActivityId(activityId).setActivityName(activityName)); processInstanceCopyMapper.insertBatch(copyList); } @Override public PageResult getProcessInstanceCopyPage(Long userId, BpmProcessInstanceCopyPageReqVO pageReqVO) { return processInstanceCopyMapper.selectPage(userId, pageReqVO); } }