123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- package com.iamberry.rst.util;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.iamberry.file.FileToBase64;
- import com.iamberry.rst.core.ei.Billing;
- import com.iamberry.rst.core.ei.BillingItem;
- import com.iamberry.rst.core.ei.PlatformInfo;
- import com.iamberry.rst.core.ei.PushLog;
- import com.iamberry.rst.faces.ei.BillingService;
- import com.iamberry.rst.faces.ei.PlatformInfoService;
- import com.iamberry.rst.faces.ei.PushLogService;
- import com.iamberry.wechat.tools.NameUtils;
- import com.jd.open.api.sdk.DefaultJdClient;
- import com.jd.open.api.sdk.JdClient;
- import com.jd.open.api.sdk.JdException;
- import com.jd.open.api.sdk.domain.jinsuanpan.FinInvoiceOwnProvider.response.apply.InvoiceOwnResult;
- import com.jd.open.api.sdk.request.jinsuanpan.PopInvoiceSelfAmountRequest;
- import com.jd.open.api.sdk.request.jinsuanpan.PopInvoiceSelfApplyRequest;
- import com.jd.open.api.sdk.response.jinsuanpan.PopInvoiceSelfAmountResponse;
- import com.jd.open.api.sdk.response.jinsuanpan.PopInvoiceSelfApplyResponse;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.text.DecimalFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 京东对接后台商户服务
- */
- @Service
- public class JdServiceUtil {
- private Logger logger = LoggerFactory.getLogger(JdServiceUtil.class);
- private String JD_URL = NameUtils.getConfig("JD_URL");
- private String JD_ORDER = NameUtils.getConfig("JD_ORDER");
- @Autowired
- private PlatformInfoService platformInfoService;
- @Autowired
- private BillingService billingService;
- @Autowired
- private PushLogService pushLogService;
- /**
- * 京东商户平台查询订单信息
- * @param orderId
- * @param platId
- * @json
- * @return
- * @throws JdException
- */
- public Billing getJdOrder(String orderId, Integer platId){
- Billing billing = new Billing();
- PlatformInfo platformInfo = platformInfoService.getPlatformInfoById(platId);
- JdClient client = new DefaultJdClient(JD_URL,platformInfo.getPlatAccessToken(),platformInfo.getPlatAppKey(),platformInfo.getPlatAppSecret());
- PopInvoiceSelfAmountRequest rq=new PopInvoiceSelfAmountRequest();
- rq.setOrderId(orderId);
- PopInvoiceSelfAmountResponse response = null;
- try {
- response = client.execute(rq);
- } catch (JdException e) {
- e.printStackTrace();
- billing.setBillResultMsg("京东api调用失败!");
- return billing;
- }
- String json = response.getMsg();
- List<BillingItem> billingItemList = new ArrayList<BillingItem>();
- String billCompanyName = "",billTaxNumber="";
- Double amount = 0.00;
- try{
- JSONObject jsonObject = JSON.parseObject(json);
- jsonObject = jsonObject.getJSONObject("jingdong_pop_invoice_self_amount_responce")
- .getJSONObject("queryamountforown_result")
- .getJSONObject("data");
- amount = jsonObject.getDouble("shouldInvoiceAmount")*100;
- billCompanyName = jsonObject.getString("ivcTitle"); //公司名称
- billTaxNumber = jsonObject.getString("customerTaxNo"); //公司税号
- JSONArray jsonArray = (JSONArray) jsonObject.get("orderShouldInvoiceAmountDetailList");
- if(jsonArray != null && jsonArray.size()>0){
- for(int i=0;i<jsonArray.size();i++){
- JSONObject insertObj = jsonArray.getJSONObject(i);
- String jdProductId = insertObj.getString("productId");
- Double invoiceAmount = insertObj.getDouble("shouldInvoiceAmount")*100; //
- Double price = insertObj.getDouble("price")*100;
- Integer num = insertObj.getInteger("num");
- boolean isPresence = false; //是否存在 该产品
- if(jdProductId != null && !"".equals(jdProductId)){
- for (BillingItem billingItem : billingItemList){
- if(jdProductId.equals(billingItem.getJdProductId())){
- isPresence = true; //存在该产品
- //id,代表是同一个订单项
- billingItem.setBillItemAmountPrice(billingItem.getBillItemAmountPrice() + Integer.valueOf(invoiceAmount.intValue())); //因为京东有一个售价和一个优惠价(负数),所以同一个商品相加就行了
- billingItem.setBillItemUnitPrice(billingItem.getBillItemUnitPrice() + Integer.valueOf(price.intValue())); //因为京东有一个售价和一个优惠价(负数),所以同一个商品相加就行了
- break;
- }
- }
- }
- if(!isPresence){
- //不存在该产品,添加该产品
- BillingItem bi = new BillingItem();
- bi.setBillItemNum(num);
- bi.setBillItemUnitPrice(Integer.valueOf(price.intValue()));
- bi.setBillItemAmountPrice(Integer.valueOf(invoiceAmount.intValue()));
- bi.setJdProductId(jdProductId);
- billingItemList.add(bi);
- }
- }
- }
- }catch (Exception e){
- e.printStackTrace();
- billing.setBillResultMsg("JSON解析失败!失败原因:"+json);
- return billing;
- }
- billing.setBillingItemList(billingItemList);
- billing.setBillAmount(Integer.valueOf(amount.intValue()));
- billing.setBillCompanyName(billCompanyName);
- billing.setBillTaxNumber(billTaxNumber);
- return billing;
- }
- /**
- * 上传蓝票
- * @param pushLog
- * @return
- */
- public PushLog pushInvo(PushLog pushLog,String path){
- Billing billing = billingService.getBillingById(pushLog.getBillId());
- PushLog ps = new PushLog();
- ps.setPlatId(billing.getPlatId());
- PlatformInfo platformInfo = platformInfoService.getPlatformInfoById(billing.getPlatId());
- JdClient client = new DefaultJdClient(JD_URL,platformInfo.getPlatAccessToken(),platformInfo.getPlatAppKey(),platformInfo.getPlatAppSecret());
- PopInvoiceSelfApplyRequest request=new PopInvoiceSelfApplyRequest();
- // request.setProductId( "jingdong,yanfa,pop" );
- // request.setProductName( "jingdong,yanfa,pop" );
- // request.setNum( "123,234,345" );
- // request.setPrice( "jingdong,yanfa,pop" );
- // request.setSpec( "jingdong,yanfa,pop" );
- // request.setUnit( "jingdong,yanfa,pop" );
- // request.setTaxRate( "jingdong,yanfa,pop" );
- // request.setTaxCategroyCode( "jingdong,yanfa,pop" );
- // request.setIsTaxDiscount( "123,234,345" );
- // request.setTaxDiscountContent( "jingdong,yanfa,pop" );
- // request.setZeroTax( "jingdong,yanfa,pop" );
- // request.setDeductions( "jingdong,yanfa,pop" );
- // request.setImei( "jingdong,yanfa,pop" );
- // request.setDiscount( "jingdong,yanfa,pop" );
- // request.setFreight( "jingdong,yanfa,pop" );
- if("1".equals(JD_ORDER)){
- request.setOrderId(JD_ORDER);
- }else{
- request.setOrderId(billing.getBillOrderNo()); //订单编号
- }
- request.setReceiverTaxNo(platformInfo.getPlatTaxNumber() ); //销货方识别号(税号)
- request.setReceiverName( platformInfo.getPlatCompanyName() );
- request.setInvoiceCode(billing.getBillInvoiceCode() ); // 发票代码
- request.setInvoiceNo(Integer.valueOf(billing.getBillInvoiceNum())); // 发票号码
- request.setIvcTitle( billing.getBillCompanyName()); //发票抬头
- Double amount = Double.valueOf(billing.getBillAmount());
- amount = amount/100;
- DecimalFormat df = new DecimalFormat("0.00");
- String amountString = df.format(amount);
- request.setTotalPrice( amountString ); //开票金额 两位小数 N
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- String invoDate = sdf.format( billing.getBillInvoiceDate());
- request.setInvoiceTime(invoDate);
- String filePath = path + billing.getBillInvoiceFileUrl();
- String base64 = "";
- try {
- base64 = FileToBase64.encodeBase64File(filePath);
- } catch (Exception e) {
- e.printStackTrace();
- logger.info("发票转为base64失败!");
- ps.setPushLogResultMsg("发票转为base64失败!");
- ps.setPushLogStatus(2);
- return ps;
- }
- request.setPdfInfo(base64); //发票PDF文件二进制流base64
- // request.setOrderType( 123 );
- // request.setIvcContentType( 123 );
- // request.setIvcContentName( "jingdong" );
- // request.setEiRemark( "jingdong" );
- request.setReceiverAddress(platformInfo.getPlatAddress()); // 销货方公司地址
- request.setReceiverPhone( platformInfo.getPlatTel() );
- // request.setReceiverBankName( "jingdong" );
- // request.setReceiverBankAccount( "jingdong" );
- request.setDrawer( platformInfo.getPlatBilling()); // 开票人platBilling
- request.setPayee( platformInfo.getPlatPayee()); // 收款人platPayee
- try {
- PopInvoiceSelfApplyResponse response = client.execute(request);
- String msg = response.getMsg();
- InvoiceOwnResult invoiceOwnResult = response.getApplyinvoiceforownResult();
- if(invoiceOwnResult.getSuccess()){
- ps.setPushLogResultMsg(invoiceOwnResult.getMessage());
- ps.setPushLogStatus(1);
- pushLogService.save(ps);
- }else{
- ps.setPushLogResultMsg("发票上传失败!");
- ps.setPushLogStatus(2);
- }
- } catch (JdException e) {
- e.printStackTrace();
- }
- return ps;
- }
- }
|