ElecInvoiceTask.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package com.uas.eis.task;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.TypeReference;
  5. import com.alibaba.fastjson.serializer.SerializerFeature;
  6. import com.uas.eis.config.DonlimConfig;
  7. import com.uas.eis.config.PNSConfig;
  8. import com.uas.eis.core.config.SpObserver;
  9. import com.uas.eis.core.support.TokenProperties;
  10. import com.uas.eis.dao.BaseDao;
  11. import com.uas.eis.dao.FindOrderStateResp;
  12. import com.uas.eis.dao.SqlRowList;
  13. import com.uas.eis.dto.FindOrderRelationInvoiceDTO;
  14. import com.uas.eis.dto.FindOrderRelationInvoiceReq;
  15. import com.uas.eis.dto.FindOrderRelationInvoiceResp;
  16. import com.uas.eis.entity.PNSSendData;
  17. import com.uas.eis.entity.PNSSendResp;
  18. import com.uas.eis.service.CommonService;
  19. import com.uas.eis.service.ERPService;
  20. import com.uas.eis.utils.*;
  21. import net.sf.json.JSONObject;
  22. import nuonuo.open.sdk.NNOpenSDK;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.dao.EmptyResultDataAccessException;
  28. import org.springframework.http.HttpStatus;
  29. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  30. import org.springframework.scheduling.annotation.Scheduled;
  31. import org.springframework.stereotype.Component;
  32. import java.io.FileOutputStream;
  33. import java.io.InputStream;
  34. import java.io.OutputStream;
  35. import java.net.URL;
  36. import java.net.URLConnection;
  37. import java.util.*;
  38. @Component
  39. public class ElecInvoiceTask {
  40. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  41. @Autowired
  42. private BaseDao baseDao;
  43. @Autowired
  44. private DonlimConfig donlimConfig;
  45. @Autowired
  46. private ERPService erpService;
  47. @Autowired
  48. private CommonService commonService;
  49. /**
  50. * 每半小时执行一次
  51. * //0:待处理 表示订单没有转了开票申请,此状态可以不用管
  52. * //1:处理中 表示订单可能转了开票申请,但是在走审批流等,此状态可以先不用管
  53. * //2:已处理 表示订单全部都转了开票申请
  54. * //4:提交失败 表示订单自动转失败了,此状态可以不用管
  55. * /*6:部分待处理 表示订单部分转了开票申请
  56. * 1.有开票记录申请,但是没开票
  57. * {"status":"8958","message":"该订单对应的开票单不存在","data":null}
  58. * 2.有开票记录申请,存在部分失败或者成功
  59. * 只返回开票失败或者成功的部分
  60. * {"status":"0000","message":"调用成功","data":[{"sellerTaxNo":"339901999999199","orderNo":"1134068619401396224","rushRedState":"0","invoiceType":1,"invoiceState":1,"invoiceLine":"b","exceptionMessage":"ukey版签章无下载码","sid":"23072713481601034933"}]}
  61. * //7:无需开票,此状态可以不用管
  62. * */
  63. @Scheduled(cron = "0 0/2 * * * ?")
  64. public void elecInvoiceDownLoad(){
  65. List<Object[]> obsList=baseDao.getFieldsDatasByCondition("YITOA_DATACENTER.dock_invoice_config",new String[]{"appKey","appSecret","token","taxnum","username"},"1=1");
  66. if(!CollectionUtil.isEmpty(obsList)){
  67. logger.info("ElecI-DownLoad Begin");
  68. for (Object[] obs: obsList) {
  69. if(obs == null){
  70. continue;
  71. }
  72. String master = obs[4].toString();
  73. String appKey = donlimConfig.getAppKey();
  74. String appSecret = donlimConfig.getAppSecret();
  75. String token = donlimConfig.getToken();
  76. String taxnum= donlimConfig.getTaxnum();
  77. if(donlimConfig.isProd()){
  78. System.out.println("prod");
  79. appKey = obs[0].toString();
  80. appSecret = obs[1].toString();
  81. token = obs[2].toString();// 访问令牌
  82. taxnum = obs[3].toString();// 授权企业税号
  83. }else{
  84. System.out.println("dev");
  85. }
  86. Map<String,Object> m = new HashMap<>();
  87. m.put("appKey",appKey);
  88. m.put("appSecret",appSecret);
  89. m.put("token",token);
  90. m.put("taxnum",taxnum);
  91. logger.info("ElecI-DownLoad: master: {} Begin ",master);
  92. SpObserver.putSp(master);
  93. SqlRowList ioList = baseDao.queryForRowSet("select io_id,io_code from(select io_id,io_code from "+master+".invoiceOrder " +
  94. " where io_status = '已审核' " +
  95. " and nvl(IO_DOCKSTATUS,' ') = '对接成功' " +
  96. " and (IO_INVOICESTATUS = 6 or (IO_INVOICESTATUS = 2 and io_refno is null)) " +
  97. //有红票的也要获取
  98. "order by io_id desc) where rownum <= 100");
  99. logger.info("ElecI-DownLoad: master: {} size: {} ",master,ioList.getResultList().size());
  100. while (ioList.next()){
  101. m.put("io_id",ioList.getGeneralInt("io_id"));
  102. m.put("io_code",ioList.getGeneralString("io_code"));
  103. erpService.getElecInvoices(m);
  104. }
  105. logger.info("ElecI-DownLoad: master: {} End ",master);
  106. }
  107. logger.info("ElecI-DownLoad End");
  108. }
  109. }
  110. @Scheduled(cron = "0 0/2 * * * ?")
  111. //PDF文件转存 一次转存
  112. public void elecInvoiceAttachSave(){
  113. List<Object[]> obsList=baseDao.getFieldsDatasByCondition("YITOA_DATACENTER.dock_invoice_config",new String[]{"appKey","appSecret","token","taxnum","username"},"1=1");
  114. if(!CollectionUtil.isEmpty(obsList)){
  115. logger.info("ElecI-AttachSave Begin");
  116. for (Object[] obs: obsList) {
  117. if(obs == null){
  118. continue;
  119. }
  120. String master = obs[4].toString();
  121. logger.info("ElecI-AttachSave: master: {} Begin ",master);
  122. SpObserver.putSp(master);
  123. SqlRowList ioList = baseDao.queryForRowSet("select * from (select ei_id,ei_pdfurl,ei_refno,ei_ioid from ELECINVOICE " +
  124. "where (ei_pdfurl is not null and ei_pdfattach is null) " +
  125. " order by ei_id ) where rownum <= 100 ");//一次最多500
  126. logger.info("ElecI-AttachSave: master: {} size: {} ",master,ioList.getResultList().size());
  127. List<Integer> ioids = new ArrayList<>();
  128. while (ioList.next()){
  129. //下载地址
  130. String fileUrl = ioList.getGeneralString("ei_pdfurl"); // "https://inv.jss.com.cn/fp2/2zpnwyKkdvu9dWzE8BhmSnER0Iux3xQRMLidDJhiIE2PziVXY4lL3k83OsK0K06YctW5cf1FFHxraffj-bf16A.pdf";
  131. // 定义要保存的文件的路径
  132. String fileName = ioList.getGeneralString("ei_refno")+".pdf";//发票号码
  133. String fileBasePath = "C:\\invoice\\"+fileName;//发票号码
  134. fileBasePath = "/usr/local/uas/invoice/"+fileName;//发票号码
  135. try {
  136. // 创建URL对象
  137. URL url = new URL(fileUrl);
  138. // 打开连接
  139. URLConnection conn = url.openConnection();
  140. // 获取输入流
  141. InputStream in = conn.getInputStream();
  142. // 创建输出流
  143. FileOutputStream out = new FileOutputStream(fileBasePath);
  144. // 创建缓冲区
  145. byte[] buffer = new byte[104857600];
  146. // 读取并写入数据
  147. int len;
  148. while ((len = in.read(buffer)) != -1) {
  149. out.write(buffer, 0, len);
  150. }
  151. long size = out.getChannel().size();
  152. // 关闭流
  153. in.close();
  154. out.close();
  155. // System.out.println("size:"+size);
  156. //存文件 filepath
  157. int id = commonService.saveFilePath(fileBasePath, (int) size, fileName, "管理员");
  158. baseDao.execute("update ELECINVOICE set ei_pdfattach ='"+id+";' where ei_id = "+ioList.getGeneralInt("ei_id"));
  159. if(!ioids.contains(ioList.getGeneralInt("ei_ioid"))){
  160. ioids.add(ioList.getGeneralInt("ei_ioid"));
  161. }
  162. System.out.println("下载成功");
  163. } catch (Exception e) {
  164. e.printStackTrace();
  165. String errMsg = "文件转存异常";
  166. if(StringUtil.hasText(e.getMessage())){
  167. errMsg = e.getMessage();
  168. }else if(e.getCause()!=null&&StringUtil.hasText(e.getCause().getMessage())){
  169. errMsg = e.getCause().getMessage();
  170. }
  171. commonService.logFRecord("订单关联发票对接",errMsg.replaceAll("'","''"),ioList.getGeneralInt("ei_id"));
  172. }
  173. }
  174. if(ioids.size()>0){
  175. baseDao.execute("update invoiceOrder set io_attach = REPLACE(to_char((select wm_concat(ei_pdfattach) from ELECINVOICE where io_id = ei_ioid and nvl(ei_rushredstate,0) <> 1)),',','') where io_id in ("+StringUtils.join(ioids.toArray(), ",")+")");
  176. }
  177. logger.info("ElecI-AttachSave: master: {} End ",master);
  178. }
  179. logger.info("ElecI-AttachSave End");
  180. }
  181. }
  182. @Scheduled(cron = "0 0/2 * * * ?")
  183. //发票红字确认状态查询
  184. public void elecInvoiceRedConfirmStateUpdate(){
  185. List<Object[]> obsList=baseDao.getFieldsDatasByCondition("YITOA_DATACENTER.dock_invoice_config",new String[]{"appKey","appSecret","token","taxnum","username"},"1=1");
  186. if(!CollectionUtil.isEmpty(obsList)){
  187. logger.info("ElecI-RedConfirmStateUpdate Begin");
  188. for (Object[] obs: obsList) {
  189. if(obs == null){
  190. continue;
  191. }
  192. String master = obs[4].toString();
  193. String appKey = donlimConfig.getAppKey();
  194. String appSecret = donlimConfig.getAppSecret();
  195. String token = donlimConfig.getToken();
  196. String taxnum= donlimConfig.getTaxnum();
  197. if(donlimConfig.isProd()){
  198. System.out.println("prod");
  199. appKey = obs[0].toString();
  200. appSecret = obs[1].toString();
  201. token = obs[2].toString();// 访问令牌
  202. taxnum = obs[3].toString();// 授权企业税号
  203. }else{
  204. System.out.println("dev");
  205. }
  206. Map<String,Object> m = new HashMap<>();
  207. m.put("appKey",appKey);
  208. m.put("appSecret",appSecret);
  209. m.put("token",token);
  210. m.put("taxnum",taxnum);
  211. logger.info("ElecI-RedConfirmStateUpdate: master: {} Begin ",master);
  212. SpObserver.putSp(master);
  213. SqlRowList ioList = baseDao.queryForRowSet("select * from ( " +
  214. "select io_id,io_Code " +
  215. "from Invoiceorder where exists (Select 1 from ElecInvoice " +
  216. //有红字确认单 但是没有获取到发票号的
  217. " where ei_ioid = io_id and nvl(EI_INVOICETYPE,'1') = '1' and ei_redapplyid is not null and EI_RELATIVEREFNO is null and nvl(EI_BILLSTATUS,' ') in (' ','02','03') " +
  218. // " and io_code = 'SZ23070006' " +
  219. ")) where rownum <= 100");
  220. logger.info("ElecI-DownLoad: master: {} size: {} ",master,ioList.getResultList().size());
  221. while (ioList.next()){
  222. m.put("io_id",ioList.getGeneralInt("io_id"));
  223. m.put("io_code",ioList.getGeneralString("io_code"));
  224. m.put("ei_id",0);
  225. erpService.queryInvoiceRedConfirm(m);
  226. }
  227. logger.info("ElecI-RedConfirmStateUpdate: master: {} End ",master);
  228. }
  229. logger.info("ElecI-RedConfirmStateUpdate End");
  230. }
  231. }
  232. @Scheduled(cron = "0 0/2 * * * ?")
  233. public void redPdfUrlUpdate(){
  234. List<Object[]> obsList=baseDao.getFieldsDatasByCondition("YITOA_DATACENTER.dock_invoice_config",new String[]{"appKey","appSecret","token","taxnum","username"},"1=1");
  235. if(!CollectionUtil.isEmpty(obsList)){
  236. logger.info("ElecI-redPdfUrlUpdate Begin");
  237. for (Object[] obs: obsList) {
  238. if(obs == null){
  239. continue;
  240. }
  241. String master = obs[4].toString();
  242. String appKey = donlimConfig.getAppKey();
  243. String appSecret = donlimConfig.getAppSecret();
  244. String token = donlimConfig.getToken();
  245. String taxnum= donlimConfig.getTaxnum();
  246. if(donlimConfig.isProd()){
  247. System.out.println("prod");
  248. appKey = obs[0].toString();
  249. appSecret = obs[1].toString();
  250. token = obs[2].toString();// 访问令牌
  251. taxnum = obs[3].toString();// 授权企业税号
  252. }else{
  253. System.out.println("dev");
  254. }
  255. Map<String,Object> m = new HashMap<>();
  256. m.put("appKey",appKey);
  257. m.put("appSecret",appSecret);
  258. m.put("token",token);
  259. m.put("taxnum",taxnum);
  260. logger.info("ElecI-redPdfUrlUpdate: master: {} Begin ",master);
  261. SpObserver.putSp(master);
  262. List<String> serialNosAll = baseDao.queryForList("select ei_redsid from ElecInvoice where EI_RELATIVEREFNO is not null and ei_redpdfurl is null and nvl(EI_BILLSTATUS,' ') in ('01','04')",String.class);
  263. if (serialNosAll == null){
  264. logger.info("ElecI-redPdfUrlUpdate: master: {} size: {} ",master,0);
  265. }else {
  266. logger.info("ElecI-redPdfUrlUpdate: master: {} size: {} ",master,serialNosAll.size());
  267. erpService.queryRedPdfUrl(serialNosAll,m);
  268. }
  269. }
  270. logger.info("ElecI-redPdfUrlUpdate End");
  271. }
  272. }
  273. @Scheduled(cron = "0 0/2 * * * ?")
  274. //红字PDF文件转存 一次转存
  275. public void elecInvoiceRedAttachSave(){
  276. List<Object[]> obsList=baseDao.getFieldsDatasByCondition("YITOA_DATACENTER.dock_invoice_config",new String[]{"appKey","appSecret","token","taxnum","username"},"1=1");
  277. if(!CollectionUtil.isEmpty(obsList)){
  278. logger.info("ElecI-AttachSave Begin");
  279. for (Object[] obs: obsList) {
  280. if(obs == null){
  281. continue;
  282. }
  283. String master = obs[4].toString();
  284. logger.info("ElecI-AttachSave: master: {} Begin ",master);
  285. SpObserver.putSp(master);
  286. SqlRowList ioList = baseDao.queryForRowSet("select * from (select ei_id,ei_redpdfurl,ei_relativerefno,ei_ioid from ELECINVOICE " +
  287. "where ei_redpdfurl is not null and ei_redpdfattach is null " +
  288. " order by ei_id ) where rownum <= 100 ");//一次最多500
  289. logger.info("ElecI-AttachSave: master: {} size: {} ",master,ioList.getResultList().size());
  290. List<Integer> ioids = new ArrayList<>();
  291. while (ioList.next()){
  292. //下载地址
  293. String fileUrl = ioList.getGeneralString("ei_redpdfurl"); // "https://inv.jss.com.cn/fp2/2zpnwyKkdvu9dWzE8BhmSnER0Iux3xQRMLidDJhiIE2PziVXY4lL3k83OsK0K06YctW5cf1FFHxraffj-bf16A.pdf";
  294. // 定义要保存的文件的路径
  295. String fileName = ioList.getGeneralString("ei_relativerefno")+".pdf";//发票号码
  296. String fileBasePath = "C:\\invoice\\"+fileName;//发票号码
  297. fileBasePath = "/usr/local/uas/invoice/"+fileName;//发票号码
  298. try {
  299. // 创建URL对象
  300. URL url = new URL(fileUrl);
  301. // 打开连接
  302. URLConnection conn = url.openConnection();
  303. // 获取输入流
  304. InputStream in = conn.getInputStream();
  305. // 创建输出流
  306. FileOutputStream out = new FileOutputStream(fileBasePath);
  307. // 创建缓冲区
  308. byte[] buffer = new byte[104857600];
  309. // 读取并写入数据
  310. int len;
  311. while ((len = in.read(buffer)) != -1) {
  312. out.write(buffer, 0, len);
  313. }
  314. long size = out.getChannel().size();
  315. // 关闭流
  316. in.close();
  317. out.close();
  318. // System.out.println("size:"+size);
  319. //存文件 filepath
  320. int id = commonService.saveFilePath(fileBasePath, (int) size, fileName, "管理员");
  321. baseDao.execute("update ELECINVOICE set ei_redpdfattach ='"+id+";' where ei_id = "+ioList.getGeneralInt("ei_id"));
  322. System.out.println("下载成功");
  323. } catch (Exception e) {
  324. e.printStackTrace();
  325. String errMsg = "文件转存异常";
  326. if(StringUtil.hasText(e.getMessage())){
  327. errMsg = e.getMessage();
  328. }else if(e.getCause()!=null&&StringUtil.hasText(e.getCause().getMessage())){
  329. errMsg = e.getCause().getMessage();
  330. }
  331. commonService.logFRecord("订单关联发票对接",errMsg.replaceAll("'","''"),ioList.getGeneralInt("ei_id"));
  332. }
  333. }
  334. logger.info("ElecI-AttachSave: master: {} End ",master);
  335. }
  336. logger.info("ElecI-AttachSave End");
  337. }
  338. }
  339. }