ERPServiceImpl.java 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. package com.uas.eis.service.Impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.TypeReference;
  5. import com.uas.eis.config.DonlimConfig;
  6. import com.uas.eis.core.config.SpObserver;
  7. import com.uas.eis.core.support.TokenProperties;
  8. import com.uas.eis.dao.*;
  9. import com.uas.eis.dto.*;
  10. import com.uas.eis.service.CommonService;
  11. import com.uas.eis.service.ERPService;
  12. import com.uas.eis.service.NuoNuoSDKService;
  13. import com.uas.eis.utils.*;
  14. import nuonuo.open.sdk.NNOpenSDK;
  15. import org.apache.commons.lang.StringUtils;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import java.text.SimpleDateFormat;
  21. import java.util.*;
  22. import java.util.stream.Collectors;
  23. import java.util.stream.Stream;
  24. /**
  25. * @author koul
  26. * @email koul@usoftchina.com
  27. * @date 2021-12-06 18:27
  28. */
  29. @Service
  30. public class ERPServiceImpl implements ERPService {
  31. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  32. private static Map<String, String> tokenConfig = TokenProperties.getAllProperty();
  33. @Autowired
  34. private BaseDao baseDao;
  35. @Autowired
  36. private DonlimConfig donlimConfig;
  37. @Autowired
  38. private CommonService commonService;
  39. @Autowired
  40. private NuoNuoSDKService nuoNuoSDKService;
  41. @Override
  42. public Map<String, Object> pushInvoiceOrder(String master, int id) {
  43. Map<String, Object> map = new HashMap<String, Object>();
  44. Object[] obs = baseDao.getFieldsDataByCondition("DATACENTER.dock_invoice_config", new String[]{"appKey", "appSecret", "token", "taxnum"}, "username='" + master + "'");
  45. String url = donlimConfig.getRoute();
  46. System.out.println(url);
  47. String appKey = donlimConfig.getAppKey();
  48. String appSecret = donlimConfig.getAppSecret();
  49. String token = donlimConfig.getToken();
  50. String taxnum = donlimConfig.getTaxnum();
  51. if (donlimConfig.isProd()) {
  52. appKey = obs[0].toString();
  53. appSecret = obs[1].toString();
  54. token = obs[2].toString();// 访问令牌
  55. taxnum = obs[3].toString();// 授权企业税号
  56. }
  57. logger.info("pushInvoiceOrder begin: master {} ,id {} ", master, id);
  58. SpObserver.putSp(master);
  59. SqlRowList rs_main = baseDao.queryForRowSet("select * from invoiceOrder left join customer on io_cucode=cu_code " +
  60. " left join custinvoicemsg on ci_cucode = cu_code " +
  61. " where io_id=" + id);
  62. if (rs_main.next()) {
  63. //String autoInvoiceFlag= baseDao.isDBSetting("InvoiceOrder", "autoInvoice")?"true":"false";
  64. //数据组装
  65. InvoiceOrderDTO invoiceOrderDTO = new InvoiceOrderDTO();
  66. invoiceOrderDTO.setOrderType(rs_main.getGeneralString("IO_ORDERTYPE"));
  67. invoiceOrderDTO.setAutoInvoiceFlag(rs_main.getGeneralString("io_autoinvoice"));//自动开票
  68. invoiceOrderDTO.setRepeatCall("0");
  69. OrderDTO orderDTO = new OrderDTO();
  70. orderDTO.setOriginalOrderNo(rs_main.getGeneralString("io_code"));
  71. orderDTO.setAddDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(rs_main.getDate("io_date")));
  72. orderDTO.setInvoiceLine(rs_main.getGeneralString("io_invoiceline"));
  73. orderDTO.setSpecificFactor("0");
  74. orderDTO.setSaleTaxNum(donlimConfig.getTaxnum());//
  75. orderDTO.setBuyerName(rs_main.getGeneralString("cu_name"));//客户名称
  76. orderDTO.setTaxNum(rs_main.getGeneralString("ci_taxnum").trim());//购方税号
  77. orderDTO.setAccount(rs_main.getGeneralString("ci_account"));
  78. orderDTO.setAddress(rs_main.getGeneralString("ci_address"));
  79. orderDTO.setTelephone(rs_main.getGeneralString("ci_telephone"));
  80. orderDTO.setRemark(rs_main.getGeneralString("io_remark"));
  81. orderDTO.setPayee(rs_main.getGeneralString("IO_PAYEE"));
  82. orderDTO.setChecker(rs_main.getGeneralString("IO_CHECKER"));
  83. orderDTO.setPhone(rs_main.getGeneralString("ci_jf_phone"));
  84. orderDTO.setEmail(rs_main.getGeneralString("ci_jf_email"));
  85. //原销售订单编号relateSaleOrderNo
  86. orderDTO.setRelateSaleOrderNo(rs_main.getGeneralString("io_RelateSaleOrderNo"));
  87. //原发票号码yfphm
  88. orderDTO.setYfphm(rs_main.getGeneralString("io_Yfphm"));
  89. invoiceOrderDTO.setOrderInfo(orderDTO);
  90. SqlRowList rs_det = baseDao.queryForRowSet("select * from invoiceOrderdet_view where id_ioid=" + id + " order by id_detno");
  91. List<OrderDetDTO> dets = new ArrayList<>();//商品列表
  92. while (rs_det.next()) {
  93. OrderDetDTO orderDetDTO = new OrderDetDTO();
  94. orderDetDTO.setRowNo(rs_det.getGeneralString("id_id"));//唯一值
  95. orderDetDTO.setGoodsName(rs_det.getGeneralString("pr_detail"));
  96. orderDetDTO.setSpec(rs_det.getGeneralString("pr_spec"));
  97. orderDetDTO.setUnit(rs_det.getGeneralString("pr_unit"));
  98. orderDetDTO.setNum(rs_det.getGeneralString("id_qty"));
  99. //orderDetDTO.setPrice(rs_det.getGeneralString("id_price"));//平台算法问题 传单价会有差异,校验可能不通过,所有传金额和数量,不传单价
  100. orderDetDTO.setTaxRate(rs_det.getGeneralString("taxrate"));//0.13
  101. orderDetDTO.setTaxAmount(rs_det.getGeneralString("id_amount"));
  102. orderDetDTO.setHsbz(rs_det.getGeneralString("hsbz"));
  103. orderDetDTO.setSpbm(rs_det.getGeneralString("spbm"));
  104. orderDetDTO.setZke(rs_det.getGeneralString("ZKE"));
  105. dets.add(orderDetDTO);
  106. }
  107. invoiceOrderDTO.setOrderOriginalDetails(dets);
  108. invoiceOrderDTO.setInvoiceGoodsTransports(new ArrayList<>());
  109. System.out.println(JSONArray.toJSONString(invoiceOrderDTO));
  110. //调用对接
  111. NNOpenSDK sdk = NNOpenSDK.getIntance();
  112. String method = "nuonuo.OpeMplatform.saveOrder"; // API方法名
  113. String content = sdk.convertToUtf8(JSONArray.toJSONString(invoiceOrderDTO));
  114. String senid = UUID.randomUUID().toString().replace("-", ""); // 唯一标识,32位随机码,无需修改,保持默认即可
  115. String result = sdk.sendPostSyncRequest(url, senid, appKey, appSecret, token, taxnum, method, content);
  116. logger.info("pushInvoiceOrder resultString {}, token {}", result, token);
  117. Map<Object, Object> res = JSONUtil.toMap(result);
  118. System.out.println(result);
  119. Object status = res.get("status");
  120. Object message = res.get("message");
  121. if (status != null && "0000".equals(status.toString())) {//调用成功
  122. baseDao.execute("update invoiceOrder set io_dockstatus='对接成功',IO_INVOICESTATUS=0 where io_id=" + id);
  123. commonService.logSRecord("开票订单对接", id);
  124. } else if (status != null && "9999".equals(status.toString()) && message != null && message.toString().contains("已存在原始订单")) {
  125. baseDao.execute("update invoiceOrder set io_dockstatus='对接成功',IO_INVOICESTATUS=0 where io_id=" + id);
  126. commonService.logFRecord("开票订单对接", "(" + status + ")" + message, id);
  127. } else {
  128. baseDao.execute("update invoiceOrder set io_dockstatus='对接失败' where io_id=" + id);
  129. commonService.logFRecord("开票订单对接", "(" + status + ")" + message, id);
  130. }
  131. logger.info("pushInvoiceOrder master {} ,id {} status {} message {}", master, id, status, message);
  132. map.put("success", true);
  133. map.put("status", status);
  134. map.put("message", message);
  135. return map;
  136. } else {//数据无效
  137. logger.info("pushInvoiceOrder err {对接源信息无效} ,master {} , id {} status {} message {}", master, id);
  138. map.put("success", false);
  139. map.put("message", "对接源信息无效");
  140. return map;
  141. }
  142. }
  143. @Override
  144. public Map<String, Object> invoiceOrderSubmitKp(String master, int id) {
  145. Object[] obs = baseDao.getFieldsDataByCondition("DATACENTER.dock_invoice_config", new String[]{"appKey", "appSecret", "token", "taxnum"}, "username='" + master + "'");
  146. String url = donlimConfig.getRoute();
  147. System.out.println(url);
  148. String appKey = donlimConfig.getAppKey();
  149. String appSecret = donlimConfig.getAppSecret();
  150. String token = donlimConfig.getToken();
  151. String taxnum = donlimConfig.getTaxnum();
  152. if (donlimConfig.isProd()) {
  153. appKey = obs[0].toString();
  154. appSecret = obs[1].toString();
  155. token = obs[2].toString();// 访问令牌
  156. taxnum = obs[3].toString();// 授权企业税号
  157. }
  158. SpObserver.putSp(master);
  159. SqlRowList rs_main = baseDao.queryForRowSet("select * from invoiceOrder left join customer on io_cucode=cu_code where io_id=" + id);
  160. if (rs_main.next()) {
  161. NNOpenSDK sdk = NNOpenSDK.getIntance();
  162. String method = "nuonuo.OpeMplatform.submitKp"; // API方法名
  163. String code = rs_main.getGeneralString("io_code");
  164. Map<String, String> reqParam = new HashMap<>();
  165. reqParam.put("orderNo", code);
  166. String content = JSONArray.toJSONString(reqParam);
  167. String senid = UUID.randomUUID().toString().replace("-", ""); // 唯一标识,32位随机码,无需修改,保持默认即可
  168. String result = sdk.sendPostSyncRequest(url, senid, appKey, appSecret, token, taxnum, method, content);
  169. System.out.println(result);
  170. } else {//数据无效
  171. logger.info("findOrderState err {对接源信息无效} ,master {} , id {} status {} message {}", master, id);
  172. }
  173. return null;
  174. }
  175. @Override
  176. public Map<String, Object> findOrderState(String master, int id) {
  177. Map<String, Object> map = new HashMap<String, Object>();
  178. Object[] obs = baseDao.getFieldsDataByCondition("DATACENTER.dock_invoice_config", new String[]{"appKey", "appSecret", "token", "taxnum"}, "username='" + master + "'");
  179. if (obs == null) {
  180. return null;//对接信息为配置
  181. }
  182. String url = donlimConfig.getRoute();
  183. String appKey = donlimConfig.getAppKey();
  184. String appSecret = donlimConfig.getAppSecret();
  185. String token = donlimConfig.getToken();
  186. String taxnum = donlimConfig.getTaxnum();
  187. if (donlimConfig.isProd()) {
  188. appKey = obs[0].toString();
  189. appSecret = obs[1].toString();
  190. token = obs[2].toString();// 访问令牌
  191. taxnum = obs[3].toString();// 授权企业税号
  192. }
  193. SpObserver.putSp(master);
  194. SqlRowList rs_main = baseDao.queryForRowSet("select * from invoiceOrder left join customer on io_cucode=cu_code where io_id=" + id);
  195. if (rs_main.next()) {
  196. String code = rs_main.getGeneralString("io_code");
  197. NNOpenSDK sdk = NNOpenSDK.getIntance();
  198. String method = "nuonuo.OpeMplatform.findOrderState"; // API方法名
  199. Map<String, String> reqParam = new HashMap<>();
  200. reqParam.put("originalOrderNo", code);
  201. reqParam.put("saleTaxNum", taxnum);
  202. String content = JSONArray.toJSONString(reqParam);
  203. String senid = UUID.randomUUID().toString().replace("-", ""); // 唯一标识,32位随机码,无需修改,保持默认即可
  204. String result = sdk.sendPostSyncRequest(url, senid, appKey, appSecret, token, taxnum, method, content);
  205. System.out.println(result);
  206. FindOrderStateResp resp = JSON.parseObject(result, new TypeReference<FindOrderStateResp>() {
  207. });
  208. System.out.println(resp.getCode());
  209. System.out.println(resp.getMessage());
  210. if ("该订单不存在".equals(resp.getMessage())) {
  211. baseDao.execute("update invoiceOrder set IO_INVOICESTATUS = '-1' where io_id = '" + id + "'");
  212. } else {
  213. baseDao.execute("update invoiceOrder set IO_INVOICESTATUS = '" + resp.getData().getInvoiceStatus() + "'"
  214. + ",io_errorMsg='" + StringUtil.nvl(resp.getData().getErrorMsg(), "") + "'"
  215. + " where io_id = '" + id + "'");
  216. }
  217. map.put("success", true);
  218. map.put("result", result);
  219. return map;
  220. } else {//数据无效
  221. logger.info("findOrderState err {对接源信息无效} ,master {} , id {} status {} message {}", master, id);
  222. }
  223. return null;
  224. }
  225. @Override
  226. public Map<String, Object> cancelInvoiceOrder(String master, int id) {
  227. Map<String, Object> map = new HashMap<String, Object>();
  228. Object[] obs = baseDao.getFieldsDataByCondition("DATACENTER.dock_invoice_config", new String[]{"appKey", "appSecret", "token", "taxnum"}, "username='" + master + "'");
  229. if (obs == null) {
  230. return null;//对接信息为配置
  231. }
  232. String url = donlimConfig.getRoute();
  233. System.out.println(url);
  234. String appKey = donlimConfig.getAppKey();
  235. String appSecret = donlimConfig.getAppSecret();
  236. String token = donlimConfig.getToken();
  237. String taxnum = donlimConfig.getTaxnum();
  238. if (donlimConfig.isProd()) {
  239. System.out.println("prod");
  240. appKey = obs[0].toString();
  241. appSecret = obs[1].toString();
  242. token = obs[2].toString();// 访问令牌
  243. taxnum = obs[3].toString();// 授权企业税号
  244. } else {
  245. System.out.println("dev");
  246. }
  247. SpObserver.putSp(master);
  248. SqlRowList rs_main = baseDao.queryForRowSet("select * from invoiceOrder left join customer on io_cucode=cu_code where io_id=" + id);
  249. if (rs_main.next()) {
  250. String code = rs_main.getGeneralString("io_code");
  251. Map<String, String> reqParam = new HashMap<>();
  252. reqParam.put("originalOrderNo", code);
  253. reqParam.put("saleTaxNum", taxnum);
  254. String content = JSONArray.toJSONString(reqParam);
  255. NNOpenSDK sdk = NNOpenSDK.getIntance();
  256. String method = "nuonuo.OpeMplatform.deleteOrder"; // API方法名
  257. String senid = UUID.randomUUID().toString().replace("-", ""); // 唯一标识,32位随机码,无需修改,保持默认即可
  258. String result = sdk.sendPostSyncRequest(url, senid, appKey, appSecret, token, taxnum, method, content);
  259. Map<Object, Object> res = JSONUtil.toMap(result);
  260. System.out.println(result);
  261. Object status = res.get("status");
  262. Object message = res.get("message");
  263. if (status != null && "0000".equals(status.toString())) {//调用成功
  264. baseDao.execute("update invoiceOrder set io_dockstatus='' where io_id=" + id);
  265. baseDao.execute("insert into invoicedockLog(id_,code_,date_,result_,err_,keyvalue_)values(invoicedockLog_seq.nextval,'开票订单删除',sysdate,'SUCCEED',''," + id + ") ");
  266. map.put("success", true);
  267. map.put("message", message);
  268. } else {
  269. baseDao.execute("update invoiceOrder set io_dockstatus='取消失败' where io_id=" + id);
  270. baseDao.execute("insert into invoicedockLog(id_,code_,date_,result_,err_,keyvalue_)values(invoicedockLog_seq.nextval,'开票订单删除',sysdate,'FAILED','(" + status + ")" + message + "'," + id + ") ");
  271. map.put("success", false);
  272. map.put("message", "(" + status + ")" + message);
  273. }
  274. logger.info("cancelInvoiceOrder master {} ,id {} status {} message {}", master, id, status, message);
  275. return map;
  276. } else {//数据无效
  277. logger.info("cancelInvoiceOrder err {对接源信息无效} ,master {} , id {} status {} message {}", master, id);
  278. }
  279. return null;
  280. }
  281. @Override
  282. public Map<String, Object> getElecInvoicesById(String master, Integer id) {
  283. Map<String, Object> map = new HashMap<String, Object>();
  284. Object[] obs = baseDao.getFieldsDataByCondition("DATACENTER.dock_invoice_config", new String[]{"appKey", "appSecret", "token", "taxnum"}, "username='" + master + "'");
  285. if (obs == null) {
  286. return null;//对接信息为配置
  287. }
  288. // System.out.println(url);
  289. String appKey = donlimConfig.getAppKey();
  290. String appSecret = donlimConfig.getAppSecret();
  291. String token = donlimConfig.getToken();
  292. String taxnum = donlimConfig.getTaxnum();
  293. logger.info("ElecI-getElecInvoicesById:master {} isProd {}", SpObserver.getSp(), donlimConfig.isProd());
  294. if (donlimConfig.isProd()) {
  295. appKey = obs[0].toString();
  296. appSecret = obs[1].toString();
  297. token = obs[2].toString();// 访问令牌
  298. taxnum = obs[3].toString();// 授权企业税号
  299. }
  300. Map<String, Object> m = new HashMap<>();
  301. m.put("appKey", appKey);
  302. m.put("appSecret", appSecret);
  303. m.put("token", token);
  304. m.put("taxnum", taxnum);
  305. SpObserver.putSp(master);
  306. SqlRowList ioList = baseDao.queryForRowSet("select io_id,io_code,io_ordertype from " + master + ".invoiceOrder left join " + master + ".customer on io_cucode=cu_code " +
  307. " where io_id = " + id);
  308. while (ioList.next()) {
  309. m.put("io_id", ioList.getGeneralInt("io_id"));
  310. m.put("io_code", ioList.getGeneralString("io_code"));
  311. m.put("io_ordertype", ioList.getGeneralInt("io_ordertype"));
  312. map = getElecInvoices(m);
  313. }
  314. return map;
  315. }
  316. @Override
  317. public Map<String, Object> getElecInvoices(Map<String, Object> m) {
  318. if (Integer.valueOf(m.get("io_ordertype").toString()).equals(2)) {
  319. return getRedReturnElecInvoices(m);
  320. }
  321. String taxnum = m.get("taxnum").toString();
  322. int ioid = Integer.valueOf(m.get("io_id").toString());
  323. String ioCode = m.get("io_code").toString();
  324. Map<String, Object> res = new HashMap<String, Object>();
  325. String errCode = "-999";
  326. String errMsg = "";
  327. try {
  328. //调用对接
  329. FindOrderRelationInvoiceReq foriReq = new FindOrderRelationInvoiceReq();
  330. foriReq.setOriginalOrderNo(ioCode);//订单编号
  331. foriReq.setSaleTaxNum(taxnum);//销方税号
  332. foriReq.setNeedInvoiceSourceDetail("1");//是否需要发票所来源的订单明细,1-是, 0-否(不填默认 0) 当为0时,将不回传orderDetailCallBackVOS。先默认不需要明细
  333. FindOrderRelationInvoiceResp resp = nuoNuoSDKService.findOrderRelationInvoice(foriReq, m);
  334. errCode = resp.getStatus();
  335. errMsg = resp.getMessage();
  336. logger.info("ElecI-DownLoad-master: {} ,id {} status {} message {}", SpObserver.getSp(), ioid, errCode, errMsg);
  337. if ("0000".equals(errCode)) {//调用成功
  338. //记录发票明细
  339. baseDao.execute(getElecInvoicesSqls(ioid, resp.getData()));
  340. //红字
  341. commonService.logSRecord("订单关联发票对接", ioid);
  342. } else if (!"8958".equals(errCode)) {// {"status":"8958","message":"该订单对应的开票单不存在","data":null} 没有开票单的不记录对接失败日志
  343. if (errMsg.length() > 3980) {
  344. errMsg = errMsg.substring(0, 3980);
  345. }
  346. baseDao.execute("update invoiceOrder set io_errorMsg='订单关联发票查询失败: " + errMsg + "' where io_id=" + ioid);
  347. commonService.logFRecord("订单关联发票对接", "(" + errCode + ")" + errMsg, ioid);
  348. }
  349. } catch (Exception e) {
  350. e.printStackTrace();
  351. errMsg = "发票获取异常";
  352. if (StringUtil.hasText(e.getMessage())) {
  353. errMsg = e.getMessage();
  354. } else if (e.getCause() != null && StringUtil.hasText(e.getCause().getMessage())) {
  355. errMsg = e.getCause().getMessage();
  356. }
  357. logger.info("ElecI-DownLoad-master: {} ,id {} message {}", SpObserver.getSp(), ioid, errMsg);
  358. commonService.logFRecord("订单关联发票对接", errMsg.replaceAll("'", "''"), ioid);
  359. }
  360. //获取红字发票PDF地址
  361. List<String> serialNosAll = baseDao.queryForList("select ei_redsid from ElecInvoice " +
  362. "where ei_ioid = " + ioid + " and EI_RELATIVEREFNO is not null and ei_redpdfurl is null and nvl(EI_BILLSTATUS,' ') in (' ','01','04')", String.class);
  363. if (!CollectionUtil.isEmpty(serialNosAll)) {
  364. queryRedPdfUrl(serialNosAll, m);
  365. }
  366. if (StringUtil.hasText(errMsg) && !"调用成功".equals(errMsg)) {
  367. res.put("success", false);
  368. res.put("errCode", errCode);
  369. res.put("errMsg", errMsg);
  370. } else {
  371. res.put("success", true);
  372. }
  373. return res;
  374. }
  375. private List<String> getElecInvoicesSqls(int ioid, List<FindOrderRelationInvoiceDTO> data) {
  376. List<String> sqls = new ArrayList<String>();
  377. boolean syncToBillOut = baseDao.isDBSetting("BillOut", "syncToBillOut");
  378. for (int i = 0; i < data.size(); i++) {
  379. FindOrderRelationInvoiceDTO friDto = data.get(i);
  380. sqls.addAll(friDto.toMergeSql(ioid, (i + 1)));
  381. }
  382. sqls.add(" update invoiceOrder set io_refno = (" +
  383. " select (min(ei_refno)||(case when max(ei_refno) <> min(ei_refno) then '-'||max(ei_refno) else '' end)) " +
  384. " from ELECINVOICE " +
  385. " where ei_ioid = io_id and EI_INVOICETYPE = 1 " +
  386. " and ( nvl(ei_rushredstate,0) <> 1 or (nvl(ei_rushredstate,0) = 1 and exists (Select 1 from invoiceOrder redio where redio.io_ordertype=2 and redio.io_yfphm = ei_refno)) ) " +
  387. " ) " +
  388. " where io_id = " + ioid);
  389. //更新开票记录
  390. if (syncToBillOut) {
  391. sqls.add("update BillOut set bi_refno = (select max(io_refno) from invoiceOrder where io_id = " + ioid + " and io_biid = bi_id) where bi_id in (select io_biid from invoiceOrder where io_id = " + ioid + " and io_refno is not null)");
  392. }
  393. // System.out.println("sqls:");
  394. // System.out.println(sqls.toString());
  395. return sqls;
  396. }
  397. @Override
  398. public Map<String, Object> rushRed(String master, int id) {
  399. Map<String, Object> map = new HashMap<String, Object>();
  400. Object[] obs = baseDao.getFieldsDataByCondition("DATACENTER.dock_invoice_config", new String[]{"appKey", "appSecret", "token", "taxnum"}, "username='" + master + "'");
  401. String url = donlimConfig.getRoute();
  402. String appKey = donlimConfig.getAppKey();
  403. String appSecret = donlimConfig.getAppSecret();
  404. String token = donlimConfig.getToken();
  405. String taxnum = donlimConfig.getTaxnum();
  406. if (donlimConfig.isProd()) {
  407. appKey = obs[0].toString();
  408. appSecret = obs[1].toString();
  409. token = obs[2].toString();// 访问令牌
  410. taxnum = obs[3].toString();// 授权企业税号
  411. }
  412. Map<String, Object> m = new HashMap<>();
  413. m.put("appKey", appKey);
  414. m.put("appSecret", appSecret);
  415. m.put("token", token);
  416. m.put("taxnum", taxnum);
  417. logger.info("rushRed begin: master {} ,id {} ", master, id);
  418. SpObserver.putSp(master);
  419. StringBuffer errmsg = new StringBuffer();
  420. StringBuffer remindmsg = new StringBuffer();
  421. SqlRowList rs_main = baseDao.queryForRowSet("select * from invoiceOrder left join customer on io_cucode=cu_code left join elecinvoice on io_id=ei_ioid where io_id=" + id + " and nvl(ei_redapplyid,' ')=' ' and nvl(EI_INVOICESTATE,0)=1 and nvl(EI_INVOICETYPE,0)=1 ");
  422. if (rs_main.hasNext()) {
  423. int scount = 0;
  424. while (rs_main.next()) {
  425. RedInvoiceReq redInvoiceReq = new RedInvoiceReq();
  426. redInvoiceReq.setBlueInvoiceLine(rs_main.getGeneralString("io_invoiceline"));//蓝票发票种类
  427. redInvoiceReq.setApplySource("0");//申请方(录入方)身份: 0 销方 1 购方
  428. redInvoiceReq.setSellerTaxNo(donlimConfig.getTaxnum());//销方税号
  429. redInvoiceReq.setBuyerName(rs_main.getGeneralString("cu_name"));//购方名称
  430. redInvoiceReq.setBuyerTaxNo(rs_main.getGeneralString("ei_taxno"));
  431. redInvoiceReq.setRedReason(rs_main.getGeneralString("io_redreason"));
  432. //redInvoiceReq.setAutoInvoice("1");//测试是否能自动红冲目前参数无效
  433. redInvoiceReq.setBlueElecInvoiceNumber(rs_main.getGeneralString("ei_allelectronicinvoicenumber"));
  434. NNOpenSDK sdk = NNOpenSDK.getIntance();
  435. String content = sdk.convertToUtf8(JSONArray.toJSONString(redInvoiceReq));
  436. String method = "nuonuo.OpeMplatform.saveInvoiceRedConfirm"; // API方法名
  437. String senid = UUID.randomUUID().toString().replace("-", ""); // 唯一标识,32位随机码,无需修改,保持默认即可
  438. String resp = sdk.sendPostSyncRequest(url, senid, appKey, appSecret, token, taxnum, method, content);
  439. Map<Object, Object> res = JSONUtil.toMap(resp);
  440. Object code = res.get("code");
  441. Object describe = res.get("describe");
  442. Object result = res.get("result");
  443. logger.info("rushRed master {} ,id {} code {} describe {} applycode {}", master, id, code, describe, result);
  444. if (code != null && "E0000".equals(code.toString())) {//调用成功
  445. baseDao.execute("update elecinvoice set ei_redapplyid='" + result.toString() + "' where ei_id=" + rs_main.getGeneralInt("ei_id"));
  446. scount++;
  447. commonService.logSRecord("红字确认单申请(" + rs_main.getGeneralString("ei_refno") + ")", id);
  448. } else {
  449. //E9755 describe 该发票存在进行中的红字确认单,暂时不能冲红
  450. commonService.logFRecord("红字确认单申请(" + rs_main.getGeneralInt("ei_id") + ")", "(" + code + ")" + describe, id);
  451. errmsg.append("(" + code + ")" + describe).append("</br>");
  452. }
  453. }
  454. //没有自动开红票 但是可以拿状态
  455. if (scount > 0) {
  456. m.put("io_id", id);
  457. m.put("ei_id", 0);
  458. //获取红字确认状态等信息,若自动开票成功,则同时获取红票信息
  459. Map<String, Object> mm = queryInvoiceRedConfirm(m);
  460. if ("false".equals(mm.get("success")) && StringUtil.hasText(mm.get("errMsg"))) {
  461. remindmsg.append(mm.get("errMsg"));
  462. }
  463. }
  464. } else {//数据无效
  465. logger.info("rushRed err {对接源信息无效} ,master {} , id {} status {} message {}", master, id);
  466. errmsg.append("对接源信息无效");
  467. return map;
  468. }
  469. if (errmsg.length() > 0) {
  470. map.put("success", false);
  471. if (remindmsg.length() > 0) {
  472. errmsg.append(remindmsg.toString());
  473. }
  474. map.put("message", errmsg.toString());
  475. } else {
  476. map.put("success", true);
  477. if (remindmsg.length() > 0) {
  478. map.put("message", remindmsg.toString());
  479. }
  480. }
  481. return map;
  482. }
  483. /*
  484. 条件:蓝票、有确认单号、待确认或者无需确认的情况下 持续获取红票相关信息
  485. 按照红冲确认单查询对应状态
  486. 可以查到信息: 确认单状态、开票状态、冲红原因、
  487. 若已开票,则自动获取红票 发票号:
  488. 冲红原因: 1销货退回 2开票有误 3服务中止 4销售折让
  489. */
  490. @Override
  491. public Map<String, Object> queryInvoiceRedConfirm(Map<String, Object> m) {
  492. int ioid = Integer.valueOf(m.get("io_id").toString());
  493. int eiid = Integer.valueOf(m.get("ei_id").toString());
  494. Map<String, Object> res = new HashMap<String, Object>();
  495. StringBuffer errmsg = new StringBuffer();
  496. SqlRowList bluePiao = baseDao.queryForRowSet("select ei_id,ei_redapplyid,ei_billStatus,ei_billNo,ei_billUuid,ei_taxExcludedAmount,ei_taxAmount,ei_redReason,io_ordertype " +
  497. "from elecinvoice left join invoiceOrder on ei_ioid = io_id " +
  498. "where ei_ioid = '" + ioid + "' " + (eiid > 0 ? "and ei_id = " + eiid : "")
  499. + " and EI_INVOICETYPE = '1' and ei_redapplyid is not null and EI_RELATIVEREFNO is null and nvl(ei_billStatus,' ') in (' ','01','02','03','15') " +
  500. "order by ei_Detno");
  501. if (bluePiao.hasNext()) {
  502. boolean isRushRedBill = false;
  503. while (bluePiao.next()) {
  504. String redApplyId = bluePiao.getGeneralString("ei_redapplyid");
  505. eiid = bluePiao.getGeneralInt("ei_id");
  506. m.put("ei_id", eiid);
  507. QueryInvoiceRedConfirmReq qircReq = new QueryInvoiceRedConfirmReq();
  508. qircReq.setIdentity("0");
  509. qircReq.setBillId(redApplyId);
  510. String rCode = "-999";
  511. String describe = "";
  512. String logCode = "发票红字确认状态查询";
  513. try {
  514. QueryInvoiceRedConfirmResp qircResp = nuoNuoSDKService.queryInvoiceRedConfirm(qircReq, m);
  515. rCode = qircResp.getCode();
  516. describe = qircResp.getDescribe();
  517. logger.info("ElecI-queryInvoiceRedConfirmByCode:master {} ,redApplyid {} rCode {} describe {} total {}", SpObserver.getSp(), redApplyId, rCode, describe, qircResp.getResult().getTotal());
  518. if ("E0000".equals(rCode)) {//调用成功 记录发票明细 1135624457459470336
  519. List<String> sqls = new ArrayList<>();
  520. List<QueryInvoiceRedConfirmDTO> qircList = qircResp.getResult().getList();
  521. for (int i = 0; i < qircList.size(); i++) {
  522. QueryInvoiceRedConfirmDTO qircfDTO = qircList.get(i);
  523. //先获取红票状态 不一致才更新
  524. sqls.add("update elecinvoice set " +
  525. "ei_billStatus='" + qircfDTO.getBillStatus() + "' " +
  526. ",ei_billNo='" + StringUtil.nvl(qircfDTO.getBillNo(), "") + "' " + //990000008655037292
  527. ",ei_billUuid='" + StringUtil.nvl(qircfDTO.getBillUuid(), "") + "' " +
  528. ",ei_redsid='" + StringUtil.nvl(qircfDTO.getInvoiceSerialNum(), "") + "' " +
  529. ",ei_redorderno='" + StringUtil.nvl(qircfDTO.getOrderNo(), "") + "' " +
  530. ",ei_taxExcludedAmount='" + StringUtil.nvl(qircfDTO.getTaxExcludedAmount(), "0") + "' " +
  531. ",ei_taxAmount='" + StringUtil.nvl(qircfDTO.getTaxAmount(), "0") + "' " +
  532. ",ei_redReason='" + StringUtil.nvl(qircfDTO.getRedReason(), "") + "' " +
  533. ",ei_openStatus='" + StringUtil.nvl(qircfDTO.getOpenStatus(), "0") + "' " +
  534. " where ei_id = " + eiid);
  535. //针对自动开票场景:若开票,则记录红票数据,调用查询接口批量更新
  536. if ("1".equals(qircfDTO.getOpenStatus())) { //已开具红字发票标记: 1:已开具 0:未开具 //orderNo 20230731182154174492
  537. isRushRedBill = true;
  538. // serialNos 流水号 -- EI_SID -- invoiceSerialNum ; orderNos 开票单号 --- EI_ORDERNO -- ORDERNO
  539. }
  540. }
  541. baseDao.execute(sqls);
  542. describe = "";
  543. } else {
  544. if (describe.length() > 3980) {
  545. describe = describe.substring(0, 3980);
  546. }
  547. errmsg.append(describe).append("</br>");
  548. baseDao.execute("update elecinvoice set EI_EXCEPTIONMESSAGE='" + logCode + ": " + describe + "' where ei_id = " + eiid + " ");
  549. commonService.logFRecord(logCode, describe, eiid);
  550. }
  551. } catch (Exception e) {
  552. e.printStackTrace();
  553. describe = "未知异常";
  554. if (StringUtil.hasText(e.getMessage())) {
  555. describe = e.getMessage();
  556. } else if (e.getCause() != null && StringUtil.hasText(e.getCause().getMessage())) {
  557. describe = e.getCause().getMessage();
  558. }
  559. commonService.logFRecord(logCode, "发票红冲状态查询:" + describe.replaceAll("'", "''"), eiid);
  560. }
  561. if (StringUtil.hasText(describe)) {
  562. errmsg.append(describe);
  563. }
  564. }
  565. if (isRushRedBill) {
  566. Map<String, Object> elM = new HashMap<>();
  567. elM.put("appKey", m.get("appKey"));
  568. elM.put("appSecret", m.get("appSecret"));
  569. elM.put("token", m.get("token"));
  570. elM.put("taxnum", m.get("taxnum"));
  571. elM.put("io_id", ioid);
  572. elM.put("io_code", m.get("io_code"));
  573. elM.put("io_ordertype", bluePiao.getGeneralInt("io_ordertype"));
  574. Map<String, Object> r = getElecInvoices(elM);
  575. if (StringUtil.hasText(r.get("errMsg"))) {
  576. baseDao.execute("update elecinvoice set EI_EXCEPTIONMESSAGE='" + r.get("errCode") + ": " + r.get("errMsg") + "' where EI_INVOICETYPE = '1' and ei_redapplyid is not null and EI_RELATIVEREFNO is null and ei_ioid = " + ioid);
  577. }
  578. }
  579. }
  580. if (errmsg.length() > 0) {
  581. res.put("success", false);
  582. res.put("errMsg", errmsg.toString());
  583. } else {
  584. res.put("success", true);
  585. }
  586. return res;
  587. }
  588. //根据红票流水号 获取红票号及其他信息
  589. public Map<String, Object> queryRedPdfUrl(List<String> serialNosAll, Map<String, Object> m) {
  590. Map<String, Object> res = new HashMap<>();
  591. String describe = "";
  592. try {
  593. logger.info("ElecI-queryRedPdfUrl:master {} ,serialNosAll {}", SpObserver.getSp(), serialNosAll.toString());
  594. List<String> rSql = new ArrayList<String>();
  595. if (serialNosAll.size() > 0) {
  596. Stream.iterate(0, n -> n + 1)
  597. .limit((int) Math.ceil((double) serialNosAll.size() / 50)).forEach(page -> {
  598. List<String> serialNos = serialNosAll.stream().skip(page * 50).limit(50).collect(Collectors.toList());
  599. QueryInvoiceResultReq qirReq = new QueryInvoiceResultReq();
  600. qirReq.setIsOfferInvoiceDetail("0");
  601. qirReq.setSerialNos(serialNos);
  602. QueryInvoiceResultResp qirResp = nuoNuoSDKService.queryInvoiceResult(qirReq, m);
  603. List<QueryInvoiceResultDTO> list = qirResp.getResult();
  604. if (!CollectionUtil.isEmpty(list)) {
  605. for (int i = 0; i < list.size(); i++) {
  606. String sql = list.get(i).updatePdfUrlSql();
  607. if (!"".equals(sql)) {
  608. rSql.add(sql);
  609. }
  610. }
  611. }
  612. });
  613. }
  614. // System.out.println("rSql:" + rSql.toString());
  615. baseDao.execute(rSql);
  616. } catch (Exception e) {
  617. e.printStackTrace();
  618. describe = "未知异常";
  619. if (StringUtil.hasText(e.getMessage())) {
  620. describe = e.getMessage();
  621. } else if (e.getCause() != null && StringUtil.hasText(e.getCause().getMessage())) {
  622. describe = e.getCause().getMessage();
  623. }
  624. commonService.logFRecord("查询红票详情", describe.replaceAll("'", "''"), 0);
  625. }
  626. return res;
  627. }
  628. //退货单 订单开票 根据来源蓝票获取红票信息:写入退货的发票明细、更新来源蓝票对应红票信息、及退货开票记录中
  629. @Override
  630. public Map<String, Object> getRedReturnElecInvoices(Map<String, Object> m) {
  631. String taxnum = m.get("taxnum").toString();
  632. int ioid = Integer.valueOf(m.get("io_id").toString());
  633. String ioCode = m.get("io_code").toString();
  634. Map<String, Object> res = new HashMap<String, Object>();
  635. String errCode = "-999";
  636. String errMsg = "";
  637. SqlRowList ioReturn = baseDao.queryForRowSet("select io_id,io_code,io_relatesaleorderno,io_yfphm,io_biid,io_bicode from invoiceOrder " +
  638. " where IO_INVOICESTATUS = 2 and io_ordertype = 2 and io_id = " + ioid);
  639. if (!ioReturn.next()) {
  640. errMsg = "退货订单:" + ioCode + "(" + ioid + ")不存在";
  641. logger.info("RedReturnElecI-DownLoad-master: {} ,id {} status {} message {}", SpObserver.getSp(), ioid, errMsg);
  642. commonService.logFRecord("退货订单关联发票对接", errMsg, ioid);
  643. }
  644. String relatesaleorderno = ioReturn.getGeneralString("io_relatesaleorderno");//关联蓝票订单号
  645. String oldInvoiceNumber = ioReturn.getGeneralString("io_yfphm");//原蓝票发票号码
  646. Integer biid = ioReturn.getGeneralInt("io_biid");
  647. String bicode = ioReturn.getGeneralString("io_bicode");
  648. SqlRowList ioBlue = baseDao.queryForRowSet("select ei_id,ei_ioid from ElecInvoice " +
  649. "where EI_IOCODE ='" + relatesaleorderno + "' and EI_ALLELECTRONICINVOICENUMBER ='" + oldInvoiceNumber + "'");
  650. if (!ioBlue.next()) {
  651. errMsg = "原订单:" + relatesaleorderno + "不存在";
  652. logger.info("RedReturnElecI-DownLoad-master: {} ,id {} status {} message {}", SpObserver.getSp(), ioid, errMsg);
  653. commonService.logFRecord("退货订单关联发票对接", errMsg, ioid);
  654. }
  655. Integer eiBlue = ioBlue.getGeneralInt("ei_id");
  656. try {
  657. Map<String, Object> mBlue = new HashMap<>();
  658. mBlue.put("taxnum", taxnum);
  659. mBlue.put("appKey", m.get("appKey"));
  660. mBlue.put("appSecret", m.get("appSecret"));
  661. mBlue.put("token", m.get("token"));
  662. FindOrderRelationInvoiceReq foriReqBlue = new FindOrderRelationInvoiceReq();
  663. foriReqBlue.setOriginalOrderNo(relatesaleorderno);//订单编号
  664. foriReqBlue.setSaleTaxNum(taxnum);//销方税号
  665. foriReqBlue.setNeedInvoiceSourceDetail("1");//是否需要发票所来源的订单明细,1-是, 0-否(不填默认 0) 当为0时,将不回传orderDetailCallBackVOS。先默认不需要明细
  666. FindOrderRelationInvoiceResp resp = nuoNuoSDKService.findOrderRelationInvoice(foriReqBlue, mBlue);
  667. errCode = resp.getStatus();
  668. errMsg = resp.getMessage();
  669. logger.info("RedReturnElecI-DownLoad-master: {} ,id {} status {} message {}", SpObserver.getSp(), ioid, errCode, errMsg);
  670. errCode = resp.getStatus();
  671. errMsg = resp.getMessage();
  672. logger.info("RedReturnElecI-DownLoad-master: {} ,id {} status {} message {}", SpObserver.getSp(), ioid, errCode, errMsg);
  673. if ("0000".equals(errCode)) {//调用成功
  674. List<String> sqls = new ArrayList<>();
  675. List<FindOrderRelationInvoiceDTO> data = resp.getData();
  676. for (int i = 0; i < data.size(); i++) {
  677. FindOrderRelationInvoiceDTO friDto = data.get(i);
  678. if (friDto.getAllElectronicInvoiceNumber().equals(oldInvoiceNumber)) {
  679. List<RelateRedInvoiceDTO> relateRedInvoiceList = friDto.getRelateRedInvoiceList();
  680. for (int j = 0; j < relateRedInvoiceList.size(); j++) {
  681. RelateRedInvoiceDTO relateRedInvoiceDTO = relateRedInvoiceList.get(j);
  682. if (StringUtil.hasText(relateRedInvoiceDTO.getAllElectronicInvoiceNumber())) {
  683. sqls.add("delete ElecInvoice where ei_ioid = " + ioid);
  684. sqls.add("insert into ElecInvoice (EI_ID, EI_DETNO, EI_IOID, EI_IOCODE, EI_BIID, EI_BICODE " +
  685. " , EI_SID, EI_ORDERNO, EI_CREATETIME, EI_INVOICETYPE, EI_INVOICELINE, EI_SPECIFICFACTOR, EI_INVOICESTATE, EI_EXCEPTIONMESSAGE, EI_INVOICECODE, EI_INVOICENUMBER, EI_ALLELECTRONICINVOICENUMBER, EI_OLDINVOICECODE, EI_OLDINVOICENUMBER, EI_OLDALLELECTRONICINVOICENU" +
  686. " , EI_INVOICETIME, EI_INVALIDTIME" +
  687. " , EI_PDFURL, EI_PDFATTACH, EI_OFDURL, EI_IMAGEURL, EI_CALCEXCLUDINGTAXAMOUNT, EI_CALCTAXAMOUNT, EI_CALEORDERAMOUNT, EI_SELLERTAXNO, EI_SELLERNAME, EI_TAXNO, EI_BUYERNAME, EI_REMARK, EI_CLERKER, EI_PAYEE, EI_CHECKER, EI_BFIELD1, EI_BFIELD2, EI_BFIELD3, EI_INDATE, EI_UPDATEDATE, EI_REFNO" +
  688. " ,EI_BILLSTATUS,ei_rushredstate,ei_redsid,ei_relativerefno,ei_taxamount,ei_taxexcludedamount) " +
  689. "SELECT ElecInvoice_Seq.nextval,1," + ioid + ",'" + ioCode + "'," + biid + ",'" + bicode + "' " +
  690. " , EI_SID, EI_ORDERNO, EI_CREATETIME, EI_INVOICETYPE, EI_INVOICELINE, EI_SPECIFICFACTOR, EI_INVOICESTATE, EI_EXCEPTIONMESSAGE, EI_INVOICECODE, EI_INVOICENUMBER, EI_ALLELECTRONICINVOICENUMBER, EI_OLDINVOICECODE, EI_OLDINVOICENUMBER, EI_OLDALLELECTRONICINVOICENU" +
  691. " , (to_date('1970-01-0100:00:00','yyyy-mm-dd hh24:mi:ss') + "+relateRedInvoiceDTO.getInvoiceTime()+"/1000/24/60/60) EI_INVOICETIME, EI_INVALIDTIME" +
  692. " , EI_PDFURL, EI_PDFATTACH, EI_OFDURL, EI_IMAGEURL, EI_CALCEXCLUDINGTAXAMOUNT, EI_CALCTAXAMOUNT, EI_CALEORDERAMOUNT, EI_SELLERTAXNO, EI_SELLERNAME, EI_TAXNO, EI_BUYERNAME, EI_REMARK, EI_CLERKER, EI_PAYEE, EI_CHECKER, EI_BFIELD1, EI_BFIELD2, EI_BFIELD3, EI_INDATE, EI_UPDATEDATE, EI_REFNO" +
  693. " ,'01' EI_BILLSTATUS,"+friDto.getRushRedState()+" ei_rushredstate,'"+relateRedInvoiceDTO.getSid()+"' ei_redsid,'"+relateRedInvoiceDTO.getAllElectronicInvoiceNumber()+"' ei_relativerefno,"+relateRedInvoiceDTO.getTaxAmount()+" ei_taxamount,"+relateRedInvoiceDTO.getExcludingTaxAmount()+" ei_taxexcludedamount " +
  694. "FROM ElecInvoice b WHERE EI_ID = " + eiBlue);
  695. sqls.add("update ElecInvoice set EI_BILLSTATUS='01'" +
  696. ",ei_rushredstate=" + friDto.getRushRedState() + "" +
  697. ",ei_redsid='" + relateRedInvoiceDTO.getSid() + "' " +
  698. ",ei_relativerefno='" + relateRedInvoiceDTO.getAllElectronicInvoiceNumber() + "' " +
  699. ",ei_taxamount=" + relateRedInvoiceDTO.getTaxAmount() +
  700. ",ei_taxexcludedamount=" + relateRedInvoiceDTO.getExcludingTaxAmount() +
  701. ",ei_redpdfurl=null,ei_redpdfattach=null " +
  702. " where EI_ID = '" + eiBlue + "'");
  703. sqls.add("update InvoiceOrder set io_refno='" + relateRedInvoiceDTO.getAllElectronicInvoiceNumber() + "' where io_id=" + ioid);
  704. sqls.add("update billout set bi_refno='" + relateRedInvoiceDTO.getAllElectronicInvoiceNumber() + "' where bi_code='" + bicode + "'");
  705. }
  706. }
  707. }
  708. }
  709. //记录发票明细
  710. // System.out.println(sqls.toString());
  711. baseDao.execute(sqls);
  712. //红字
  713. commonService.logSRecord("退货订单关联发票对接", ioid);
  714. } else {// {"status":"8958","message":"该订单对应的开票单不存在","data":null} 没有开票单的不记录对接失败日志
  715. if (errMsg.length() > 3850) {
  716. errMsg = errMsg.substring(0, 3850);
  717. }
  718. baseDao.execute("update invoiceOrder set io_errorMsg='订单关联发票查询失败: " + errMsg + "' where io_id=" + ioid);
  719. commonService.logFRecord("退货订单关联发票对接", "蓝发票信息获取:(" + errCode + ")" + errMsg, ioid);
  720. }
  721. } catch (Exception e) {
  722. e.printStackTrace();
  723. errMsg = "发票获取异常";
  724. if (StringUtil.hasText(e.getMessage())) {
  725. errMsg = e.getMessage();
  726. } else if (e.getCause() != null && StringUtil.hasText(e.getCause().getMessage())) {
  727. errMsg = e.getCause().getMessage();
  728. }
  729. logger.info("RedReturnElecI-DownLoad-master: {} ,id {} message {}", SpObserver.getSp(), ioid, errMsg);
  730. commonService.logFRecord("退货订单关联发票对接", errMsg.replaceAll("'", "''"), ioid);
  731. }
  732. //获取红字发票PDF地址
  733. List<String> serialNosAll = baseDao.queryForList("select ei_redsid from ElecInvoice " +
  734. "where ei_id = " + eiBlue + " and EI_RELATIVEREFNO is not null and ei_redpdfurl is null and nvl(EI_BILLSTATUS,' ') in (' ','01','04')", String.class);
  735. if (!CollectionUtil.isEmpty(serialNosAll)) {
  736. queryRedPdfUrl(serialNosAll, m);
  737. }
  738. if (StringUtil.hasText(errMsg) && !"调用成功".equals(errMsg)) {
  739. res.put("success", false);
  740. res.put("errCode", errCode);
  741. res.put("errMsg", errMsg);
  742. } else {
  743. res.put("success", true);
  744. }
  745. return res;
  746. }
  747. }