ERPServiceImpl.java 47 KB

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