content: package com.ruoyi.flowable.handler;
  
  import cn.hutool.core.bean.BeanUtil;
  import com.ruoyi.common.enums.FlowMenuEnum;
  import com.ruoyi.common.enums.ProcessStatus;
  import com.ruoyi.flowable.api.domain.WfBusinessProcess;
  import com.ruoyi.flowable.api.domain.vo.WorkSignVo;
  import com.ruoyi.flowable.api.service.IWorkSignServiceApi;
  import com.ruoyi.flowable.service.IWfBusinessProcessService;
  import com.ruoyi.flowable.service.IWfProcessService;
  import org.flowable.engine.HistoryService;
  import org.flowable.engine.runtime.ProcessInstance;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Lazy;
  import org.springframework.stereotype.Component;
  import org.springframework.transaction.annotation.Transactional;
  
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.List;
  
  /**
   * 重新发起流程处理类
   *
   * @author fengcheng
   */
  @Component("resubmitProcessHandler")
  public class ResubmitProcessHandler {
  
      @Autowired
      @Lazy
      private IWfBusinessProcessService wfBusinessProcessService;
  
      @Autowired
      @Lazy
      private IWfProcessService wfProcessService;
  
      @Autowired
      @Lazy
      protected HistoryService historyService;
  
      @Autowired
      @Lazy
      private IWorkSignServiceApi workSignServiceApi;
  
      /**
       * 重新发起流程
       *
       * @param wfBusinessProcess 重新发起
       */
      @Transactional(rollbackFor = Exception.class)
      public void resubmit(WfBusinessProcess wfBusinessProcess) {
          // 电子签章
          if (FlowMenuEnum.SIGN_FLOW_MENU.getCode().equals(wfBusinessProcess.getBusinessProcessType())) {
              updateSign(wfBusinessProcess);
              return;
          }
      }
  
      /**
       * 修改签署
       *
       * @param wfBusinessProcess 重新发起
       */
      private void updateSign(WfBusinessProcess wfBusinessProcess) {
          // 删除流程实例
          List<String> ids = Collections.singletonList(wfBusinessProcess.getProcessId());
          // 删除历史流程实例
          historyService.bulkDeleteHistoricProcessInstances(ids);
  
          // 删除业务流程信息
          wfBusinessProcessService.deleteWfBusinessProcessByBusinessId(wfBusinessProcess.getBusinessId(), wfBusinessProcess.getBusinessProcessType());
  
          WorkSignVo workSignVo = workSignServiceApi.selectWorkSignBySignId(wfBusinessProcess.getBusinessId());
          // 发起流程
          workSignVo.setBusinessId(String.valueOf(workSignVo.getSignId()));
          workSignVo.setBusinessProcessType(FlowMenuEnum.SIGN_FLOW_MENU.getCode());
          workSignVo.setSignFileList(null);
          ProcessInstance processInstance = wfProcessService.startProcessByDefId(workSignVo.getDefinitionId(), BeanUtil.beanToMap(workSignVo, new HashMap<>(16), false, false));
          String processInstanceId = processInstance.getProcessInstanceId();
  
          // 添加业务流程
          WfBusinessProcess process = new WfBusinessProcess();
          process.setProcessId(processInstanceId);
          process.setBusinessId(String.valueOf(workSignVo.getSignId()));
          process.setBusinessProcessType(FlowMenuEnum.SIGN_FLOW_MENU.getCode());
          wfBusinessProcessService.insertWfBusinessProcess(process);
  
          // 修改对应流程
          WorkSignVo workSign = new WorkSignVo();
          workSign.setProcessId(processInstanceId);
          workSign.setSchedule(ProcessStatus.RUNNING.getStatus());
          workSign.setSignId(workSignVo.getSignId());
          workSignServiceApi.updateWorkSign(workSign);
      }
  }
  