You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

693 lines
30 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. package com.inscloudtech.bankStatementAnalysis.helper;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.util.IdUtil;
  5. import cn.hutool.core.util.NumberUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.alibaba.excel.EasyExcel;
  8. import com.alibaba.excel.ExcelReader;
  9. import com.alibaba.excel.context.AnalysisContext;
  10. import com.alibaba.excel.exception.ExcelAnalysisStopException;
  11. import com.alibaba.excel.read.listener.ReadListener;
  12. import com.alibaba.excel.read.metadata.ReadSheet;
  13. import com.alibaba.excel.util.ListUtils;
  14. import com.aspose.cells.Cell;
  15. import com.aspose.cells.Cells;
  16. import com.aspose.cells.Workbook;
  17. import com.aspose.cells.Worksheet;
  18. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  19. import com.inscloudtech.bankStatementAnalysis.mapper.ESOpeningAccountInfoMapper;
  20. import com.inscloudtech.common.constant.Constants;
  21. import com.inscloudtech.common.exception.dc.AnalyzeDataFailedException;
  22. import com.inscloudtech.common.exception.dc.ImportDataFailedException;
  23. import com.inscloudtech.common.exception.dc.TemplateNotFindException;
  24. import com.inscloudtech.common.utils.bean.BeanUtils;
  25. import com.inscloudtech.converter.ExcelStringToJavaBigDecimalConverter;
  26. import com.inscloudtech.datacenter.domain.PlateNumberInfo;
  27. import com.inscloudtech.bankStatementAnalysis.mapper.EsCITICBankStatementMapper;
  28. import com.inscloudtech.bankStatementAnalysis.mapper.EsCITICOpeningAccountInfoMapper;
  29. import com.inscloudtech.datacenter.service.ImportResultService;
  30. import com.inscloudtech.bankStatementAnalysis.listener.CITICBankOpeningAccountInfoReadListener;
  31. import com.inscloudtech.datacenter.domain.BankStatement;
  32. import com.inscloudtech.datacenter.domain.OpeningAccountInfo;
  33. import com.inscloudtech.bankStatementAnalysis.domain.entity.CITICBankStatementEntity;
  34. import com.inscloudtech.bankStatementAnalysis.domain.entity.CITICOpeningAccountInfoEntity;
  35. import com.inscloudtech.bankStatementAnalysis.util.AsposeUtil;
  36. import lombok.RequiredArgsConstructor;
  37. import lombok.extern.slf4j.Slf4j;
  38. import org.springframework.stereotype.Component;
  39. import java.io.File;
  40. import java.math.BigDecimal;
  41. import java.util.*;
  42. import java.util.stream.Collectors;
  43. /**
  44. * 中信银行数据分析
  45. */
  46. @RequiredArgsConstructor
  47. @Component
  48. @Slf4j
  49. public class CITICDataAnalysisHelper {
  50. private final ImportResultService importResultService;
  51. private final EsCITICOpeningAccountInfoMapper esCITICOpeningAccountInfoMapper;
  52. private final EsCITICBankStatementMapper esCITICBankStatementMapper;
  53. private final ESOpeningAccountInfoMapper esOAIMapper;
  54. private final static String BANK_NAME = "中信银行";
  55. private String caseId = "";
  56. public void analyzeData(String caseId) throws Exception {
  57. // 开户信息分析
  58. analyzeOAI( caseId);
  59. analyzeBS(caseId);
  60. }
  61. public void importData(File dir,String caseId) throws Exception {
  62. this.caseId = caseId;
  63. if (!dir.exists()) {
  64. return;
  65. }
  66. List<File> fileList = FileUtil.loopFiles(dir);
  67. // // pdf
  68. // List<File> pdfFileList =
  69. // fileList.stream().filter(f -> f.getName().endsWith(".pdf")).collect(Collectors.toList());
  70. //
  71. //
  72. // // pdf转化为Excel
  73. // for (File f : pdfFileList) {
  74. // try {
  75. // String excelFilename = AsposeUtil.pdf2ExcelV6(f.getAbsolutePath());
  76. // excelFile.add(new File(excelFilename));
  77. // } catch (Exception e) {
  78. // log.error("pdf文件转excel失败:" + e.getMessage(), e);
  79. // throw new ImportDataFailedException("pdf文件转excel失败:" + f.getAbsolutePath());
  80. // }
  81. // }
  82. List<File> excelFile = HelperUtil.getExcelFile(fileList);
  83. for (File file : excelFile) {
  84. try {
  85. String absolutePath = file.getAbsolutePath();
  86. // 读取文件来确认是开户信息,还是流水信息
  87. // 这种方式是有漏洞的,一个文件,既可能包含开户信息,也可能包含流水信心
  88. Workbook wb = new Workbook(absolutePath);
  89. for (int sheetNo = 0; sheetNo < wb.getWorksheets().getCount(); sheetNo++) {
  90. try {
  91. Cells cells = wb.getWorksheets().get(sheetNo).getCells();
  92. String sourceFile = HelperUtil.getSourceFileName(absolutePath,BANK_NAME);
  93. Cell creditCardDateCell = AsposeUtil.getCell(cells, "入账日期");
  94. Cell tradeCell = AsposeUtil.getCell(cells, "交易日期");
  95. if (AsposeUtil.getCell(cells, "开户日期") != null) {
  96. if((AsposeUtil.getCell(cells, "地址") != null
  97. && !Objects.equals(
  98. Objects.requireNonNull(AsposeUtil.getCell(cells, "地址"))
  99. .getStringValue(),
  100. "IP地址"))){
  101. // 解析开户信息
  102. readOAI(file, cells, sheetNo);
  103. }else {
  104. try (ExcelReader excelReader = EasyExcel.read(file).build()) {
  105. ReadSheet readSheet = EasyExcel.readSheet(sheetNo)
  106. .headRowNumber(1)
  107. .head(CITICOpeningAccountInfoEntity.class)
  108. .registerReadListener(new CITICBankOpeningAccountInfoReadListener(
  109. esCITICOpeningAccountInfoMapper, null))
  110. .build();
  111. excelReader.read(readSheet);
  112. } catch (Exception e) {
  113. throw new ImportDataFailedException(e.getMessage(), sourceFile);
  114. }
  115. }
  116. } else if (tradeCell != null) {
  117. if(AsposeUtil.getCell(cells, "交易时间") != null){
  118. readBSV4(wb, file, cells, sheetNo);
  119. /**20240418新信用卡流水模板*/
  120. }else {
  121. /**20240428新流水模板*/
  122. readCreditCard(absolutePath,sheetNo,tradeCell);
  123. }
  124. }else if(creditCardDateCell != null){
  125. /**20240418新信用卡流水模板*/
  126. readCreditCard(absolutePath,sheetNo,creditCardDateCell);
  127. }else {
  128. // throw new ImportDataFailedException("文件无对应模板", sourceFile);
  129. }
  130. }catch (Exception e){
  131. importResultService.record(caseId,BANK_NAME,e);
  132. }
  133. }
  134. }catch (Exception e){
  135. importResultService.record(caseId,BANK_NAME,e);
  136. }
  137. }
  138. }
  139. private void analyzeBS(String caseId) {
  140. List<BankStatement> bsList = ListUtils.newArrayListWithExpectedSize(Constants.BATCH_SIZE);
  141. List<CITICBankStatementEntity> entityList =
  142. HelperUtil.getEntityList(esCITICBankStatementMapper, CITICBankStatementEntity.class);
  143. List<OpeningAccountInfo> oaiList = HelperUtil.getEntityListV2(esOAIMapper, OpeningAccountInfo.class, caseId);
  144. Map<String, List<OpeningAccountInfo>> groupByName = oaiList.stream().filter(item ->StrUtil.isNotEmpty(item.getName()))
  145. .collect(Collectors.groupingBy(OpeningAccountInfo::getName));
  146. Map<String, List<OpeningAccountInfo>> groupByAccountNumber = oaiList.stream().filter(item ->StrUtil.isNotEmpty(item.getAccountNumber()))
  147. .collect(Collectors.groupingBy(OpeningAccountInfo::getAccountNumber));
  148. Map<String, List<OpeningAccountInfo>> groupByIdCard = oaiList.stream().filter(item ->StrUtil.isNotEmpty(item.getIdNo()))
  149. .collect(Collectors.groupingBy(OpeningAccountInfo::getIdNo));
  150. Map<String, String> nameCardNumberMap = new HashMap<>();
  151. Map<String, String> cardAndIdCardMap = new HashMap<>();
  152. Set<String> uniqueKeySet = new HashSet();
  153. List<PlateNumberInfo> plateNumberInfoList = new ArrayList<>();
  154. for (CITICBankStatementEntity entity : entityList) {
  155. String sourceFile = entity.getSourceFile();
  156. try {
  157. BankStatement bs = new BankStatement();
  158. bs.setBankName(BANK_NAME);
  159. String cardNumber = entity.getCardNumber();
  160. bs.setCardNumber(cardNumber);
  161. String customerName = entity.getCustomerName();
  162. if(StrUtil.isEmpty(customerName)){//流水名字为空,通过卡号关联出名字
  163. if(groupByAccountNumber.containsKey(cardNumber)){
  164. List<OpeningAccountInfo> openingAccountInfos = groupByAccountNumber.get(cardNumber);
  165. for (OpeningAccountInfo info : openingAccountInfos) {
  166. if(StrUtil.isNotEmpty(info.getName())){
  167. customerName = info.getName();
  168. break;
  169. }
  170. }
  171. }
  172. }
  173. bs.setCardHolderName(customerName);
  174. if (StringUtils.isNotEmpty(cardNumber)) {
  175. nameCardNumberMap.put(customerName, cardNumber);
  176. if(groupByAccountNumber.containsKey(cardNumber)){
  177. List<OpeningAccountInfo> openingAccountInfos = groupByAccountNumber.get(cardNumber);
  178. for (OpeningAccountInfo info : openingAccountInfos) {
  179. if(StrUtil.isNotEmpty(info.getIdNo())){
  180. bs.setIdCardNo(info.getIdNo());
  181. break;
  182. }
  183. }
  184. }
  185. // 开户信息中没有的卡号,根据姓名来查询
  186. // 姓名为空,则不处理
  187. if (StringUtils.isNotEmpty(customerName)) {
  188. if(groupByName.containsKey(customerName)){
  189. List<OpeningAccountInfo> infoList = groupByName.get(customerName);
  190. for (OpeningAccountInfo info : infoList) {
  191. if(StrUtil.isNotEmpty(info.getIdNo())){
  192. bs.setIdCardNo(info.getIdNo());
  193. break;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. if(StrUtil.isEmpty(bs.getIdCardNo()) && StrUtil.isNotEmpty(entity.getIdCardNo())){
  200. bs.setIdCardNo(entity.getIdCardNo());
  201. }
  202. if(StrUtil.isEmpty(bs.getCardHolderName()) && StrUtil.isNotEmpty(bs.getIdCardNo())){
  203. if(groupByIdCard.containsKey(bs.getIdCardNo())){
  204. List<OpeningAccountInfo> openingAccountInfos = groupByIdCard.get(bs.getIdCardNo());
  205. for (OpeningAccountInfo info : openingAccountInfos) {
  206. if(StrUtil.isNotEmpty(info.getName())){
  207. bs.setCardHolderName(info.getName());
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. // 交易时间
  214. String transDate = entity.getTransDate();
  215. if (StrUtil.isNotEmpty(transDate)) {
  216. // 三种情况,/ - 以及没有符号
  217. String format = null;
  218. if (transDate.contains("/")) {
  219. format = "yyyy/MM/dd";
  220. } else if (transDate.contains("-")) {
  221. format = "yyyy-MM-dd";
  222. } else {
  223. format = "yyyyMMdd";
  224. }
  225. String transTime = entity.getTransTime();
  226. if (StrUtil.isNotEmpty(transTime)) {
  227. // 两种情况,一种是包含冒号,一种没有
  228. if (transTime.contains(":")) {
  229. format = format + " HH:mm:ss";
  230. } else {
  231. format = format + " HHmmss";
  232. }
  233. // 如果不足六位,则补足
  234. if (transTime.length() < Constants.TIME_FULL_FORMAT_LENGTH) {
  235. int cnt = Constants.TIME_FULL_FORMAT_LENGTH - transTime.length();
  236. StringBuilder transTimeBuilder = new StringBuilder(transTime);
  237. for (int i = 0; i < cnt; i++) {
  238. transTimeBuilder.insert(0, "0");
  239. }
  240. transTime = String.valueOf(transTimeBuilder);
  241. }
  242. try {
  243. bs.setTransactionTime(DateUtil.parse(transDate + " " + transTime, format));
  244. } catch (Exception e) {
  245. throw new AnalyzeDataFailedException(
  246. StrUtil.format("解析交易时间异常[{}]", entity.getTransDate() + " " + entity.getTransTime()), e, entity.getSourceFile());
  247. }
  248. } else {
  249. try {
  250. bs.setTransactionTime(DateUtil.parse(transDate, format));
  251. } catch (Exception e) {
  252. throw new AnalyzeDataFailedException(StrUtil.format("解析交易时间异常[{}]", transDate), e,entity.getSourceFile());
  253. }
  254. }
  255. }
  256. // 交易金额
  257. // 需要根据借贷标记来计算
  258. // c+ d-
  259. String creditMark = entity.getCreditMark();
  260. if (StrUtil.isNotEmpty(creditMark)) {
  261. BigDecimal transactionAmount = NumberUtil.toBigDecimal(entity.getTransactionAmount());
  262. if (creditMark.toLowerCase().contains("c")) {
  263. bs.setTransactionAmount(transactionAmount);
  264. } else {
  265. bs.setTransactionAmount(BigDecimal.ZERO.subtract(transactionAmount));
  266. }
  267. } else if (entity.getCreditCard()){ // 没有借贷标志,说明交易金额是零
  268. bs.setTransactionAmount(NumberUtil.toBigDecimal(entity.getTransactionAmount()));
  269. }else {
  270. bs.setTransactionAmount(BigDecimal.ZERO);
  271. }
  272. // 余额
  273. bs.setBalance(NumberUtil.toBigDecimal(entity.getBalance()));
  274. // 交易对手
  275. String counterpartName = entity.getCounterpartyName();
  276. bs.setCounterpartyName(counterpartName);
  277. // 交易币种
  278. bs.setTransCurrencyType(Constants.CURRENCY_TYPE_CHINA);
  279. bs.setCounterpartyAccount(entity.getCounterpartyAccount());
  280. bs.setCounterpartyBankName(entity.getCounterpartyBankName());
  281. bs.setSummary(entity.getSummary());
  282. bs.setTransRemark(entity.getTransRemark());
  283. bs.setTransChannel(entity.getTransChannel());
  284. cardAndIdCardMap.put(bs.getCardNumber(),bs.getIdCardNo());
  285. String md5Id = HelperUtil.generateMD5Id(bs,caseId);
  286. //未导入数据内部去重
  287. if(HelperUtil.deduplication(md5Id,uniqueKeySet)){
  288. continue;
  289. }
  290. bs.setSourceFile(entity.getSourceFile());
  291. bs.setId(md5Id);
  292. bs.setCaseId(caseId);
  293. try {
  294. BeanUtils.beanAttributeValueTrim(bs);
  295. } catch (Exception e) {
  296. e.printStackTrace();
  297. }
  298. bsList.add(bs);
  299. HelperUtil.extractPlateNumber(bs,plateNumberInfoList);
  300. if (bsList.size() >= Constants.BATCH_SIZE) {
  301. bsList = handleCardNumber(bsList, nameCardNumberMap);
  302. // 批量保存
  303. // 保存数据库
  304. List<BankStatement> dest = HelperUtil.getDest(bsList);
  305. HelperUtil.batchInsert2Es(dest, caseId);
  306. bsList = ListUtils.newArrayListWithExpectedSize(Constants.BATCH_SIZE);
  307. }
  308. } catch (Exception e) {
  309. importResultService.record(caseId, BANK_NAME, e,sourceFile);
  310. }
  311. }
  312. uniqueKeySet.clear();
  313. HelperUtil.batchInsertPlateNumber(plateNumberInfoList);
  314. if (!bsList.isEmpty()) {
  315. //注释原因该方法导致某些流水姓名字段为空
  316. // bsList = handleCardNumber(bsList, nameCardNumberMap);
  317. List<BankStatement> dest = HelperUtil.getDest(bsList);
  318. HelperUtil.batchInsert2Es(dest, caseId);
  319. }
  320. }
  321. /**
  322. * 为什么再次 if (StrUtil.isNotEmpty(cardNumber)) { bs.setCardNumber(no);
  323. * @param bsList
  324. * @param nameCardNumberMap
  325. * @return
  326. */
  327. private List<BankStatement> handleCardNumber(List<BankStatement> bsList, Map<String, String> nameCardNumberMap) {
  328. return bsList.stream()
  329. .peek(bs -> {
  330. String cardNumber = bs.getCardNumber();
  331. if (StrUtil.isNotEmpty(cardNumber)) {
  332. String cardHolderName = bs.getCardHolderName();
  333. if (StrUtil.isNotEmpty(cardHolderName)) {
  334. String no = nameCardNumberMap.getOrDefault(cardHolderName, null);
  335. if (StrUtil.isNotEmpty(no)) {
  336. bs.setCardNumber(no);
  337. }
  338. }
  339. }
  340. })
  341. .collect(Collectors.toList());
  342. }
  343. private void analyzeOAI( String caseId) {
  344. List<OpeningAccountInfo> oaiList = ListUtils.newArrayListWithExpectedSize(Constants.BATCH_SIZE);
  345. // ES中有开户信息
  346. List<CITICOpeningAccountInfoEntity> entityList =
  347. HelperUtil.getEntityList(esCITICOpeningAccountInfoMapper, CITICOpeningAccountInfoEntity.class);
  348. Set<String> uniqueKeySet = new HashSet();
  349. for (CITICOpeningAccountInfoEntity entity : entityList) {
  350. if (StringUtils.isEmpty(entity.getCardHolderName())) {
  351. continue;
  352. }
  353. OpeningAccountInfo oai = new OpeningAccountInfo();
  354. oai.setName(entity.getCardHolderName());
  355. oai.setBankName(BANK_NAME);
  356. oai.setIdType(entity.getIdType());
  357. oai.setIdNo(entity.getIdCardNo());
  358. oai.setPhone(entity.getPhone());
  359. oai.setAddress(entity.getAddress());
  360. String accountNumber = StrUtil.isEmpty(entity.getAccountNumberItem())?entity.getAccountNumber():entity.getAccountNumberItem();
  361. oai.setAccountNumber(accountNumber);
  362. oai.setStatus(entity.getStatus());
  363. oai.setOpeningAccountDate(entity.getOpeningAccountDate());
  364. oai.setBalance(entity.getBalance());
  365. String md5Id = HelperUtil.generateMD5Id4OAI(oai,caseId);
  366. //未导入数据内部去重
  367. if(HelperUtil.deduplication(md5Id,uniqueKeySet)){
  368. continue;
  369. }
  370. oai.setId(md5Id);
  371. oai.setCaseId(caseId);
  372. oaiList.add(oai);
  373. if (oaiList.size() >= Constants.BATCH_SIZE) {
  374. List<OpeningAccountInfo> dest = HelperUtil.getDest(oaiList);
  375. HelperUtil.batchSaveOAI2Es(dest, caseId);
  376. }
  377. }
  378. if (!oaiList.isEmpty()) {
  379. List<OpeningAccountInfo> dest = HelperUtil.getDest(oaiList);
  380. HelperUtil.batchSaveOAI2Es(dest, caseId);
  381. }
  382. }
  383. public ReadListener<CITICBankStatementEntity> creditCardReadListener(String sourceFile) {
  384. return new ReadListener<CITICBankStatementEntity>() {
  385. List<CITICBankStatementEntity> cacheList = ListUtils.newArrayListWithExpectedSize(Constants.BATCH_SIZE);
  386. @Override
  387. public void invoke(CITICBankStatementEntity entity, AnalysisContext context) {
  388. entity.setId(IdUtil.objectId());
  389. entity.setSourceFile(sourceFile);
  390. entity.setCreditCard(true);
  391. entity.setBalance("0.0");
  392. cacheList.add(entity);
  393. if (cacheList.size() >= Constants.BATCH_SIZE) {
  394. saveData2Es();
  395. }
  396. }
  397. @Override
  398. public void doAfterAllAnalysed(AnalysisContext context) {
  399. if (!cacheList.isEmpty()) {
  400. saveData2Es();
  401. }
  402. }
  403. private void saveData2Es() {
  404. esCITICBankStatementMapper.insertBatch(cacheList);
  405. cacheList = ListUtils.newArrayListWithExpectedSize(Constants.BATCH_SIZE);
  406. }
  407. };
  408. }
  409. private void readCreditCard(String excelFileName,int sheetNum,Cell creditCardDateCell) {
  410. String sourceFile = HelperUtil.getSourceFileName(excelFileName,BANK_NAME);
  411. int headRowNumber = creditCardDateCell.getRow() + 1;
  412. try (ExcelReader reader = EasyExcel.read(excelFileName).build()) {
  413. ReadSheet sheet = EasyExcel.readSheet(sheetNum)
  414. .head(CITICBankStatementEntity.class)
  415. .headRowNumber(headRowNumber)
  416. .registerReadListener(creditCardReadListener(sourceFile))
  417. .build();
  418. reader.read(sheet);
  419. } catch (Exception e) {
  420. // log.error("读取私人银行流水出错:{}", e.getMessage(), e);
  421. throw new ImportDataFailedException(
  422. StrUtil.format("读取私人银行流水出错, 请检查文件【{}秒】是否正确。", sourceFile), sourceFile);
  423. }
  424. }
  425. private void readBSV4(Workbook wb, File excelFile, Cells cells, int sheetNo) throws Exception {
  426. // 表头
  427. List<Cell> headerCellList = AsposeUtil.find(cells, "交易日期");
  428. for (Cell headerCell : headerCellList) {
  429. int headerRowNum = headerCell.getRow() + 1;
  430. boolean modified = fixWorksheetAndReadExcelFile(wb, cells, excelFile, headerCell, headerRowNum, sheetNo);
  431. if (modified) {
  432. wb = new Workbook(excelFile.getAbsolutePath());
  433. Worksheet worksheet = wb.getWorksheets().get(sheetNo);
  434. cells = worksheet.getCells();
  435. }
  436. }
  437. }
  438. private boolean fixWorksheetAndReadExcelFile(Workbook wb, Cells cells, File excelFile, Cell headerCell, int headRowNum, int sheetNo)
  439. throws Exception {
  440. int customerInfoRow = headerCell.getRow() - 1;
  441. boolean isModified = false;
  442. // 需要分类
  443. // 先根据客户名来分,一种有客户名,一种没有客户名
  444. Cell customerNameCell = AsposeUtil.getCell(cells, "客户名", Math.max(customerInfoRow, 0));
  445. String pathname = excelFile.getAbsolutePath();
  446. String sourceFile = HelperUtil.getSourceFileName(pathname,BANK_NAME);
  447. if (customerNameCell != null && customerNameCell.getRow() == customerInfoRow) {
  448. // 有客户名的又分为两种,
  449. // 一种是账号在外,一种是表内有客户账号
  450. Cell cell = AsposeUtil.getCell(cells, "账号", Math.max(customerInfoRow, 0));
  451. if (cell != null && cell.getRow() <= headRowNum - 1) {
  452. String customerName = null;
  453. // 获取客户名
  454. String v = customerNameCell.getStringValue();
  455. if (StrUtil.isNotEmpty(v)) {
  456. if (v.contains(":") || v.contains(":")) {
  457. v = v.replace(":", ":");
  458. customerName =
  459. v.substring(v.indexOf(":") + 1).trim();
  460. } else {
  461. customerName = AsposeUtil.getNextCellValue(pathname, 0, customerNameCell);
  462. }
  463. }
  464. if (cell.getRow() < headRowNum - 1) {
  465. // 一种,客户账号在表外
  466. String cardNumber = null;
  467. // 获取账号
  468. String value = cell.getStringValue();
  469. if (StrUtil.isNotEmpty(value)) {
  470. if (value.contains(":") || value.contains(":")) {
  471. value = value.replace(":", ":");
  472. cardNumber = value.substring(value.indexOf(":") + 1).trim();
  473. } else {
  474. cardNumber = AsposeUtil.getNextCellValue(pathname, 0, cell);
  475. }
  476. }
  477. Cell c = AsposeUtil.getCell(cells, "客户名", headerCell.getRow() + 1);
  478. int endRowNum = -1;
  479. if (c != null) {
  480. endRowNum = c.getRow();
  481. } else {
  482. endRowNum = cells.getMaxRow();
  483. }
  484. if (StrUtil.isNotEmpty(cardNumber)) {
  485. fix(wb, pathname, cells, headerCell, endRowNum, "客户账号", cardNumber);
  486. isModified = true;
  487. excelFile = new File(pathname);
  488. }
  489. if (StrUtil.isNotEmpty(customerName)) {
  490. fix(wb, pathname, cells, headerCell, endRowNum, "客户名称", customerName);
  491. isModified = true;
  492. excelFile = new File(pathname);
  493. }
  494. readBSV5(pathname, headRowNum, endRowNum, sheetNo);
  495. } else {
  496. // 一种,客户账号在表内
  497. try {
  498. // 分析
  499. Cell c = AsposeUtil.getCell(cells, "客户名", headerCell.getRow() + 1);
  500. int endRowNum = -1;
  501. if (c != null) {
  502. endRowNum = c.getRow();
  503. } else {
  504. endRowNum = cells.getMaxRow();
  505. }
  506. if (StrUtil.isNotEmpty(customerName)) {
  507. // 添加一列
  508. fix(wb, pathname, cells, headerCell, endRowNum, "客户名称", customerName);
  509. isModified = true;
  510. excelFile = new File(pathname);//??
  511. }
  512. readBSV5(pathname, headRowNum, endRowNum, sheetNo);
  513. } catch (Exception e) {
  514. log.error("解析Excel文件失败.", e);
  515. throw new ImportDataFailedException(e.getMessage(), pathname);
  516. }
  517. }
  518. }
  519. } else {
  520. if(customerNameCell == null){
  521. /**20240418新模板*/
  522. customerNameCell = AsposeUtil.getCell(cells, "客户名", Math.max(customerInfoRow - 1, 0));
  523. if(customerNameCell != null){
  524. fix(wb, pathname, cells, headerCell, cells.getMaxRow(), "客户名称", cells.get(customerInfoRow, 1).getStringValue());
  525. readBSV5(excelFile.getAbsolutePath(), headRowNum, cells.getMaxRow() + 1, sheetNo);
  526. }else {
  527. throw new TemplateNotFindException(sourceFile);
  528. }
  529. }else {
  530. readBSV5(excelFile.getAbsolutePath(), headRowNum, cells.getMaxRow() + 1, sheetNo);
  531. }
  532. }
  533. return isModified;
  534. }
  535. private void readBSV5(String pathname, int headRowNumber, int endRowNum, int sheetNo) {
  536. String sourceFile = HelperUtil.getSourceFileName(pathname,BANK_NAME);
  537. try (ExcelReader reader = EasyExcel.read(pathname).build()) {
  538. ReadSheet rs = EasyExcel.readSheet(sheetNo)
  539. .headRowNumber(headRowNumber)
  540. .head(CITICBankStatementEntity.class)
  541. .registerReadListener(HelperUtil.getReadListener(
  542. esCITICBankStatementMapper,
  543. CITICBankStatementEntity.class,
  544. headRowNumber,
  545. endRowNum ,pathname))
  546. .build();
  547. reader.read(rs);
  548. } catch (Exception e) {
  549. if (e instanceof ExcelAnalysisStopException) {
  550. return;
  551. }
  552. log.error("解析Excel文件失败.", e);
  553. throw new ImportDataFailedException(e.getMessage(), sourceFile);
  554. }
  555. }
  556. private void fix(Workbook wb, String filename, Cells cells, Cell headerCell, int endRowNum, String cellName, String cellValue)
  557. throws Exception {
  558. cells.insertColumn(0, true);
  559. cells.get(headerCell.getRow(), 0).setValue(cellName);
  560. for (int i = headerCell.getRow() + 1; i <= endRowNum; i++) {
  561. cells.get(i, 0).setValue(cellValue);
  562. }
  563. wb.save(filename);
  564. }
  565. /**
  566. * 解析开户信息
  567. */
  568. private void readOAI(File file, Cells cells, int sheetNo) {
  569. // 获取表头信息
  570. try {
  571. // 身份证号
  572. String idCarNo = null;
  573. Cell idCell = AsposeUtil.getCell(cells, "证件号码");
  574. if (idCell != null) {
  575. int headRowNumber = idCell.getRow() + 1;
  576. int col = idCell.getColumn();
  577. idCarNo = cells.get(headRowNumber, col).getStringValue();
  578. }
  579. Cell cell = AsposeUtil.getCell(cells, "客户号");
  580. if (cell != null) {
  581. int headRowNumber = cell.getRow() + 1;
  582. try (ExcelReader excelReader = EasyExcel.read(file).build()) {
  583. ReadSheet readSheet = EasyExcel.readSheet(sheetNo)
  584. .headRowNumber(headRowNumber)
  585. .head(CITICOpeningAccountInfoEntity.class)
  586. .registerConverter(new ExcelStringToJavaBigDecimalConverter())
  587. .registerReadListener(new CITICBankOpeningAccountInfoReadListener(
  588. esCITICOpeningAccountInfoMapper, idCarNo))
  589. .build();
  590. excelReader.read(readSheet);
  591. } catch (Exception e) {
  592. log.error("解析Excel文件失败.", e);
  593. throw new ImportDataFailedException(e.getMessage(), file.getAbsolutePath());
  594. }
  595. }
  596. } catch (Exception e) {
  597. log.error("解析Excel文件失败.", e);
  598. throw new ImportDataFailedException(e.getMessage(), file.getAbsolutePath());
  599. }
  600. }
  601. }