NhIVController.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. package com.malk.pro.guyuan.controller;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import cn.hutool.http.HttpUtil;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.malk.pro.guyuan.server.model.McInvoiceDto;
  7. import com.malk.pro.guyuan.server.model.McInvoiceKind;
  8. import com.malk.pro.guyuan.server.tencent.TXYConf;
  9. import com.malk.pro.guyuan.service.tencent.IvYdService;
  10. import com.malk.pro.guyuan.service.tencent.NhTXYInvoice;
  11. import com.malk.server.aliwork.YDConf;
  12. import com.malk.server.aliwork.YDParam;
  13. import com.malk.server.common.FilePath;
  14. import com.malk.server.common.McException;
  15. import com.malk.server.common.McR;
  16. import com.malk.server.dingtalk.DDR_New;
  17. import com.malk.service.aliwork.YDClient;
  18. import com.malk.service.aliwork.YDService;
  19. import com.malk.utils.*;
  20. import com.spire.pdf.PdfCompressionLevel;
  21. import com.spire.pdf.PdfDocument;
  22. import com.spire.pdf.PdfPageBase;
  23. import com.spire.pdf.exporting.PdfImageInfo;
  24. import com.spire.pdf.graphics.PdfBitmap;
  25. import com.tencentcloudapi.common.exception.TencentCloudSDKException;
  26. import lombok.SneakyThrows;
  27. import lombok.extern.slf4j.Slf4j;
  28. import net.coobird.thumbnailator.Thumbnails;
  29. import org.apache.commons.lang3.StringUtils;
  30. import org.apache.poi.util.IOUtils;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.web.bind.annotation.PostMapping;
  33. import org.springframework.web.bind.annotation.RequestBody;
  34. import org.springframework.web.bind.annotation.RequestMapping;
  35. import org.springframework.web.bind.annotation.RestController;
  36. import javax.servlet.http.HttpServletRequest;
  37. import java.io.ByteArrayInputStream;
  38. import java.io.ByteArrayOutputStream;
  39. import java.io.File;
  40. import java.io.InputStream;
  41. import java.net.URL;
  42. import java.util.*;
  43. import java.util.stream.Collectors;
  44. /**
  45. * 错误抛出与拦截详见CatchException
  46. */
  47. @Slf4j
  48. @RestController
  49. @RequestMapping("/guyuan/nh/")
  50. public class NhIVController {
  51. @Autowired
  52. private NhTXYInvoice txyInvoice;
  53. @Autowired
  54. private YDClient ydClient;
  55. private final static String APP_TYPE="APP_Y5KGBSIKJGG6ZQBTNKSZ";
  56. private final static String SYSTEM_TOKEN="GFA66U91QQDMO11Y7N7YC7MA6U0M236VMM2YLBO11";
  57. /// 优先获取字段, 新版本接口已支持字段返回
  58. private String findValue(List<Map<String, String>> infos, String... names) {
  59. for (String name : names) {
  60. Optional optional = infos.stream().filter(info -> info.get("Name").equals(name)).findAny();
  61. if (optional.isPresent()) {
  62. return String.valueOf(((Map) optional.get()).get("Value"));
  63. }
  64. }
  65. return "";
  66. }
  67. // 兼容历史配置, 格式 (谷元)
  68. private String guyuanNameRepalce(String name) {
  69. if (name.contains("谷元")) {
  70. return UtilString.replaceBracketIsWhole(name);
  71. } else {
  72. return UtilString.replaceBracketIsSemiangle(name);
  73. }
  74. }
  75. /// url压缩转base64
  76. @SneakyThrows
  77. private String imageUrlConvertBase64(String imageUrl) {
  78. ByteArrayOutputStream out = new ByteArrayOutputStream();
  79. // scale(比例), outputQuality(质量)
  80. Thumbnails.fromURLs(Arrays.asList(new URL(imageUrl))).scale(0.5f).outputQuality(0.25f).toOutputStream(out);
  81. InputStream inputStream = new ByteArrayInputStream(out.toByteArray());
  82. //转换为base64
  83. byte[] bytes = IOUtils.toByteArray(inputStream);
  84. return Base64.getEncoder().encodeToString(bytes);
  85. }
  86. @Autowired
  87. private FilePath filePath;
  88. /// PDF压缩转base64
  89. @SneakyThrows
  90. private String pdfUrlConvertBase64(String pdfUrl) {
  91. String fileName = "tmp_" + new Date().getTime() + ".pdf";
  92. // 下载文件
  93. File file = UtilFile.mkdirIfNot(fileName, filePath.getPath().getTmp());
  94. UtilHttp.doDownload(pdfUrl, file);
  95. // PDF压缩
  96. PdfDocument doc = new PdfDocument(); // 创建PdfDocument类的对象
  97. doc.loadFromFile(file.getAbsolutePath()); // 加载PDF文档
  98. doc.getFileInfo().setIncrementalUpdate(false); // 禁用增量更新
  99. doc.setCompressionLevel(PdfCompressionLevel.Normal); // 将压缩级别设置为最佳
  100. // 遍历文档页面
  101. for (int i = 0; i < doc.getPages().getCount(); i++) {
  102. PdfPageBase page = doc.getPages().get(i); // 获取指定页面
  103. PdfImageInfo[] images = page.getImagesInfo(); // 获取每个页面的图像信息集合
  104. // 遍历集合中的所有项目
  105. if (images != null && images.length > 0)
  106. for (int j = 0; j < images.length; j++) {
  107. PdfImageInfo image = images[j]; // 获取指定图片
  108. PdfBitmap bp = new PdfBitmap(image.getImage());
  109. bp.setQuality(30); // 设置压缩质量
  110. page.replaceImage(j, bp); // 将原始图像替换为压缩图像
  111. }
  112. }
  113. // 将结果文档保存至另一个PDF文档中: 覆盖
  114. doc.saveToFile(file.getAbsolutePath());
  115. doc.close();
  116. // PDF转base64, 无需透出本地文件地址
  117. String base64 = UtilFile.fileToBase64(file.getAbsolutePath());
  118. // 删除临时PDF文件
  119. UtilFile.deleteFile(file.getAbsolutePath());
  120. return base64;
  121. }
  122. // prd 校验发票抬头, 购买方范围
  123. private void validateBuyer(String BuyerName, String tips) {
  124. List<String> corpNames = Arrays.asList(
  125. "珠海能魁新能源科技有限公司",
  126. "珠海金魁新能源科技有限公司",
  127. "珠海乾魁新能源科技有限公司",
  128. "珠海创伟新能源有限公司",
  129. "邓州能辉新能源有限公司",
  130. "河南省绿色生态新能源科技有限公司",
  131. "山东烁辉光伏科技有限公司",
  132. "上海能辉清洁能源科技有限公司",
  133. "上海奉魁新能源科技有限公司",
  134. "上海能魁新能源科技有限公司",
  135. "上海能魁新能源科技有限公司",
  136. "珠海永魁新能源科技有限公司",
  137. "珠海新魁新能源科技有限公司",
  138. "珠海烁辉新能源开发有限公司",
  139. "珠海乾魁新能源科技有限公司",
  140. "珠海奉魁新能源有限公司",
  141. "上海能辉科技股份有限公司中部分公司",
  142. "上海能辉储能科技有限公司",
  143. "河南能辉绿电科技有限公司",
  144. "河南省绿色生态新能源科技有限公司",
  145. "贵州能辉智慧能源科技有限公司",
  146. "上海能辉科技股份有限公司");
  147. McException.assertAccessException(!corpNames.contains(BuyerName), tips + ", 购买方名称不合法!");
  148. }
  149. /**
  150. * 混票识别 [新版本]
  151. */
  152. @PostMapping("invoice-iv2")
  153. McR invoice_iv2(@RequestBody Map<String, String> data) throws TencentCloudSDKException {
  154. McException.assertParamException_Null(data, "url");
  155. String image = ydClient.convertTemporaryUrl(data.get("url"),60000,APP_TYPE,SYSTEM_TOKEN);
  156. log.info("混票识别, 免登地址, {}", image);
  157. // 非PDF, 且内存大于3M, 压缩后上传
  158. if (UtilMap.getFloat(data, "size") > 3.0f && !UtilMap.getBoolean(data, "isPdf")) {
  159. image = imageUrlConvertBase64(image);
  160. }
  161. if (UtilMap.getFloat(data, "size") > 6.0f && UtilMap.getBoolean(data, "isPdf")) {
  162. image = pdfUrlConvertBase64(image);
  163. }
  164. List<Map> invoices = (List<Map>) txyInvoice.doRecognizeGeneralInvoice(image).get("MixedInvoiceItems");
  165. List<String> nos=new ArrayList<>();
  166. List<McInvoiceDto> result = invoices.stream().map(item -> {
  167. Map prop = UtilMap.getMap(UtilMap.getMap(item, "SingleInvoiceInfos"), UtilMap.getString(item, "SubType"));
  168. // 2025.5.12 去除小计金额,并去除发票编号重复数据
  169. String no=UtilMap.getString(prop, "Number");
  170. if(nos.contains(no)){
  171. return null;
  172. }
  173. nos.add(no);
  174. String kind = UtilMap.getString(item, "TypeDescription");
  175. String invoiceName = UtilMap.getString(item, "SubTypeDescription");
  176. if (kind.equals("全电发票")) {
  177. if(invoiceName.contains("铁路电子客票")){
  178. kind="火车票";
  179. } else if(invoiceName.contains("机票行程单")){
  180. kind="机票行程单";
  181. } else if (invoiceName.contains("专用发票")) {
  182. kind="全电专用发票";
  183. }else {
  184. kind="全电普通发票";
  185. }
  186. }
  187. if (kind.equals("增值税发票")) {
  188. if(invoiceName.contains("区块链电子发票")){
  189. kind="区块链电子发票";
  190. } else if(invoiceName.contains("增值税专用发票")){
  191. kind="增值税专用发票";
  192. } else if(invoiceName.contains("增值税普通发票")){
  193. kind="增值税普通发票";
  194. } else if (invoiceName.contains("增值税电子专用发票")) {
  195. kind="增值税电子专用发票";
  196. } else if (invoiceName.contains("增值税电子普通发票")) {
  197. kind="增值税电子普通发票";
  198. }else {
  199. kind="增值税普通发票";
  200. }
  201. }
  202. // ppExt: 通用字段定义
  203. McInvoiceDto invoiceDto = McInvoiceDto.builder()
  204. .name(UtilMap.getString(item, "SubTypeDescription"))
  205. .kindName(kind)
  206. .kind(UtilMap.getInt(item, "Type"))
  207. .code(UtilMap.getString(prop, "Code"))
  208. .serial(UtilMap.getString(prop, "Number"))
  209. .date(UtilString.replaceDateZH_cn(UtilMap.getString(prop, "Date")))
  210. .checkCode(UtilMap.getString(prop, "CheckCode"))
  211. // ppExt: 多明细行时, 优先取值合计 [全电票返回了subTotal字段, 但值为空] // 2025.5.12 去除小计金额,并去除发票编号重复数据
  212. .amount(UtilNumber.setBigDecimal(UtilMap.getString_first(prop, "Total", "Fare"))) // "SubTotal",
  213. .tax(UtilNumber.setBigDecimal(UtilMap.getString_first(prop, "Tax"))) // "SubTax",
  214. .excludingTax(UtilNumber.setBigDecimal(UtilMap.getString(prop, "PretaxAmount")))
  215. .buyerName(StringUtils.isBlank(guyuanNameRepalce(UtilMap.getString(prop, "Buyer")))?"上海能辉科技股份有限公司":guyuanNameRepalce(UtilMap.getString(prop, "Buyer")))
  216. // ppExt: 中央非税未返回税号官方说明: 非税发票理论是没有税号的,图片中属于信用代码
  217. .buyerTaxId(StringUtils.isBlank(UtilMap.getString(prop, "BuyerTaxID"))?"91310000685457643J": UtilMap.getString(prop, "BuyerTaxID"))
  218. .sellerName(guyuanNameRepalce(UtilMap.getString_first(prop, "Seller", "Issuer"))) // 行程单: 填开单位
  219. .sellerTaxId(UtilMap.getString_first(prop, "SellerTaxID", "AgentCode")) // 行程单: 销售单位代号
  220. .passengerName(UtilMap.getString_first(prop, "Name", "UserName")) // 火车票, 行程单
  221. // 交通出行
  222. .seatType(UtilMap.getString(prop, "Seat"))
  223. .departureTime(UtilString.replaceDateZH_cn(UtilMap.getString(prop, "DateGetOn")) + " " + UtilMap.getString(prop, "TimeGetOn"))
  224. .departurePort(UtilMap.getString_first(prop, "StationGetOn", "Entrance", "Place")) // 火车票: 出发车站, 过路过桥费: 入口, 出租车: 发票所在地
  225. .arrivePort(UtilMap.getString_first(prop, "StationGetOff", "Exit")) // 行程单, 火车票: 到达车站, 过路过桥费: 出口
  226. .trainNo(UtilMap.getString_first(prop, "TrainNumber", "LicensePlate")) // 火车票: 车次, 出租车: 车牌号
  227. .insuranceCosts(UtilNumber.setBigDecimal((UtilMap.getString(prop, "Insurance")))) // 行程单: 保险费
  228. .fuelCosts(UtilNumber.setBigDecimal((UtilMap.getString(prop, "FuelSurcharge")))) // 行程单: 燃油附加费
  229. .constructionCosts(UtilNumber.setBigDecimal((UtilMap.getString(prop, "AirDevelopmentFund")))) // 行程单: 民航发展基金
  230. .build();
  231. // ppExt: 机票行程单, 行程与座位信息在明细内
  232. if ("机票行程单".equals(kind)) {
  233. invoiceDto.setSellerName(UtilMap.getString_first(prop, "Issuer"));
  234. invoiceDto.setSellerTaxId(UtilMap.getString_first(prop, "Seller"));
  235. Map flight = (Map) UtilMap.getList(prop, "FlightItems").get(0);
  236. invoiceDto.setDepartureTime(UtilString.replaceDateZH_cn(UtilMap.getString(item, "DateGetOn")) + " " + UtilMap.getString(prop, "TimeGetOn"));
  237. invoiceDto.setDeparturePort(UtilMap.getString(flight, "StationGetOn"));
  238. invoiceDto.setArrivePort(UtilMap.getString(flight, "StationGetOff"));
  239. invoiceDto.setSeatType(UtilMap.getString(flight, "Seat"));
  240. }
  241. if ("出租车发票".equals(item.get("TypeDescription"))) {
  242. // 上下车时间
  243. invoiceDto.setDepartureTime(UtilMap.getString(prop, "TimeGetOn") + " ~ " + UtilMap.getString(prop, "TimeGetOff"));
  244. }
  245. return invoiceDto;
  246. }).filter(item -> item!=null).collect(Collectors.toList());
  247. return McR.success(McInvoiceDto.formatResponse(result));
  248. }
  249. /**
  250. * 混票识别 [旧版本, 已废弃]
  251. */
  252. @PostMapping("invoice-iv")
  253. McR invoice_iv(@RequestBody Map<String, String> data) throws TencentCloudSDKException {
  254. McException.assertParamException_Null(data, "url");
  255. String image = ydClient.convertTemporaryUrl(data.get("url"),6000,APP_TYPE,SYSTEM_TOKEN);
  256. log.info("混票识别, 免登地址, {}", image);
  257. // 非PDF, 且内存大于3M, 压缩后上传
  258. if (UtilMap.getFloat(data, "size") > 3.0f && !UtilMap.getBoolean(data, "isPdf")) {
  259. image = imageUrlConvertBase64(image);
  260. }
  261. if (UtilMap.getFloat(data, "size") > 6.0f && UtilMap.getBoolean(data, "isPdf")) {
  262. image = pdfUrlConvertBase64(image);
  263. }
  264. // ppExt: 通用字段定义
  265. List<Map> invoices = (List<Map>) txyInvoice.doMixedInvoiceOCR(image).get("MixedInvoiceItems");
  266. List<McInvoiceDto> result = invoices.stream().map(item -> {
  267. String kind = TXYConf.TYPE_INVOICE.get(item.get("Type").toString());
  268. List<Map<String, String>> infos = (List<Map<String, String>>) item.get("SingleInvoiceInfos");
  269. McInvoiceDto.assertSuccess(item, kind); // 响应断言
  270. String invoiceName = findValue(infos, "发票名称");
  271. if (kind.equals("全电发票")) {
  272. kind = invoiceName.contains("增值税专用发票") ? "全电专用发票" : "全电普通发票";
  273. }
  274. if (kind.equals("增值税发票")) {
  275. kind = invoiceName.contains("增值税专用发票") ? "增值税专用发票" : "增值税普通发票";
  276. if (invoiceName.contains("增值税电子")) {
  277. kind = invoiceName.contains("专用发票") ? "增值税电子专用发票" : "增值税电子普通发票";
  278. }
  279. }
  280. McInvoiceDto invoiceDto = McInvoiceDto.builder()
  281. .name(invoiceName)
  282. .kindName(kind)
  283. .kind(McInvoiceKind.getKindCode(kind))
  284. .code(findValue(infos, "发票代码", "票据代码")) // 发票, 非税发票
  285. // 储存唯一ID [发票, 火车票, 行程单]
  286. .serial(findValue(infos, "发票号码", "编号", "电子客票号码", "票据号码").replace("No", "")) // 发票, 非税发票
  287. .date(findValue(infos, "开票日期").replace("年", "-").replace("月", "-").replace("日", ""))
  288. .checkCode(findValue(infos, "校验码"))
  289. .amount(UtilNumber.replaceCurrencyCHYToDecimal(findValue(infos, "小写金额", "价税合计(小写)", "合计金额", "票价", "金额"))) // 发票, 全电票, 行程单, 火车票, 过路过桥费
  290. .excludingTax(UtilNumber.replaceCurrencyCHYToDecimal(findValue(infos, "合计金额", "金额", "票价", "小写金额"))) // [ppExt: 多明细行时, 优先取值合计] 行程单, 火车票, 定额发票
  291. .tax(UtilNumber.replaceCurrencyCHYToDecimal(findValue(infos, "合计税额"))) // 增值税发票
  292. .buyerName(guyuanNameRepalce(findValue(infos, "购买方名称", "交款人"))) // 发票, 非税发票
  293. .buyerTaxId(findValue(infos, "购买方识别号", "购买方统一社会信用代码/纳税人识别号", "交款人统一社会信用代码")) // 发票, 全电票, 非税发票
  294. .sellerName(guyuanNameRepalce(findValue(infos, "销售方名称", "填开单位"))) // 行程单
  295. .sellerTaxId(findValue(infos, "销售方识别号", "销售方统一社会信用代码/纳税人识别号", "销售单位代号")) // 发票, 全电票, 行程单
  296. .passengerName(findValue(infos, "旅客姓名", "姓名")) // 行程单, 火车票
  297. .seatType(findValue(infos, "座位等级", "席别")) // 行程单, 火车票
  298. .departurePort(findValue(infos, "始发地", "出发站", "入口")) // 行程单, 火车票, 过路过桥费
  299. .arrivePort(findValue(infos, "目的地", "到达站", "出口")) // 行程单, 火车票, 过路过桥费
  300. .trainNo(findValue(infos, "航班号", "车次", "车牌号")) // 行程单, 火车票, 出租车
  301. .insuranceCosts(UtilNumber.setBigDecimal((findValue(infos, "保险费")))) // 行程单
  302. .fuelCosts(UtilNumber.setBigDecimal((findValue(infos, "燃油附加费")))) // 行程单
  303. .constructionCosts(UtilNumber.setBigDecimal((findValue(infos, "民航发展基金")))) // 行程单
  304. .build();
  305. // 价格不一致情况下, 通过合计返回
  306. if (!UtilNumber.equalBigDecimal(invoiceDto.getAmount(), invoiceDto.getExcludingTax().add(invoiceDto.getTax()))) {
  307. invoiceDto.setAmount(invoiceDto.getExcludingTax().add(invoiceDto.getTax()));
  308. }
  309. // 机票行程单
  310. if (kind.equals(McInvoiceKind.JP.getDesc())) {
  311. String date = findValue(infos, "日期").replace("年", "-").replace("月", "-").replace("日", " ");
  312. invoiceDto.setDepartureTime(date + " " + findValue(infos, "时间"));
  313. }
  314. // 火车票
  315. if (kind.equals(McInvoiceKind.HC.getDesc())) {
  316. invoiceDto.setDepartureTime(findValue(infos, "出发时间").replace("年", "-").replace("月", "-").replace("日", " "));
  317. }
  318. // 火车票
  319. if (kind.equals(McInvoiceKind.HCDZ.getDesc())) {
  320. invoiceDto.setDepartureTime(findValue(infos, "出发时间").replace("年", "-").replace("月", "-").replace("日", " "));
  321. }
  322. // 出租车
  323. if (kind.equals(McInvoiceKind.CZC.getDesc())) {
  324. String date = findValue(infos, "日期").replace("年", "-").replace("月", "-").replace("日", " ");
  325. invoiceDto.setDepartureTime(date + " " + findValue(infos, "上车"));
  326. }
  327. return invoiceDto;
  328. }).collect(Collectors.toList());
  329. return McR.success(McInvoiceDto.formatResponse(result));
  330. }
  331. /**
  332. * 发票查重, 验真
  333. */
  334. @PostMapping("invoice-va")
  335. McR invoice_va(@RequestBody Map data) {
  336. McException.assertParamException_Null(data, "param");
  337. List<McInvoiceDto> invoices = JSON.parseArray(JSON.toJSONString(data.get("param")), McInvoiceDto.class);
  338. log.info("发票查重, 验真, {}", invoices);
  339. invoices.forEach(UtilMc.consumerWithIndex((item, index) -> {
  340. McInvoiceDto dto = (McInvoiceDto) item;
  341. String invoiceNo = dto.getSerial(); // 唯一标识, 发票号码
  342. String serial = "第【" + (index + 1) + "】张发票";
  343. validateBuyer(dto.getBuyerName(), serial + "有疑问");
  344. McException.assertAccessException(StringUtils.isBlank(invoiceNo), serial + ", 识别结果为空, 请检查!");
  345. YDParam ydParam = YDParam.builder().systemToken(SYSTEM_TOKEN).appType(APP_TYPE)
  346. .formUuid("FORM-442A54C312A64FCA9C1D19C7C1AD7314MXAJ")
  347. .searchFieldJson(JSON.toJSONString(UtilMap.map("radioField_liihyrtb, textField_liihyrt8", "否", invoiceNo)))
  348. .build();
  349. List<String> idList = (List<String>) ydClient.queryData(ydParam, YDConf.FORM_QUERY.retrieve_search_form_id).getData();
  350. if (idList.size() > 0) {
  351. McException.exceptionAccess(serial + "已存在, 请勿重复提交!");
  352. }
  353. // prd 仅仅识别 报销 用途的发票
  354. List<String> yzType=Arrays.asList("增值税普通发票",
  355. "增值税专用发票",
  356. "增值税电子专用发票",
  357. "增值税电子普通发票",
  358. "全电普通发票",
  359. "全电专用发票",
  360. "机票行程单");
  361. // prd 仅仅识别 报销 用途的发票
  362. if(!dto.getType().contains("报销")||!yzType.contains(dto.getKindName())){
  363. return;
  364. }
  365. String serialTips = serial + "有疑问";
  366. try {
  367. // ppExt: 识别与验真后抬头对比 [全电票, 新版本识别接口, 返回名称为: 电子发票(普通发票) 不包含全电标识, 发类型为: 全电发票. 注意取值]
  368. Map rsp = txyInvoice.doVatInvoiceVerifyNew(dto.getKindName(), dto.getCode(), invoiceNo, dto.getDate(), String.valueOf(dto.getAmount()), dto.getCheckCode(), String.valueOf(dto.getExcludingTax()), serialTips,dto.getSellerTaxId());
  369. Map invoice = (Map) rsp.get("Invoice");
  370. McException.assertAccessException(!dto.getBuyerName().equals(guyuanNameRepalce(invoice.get("BuyerName").toString())), serialTips + ", 购买方名称不匹配!");
  371. if(!PublicUtil.isNull(invoice.get("BuyerTaxCode"))){
  372. McException.assertAccessException(!dto.getBuyerTaxId().equals(invoice.get("BuyerTaxCode")), serialTips + ", 购买方税号不匹配!");
  373. }
  374. if(!dto.getKindName().equals("机票行程单")){
  375. McException.assertAccessException(!dto.getSellerName().equals(guyuanNameRepalce(invoice.get("SellerName").toString())), serialTips + ", 销售方名称不匹配!");
  376. McException.assertAccessException(!dto.getSellerTaxId().equals(invoice.get("SellerTaxCode")), serialTips + ", 销售方税号不匹配!");
  377. }
  378. } catch (TencentCloudSDKException e) {
  379. log.error(e.getMessage(), e);
  380. // prd: 上传发票为假发票时,提示:该发票有疑问,请联系财务人员
  381. String message = e.getMessage();
  382. // ppExt: 已经是新版本接口, 过滤提示 [官方答复: 提示不会检测您是否使用的是新版,所有的用户都会提示, 忽略即可]
  383. if (message.contains("温馨提示")) {
  384. message = message.split("温馨提示")[0];
  385. }
  386. if (message.contains("发票不存在")) {
  387. message = "有疑问,请联系财务人员";
  388. }
  389. McException.exceptionAccess(serial + message);
  390. }
  391. }));
  392. return McR.success();
  393. }
  394. @Autowired
  395. private YDService ydService;
  396. @Autowired
  397. private IvYdService ivYdService;
  398. /**
  399. * 发票状态更新: 服务注册
  400. */
  401. @PostMapping("invoice-up")
  402. McR invoice_va(HttpServletRequest request) {
  403. Map data = UtilServlet.getParamMap(request);
  404. log.info("发票状态更新: 服务注册, {}", data);
  405. String compId = UtilMap.getString(data, "compId");
  406. String status = UtilMap.getString(data, "status");
  407. // 读取关联表单
  408. String formUUid="";
  409. List<String> formInstanceIds = new ArrayList<>();
  410. if(compId.equals("selectField_lzs0bpk2")){
  411. // 采购表单
  412. formUUid="FORM-B5A7B20013AE4CD09AD87FAB9A3E145FS3P6";
  413. List<Map> associationData = (List<Map>) JSON.parse(UtilMap.getString(data, "multiAssociation"));
  414. formInstanceIds.addAll(associationData.stream().map(form -> UtilMap.getString(form, "instanceId")).collect(Collectors.toList()));
  415. }else{
  416. formUUid="FORM-442A54C312A64FCA9C1D19C7C1AD7314MXAJ";
  417. List<String> associationForm = (List<String>) JSON.parse(UtilMap.getString(data, "multiAssociation"));
  418. for (String record : associationForm) {
  419. // 解析关联表单
  420. List<Map> associationData = (List<Map>) JSON.parse(record);
  421. formInstanceIds.addAll(associationData.stream().map(form -> UtilMap.getString(form, "instanceId")).collect(Collectors.toList()));
  422. }
  423. }
  424. // 宜搭批量更新
  425. Map update = UtilMap.map(compId, status);
  426. if (compId.equals("selectField_liihyrt6")) {
  427. update.put("radioField_liw7rb2q", "否"); // 提交后, 更新是否退回标识为否
  428. }
  429. // prd 9.10 更新报销单, 关联到发票:: ppExt 宜搭服务注册, 提交规则系统默认字段 [详见 YDService]
  430. // ydService.operateData3(data, update, YDParam.builder().systemToken(SYSTEM_TOKEN).appType(APP_TYPE)
  431. // .formUuid(formUUid)
  432. // .formInstanceIdList(formInstanceIds)
  433. // .updateFormDataJson(JSON.toJSONString(update))
  434. // .build(), YDConf.FORM_OPERATION.multi_update);
  435. ivYdService.operateData(data, update, YDParam.builder().systemToken(SYSTEM_TOKEN).appType(APP_TYPE)
  436. .formUuid(formUUid)
  437. .formInstanceIdList(formInstanceIds)
  438. .updateFormDataJson(JSON.toJSONString(update))
  439. .build(), YDConf.FORM_OPERATION.multi_update);
  440. return McR.success();
  441. }
  442. /**
  443. * 发票状态更新: 退回提交
  444. */
  445. @PostMapping("invoice-zy")
  446. McR invoice_zy(@RequestBody Map data) {
  447. log.info("发票状态更新: 退回提交, {}", data);
  448. List<String> pre_ids = (List<String>) data.get("pre_ids"); // 释放修改前
  449. List<String> cur_ids = (List<String>) data.get("cur_ids"); // 占用修改后
  450. // [前端调用添加] 退回为监听宜搭dom事件, 先执行接口调用, 才会校验宜搭必填, 过滤无效调用
  451. if (cur_ids.size() == 0) {
  452. return McR.success();
  453. }
  454. Map pre_update = (Map) data.get("pre_update");
  455. Map cur_update = (Map) data.get("cur_update");
  456. // 宜搭批量更新
  457. ydClient.operateData(YDParam.builder().systemToken(SYSTEM_TOKEN).appType(APP_TYPE)
  458. .formUuid("FORM-442A54C312A64FCA9C1D19C7C1AD7314MXAJ")
  459. .formInstanceIdList(pre_ids)
  460. .updateFormDataJson(JSON.toJSONString(pre_update))
  461. .build(), YDConf.FORM_OPERATION.multi_update);
  462. ydClient.operateData(YDParam.builder().systemToken(SYSTEM_TOKEN).appType(APP_TYPE)
  463. .formUuid("FORM-442A54C312A64FCA9C1D19C7C1AD7314MXAJ")
  464. .formInstanceIdList(cur_ids)
  465. .updateFormDataJson(JSON.toJSONString(cur_update))
  466. .build(), YDConf.FORM_OPERATION.multi_update);
  467. return McR.success();
  468. }
  469. /**
  470. * 全局查询(不匹配子表单)
  471. */
  472. @PostMapping("validate")
  473. McR queryAll(HttpServletRequest request) {
  474. Map<String, ?> param = UtilServlet.getParamMap(request);
  475. log.info("全局查询(不匹配子表单), {}", param);
  476. if (ObjectUtil.isNull(param.get("uniques"))) {
  477. return McR.success();
  478. }
  479. McException.assertParamException_Null(param, "uniques", "formUuid", "compId");
  480. // 容错 - 尾部分号的空格会被输入框忽略
  481. String[] uniques = String.valueOf(param.get("uniques")).replace("; ", ";").split(";");
  482. for (String val : uniques) {
  483. // 查重校验: 单张发票唯一标识 + 审批已通过 / 审批中
  484. List<Map> conditions = new ArrayList<>();
  485. Map unique = new HashMap();
  486. unique.put("key", param.get("compId"));
  487. unique.put("value", val.split(": ")[1]);
  488. unique.put("type", "TEXT");
  489. unique.put("operator", "like");
  490. unique.put("componentName", "TextField");
  491. conditions.add(unique);
  492. Map approve = new HashMap();
  493. approve.put("key", "processApprovedResult");
  494. approve.put("value", new String[]{"agree"});
  495. approve.put("type", "ARRAY");
  496. approve.put("operator", "in");
  497. approve.put("componentName", "SelectField");
  498. conditions.add(approve);
  499. YDParam ydParam = YDParam.builder()
  500. .appType(APP_TYPE)
  501. .systemToken(SYSTEM_TOKEN)
  502. .formUuid(String.valueOf(param.get("formUuid")))
  503. .searchCondition(JSON.toJSONString(conditions))
  504. .build();
  505. DDR_New ddr_new = ydClient.queryData(ydParam, YDConf.FORM_QUERY.retrieve_list);
  506. log.info("审批通过匹配结果, {}, {}", ddr_new.getTotalCount(), ddr_new.getData());
  507. if (ddr_new.getTotalCount() > 0) {
  508. return McR.errorAccess("发票已被使用, 请勿重复提交!");
  509. }
  510. conditions.remove(approve);
  511. Map status = new HashMap();
  512. status.put("key", "processInstanceStatus");
  513. status.put("value", new String[]{"RUNNING"});
  514. status.put("type", "ARRAY");
  515. status.put("operator", "in");
  516. status.put("componentName", "SelectField");
  517. conditions.add(status);
  518. ydParam.setSearchCondition(JSON.toJSONString(conditions));
  519. ddr_new = ydClient.queryData(ydParam, YDConf.FORM_QUERY.retrieve_list);
  520. log.info("审批通过匹配结果, {}, {}", ddr_new.getTotalCount(), ddr_new.getData());
  521. if (ddr_new.getTotalCount() > 0) {
  522. return McR.errorAccess("发票已在流程中, 请勿重复提交!");
  523. }
  524. }
  525. return McR.success();
  526. }
  527. @PostMapping("test")
  528. McR test() {
  529. // List<Map> process = (List<Map>) ydClient.queryData(YDParam.builder()
  530. // .formUuid("442A54C312A64FCA9C1D19C7C1AD7314MXAJ")
  531. // .formInstId("FINST-NGA66WA1FV4EB7QJC3OATA3EV8MK35Z9COEMLFR22")
  532. // .build(), YDConf.FORM_QUERY.retrieve_id).getData();
  533. List<Map> process = (List<Map>) ydClient.queryData(YDParam.builder().systemToken(SYSTEM_TOKEN).appType(APP_TYPE)
  534. .formUuid("FORM-0IA66C71F6NBAETREO8DE9SSN43D3YIZ0AYILC")
  535. .searchFieldJson(JSON.toJSONString(UtilMap.map("textField_lmewsobs", "Y16668919W4E4FHQ6123ADDHB8XK3S709YEMLXWF")))
  536. .build(), YDConf.FORM_QUERY.retrieve_search_form).getData();
  537. return McR.success();
  538. }
  539. }