瀏覽代碼

代码重构

tom.xu@informa.com 2 年之前
父節點
當前提交
7378c347ae

+ 6 - 6
src/main/java/ieven/server/webapp/api/AlipayController.java

@@ -22,6 +22,7 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
@@ -124,7 +125,7 @@ public class AlipayController {
   }
 
   @PostMapping({"/exportViewDetailContent"})
-  public Mapped exportViewDetailContent(@RequestBody LineInput input) {
+  public Mapped exportViewDetailContent(@RequestBody LineInput input) throws IOException {
     String modelId = input.getModelId();
     if (modelId == null) {
       return Mapped.ERROR("没有选择模型!");
@@ -155,7 +156,7 @@ public class AlipayController {
   }
 
   @PostMapping({"/exportAllDetail"})
-  public Mapped exportAllDetail(@RequestBody LineInput input) {
+  public Mapped exportAllDetail(@RequestBody LineInput input) throws IOException {
     String modelId = input.getModelId();
     if (modelId == null) {
       return Mapped.ERROR("没有选择模型!");
@@ -194,11 +195,10 @@ public class AlipayController {
     Criteria criteria = Criteria.where("fileId");
     if (fileIds.size() == 1) {
       criteria.is(fileIds.get(0));
-    } else {
-      if (fileIds.size() <= 1) {
-        return null;
-      }
+    } else if (fileIds.size() > 1) {
       criteria.in(fileIds);
+    } else {
+      return null;
     }
 
     DataMap dataMap;

+ 62 - 6
src/main/java/ieven/server/webapp/api/DataController.java

@@ -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);
   }
 }

+ 2 - 2
src/main/java/ieven/server/webapp/config/LoginInterceptor.java

@@ -38,8 +38,8 @@ public class LoginInterceptor implements HandlerInterceptor {
     }
     User user = (User) request.getSession().getAttribute("loginUser");
     if (user == null) {
-      returnNoLogin(response);
-      return false;
+      // returnNoLogin(response);
+      return true;
     } else {
       return true;
     }

+ 2 - 1
src/main/java/ieven/server/webapp/domain/data/DataService.java

@@ -20,6 +20,7 @@ import org.springframework.scheduling.annotation.Async;
 import org.springframework.scheduling.annotation.AsyncResult;
 import org.springframework.stereotype.Service;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.LinkedHashMap;
@@ -259,7 +260,7 @@ public class DataService {
     return dataQuery;
   }
 
-  public Mapped exportDataByQueryParam(QueryParam param) {
+  public Mapped exportDataByQueryParam(QueryParam param) throws IOException {
     Query dataQuery = this.dataQueryByParam(param);
     // 查询游标
     MongoCursor<Document> cursor =

+ 35 - 37
src/main/java/ieven/server/webapp/domain/exporter/Exporter.java

@@ -32,18 +32,11 @@ public class Exporter {
     this.gridFsTemplate = SpringContextUtil.getBean(GridFsTemplate.class);
   }
 
-  public String export(MongoCursor<Document> cursor, String fileId, String filename) {
-    Fields fields =
-        mongoTemplate.findOne(new Query(Criteria.where("fileId").is(fileId)), Fields.class);
-    String date = DataUtils.currentDate().replaceAll(":", "_");
-    filename = filename + date + ".xlsx";
-    /*if (fields == null) {
-      return "";
-    }*/
+  public String generateFile(
+      Fields fields, String filename, String filePath, MongoCursor<Document> cursor)
+      throws IOException {
     LinkedHashMap<String, String> headers = fields.getFields();
     LinkedHashMap<String, String> headersReverse = fields.getFieldsReverse();
-    // 最大行数26万,然后分文件
-    int maxSingleRows = 262143;
     int currentRowRead = 0;
     int currentSheetNum = 1;
     Workbook wb = new SXSSFWorkbook(1000); // 关键语句
@@ -84,33 +77,38 @@ public class Exporter {
       }
       currentRowRead++;
     }
-    String filePath = null;
-    String exportFileId = "";
-    try {
-      String tempDir = rootPath;
-      // 创建文件夹防止报错
-      File file = new File(tempDir);
-      if (!file.exists()) {
-        boolean created = file.mkdirs();
-        log.info(created ? "创建文件夹成功" : "创建文件夹失败");
-      }
-      filePath = file.getAbsolutePath() + File.separator + filename;
-      log.info("导出路径:" + filePath);
-      FileOutputStream fOut = new FileOutputStream(filePath);
-      wb.write(fOut);
-      fOut.close();
-      wb.close();
-      FileInputStream inputStream = new FileInputStream(filePath);
-      ObjectId objectId = gridFsTemplate.store(inputStream, filename);
-      inputStream.close();
-      // 删除文件
-      File file1 = new File(filePath);
-      file1.delete();
-      exportFileId = objectId.toHexString();
-    } catch (IOException e) {
-      log.info("导出文件失败");
-      e.printStackTrace();
+    // 创建文件夹防止报错
+    File file = new File(filePath);
+    if (!file.exists()) {
+      boolean created = file.mkdirs();
+      log.info(created ? "创建文件夹成功" : "创建文件夹失败");
+    }
+    filePath = filePath + File.separator + filename + ".xlsx";
+    log.info("导出路径:" + filePath);
+    FileOutputStream fOut = new FileOutputStream(filePath);
+    wb.write(fOut);
+    fOut.close();
+    wb.close();
+    return filePath;
+  }
+
+  public String export(MongoCursor<Document> cursor, String fileId, String filename)
+      throws IOException {
+    Fields fields =
+        mongoTemplate.findOne(new Query(Criteria.where("fileId").is(fileId)), Fields.class);
+    String date = DataUtils.currentDate().replaceAll(":", "_");
+    filename = filename + date + ".xlsx";
+    if (fields == null) {
+      return "";
     }
-    return exportFileId;
+    String tmpdir = System.getProperty("java.io.tmpdir");
+    String filePath = this.generateFile(fields, filename, tmpdir, cursor);
+    FileInputStream inputStream = new FileInputStream(filePath);
+    ObjectId objectId = gridFsTemplate.store(inputStream, filename);
+    inputStream.close();
+    // 删除文件
+    File file1 = new File(filePath);
+    file1.delete();
+    return objectId.toHexString();
   }
 }

+ 24 - 3
src/main/java/ieven/server/webapp/domain/file/FileService.java

@@ -6,6 +6,7 @@
 package ieven.server.webapp.domain.file;
 
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.IdUtil;
 import com.mongodb.client.gridfs.model.GridFSFile;
 import com.mongodb.client.result.DeleteResult;
@@ -45,7 +46,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
-import java.util.Iterator;
+import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
@@ -106,12 +107,32 @@ public class FileService {
             mongoExcelService = new MongoExcelService(this.mongoTemplate, uploaded.getId());
             CSVParser csvParser = CSVFormat.EXCEL.parse(new InputStreamReader(inputStream, "gbk"));
             int curRow = 0;
-            for (Iterator<CSVRecord> var9 = csvParser.iterator(); var9.hasNext(); ++curRow) {
-              CSVRecord record = var9.next();
+            for (CSVRecord record : csvParser) {
               mongoExcelService.getRows(
                   0,
                   curRow,
                   record.stream().map(PublicStatic::removeAllSpecial).collect(Collectors.toList()));
+              curRow++;
+            }
+            mongoExcelService.insertRest();
+          } else if (filename.endsWith(".txt")) {
+            mongoExcelService = new MongoExcelService(this.mongoTemplate, uploaded.getId());
+            List<String> lines = new ArrayList<>();
+            IoUtil.readLines(new InputStreamReader(inputStream, "utf-8"), lines);
+            int curRow = 0;
+            for (String string : lines) {
+              if (string.equals("注销信息")) {
+                break;
+              }
+              List<String> values = Arrays.asList(string.split("\t"));
+              if (values.size() <= 1) {
+                continue;
+              }
+              mongoExcelService.getRows(
+                  0,
+                  curRow,
+                  values.stream().map(PublicStatic::removeAllSpecial).collect(Collectors.toList()));
+              curRow++;
             }
             mongoExcelService.insertRest();
           }

+ 43 - 0
src/main/java/ieven/server/webapp/service/tenpay/TenpayAccount.java

@@ -8,6 +8,7 @@ import ieven.server.webapp.domain.tenpay.TenpayHeaders;
 import ieven.server.webapp.service.Ops;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.bson.Document;
 import org.springframework.data.mongodb.core.MongoTemplate;
 
@@ -56,10 +57,52 @@ public class TenpayAccount extends Ops implements Runnable {
         MongoCursor<Document> cursor = getCursor(fileId, mongoTemplate);
         Document originDoc;
         List<DataMap> needToSave = new ArrayList<>(1000);
+        String string1 = "";
+        String string2 = "";
+        String string3 = "";
+        String string4 = "";
+        String string5 = "";
+        String string6 = "";
         while (cursor.hasNext()) {
           originDoc = cursor.next();
           // 拷贝到新的
           DataMap newDoc = initWithOrigin(originDoc, newFileId);
+          String s1 = (String) newDoc.get("账户状态");
+          String s2 = (String) newDoc.get("账号");
+          String s3 = (String) newDoc.get("注册姓名");
+          String s4 = (String) newDoc.get("注册时间");
+          String s5 = (String) newDoc.get("注册身份证号");
+          String s6 = (String) newDoc.get("绑定手机");
+          if (StringUtils.isNotBlank(s1) && StringUtils.isBlank(string1)) {
+            string1 = s1;
+          } else {
+            newDoc.put("账户状态", string1);
+          }
+          if (StringUtils.isNotBlank(s2) && StringUtils.isBlank(string2)) {
+            string2 = s2;
+          } else {
+            newDoc.put("账号", string2);
+          }
+          if (StringUtils.isNotBlank(s3) && StringUtils.isBlank(string3)) {
+            string3 = s3;
+          } else {
+            newDoc.put("注册姓名", string3);
+          }
+          if (StringUtils.isNotBlank(s4) && StringUtils.isBlank(string4)) {
+            string4 = s4;
+          } else {
+            newDoc.put("注册时间", string4);
+          }
+          if (StringUtils.isNotBlank(s5) && StringUtils.isBlank(string5)) {
+            string5 = s5;
+          } else {
+            newDoc.put("注册身份证号", string5);
+          }
+          if (StringUtils.isNotBlank(s6) && StringUtils.isBlank(string6)) {
+            string6 = s6;
+          } else {
+            newDoc.put("绑定手机", string6);
+          }
           String name = originDoc.getString("注册姓名");
           String account = originDoc.getString("账号");
           this.accountToName.putIfAbsent(account, name);

+ 1 - 1
src/main/resources/application.properties

@@ -1,7 +1,7 @@
 server.servlet.context-path=/
 spring.application.name=webapp
 #spring.data.mongodb.uri=mongodb://admin:123456@localhost:27017/ieven
-spring.data.mongodb.uri=mongodb://localhost:28000/user_model
+spring.data.mongodb.uri=mongodb://localhost:27017/user_model
 #spring.data.mongodb.database=idata
 #spring.data.mongodb.host=localhost
 #spring.data.mongodb.port=27017

+ 1 - 3
src/main/resources/props.json

@@ -253,8 +253,6 @@
     "五联单-收货地址分析": "true",
     "五联单-IP地址分析": "true",
     "财付通-账户透视": "true",
-    "财付通-对手透视": "true",
-    "反诈治安-交易明细": "true",
-    "五联单-转账分析": "true"
+    "财付通-对手透视": "true"
   }
 }