Browse Source

发票处理

master
xyiege 3 years ago
parent
commit
fac9d6fee0
  1. 51
      src/main/java/cn/chjyj/szwh/utils/RequestUtils.java
  2. 6
      src/main/java/cn/chjyj/szwh/utils/SzwhApiUtils.java
  3. 11
      src/main/java/cn/chjyj/szwh/utils/TaxUtils.java

51
src/main/java/cn/chjyj/szwh/utils/RequestUtils.java

@ -1,5 +1,6 @@
package cn.chjyj.szwh.utils;
import cn.chjyj.szwh.constant.ChConstant;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
@ -20,6 +21,8 @@ import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
@ -194,4 +197,52 @@ public class RequestUtils {
}
return "";
}
/**
* 下载发票信息到本地目录
* @param tdurl 发票链接地址
* @param fname 本地存储名称
* @return
*/
public static String downTaxTicket(String tdurl,String fname){
String path= ChConstant.INVOICE; //发票文件夹
//按年月日目录分开
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DATE);
//检查文件夹是否存在
// 构建形如: upload/pdf/2022/10/20 的目录
String folder = path+ File.separator+year+File.separator
+month+File.separator+day;
//创建文件夹
File pathFile = new File(folder);
if(!pathFile.exists()){
//多级目录方式
pathFile.mkdirs();
}
//最终的文件目录
String filename = pathFile+File.separator+fname;
//文件下载
CloseableHttpResponse httpResponse = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(tdurl);
try {
httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
if(entity!=null){
FileOutputStream fos = new FileOutputStream(new File(filename));
entity.writeTo(fos);
EntityUtils.consume(entity);
}
//关闭
httpGet.clone();
httpResponse.close();
httpClient.close();
}catch (Exception ex){
logger.error("发票:"+tdurl+",下载出错:"+ex.getMessage());
}
return filename;
}
}

6
src/main/java/cn/chjyj/szwh/utils/SzwhApiUtils.java

@ -267,8 +267,10 @@ public class SzwhApiUtils {
JSONObject json = RequestUtils.postData(turl,reqmap.toJSONString(),hmap);
log.info("发票信息结果::"+reqmap.toJSONString()+",ret:"+json);
String tokenStr =json.getString("access_token");
//做缓存
RedisUtil.set(key,token,3600);
if(StringUtils.isNotEmpty(tokenStr)){
//做缓存
RedisUtil.set(key,tokenStr,3600);
}
return tokenStr;
}
}

11
src/main/java/cn/chjyj/szwh/utils/TaxUtils.java

@ -60,6 +60,11 @@ public class TaxUtils {
public static Map fillTax(BigDecimal price,OrderUser orderUser){
Map tmap = new HashMap();
tmap.put("totalAmountTax",price); //发票总金额
//购买方信息
tmap.put("buyerName",orderUser.getName());
tmap.put("buyerType","01");
//购买方税号
tmap.put("buyerTaxNo",orderUser.getUscc());
Map tdmap = new HashMap();
tdmap.put("amount",price); //金额
tdmap.put("invoiceNature","0");//0:正常 1:折扣 2:被折扣
@ -70,11 +75,7 @@ public class TaxUtils {
tdmap.put("yhzcbs","0"); //优惠政策标识
//添加到细节节点
tmap.put("manualOrderDetails",tdmap);//发票细节
//购买方信息
tmap.put("buyerName",orderUser.getName());
tmap.put("buyerType","01");
//购买方税号
tmap.put("buyerTaxNo",orderUser.getUscc());
//返回发票对象map
return tdmap;
}

Loading…
Cancel
Save