From 098d3347a0df808908aab8c554cd7c4febc5e6d9 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期一, 26 八月 2024 11:43:59 +0800
Subject: [PATCH] !577 发布 5.2.2 正式版 安全性提升 Merge pull request !577 from 疯狂的狮子Li/dev

---
 ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/ActProcessDefinitionServiceImpl.java |  381 ++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 241 insertions(+), 140 deletions(-)

diff --git a/ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/ActProcessDefinitionServiceImpl.java b/ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/ActProcessDefinitionServiceImpl.java
index f29db1d..77fb257 100644
--- a/ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/ActProcessDefinitionServiceImpl.java
+++ b/ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/ActProcessDefinitionServiceImpl.java
@@ -3,38 +3,53 @@
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.codec.Base64;
 import cn.hutool.core.collection.CollUtil;
-import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
 import lombok.RequiredArgsConstructor;
 import lombok.SneakyThrows;
-import org.apache.commons.io.IOUtils;
+import lombok.extern.slf4j.Slf4j;
 import org.dromara.common.core.exception.ServiceException;
 import org.dromara.common.core.utils.StreamUtils;
 import org.dromara.common.core.utils.StringUtils;
+import org.dromara.common.mybatis.core.page.PageQuery;
 import org.dromara.common.mybatis.core.page.TableDataInfo;
 import org.dromara.common.tenant.helper.TenantHelper;
 import org.dromara.workflow.common.constant.FlowConstant;
 import org.dromara.workflow.domain.WfCategory;
+import org.dromara.workflow.domain.WfDefinitionConfig;
+import org.dromara.workflow.domain.WfNodeConfig;
 import org.dromara.workflow.domain.bo.ProcessDefinitionBo;
+import org.dromara.workflow.domain.bo.WfDefinitionConfigBo;
 import org.dromara.workflow.domain.vo.ProcessDefinitionVo;
+import org.dromara.workflow.domain.vo.WfDefinitionConfigVo;
+import org.dromara.workflow.mapper.WfDefinitionConfigMapper;
 import org.dromara.workflow.service.IActProcessDefinitionService;
 import org.dromara.workflow.service.IWfCategoryService;
-import org.flowable.engine.HistoryService;
+import org.dromara.workflow.service.IWfDefinitionConfigService;
+import org.dromara.workflow.service.IWfNodeConfigService;
+import org.dromara.workflow.utils.ModelUtils;
+import org.dromara.workflow.utils.QueryUtils;
+import org.flowable.bpmn.model.UserTask;
 import org.flowable.engine.ProcessMigrationService;
 import org.flowable.engine.RepositoryService;
+import org.flowable.engine.history.HistoricProcessInstance;
 import org.flowable.engine.impl.bpmn.deployer.ResourceNameUtil;
 import org.flowable.engine.repository.*;
-import org.flowable.task.api.history.HistoricTaskInstance;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
-import java.util.stream.Collectors;
+import java.util.Set;
+import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
 /**
@@ -42,62 +57,70 @@
  *
  * @author may
  */
