From c54b2d22c68be31ffdfbca13f5052576df617a39 Mon Sep 17 00:00:00 2001
From: PoffyZhang <99775271@qq.com>
Date: Wed, 6 Sep 2023 10:18:01 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=BD=ACPDF=E4=B8=8A?=
=?UTF-8?q?=E4=BC=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../common/util/CommonInputStreamResource.java | 53 +++++++++
.../gov/manage/GovProjectCollectionManage.java | 69 +++++++++++-
.../pmapi/gov/model/entity/GovBizProjectApply.java | 3 +
.../provincial/contants/ProvincialContant.java | 15 +++
.../pmapi/provincial/manage/ProvincialManage.java | 56 ++++++++++
.../pmapi/provincial/model/res/OssApiData.java | 33 ++++++
.../pmapi/provincial/model/res/OssApiResponse.java | 120 +++++++++++++++++++++
.../pmapi/provincial/utils/FileUtil.java | 32 ++++++
.../pmapi/wps/manage/WpsConvertManage.java | 56 ++++++++++
.../pmapi/file/CommonInputStreamResource.java | 53 ---------
.../com/ningdatech/pmapi/file/ProvinceOssTest.java | 10 +-
11 files changed, 436 insertions(+), 64 deletions(-)
create mode 100644 pmapi/src/main/java/com/ningdatech/pmapi/common/util/CommonInputStreamResource.java
create mode 100644 pmapi/src/main/java/com/ningdatech/pmapi/provincial/contants/ProvincialContant.java
create mode 100644 pmapi/src/main/java/com/ningdatech/pmapi/provincial/manage/ProvincialManage.java
create mode 100644 pmapi/src/main/java/com/ningdatech/pmapi/provincial/model/res/OssApiData.java
create mode 100644 pmapi/src/main/java/com/ningdatech/pmapi/provincial/model/res/OssApiResponse.java
create mode 100644 pmapi/src/main/java/com/ningdatech/pmapi/provincial/utils/FileUtil.java
delete mode 100644 pmapi/src/test/java/com/ningdatech/pmapi/file/CommonInputStreamResource.java
diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/common/util/CommonInputStreamResource.java b/pmapi/src/main/java/com/ningdatech/pmapi/common/util/CommonInputStreamResource.java
new file mode 100644
index 0000000..81e9cd3
--- /dev/null
+++ b/pmapi/src/main/java/com/ningdatech/pmapi/common/util/CommonInputStreamResource.java
@@ -0,0 +1,53 @@
+package com.ningdatech.pmapi.common.util;
+
+import org.springframework.core.io.InputStreamResource;
+
+import java.io.InputStream;
+
+public class CommonInputStreamResource extends InputStreamResource {
+ /***
+ * 文件長度
+ */
+ private int length;
+ /***
+ * 文件名稱
+ */
+ private String fileName;
+ public CommonInputStreamResource(InputStream inputStream) {
+ super(inputStream);
+ }
+ public CommonInputStreamResource(InputStream inputStream, int length,String fileName) {
+ super(inputStream);
+ this.length = length;
+ this.fileName = fileName;
+ }
+ /**
+ * 覆写父类方法
+ * 如果不重写这个方法,并且文件有一定大小,那么服务端会出现异常
+ * {@code The multi-part request contained parameter data (excluding uploaded files) that exceeded}
+ *
+ * @return
+ */
+ @Override
+ public String getFilename() {
+ return this.fileName;
+ }
+ /**
+ * 覆写父类 contentLength 方法
+ * 因为 {@link org.springframework.core.io.AbstractResource#contentLength()}方法会重新读取一遍文件,
+ * 而上传文件时,restTemplate 会通过这个方法获取大小。然后当真正需要读取内容的时候,发现已经读完,会报如下错误。
+ *
+ * java.lang.IllegalStateException: InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times
+ * at org.springframework.core.io.InputStreamResource.getInputStream(InputStreamResource.java:96)
+ *
+ *
+ * ref:com.amazonaws.services.s3.model.S3ObjectInputStream#available()
+ *
+ * @return
+ */
+ @Override
+ public long contentLength() {
+ int estimate = length;
+ return estimate == 0 ? 1 : estimate;
+ }
+}
\ No newline at end of file
diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/gov/manage/GovProjectCollectionManage.java b/pmapi/src/main/java/com/ningdatech/pmapi/gov/manage/GovProjectCollectionManage.java
index 404d64f..df0f4a0 100644
--- a/pmapi/src/main/java/com/ningdatech/pmapi/gov/manage/GovProjectCollectionManage.java
+++ b/pmapi/src/main/java/com/ningdatech/pmapi/gov/manage/GovProjectCollectionManage.java
@@ -4,6 +4,8 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -23,10 +25,12 @@ import com.ningdatech.pmapi.gov.model.vo.*;
import com.ningdatech.pmapi.gov.service.*;
import com.ningdatech.pmapi.projectdeclared.utils.GenerateProjectCodeUtil;
import com.ningdatech.pmapi.projectlib.model.req.ProjectListReq;
+import com.ningdatech.pmapi.provincial.manage.ProvincialManage;
import com.ningdatech.pmapi.sys.model.entity.Role;
import com.ningdatech.pmapi.user.entity.enumeration.RoleEnum;
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.pmapi.user.util.LoginUserUtil;
+import com.ningdatech.pmapi.wps.manage.WpsConvertManage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@@ -39,11 +43,9 @@ import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
+import java.lang.reflect.Field;
import java.time.LocalDateTime;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
+import java.util.*;
import java.util.stream.Collectors;
/**
@@ -76,6 +78,8 @@ public class GovProjectCollectionManage {
private final RestTemplate restTemplate;
+ private final WpsConvertManage wpsConvertManage;
+
@Value("${project.push-url}")
private String pushUrl;
@@ -425,7 +429,9 @@ public class GovProjectCollectionManage {
saveApply.setBizTime(LocalDateTime.now());
saveApply.setUpdateBy(user.getUsername());
saveApply.setUpdateOn(LocalDateTime.now());
- projectApplyService.saveOrUpdate(saveApply);
+ if(projectApplyService.saveOrUpdate(saveApply)){
+ uploadFileToProvincialOss(apply,oldApply,saveApply);
+ }
}
// 3.保存 审批信息
@@ -657,6 +663,8 @@ public class GovProjectCollectionManage {
//改正逻辑删除
baseinfo.setDeleted(Boolean.TRUE);
+ baseinfo.setUpdateBy(username);
+ baseinfo.setUpdateOn(LocalDateTime.now());
baseinfoService.updateById(baseinfo);
projectApplyService.update(Wrappers.lambdaUpdate(GovBizProjectApply.class)
.eq(GovBizProjectApply::getBaseProjId,projId)
@@ -779,4 +787,55 @@ public class GovProjectCollectionManage {
log.info("推送删除失败,{}", e.getMessage());
}
}
+
+ /**
+ * 上传项目归集申报的文件到OSS 前提还要先转为PDF
+ * @param apply
+ * @param oldApply
+ */
+ private void uploadFileToProvincialOss(GovBizProjectApplyDTO apply,
+ GovBizProjectApply oldApply,GovBizProjectApply saveApply) {
+ if(checkFieldHas(apply.getBaseProjBasisFile(),oldApply,"baseProjBasisFile")){
+ JSONArray fileArray = JSON.parseArray(apply.getBaseProjBasisFile());
+ StringJoiner sj = new StringJoiner(";");
+ fileArray.forEach(j -> {
+ JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(j));
+ byte[] btyes = wpsConvertManage.downloadToPdfStream(jsonObject.getLong("id"));
+ String oss = ProvincialManage.uploadToOss(btyes, jsonObject.getString("originalFileName"));
+ sj.add(oss);
+ });
+ saveApply.setBaseProjBasisFilePdf(sj.toString());
+ }
+ projectApplyService.updateById(saveApply);
+ }
+
+ private Boolean checkFieldHas(String field,GovBizProjectApply oldApply,String fieldName){
+ if(StringUtils.isBlank(field)){
+ return Boolean.FALSE;
+ }
+
+ //如果没有老值
+ if(Objects.isNull(oldApply)){
+ return Boolean.TRUE;
+ }
+
+ //和老值做个对比 不一样则true
+ Class> clazz = oldApply.getClass();
+ String oldFiled = StringUtils.EMPTY;
+ try {
+ Field fieldDeclared = clazz.getDeclaredField(fieldName);
+ fieldDeclared.setAccessible(Boolean.TRUE);
+ oldFiled = Objects.nonNull(fieldDeclared.get(oldApply)) ? String.valueOf(fieldDeclared.get(oldApply)) : null;
+ } catch (NoSuchFieldException e) {
+ log.error("转换PDF文件出错" + e);
+ } catch (IllegalAccessException e) {
+ log.error("转换PDF文件出错" + e);
+ }
+
+ if(!oldFiled.equals(field)){
+ return Boolean.TRUE;
+ }
+
+ return Boolean.FALSE;
+ }
}
diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/gov/model/entity/GovBizProjectApply.java b/pmapi/src/main/java/com/ningdatech/pmapi/gov/model/entity/GovBizProjectApply.java
index db6c5d9..b5ec5d6 100644
--- a/pmapi/src/main/java/com/ningdatech/pmapi/gov/model/entity/GovBizProjectApply.java
+++ b/pmapi/src/main/java/com/ningdatech/pmapi/gov/model/entity/GovBizProjectApply.java
@@ -149,4 +149,7 @@ public class GovBizProjectApply implements Serializable {
@ApiModelProperty("最后修改人")
private String updateBy;
+
+ @ApiModelProperty("立项依据材料PDF版")
+ private String baseProjBasisFilePdf;
}
diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/provincial/contants/ProvincialContant.java b/pmapi/src/main/java/com/ningdatech/pmapi/provincial/contants/ProvincialContant.java
new file mode 100644
index 0000000..88efe8b
--- /dev/null
+++ b/pmapi/src/main/java/com/ningdatech/pmapi/provincial/contants/ProvincialContant.java
@@ -0,0 +1,15 @@
+package com.ningdatech.pmapi.provincial.contants;
+
+/**
+ * @Classname ProvincialContant
+ * @Description
+ * @Date 2023/9/6 9:16
+ * @Author PoffyZhang
+ */
+public interface ProvincialContant {
+
+ class OssUpload {
+ public static final String OSS_UPLOAD_URL = "https://pms.zj.gov.cn/prometheus-zhejiang_file_service/api/v1/file/uploadFile";
+ public static final String MEDIA_TYPE = "multipart/form-data";
+ }
+}
diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/provincial/manage/ProvincialManage.java b/pmapi/src/main/java/com/ningdatech/pmapi/provincial/manage/ProvincialManage.java
new file mode 100644
index 0000000..6e0967d
--- /dev/null
+++ b/pmapi/src/main/java/com/ningdatech/pmapi/provincial/manage/ProvincialManage.java
@@ -0,0 +1,56 @@
+package com.ningdatech.pmapi.provincial.manage;
+
+import com.alibaba.fastjson.JSON;
+import com.ningdatech.basic.exception.BizException;
+import com.ningdatech.pmapi.common.util.CommonInputStreamResource;
+import com.ningdatech.pmapi.provincial.contants.ProvincialContant;
+import com.ningdatech.pmapi.provincial.model.res.OssApiData;
+import com.ningdatech.pmapi.provincial.model.res.OssApiResponse;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.*;
+import org.springframework.stereotype.Component;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestTemplate;
+import java.io.ByteArrayInputStream;
+import java.util.Objects;
+
+/**
+ * @Classname ProvincialManage
+ * @Description
+ * @Date 2023/9/6 9:08
+ * @Author PoffyZhang
+ */
+@Component
+@RequiredArgsConstructor
+@Slf4j
+public class ProvincialManage {
+
+ public static String uploadToOss(byte[] fileBytes,String fileName) {
+ String url = ProvincialContant.OssUpload.OSS_UPLOAD_URL;
+ MultiValueMap
+ * OssApiResponse -
+ *
+ * OssApiResponse -
+ *
- * ref:com.amazonaws.services.s3.model.S3ObjectInputStream#available()
- *
- * @return
- */
- @Override
- public long contentLength() {
- int estimate = length;
- return estimate == 0 ? 1 : estimate;
- }
-}
\ No newline at end of file
diff --git a/pmapi/src/test/java/com/ningdatech/pmapi/file/ProvinceOssTest.java b/pmapi/src/test/java/com/ningdatech/pmapi/file/ProvinceOssTest.java
index c1a4500..92242d3 100644
--- a/pmapi/src/test/java/com/ningdatech/pmapi/file/ProvinceOssTest.java
+++ b/pmapi/src/test/java/com/ningdatech/pmapi/file/ProvinceOssTest.java
@@ -1,7 +1,6 @@
package com.ningdatech.pmapi.file;
-import org.apache.tomcat.util.http.fileupload.IOUtils;
-import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
+import com.ningdatech.pmapi.common.util.CommonInputStreamResource;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
@@ -12,11 +11,10 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
-import org.springframework.web.multipart.commons.CommonsMultipartFile;
+
import java.io.File;
import java.io.FileInputStream;
-import org.springframework.web.multipart.MultipartFile;
-import org.springframework.mock.web.MockMultipartFile;
+
import org.apache.http.entity.ContentType;
import java.io.*;
@@ -43,7 +41,7 @@ public class ProvinceOssTest {
headers.setContentType(type);
HttpEntity
- * java.lang.IllegalStateException: InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times
- * at org.springframework.core.io.InputStreamResource.getInputStream(InputStreamResource.java:96)
- *
- *