ElecInvoiceTask.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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,cu_name " +
  124. " from ELECINVOICE LEFT JOIN InvoiceOrder ON io_id = ei_ioid LEFT JOIN CUSTOMER ON CU_CODE = io_cucode " +
  125. "where (ei_pdfurl is not null and ei_pdfattach is null) " +
  126. " order by ei_id ) where rownum <= 100 ");//一次最多500
  127. logger.info("ElecI-AttachSave: master: {} size: {} ",master,ioList.getResultList().size());
  128. List<Integer> ioids = new ArrayList<>();
  129. while (ioList.next()){
  130. //下载地址
  131. String fileUrl = ioList.getGeneralString("ei_pdfurl"); // "https://inv.jss.com.cn/fp2/2zpnwyKkdvu9dWzE8BhmSnER0Iux3xQRMLidDJhiIE2PziVXY4lL3k83OsK0K06YctW5cf1FFHxraffj-bf16A.pdf";
  132. // 定义要保存的文件的路径
  133. String fileName = ioList.getGeneralString("cu_name")+ioList.getGeneralString("ei_refno")+".pdf";//发票号码
  134. String fileBasePath = "C:\\invoice\\"+fileName;//发票号码
  135. fileBasePath = "/usr/local/uas/invoice/"+fileName;//发票号码
  136. try {
  137. // 创建URL对象
  138. URL url = new URL(fileUrl);
  139. // 打开连接
  140. URLConnection conn = url.openConnection();
  141. // 获取输入流
  142. InputStream in = conn.getInputStream();
  143. // 创建输出流
  144. FileOutputStream out = new FileOutputStream(fileBasePath);
  145. // 创建缓冲区
  146. byte[] buffer = new byte[104857600];
  147. // 读取并写入数据
  148. int len;
  149. while ((len = in.read(buffer)) != -1) {
  150. out.write(buffer, 0, len);
  151. }
  152. long size = out.getChannel().size();
  153. // 关闭流
  154. in.close();
  155. out.close();
  156. // System.out.println("size:"+size);
  157. //存文件 filepath
  158. int id = commonService.saveFilePath(fileBasePath, (int) size, fileName, "管理员");
  159. baseDao.execute("update ELECINVOICE set ei_pdfattach ='"+id+";' where ei_id = "+ioList.getGeneralInt("ei_id"));
  160. if(!ioids.contains(ioList.getGeneralInt("ei_ioid"))){
  161. ioids.add(ioList.getGeneralInt("ei_ioid"));
  162. }
  163. System.out.println("下载成功");
  164. } catch (Exception e) {
  165. e.printStackTrace();
  166. String errMsg = "文件转存异常";
  167. if(StringUtil.hasText(e.getMessage())){
  168. errMsg = e.getMessage();
  169. }else if(e.getCause()!=null&&StringUtil.hasText(e.getCause().getMessage())){
  170. errMsg = e.getCause().getMessage();
  171. }
  172. commonService.logFRecord("订单关联发票对接",errMsg.replaceAll("'","''"),ioList.getGeneralInt("ei_id"));
  173. }
  174. }
  175. if(ioids.size()>0){
  176. 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(), ",")+")");
  177. }
  178. logger.info("ElecI-AttachSave: master: {} End ",master);
  179. }
  180. logger.info("ElecI-AttachSave End");
  181. }
  182. }
  183. @Scheduled(cron = "0 0/2 * * * ?")
  184. //发票红字确认状态查询
  185. public void elecInvoiceRedConfirmStateUpdate(){
  186. List<Object[]> obsList=baseDao.getFieldsDatasByCondition("YITOA_DATACENTER.dock_invoice_config",new String[]{"appKey","appSecret","token","taxnum","username"},"1=1");
  187. if(!CollectionUtil.isEmpty(obsList)){
  188. logger.info("ElecI-RedConfirmStateUpdate Begin");
  189. for (Object[] obs: obsList) {
  190. if(obs == null){
  191. continue;
  192. }
  193. String master = obs[4].toString();
  194. String appKey = donlimConfig.getAppKey();
  195. String appSecret = donlimConfig.getAppSecret();
  196. String token = donlimConfig.getToken();
  197. String taxnum= donlimConfig.getTaxnum();
  198. if(donlimConfig.isProd()){
  199. System.out.println("prod");
  200. appKey = obs[0].toString();
  201. appSecret = obs[1].toString();
  202. token = obs[2].toString();// 访问令牌
  203. taxnum = obs[3].toString();// 授权企业税号
  204. }else{
  205. System.out.println("dev");
  206. }
  207. Map<String,Object> m = new HashMap<>();
  208. m.put("appKey",appKey);
  209. m.put("appSecret",appSecret);
  210. m.put("token",token);
  211. m.put("taxnum",taxnum);
  212. logger.info("ElecI-RedConfirmStateUpdate: master: {} Begin ",master);
  213. SpObserver.putSp(master);
  214. SqlRowList ioList = baseDao.queryForRowSet("select * from ( " +
  215. "select io_id,io_Code " +
  216. "from Invoiceorder where exists (Select 1 from ElecInvoice " +
  217. //有红字确认单 但是没有获取到发票号的
  218. " where ei_ioid = io_id and EI_EXCEPTIONMESSAGE is null and nvl(EI_INVOICETYPE,'1') = '1' and ei_redapplyid is not null and EI_RELATIVEREFNO is null and nvl(EI_BILLSTATUS,' ') in (' ','01','02','03','15') " +
  219. // " and io_code = 'SZ23070033' " +
  220. ")) where rownum <= 100");
  221. logger.info("ElecI-DownLoad: master: {} size: {} ",master,ioList.getResultList().size());
  222. while (ioList.next()){
  223. m.put("io_id",ioList.getGeneralInt("io_id"));
  224. m.put("io_code",ioList.getGeneralString("io_code"));
  225. m.put("ei_id",0);
  226. erpService.queryInvoiceRedConfirm(m);
  227. }
  228. logger.info("ElecI-RedConfirmStateUpdate: master: {} End ",master);
  229. }
  230. logger.info("ElecI-RedConfirmStateUpdate End");
  231. }
  232. }
  233. @Scheduled(cron = "0 0/2 * * * ?")
  234. public void redPdfUrlUpdate(){
  235. List<Object[]> obsList=baseDao.getFieldsDatasByCondition("YITOA_DATACENTER.dock_invoice_config",new String[]{"appKey","appSecret","token","taxnum","username"},"1=1");
  236. if(!CollectionUtil.isEmpty(obsList)){
  237. logger.info("ElecI-redPdfUrlUpdate Begin");
  238. for (Object[] obs: obsList) {
  239. if(obs == null){
  240. continue;
  241. }
  242. String master = obs[4].toString();
  243. String appKey = donlimConfig.getAppKey();
  244. String appSecret = donlimConfig.getAppSecret();
  245. String token = donlimConfig.getToken();
  246. String taxnum= donlimConfig.getTaxnum();
  247. if(donlimConfig.isProd()){
  248. System.out.println("prod");
  249. appKey = obs[0].toString();
  250. appSecret = obs[1].toString();
  251. token = obs[2].toString();// 访问令牌
  252. taxnum = obs[3].toString();// 授权企业税号
  253. }else{
  254. System.out.println("dev");
  255. }
  256. Map<String,Object> m = new HashMap<>();
  257. m.put("appKey",appKey);
  258. m.put("appSecret",appSecret);
  259. m.put("token",token);
  260. m.put("taxnum",taxnum);
  261. logger.info("ElecI-redPdfUrlUpdate: master: {} Begin ",master);
  262. SpObserver.putSp(master);
  263. 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);
  264. if (serialNosAll == null){
  265. logger.info("ElecI-redPdfUrlUpdate: master: {} size: {} ",master,0);
  266. }else {
  267. logger.info("ElecI-redPdfUrlUpdate: master: {} size: {} ",master,serialNosAll.size());
  268. erpService.queryRedPdfUrl(serialNosAll,m);
  269. }
  270. }
  271. logger.info("ElecI-redPdfUrlUpdate End");
  272. }
  273. }
  274. @Scheduled(cron = "0 0/2 * * * ?")
  275. //红字PDF文件转存 一次转存
  276. public void elecInvoiceRedAttachSave(){
  277. List<Object[]> obsList=baseDao.getFieldsDatasByCondition("YITOA_DATACENTER.dock_invoice_config",new String[]{"appKey","appSecret","token","taxnum","username"},"1=1");
  278. if(!CollectionUtil.isEmpty(obsList)){
  279. logger.info("ElecI-AttachSave Begin");
  280. for (Object[] obs: obsList) {
  281. if(obs == null){
  282. continue;
  283. }
  284. String master = obs[4].toString();
  285. logger.info("ElecI-AttachSave: master: {} Begin ",master);
  286. SpObserver.putSp(master);
  287. SqlRowList ioList = baseDao.queryForRowSet("select * from (select ei_id,ei_redpdfurl,ei_relativerefno,ei_ioid,cu_name" +
  288. " from ELECINVOICE LEFT JOIN InvoiceOrder ON io_id = ei_ioid LEFT JOIN CUSTOMER ON CU_CODE = io_cucode " +
  289. "where ei_redpdfurl is not null and ei_redpdfattach is null " +
  290. " order by ei_id ) where rownum <= 100 ");//一次最多500
  291. logger.info("ElecI-AttachSave: master: {} size: {} ",master,ioList.getResultList().size());
  292. List<Integer> ioids = new ArrayList<>();
  293. while (ioList.next()){
  294. //下载地址
  295. String fileUrl = ioList.getGeneralString("ei_redpdfurl"); // "https://inv.jss.com.cn/fp2/2zpnwyKkdvu9dWzE8BhmSnER0Iux3xQRMLidDJhiIE2PziVXY4lL3k83OsK0K06YctW5cf1FFHxraffj-bf16A.pdf";
  296. // 定义要保存的文件的路径
  297. String fileName = ioList.getGeneralString("cu_name")+ioList.getGeneralString("ei_relativerefno")+".pdf";//发票号码
  298. String fileBasePath = "C:\\invoice\\"+fileName;//发票号码
  299. fileBasePath = "/usr/local/uas/invoice/"+fileName;//发票号码
  300. try {
  301. // 创建URL对象
  302. URL url = new URL(fileUrl);
  303. // 打开连接
  304. URLConnection conn = url.openConnection();
  305. // 获取输入流
  306. InputStream in = conn.getInputStream();
  307. // 创建输出流
  308. FileOutputStream out = new FileOutputStream(fileBasePath);
  309. // 创建缓冲区
  310. byte[] buffer = new byte[104857600];
  311. // 读取并写入数据
  312. int len;
  313. while ((len = in.read(buffer)) != -1) {
  314. out.write(buffer, 0, len);
  315. }
  316. long size = out.getChannel().size();
  317. // 关闭流
  318. in.close();
  319. out.close();
  320. // System.out.println("size:"+size);
  321. //存文件 filepath
  322. int id = commonService.saveFilePath(fileBasePath, (int) size, fileName, "管理员");
  323. baseDao.execute("update ELECINVOICE set ei_redpdfattach ='"+id+";' where ei_id = "+ioList.getGeneralInt("ei_id"));
  324. System.out.println("下载成功");
  325. } catch (Exception e) {
  326. e.printStackTrace();
  327. String errMsg = "文件转存异常";
  328. if(StringUtil.hasText(e.getMessage())){
  329. errMsg = e.getMessage();
  330. }else if(e.getCause()!=null&&StringUtil.hasText(e.getCause().getMessage())){
  331. errMsg = e.getCause().getMessage();
  332. }
  333. commonService.logFRecord("订单关联发票对接",errMsg.replaceAll("'","''"),ioList.getGeneralInt("ei_id"));
  334. }
  335. }
  336. logger.info("ElecI-AttachSave: master: {} End ",master);
  337. }
  338. logger.info("ElecI-AttachSave End");
  339. }
  340. }
  341. }