发票下载改为下载压缩包

This commit is contained in:
lihao 2025-05-23 10:32:45 +08:00
parent 6b4e8083a6
commit bc58cc89b4
1 changed files with 200 additions and 195 deletions

View File

@ -4,14 +4,12 @@ import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.net.URLEncoder;
import com.alibaba.fastjson.JSONObject;
@ -20,6 +18,9 @@ import com.google.gson.Gson;
import java.io.ByteArrayInputStream;
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.RequestConfig;
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.itf.sscivm.ivsale.service.IVSaleQueryService;
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 {
@ -51,133 +55,131 @@ public class InvoiceDownloadAction implements ICommonAction {
@Override
public Object doAction(IRequest request) {
Map<String, String[]> params_1 = request.readParameters();
String[] pk = params_1.get("pk");
String[] invoiceTypes = params_1.get("invoiceType");
String[] pks = params_1.get("pk"); // 获取所有 pk
String[] arrInvoiceTypes = params_1.get("arrInvoiceTypes");
String isdoc = "z";
WebFile file = null;
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);
String invoiceType = invoiceTypes[0];
// 获取旗舰版的token
// 获取旗舰版的token
String appKey = "8c9eb1ea1ba54b3f9683a8b355f8e615";
String appSecret = "999e15c2a13ee4eab480534be4bf52fdf9032a6d";
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 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=";
Map<String, String> params = new HashMap<>();
// 除签名外的其他参数
params.put("appKey", appKey);
String timestamp = String.valueOf(System.currentTimeMillis());
params.put("timestamp", timestamp);
// 计算签名
Map<String, String> treeMap;
if (params instanceof TreeMap) {
treeMap = params;
} else {
treeMap = new TreeMap<>(params);
}
// 计算签名
Map<String, String> treeMap = new TreeMap<>(params);
StringBuilder stringBuilder = new StringBuilder();
for (Map.Entry<String, String> entry : treeMap.entrySet()) {
stringBuilder.append(entry.getKey()).append(entry.getValue());
}
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(appSecret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] signData = mac.doFinal(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
String base64String = Base64.getEncoder().encodeToString(signData);
String signature = URLEncoder.encode(base64String, "UTF-8");
params.put("signature", signature);
// String responseString = doGet(
// "https://c1.yonyoucloud.com/iuap-api-auth/open-auth/selfAppAuth/getAccessToken", params);
String responseString = doGet(tokenUrl, params);
Gson gson = new Gson();
Map result = gson.fromJson(responseString, Map.class);
if (StringUtils.equals("00000", result.get("code").toString())) {
Map<String, Object> tokenInfo = (Map<String, Object>) result.get("data");
String access_token = (String) tokenInfo.get("access_token");
// 税务服务-调用销项发票管理-已开票-版式文件查询接口
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("slfphm", headVO.getFphm());
// 用友自画pdf
map.put("yypdf", "0");
// 税局pdf
map.put("taxpdf", "1".equals(invoiceType) ? "1" : "0");
// 税局xml
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);
map.put("taxpdf", Arrays.asList(arrInvoiceTypes).contains("1") ? "1" : "0");
map.put("taxxml", Arrays.asList(arrInvoiceTypes).contains("2") ? "1" : "0");
map.put("taxofd", Arrays.asList(arrInvoiceTypes).contains("3") ? "1" : "0");
String resString = doPost(getbsfileUrl + access_token,map);
String resString = doPost(getbsfileUrl + access_token, map);
gson = new Gson();
Map resultMap = gson.fromJson(resString, Map.class);
if (StringUtils.equals("200", resultMap.get("code").toString())) {
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") : "";
if (MMValueCheck.isEmpty(tax)) {
throw new Exception("未查询到发票信息");
String pdfData = (String) infoMap.get("taxpdf");
String xmlData = (String) infoMap.get("taxxml");
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();
IVApplogVO[] ivApplogVO=(IVApplogVO[]) dmo.queryByWhereClause(IVApplogVO.class, "fphm='"+Fphm+"' and dr=0 ");
if(ivApplogVO!=null&&ivApplogVO.length>0) {
ivApplogVO[0].getLyid();//开票申请单主键
IVApplicationHeadVO ivApplicationHeadVO =(IVApplicationHeadVO) dmo.queryByPrimaryKey(IVApplicationHeadVO.class, ivApplogVO[0].getLyid());
ctcode=ivApplicationHeadVO.getDef2();//合同号
IVApplogVO[] ivApplogVO = (IVApplogVO[]) dmo.queryByWhereClause(IVApplogVO.class, "fphm='" + headVO.getFphm() + "' and dr=0 ");
if (ivApplogVO != null && ivApplogVO.length > 0) {
IVApplicationHeadVO ivApplicationHeadVO = (IVApplicationHeadVO) dmo.queryByPrimaryKey(IVApplicationHeadVO.class, ivApplogVO[0].getLyid());
// 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;
file = new WebFile(name, ins);
// 添加 OFD
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 {
throw new Exception("获取发票信息失败");
throw new Exception("获取发票信息失败");
}
} else {
throw new Exception("获取access_token失败");
}
}
}else {
throw new Exception("获取access_token失败");
zipStream.finish();
zipStream.close();
if(pks.length > 1){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
zipName = "发票下载_" + sdf.format(new Date());
}
// String url = "";
// IVMInvoiceHeadVO headVO = headVOList.get(0);
//
// 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);
// 构造 WebFile 返回 ZIP 文件
InputStream ins = new ByteArrayInputStream(zipOut.toByteArray());
file = new WebFile(zipName+".zip", ins);
} catch (BusinessException e) {
ExceptionUtils.wrappException(e);
} catch (NoSuchAlgorithmException e) {
@ -205,9 +207,9 @@ public class InvoiceDownloadAction implements ICommonAction {
cm.setMaxTotal(500);
cm.setDefaultMaxPerRoute(50);
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 连接池获取连接超时
.setConnectTimeout(5000) // 连接建立超时
.setSocketTimeout(20000) // 等待响应超时
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 连接池获取连接超时
.setConnectTimeout(5000) // 连接建立超时
.setSocketTimeout(20000) // 等待响应超时
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
@ -226,9 +228,9 @@ public class InvoiceDownloadAction implements ICommonAction {
cm.setMaxTotal(500);
cm.setDefaultMaxPerRoute(50);
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 连接池获取连接超时
.setConnectTimeout(5000) // 连接建立超时
.setSocketTimeout(20000) // 等待响应超时
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 连接池获取连接超时
.setConnectTimeout(5000) // 连接建立超时
.setSocketTimeout(20000) // 等待响应超时
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
@ -249,5 +251,8 @@ public class InvoiceDownloadAction implements ICommonAction {
get.releaseConnection();
return responseString;
}
public IUAPQueryBS getQueryService() {
return NCLocator.getInstance().lookup(IUAPQueryBS.class);
}
}