|
@@ -1,13 +1,19 @@
|
|
|
package ieven.server.webapp.api;
|
|
|
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import cn.hutool.core.util.ZipUtil;
|
|
|
+import com.mongodb.client.MongoCursor;
|
|
|
import ieven.server.webapp.domain.FileIdInput;
|
|
|
import ieven.server.webapp.domain.data.DataService;
|
|
|
+import ieven.server.webapp.domain.data.Fields;
|
|
|
import ieven.server.webapp.domain.data.QueryParam;
|
|
|
import ieven.server.webapp.domain.data.ViewInput;
|
|
|
+import ieven.server.webapp.domain.exporter.Exporter;
|
|
|
import ieven.server.webapp.domain.file.LogicalFile;
|
|
|
import ieven.server.webapp.infrastructure.wrapper.Mapped;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.bson.Document;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
@@ -17,6 +23,9 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
@@ -47,14 +56,15 @@ public class DataController {
|
|
|
}
|
|
|
|
|
|
@PostMapping({"/exportByQuery"})
|
|
|
- public Mapped exportByQuery(@RequestBody QueryParam param) {
|
|
|
+ public Mapped exportByQuery(@RequestBody QueryParam param) throws IOException {
|
|
|
return StringUtils.isBlank(param.getFileId())
|
|
|
? Mapped.ERROR("文件选择为空!")
|
|
|
: this.dataService.exportDataByQueryParam(param);
|
|
|
}
|
|
|
|
|
|
@PostMapping({"/batchExport"})
|
|
|
- public Mapped batchExport(@RequestBody QueryParam param) {
|
|
|
+ public Mapped batchExport(@RequestBody QueryParam param, HttpServletResponse response)
|
|
|
+ throws IOException {
|
|
|
if (StringUtils.isBlank(param.getModelId())) {
|
|
|
return Mapped.ERROR("请选择按键!");
|
|
|
}
|
|
@@ -67,11 +77,57 @@ public class DataController {
|
|
|
.and("filename")
|
|
|
.in("反诈治安-交易明细", "五联单-账户明细", "财付通-交易明细"));
|
|
|
List<LogicalFile> resultList = this.mongoTemplate.find(query, LogicalFile.class);
|
|
|
- List<String> fileids = new ArrayList<>();
|
|
|
+ List<String> filePaths = new ArrayList<>();
|
|
|
+ String randomDir = RandomUtil.randomString(5);
|
|
|
+ String tmpdir = System.getProperty("java.io.tmpdir");
|
|
|
for (LogicalFile logicalFile : resultList) {
|
|
|
- fileids.add(logicalFile.getId());
|
|
|
+ Criteria criteria = Criteria.where("fileId").in(logicalFile.getId());
|
|
|
+ Query dataQuery = new Query(criteria);
|
|
|
+ MongoCursor<Document> cursor =
|
|
|
+ mongoTemplate
|
|
|
+ .getCollection("data")
|
|
|
+ .find(dataQuery.getQueryObject())
|
|
|
+ .batchSize(1000)
|
|
|
+ .noCursorTimeout(true)
|
|
|
+ .cursor();
|
|
|
+ Fields fields =
|
|
|
+ mongoTemplate.findOne(
|
|
|
+ new Query(Criteria.where("fileId").is(logicalFile.getId())), Fields.class);
|
|
|
+ Exporter exporter = new Exporter();
|
|
|
+ String filePath =
|
|
|
+ exporter.generateFile(
|
|
|
+ fields, logicalFile.getFilename(), tmpdir + File.separator + randomDir, cursor);
|
|
|
+ filePaths.add(filePath);
|
|
|
+ }
|
|
|
+ Mapped ok = Mapped.OK();
|
|
|
+ ok.put("filepath", randomDir);
|
|
|
+ return ok;
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping({"/batchExport/download"})
|
|
|
+ public void batchExport(String filepath, HttpServletResponse response) {
|
|
|
+ String tmpdir = System.getProperty("java.io.tmpdir");
|
|
|
+ File zip = ZipUtil.zip(tmpdir + File.separator + filepath);
|
|
|
+ String filename = "交易明细.zip";
|
|
|
+ try {
|
|
|
+ response.setHeader("Content-Type", "multipart/form-data");
|
|
|
+ response.addHeader(
|
|
|
+ "Content-Disposition",
|
|
|
+ "attachment;filename="
|
|
|
+ + new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
|
|
+
|
|
|
+ OutputStream out = response.getOutputStream();
|
|
|
+ InputStream input = new FileInputStream(zip);
|
|
|
+ response.setHeader("Content-Length", String.valueOf(zip.length()));
|
|
|
+ byte[] b = new byte[2048];
|
|
|
+ int len;
|
|
|
+ while ((len = input.read(b)) != -1) {
|
|
|
+ out.write(b, 0, len);
|
|
|
+ }
|
|
|
+ input.close();
|
|
|
+ } catch (IOException var11) {
|
|
|
+ this.log.info("下载文件失败:" + filename);
|
|
|
+ var11.printStackTrace();
|
|
|
}
|
|
|
- param.setFileIds(fileids);
|
|
|
- return this.dataService.exportDataByQueryParam(param);
|
|
|
}
|
|
|
}
|