|
@@ -0,0 +1,207 @@
|
|
|
|
+//
|
|
|
|
+// Source code recreated from a .class file by IntelliJ IDEA
|
|
|
|
+// (powered by FernFlower decompiler)
|
|
|
|
+//
|
|
|
|
+
|
|
|
|
+package kd.imc.rim.common.invoice.collector;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Iterator;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import kd.bos.context.RequestContext;
|
|
|
|
+import kd.bos.dataentity.utils.ObjectUtils;
|
|
|
|
+import kd.bos.dataentity.utils.StringUtils;
|
|
|
|
+import kd.bos.logging.Log;
|
|
|
|
+import kd.bos.logging.LogFactory;
|
|
|
|
+import kd.bos.mvc.cache.PageCache;
|
|
|
|
+import kd.bos.orm.util.CollectionUtils;
|
|
|
|
+import kd.imc.rim.common.invoice.recognition.impl.RecognitionProgress;
|
|
|
|
+import kd.imc.rim.common.invoice.recognition.impl.RecognitionProgressError;
|
|
|
|
+import kd.imc.rim.common.invoice.recognition.listener.IRecognitionListener;
|
|
|
|
+import kd.imc.rim.common.invoice.recognition.listener.RecognitionListenerResult;
|
|
|
|
+import kd.imc.rim.common.service.EInvoiceZipXmlDealService;
|
|
|
|
+import kd.imc.rim.common.service.ElectAccVoucherService;
|
|
|
|
+import kd.imc.rim.common.service.ExcelInvoiceUploadService;
|
|
|
|
+import kd.imc.rim.common.service.RecognitionCheckServiceEx;
|
|
|
|
+import kd.imc.rim.common.utils.FileUtils;
|
|
|
|
+
|
|
|
|
+public class InvoiceCollectTaskNew implements Runnable, IRecognitionListener {
|
|
|
|
+ private static Log logger = LogFactory.getLog(InvoiceCollectTaskNew.class);
|
|
|
|
+ private RequestContext rc;
|
|
|
|
+ private String pageId;
|
|
|
|
+ private List<Map<String, String>> fileUrls;
|
|
|
|
+ private RecognitionProgress recognitionProgress;
|
|
|
|
+ private JSONObject businessParam;
|
|
|
|
+
|
|
|
|
+ public InvoiceCollectTaskNew(RequestContext rc, String pageId, List<Map<String, String>> fileUrls, JSONObject businessParam) {
|
|
|
|
+ this.rc = rc;
|
|
|
|
+ this.pageId = pageId;
|
|
|
|
+ this.fileUrls = fileUrls;
|
|
|
|
+ this.businessParam = businessParam;
|
|
|
|
+ this.recognitionProgress = new RecognitionProgress();
|
|
|
|
+ Object[] urls = new Object[fileUrls.size()];
|
|
|
|
+ int i = 0;
|
|
|
|
+
|
|
|
|
+ for(Iterator var7 = fileUrls.iterator(); var7.hasNext(); ++i) {
|
|
|
|
+ Map<String, String> map = (Map)var7.next();
|
|
|
|
+ urls[i] = map.get("url");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.recognitionProgress.setUrls(urls);
|
|
|
|
+ this.recognitionProgress.setProgress(1);
|
|
|
|
+ this.recognitionProgress.setUnHandleSize(fileUrls.size());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void run() {
|
|
|
|
+ RequestContext.copyAndSet(this.rc);
|
|
|
|
+ this.doTask();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void doTask() {
|
|
|
|
+ PageCache pageCache = new PageCache(this.pageId);
|
|
|
|
+ long taskStart = System.currentTimeMillis();
|
|
|
|
+ int size = this.fileUrls.size();
|
|
|
|
+
|
|
|
|
+ for(int i = 0; i < size; ++i) {
|
|
|
|
+ Map<String, String> map = (Map)this.fileUrls.get(i);
|
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
|
+ String url = (String)map.get("url");
|
|
|
|
+ String fileName = (String)map.get("name");
|
|
|
|
+ String seqStr = (String)map.get("seq");
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ if (StringUtils.isEmpty(fileName)) {
|
|
|
|
+ fileName = url.substring(url.lastIndexOf(47) + 1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.recognitionProgress.setHandleUrl(url);
|
|
|
|
+ pageCache.put("progress", JSONObject.toJSONString(this.recognitionProgress));
|
|
|
|
+ long recognitionStart = System.currentTimeMillis();
|
|
|
|
+ this.businessParam.put("pageId", this.pageId);
|
|
|
|
+ this.businessParam.put("uploadIndex", start + (long)i);
|
|
|
|
+ Long orgId;
|
|
|
|
+ if (this.businessParam.get("orgId") == null) {
|
|
|
|
+ orgId = RequestContext.get().getOrgId();
|
|
|
|
+ } else {
|
|
|
|
+ orgId = (Long)this.businessParam.get("orgId");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ JSONObject invoiceResult = new JSONObject();
|
|
|
|
+ boolean isNeedDeal = FileUtils.checkFileType(fileName, new String[]{"zip", "ofd", "pdf"});
|
|
|
|
+ if (isNeedDeal) {
|
|
|
|
+ ElectAccVoucherService electAccVoucherService = new ElectAccVoucherService();
|
|
|
|
+ invoiceResult = electAccVoucherService.dealVoucher(url, fileName, orgId, this.businessParam, this);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ boolean isZipXmlEI = FileUtils.checkFileType(fileName, new String[]{"zip", "xml"});
|
|
|
|
+ if (isZipXmlEI && !"0000".equals(invoiceResult.getString("errcode"))) {
|
|
|
|
+ invoiceResult = EInvoiceZipXmlDealService.analysisAndCheckSave(url, fileName, orgId, this.businessParam, this);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String xbrlErrCode = invoiceResult.getString("errcode");
|
|
|
|
+ boolean dealResult = StringUtils.isEmpty(xbrlErrCode) || !xbrlErrCode.equals("0000");
|
|
|
|
+ boolean isZip = FileUtils.checkFileType(fileName, new String[]{"zip"});
|
|
|
|
+ if (dealResult && !isZip) {
|
|
|
|
+ if (FileUtils.isExcel(fileName)) {
|
|
|
|
+ invoiceResult = ExcelInvoiceUploadService.getInstance().uploadExcelInvoice(url, fileName, this, this.businessParam);
|
|
|
|
+ } else {
|
|
|
|
+ invoiceResult = RecognitionCheckServiceEx.getInstance().recognitionCheckInvoice(url, fileName, this, this.businessParam);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ logger.info(url + "RecognitionCheckServiceEx识别结果:" + invoiceResult);
|
|
|
|
+ logger.info(String.format("文件%s中发票识别与查验完毕 共耗时%s", fileName, System.currentTimeMillis() - recognitionStart));
|
|
|
|
+ if (null == this.recognitionProgress.getRecognitionProgressErrors()) {
|
|
|
|
+ this.recognitionProgress.setRecognitionProgressErrors(new ArrayList());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!"0000".equals(invoiceResult.getString("errcode"))) {
|
|
|
|
+ String errdescription = "识别异常,请重试";
|
|
|
|
+ if (StringUtils.isNotEmpty(invoiceResult.getString("description"))) {
|
|
|
|
+ errdescription = invoiceResult.getString("description");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.recognitionProgress.getRecognitionProgressErrors().add(new RecognitionProgressError(url, errdescription));
|
|
|
|
+ } else {
|
|
|
|
+ JSONArray recognitionData = invoiceResult.getJSONArray("data");
|
|
|
|
+ if (recognitionData != null) {
|
|
|
|
+ for(int k = 0; k < recognitionData.size(); ++k) {
|
|
|
|
+ JSONObject invoiceInfo = recognitionData.getJSONObject(k);
|
|
|
|
+ if (invoiceInfo != null) {
|
|
|
|
+ Object isFalse = invoiceInfo.get("isFalse");
|
|
|
|
+ if (isFalse != null) {
|
|
|
|
+ String snapshotUrl = invoiceInfo.getString("pageUrl");
|
|
|
|
+ String pageIndex = invoiceInfo.getString("pageIndex");
|
|
|
|
+ StringBuilder description = new StringBuilder();
|
|
|
|
+ description.append(fileName).append('第').append(pageIndex).append("页识别失败,请重试");
|
|
|
|
+ this.recognitionProgress.getRecognitionProgressErrors().add(new RecognitionProgressError(snapshotUrl, description.toString()));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ JSONObject tips = invoiceResult.getJSONObject("tips");
|
|
|
|
+ if (!ObjectUtils.isEmpty(tips) && FileUtils.isExcel(fileName)) {
|
|
|
|
+ RecognitionProgressError recognitionProgressError = new RecognitionProgressError();
|
|
|
|
+ recognitionProgressError.setUrl(url);
|
|
|
|
+ recognitionProgressError.setTipDescription(tips.getString("tipDescription"));
|
|
|
|
+ this.recognitionProgress.getRecognitionProgressErrors().add(recognitionProgressError);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ BigDecimal a = new BigDecimal(i + 1);
|
|
|
|
+ BigDecimal b = new BigDecimal(size);
|
|
|
|
+ int progress = a.divide(b, 2, 4).multiply(new BigDecimal("100")).intValue();
|
|
|
|
+ if (progress != 100) {
|
|
|
|
+ this.recognitionProgress.setProgress(progress);
|
|
|
|
+ }
|
|
|
|
+ } catch (Throwable var31) {
|
|
|
|
+ if (!FileUtils.isExcel(fileName)) {
|
|
|
|
+ if (null == this.recognitionProgress.getRecognitionProgressErrors()) {
|
|
|
|
+ this.recognitionProgress.setRecognitionProgressErrors(new ArrayList());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.recognitionProgress.getRecognitionProgressErrors().add(new RecognitionProgressError(url, var31.getMessage()));
|
|
|
|
+ }
|
|
|
|
+ } finally {
|
|
|
|
+ this.recognitionProgress.setUnHandleSize(size - i - 1);
|
|
|
|
+ pageCache.put("progress", JSONObject.toJSONString(this.recognitionProgress));
|
|
|
|
+ logger.info(String.format("文件%s第%s页,InvoiceCollectTask统计信息-共%s个,耗时%s", fileName, i + 1, size, System.currentTimeMillis() - start));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.recognitionProgress.setProgress(100);
|
|
|
|
+ PageCache pageCacheEnd = new PageCache(this.pageId);
|
|
|
|
+ pageCacheEnd.put("progress", JSONObject.toJSONString(this.recognitionProgress));
|
|
|
|
+ logger.info(String.format("%s任务统计信息总耗时%s", this.rc.getTraceId(), System.currentTimeMillis() - taskStart));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void handle(RecognitionListenerResult recognitionListenerResult) {
|
|
|
|
+ PageCache pageCache = new PageCache(this.pageId);
|
|
|
|
+ String fileUrl = recognitionListenerResult.getFileUrl();
|
|
|
|
+ this.recognitionProgress.setHandleUrl(fileUrl);
|
|
|
|
+ if (StringUtils.isNotEmpty(recognitionListenerResult.getErrDescription())) {
|
|
|
|
+ logger.info(fileUrl + "识别失败/中断," + recognitionListenerResult.getErrDescription());
|
|
|
|
+ this.recognitionProgress.setHandleUrlIndex(recognitionListenerResult.getRecognitionIndex());
|
|
|
|
+ this.recognitionProgress.setHandleUrlErrorDescription(recognitionListenerResult.getErrDescription());
|
|
|
|
+ pageCache.put("progress", JSONObject.toJSONString(this.recognitionProgress));
|
|
|
|
+ pageCache.put("task_" + fileUrl, "0");
|
|
|
|
+ } else {
|
|
|
|
+ int index = recognitionListenerResult.getRecognitionIndex();
|
|
|
|
+ logger.info(fileUrl + "第" + index + "页识别完成:" + recognitionListenerResult.getRecognitionInvoice().toJSONString());
|
|
|
|
+ this.recognitionProgress.setHandleUrlIndex(index);
|
|
|
|
+ this.recognitionProgress.setHandleUrlSize(recognitionListenerResult.getRecognitionSize());
|
|
|
|
+ pageCache.put("progress", JSONObject.toJSONString(this.recognitionProgress));
|
|
|
|
+ pageCache.put("task_" + fileUrl, recognitionListenerResult.getRecognitionSize() + "");
|
|
|
|
+ pageCache.put("task_" + fileUrl + "_" + index, recognitionListenerResult.getRecognitionInvoice().toJSONString());
|
|
|
|
+ if (!CollectionUtils.isEmpty(recognitionListenerResult.getImportFailInvoice())) {
|
|
|
|
+ pageCache.put("failed_task_" + fileUrl + "_" + index, recognitionListenerResult.getImportFailInvoice().toJSONString());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|