发票下载改为下载压缩包
This commit is contained in:
parent
6b4e8083a6
commit
bc58cc89b4
|
@ -4,14 +4,12 @@ import java.io.InputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.HashMap;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.TreeMap;
|
|
||||||
import javax.crypto.Mac;
|
import javax.crypto.Mac;
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import javax.xml.bind.DatatypeConverter;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Base64;
|
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
@ -20,6 +18,9 @@ import com.google.gson.Gson;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import nc.bs.framework.common.NCLocator;
|
||||||
|
import nc.itf.uap.IUAPQueryBS;
|
||||||
|
import nc.jdbc.framework.processor.MapProcessor;
|
||||||
import org.apache.http.client.config.CookieSpecs;
|
import org.apache.http.client.config.CookieSpecs;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
@ -44,6 +45,9 @@ import nccloud.framework.web.action.itf.ICommonAction;
|
||||||
import nccloud.framework.web.container.IRequest;
|
import nccloud.framework.web.container.IRequest;
|
||||||
import nccloud.itf.sscivm.ivsale.service.IVSaleQueryService;
|
import nccloud.itf.sscivm.ivsale.service.IVSaleQueryService;
|
||||||
import nccloud.pubitf.platform.attachment.IAttachmentService;
|
import nccloud.pubitf.platform.attachment.IAttachmentService;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
public class InvoiceDownloadAction implements ICommonAction {
|
public class InvoiceDownloadAction implements ICommonAction {
|
||||||
|
|
||||||
|
@ -51,133 +55,131 @@ public class InvoiceDownloadAction implements ICommonAction {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object doAction(IRequest request) {
|
public Object doAction(IRequest request) {
|
||||||
|
|
||||||
Map<String, String[]> params_1 = request.readParameters();
|
Map<String, String[]> params_1 = request.readParameters();
|
||||||
|
String[] pks = params_1.get("pk"); // 获取所有 pk
|
||||||
String[] pk = params_1.get("pk");
|
String[] arrInvoiceTypes = params_1.get("arrInvoiceTypes");
|
||||||
String[] invoiceTypes = params_1.get("invoiceType");
|
|
||||||
String isdoc = "z";
|
String isdoc = "z";
|
||||||
WebFile file = null;
|
WebFile file = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
List<IVMInvoiceHeadVO> headVOList =ServiceLocator.find(IVSaleQueryService.class).queryIVMInvoiceHeadVOsByPks(new String[] {pk[0]});
|
// 创建内存中的 ZIP 输出流
|
||||||
|
ByteArrayOutputStream zipOut = new ByteArrayOutputStream();
|
||||||
|
ZipOutputStream zipStream = new ZipOutputStream(zipOut);
|
||||||
|
String zipName = "";
|
||||||
|
// 遍历所有 pk
|
||||||
|
for (String pk : pks) {
|
||||||
|
List<IVMInvoiceHeadVO> headVOList = ServiceLocator.find(IVSaleQueryService.class).queryIVMInvoiceHeadVOsByPks(new String[]{pk});
|
||||||
IVMInvoiceHeadVO headVO = headVOList.get(0);
|
IVMInvoiceHeadVO headVO = headVOList.get(0);
|
||||||
String invoiceType = invoiceTypes[0];
|
|
||||||
|
|
||||||
|
// 获取旗舰版的token
|
||||||
// 获取旗舰版的token
|
|
||||||
String appKey = "8c9eb1ea1ba54b3f9683a8b355f8e615";
|
String appKey = "8c9eb1ea1ba54b3f9683a8b355f8e615";
|
||||||
String appSecret = "999e15c2a13ee4eab480534be4bf52fdf9032a6d";
|
String appSecret = "999e15c2a13ee4eab480534be4bf52fdf9032a6d";
|
||||||
String tokenUrl = "https://c2.yonyoucloud.com/iuap-api-auth/open-auth/selfAppAuth/getAccessToken";
|
String tokenUrl = "https://c2.yonyoucloud.com/iuap-api-auth/open-auth/selfAppAuth/getAccessToken";
|
||||||
String getbsfileUrl = "https://c2.yonyoucloud.com/iuap-api-gateway/yonbip/tax/output-tax/api/einvoice/getbsfile?access_token=";
|
String getbsfileUrl = "https://c2.yonyoucloud.com/iuap-api-gateway/yonbip/tax/output-tax/api/einvoice/getbsfile?access_token=";
|
||||||
|
|
||||||
Map<String, String> params = new HashMap<>();
|
Map<String, String> params = new HashMap<>();
|
||||||
|
|
||||||
// 除签名外的其他参数
|
|
||||||
params.put("appKey", appKey);
|
params.put("appKey", appKey);
|
||||||
String timestamp = String.valueOf(System.currentTimeMillis());
|
String timestamp = String.valueOf(System.currentTimeMillis());
|
||||||
params.put("timestamp", timestamp);
|
params.put("timestamp", timestamp);
|
||||||
// 计算签名
|
|
||||||
Map<String, String> treeMap;
|
// 计算签名
|
||||||
if (params instanceof TreeMap) {
|
Map<String, String> treeMap = new TreeMap<>(params);
|
||||||
treeMap = params;
|
|
||||||
} else {
|
|
||||||
treeMap = new TreeMap<>(params);
|
|
||||||
}
|
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
for (Map.Entry<String, String> entry : treeMap.entrySet()) {
|
for (Map.Entry<String, String> entry : treeMap.entrySet()) {
|
||||||
stringBuilder.append(entry.getKey()).append(entry.getValue());
|
stringBuilder.append(entry.getKey()).append(entry.getValue());
|
||||||
}
|
}
|
||||||
Mac mac = Mac.getInstance("HmacSHA256");
|
Mac mac = Mac.getInstance("HmacSHA256");
|
||||||
|
|
||||||
mac.init(new SecretKeySpec(appSecret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
mac.init(new SecretKeySpec(appSecret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
||||||
byte[] signData = mac.doFinal(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
|
byte[] signData = mac.doFinal(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
|
||||||
String base64String = Base64.getEncoder().encodeToString(signData);
|
String base64String = Base64.getEncoder().encodeToString(signData);
|
||||||
String signature = URLEncoder.encode(base64String, "UTF-8");
|
String signature = URLEncoder.encode(base64String, "UTF-8");
|
||||||
params.put("signature", signature);
|
params.put("signature", signature);
|
||||||
// String responseString = doGet(
|
|
||||||
// "https://c1.yonyoucloud.com/iuap-api-auth/open-auth/selfAppAuth/getAccessToken", params);
|
|
||||||
String responseString = doGet(tokenUrl, params);
|
String responseString = doGet(tokenUrl, params);
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
Map result = gson.fromJson(responseString, Map.class);
|
Map result = gson.fromJson(responseString, Map.class);
|
||||||
|
|
||||||
if (StringUtils.equals("00000", result.get("code").toString())) {
|
if (StringUtils.equals("00000", result.get("code").toString())) {
|
||||||
|
|
||||||
Map<String, Object> tokenInfo = (Map<String, Object>) result.get("data");
|
Map<String, Object> tokenInfo = (Map<String, Object>) result.get("data");
|
||||||
String access_token = (String) tokenInfo.get("access_token");
|
String access_token = (String) tokenInfo.get("access_token");
|
||||||
// 税务服务-调用销项发票管理-已开票-版式文件查询接口
|
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
// 发票类型 1:增值税电子普通发票; 2:增值税电子专用发票; 3:增值税普通发票; 4:增值税专用发票 、增值税专用发票(机动车); 5:机动车销售统一发票; 8:增值税电子普通发票(成品油); 10:成品油普通发票; 11:成品油专用发票; 15:二手车销售统一发票; 31:数电专用发票; 32:数电普通发票; 33:数电纸质发票(增值税专用发票); 34:数电纸质发票(普通发票);
|
|
||||||
map.put("fplx", headVO.getInvoice_type());
|
map.put("fplx", headVO.getInvoice_type());
|
||||||
// 数电发票号码
|
|
||||||
map.put("slfphm", headVO.getFphm());
|
map.put("slfphm", headVO.getFphm());
|
||||||
// 用友自画pdf
|
|
||||||
map.put("yypdf", "0");
|
map.put("yypdf", "0");
|
||||||
// 税局pdf
|
map.put("taxpdf", Arrays.asList(arrInvoiceTypes).contains("1") ? "1" : "0");
|
||||||
map.put("taxpdf", "1".equals(invoiceType) ? "1" : "0");
|
map.put("taxxml", Arrays.asList(arrInvoiceTypes).contains("2") ? "1" : "0");
|
||||||
// 税局xml
|
map.put("taxofd", Arrays.asList(arrInvoiceTypes).contains("3") ? "1" : "0");
|
||||||
map.put("taxxml", "2".equals(invoiceType) ? "1" : "0");
|
|
||||||
// 税局ofd
|
|
||||||
map.put("taxofd", "3".equals(invoiceType) ? "1" : "0");
|
|
||||||
// String resString = doPost(
|
|
||||||
// "https://c1.yonyoucloud.com/iuap-api-gateway/yonbip/tax/output-tax/api/einvoice/getbsfile?access_token=" + access_token,
|
|
||||||
// map);
|
|
||||||
|
|
||||||
String resString = doPost(getbsfileUrl + access_token, map);
|
String resString = doPost(getbsfileUrl + access_token, map);
|
||||||
gson = new Gson();
|
gson = new Gson();
|
||||||
|
|
||||||
Map resultMap = gson.fromJson(resString, Map.class);
|
Map resultMap = gson.fromJson(resString, Map.class);
|
||||||
|
|
||||||
if (StringUtils.equals("200", resultMap.get("code").toString())) {
|
if (StringUtils.equals("200", resultMap.get("code").toString())) {
|
||||||
Map<String, Object> infoMap = (Map<String, Object>) resultMap.get("data");
|
Map<String, Object> infoMap = (Map<String, Object>) resultMap.get("data");
|
||||||
String tax = "1".equals(invoiceType) ? (String) infoMap.get("taxpdf") : "2".equals(invoiceType) ? (String) infoMap.get("taxxml") : "3".equals(invoiceType) ? (String) infoMap.get("taxofd") : "";
|
String pdfData = (String) infoMap.get("taxpdf");
|
||||||
if (MMValueCheck.isEmpty(tax)) {
|
String xmlData = (String) infoMap.get("taxxml");
|
||||||
throw new Exception("未查询到发票信息");
|
String ofdData = (String) infoMap.get("taxofd");
|
||||||
|
|
||||||
|
if (MMValueCheck.isEmpty(pdfData) && MMValueCheck.isEmpty(xmlData) && MMValueCheck.isEmpty(ofdData)) {
|
||||||
|
throw new Exception("未查询到发票信息");
|
||||||
}
|
}
|
||||||
byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(tax);
|
|
||||||
InputStream ins = new ByteArrayInputStream(imageBytes);
|
|
||||||
String Fphm = headVO.getFphm();
|
|
||||||
|
|
||||||
//发票号码+单位名称+合同号(开票申请字段)+金额(发票上的价税合计)
|
|
||||||
String filename = "1".equals(invoiceType) ? ".pdf" : "2".equals(invoiceType) ? ".xml" : "3".equals(invoiceType) ? ".ofd" : "";
|
|
||||||
|
|
||||||
|
|
||||||
String ctcode = "";
|
String ctcode = "";
|
||||||
HYSuperDMO dmo = new HYSuperDMO();
|
HYSuperDMO dmo = new HYSuperDMO();
|
||||||
IVApplogVO[] ivApplogVO=(IVApplogVO[]) dmo.queryByWhereClause(IVApplogVO.class, "fphm='"+Fphm+"' and dr=0 ");
|
IVApplogVO[] ivApplogVO = (IVApplogVO[]) dmo.queryByWhereClause(IVApplogVO.class, "fphm='" + headVO.getFphm() + "' and dr=0 ");
|
||||||
if (ivApplogVO != null && ivApplogVO.length > 0) {
|
if (ivApplogVO != null && ivApplogVO.length > 0) {
|
||||||
ivApplogVO[0].getLyid();//开票申请单主键
|
|
||||||
IVApplicationHeadVO ivApplicationHeadVO = (IVApplicationHeadVO) dmo.queryByPrimaryKey(IVApplicationHeadVO.class, ivApplogVO[0].getLyid());
|
IVApplicationHeadVO ivApplicationHeadVO = (IVApplicationHeadVO) dmo.queryByPrimaryKey(IVApplicationHeadVO.class, ivApplogVO[0].getLyid());
|
||||||
ctcode=ivApplicationHeadVO.getDef2();//合同号
|
// ctcode = ivApplicationHeadVO.getDef2(); // 合同号
|
||||||
|
String operationSql = "SELECT bd.NAME name from bd_defdoc bd LEFT join bd_defdoclist bdl ON bd.pk_defdoclist=bdl.pk_defdoclist WHERE bdl.code ='zdy-001'\n" +
|
||||||
|
"AND bd.pk_defdoc = '" + ivApplicationHeadVO.getDef2().toString() + "' ";
|
||||||
|
// List<Map<String,Object>> operationresult = (List<Map<String,Object>>) getQueryService().executeQuery(operationSql, new ArrayListProcessor());
|
||||||
|
Map<String, Object> valList = (Map<String, Object>) getQueryService().executeQuery(operationSql, new MapProcessor());
|
||||||
|
ctcode = (String) valList.get("name");
|
||||||
}
|
}
|
||||||
//发票号码+单位名称+合同号(开票申请字段)+金额(发票上的价税合计) 合同号_单位名称_发票号码_金额
|
|
||||||
|
|
||||||
// 合同号_单位名称_发票号码_金额
|
// 添加 PDF
|
||||||
|
if (!MMValueCheck.isEmpty(pdfData)) {
|
||||||
|
byte[] pdfBytes = DatatypeConverter.parseBase64Binary(pdfData);
|
||||||
|
zipStream.putNextEntry(new ZipEntry(ctcode + "_" + headVO.getGmfmc() + "_" + headVO.getFphm() + "_" + String.valueOf(headVO.getJshj().toDouble()) + ".pdf"));
|
||||||
|
zipStream.write(pdfBytes);
|
||||||
|
zipStream.closeEntry();
|
||||||
|
}
|
||||||
|
|
||||||
String name = ctcode+"_"+headVO.getGmfmc()+"_"+Fphm+"_"+String.valueOf(headVO.getJshj().toDouble()) + filename;
|
// 添加 XML
|
||||||
|
if (!MMValueCheck.isEmpty(xmlData)) {
|
||||||
|
byte[] xmlBytes = DatatypeConverter.parseBase64Binary(xmlData);
|
||||||
|
zipStream.putNextEntry(new ZipEntry(ctcode + "_" + headVO.getGmfmc() + "_" + headVO.getFphm() + "_" + String.valueOf(headVO.getJshj().toDouble()) + ".xml"));
|
||||||
|
zipStream.write(xmlBytes);
|
||||||
|
zipStream.closeEntry();
|
||||||
|
}
|
||||||
|
|
||||||
// String name = Fphm+"_"+headVO.getGmfmc()+"_"+ctcode+"_"+String.valueOf(headVO.getJshj().toDouble()) + filename;
|
// 添加 OFD
|
||||||
file = new WebFile(name, ins);
|
if (!MMValueCheck.isEmpty(ofdData)) {
|
||||||
|
byte[] ofdBytes = DatatypeConverter.parseBase64Binary(ofdData);
|
||||||
|
zipStream.putNextEntry(new ZipEntry(ctcode + "_" + headVO.getGmfmc() + "_" + headVO.getFphm() + "_" + String.valueOf(headVO.getJshj().toDouble()) + ".ofd"));
|
||||||
|
zipStream.write(ofdBytes);
|
||||||
|
zipStream.closeEntry();
|
||||||
|
}
|
||||||
|
zipName=ctcode + "_" + headVO.getGmfmc() + "_" + headVO.getFphm() + "_" + String.valueOf(headVO.getJshj().toDouble());
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("获取发票信息失败");
|
throw new Exception("获取发票信息失败");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Exception("获取access_token失败");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}else {
|
zipStream.finish();
|
||||||
throw new Exception("获取access_token失败");
|
zipStream.close();
|
||||||
|
if(pks.length > 1){
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
|
||||||
|
zipName = "发票下载_" + sdf.format(new Date());
|
||||||
}
|
}
|
||||||
// String url = "";
|
// 构造 WebFile 返回 ZIP 文件
|
||||||
// IVMInvoiceHeadVO headVO = headVOList.get(0);
|
InputStream ins = new ByteArrayInputStream(zipOut.toByteArray());
|
||||||
//
|
file = new WebFile(zipName+".zip", ins);
|
||||||
// if (headVOList != null && headVOList.size() > 0) {
|
|
||||||
// url = headVO.getViewurl();
|
|
||||||
// }
|
|
||||||
// InputStream ins = this.ncservice.download(isdoc, url, "sscivm");
|
|
||||||
// String Fpdm = headVO.getFpdm();
|
|
||||||
// String Fphm = headVO.getFphm();
|
|
||||||
// String name = headVO.getFilename();
|
|
||||||
// String filename = ".pdf";
|
|
||||||
// if(name!=null && name.endsWith(".ofd")){
|
|
||||||
// filename = ".ofd";
|
|
||||||
// }
|
|
||||||
// file = new WebFile(Fpdm + "_" + Fphm + filename, ins);
|
|
||||||
} catch (BusinessException e) {
|
} catch (BusinessException e) {
|
||||||
ExceptionUtils.wrappException(e);
|
ExceptionUtils.wrappException(e);
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
@ -205,9 +207,9 @@ public class InvoiceDownloadAction implements ICommonAction {
|
||||||
cm.setMaxTotal(500);
|
cm.setMaxTotal(500);
|
||||||
cm.setDefaultMaxPerRoute(50);
|
cm.setDefaultMaxPerRoute(50);
|
||||||
|
|
||||||
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 连接池获取连接超时
|
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 连接池获取连接超时
|
||||||
.setConnectTimeout(5000) // 连接建立超时
|
.setConnectTimeout(5000) // 连接建立超时
|
||||||
.setSocketTimeout(20000) // 等待响应超时
|
.setSocketTimeout(20000) // 等待响应超时
|
||||||
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
|
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
|
||||||
|
|
||||||
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
|
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
|
||||||
|
@ -226,9 +228,9 @@ public class InvoiceDownloadAction implements ICommonAction {
|
||||||
cm.setMaxTotal(500);
|
cm.setMaxTotal(500);
|
||||||
cm.setDefaultMaxPerRoute(50);
|
cm.setDefaultMaxPerRoute(50);
|
||||||
|
|
||||||
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 连接池获取连接超时
|
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 连接池获取连接超时
|
||||||
.setConnectTimeout(5000) // 连接建立超时
|
.setConnectTimeout(5000) // 连接建立超时
|
||||||
.setSocketTimeout(20000) // 等待响应超时
|
.setSocketTimeout(20000) // 等待响应超时
|
||||||
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
|
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
|
||||||
|
|
||||||
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
|
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
|
||||||
|
@ -249,5 +251,8 @@ public class InvoiceDownloadAction implements ICommonAction {
|
||||||
get.releaseConnection();
|
get.releaseConnection();
|
||||||
return responseString;
|
return responseString;
|
||||||
}
|
}
|
||||||
|
public IUAPQueryBS getQueryService() {
|
||||||
|
return NCLocator.getInstance().lookup(IUAPQueryBS.class);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue