FileService.java 19 KB

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