// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package kd.imc.rim; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.Callable; import kd.bos.context.RequestContext; import kd.bos.dataentity.entity.DynamicObject; import kd.bos.dataentity.entity.DynamicObjectCollection; import kd.bos.dataentity.resource.ResManager; import kd.bos.dataentity.utils.StringUtils; import kd.bos.dlock.DLock; import kd.bos.logging.Log; import kd.bos.logging.LogFactory; import kd.bos.orm.query.QFilter; import kd.bos.orm.util.CollectionUtils; import kd.bos.servicehelper.BusinessDataServiceHelper; import kd.bos.servicehelper.operation.SaveServiceHelper; import kd.imc.rim.common.constant.InputInvoiceTypeEnum; import kd.imc.rim.common.constant.ResultContant; import kd.imc.rim.common.invoice.fpzs.FpzsMainService; import kd.imc.rim.common.invoice.recognition.listener.IRecognitionListener; 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.utils.BigDecimalUtil; import kd.imc.rim.common.utils.CacheHelper; import kd.imc.rim.common.utils.FileUtils; import kd.imc.rim.common.utils.MD5; import org.apache.commons.lang3.tuple.Pair; public class RecognitionCheckTaskEx implements Callable { private static Log logger = LogFactory.getLog(RecognitionCheckTaskEx.class); private static int cache_time_out = 1800; private JSONObject businessParam; private Map customParam; private String url; private String fileName; private String pageId; private String source; private RequestContext ctx; public static final String waiting = "waiting"; public static final String success = "success"; public static final String fail = "fail"; protected static final List telGoodNames = new ArrayList(12) { private static final long serialVersionUID = 2608083843312492007L; { this.add("电信服务"); this.add("基础电信服务"); this.add("语音通话服务"); this.add("出租或出售网络元素"); this.add("增值电信服务"); this.add("短信和彩信服务"); this.add("电子数据和信息的传输及应用服务"); this.add("互联网接入服务"); this.add("广播电视信号传输服务"); this.add("卫星电视信号落地转接服务"); this.add("其他增值电信服务"); } }; public RecognitionCheckTaskEx(RequestContext ctx, String source, String pageId, JSONObject businessParam, Map customParam, String url, String fileName) { saveCacheFile(pageId, url, "waiting"); this.ctx = ctx; this.pageId = pageId; this.businessParam = businessParam; this.url = url; this.source = source; this.fileName = fileName; this.customParam = customParam; } public JSONObject call() throws Exception { logger.info("begin RecognitionCheckTask :{},{}", this.url, this.fileName); try { RequestContext.copyAndSet(this.ctx); if (StringUtils.isEmpty(this.url)) { return null; } else { JSONObject invoiceResult = new JSONObject(); Long orgId = RequestContext.get().getOrgId(); long rim_user; if (this.customParam != null) { try { rim_user = BigDecimalUtil.transDecimal(this.customParam.get("orgId")).longValue(); if (rim_user > 0L) { orgId = rim_user; } } catch (Exception var12) { } } if (this.businessParam != null) { rim_user = BigDecimalUtil.transDecimal(this.businessParam.get("rim_user")).longValue(); if (rim_user < 1L) { this.businessParam.put("rim_user", RequestContext.get().getCurrUserId()); } } boolean isNeedDeal = FileUtils.checkFileType(this.fileName, new String[]{"zip", "ofd", "pdf"}); if (isNeedDeal) { ElectAccVoucherService electAccVoucherService = new ElectAccVoucherService(); invoiceResult = electAccVoucherService.dealVoucher(this.url, this.fileName, orgId, this.businessParam, (IRecognitionListener)null); } boolean isZipXmlEI = FileUtils.checkFileType(this.fileName, new String[]{"zip", "xml"}); if (isZipXmlEI && !"0000".equals(invoiceResult.getString("errcode"))) { invoiceResult = EInvoiceZipXmlDealService.analysisAndCheckSave(this.url, this.fileName, orgId, this.businessParam, (IRecognitionListener)null); } String xbrlErrCode = invoiceResult.getString("errcode"); boolean dealResult = StringUtils.isEmpty(xbrlErrCode) || !xbrlErrCode.equals("0000"); boolean isZip = FileUtils.checkFileType(this.fileName, new String[]{"zip"}); if (dealResult && !isZip) { if (FileUtils.isExcel(this.fileName)) { invoiceResult = ExcelInvoiceUploadService.getInstance().uploadExcelInvoice(this.url, this.fileName, (IRecognitionListener)null, this.businessParam); } else { logger.info("RecognitionCheckTask识别url:{}{}", this.fileName, this.url); invoiceResult = RecognitionCheckServiceEx.getInstance().recognitionCheckInvoice(this.url, this.fileName, (IRecognitionListener)null, this.businessParam); } } logger.info("发票助手本地上传最终数据返回:{}-{}", this.pageId, invoiceResult); if (ResultContant.isSuccess(invoiceResult)) { JSONArray invoiceArray = invoiceResult.getJSONArray("data"); classOfInvoice(invoiceArray); JSONArray failInvoiceArray = invoiceResult.getJSONArray("failData"); JSONArray attachArray = invoiceResult.getJSONArray("attach"); if (!CollectionUtils.isEmpty(failInvoiceArray)) { CacheHelper.put(this.pageId + "failResult", failInvoiceArray.toJSONString(), cache_time_out); } if (invoiceArray == null && attachArray == null) { saveCacheFile(this.pageId, this.url, "fail"); } else { CacheHelper.put(this.pageId + "refresh", "1", cache_time_out); if ("fpzs".equals(this.source)) { Pair cachePair = FpzsMainService.cacheInvoiceList(this.pageId, this.customParam, invoiceArray, attachArray); if ((Boolean)cachePair.getRight()) { CacheHelper.put(this.pageId + "scannerProcessRepeat", cachePair.getRight() + "", cache_time_out); } } else { saveCacheFileResult(this.url, invoiceArray); } saveCacheFile(this.pageId, this.url, "success"); } } else { saveCacheFile(this.pageId, this.url, "fail"); saveCacheCause(this.pageId, this.url, "fail", invoiceResult.getString("description")); } return invoiceResult; } } catch (Throwable var13) { logger.info("RecognitionCheckTask throwable:{}", var13); saveCacheFile(this.pageId, this.url, "fail"); saveCacheCause(this.pageId, this.url, "fail", ResManager.loadKDString("程序错误", "RecognitionCheckTask_0", "imc-rim-common", new Object[0])); return new JSONObject(); } } public static void classOfInvoice(JSONArray invoiceArray) { if (invoiceArray != null) { for(int i = 0; i < invoiceArray.size(); ++i) { JSONObject invoice = invoiceArray.getJSONObject(i); Long invoiceType = invoice.getLong("invoiceType"); if (InputInvoiceTypeEnum.needClassOfInvoice(invoiceType)) { boolean telFlag = telType(invoice); boolean childFlag = childType(invoice); if (telFlag && childFlag) { setInvoiceClass(invoice, "11", "9"); } else if (telFlag) { setInvoiceClass(invoice, "9"); } else if (childFlag) { setInvoiceClass(invoice, "11"); } } } } } private static boolean childType(JSONObject invoice) { boolean flag = false; Long invoiceType = invoice.getLong("invoiceType"); if (InputInvoiceTypeEnum.FINANCIAL_INVOICE.getCode().equals(invoiceType)) { String invoicingPartyName = invoice.getString("invoicingPartyName"); if (StringUtils.isNotEmpty(invoicingPartyName) && invoicingPartyName.contains("幼儿园")) { flag = true; } } else if (InputInvoiceTypeEnum.ORDINARY_ELECTRON.getCode().equals(invoiceType) || InputInvoiceTypeEnum.ORDINARY_PAPER.getCode().equals(invoiceType) || InputInvoiceTypeEnum.GENERAL_ELECTRON.getCode().equals(invoiceType) || InputInvoiceTypeEnum.ELECTRIC_ORDINARY.getCode().equals(invoiceType)) { JSONArray items = invoice.getJSONArray("items"); if (items == null) { return false; } for(int i = 0; i < items.size(); ++i) { JSONObject item = items.getJSONObject(i); String goodName = item.getString("goodsName"); if (StringUtils.isNotEmpty(goodName) && goodName.contains("保教费")) { flag = true; break; } } } return flag; } private static boolean telType(JSONObject invoice) { Long invoiceType = invoice.getLong("invoiceType"); boolean telFlag = false; if (InputInvoiceTypeEnum.ORDINARY_ELECTRON.getCode().equals(invoiceType) || InputInvoiceTypeEnum.SPECIAL_ELECTRON.getCode().equals(invoiceType) || InputInvoiceTypeEnum.ORDINARY_PAPER.getCode().equals(invoiceType) || InputInvoiceTypeEnum.SPECIAL_PAPER.getCode().equals(invoiceType) || InputInvoiceTypeEnum.TOLL_ELECTRON.getCode().equals(invoiceType) || InputInvoiceTypeEnum.ELECTRIC_ORDINARY.getCode().equals(invoiceType) || InputInvoiceTypeEnum.ELECTRIC_SPECIAL.getCode().equals(invoiceType) || InputInvoiceTypeEnum.GENERAL_ELECTRON.getCode().equals(invoiceType)) { JSONArray items = invoice.getJSONArray("items"); if (items == null) { return false; } boolean flag = false; for(int i = 0; i < items.size(); ++i) { JSONObject item = items.getJSONObject(i); String goodName = item.getString("goodsName"); Iterator var8 = telGoodNames.iterator(); while(var8.hasNext()) { String s = (String)var8.next(); if (goodName.contains(s)) { telFlag = true; flag = true; break; } } if (flag) { break; } } } return telFlag; } private static void setInvoiceClass(JSONObject invoice, String... classType) { Long mainId = invoice.getLong("mainId"); DynamicObject invoices = BusinessDataServiceHelper.loadSingle("rim_invoice", "id, mul_class, ext_info", new QFilter[]{new QFilter("id", "=", mainId)}); if (invoices != null) { String mulClassStr = String.join(",", classType); String extInfo = invoices.getString("ext_info"); JSONObject extObject = new JSONObject(); if (StringUtils.isNotEmpty(extInfo)) { try { extObject = JSONObject.parseObject(extInfo); } catch (Exception var13) { extObject = new JSONObject(); } } extObject.put("sys_mulclass", mulClassStr); invoices.set("ext_info", extObject.toString()); DynamicObjectCollection mul_class = invoices.getDynamicObjectCollection("mul_class"); if (CollectionUtils.isEmpty(mul_class)) { String[] var8 = classType; int var9 = classType.length; for(int var10 = 0; var10 < var9; ++var10) { String typeStr = var8[var10]; DynamicObject classInfo = mul_class.addNew(); classInfo.set("fbasedataid_id", typeStr); SaveServiceHelper.save(new DynamicObject[]{invoices}); } } } } public JSONObject getBusinessParam() { return this.businessParam; } public void setBusinessParam(JSONObject businessParam) { this.businessParam = businessParam; } public String getUrl() { return this.url; } public void setUrl(String url) { this.url = url; } public static void saveCacheFileResult(String url, JSONArray result) { if (result != null) { CacheHelper.put(MD5.md5Hex(url), result.toJSONString(), cache_time_out); } else { CacheHelper.remove(MD5.md5Hex(url)); } } public static JSONArray queryCacheFileResult(String url) { String result = CacheHelper.get(MD5.md5Hex(url)); return result != null ? JSONArray.parseArray(result) : new JSONArray(); } public static void saveCacheFile(String pageId, String url, String operate) { cacheFile(pageId, url, operate); } public static void saveCacheCause(String pageId, String url, String operate, String cause) { cacheCause(pageId, url, operate, cause); } public static JSONObject queryCacheFile(String pageId) { return cacheFile(pageId, (String)null, "query"); } public static JSONObject queryCacheCause(String pageId) { return cacheCause(pageId, (String)null, "query", (String)null); } public static void clearCacheFile(String pageId) { cacheFile(pageId, (String)null, "remove"); } private static JSONObject cacheCause(String pageId, String url, String operate, String cause) { logger.info(pageId + url + operate + cause); if (StringUtils.isEmpty(pageId)) { return null; } else { String cacheKey = "scaner_cause_" + pageId; DLock lock = DLock.create("lock_" + cacheKey, ResManager.loadKDString("刷新卡片锁", "RecognitionCheckTask_1", "imc-rim-common", new Object[0])); Throwable var6 = null; try { int times = 0; while(times < 10) { ++times; if (lock.tryLock(100L)) { try { String cache; if ("remove".equals(operate)) { CacheHelper.remove(cacheKey); cache = null; return null; } cache = CacheHelper.get(cacheKey); JSONObject obj = null; if (StringUtils.isEmpty(cache)) { obj = new JSONObject(); } else { obj = JSON.parseObject(cache); } JSONObject var10; if ("query".equals(operate)) { var10 = obj; return var10; } if (StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(operate) && StringUtils.isNotEmpty(cause)) { obj.put(url, cause); CacheHelper.put(cacheKey, obj.toJSONString(), cache_time_out); } var10 = obj; return var10; } finally { lock.unlock(); } } } } catch (Throwable var29) { var6 = var29; throw var29; } finally { if (lock != null) { if (var6 != null) { try { lock.close(); } catch (Throwable var27) { var6.addSuppressed(var27); } } else { lock.close(); } } } return null; } } private static JSONObject cacheFile(String pageId, String url, String operate) { logger.info(pageId + url + operate); if (StringUtils.isEmpty(pageId)) { return null; } else { String cacheKey = "scaner_" + pageId; DLock lock = DLock.create("lock_" + cacheKey, ResManager.loadKDString("刷新卡片锁", "RecognitionCheckTask_1", "imc-rim-common", new Object[0])); Throwable var5 = null; JSONObject var11; try { int times = 0; do { if (times >= 10) { return null; } ++times; } while(!lock.tryLock(100L)); long starttime = System.currentTimeMillis(); try { String cache; if ("remove".equals(operate)) { CacheHelper.remove(cacheKey); cache = null; return null; } cache = CacheHelper.get(cacheKey); JSONObject obj = null; if (StringUtils.isEmpty(cache)) { obj = new JSONObject(); } else { obj = JSON.parseObject(cache); } if ("query".equals(operate)) { var11 = obj; return var11; } if (StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(operate)) { obj.put(url, operate); CacheHelper.put(cacheKey, obj.toJSONString(), cache_time_out); } var11 = obj; } finally { logger.info("cacheFile总耗时:{}-{}", pageId, System.currentTimeMillis() - starttime); lock.unlock(); } } catch (Throwable var30) { var5 = var30; throw var30; } finally { if (lock != null) { if (var5 != null) { try { lock.close(); } catch (Throwable var28) { var5.addSuppressed(var28); } } else { lock.close(); } } } return var11; } } public String getSource() { return this.source; } public void setSource(String source) { this.source = source; } public Map getCustomParam() { return this.customParam; } public void setCustomParam(Map customParam) { this.customParam = customParam; } }