Selaa lähdekoodia

新增流程上传接口

wangliang 1 vuosi sitten
vanhempi
commit
a3f083050a

+ 11 - 0
sop-business/src/main/java/com/qmth/sop/business/activiti/service/ActivitiService.java

@@ -4,6 +4,7 @@ import com.qmth.sop.business.bean.bean.FormPropertyBean;
 import com.qmth.sop.business.bean.result.FlowViewResult;
 import com.qmth.sop.common.enums.FlowApprovePassEnum;
 import com.qmth.sop.common.enums.TFCustomTypeEnum;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
 import java.util.List;
@@ -26,11 +27,21 @@ public interface ActivitiService {
      */
     String findProcessDefinitionIdByDeploymentId(String deploymentId);
 
+    /**
+     * 上传流程
+     *
+     * @param file
+     * @return
+     * @throws IOException
+     */
+    public Map<String, Object> upload(MultipartFile file) throws IOException;
+
     /**
      * 部署流程
      *
      * @param tfCustomTypeEnumList
      * @return
+     * @throws IOException
      */
     public Map<String, Object> createDeployment(List<TFCustomTypeEnum> tfCustomTypeEnumList) throws IOException;
 

+ 36 - 11
sop-business/src/main/java/com/qmth/sop/business/activiti/service/impl/ActivitiServiceImpl.java

@@ -13,10 +13,7 @@ import com.qmth.sop.business.entity.TFCustomFlowEntity;
 import com.qmth.sop.business.entity.TFFlowApprove;
 import com.qmth.sop.business.entity.TFFlowLog;
 import com.qmth.sop.business.mapper.SysUserMapper;
-import com.qmth.sop.business.service.TFCustomFlowEntityService;
-import com.qmth.sop.business.service.TFCustomFlowService;
-import com.qmth.sop.business.service.TFFlowApproveService;
-import com.qmth.sop.business.service.TFFlowLogService;
+import com.qmth.sop.business.service.*;
 import com.qmth.sop.common.contant.SystemConstant;
 import com.qmth.sop.common.enums.*;
 import com.qmth.sop.common.lock.MemoryLock;
@@ -41,6 +38,7 @@ import org.springframework.core.io.ClassPathResource;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import java.io.IOException;
@@ -89,6 +87,9 @@ public class ActivitiServiceImpl implements ActivitiService {
     @Resource
     TFFlowLogService tfFlowLogService;
 
+    @Resource
+    BasicAttachmentService basicAttachmentService;
+
     /**
      * 根据deploymentId查找processDefinitionId
      *
@@ -100,22 +101,46 @@ public class ActivitiServiceImpl implements ActivitiService {
         return sysUserMapper.findProcessDefinitionIdByDeploymentId(deploymentId);
     }
 
+    /**
+     * 上传流程
+     *
+     * @param file
+     * @return
+     * @throws IOException
+     */
+    @Override
+    public Map<String, Object> upload(MultipartFile file) throws IOException {
+        basicAttachmentService.validateAttachment(file);
+        Map<String, Object> map = new HashMap<>();
+        DeploymentBuilder builder = repositoryService.createDeployment();
+        ZipInputStream zip = new ZipInputStream(file.getInputStream());
+        builder.addZipInputStream(zip);
+        Map<String, Object> mapData = formPropertiesGet(builder.deploy().getId(), null, null);
+        map.put(file.getName(), mapData);
+        return map;
+    }
+
     /**
      * 部署流程
      *
      * @param tfCustomTypeEnumList
      * @return
+     * @throws IOException
      */
     @Override
     public Map<String, Object> createDeployment(List<TFCustomTypeEnum> tfCustomTypeEnumList) throws IOException {
-        Map<String, Object> map = new HashMap<>();
+        Map<String, Object> map = null;
         for (TFCustomTypeEnum tfCustomTypeEnum : tfCustomTypeEnumList) {
-            DeploymentBuilder builder = repositoryService.createDeployment();
-            ClassPathResource resource = new ClassPathResource(tfCustomTypeEnum.getFileName());
-            ZipInputStream zip = new ZipInputStream(resource.getInputStream());
-            builder.addZipInputStream(zip);
-            Map<String, Object> mapData = formPropertiesGet(builder.deploy().getId(), null, null);
-            map.put(tfCustomTypeEnum.name(), mapData);
+            List<TFCustomFlow> tfCustomFlowList = tfCustomFlowService.list(new QueryWrapper<TFCustomFlow>().lambda().eq(TFCustomFlow::getType, tfCustomTypeEnum));
+            if (CollectionUtils.isEmpty(tfCustomFlowList)) {
+                map = CollectionUtils.isEmpty(map) ? new HashMap<>() : map;
+                DeploymentBuilder builder = repositoryService.createDeployment();
+                ClassPathResource resource = new ClassPathResource(tfCustomTypeEnum.getFileName());
+                ZipInputStream zip = new ZipInputStream(resource.getInputStream());
+                builder.addZipInputStream(zip);
+                Map<String, Object> mapData = formPropertiesGet(builder.deploy().getId(), null, null);
+                map.put(tfCustomTypeEnum.name(), mapData);
+            }
         }
         return map;
     }

+ 7 - 0
sop-business/src/main/java/com/qmth/sop/business/service/BasicAttachmentService.java

@@ -2,6 +2,7 @@ package com.qmth.sop.business.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.qmth.sop.business.entity.BasicAttachment;
+import org.springframework.web.multipart.MultipartFile;
 
 /**
  * <p>
@@ -13,4 +14,10 @@ import com.qmth.sop.business.entity.BasicAttachment;
  */
 public interface BasicAttachmentService extends IService<BasicAttachment> {
 
+    /**
+     * 验证附件
+     *
+     * @param file
+     */
+    public void validateAttachment(MultipartFile file);
 }

+ 55 - 0
sop-business/src/main/java/com/qmth/sop/business/service/impl/BasicAttachmentServiceImpl.java

@@ -1,10 +1,23 @@
 package com.qmth.sop.business.service.impl;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.qmth.sop.business.cache.CommonCacheService;
 import com.qmth.sop.business.entity.BasicAttachment;
+import com.qmth.sop.business.entity.SysConfig;
 import com.qmth.sop.business.mapper.BasicAttachmentMapper;
 import com.qmth.sop.business.service.BasicAttachmentService;
+import com.qmth.sop.common.contant.SystemConstant;
+import com.qmth.sop.common.enums.ExceptionResultEnum;
+import org.apache.commons.io.FilenameUtils;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
 
 /**
  * <p>
@@ -17,4 +30,46 @@ import org.springframework.stereotype.Service;
 @Service
 public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMapper, BasicAttachment> implements BasicAttachmentService {
 
+    @Resource
+    CommonCacheService commonCacheService;
+
+    /**
+     * 验证附件
+     *
+     * @param file
+     */
+    @Override
+    public void validateAttachment(MultipartFile file) {
+        String fileName = FilenameUtils.getBaseName(file.getOriginalFilename());
+        String format = "." + FilenameUtils.getExtension(file.getOriginalFilename());
+
+        SysConfig sysConfig = commonCacheService.addSysConfigCache(SystemConstant.ATTACHMENT_TYPE);
+        Optional.ofNullable(sysConfig).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置附件类型"));
+        List<String> attachmentTypeList = Arrays.asList(sysConfig.getConfigValue().replaceAll("\\[", "").replaceAll("\\]", "").split(","));
+        if (Objects.nonNull(format)) {
+            long count = attachmentTypeList.stream().filter(s -> format.equalsIgnoreCase(s)).count();
+            if (count == 0) {
+                throw ExceptionResultEnum.ERROR.exception("文件格式只能为" + attachmentTypeList.toString());
+            }
+        }
+
+        SysConfig sysConfigLength = commonCacheService.addSysConfigCache(SystemConstant.ATTACHMENT_LENGTH);
+        Optional.ofNullable(sysConfigLength).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置附件名称长度"));
+        int attachmentLength = Integer.parseInt(sysConfigLength.getConfigValue());
+        if (Objects.nonNull(fileName) && fileName.length() > attachmentLength) {
+            throw ExceptionResultEnum.ERROR.exception("文件名长度不能超过" + attachmentLength + "个字符");
+        }
+        long size = file.getSize();
+        BigDecimal b = new BigDecimal(size);
+        BigDecimal num = new BigDecimal(1024);
+        b = b.divide(num, 2, BigDecimal.ROUND_HALF_UP).divide(num, 2, BigDecimal.ROUND_HALF_UP)
+                .setScale(2, BigDecimal.ROUND_HALF_UP);
+
+        SysConfig sysConfigSize = commonCacheService.addSysConfigCache(SystemConstant.ATTACHMENT_SIZE);
+        Optional.ofNullable(sysConfigSize).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置附件大小"));
+        double attachmentSize = Double.valueOf(sysConfigSize.getConfigValue());
+        if (b.doubleValue() > attachmentSize) {
+            throw ExceptionResultEnum.ERROR.exception("文件大小不能超过" + attachmentSize + "MB");
+        }
+    }
 }

+ 8 - 0
sop-server/src/main/java/com/qmth/sop/server/api/ActivitiFromHtmlController.java

@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
@@ -39,6 +40,13 @@ public class ActivitiFromHtmlController {
         return ResultUtil.ok(activitiService.createDeployment(Arrays.asList(TFCustomTypeEnum.OFFICE_SOP_FLOW)));
     }
 
+    @ApiOperation(value = "上传流程接口")
+    @RequestMapping(value = "/upload", method = RequestMethod.POST)
+    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
+    public Result upload(@ApiParam(required = true, value = "上传文件") @RequestParam MultipartFile file) throws IOException {
+        return ResultUtil.ok(activitiService.upload(file));
+    }
+
     @ApiOperation(value = "获取流程部署信息")
     @RequestMapping(value = "/deployment/data", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})

+ 7 - 0
sop-server/src/main/java/com/qmth/sop/server/start/StartRunning.java

@@ -1,12 +1,15 @@
 package com.qmth.sop.server.start;
 
+import com.qmth.sop.business.activiti.service.ActivitiService;
 import com.qmth.sop.business.service.SysConfigService;
+import com.qmth.sop.common.enums.TFCustomTypeEnum;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.util.Arrays;
 
 /**
  * @Description: 服务启动时初始化运行,哪个微服务模块需要则拿此模版去用
@@ -22,10 +25,14 @@ public class StartRunning implements CommandLineRunner {
     @Resource
     SysConfigService sysConfigService;
 
+    @Resource
+    ActivitiService activitiService;
+
     @Override
     public void run(String... args) throws Exception {
         log.info("服务器启动时执行 start");
         sysConfigService.selectAll();
+        activitiService.createDeployment(Arrays.asList(TFCustomTypeEnum.OFFICE_SOP_FLOW));
         log.info("服务器启动时执行 end");
     }