+@Slf4j
 @RequiredArgsConstructor
 @Service
 public class ActProcessDefinitionServiceImpl implements IActProcessDefinitionService {
 
-    private final RepositoryService repositoryService;
-
-    private final HistoryService historyService;
-
-    private final ProcessMigrationService processMigrationService;
-
+    @Autowired(required = false)
+    private RepositoryService repositoryService;
+    @Autowired(required = false)
+    private ProcessMigrationService processMigrationService;
     private final IWfCategoryService wfCategoryService;
+    private final IWfDefinitionConfigService wfDefinitionConfigService;
+    private final WfDefinitionConfigMapper wfDefinitionConfigMapper;
+    private final IWfNodeConfigService wfNodeConfigService;
 
     /**
      * 鍒嗛〉鏌ヨ
      *
-     * @param processDefinitionBo 鍙傛暟
+     * @param bo 鍙傛暟
      * @return 杩斿洖鍒嗛〉鍒楄〃
      */
     @Override
-    public TableDataInfo<ProcessDefinitionVo> page(ProcessDefinitionBo processDefinitionBo) {
-        ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
-        if (TenantHelper.isEnable()) {
-            query.processDefinitionTenantId(TenantHelper.getTenantId());
+    public TableDataInfo<ProcessDefinitionVo> page(ProcessDefinitionBo bo, PageQuery pageQuery) {
+        ProcessDefinitionQuery query = QueryUtils.definitionQuery();
+        if (StringUtils.isNotEmpty(bo.getKey())) {
+            query.processDefinitionKey(bo.getKey());
         }
-        if (StringUtils.isNotEmpty(processDefinitionBo.getKey())) {
-            query.processDefinitionKey(processDefinitionBo.getKey());
+        if (StringUtils.isNotEmpty(bo.getCategoryCode())) {
+            query.processDefinitionCategory(bo.getCategoryCode());
         }
-        if (StringUtils.isNotEmpty(processDefinitionBo.getCategoryCode())) {
-            query.processDefinitionCategory(processDefinitionBo.getCategoryCode());
-        }
-        if (StringUtils.isNotEmpty(processDefinitionBo.getName())) {
-            query.processDefinitionNameLike("%" + processDefinitionBo.getName() + "%");
+        if (StringUtils.isNotEmpty(bo.getName())) {
+            query.processDefinitionNameLike("%" + bo.getName() + "%");
         }
         query.orderByDeploymentId().desc();
         // 鍒嗛〉鏌ヨ
         List<ProcessDefinitionVo> processDefinitionVoList = new ArrayList<>();
-        List<ProcessDefinition> definitionList = query.latestVersion().listPage(processDefinitionBo.getPageNum(), processDefinitionBo.getPageSize());
+        List<ProcessDefinition> definitionList = query.latestVersion().listPage(pageQuery.getFirstNum(), pageQuery.getPageSize());
         List<Deployment> deploymentList = null;
         if (CollUtil.isNotEmpty(definitionList)) {
             List<String> deploymentIds = StreamUtils.toList(definitionList, ProcessDefinition::getDeploymentId);
-            deploymentList = repositoryService.createDeploymentQuery().deploymentIds(deploymentIds).list();
+            deploymentList = QueryUtils.deploymentQuery(deploymentIds).list();
         }
-        for (ProcessDefinition processDefinition : definitionList) {
-            ProcessDefinitionVo processDefinitionVo = BeanUtil.toBean(processDefinition, ProcessDefinitionVo.class);
-            if (CollUtil.isNotEmpty(deploymentList)) {
-                // 閮ㄧ讲鏃堕棿
-                deploymentList.stream().filter(e -> e.getId().equals(processDefinition.getDeploymentId())).findFirst().ifPresent(e -> {
-                    processDefinitionVo.setDeploymentTime(e.getDeploymentTime());
-                });
+        if (CollUtil.isNotEmpty(definitionList)) {
+            List<String> ids = StreamUtils.toList(definitionList, ProcessDefinition::getId);
+            List<WfDefinitionConfigVo> wfDefinitionConfigVos = wfDefinitionConfigService.queryList(ids);
+            for (ProcessDefinition processDefinition : definitionList) {
+                ProcessDefinitionVo processDefinitionVo = BeanUtil.toBean(processDefinition, ProcessDefinitionVo.class);
+                if (CollUtil.isNotEmpty(deploymentList)) {
+                    // 閮ㄧ讲鏃堕棿
+                    deploymentList.stream().filter(e -> e.getId().equals(processDefinition.getDeploymentId())).findFirst().ifPresent(e -> {
+                        processDefinitionVo.setDeploymentTime(e.getDeploymentTime());
+                    });
+                }
+                if (CollUtil.isNotEmpty(wfDefinitionConfigVos)) {
+                    wfDefinitionConfigVos.stream().filter(e -> e.getDefinitionId().equals(processDefinition.getId())).findFirst().ifPresent(processDefinitionVo::setWfDefinitionConfigVo);
+                }
+                processDefinitionVoList.add(processDefinitionVo);
             }
-            processDefinitionVoList.add(processDefinitionVo);
         }
         // 鎬昏褰曟暟
         long total = query.count();
-
-        return new TableDataInfo<>(processDefinitionVoList, total);
+        TableDataInfo<ProcessDefinitionVo> build = TableDataInfo.build();
+        build.setRows(processDefinitionVoList);
+        build.setTotal(total);
+        return build;
     }
 
     /**
@@ -106,30 +129,33 @@
      * @param key 娴佺▼瀹氫箟key
      */
     @Override
-    public List<ProcessDefinitionVo> getProcessDefinitionListByKey(String key) {
+    public List<ProcessDefinitionVo> getListByKey(String key) {
         List<ProcessDefinitionVo> processDefinitionVoList = new ArrayList<>();
-        ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
-        if (TenantHelper.isEnable()) {
-            query.processDefinitionTenantId(TenantHelper.getTenantId());
-        }
+        ProcessDefinitionQuery query = QueryUtils.definitionQuery();
         List<ProcessDefinition> definitionList = query.processDefinitionKey(key).list();
         List<Deployment> deploymentList = null;
         if (CollUtil.isNotEmpty(definitionList)) {
-            List<String> deploymentIds = definitionList.stream().map(ProcessDefinition::getDeploymentId).collect(Collectors.toList());
-            deploymentList = repositoryService.createDeploymentQuery()
-                .deploymentIds(deploymentIds).list();
+            List<String> deploymentIds = StreamUtils.toList(definitionList, ProcessDefinition::getDeploymentId);
+            deploymentList = QueryUtils.deploymentQuery(deploymentIds).list();
         }
-        for (ProcessDefinition processDefinition : definitionList) {
-            ProcessDefinitionVo processDefinitionVo = BeanUtil.toBean(processDefinition, ProcessDefinitionVo.class);
-            if (CollUtil.isNotEmpty(deploymentList)) {
-                // 閮ㄧ讲鏃堕棿
-                deploymentList.stream().filter(e -> e.getId().equals(processDefinition.getDeploymentId())).findFirst().ifPresent(e -> {
-                    processDefinitionVo.setDeploymentTime(e.getDeploymentTime());
-                });
+        if (CollUtil.isNotEmpty(definitionList)) {
+            List<String> ids = StreamUtils.toList(definitionList, ProcessDefinition::getId);
+            List<WfDefinitionConfigVo> wfDefinitionConfigVos = wfDefinitionConfigService.queryList(ids);
+            for (ProcessDefinition processDefinition : definitionList) {
+                ProcessDefinitionVo processDefinitionVo = BeanUtil.toBean(processDefinition, ProcessDefinitionVo.class);
+                if (CollUtil.isNotEmpty(deploymentList)) {
+                    // 閮ㄧ讲鏃堕棿
+                    deploymentList.stream().filter(e -> e.getId().equals(processDefinition.getDeploymentId())).findFirst().ifPresent(e -> {
+                        processDefinitionVo.setDeploymentTime(e.getDeploymentTime());
+                    });
+                    if (CollUtil.isNotEmpty(wfDefinitionConfigVos)) {
+                        wfDefinitionConfigVos.stream().filter(e -> e.getDefinitionId().equals(processDefinition.getId())).findFirst().ifPresent(processDefinitionVo::setWfDefinitionConfigVo);
+                    }
+                }
+                processDefinitionVoList.add(processDefinitionVo);
             }
-            processDefinitionVoList.add(processDefinitionVo);
         }
-        return CollectionUtil.reverse(processDefinitionVoList);
+        return CollUtil.reverse(processDefinitionVoList);
     }
 
     /**
@@ -139,9 +165,9 @@
      */
     @SneakyThrows
     @Override
-    public String processDefinitionImage(String processDefinitionId) {
+    public String definitionImage(String processDefinitionId) {
         InputStream inputStream = repositoryService.getProcessDiagram(processDefinitionId);
-        return Base64.encode(IOUtils.toByteArray(inputStream));
+        return Base64.encode(IoUtil.readBytes(inputStream));
     }
 
     /**
@@ -150,38 +176,44 @@
      * @param processDefinitionId 娴佺▼瀹氫箟id
      */
     @Override
-    public String processDefinitionXml(String processDefinitionId) {
+    public String definitionXml(String processDefinitionId) {
         StringBuilder xml = new StringBuilder();
         ProcessDefinition processDefinition = repositoryService.getProcessDefinition(processDefinitionId);
-        InputStream inputStream;
-        try {
-            inputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName());
-            xml.append(IOUtils.toString(inputStream, StandardCharsets.UTF_8));
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
+        InputStream inputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName());
+        xml.append(IoUtil.read(inputStream, StandardCharsets.UTF_8));
         return xml.toString();
     }
 
     /**
      * 鍒犻櫎娴佺▼瀹氫箟
      *
-     * @param deploymentId        閮ㄧ讲id
-     * @param processDefinitionId 娴佺▼瀹氫箟id
+     * @param deploymentIds        閮ㄧ讲id
+     * @param processDefinitionIds 娴佺▼瀹氫箟id
      */
     @Override
-    public boolean deleteDeployment(String deploymentId, String processDefinitionId) {
+    @Transactional(rollbackFor = Exception.class)
+    public boolean deleteDeployment(List<String> deploymentIds, List<String> processDefinitionIds) {
         try {
-            List<HistoricTaskInstance> taskInstanceList = historyService.createHistoricTaskInstanceQuery()
-                .processDefinitionId(processDefinitionId).list();
-            if (CollectionUtil.isNotEmpty(taskInstanceList)) {
-                throw new ServiceException("褰撳墠娴佺▼瀹氫箟宸茶浣跨敤涓嶅彲鍒犻櫎锛�");
+            List<HistoricProcessInstance> historicProcessInstances = QueryUtils.hisInstanceQuery().deploymentIdIn(deploymentIds).list();
+            if (CollUtil.isNotEmpty(historicProcessInstances)) {
+                Set<String> defIds = StreamUtils.toSet(historicProcessInstances, HistoricProcessInstance::getProcessDefinitionId);
+                List<ProcessDefinition> processDefinitions = QueryUtils.definitionQuery().processDefinitionIds(defIds).list();
+                if (CollUtil.isNotEmpty(processDefinitions)) {
+                    Set<String> keys = StreamUtils.toSet(processDefinitions, ProcessDefinition::getKey);
+                    throw new ServiceException("褰撳墠銆�" + String.join(",", keys) + "銆戞祦绋嬪畾涔夊凡琚娇鐢ㄤ笉鍙垹闄わ紒");
+                }
             }
             //鍒犻櫎娴佺▼瀹氫箟
-            repositoryService.deleteDeployment(deploymentId);
+            for (String deploymentId : deploymentIds) {
+                repositoryService.deleteDeployment(deploymentId);
+            }
+            //鍒犻櫎娴佺▼瀹氫箟閰嶇疆
+            wfDefinitionConfigService.deleteByDefIds(processDefinitionIds);
+            //鍒犻櫎鑺傜偣閰嶇疆
+            wfNodeConfigService.deleteByDefIds(processDefinitionIds);
             return true;
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error(e.getMessage(), e);
             throw new ServiceException(e.getMessage());
         }
     }
@@ -192,13 +224,10 @@
      * @param processDefinitionId 娴佺▼瀹氫箟id
      */
     @Override
-    public boolean updateProcessDefState(String processDefinitionId) {
+    public boolean updateDefinitionState(String processDefinitionId) {
         try {
-            ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
-            if (TenantHelper.isEnable()) {
-                query.processDefinitionTenantId(TenantHelper.getTenantId());
-            }
-            ProcessDefinition processDefinition = query.processDefinitionId(processDefinitionId).singleResult();
+            ProcessDefinition processDefinition = QueryUtils.definitionQuery()
+                .processDefinitionId(processDefinitionId).singleResult();
             //灏嗗綋鍓嶄负鎸傝捣鐘舵�佹洿鏂颁负婵�娲荤姸鎬�
             //鍙傛暟璇存槑锛氬弬鏁�1锛氭祦绋嬪畾涔塱d,鍙傛暟2锛氭槸鍚︽縺娲伙紙true鏄惁绾ц仈瀵瑰簲娴佺▼瀹炰緥锛屾縺娲讳簡鍒欏搴旀祦绋嬪疄渚嬮兘鍙互瀹℃壒锛夛紝
             //鍙傛暟3锛氫粈涔堟椂鍊欐縺娲伙紝濡傛灉涓簄ull鍒欑珛鍗虫縺娲伙紝濡傛灉涓哄叿浣撴椂闂村垯鍒拌揪姝ゆ椂闂村悗婵�娲�
@@ -209,7 +238,7 @@
             }
             return true;
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error(e.getMessage(), e);
             throw new ServiceException("鎿嶄綔澶辫触:" + e.getMessage());
         }
     }
@@ -222,7 +251,7 @@
      */
 
     @Override
-    public boolean migrationProcessDefinition(String currentProcessDefinitionId, String fromProcessDefinitionId) {
+    public boolean migrationDefinition(String currentProcessDefinitionId, String fromProcessDefinitionId) {
         try {
             // 杩佺Щ楠岃瘉
             boolean migrationValid = processMigrationService.createProcessInstanceMigrationBuilder()
@@ -238,6 +267,7 @@
                 .migrateProcessInstances(fromProcessDefinitionId);
             return true;
         } catch (Exception e) {
+            log.error(e.getMessage(), e);
             throw new ServiceException(e.getMessage());
         }
     }
@@ -249,13 +279,10 @@
      */
     @Override
     public boolean convertToModel(String processDefinitionId) {
-        ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
+        ProcessDefinition pd = QueryUtils.definitionQuery()
             .processDefinitionId(processDefinitionId).singleResult();
         InputStream inputStream = repositoryService.getResourceAsStream(pd.getDeploymentId(), pd.getResourceName());
-        ModelQuery query = repositoryService.createModelQuery();
-        if (TenantHelper.isEnable()) {
-            query.modelTenantId(TenantHelper.getTenantId());
-        }
+        ModelQuery query = QueryUtils.modelQuery();
         Model model = query.modelKey(pd.getKey()).singleResult();
         try {
             if (ObjectUtil.isNotNull(model)) {
@@ -264,13 +291,14 @@
                 Model modelData = repositoryService.newModel();
                 modelData.setKey(pd.getKey());
                 modelData.setName(pd.getName());
+                modelData.setCategory(pd.getCategory());
                 modelData.setTenantId(pd.getTenantId());
                 repositoryService.saveModel(modelData);
                 repositoryService.addModelEditorSource(modelData.getId(), IoUtil.readBytes(inputStream));
             }
             return true;
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error(e.getMessage(), e);
             throw new ServiceException(e.getMessage());
         }
     }
@@ -281,63 +309,136 @@
      * @param file         鏂囦欢
      * @param categoryCode 鍒嗙被
      */
+    @SneakyThrows
     @Override
-    public boolean deployByFile(MultipartFile file, String categoryCode) {
-        try {
-            WfCategory wfCategory = wfCategoryService.queryByCategoryCode(categoryCode);
-            if (wfCategory == null) {
-                throw new ServiceException("娴佺▼鍒嗙被涓嶅瓨鍦�");
-            }
-            // 鏂囦欢鍚� = 娴佺▼鍚嶇О-娴佺▼key
-            String filename = file.getOriginalFilename();
-            assert filename != null;
-            String[] splitFilename = filename.substring(0, filename.lastIndexOf(".")).split("-");
-            if (splitFilename.length < 2) {
-                throw new ServiceException("娴佺▼鍒嗙被涓嶈兘涓虹┖(鏂囦欢鍚� = 娴佺▼鍚嶇О-娴佺▼key)");
-            }
-            //娴佺▼鍚嶇О
-            String processName = splitFilename[0];
-            //娴佺▼key
-            String processKey = splitFilename[1];
-            // 鏂囦欢鍚庣紑鍚�
-            String suffix = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
-            InputStream inputStream = file.getInputStream();
-            Deployment deployment;
-            if (FlowConstant.ZIP.equals(suffix)) {
-                DeploymentBuilder query = repositoryService.createDeployment();
-                if (TenantHelper.isEnable()) {
-                    query.tenantId(TenantHelper.getTenantId());
-                }
-                deployment = query.addZipInputStream(new ZipInputStream(inputStream))
-                    .name(processName).key(processKey).category(categoryCode).deploy();
-            } else {
-                String[] list = ResourceNameUtil.BPMN_RESOURCE_SUFFIXES;
-                boolean flag = false;
-                for (String str : list) {
-                    if (filename.contains(str)) {
-                        flag = true;
-                        break;
-                    }
-                }
-                if (flag) {
-                    DeploymentBuilder query = repositoryService.createDeployment();
-                    if (TenantHelper.isEnable()) {
-                        query.tenantId(TenantHelper.getTenantId());
-                    }
-                    deployment = query.addInputStream(filename, inputStream)
-                        .name(processName).key(processKey).category(categoryCode).deploy();
-                } else {
-                    throw new ServiceException("鏂囦欢绫诲瀷涓婁紶閿欒锛�");
-                }
-            }
-            // 鏇存柊鍒嗙被
-            ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
-            repositoryService.setProcessDefinitionCategory(definition.getId(), categoryCode);
+    @Transactional(rollbackFor = Exception.class)
+    public void deployByFile(MultipartFile file, String categoryCode) {
 
-            return true;
-        } catch (IOException e) {
-            e.printStackTrace();
-            throw new ServiceException("閮ㄧ讲澶辫触" + e.getMessage());
+        WfCategory wfCategory = wfCategoryService.queryByCategoryCode(categoryCode);
+        if (wfCategory == null) {
+            throw new ServiceException("娴佺▼鍒嗙被涓嶅瓨鍦�");
+        }
+        // 鏂囦欢鍚庣紑鍚�
+        String suffix = FileUtil.extName(file.getOriginalFilename());
+        InputStream inputStream = file.getInputStream();
+        if (FlowConstant.ZIP.equalsIgnoreCase(suffix)) {
+            ZipInputStream zipInputStream = null;
+            try {
+                zipInputStream = new ZipInputStream(inputStream);
+                ZipEntry zipEntry;
+                while ((zipEntry = zipInputStream.getNextEntry()) != null) {
+                    String filename = zipEntry.getName();
+                    String[] splitFilename = filename.substring(0, filename.lastIndexOf(".")).split("-");
+                    //娴佺▼鍚嶇О
+                    String processName = splitFilename[0];
+                    //娴佺▼key
+                    String processKey = splitFilename[1];
+                    ProcessDefinition oldProcessDefinition = QueryUtils.definitionQuery().processDefinitionKey(processKey).latestVersion().singleResult();
+                    DeploymentBuilder builder = repositoryService.createDeployment();
+                    Deployment deployment = builder.addInputStream(filename, zipInputStream)
+                        .tenantId(TenantHelper.getTenantId())
+                        .name(processName).key(processKey).category(categoryCode).deploy();
+                    ProcessDefinition definition = QueryUtils.definitionQuery().deploymentId(deployment.getId()).singleResult();
+                    repositoryService.setProcessDefinitionCategory(definition.getId(), categoryCode);
+                    setWfConfig(oldProcessDefinition, definition);
+                    zipInputStream.closeEntry();
+                }
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            } finally {
+                if (zipInputStream != null) {
+                    zipInputStream.close();
+                }
+            }
+            //鍒濆鍖栭厤缃暟鎹紙demo浣跨敤锛屼笉鐢ㄥ彲鍒犻櫎锛�
+            initWfDefConfig();
+        } else {
+            String originalFilename = file.getOriginalFilename();
+            if (StringUtils.containsAny(originalFilename, ResourceNameUtil.BPMN_RESOURCE_SUFFIXES)) {
+                // 鏂囦欢鍚� = 娴佺▼鍚嶇О-娴佺▼key
+                String[] splitFilename = originalFilename.substring(0, originalFilename.lastIndexOf(".")).split("-");
+                if (splitFilename.length < 2) {
+                    throw new ServiceException("鏂囦欢鍚� = 娴佺▼鍚嶇О-娴佺▼KEY");
+                }
+                //娴佺▼鍚嶇О
+                String processName = splitFilename[0];
+                //娴佺▼key
+                String processKey = splitFilename[1];
+                ProcessDefinition oldProcessDefinition = QueryUtils.definitionQuery().processDefinitionKey(processKey).latestVersion().singleResult();
+                DeploymentBuilder builder = repositoryService.createDeployment();
+                Deployment deployment = builder.addInputStream(originalFilename, inputStream)
+                    .tenantId(TenantHelper.getTenantId())
+                    .name(processName).key(processKey).category(categoryCode).deploy();
+                // 鏇存柊鍒嗙被
+                ProcessDefinition definition = QueryUtils.definitionQuery().deploymentId(deployment.getId()).singleResult();
+                repositoryService.setProcessDefinitionCategory(definition.getId(), categoryCode);
+                setWfConfig(oldProcessDefinition, definition);
+            } else {
+                throw new ServiceException("鏂囦欢绫诲瀷涓婁紶閿欒锛�");
+            }
+        }
+
+    }
+
+    /**
+     * 鍒濆鍖栭厤缃暟鎹紙demo浣跨敤锛屼笉鐢ㄥ彲鍒犻櫎锛�
+     */
+    private void initWfDefConfig() {
+        List<WfDefinitionConfig> wfDefinitionConfigs = wfDefinitionConfigMapper.selectList();
+        if (CollUtil.isEmpty(wfDefinitionConfigs)) {
+            ProcessDefinition processDefinition = QueryUtils.definitionQuery().processDefinitionKey("leave1").latestVersion().singleResult();
+            if (processDefinition != null) {
+                WfDefinitionConfigBo wfDefinitionConfigBo = new WfDefinitionConfigBo();
+                wfDefinitionConfigBo.setDefinitionId(processDefinition.getId());
+                wfDefinitionConfigBo.setProcessKey(processDefinition.getKey());
+                wfDefinitionConfigBo.setTableName("test_leave");
+                wfDefinitionConfigBo.setVersion(processDefinition.getVersion());
+                wfDefinitionConfigService.saveOrUpdate(wfDefinitionConfigBo);
+            }
+        }
+
+    }
+
+    /**
+     * 璁剧疆琛ㄥ崟鍐呭
+     *
+     * @param oldProcessDefinition 閮ㄧ讲鍓嶆渶鏂版祦绋嬪畾涔�
+     * @param definition           閮ㄧ讲鍚庢渶鏂版祦绋嬪畾涔�
+     */
+    private void setWfConfig(ProcessDefinition oldProcessDefinition, ProcessDefinition definition) {
+        //鏇存柊娴佺▼瀹氫箟琛ㄥ崟
+        if (oldProcessDefinition != null) {
+            WfDefinitionConfigVo definitionVo = wfDefinitionConfigService.getByDefId(oldProcessDefinition.getId());
+            if (definitionVo != null) {
+                wfDefinitionConfigService.deleteByDefIds(Collections.singletonList(oldProcessDefinition.getId()));
+                WfDefinitionConfigBo wfDefinitionConfigBo = new WfDefinitionConfigBo();
+                wfDefinitionConfigBo.setDefinitionId(definition.getId());
+                wfDefinitionConfigBo.setProcessKey(definition.getKey());
+                wfDefinitionConfigBo.setTableName(definitionVo.getTableName());
+                wfDefinitionConfigBo.setVersion(definition.getVersion());
+                wfDefinitionConfigBo.setRemark(definitionVo.getRemark());
+                wfDefinitionConfigService.saveOrUpdate(wfDefinitionConfigBo);
+            }
+        }
+        //鏇存柊娴佺▼鑺傜偣閰嶇疆琛ㄥ崟
+        List<UserTask> userTasks = ModelUtils.getUserTaskFlowElements(definition.getId());
+        UserTask applyUserTask = ModelUtils.getApplyUserTask(definition.getId());
+        List<WfNodeConfig> wfNodeConfigList = new ArrayList<>();
+        for (UserTask userTask : userTasks) {
+            if (StringUtils.isNotBlank(userTask.getFormKey()) && userTask.getFormKey().contains(StrUtil.COLON)) {
+                WfNodeConfig wfNodeConfig = new WfNodeConfig();
+                wfNodeConfig.setNodeId(userTask.getId());
+                wfNodeConfig.setNodeName(userTask.getName());
+                wfNodeConfig.setDefinitionId(definition.getId());
+                String[] split = userTask.getFormKey().split(StrUtil.COLON);
+                wfNodeConfig.setFormType(split[0]);
+                wfNodeConfig.setFormId(Long.valueOf(split[1]));
+                wfNodeConfig.setApplyUserTask(applyUserTask.getId().equals(userTask.getId()) ? FlowConstant.TRUE : FlowConstant.FALSE);
+                wfNodeConfigList.add(wfNodeConfig);
+            }
+        }
+        if (CollUtil.isNotEmpty(wfNodeConfigList)) {
+            wfNodeConfigService.saveOrUpdate(wfNodeConfigList);
         }
     }
 }

--
Gitblit v1.9.3