AlipayService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package ieven.server.webapp.service.alipay;
  2. import ieven.server.webapp.domain.IdInput;
  3. import ieven.server.webapp.domain.alipay.HeaderProperties;
  4. import ieven.server.webapp.domain.alipay.TFromToFileIds;
  5. import ieven.server.webapp.domain.data.Fields;
  6. import ieven.server.webapp.domain.file.FileService;
  7. import ieven.server.webapp.domain.file.LogicalFile;
  8. import ieven.server.webapp.infrastructure.wrapper.Mapped;
  9. import ieven.server.webapp.service.FieldsService;
  10. import ieven.server.webapp.service.datastatic.*;
  11. import ieven.server.webapp.service.tenpay.*;
  12. import ieven.server.webapp.util.DataUtils;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.apache.commons.collections4.CollectionUtils;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.data.mongodb.core.MongoTemplate;
  19. import org.springframework.data.mongodb.core.query.Criteria;
  20. import org.springframework.data.mongodb.core.query.Query;
  21. import org.springframework.data.mongodb.core.query.Update;
  22. import org.springframework.stereotype.Service;
  23. import java.util.ArrayList;
  24. import java.util.Date;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.concurrent.ConcurrentHashMap;
  28. import java.util.concurrent.CountDownLatch;
  29. import java.util.concurrent.ExecutorService;
  30. import java.util.concurrent.Executors;
  31. @Slf4j
  32. @Service
  33. public class AlipayService {
  34. @Autowired
  35. FieldsService fieldsService;
  36. @Autowired
  37. private MongoTemplate mongoTemplate;
  38. @Autowired
  39. private HeaderProperties headerProperties;
  40. @Autowired
  41. private FileService fileService;
  42. @Value("${spring.data.fileExportPath}")
  43. private String path;
  44. public Mapped extractAll(String modelId) throws InterruptedException {
  45. final List<Fields> originFields = queryAllFields(modelId);
  46. // 先校验文件结构如果有不符合结构的文件 则暂停执行 给于提示
  47. fieldsService.checkAllFields(originFields);
  48. List<LogicalFile> logicalFiles = this.queryAllLogicalFiles2(modelId);
  49. List<String> checkResultStrings = new ArrayList<>();
  50. for (LogicalFile logicalFile : logicalFiles) {
  51. if (StringUtils.isBlank(logicalFile.getMatchType())) {
  52. checkResultStrings.add(logicalFile.getFilename() + "未匹配到文件校验规则,请检查!");
  53. }
  54. }
  55. if (checkResultStrings.size() > 0) {
  56. return Mapped.ERROR(String.join(" ", checkResultStrings));
  57. }
  58. // 先删除所有原来生成的表
  59. clearAllGenerated(modelId);
  60. // 查询出modelId的所有原始表头
  61. if (CollectionUtils.isEmpty(originFields)) {
  62. return Mapped.ERROR("文件为空");
  63. }
  64. // 无限制大小的threadpool
  65. ExecutorService exec = Executors.newCachedThreadPool();
  66. log.info("latch");
  67. CountDownLatch latch = new CountDownLatch(10);
  68. // 五联单-注册信息
  69. exec.execute(new ALIPAY_ORIGIN_REGISTER(originFields, modelId, mongoTemplate, latch));
  70. exec.execute(new ALIPAY_ORIGIN_REGISTER_JZY(originFields, modelId, mongoTemplate, latch));
  71. // 五联单-登录日志
  72. exec.execute(new ALIPAY_GENERATED_LOGIN(originFields, modelId, mongoTemplate, latch));
  73. // 五联单-交易记录
  74. exec.execute(new ALIPAY_GENERATED_TRADE(originFields, modelId, mongoTemplate, latch));
  75. // 五联单-账户明细
  76. exec.execute(new ALIPAY_GENERATED_ACCOUNT(originFields, modelId, mongoTemplate, latch));
  77. //经侦云-账户明细
  78. exec.execute(new ALIPAY_GENERATED_ACCOUNT_JZY(headerProperties, originFields, modelId, mongoTemplate, fileService, latch));
  79. // 五联单-转账明细
  80. exec.execute(new ALIPAY_GENERATED_TRANSFER(originFields, modelId, mongoTemplate, latch));
  81. // 反诈平台订单数据
  82. exec.execute(
  83. new AntiCheat(headerProperties, originFields, modelId, mongoTemplate, fileService, latch));
  84. // 治安平台订单数据
  85. exec.execute(
  86. new SecurityOrder(
  87. headerProperties, originFields, modelId, mongoTemplate, fileService, latch));
  88. // 经侦数据-订单数据
  89. exec.execute(
  90. new EconomicOrder(
  91. headerProperties, originFields, modelId, mongoTemplate, fileService, latch));
  92. // 网黑数据-订单数据
  93. latch.await();
  94. CountDownLatch latch15 = new CountDownLatch(1);
  95. exec.execute(
  96. new BlackOrder(
  97. headerProperties, originFields, modelId, mongoTemplate, fileService, latch15));
  98. latch15.await();
  99. //反诈治安-订单明细2
  100. CountDownLatch latch16 = new CountDownLatch(1);
  101. exec.execute(
  102. new AntiCheatTwo(
  103. headerProperties, originFields, modelId, mongoTemplate, fileService, latch16));
  104. latch16.await();
  105. log.info("latch");
  106. log.info("latch2");
  107. CountDownLatch latch2 = new CountDownLatch(7);
  108. // 五联单-账户透视
  109. exec.execute(
  110. new ScaleAnalysis(
  111. headerProperties, originFields, modelId, mongoTemplate, fileService, latch2));
  112. // 转账分析
  113. exec.execute(
  114. new TransferAnalysis(
  115. headerProperties, originFields, modelId, mongoTemplate, fileService, latch2));
  116. // 对手分析
  117. exec.execute(
  118. new EndAnalysis(
  119. headerProperties, originFields, modelId, mongoTemplate, fileService, latch2));
  120. // 收货地址分析
  121. exec.execute(
  122. new DeliveryAnalysis(
  123. headerProperties, originFields, modelId, mongoTemplate, fileService, latch2));
  124. // IP分析
  125. exec.execute(
  126. new IpAnalysis(
  127. headerProperties, originFields, modelId, mongoTemplate, fileService, latch2));
  128. // 存储了账号-户名的的信息
  129. Map<String, String> accountToName = new ConcurrentHashMap<>();
  130. // 反诈治安交易数据明细-从反诈读取户名
  131. exec.execute(new AntiSecurityTrade1(modelId, mongoTemplate, accountToName, latch2));
  132. // 反诈治安交易数据明细-从治安读取户名
  133. exec.execute(new AntiSecurityTrade2(modelId, mongoTemplate, accountToName, latch2));
  134. latch2.await();
  135. log.info("latch2");
  136. log.info("latch3");
  137. CountDownLatch latch3 = new CountDownLatch(1);
  138. // 反诈治安交易数据明细-从反诈读取 反诈治安交易数据明细合并到这里
  139. exec.execute(
  140. new AntiSecurityTrade3(
  141. headerProperties, modelId, mongoTemplate, accountToName, fileService, latch3));
  142. latch3.await();
  143. /* CountDownLatch latch11 = new CountDownLatch(1);
  144. // 反诈治安交易数据明细-从反诈读取 反诈治安交易数据明细合并到这里
  145. exec.execute(
  146. new AntiSecurityTrade5(headerProperties,originFields, modelId, mongoTemplate, fileService, latch11));
  147. latch11.await();*/
  148. // 反诈治安交易数据明细-从治安读取
  149. /*CountDownLatch latch8 = new CountDownLatch(1);
  150. exec.execute(
  151. new AntiSecurityTrade4(
  152. headerProperties, modelId, mongoTemplate, accountToName, fileService, latch8));
  153. latch8.await();*/
  154. log.info("latch3");
  155. log.info("latch4");
  156. CountDownLatch latch4 = new CountDownLatch(2);
  157. // 反诈治安-账户透视
  158. exec.execute(
  159. new AntiSecurityTradeScaleAnalysis(
  160. headerProperties, modelId, mongoTemplate, fileService, latch4));
  161. // 反诈治安-对手分析
  162. exec.execute(
  163. new AntiSecurityTradeEndAnalysis(
  164. headerProperties, modelId, mongoTemplate, fileService, latch4));
  165. latch4.await();
  166. log.info("latch4");
  167. log.info("latch5");
  168. CountDownLatch latch5 = new CountDownLatch(1);
  169. // 财付通 开户信息
  170. exec.execute(
  171. new TenpayAccount(
  172. originFields, modelId, mongoTemplate, fileService, accountToName, latch5));
  173. latch5.await();
  174. log.info("latch5");
  175. log.info("latch6");
  176. CountDownLatch latch6 = new CountDownLatch(1);
  177. // 财付通-订单明细
  178. exec.execute(new TenpayOrders(originFields, modelId, mongoTemplate, fileService, latch6));
  179. latch6.await();
  180. log.info("latch6");
  181. log.info("latch8");
  182. CountDownLatch latch8 = new CountDownLatch(2);
  183. // 财付通-订单明细
  184. exec.execute(new TenpayOrders0422(originFields, modelId, mongoTemplate, fileService, latch8));
  185. exec.execute(new TenpayShopOrders(originFields, modelId, mongoTemplate, fileService, latch8));
  186. latch8.await();
  187. log.info("latch8");
  188. log.info("latch7");
  189. CountDownLatch latch9 = new CountDownLatch(1);
  190. // 财付通-交易明细
  191. exec.execute(new TenpayTrades(modelId, mongoTemplate, accountToName, fileService, latch9));
  192. latch9.await();
  193. CountDownLatch latch7 = new CountDownLatch(3);
  194. // 财付通-手机明细
  195. exec.execute(new TenpayMobile(originFields, modelId, mongoTemplate, fileService, latch7));
  196. // 财付通-对手透视
  197. exec.execute(
  198. new TenpayTradesEndAnalysis(headerProperties, modelId, mongoTemplate, fileService, latch7));
  199. // 财付通-账户透视
  200. exec.execute(
  201. new TenpayTradesScaleAnalysis(
  202. headerProperties, modelId, mongoTemplate, fileService, latch7));
  203. latch7.await();
  204. //主体账户信息
  205. CountDownLatch latch10 = new CountDownLatch(1);
  206. exec.execute(
  207. new DataStaticAnalysis(
  208. headerProperties, modelId, mongoTemplate, fileService, latch10));
  209. latch10.await();
  210. //主体账户信息
  211. CountDownLatch latch11 = new CountDownLatch(1);
  212. exec.execute(
  213. new DataStaticAnalysisTotal(
  214. headerProperties, modelId, mongoTemplate, fileService, latch11));
  215. latch11.await();
  216. //数据统计-账户透视
  217. CountDownLatch latch12 = new CountDownLatch(1);
  218. exec.execute(
  219. new DataStaticAnalysisMain(
  220. headerProperties, modelId, mongoTemplate, fileService, latch12));
  221. latch12.await();
  222. //数据统计-对手透视
  223. CountDownLatch latch13 = new CountDownLatch(1);
  224. exec.execute(
  225. new DataStaticAnalysisDs(
  226. headerProperties, modelId, mongoTemplate, fileService, latch13));
  227. latch13.await();
  228. //数据统计-生成资金excel
  229. CountDownLatch latch14 = new CountDownLatch(1);
  230. exec.execute(new DataStaticExportExcel(modelId, mongoTemplate, fileService, latch14, path));
  231. latch14.await();
  232. reorder(modelId);
  233. log.info("执行完毕");
  234. return Mapped.OK();
  235. }
  236. // 对文件重排序
  237. private void reorder(String modelId) {
  238. List<String> orderedNames = headerProperties.getOrderedNames();
  239. Query query =
  240. new Query(Criteria.where("modelId").is(modelId).and("generated").is(Boolean.TRUE));
  241. List<LogicalFile> generated = mongoTemplate.find(query, LogicalFile.class);
  242. // current
  243. Date current = new Date();
  244. long currentMills = current.getTime();
  245. for (int i = 0; i < orderedNames.size(); i++) {
  246. for (LogicalFile logicalFile : generated) {
  247. String filename = logicalFile.getFilename();
  248. if (orderedNames.get(i).equals(filename)) {
  249. // 减1秒,造成顺序递减
  250. current.setTime(currentMills - (long) i * 1000);
  251. Update update = new Update().set("uploadDate", DataUtils.getDate(current));
  252. String id = logicalFile.getId();
  253. mongoTemplate.updateFirst(
  254. new Query(Criteria.where("id").is(id)), update, LogicalFile.class);
  255. break;
  256. }
  257. }
  258. }
  259. }
  260. public List<LogicalFile> queryAllLogicalFiles(String modelId) {
  261. Query query =
  262. new Query(Criteria.where("modelId").is(modelId).and("generated").is(Boolean.TRUE));
  263. return mongoTemplate.find(query, LogicalFile.class);
  264. }
  265. public List<LogicalFile> queryAllLogicalFiles2(String modelId) {
  266. Query query =
  267. new Query(Criteria.where("modelId").is(modelId).and("generated").is(Boolean.FALSE));
  268. return mongoTemplate.find(query, LogicalFile.class);
  269. }
  270. private void clearAllGenerated(String modelId) {
  271. List<LogicalFile> logicalFiles = queryAllLogicalFiles(modelId);
  272. for (LogicalFile old : logicalFiles) {
  273. IdInput idInput = new IdInput();
  274. idInput.setId(old.getId());
  275. fileService.deleteById(idInput);
  276. }
  277. mongoTemplate.remove(new Query(Criteria.where("modelId").is(modelId)), TFromToFileIds.class);
  278. }
  279. private List<Fields> queryAllFields(String modelId) {
  280. Query query = new Query(Criteria.where("modelId").is(modelId).and("generated").is(Boolean.FALSE));
  281. List<LogicalFile> fileList = mongoTemplate.find(query, LogicalFile.class);
  282. List<String> fileIdList = new ArrayList<>();
  283. for (LogicalFile file : fileList) {
  284. String fileId = file.getId();
  285. fileIdList.add(fileId);
  286. }
  287. Query query1 = new Query(Criteria.where("fileId").in(fileIdList));
  288. return mongoTemplate.find(query1, Fields.class);
  289. }
  290. }