|
@@ -6,8 +6,11 @@
|
|
package ieven.server.webapp.domain.file;
|
|
package ieven.server.webapp.domain.file;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.io.IoUtil;
|
|
import cn.hutool.core.io.IoUtil;
|
|
import cn.hutool.core.util.IdUtil;
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
|
+import cn.hutool.poi.excel.ExcelReader;
|
|
|
|
+import cn.hutool.poi.excel.ExcelUtil;
|
|
import com.mongodb.client.gridfs.model.GridFSFile;
|
|
import com.mongodb.client.gridfs.model.GridFSFile;
|
|
import com.mongodb.client.result.DeleteResult;
|
|
import com.mongodb.client.result.DeleteResult;
|
|
import ieven.server.webapp.domain.IdInput;
|
|
import ieven.server.webapp.domain.IdInput;
|
|
@@ -16,17 +19,22 @@ import ieven.server.webapp.domain.data.Fields;
|
|
import ieven.server.webapp.domain.model.Model;
|
|
import ieven.server.webapp.domain.model.Model;
|
|
import ieven.server.webapp.infrastructure.wrapper.Mapped;
|
|
import ieven.server.webapp.infrastructure.wrapper.Mapped;
|
|
import ieven.server.webapp.service.MongoExcelService;
|
|
import ieven.server.webapp.service.MongoExcelService;
|
|
|
|
+import ieven.server.webapp.util.EncodeDetector;
|
|
|
|
+import ieven.server.webapp.util.FileUtils;
|
|
import ieven.server.webapp.util.excel.ExcelXlsReader;
|
|
import ieven.server.webapp.util.excel.ExcelXlsReader;
|
|
import ieven.server.webapp.util.excel.ExcelXlsxReader;
|
|
import ieven.server.webapp.util.excel.ExcelXlsxReader;
|
|
import ieven.server.webapp.util.excel.PublicStatic;
|
|
import ieven.server.webapp.util.excel.PublicStatic;
|
|
import info.monitorenter.cpdetector.io.*;
|
|
import info.monitorenter.cpdetector.io.*;
|
|
|
|
+import lombok.SneakyThrows;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.csv.CSVFormat;
|
|
import org.apache.commons.csv.CSVFormat;
|
|
import org.apache.commons.csv.CSVParser;
|
|
import org.apache.commons.csv.CSVParser;
|
|
import org.apache.commons.csv.CSVRecord;
|
|
import org.apache.commons.csv.CSVRecord;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
import org.bson.types.ObjectId;
|
|
import org.bson.types.ObjectId;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.context.annotation.Lazy;
|
|
import org.springframework.context.annotation.Lazy;
|
|
import org.springframework.data.domain.Sort;
|
|
import org.springframework.data.domain.Sort;
|
|
import org.springframework.data.domain.Sort.Order;
|
|
import org.springframework.data.domain.Sort.Order;
|
|
@@ -41,12 +49,12 @@ import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.scheduling.annotation.AsyncResult;
|
|
import org.springframework.scheduling.annotation.AsyncResult;
|
|
import org.springframework.scheduling.annotation.EnableAsync;
|
|
import org.springframework.scheduling.annotation.EnableAsync;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.Charset;
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Arrays;
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
+import java.util.*;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.Future;
|
|
import java.util.concurrent.Future;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -55,6 +63,9 @@ import java.util.stream.Collectors;
|
|
@EnableAsync
|
|
@EnableAsync
|
|
@Slf4j
|
|
@Slf4j
|
|
public class FileService {
|
|
public class FileService {
|
|
|
|
+ @Value("${spring.data.fileExportPath}")
|
|
|
|
+ private String fileUploadPath;
|
|
|
|
+
|
|
String[] order1 = {"反诈数据-订单明细", "治安数据-订单明细","经侦数据-订单明细","网黑数据-订单明细", "反诈治安-交易明细", "反诈治安-账户透视", "反诈治安-对手透视"};
|
|
String[] order1 = {"反诈数据-订单明细", "治安数据-订单明细","经侦数据-订单明细","网黑数据-订单明细", "反诈治安-交易明细", "反诈治安-账户透视", "反诈治安-对手透视"};
|
|
String[] order2 = {
|
|
String[] order2 = {
|
|
"五联单-注册信息",
|
|
"五联单-注册信息",
|
|
@@ -444,4 +455,78 @@ public class FileService {
|
|
Model model = this.mongoTemplate.findOne(query, Model.class, "model");
|
|
Model model = this.mongoTemplate.findOne(query, Model.class, "model");
|
|
return new AsyncResult(model != null ? model.getModelName() : "");
|
|
return new AsyncResult(model != null ? model.getModelName() : "");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public Mapped uploadFilesAndSave(List<MultipartFile> multipartFiles, String modelId) {
|
|
|
|
+ List<String> failNameList = new ArrayList<>();
|
|
|
|
+ for (int i = 1; i <= multipartFiles.size(); i++) {
|
|
|
|
+ MultipartFile multipartFile = multipartFiles.get(i);
|
|
|
|
+ String originName = multipartFile.getOriginalFilename();
|
|
|
|
+ String suffix = originName.substring(originName.lastIndexOf(".") + 1);
|
|
|
|
+ String prefix = originName.substring(0, originName.lastIndexOf("."));
|
|
|
|
+ String savedName = prefix + "(" + i + ")." + suffix;
|
|
|
|
+ String filepath = fileUploadPath + File.separator + modelId + File.separator + savedName;
|
|
|
|
+ FileUtils.uploadFile(multipartFile, filepath);
|
|
|
|
+ MultipartFile multipartFile1 = FileUtils.getMultipartFile(new File(filepath));
|
|
|
|
+ try {
|
|
|
|
+ Mapped mapped = uploadSingle(multipartFile1, modelId);
|
|
|
|
+ if(!"200".equals(mapped.get("code"))){
|
|
|
|
+ failNameList.add(String.valueOf(mapped.get("message")));
|
|
|
|
+ }
|
|
|
|
+ //删除文件
|
|
|
|
+ FileUtils.deleteFile(filepath);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ Mapped mapped = Mapped.OK();
|
|
|
|
+ mapped.put("failNameList", failNameList);
|
|
|
|
+ return mapped;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Mapped uploadSingle(MultipartFile file, String modelId)
|
|
|
|
+ throws Exception {
|
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
|
+ String extName = FileUtil.extName(filename);
|
|
|
|
+ String contentType = file.getContentType();
|
|
|
|
+ ByteArrayOutputStream baos = FileUtils.cloneInputStream(file.getInputStream());
|
|
|
|
+ if (extName.equalsIgnoreCase("xls") || extName.equalsIgnoreCase("xlsx")) {
|
|
|
|
+ ExcelReader reader = ExcelUtil.getReader(new ByteArrayInputStream(baos.toByteArray()));
|
|
|
|
+ List<Sheet> sheets = reader.getSheets();
|
|
|
|
+ List<String> headers = new ArrayList<>();
|
|
|
|
+ for (Sheet sheet : sheets) {
|
|
|
|
+ reader.setSheet(sheet);
|
|
|
|
+ List<Object> objects = reader.readRow(0);
|
|
|
|
+ if (objects.size() > 1) { // 至少大于1
|
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
+ for (Object object : objects) {
|
|
|
|
+ stringBuilder.append(object);
|
|
|
|
+ }
|
|
|
|
+ headers.add(stringBuilder.toString());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (headers.size() > 1) {
|
|
|
|
+ // 判断sheet第一行内容是否一致
|
|
|
|
+ StringBuilder errorHeaders = new StringBuilder();
|
|
|
|
+ String baseHeader = headers.get(0);
|
|
|
|
+ for (int i = 0; i < headers.size(); i++) {
|
|
|
|
+ //
|
|
|
|
+ if (i == 0) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if (!headers.get(i).equals(baseHeader)) {
|
|
|
|
+ errorHeaders.append("sheet").append(i).append("标题列与第一个sheet不一致!");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (errorHeaders.length() > 0) {
|
|
|
|
+ return Mapped.ERROR(errorHeaders.toString());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ reader.close();
|
|
|
|
+ }
|
|
|
|
+ String encoding = EncodeDetector.getEncoding(baos.toByteArray());
|
|
|
|
+ baos.close();
|
|
|
|
+ return this.fileService.storeUploaded(
|
|
|
|
+ new ByteArrayInputStream(baos.toByteArray()), encoding, filename, contentType, modelId);
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|