This commit is contained in:
lihao 2025-05-05 15:21:21 +08:00
parent d41304656c
commit 7dd8b5367c
1 changed files with 253 additions and 0 deletions

View File

@ -0,0 +1,253 @@
package nccloud.web.sscivm.ivsale.sale.action;
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 javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.net.URLEncoder;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import com.yonyou.cloud.utils.StringUtils;
import nc.bs.trade.business.HYSuperDMO;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.sscivm.invoice.IVMInvoiceHeadVO;
import nc.vo.sscivm.ivsale.IVApplicationHeadVO;
import nc.vo.sscivm.ivsale.IVApplogVO;
import nccloud.framework.core.io.WebFile;
import nccloud.framework.service.ServiceLocator;
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;
public class InvoiceDownloadAction implements ICommonAction {
IAttachmentService ncservice = ServiceLocator.find(IAttachmentService.class);
@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 isdoc = "z";
WebFile file = null;
try {
List<IVMInvoiceHeadVO> headVOList =ServiceLocator.find(IVSaleQueryService.class).queryIVMInvoiceHeadVOsByPks(new String[] {pk[0]});
IVMInvoiceHeadVO headVO = headVOList.get(0);
String invoiceType = invoiceTypes[0];
// 获取旗舰版的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=";
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);
}
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);
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("未查询到发票信息");
}
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="";
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();//合同号
}
//发票号码+单位名称+合同号开票申请字段+金额发票上的价税合计 合同号_单位名称_发票号码_金额
// 合同号_单位名称_发票号码_金额
String name = ctcode+"_"+headVO.getGmfmc()+"_"+Fphm+"_"+String.valueOf(headVO.getJshj().toDouble()) + filename;
// String name = Fphm+"_"+headVO.getGmfmc()+"_"+ctcode+"_"+String.valueOf(headVO.getJshj().toDouble()) + filename;
file = new WebFile(name, ins);
} else {
throw new Exception("获取发票信息失败");
}
}else {
throw new Exception("获取access_token失败");
}
// 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);
} catch (BusinessException e) {
ExceptionUtils.wrappException(e);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file;
}
private String doPost(String requestUrl, Map<String, Object> param) throws IOException {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(500);
cm.setDefaultMaxPerRoute(50);
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 连接池获取连接超时
.setConnectTimeout(5000) // 连接建立超时
.setSocketTimeout(20000) // 等待响应超时
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
.setDefaultRequestConfig(globalConfig).build();
HttpPost post = new HttpPost(requestUrl);
post.setHeader("Content-Type", "application/json;charset=UTF-8");
post.setEntity(new StringEntity(JSONObject.toJSONString(param), "utf-8"));
String responseString = httpClient.execute(post, response -> EntityUtils.toString(response.getEntity()));
return responseString;
}
private String doGet(String requestUrl, Map<String, String> paramMap) throws IOException {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(500);
cm.setDefaultMaxPerRoute(50);
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 连接池获取连接超时
.setConnectTimeout(5000) // 连接建立超时
.setSocketTimeout(20000) // 等待响应超时
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
.setDefaultRequestConfig(globalConfig).build();
StringBuilder param = new StringBuilder("?");
if (paramMap != null) {
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
param.append(entry.getKey());
param.append("=");
param.append(entry.getValue());
param.append("&");
}
param.deleteCharAt(param.length() - 1);
}
String url = requestUrl + param;
HttpGet get = new HttpGet(url);
String responseString = httpClient.execute(get, response -> EntityUtils.toString(response.getEntity()));
get.releaseConnection();
return responseString;
}
}