NhIVController.java 30 KB

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