|
|
@@ -24,6 +24,7 @@ import com.jeeplus.finance.practiceSignature.mapper.CwPracticeSignatureFileMappe
|
|
|
import com.jeeplus.finance.practiceSignature.mapper.CwPracticeSignatureMapper;
|
|
|
import com.jeeplus.finance.practiceSignature.mapper.CwPracticeSignatureReportMapper;
|
|
|
import com.jeeplus.finance.practiceSignature.mapper.CwPracticeSignatureSignerMapper;
|
|
|
+import com.jeeplus.finance.practiceSignature.service.dto.CwPracticeSignatureBatchDownloadDTO;
|
|
|
import com.jeeplus.finance.practiceSignature.service.dto.CwPracticeSignatureDTO;
|
|
|
import com.jeeplus.finance.practiceSignature.service.dto.CwPracticeSignStatusDTO;
|
|
|
import com.jeeplus.finance.practiceSignature.service.dto.CwPracticeSignatureReportDTO;
|
|
|
@@ -48,6 +49,7 @@ import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Propagation;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
@@ -60,13 +62,16 @@ import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.io.PrintWriter;
|
|
|
import java.io.StringWriter;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
@@ -77,6 +82,7 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.UUID;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 报告执业签章service
|
|
|
@@ -203,6 +209,11 @@ public class CwPracticeSignatureService extends ServiceImpl<CwPracticeSignatureM
|
|
|
private static final String STATUS_SIGNING = "6";
|
|
|
|
|
|
/**
|
|
|
+ * 批量下载单次上限:防止一次勾全量把服务器临时目录和带宽打满,超出让前端分批
|
|
|
+ */
|
|
|
+ private static final int BATCH_DOWNLOAD_MAX = 20;
|
|
|
+
|
|
|
+ /**
|
|
|
* 契约锁合同详情回退查询的节流窗口(秒)。
|
|
|
*
|
|
|
* 主判据是"读库"(回调写的本人签署结果),详情查询只是库里没结论时的回退,
|
|
|
@@ -2321,6 +2332,286 @@ public class CwPracticeSignatureService extends ServiceImpl<CwPracticeSignatureM
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 批量下载已盖章件(列表页多选,只打包完成(5)且有件的单)
|
|
|
+ *
|
|
|
+ * 只打包 signed_zip_url 这一份"下载件":单文件时它就是已盖章件本身,多文件时是回调打好的整包 zip,
|
|
|
+ * 跟列表页逐个【下载】拿到的内容完全一致,不含原件。
|
|
|
+ *
|
|
|
+ * 跨服务只取临时签名地址(字符串),文件字节由本进程 HTTP 直拉 OSS,
|
|
|
+ * 不走 downBytesByStream / downloadFolderFromOss 这类跨服务返回文件流的老接口(历史上出过问题)。
|
|
|
+ * 先把每个件拉到本地临时目录再统一打包:单个失败不中断整包,跳过原因写进 zip 内《下载说明.txt》,
|
|
|
+ * 全部失败则回 JSON 错误体,前端按 blob 解析提示。
|
|
|
+ *
|
|
|
+ * @param dto 选中的申请id集合
|
|
|
+ * @param response 响应(zip 流或 JSON 错误体)
|
|
|
+ * @throws Exception IO 或调用异常
|
|
|
+ */
|
|
|
+ // 只读且要边下载边写响应,可能持续几十秒:挂起类级事务,避免整个下载期间占用数据库连接
|
|
|
+ @Transactional(propagation = Propagation.NOT_SUPPORTED)
|
|
|
+ public void downloadSignedFilesBatch(CwPracticeSignatureBatchDownloadDTO dto, HttpServletResponse response) throws Exception {
|
|
|
+ List<String> ids = dto == null || dto.getIds() == null ? new ArrayList<>()
|
|
|
+ : dto.getIds().stream().filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
|
|
|
+ if (ids.isEmpty()) {
|
|
|
+ writeBatchDownloadError(response, "请选择需要下载的申请");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (ids.size() > BATCH_DOWNLOAD_MAX) {
|
|
|
+ writeBatchDownloadError(response, "单次批量下载不能超过 " + BATCH_DOWNLOAD_MAX + " 条,请分批下载");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Map<String, CwPracticeSignature> mainMap = new HashMap<>();
|
|
|
+ for (CwPracticeSignature main : this.listByIds(ids)) {
|
|
|
+ mainMap.put(main.getId(), main);
|
|
|
+ }
|
|
|
+ List<String> failures = new ArrayList<>();
|
|
|
+ int missing = 0;
|
|
|
+ List<CwPracticeSignature> packable = new ArrayList<>();
|
|
|
+ for (String id : ids) {
|
|
|
+ CwPracticeSignature main = mainMap.get(id);
|
|
|
+ if (main == null) {
|
|
|
+ missing++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!STATUS_FINISHED.equals(main.getStatus())) {
|
|
|
+ failures.add(batchDownloadLabel(main) + ":未到完成状态,已跳过");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(main.getSignedZipUrl())) {
|
|
|
+ failures.add(batchDownloadLabel(main) + ":已盖章文件还在生成中,请稍后重试");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ packable.add(main);
|
|
|
+ }
|
|
|
+ if (missing > 0) {
|
|
|
+ failures.add("另有 " + missing + " 条申请不存在或已被删除,已跳过");
|
|
|
+ }
|
|
|
+ if (packable.isEmpty()) {
|
|
|
+ writeBatchDownloadError(response, "选中的申请都没有可下载的已盖章件:" + String.join(";", failures));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ File tempDir = new File(localTempDir() + "practice_signature_batch");
|
|
|
+ if (!tempDir.exists() && !tempDir.mkdirs()) {
|
|
|
+ writeBatchDownloadError(response, "服务器临时目录创建失败,请稍后重试");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<CwPracticeSignature> ready = new ArrayList<>();
|
|
|
+ Map<String, File> unitFiles = new LinkedHashMap<>();
|
|
|
+ try {
|
|
|
+ for (CwPracticeSignature main : packable) {
|
|
|
+ File target = new File(tempDir, main.getId() + ".download");
|
|
|
+ try {
|
|
|
+ String temporaryUrl = SpringUtil.getBean(IWorkAttachmentApi.class).getTemporaryUrl(main.getSignedZipUrl());
|
|
|
+ if (StringUtils.isBlank(temporaryUrl)) {
|
|
|
+ failures.add(batchDownloadLabel(main) + ":取下载地址失败");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ long size = downloadToLocalFile(temporaryUrl, target);
|
|
|
+ if (size <= 0) {
|
|
|
+ failures.add(batchDownloadLabel(main) + ":文件内容为空");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ ready.add(main);
|
|
|
+ unitFiles.put(main.getId(), target);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("报告执业签章批量下载:拉取已盖章件失败,id={}", main.getId(), e);
|
|
|
+ failures.add(batchDownloadLabel(main) + ":文件拉取失败,请单独重试");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (ready.isEmpty()) {
|
|
|
+ writeBatchDownloadError(response, "选中的申请都没有可下载的已盖章件:" + String.join(";", failures));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String zipName = "报告执业签章已盖章件_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";
|
|
|
+ String encodedName = URLEncoder.encode(zipName, "UTF-8").replace("+", "%20");
|
|
|
+ response.reset();
|
|
|
+ response.setContentType("application/zip");
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedName + "\"; filename*=UTF-8''" + encodedName);
|
|
|
+
|
|
|
+ Map<String, Integer> usedNames = new HashMap<>();
|
|
|
+ ZipOutputStream zos = new ZipOutputStream(response.getOutputStream(), StandardCharsets.UTF_8);
|
|
|
+ try {
|
|
|
+ for (CwPracticeSignature main : ready) {
|
|
|
+ writeFileToZip(zos, unitFiles.get(main.getId()), buildBatchEntryName(main, usedNames));
|
|
|
+ }
|
|
|
+ if (!failures.isEmpty()) {
|
|
|
+ writeTextToZip(zos, "下载说明.txt", buildBatchDownloadNote(failures));
|
|
|
+ }
|
|
|
+ zos.finish();
|
|
|
+ zos.flush();
|
|
|
+ } finally {
|
|
|
+ zos.close();
|
|
|
+ }
|
|
|
+ log.info("报告执业签章批量下载完成:打包 {} 单,跳过 {} 条", ready.size(), failures.size());
|
|
|
+ } finally {
|
|
|
+ for (File file : unitFiles.values()) {
|
|
|
+ deleteLocalFile(file.getPath());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * HTTP 直拉 OSS 临时签名地址到本地临时文件(不跨服务传文件流)
|
|
|
+ *
|
|
|
+ * @param url 临时签名地址
|
|
|
+ * @param target 本地临时文件
|
|
|
+ * @return 写入字节数
|
|
|
+ * @throws Exception 网络或 IO 异常
|
|
|
+ */
|
|
|
+ private long downloadToLocalFile(String url, File target) throws Exception {
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
|
|
+ connection.setConnectTimeout(10000);
|
|
|
+ connection.setReadTimeout(120000);
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ try {
|
|
|
+ int code = connection.getResponseCode();
|
|
|
+ if (code != HttpURLConnection.HTTP_OK) {
|
|
|
+ throw new IOException("下载已盖章件返回 HTTP " + code);
|
|
|
+ }
|
|
|
+ InputStream in = connection.getInputStream();
|
|
|
+ try {
|
|
|
+ OutputStream out = new FileOutputStream(target);
|
|
|
+ try {
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
+ long total = 0L;
|
|
|
+ int len;
|
|
|
+ while ((len = in.read(buffer)) != -1) {
|
|
|
+ out.write(buffer, 0, len);
|
|
|
+ total += len;
|
|
|
+ }
|
|
|
+ return total;
|
|
|
+ } finally {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ connection.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把一个本地临时文件写进 zip 的一个条目
|
|
|
+ *
|
|
|
+ * @param zos zip 流
|
|
|
+ * @param file 本地文件
|
|
|
+ * @param entryName 条目名
|
|
|
+ * @throws Exception IO 异常
|
|
|
+ */
|
|
|
+ private void writeFileToZip(ZipOutputStream zos, File file, String entryName) throws Exception {
|
|
|
+ zos.putNextEntry(new ZipEntry(entryName));
|
|
|
+ InputStream in = new FileInputStream(file);
|
|
|
+ try {
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
+ int len;
|
|
|
+ while ((len = in.read(buffer)) != -1) {
|
|
|
+ zos.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ in.close();
|
|
|
+ zos.closeEntry();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把文本内容写进 zip 的一个条目(条目内容统一 UTF-8)
|
|
|
+ *
|
|
|
+ * @param zos zip 流
|
|
|
+ * @param entryName 条目名
|
|
|
+ * @param content 文本内容
|
|
|
+ * @throws Exception IO 异常
|
|
|
+ */
|
|
|
+ private void writeTextToZip(ZipOutputStream zos, String entryName, String content) throws Exception {
|
|
|
+ zos.putNextEntry(new ZipEntry(entryName));
|
|
|
+ try {
|
|
|
+ zos.write(content.getBytes(StandardCharsets.UTF_8));
|
|
|
+ } finally {
|
|
|
+ zos.closeEntry();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量下载时给人看的单元名:报告号 + 项目名,不出现 id
|
|
|
+ *
|
|
|
+ * @param main 单据
|
|
|
+ * @return 展示名
|
|
|
+ */
|
|
|
+ private String batchDownloadLabel(CwPracticeSignature main) {
|
|
|
+ return StringUtils.defaultIfBlank(main.getReportNo(), "(无报告号)")
|
|
|
+ + " " + StringUtils.defaultIfBlank(main.getProjectName(), "(无项目名)");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拼批量下载 zip 内的单元文件名"报告号_项目名.扩展名":去掉文件系统非法字符,扩展名取自下载件地址,重名补序号
|
|
|
+ *
|
|
|
+ * @param main 单据
|
|
|
+ * @param usedNames 已用文件名计数
|
|
|
+ * @return 条目名
|
|
|
+ */
|
|
|
+ private String buildBatchEntryName(CwPracticeSignature main, Map<String, Integer> usedNames) {
|
|
|
+ String extension = "";
|
|
|
+ String url = StringUtils.defaultIfBlank(main.getSignedZipUrl(), "");
|
|
|
+ String segment = url.substring(url.lastIndexOf('/') + 1);
|
|
|
+ int dot = segment.lastIndexOf('.');
|
|
|
+ if (dot >= 0) {
|
|
|
+ String raw = segment.substring(dot + 1);
|
|
|
+ if (raw.matches("[A-Za-z0-9]{1,10}")) {
|
|
|
+ extension = "." + raw.toLowerCase();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String base = StringUtils.defaultIfBlank(main.getReportNo(), "") + "_" + StringUtils.defaultIfBlank(main.getProjectName(), "");
|
|
|
+ base = base.replaceAll("[\\\\/:*?\"<>|\\r\\n]", "_").replaceAll("^_+|_+$", "");
|
|
|
+ if (StringUtils.isBlank(base)) {
|
|
|
+ base = "报告执业签章";
|
|
|
+ }
|
|
|
+ if (base.length() > 80) {
|
|
|
+ base = base.substring(0, 80);
|
|
|
+ }
|
|
|
+ Integer count = usedNames.get(base);
|
|
|
+ usedNames.put(base, count == null ? 1 : count + 1);
|
|
|
+ return count == null ? base + extension : base + "(" + (count + 1) + ")" + extension;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量下载跳过清单,写进 zip 内的《下载说明.txt》
|
|
|
+ *
|
|
|
+ * @param failures 跳过原因
|
|
|
+ * @return 说明文本
|
|
|
+ */
|
|
|
+ private String buildBatchDownloadNote(List<String> failures) {
|
|
|
+ StringBuilder note = new StringBuilder("以下申请未打包进本压缩包,请核对后单独下载:\r\n");
|
|
|
+ for (String failure : failures) {
|
|
|
+ note.append("- ").append(failure).append("\r\n");
|
|
|
+ }
|
|
|
+ return note.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量下载的 JSON 错误体(前端以 blob 接收,按 400 解析出 message 提示)
|
|
|
+ *
|
|
|
+ * @param response 响应
|
|
|
+ * @param message 提示文案
|
|
|
+ * @throws IOException 写响应异常
|
|
|
+ */
|
|
|
+ private void writeBatchDownloadError(HttpServletResponse response, String message) throws IOException {
|
|
|
+ if (response.isCommitted()) {
|
|
|
+ log.warn("报告执业签章批量下载:响应已提交,无法回写错误提示,message={}", message);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ response.reset();
|
|
|
+ response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
|
|
+ response.setContentType("application/json;charset=utf-8");
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("code", 400);
|
|
|
+ body.put("message", message);
|
|
|
+ response.getWriter().write(JSON.toJSONString(body));
|
|
|
+ response.getWriter().flush();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 根据文件名推断 MIME 类型(下载单文件时用来设置 Content-Type,让浏览器能直接预览 PDF 等)
|
|
|
*/
|
|
|
private String getContentTypeByFileName(String fileName) {
|