RecognitionCheckTaskEx.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by FernFlower decompiler)
  4. //
  5. package kd.imc.rim;
  6. import com.alibaba.fastjson.JSON;
  7. import com.alibaba.fastjson.JSONArray;
  8. import com.alibaba.fastjson.JSONObject;
  9. import java.util.ArrayList;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.concurrent.Callable;
  14. import kd.bos.context.RequestContext;
  15. import kd.bos.dataentity.entity.DynamicObject;
  16. import kd.bos.dataentity.entity.DynamicObjectCollection;
  17. import kd.bos.dataentity.resource.ResManager;
  18. import kd.bos.dataentity.utils.StringUtils;
  19. import kd.bos.dlock.DLock;
  20. import kd.bos.logging.Log;
  21. import kd.bos.logging.LogFactory;
  22. import kd.bos.orm.query.QFilter;
  23. import kd.bos.orm.util.CollectionUtils;
  24. import kd.bos.servicehelper.BusinessDataServiceHelper;
  25. import kd.bos.servicehelper.operation.SaveServiceHelper;
  26. import kd.imc.rim.common.constant.InputInvoiceTypeEnum;
  27. import kd.imc.rim.common.constant.ResultContant;
  28. import kd.imc.rim.common.invoice.fpzs.FpzsMainService;
  29. import kd.imc.rim.common.invoice.recognition.listener.IRecognitionListener;
  30. import kd.imc.rim.common.service.EInvoiceZipXmlDealService;
  31. import kd.imc.rim.common.service.ElectAccVoucherService;
  32. import kd.imc.rim.common.service.ExcelInvoiceUploadService;
  33. import kd.imc.rim.common.utils.BigDecimalUtil;
  34. import kd.imc.rim.common.utils.CacheHelper;
  35. import kd.imc.rim.common.utils.FileUtils;
  36. import kd.imc.rim.common.utils.MD5;
  37. import org.apache.commons.lang3.tuple.Pair;
  38. public class RecognitionCheckTaskEx implements Callable<JSONObject> {
  39. private static Log logger = LogFactory.getLog(RecognitionCheckTaskEx.class);
  40. private static int cache_time_out = 1800;
  41. private JSONObject businessParam;
  42. private Map<String, Object> customParam;
  43. private String url;
  44. private String fileName;
  45. private String pageId;
  46. private String source;
  47. private RequestContext ctx;
  48. public static final String waiting = "waiting";
  49. public static final String success = "success";
  50. public static final String fail = "fail";
  51. protected static final List<String> telGoodNames = new ArrayList<String>(12) {
  52. private static final long serialVersionUID = 2608083843312492007L;
  53. {
  54. this.add("电信服务");
  55. this.add("基础电信服务");
  56. this.add("语音通话服务");
  57. this.add("出租或出售网络元素");
  58. this.add("增值电信服务");
  59. this.add("短信和彩信服务");
  60. this.add("电子数据和信息的传输及应用服务");
  61. this.add("互联网接入服务");
  62. this.add("广播电视信号传输服务");
  63. this.add("卫星电视信号落地转接服务");
  64. this.add("其他增值电信服务");
  65. }
  66. };
  67. public RecognitionCheckTaskEx(RequestContext ctx, String source, String pageId, JSONObject businessParam, Map<String, Object> customParam, String url, String fileName) {
  68. saveCacheFile(pageId, url, "waiting");
  69. this.ctx = ctx;
  70. this.pageId = pageId;
  71. this.businessParam = businessParam;
  72. this.url = url;
  73. this.source = source;
  74. this.fileName = fileName;
  75. this.customParam = customParam;
  76. }
  77. public JSONObject call() throws Exception {
  78. logger.info("begin RecognitionCheckTask :{},{}", this.url, this.fileName);
  79. try {
  80. RequestContext.copyAndSet(this.ctx);
  81. if (StringUtils.isEmpty(this.url)) {
  82. return null;
  83. } else {
  84. JSONObject invoiceResult = new JSONObject();
  85. Long orgId = RequestContext.get().getOrgId();
  86. long rim_user;
  87. if (this.customParam != null) {
  88. try {
  89. rim_user = BigDecimalUtil.transDecimal(this.customParam.get("orgId")).longValue();
  90. if (rim_user > 0L) {
  91. orgId = rim_user;
  92. }
  93. } catch (Exception var12) {
  94. }
  95. }
  96. if (this.businessParam != null) {
  97. rim_user = BigDecimalUtil.transDecimal(this.businessParam.get("rim_user")).longValue();
  98. if (rim_user < 1L) {
  99. this.businessParam.put("rim_user", RequestContext.get().getCurrUserId());
  100. }
  101. }
  102. boolean isNeedDeal = FileUtils.checkFileType(this.fileName, new String[]{"zip", "ofd", "pdf"});
  103. if (isNeedDeal) {
  104. ElectAccVoucherService electAccVoucherService = new ElectAccVoucherService();
  105. invoiceResult = electAccVoucherService.dealVoucher(this.url, this.fileName, orgId, this.businessParam, (IRecognitionListener)null);
  106. }
  107. boolean isZipXmlEI = FileUtils.checkFileType(this.fileName, new String[]{"zip", "xml"});
  108. if (isZipXmlEI && !"0000".equals(invoiceResult.getString("errcode"))) {
  109. invoiceResult = EInvoiceZipXmlDealService.analysisAndCheckSave(this.url, this.fileName, orgId, this.businessParam, (IRecognitionListener)null);
  110. }
  111. String xbrlErrCode = invoiceResult.getString("errcode");
  112. boolean dealResult = StringUtils.isEmpty(xbrlErrCode) || !xbrlErrCode.equals("0000");
  113. boolean isZip = FileUtils.checkFileType(this.fileName, new String[]{"zip"});
  114. if (dealResult && !isZip) {
  115. if (FileUtils.isExcel(this.fileName)) {
  116. invoiceResult = ExcelInvoiceUploadService.getInstance().uploadExcelInvoice(this.url, this.fileName, (IRecognitionListener)null, this.businessParam);
  117. } else {
  118. logger.info("RecognitionCheckTask识别url:{}{}", this.fileName, this.url);
  119. invoiceResult = RecognitionCheckServiceEx.getInstance().recognitionCheckInvoice(this.url, this.fileName, (IRecognitionListener)null, this.businessParam);
  120. }
  121. }
  122. logger.info("发票助手本地上传最终数据返回:{}-{}", this.pageId, invoiceResult);
  123. if (ResultContant.isSuccess(invoiceResult)) {
  124. JSONArray invoiceArray = invoiceResult.getJSONArray("data");
  125. classOfInvoice(invoiceArray);
  126. JSONArray failInvoiceArray = invoiceResult.getJSONArray("failData");
  127. JSONArray attachArray = invoiceResult.getJSONArray("attach");
  128. if (!CollectionUtils.isEmpty(failInvoiceArray)) {
  129. CacheHelper.put(this.pageId + "failResult", failInvoiceArray.toJSONString(), cache_time_out);
  130. }
  131. if (invoiceArray == null && attachArray == null) {
  132. saveCacheFile(this.pageId, this.url, "fail");
  133. } else {
  134. CacheHelper.put(this.pageId + "refresh", "1", cache_time_out);
  135. if ("fpzs".equals(this.source)) {
  136. Pair<JSONObject, Boolean> cachePair = FpzsMainService.cacheInvoiceList(this.pageId, this.customParam, invoiceArray, attachArray);
  137. if ((Boolean)cachePair.getRight()) {
  138. CacheHelper.put(this.pageId + "scannerProcessRepeat", cachePair.getRight() + "", cache_time_out);
  139. }
  140. } else {
  141. saveCacheFileResult(this.url, invoiceArray);
  142. }
  143. saveCacheFile(this.pageId, this.url, "success");
  144. }
  145. } else {
  146. saveCacheFile(this.pageId, this.url, "fail");
  147. saveCacheCause(this.pageId, this.url, "fail", invoiceResult.getString("description"));
  148. }
  149. return invoiceResult;
  150. }
  151. } catch (Throwable var13) {
  152. logger.info("RecognitionCheckTask throwable:{}", var13);
  153. saveCacheFile(this.pageId, this.url, "fail");
  154. saveCacheCause(this.pageId, this.url, "fail", ResManager.loadKDString("程序错误", "RecognitionCheckTask_0", "imc-rim-common", new Object[0]));
  155. return new JSONObject();
  156. }
  157. }
  158. public static void classOfInvoice(JSONArray invoiceArray) {
  159. if (invoiceArray != null) {
  160. for(int i = 0; i < invoiceArray.size(); ++i) {
  161. JSONObject invoice = invoiceArray.getJSONObject(i);
  162. Long invoiceType = invoice.getLong("invoiceType");
  163. if (InputInvoiceTypeEnum.needClassOfInvoice(invoiceType)) {
  164. boolean telFlag = telType(invoice);
  165. boolean childFlag = childType(invoice);
  166. if (telFlag && childFlag) {
  167. setInvoiceClass(invoice, "11", "9");
  168. } else if (telFlag) {
  169. setInvoiceClass(invoice, "9");
  170. } else if (childFlag) {
  171. setInvoiceClass(invoice, "11");
  172. }
  173. }
  174. }
  175. }
  176. }
  177. private static boolean childType(JSONObject invoice) {
  178. boolean flag = false;
  179. Long invoiceType = invoice.getLong("invoiceType");
  180. if (InputInvoiceTypeEnum.FINANCIAL_INVOICE.getCode().equals(invoiceType)) {
  181. String invoicingPartyName = invoice.getString("invoicingPartyName");
  182. if (StringUtils.isNotEmpty(invoicingPartyName) && invoicingPartyName.contains("幼儿园")) {
  183. flag = true;
  184. }
  185. } 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)) {
  186. JSONArray items = invoice.getJSONArray("items");
  187. if (items == null) {
  188. return false;
  189. }
  190. for(int i = 0; i < items.size(); ++i) {
  191. JSONObject item = items.getJSONObject(i);
  192. String goodName = item.getString("goodsName");
  193. if (StringUtils.isNotEmpty(goodName) && goodName.contains("保教费")) {
  194. flag = true;
  195. break;
  196. }
  197. }
  198. }
  199. return flag;
  200. }
  201. private static boolean telType(JSONObject invoice) {
  202. Long invoiceType = invoice.getLong("invoiceType");
  203. boolean telFlag = false;
  204. 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)) {
  205. JSONArray items = invoice.getJSONArray("items");
  206. if (items == null) {
  207. return false;
  208. }
  209. boolean flag = false;
  210. for(int i = 0; i < items.size(); ++i) {
  211. JSONObject item = items.getJSONObject(i);
  212. String goodName = item.getString("goodsName");
  213. Iterator var8 = telGoodNames.iterator();
  214. while(var8.hasNext()) {
  215. String s = (String)var8.next();
  216. if (goodName.contains(s)) {
  217. telFlag = true;
  218. flag = true;
  219. break;
  220. }
  221. }
  222. if (flag) {
  223. break;
  224. }
  225. }
  226. }
  227. return telFlag;
  228. }
  229. private static void setInvoiceClass(JSONObject invoice, String... classType) {
  230. Long mainId = invoice.getLong("mainId");
  231. DynamicObject invoices = BusinessDataServiceHelper.loadSingle("rim_invoice", "id, mul_class, ext_info", new QFilter[]{new QFilter("id", "=", mainId)});
  232. if (invoices != null) {
  233. String mulClassStr = String.join(",", classType);
  234. String extInfo = invoices.getString("ext_info");
  235. JSONObject extObject = new JSONObject();
  236. if (StringUtils.isNotEmpty(extInfo)) {
  237. try {
  238. extObject = JSONObject.parseObject(extInfo);
  239. } catch (Exception var13) {
  240. extObject = new JSONObject();
  241. }
  242. }
  243. extObject.put("sys_mulclass", mulClassStr);
  244. invoices.set("ext_info", extObject.toString());
  245. DynamicObjectCollection mul_class = invoices.getDynamicObjectCollection("mul_class");
  246. if (CollectionUtils.isEmpty(mul_class)) {
  247. String[] var8 = classType;
  248. int var9 = classType.length;
  249. for(int var10 = 0; var10 < var9; ++var10) {
  250. String typeStr = var8[var10];
  251. DynamicObject classInfo = mul_class.addNew();
  252. classInfo.set("fbasedataid_id", typeStr);
  253. SaveServiceHelper.save(new DynamicObject[]{invoices});
  254. }
  255. }
  256. }
  257. }
  258. public JSONObject getBusinessParam() {
  259. return this.businessParam;
  260. }
  261. public void setBusinessParam(JSONObject businessParam) {
  262. this.businessParam = businessParam;
  263. }
  264. public String getUrl() {
  265. return this.url;
  266. }
  267. public void setUrl(String url) {
  268. this.url = url;
  269. }
  270. public static void saveCacheFileResult(String url, JSONArray result) {
  271. if (result != null) {
  272. CacheHelper.put(MD5.md5Hex(url), result.toJSONString(), cache_time_out);
  273. } else {
  274. CacheHelper.remove(MD5.md5Hex(url));
  275. }
  276. }
  277. public static JSONArray queryCacheFileResult(String url) {
  278. String result = CacheHelper.get(MD5.md5Hex(url));
  279. return result != null ? JSONArray.parseArray(result) : new JSONArray();
  280. }
  281. public static void saveCacheFile(String pageId, String url, String operate) {
  282. cacheFile(pageId, url, operate);
  283. }
  284. public static void saveCacheCause(String pageId, String url, String operate, String cause) {
  285. cacheCause(pageId, url, operate, cause);
  286. }
  287. public static JSONObject queryCacheFile(String pageId) {
  288. return cacheFile(pageId, (String)null, "query");
  289. }
  290. public static JSONObject queryCacheCause(String pageId) {
  291. return cacheCause(pageId, (String)null, "query", (String)null);
  292. }
  293. public static void clearCacheFile(String pageId) {
  294. cacheFile(pageId, (String)null, "remove");
  295. }
  296. private static JSONObject cacheCause(String pageId, String url, String operate, String cause) {
  297. logger.info(pageId + url + operate + cause);
  298. if (StringUtils.isEmpty(pageId)) {
  299. return null;
  300. } else {
  301. String cacheKey = "scaner_cause_" + pageId;
  302. DLock lock = DLock.create("lock_" + cacheKey, ResManager.loadKDString("刷新卡片锁", "RecognitionCheckTask_1", "imc-rim-common", new Object[0]));
  303. Throwable var6 = null;
  304. try {
  305. int times = 0;
  306. while(times < 10) {
  307. ++times;
  308. if (lock.tryLock(100L)) {
  309. try {
  310. String cache;
  311. if ("remove".equals(operate)) {
  312. CacheHelper.remove(cacheKey);
  313. cache = null;
  314. return null;
  315. }
  316. cache = CacheHelper.get(cacheKey);
  317. JSONObject obj = null;
  318. if (StringUtils.isEmpty(cache)) {
  319. obj = new JSONObject();
  320. } else {
  321. obj = JSON.parseObject(cache);
  322. }
  323. JSONObject var10;
  324. if ("query".equals(operate)) {
  325. var10 = obj;
  326. return var10;
  327. }
  328. if (StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(operate) && StringUtils.isNotEmpty(cause)) {
  329. obj.put(url, cause);
  330. CacheHelper.put(cacheKey, obj.toJSONString(), cache_time_out);
  331. }
  332. var10 = obj;
  333. return var10;
  334. } finally {
  335. lock.unlock();
  336. }
  337. }
  338. }
  339. } catch (Throwable var29) {
  340. var6 = var29;
  341. throw var29;
  342. } finally {
  343. if (lock != null) {
  344. if (var6 != null) {
  345. try {
  346. lock.close();
  347. } catch (Throwable var27) {
  348. var6.addSuppressed(var27);
  349. }
  350. } else {
  351. lock.close();
  352. }
  353. }
  354. }
  355. return null;
  356. }
  357. }
  358. private static JSONObject cacheFile(String pageId, String url, String operate) {
  359. logger.info(pageId + url + operate);
  360. if (StringUtils.isEmpty(pageId)) {
  361. return null;
  362. } else {
  363. String cacheKey = "scaner_" + pageId;
  364. DLock lock = DLock.create("lock_" + cacheKey, ResManager.loadKDString("刷新卡片锁", "RecognitionCheckTask_1", "imc-rim-common", new Object[0]));
  365. Throwable var5 = null;
  366. JSONObject var11;
  367. try {
  368. int times = 0;
  369. do {
  370. if (times >= 10) {
  371. return null;
  372. }
  373. ++times;
  374. } while(!lock.tryLock(100L));
  375. long starttime = System.currentTimeMillis();
  376. try {
  377. String cache;
  378. if ("remove".equals(operate)) {
  379. CacheHelper.remove(cacheKey);
  380. cache = null;
  381. return null;
  382. }
  383. cache = CacheHelper.get(cacheKey);
  384. JSONObject obj = null;
  385. if (StringUtils.isEmpty(cache)) {
  386. obj = new JSONObject();
  387. } else {
  388. obj = JSON.parseObject(cache);
  389. }
  390. if ("query".equals(operate)) {
  391. var11 = obj;
  392. return var11;
  393. }
  394. if (StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(operate)) {
  395. obj.put(url, operate);
  396. CacheHelper.put(cacheKey, obj.toJSONString(), cache_time_out);
  397. }
  398. var11 = obj;
  399. } finally {
  400. logger.info("cacheFile总耗时:{}-{}", pageId, System.currentTimeMillis() - starttime);
  401. lock.unlock();
  402. }
  403. } catch (Throwable var30) {
  404. var5 = var30;
  405. throw var30;
  406. } finally {
  407. if (lock != null) {
  408. if (var5 != null) {
  409. try {
  410. lock.close();
  411. } catch (Throwable var28) {
  412. var5.addSuppressed(var28);
  413. }
  414. } else {
  415. lock.close();
  416. }
  417. }
  418. }
  419. return var11;
  420. }
  421. }
  422. public String getSource() {
  423. return this.source;
  424. }
  425. public void setSource(String source) {
  426. this.source = source;
  427. }
  428. public Map<String, Object> getCustomParam() {
  429. return this.customParam;
  430. }
  431. public void setCustomParam(Map<String, Object> customParam) {
  432. this.customParam = customParam;
  433. }
  434. }