| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- 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<Object[]> 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<String,Object> 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<Object[]> 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<Integer> 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<Object[]> 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<String,Object> 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','15') " +
- // " 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<Object[]> 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<String,Object> 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<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);
- 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<Object[]> 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<Integer> 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");
- }
- }
- }
|