FileService.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by FernFlower decompiler)
  4. //
  5. package ieven.server.webapp.domain.file;
  6. import cn.hutool.core.date.DateUtil;
  7. import cn.hutool.core.io.IoUtil;
  8. import cn.hutool.core.util.IdUtil;
  9. import com.mongodb.client.gridfs.model.GridFSFile;
  10. import com.mongodb.client.result.DeleteResult;
  11. import ieven.server.webapp.domain.IdInput;
  12. import ieven.server.webapp.domain.data.DataMap;
  13. import ieven.server.webapp.domain.data.Fields;
  14. import ieven.server.webapp.domain.model.Model;
  15. import ieven.server.webapp.infrastructure.wrapper.Mapped;
  16. import ieven.server.webapp.service.MongoExcelService;
  17. import ieven.server.webapp.util.excel.ExcelXlsReader;
  18. import ieven.server.webapp.util.excel.ExcelXlsxReader;
  19. import ieven.server.webapp.util.excel.PublicStatic;
  20. import info.monitorenter.cpdetector.io.*;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.apache.commons.csv.CSVFormat;
  23. import org.apache.commons.csv.CSVParser;
  24. import org.apache.commons.csv.CSVRecord;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.bson.types.ObjectId;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.context.annotation.Lazy;
  29. import org.springframework.data.domain.Sort;
  30. import org.springframework.data.domain.Sort.Order;
  31. import org.springframework.data.mongodb.core.MongoTemplate;
  32. import org.springframework.data.mongodb.core.query.Criteria;
  33. import org.springframework.data.mongodb.core.query.Query;
  34. import org.springframework.data.mongodb.core.query.Update;
  35. import org.springframework.data.mongodb.gridfs.GridFsOperations;
  36. import org.springframework.data.mongodb.gridfs.GridFsResource;
  37. import org.springframework.data.mongodb.gridfs.GridFsTemplate;
  38. import org.springframework.scheduling.annotation.Async;
  39. import org.springframework.scheduling.annotation.AsyncResult;
  40. import org.springframework.scheduling.annotation.EnableAsync;
  41. import org.springframework.stereotype.Service;
  42. import java.io.*;
  43. import java.nio.charset.Charset;
  44. import java.util.ArrayList;
  45. import java.util.Arrays;
  46. import java.util.List;
  47. import java.util.concurrent.ExecutionException;
  48. import java.util.concurrent.Future;
  49. import java.util.stream.Collectors;
  50. @Service
  51. @EnableAsync
  52. @Slf4j
  53. public class FileService {
  54. String[] order1 = {"反诈数据-订单明细", "治安数据-订单明细","经侦数据-订单明细","网黑数据-订单明细", "反诈治安-交易明细", "反诈治安-账户透视", "反诈治安-对手透视"};
  55. String[] order2 = {
  56. "五联单-注册信息",
  57. "五联单-登录日志",
  58. "五联单-交易记录",
  59. "五联单-账户明细",
  60. "五联单-转账明细",
  61. "五联单-转账分析",
  62. "五联单-账户透视",
  63. "五联单-对手透视",
  64. "五联单-IP地址分析",
  65. "五联单-收货地址分析"
  66. };
  67. String[] order3 = {"财付通-注册信息", "财付通-订单明细1","财付通-订单明细2", "财付通-交易明细", "财付通-账户透视", "财付通-对手透视", "财付通-手机明细"};
  68. String[] order4 = {"数据分析-主体信息汇总","数据分析-账户透视","数据分析-对手透视", "数据分析-共同关系网络"};
  69. @Autowired
  70. private GridFsTemplate gridFsTemplate;
  71. @Autowired
  72. private MongoTemplate mongoTemplate;
  73. @Autowired
  74. private GridFsOperations fsOperations;
  75. @Autowired
  76. @Lazy
  77. private FileService fileService;
  78. public FileService() {
  79. }
  80. public static String getCharsetName(File file) throws IOException {
  81. String charsetName = "UTF-8";
  82. // 获取 CodepageDetectorProxy 实例
  83. CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
  84. // 添加解析器,会使用到添加的后 2 个 ext 里的 jar 包
  85. detector.add(new ParsingDetector(false));
  86. detector.add(JChardetFacade.getInstance());
  87. detector.add(ASCIIDetector.getInstance());
  88. detector.add(UnicodeDetector.getInstance());
  89. Charset charset = detector.detectCodepage(file.toURI().toURL());
  90. if (charset != null) {
  91. charsetName = charset.name();
  92. }
  93. return charsetName;
  94. }
  95. public static void main(String[] args) throws IOException {
  96. File file =
  97. new File(
  98. "C:\\Users\\Administrator\\Desktop\\楼\\支付宝五联单数据格式\\20120303组织卖淫案_OR222713D2400411_注册信息_20210913100124.csv");
  99. String charsetName = getCharsetName(file);
  100. CSVParser csvParser =
  101. CSVFormat.EXCEL.parse(new InputStreamReader(new FileInputStream(file), charsetName));
  102. for (CSVRecord strings : csvParser) {
  103. System.out.println(strings);
  104. }
  105. file = new File("C:\\Users\\Administrator\\Desktop\\楼\\微信支付账单(2).csv");
  106. charsetName = getCharsetName(file);
  107. csvParser =
  108. CSVFormat.EXCEL.parse(new InputStreamReader(new FileInputStream(file), charsetName));
  109. for (CSVRecord strings : csvParser) {
  110. System.out.println(strings);
  111. }
  112. }
  113. public Mapped storeUploaded(
  114. InputStream inputStream, String encoding, String filename, String contentType, String modelId) {
  115. ObjectId objectId;
  116. objectId = this.gridFsTemplate.store(inputStream, filename, contentType);
  117. Query query =
  118. new Query(
  119. Criteria.where("modelId")
  120. .is(modelId)
  121. .and("filename")
  122. .is(filename));
  123. List<LogicalFile> existsFileList = this.mongoTemplate.find(query, LogicalFile.class);
  124. if (existsFileList != null && existsFileList.size() > 0) {
  125. return Mapped.OK();
  126. }
  127. LogicalFile logicalFile = new LogicalFile();
  128. logicalFile.setFilename(filename);
  129. logicalFile.setUploadDate(DateUtil.now());
  130. logicalFile.setModelId(modelId);
  131. logicalFile.setGridId(objectId);
  132. logicalFile.setGenerated(Boolean.FALSE);
  133. logicalFile.setStatus("LOADING");
  134. logicalFile = this.mongoTemplate.insert(logicalFile);
  135. this.readFile(logicalFile, encoding);
  136. return Mapped.OK();
  137. }
  138. @Async
  139. public void readFile(LogicalFile uploaded, String encoding) {
  140. GridFSFile fsFile =
  141. this.fsOperations.findOne(new Query(Criteria.where("_id").is(uploaded.getGridId())));
  142. if (fsFile == null) {
  143. this.modifyStatus(uploaded.getId(), "ERROR");
  144. } else {
  145. GridFsResource resource = this.fsOperations.getResource(fsFile);
  146. String filename = resource.getFilename();
  147. if (StringUtils.isBlank(filename)) {
  148. this.modifyStatus(uploaded.getId(), "ERROR");
  149. } else {
  150. try {
  151. InputStream inputStream = resource.getInputStream();
  152. MongoExcelService mongoExcelService;
  153. if (filename.endsWith(".xlsx")) {
  154. mongoExcelService = new MongoExcelService(this.mongoTemplate, uploaded.getId());
  155. ExcelXlsxReader reader = new ExcelXlsxReader(mongoExcelService);
  156. reader.processStream(inputStream);
  157. mongoExcelService.insertRest();
  158. } else if (filename.endsWith(".xls")) {
  159. mongoExcelService = new MongoExcelService(this.mongoTemplate, uploaded.getId());
  160. ExcelXlsReader reader = new ExcelXlsReader(mongoExcelService);
  161. reader.processStream(inputStream);
  162. mongoExcelService.insertRest();
  163. } else if (filename.endsWith(".csv")) {
  164. mongoExcelService = new MongoExcelService(this.mongoTemplate, uploaded.getId());
  165. log.info("encoding2:{}", encoding);
  166. CSVParser csvParser = null;
  167. if (encoding.equalsIgnoreCase("UTF-8")) {
  168. csvParser = CSVFormat.EXCEL.parse(new InputStreamReader(inputStream, "utf-8"));
  169. } else {
  170. csvParser = CSVFormat.EXCEL.parse(new InputStreamReader(inputStream));
  171. }
  172. int curRow = 0;
  173. int curRowWxFile = 0;
  174. boolean wxFile = false;
  175. String nickname = ""; // 微信手机账单文件 添加一列 微信昵称 需要单独处理
  176. for (CSVRecord record : csvParser) {
  177. log.info("record,{}", record);
  178. List<String> strings = record.toList();
  179. List<String> strings2 = new ArrayList<>();
  180. strings2.addAll(strings);
  181. /************* 此处内容一下均为处理微信账单表头 ****************/
  182. String join = String.join("", strings);
  183. join = PublicStatic.removeAllSpecial(join);
  184. if (join.equals("微信支付账单明细")) {
  185. wxFile = true;
  186. }
  187. if (wxFile && curRowWxFile < 16) {
  188. if (curRowWxFile == 1) {
  189. String temp = strings.get(0);
  190. if (StringUtils.isNotBlank(temp)) {
  191. nickname = temp.replace("微信昵称:[", "").replace("]", "");
  192. }
  193. }
  194. curRowWxFile++;
  195. continue;
  196. }
  197. if (wxFile && curRowWxFile > 16) {
  198. strings2.add(0, nickname);
  199. curRowWxFile++;
  200. }
  201. if (wxFile && curRowWxFile == 16) {
  202. strings2.add(0, "微信昵称");
  203. curRowWxFile++;
  204. }
  205. /************* 此处内容以上均为处理微信账单表头 ****************/
  206. mongoExcelService.getRows(
  207. 0,
  208. curRow,
  209. strings2.stream()
  210. .map(PublicStatic::removeAllSpecial)
  211. .collect(Collectors.toList()));
  212. curRow++;
  213. }
  214. mongoExcelService.insertRest();
  215. } else if (filename.endsWith(".txt")) {
  216. mongoExcelService = new MongoExcelService(this.mongoTemplate, uploaded.getId());
  217. List<String> lines = new ArrayList<>();
  218. IoUtil.readLines(new InputStreamReader(inputStream, "utf-8"), lines);
  219. int curRow = 0;
  220. for (String string : lines) {
  221. if (string.equals("注销信息")) {
  222. break;
  223. }
  224. List<String> values = Arrays.asList(string.split("\t"));
  225. if (values.size() <= 1) {
  226. continue;
  227. }
  228. mongoExcelService.getRows(
  229. 0,
  230. curRow,
  231. values.stream().map(PublicStatic::removeAllSpecial).collect(Collectors.toList()));
  232. curRow++;
  233. }
  234. mongoExcelService.insertRest();
  235. }
  236. } catch (Exception var11) {
  237. this.modifyStatus(uploaded.getId(), "ERROR");
  238. var11.printStackTrace();
  239. }
  240. this.modifyStatus(uploaded.getId(), "FINISHED");
  241. }
  242. }
  243. }
  244. private void modifyStatus(String id, String status) {
  245. Query query = new Query();
  246. query.addCriteria(Criteria.where("_id").is(id));
  247. Update update = new Update();
  248. update.set("status", status);
  249. this.mongoTemplate.updateFirst(query, update, "logical_file");
  250. }
  251. public Mapped listGroupUploaded(FileListInput fileListInput) {
  252. String modelId = fileListInput.getModelId();
  253. if (modelId == null) {
  254. return Mapped.ERROR("没有选择任何分组");
  255. } else {
  256. Query query =
  257. new Query(Criteria.where("modelId").is(modelId).and("generated").is(Boolean.TRUE));
  258. query.with(Sort.by(Order.desc("uploadDate")));
  259. List<LogicalFile> resultList = this.mongoTemplate.find(query, LogicalFile.class);
  260. // 将文件 按照 反诈 财付通 五联单 进行分类
  261. List<GroupLogincalFile> groupLogincalFiles = new ArrayList<>();
  262. groupLogicalFile("反诈治安数据分析", new String[]{"反诈", "治安","经侦"}, resultList, groupLogincalFiles);
  263. groupLogicalFile("支付宝数据分析", new String[]{"五联单"}, resultList, groupLogincalFiles);
  264. groupLogicalFile("财付通数据分析", new String[]{"财付通"}, resultList, groupLogincalFiles);
  265. groupLogicalFile("数据综合分析", new String[]{"数据分析"}, resultList, groupLogincalFiles);
  266. Mapped res = Mapped.OK();
  267. res.put("code", 200);
  268. res.put("result", groupLogincalFiles);
  269. return res;
  270. }
  271. }
  272. public void groupLogicalFile(
  273. String groupName,
  274. String[] group,
  275. List<LogicalFile> logicalFileList,
  276. List<GroupLogincalFile> groupLogincalFiles) {
  277. List<LogicalFile> children = new ArrayList<>();
  278. for (String s : group) {
  279. children.addAll(
  280. logicalFileList.stream()
  281. .filter(t -> t.getFilename().startsWith(s))
  282. .collect(Collectors.toList()));
  283. }
  284. if (children != null && children.size() > 0) {
  285. GroupLogincalFile groupLogincalFile = new GroupLogincalFile();
  286. groupLogincalFile.setGroupName(groupName);
  287. groupLogincalFile.setId(IdUtil.randomUUID());
  288. groupLogincalFile.setChildren(orderGroupLogicalFile(groupName, children));
  289. groupLogincalFiles.add(groupLogincalFile);
  290. }
  291. }
  292. public List<LogicalFile> orderGroupLogicalFile(String groupName, List<LogicalFile> list) {
  293. if (groupName.equals("反诈治安数据分析")) {
  294. return orderGroupLogicalFileChild(order1, list);
  295. }
  296. if (groupName.equals("支付宝数据分析")) {
  297. return orderGroupLogicalFileChild(order2, list);
  298. }
  299. if (groupName.equals("财付通数据分析")) {
  300. return orderGroupLogicalFileChild(order3, list);
  301. }
  302. if (groupName.equals("数据综合分析")) {
  303. return orderGroupLogicalFileChild(order4, list);
  304. }
  305. return null;
  306. }
  307. public List<LogicalFile> orderGroupLogicalFileChild(String[] orders, List<LogicalFile> list) {
  308. List<LogicalFile> child = new ArrayList<>();
  309. for (String s : orders) {
  310. for (LogicalFile logicalFile : list) {
  311. if (logicalFile.getFilename().equals(s)) {
  312. if("数据分析-共同关系网络".equals(logicalFile.getFilename())){
  313. logicalFile.setFileType("map");
  314. }
  315. child.add(logicalFile);
  316. break;
  317. }
  318. }
  319. }
  320. return child;
  321. }
  322. public Mapped listUploaded(FileListInput fileListInput) {
  323. String modelId = fileListInput.getModelId();
  324. if (modelId == null) {
  325. return Mapped.ERROR("没有选择任何分组");
  326. } else {
  327. String generated = fileListInput.getGenerated();
  328. Future<Long> longFuture = this.fileService.countTotalByModelId(modelId, generated);
  329. Future<String> modelNameFuture = this.fileService.retrieveModelname(modelId);
  330. int page = fileListInput.getPage();
  331. int pageSize = fileListInput.getPageSize();
  332. int skip = (page - 1) * pageSize;
  333. Criteria criteria = Criteria.where("modelId").is(modelId);
  334. if ("true".equals(generated)) {
  335. criteria.and("generated").is(Boolean.TRUE);
  336. } else if ("false".equals(generated)) {
  337. criteria.and("generated").is(Boolean.FALSE);
  338. }
  339. Query query = new Query(criteria);
  340. query.with(Sort.by(Order.desc("uploadDate")));
  341. List<LogicalFile> resultList =
  342. this.mongoTemplate.find(query.skip(skip).limit(pageSize), LogicalFile.class);
  343. Long total;
  344. try {
  345. total = longFuture.get();
  346. } catch (ExecutionException | InterruptedException var17) {
  347. return Mapped.ERROR("查询出错");
  348. }
  349. String modelName;
  350. try {
  351. modelName = modelNameFuture.get();
  352. } catch (ExecutionException | InterruptedException var17) {
  353. return Mapped.ERROR("查询出错");
  354. }
  355. int i = skip;
  356. for (LogicalFile file : resultList) {
  357. ++i;
  358. file.setCount(i);
  359. file.setModelName(modelName);
  360. }
  361. Mapped res = Mapped.OK();
  362. res.put("code", 200);
  363. res.put("result", resultList);
  364. res.put("total", total);
  365. res.put("page", page);
  366. res.put("pageSize", pageSize);
  367. return res;
  368. }
  369. }
  370. public Mapped deleteById(IdInput idInput) {
  371. String id = idInput.getId();
  372. Query query = new Query(Criteria.where("id").is(id));
  373. LogicalFile file = this.mongoTemplate.findOne(query, LogicalFile.class);
  374. if (file != null && file.getGridId() != null) {
  375. String physicalId = file.getGridId().toHexString();
  376. this.fileService.removePhysicalFile(physicalId);
  377. }
  378. this.fileService.removeDataByFileId(id);
  379. this.fileService.removeFieldsByFileId(id);
  380. DeleteResult result = this.mongoTemplate.remove(query, LogicalFile.class);
  381. return result.getDeletedCount() > 0L ? Mapped.OK() : Mapped.ERROR("删除失败");
  382. }
  383. @Async
  384. Future<Long> countTotalByModelId(String modelId, String generated) {
  385. Criteria criteria = Criteria.where("modelId").is(modelId);
  386. if ("true".equals(generated)) {
  387. criteria.and("generated").is(Boolean.TRUE);
  388. } else if ("false".equals(generated)) {
  389. criteria.and("generated").is(Boolean.FALSE);
  390. }
  391. Long total = this.mongoTemplate.count(new Query(criteria), LogicalFile.class);
  392. return new AsyncResult(total);
  393. }
  394. @Async
  395. public void removePhysicalFile(String id) {
  396. this.fsOperations.delete(new Query(Criteria.where("_id").is(id)));
  397. }
  398. @Async
  399. public void removeLogicalFile(String fileId) {
  400. this.mongoTemplate.remove(new Query(Criteria.where("id").is(fileId)), LogicalFile.class);
  401. }
  402. @Async
  403. public void removeDataByFileId(String fileId) {
  404. this.mongoTemplate.remove(new Query(Criteria.where("fileId").is(fileId)), DataMap.class);
  405. }
  406. @Async
  407. public void removeFieldsByFileId(String fileId) {
  408. this.mongoTemplate.remove(new Query(Criteria.where("fileId").is(fileId)), Fields.class);
  409. }
  410. @Async
  411. Future<String> retrieveModelname(String modelId) {
  412. Query query = new Query(Criteria.where("id").is(modelId));
  413. query.fields().include("modelName");
  414. Model model = this.mongoTemplate.findOne(query, Model.class, "model");
  415. return new AsyncResult(model != null ? model.getModelName() : "");
  416. }
  417. }