package com.uas.eis.task; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.serializer.SerializerFeature; import com.uas.eis.config.DonlimConfig; import com.uas.eis.config.PNSConfig; import com.uas.eis.core.config.SpObserver; import com.uas.eis.core.support.TokenProperties; import com.uas.eis.dao.BaseDao; import com.uas.eis.dao.FindOrderStateResp; import com.uas.eis.dao.SqlRowList; import com.uas.eis.dto.FindOrderRelationInvoiceDTO; import com.uas.eis.dto.FindOrderRelationInvoiceReq; import com.uas.eis.dto.FindOrderRelationInvoiceResp; import com.uas.eis.entity.PNSSendData; import com.uas.eis.entity.PNSSendResp; import com.uas.eis.service.CommonService; import com.uas.eis.service.ERPService; import com.uas.eis.utils.*; import net.sf.json.JSONObject; import nuonuo.open.sdk.NNOpenSDK; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.http.HttpStatus; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import java.util.*; @Component public class ElecInvoiceTask { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private BaseDao baseDao; @Autowired private DonlimConfig donlimConfig; @Autowired private ERPService erpService; @Autowired private CommonService commonService; /** * 每半小时执行一次 * //0:待处理 表示订单没有转了开票申请,此状态可以不用管 * //1:处理中 表示订单可能转了开票申请,但是在走审批流等,此状态可以先不用管 * //2:已处理 表示订单全部都转了开票申请 * //4:提交失败 表示订单自动转失败了,此状态可以不用管 * /*6:部分待处理 表示订单部分转了开票申请 * 1.有开票记录申请,但是没开票 * {"status":"8958","message":"该订单对应的开票单不存在","data":null} * 2.有开票记录申请,存在部分失败或者成功 * 只返回开票失败或者成功的部分 * {"status":"0000","message":"调用成功","data":[{"sellerTaxNo":"339901999999199","orderNo":"1134068619401396224","rushRedState":"0","invoiceType":1,"invoiceState":1,"invoiceLine":"b","exceptionMessage":"ukey版签章无下载码","sid":"23072713481601034933"}]} * //7:无需开票,此状态可以不用管 * */ @Scheduled(cron = "0 0/2 * * * ?") public void elecInvoiceDownLoad(){ List obsList=baseDao.getFieldsDatasByCondition("YITOA_DATACENTER.dock_invoice_config",new String[]{"appKey","appSecret","token","taxnum","username"},"1=1"); if(!CollectionUtil.isEmpty(obsList)){ logger.info("ElecI-DownLoad Begin"); for (Object[] obs: obsList) { if(obs == null){ continue; } String master = obs[4].toString(); String appKey = donlimConfig.getAppKey(); String appSecret = donlimConfig.getAppSecret(); String token = donlimConfig.getToken(); String taxnum= donlimConfig.getTaxnum(); if(donlimConfig.isProd()){ System.out.println("prod"); appKey = obs[0].toString(); appSecret = obs[1].toString(); token = obs[2].toString();// 访问令牌 taxnum = obs[3].toString();// 授权企业税号 }else{ System.out.println("dev"); } Map m = new HashMap<>(); m.put("appKey",appKey); m.put("appSecret",appSecret); m.put("token",token); m.put("taxnum",taxnum); logger.info("ElecI-DownLoad: master: {} Begin ",master); SpObserver.putSp(master); SqlRowList ioList = baseDao.queryForRowSet("select io_id,io_code from(select io_id,io_code from "+master+".invoiceOrder " + " where io_status = '已审核' " + " and nvl(IO_DOCKSTATUS,' ') = '对接成功' " + " and (IO_INVOICESTATUS = 6 or (IO_INVOICESTATUS = 2 and io_refno is null)) " + //有红票的也要获取 "order by io_id desc) where rownum <= 100"); logger.info("ElecI-DownLoad: master: {} size: {} ",master,ioList.getResultList().size()); while (ioList.next()){ m.put("io_id",ioList.getGeneralInt("io_id")); m.put("io_code",ioList.getGeneralString("io_code")); erpService.getElecInvoices(m); } logger.info("ElecI-DownLoad: master: {} End ",master); } logger.info("ElecI-DownLoad End"); } } @Scheduled(cron = "0 0/2 * * * ?") //PDF文件转存 一次转存 public void elecInvoiceAttachSave(){ List obsList=baseDao.getFieldsDatasByCondition("YITOA_DATACENTER.dock_invoice_config",new String[]{"appKey","appSecret","token","taxnum","username"},"1=1"); if(!CollectionUtil.isEmpty(obsList)){ logger.info("ElecI-AttachSave Begin"); for (Object[] obs: obsList) { if(obs == null){ continue; } String master = obs[4].toString(); logger.info("ElecI-AttachSave: master: {} Begin ",master); SpObserver.putSp(master); SqlRowList ioList = baseDao.queryForRowSet("select * from (select ei_id,ei_pdfurl,ei_refno,ei_ioid,cu_name " + " from ELECINVOICE LEFT JOIN InvoiceOrder ON io_id = ei_ioid LEFT JOIN CUSTOMER ON CU_CODE = io_cucode " + "where (ei_pdfurl is not null and ei_pdfattach is null) " + " order by ei_id ) where rownum <= 100 ");//一次最多500 logger.info("ElecI-AttachSave: master: {} size: {} ",master,ioList.getResultList().size()); List ioids = new ArrayList<>(); while (ioList.next()){ //下载地址 String fileUrl = ioList.getGeneralString("ei_pdfurl"); // "https://inv.jss.com.cn/fp2/2zpnwyKkdvu9dWzE8BhmSnER0Iux3xQRMLidDJhiIE2PziVXY4lL3k83OsK0K06YctW5cf1FFHxraffj-bf16A.pdf"; // 定义要保存的文件的路径 String fileName = ioList.getGeneralString("cu_name")+ioList.getGeneralString("ei_refno")+".pdf";//发票号码 String fileBasePath = "C:\\invoice\\"+fileName;//发票号码 fileBasePath = "/usr/local/uas/invoice/"+fileName;//发票号码 try { // 创建URL对象 URL url = new URL(fileUrl); // 打开连接 URLConnection conn = url.openConnection(); // 获取输入流 InputStream in = conn.getInputStream(); // 创建输出流 FileOutputStream out = new FileOutputStream(fileBasePath); // 创建缓冲区 byte[] buffer = new byte[104857600]; // 读取并写入数据 int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } long size = out.getChannel().size(); // 关闭流 in.close(); out.close(); // System.out.println("size:"+size); //存文件 filepath int id = commonService.saveFilePath(fileBasePath, (int) size, fileName, "管理员"); baseDao.execute("update ELECINVOICE set ei_pdfattach ='"+id+";' where ei_id = "+ioList.getGeneralInt("ei_id")); if(!ioids.contains(ioList.getGeneralInt("ei_ioid"))){ ioids.add(ioList.getGeneralInt("ei_ioid")); } System.out.println("下载成功"); } catch (Exception e) { e.printStackTrace(); String errMsg = "文件转存异常"; if(StringUtil.hasText(e.getMessage())){ errMsg = e.getMessage(); }else if(e.getCause()!=null&&StringUtil.hasText(e.getCause().getMessage())){ errMsg = e.getCause().getMessage(); } commonService.logFRecord("订单关联发票对接",errMsg.replaceAll("'","''"),ioList.getGeneralInt("ei_id")); } } if(ioids.size()>0){ 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(), ",")+")"); } logger.info("ElecI-AttachSave: master: {} End ",master); } logger.info("ElecI-AttachSave End"); } } @Scheduled(cron = "0 0/2 * * * ?") //发票红字确认状态查询 public void elecInvoiceRedConfirmStateUpdate(){ List obsList=baseDao.getFieldsDatasByCondition("YITOA_DATACENTER.dock_invoice_config",new String[]{"appKey","appSecret","token","taxnum","username"},"1=1"); if(!CollectionUtil.isEmpty(obsList)){ logger.info("ElecI-RedConfirmStateUpdate Begin"); for (Object[] obs: obsList) { if(obs == null){ continue; } String master = obs[4].toString(); String appKey = donlimConfig.getAppKey(); String appSecret = donlimConfig.getAppSecret(); String token = donlimConfig.getToken(); String taxnum= donlimConfig.getTaxnum(); if(donlimConfig.isProd()){ System.out.println("prod"); appKey = obs[0].toString(); appSecret = obs[1].toString(); token = obs[2].toString();// 访问令牌 taxnum = obs[3].toString();// 授权企业税号 }else{ System.out.println("dev"); } Map m = new HashMap<>(); m.put("appKey",appKey); m.put("appSecret",appSecret); m.put("token",token); m.put("taxnum",taxnum); logger.info("ElecI-RedConfirmStateUpdate: master: {} Begin ",master); SpObserver.putSp(master); SqlRowList ioList = baseDao.queryForRowSet("select * from ( " + "select io_id,io_Code " + "from Invoiceorder where exists (Select 1 from ElecInvoice " + //有红字确认单 但是没有获取到发票号的 " 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') " + // " and io_code = 'SZ23070033' " + ")) where rownum <= 100"); logger.info("ElecI-DownLoad: master: {} size: {} ",master,ioList.getResultList().size()); while (ioList.next()){ m.put("io_id",ioList.getGeneralInt("io_id")); m.put("io_code",ioList.getGeneralString("io_code")); m.put("ei_id",0); erpService.queryInvoiceRedConfirm(m); } logger.info("ElecI-RedConfirmStateUpdate: master: {} End ",master); } logger.info("ElecI-RedConfirmStateUpdate End"); } } @Scheduled(cron = "0 0/2 * * * ?") public void redPdfUrlUpdate(){ List obsList=baseDao.getFieldsDatasByCondition("YITOA_DATACENTER.dock_invoice_config",new String[]{"appKey","appSecret","token","taxnum","username"},"1=1"); if(!CollectionUtil.isEmpty(obsList)){ logger.info("ElecI-redPdfUrlUpdate Begin"); for (Object[] obs: obsList) { if(obs == null){ continue; } String master = obs[4].toString(); String appKey = donlimConfig.getAppKey(); String appSecret = donlimConfig.getAppSecret(); String token = donlimConfig.getToken(); String taxnum= donlimConfig.getTaxnum(); if(donlimConfig.isProd()){ System.out.println("prod"); appKey = obs[0].toString(); appSecret = obs[1].toString(); token = obs[2].toString();// 访问令牌 taxnum = obs[3].toString();// 授权企业税号 }else{ System.out.println("dev"); } Map m = new HashMap<>(); m.put("appKey",appKey); m.put("appSecret",appSecret); m.put("token",token); m.put("taxnum",taxnum); logger.info("ElecI-redPdfUrlUpdate: master: {} Begin ",master); SpObserver.putSp(master); List 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); if (serialNosAll == null){ logger.info("ElecI-redPdfUrlUpdate: master: {} size: {} ",master,0); }else { logger.info("ElecI-redPdfUrlUpdate: master: {} size: {} ",master,serialNosAll.size()); erpService.queryRedPdfUrl(serialNosAll,m); } } logger.info("ElecI-redPdfUrlUpdate End"); } } @Scheduled(cron = "0 0/2 * * * ?") //红字PDF文件转存 一次转存 public void elecInvoiceRedAttachSave(){ List obsList=baseDao.getFieldsDatasByCondition("YITOA_DATACENTER.dock_invoice_config",new String[]{"appKey","appSecret","token","taxnum","username"},"1=1"); if(!CollectionUtil.isEmpty(obsList)){ logger.info("ElecI-AttachSave Begin"); for (Object[] obs: obsList) { if(obs == null){ continue; } String master = obs[4].toString(); logger.info("ElecI-AttachSave: master: {} Begin ",master); SpObserver.putSp(master); SqlRowList ioList = baseDao.queryForRowSet("select * from (select ei_id,ei_redpdfurl,ei_relativerefno,ei_ioid,cu_name" + " from ELECINVOICE LEFT JOIN InvoiceOrder ON io_id = ei_ioid LEFT JOIN CUSTOMER ON CU_CODE = io_cucode " + "where ei_redpdfurl is not null and ei_redpdfattach is null " + " order by ei_id ) where rownum <= 100 ");//一次最多500 logger.info("ElecI-AttachSave: master: {} size: {} ",master,ioList.getResultList().size()); List ioids = new ArrayList<>(); while (ioList.next()){ //下载地址 String fileUrl = ioList.getGeneralString("ei_redpdfurl"); // "https://inv.jss.com.cn/fp2/2zpnwyKkdvu9dWzE8BhmSnER0Iux3xQRMLidDJhiIE2PziVXY4lL3k83OsK0K06YctW5cf1FFHxraffj-bf16A.pdf"; // 定义要保存的文件的路径 String fileName = ioList.getGeneralString("cu_name")+ioList.getGeneralString("ei_relativerefno")+".pdf";//发票号码 String fileBasePath = "C:\\invoice\\"+fileName;//发票号码 fileBasePath = "/usr/local/uas/invoice/"+fileName;//发票号码 try { // 创建URL对象 URL url = new URL(fileUrl); // 打开连接 URLConnection conn = url.openConnection(); // 获取输入流 InputStream in = conn.getInputStream(); // 创建输出流 FileOutputStream out = new FileOutputStream(fileBasePath); // 创建缓冲区 byte[] buffer = new byte[104857600]; // 读取并写入数据 int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } long size = out.getChannel().size(); // 关闭流 in.close(); out.close(); // System.out.println("size:"+size); //存文件 filepath int id = commonService.saveFilePath(fileBasePath, (int) size, fileName, "管理员"); baseDao.execute("update ELECINVOICE set ei_redpdfattach ='"+id+";' where ei_id = "+ioList.getGeneralInt("ei_id")); System.out.println("下载成功"); } catch (Exception e) { e.printStackTrace(); String errMsg = "文件转存异常"; if(StringUtil.hasText(e.getMessage())){ errMsg = e.getMessage(); }else if(e.getCause()!=null&&StringUtil.hasText(e.getCause().getMessage())){ errMsg = e.getCause().getMessage(); } commonService.logFRecord("订单关联发票对接",errMsg.replaceAll("'","''"),ioList.getGeneralInt("ei_id")); } } logger.info("ElecI-AttachSave: master: {} End ",master); } logger.info("ElecI-AttachSave End"); } } }