JdServiceUtil.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package com.iamberry.rst.util;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.iamberry.file.FileToBase64;
  6. import com.iamberry.rst.core.ei.Billing;
  7. import com.iamberry.rst.core.ei.BillingItem;
  8. import com.iamberry.rst.core.ei.PlatformInfo;
  9. import com.iamberry.rst.core.ei.PushLog;
  10. import com.iamberry.rst.faces.ei.BillingService;
  11. import com.iamberry.rst.faces.ei.PlatformInfoService;
  12. import com.iamberry.rst.faces.ei.PushLogService;
  13. import com.iamberry.wechat.tools.NameUtils;
  14. import com.jd.open.api.sdk.DefaultJdClient;
  15. import com.jd.open.api.sdk.JdClient;
  16. import com.jd.open.api.sdk.JdException;
  17. import com.jd.open.api.sdk.domain.jinsuanpan.FinInvoiceOwnProvider.response.apply.InvoiceOwnResult;
  18. import com.jd.open.api.sdk.request.jinsuanpan.PopInvoiceSelfAmountRequest;
  19. import com.jd.open.api.sdk.request.jinsuanpan.PopInvoiceSelfApplyRequest;
  20. import com.jd.open.api.sdk.response.jinsuanpan.PopInvoiceSelfAmountResponse;
  21. import com.jd.open.api.sdk.response.jinsuanpan.PopInvoiceSelfApplyResponse;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.stereotype.Service;
  26. import java.text.DecimalFormat;
  27. import java.text.SimpleDateFormat;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. /**
  31. * 京东对接后台商户服务
  32. */
  33. @Service
  34. public class JdServiceUtil {
  35. private Logger logger = LoggerFactory.getLogger(JdServiceUtil.class);
  36. private String JD_URL = NameUtils.getConfig("JD_URL");
  37. private String JD_ORDER = NameUtils.getConfig("JD_ORDER");
  38. @Autowired
  39. private PlatformInfoService platformInfoService;
  40. @Autowired
  41. private BillingService billingService;
  42. @Autowired
  43. private PushLogService pushLogService;
  44. /**
  45. * 京东商户平台查询订单信息
  46. * @param orderId
  47. * @param platId
  48. * @json
  49. * @return
  50. * @throws JdException
  51. */
  52. public Billing getJdOrder(String orderId, Integer platId){
  53. Billing billing = new Billing();
  54. PlatformInfo platformInfo = platformInfoService.getPlatformInfoById(platId);
  55. JdClient client = new DefaultJdClient(JD_URL,platformInfo.getPlatAccessToken(),platformInfo.getPlatAppKey(),platformInfo.getPlatAppSecret());
  56. PopInvoiceSelfAmountRequest rq=new PopInvoiceSelfAmountRequest();
  57. rq.setOrderId(orderId);
  58. PopInvoiceSelfAmountResponse response = null;
  59. try {
  60. response = client.execute(rq);
  61. } catch (JdException e) {
  62. e.printStackTrace();
  63. billing.setBillResultMsg("京东api调用失败!");
  64. return billing;
  65. }
  66. String json = response.getMsg();
  67. List<BillingItem> billingItemList = new ArrayList<BillingItem>();
  68. String billCompanyName = "",billTaxNumber="";
  69. Double amount = 0.00;
  70. try{
  71. JSONObject jsonObject = JSON.parseObject(json);
  72. jsonObject = jsonObject.getJSONObject("jingdong_pop_invoice_self_amount_responce")
  73. .getJSONObject("queryamountforown_result")
  74. .getJSONObject("data");
  75. amount = jsonObject.getDouble("shouldInvoiceAmount")*100;
  76. billCompanyName = jsonObject.getString("ivcTitle"); //公司名称
  77. billTaxNumber = jsonObject.getString("customerTaxNo"); //公司税号
  78. JSONArray jsonArray = (JSONArray) jsonObject.get("orderShouldInvoiceAmountDetailList");
  79. if(jsonArray != null && jsonArray.size()>0){
  80. for(int i=0;i<jsonArray.size();i++){
  81. JSONObject insertObj = jsonArray.getJSONObject(i);
  82. String jdProductId = insertObj.getString("productId");
  83. Double invoiceAmount = insertObj.getDouble("shouldInvoiceAmount")*100; //
  84. Double price = insertObj.getDouble("price")*100;
  85. Integer num = insertObj.getInteger("num");
  86. boolean isPresence = false; //是否存在 该产品
  87. if(jdProductId != null && !"".equals(jdProductId)){
  88. for (BillingItem billingItem : billingItemList){
  89. if(jdProductId.equals(billingItem.getJdProductId())){
  90. isPresence = true; //存在该产品
  91. //id,代表是同一个订单项
  92. billingItem.setBillItemAmountPrice(billingItem.getBillItemAmountPrice() + Integer.valueOf(invoiceAmount.intValue())); //因为京东有一个售价和一个优惠价(负数),所以同一个商品相加就行了
  93. billingItem.setBillItemUnitPrice(billingItem.getBillItemUnitPrice() + Integer.valueOf(price.intValue())); //因为京东有一个售价和一个优惠价(负数),所以同一个商品相加就行了
  94. break;
  95. }
  96. }
  97. }
  98. if(!isPresence){
  99. //不存在该产品,添加该产品
  100. BillingItem bi = new BillingItem();
  101. bi.setBillItemNum(num);
  102. bi.setBillItemUnitPrice(Integer.valueOf(price.intValue()));
  103. bi.setBillItemAmountPrice(Integer.valueOf(invoiceAmount.intValue()));
  104. bi.setJdProductId(jdProductId);
  105. billingItemList.add(bi);
  106. }
  107. }
  108. }
  109. }catch (Exception e){
  110. e.printStackTrace();
  111. billing.setBillResultMsg("JSON解析失败!失败原因:"+json);
  112. return billing;
  113. }
  114. billing.setBillingItemList(billingItemList);
  115. billing.setBillAmount(Integer.valueOf(amount.intValue()));
  116. billing.setBillCompanyName(billCompanyName);
  117. billing.setBillTaxNumber(billTaxNumber);
  118. return billing;
  119. }
  120. /**
  121. * 上传蓝票
  122. * @param pushLog
  123. * @return
  124. */
  125. public PushLog pushInvo(PushLog pushLog,String path){
  126. Billing billing = billingService.getBillingById(pushLog.getBillId());
  127. PushLog ps = new PushLog();
  128. ps.setPlatId(billing.getPlatId());
  129. PlatformInfo platformInfo = platformInfoService.getPlatformInfoById(billing.getPlatId());
  130. JdClient client = new DefaultJdClient(JD_URL,platformInfo.getPlatAccessToken(),platformInfo.getPlatAppKey(),platformInfo.getPlatAppSecret());
  131. PopInvoiceSelfApplyRequest request=new PopInvoiceSelfApplyRequest();
  132. // request.setProductId( "jingdong,yanfa,pop" );
  133. // request.setProductName( "jingdong,yanfa,pop" );
  134. // request.setNum( "123,234,345" );
  135. // request.setPrice( "jingdong,yanfa,pop" );
  136. // request.setSpec( "jingdong,yanfa,pop" );
  137. // request.setUnit( "jingdong,yanfa,pop" );
  138. // request.setTaxRate( "jingdong,yanfa,pop" );
  139. // request.setTaxCategroyCode( "jingdong,yanfa,pop" );
  140. // request.setIsTaxDiscount( "123,234,345" );
  141. // request.setTaxDiscountContent( "jingdong,yanfa,pop" );
  142. // request.setZeroTax( "jingdong,yanfa,pop" );
  143. // request.setDeductions( "jingdong,yanfa,pop" );
  144. // request.setImei( "jingdong,yanfa,pop" );
  145. // request.setDiscount( "jingdong,yanfa,pop" );
  146. // request.setFreight( "jingdong,yanfa,pop" );
  147. if("1".equals(JD_ORDER)){
  148. request.setOrderId(JD_ORDER);
  149. }else{
  150. request.setOrderId(billing.getBillOrderNo()); //订单编号
  151. }
  152. request.setReceiverTaxNo(platformInfo.getPlatTaxNumber() ); //销货方识别号(税号)
  153. request.setReceiverName( platformInfo.getPlatCompanyName() );
  154. request.setInvoiceCode(billing.getBillInvoiceCode() ); // 发票代码
  155. request.setInvoiceNo(Integer.valueOf(billing.getBillInvoiceNum())); // 发票号码
  156. request.setIvcTitle( billing.getBillCompanyName()); //发票抬头
  157. Double amount = Double.valueOf(billing.getBillAmount());
  158. amount = amount/100;
  159. DecimalFormat df = new DecimalFormat("0.00");
  160. String amountString = df.format(amount);
  161. request.setTotalPrice( amountString ); //开票金额 两位小数 N
  162. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  163. String invoDate = sdf.format( billing.getBillInvoiceDate());
  164. request.setInvoiceTime(invoDate);
  165. String filePath = path + billing.getBillInvoiceFileUrl();
  166. String base64 = "";
  167. try {
  168. base64 = FileToBase64.encodeBase64File(filePath);
  169. } catch (Exception e) {
  170. e.printStackTrace();
  171. logger.info("发票转为base64失败!");
  172. ps.setPushLogResultMsg("发票转为base64失败!");
  173. ps.setPushLogStatus(2);
  174. return ps;
  175. }
  176. request.setPdfInfo(base64); //发票PDF文件二进制流base64
  177. // request.setOrderType( 123 );
  178. // request.setIvcContentType( 123 );
  179. // request.setIvcContentName( "jingdong" );
  180. // request.setEiRemark( "jingdong" );
  181. request.setReceiverAddress(platformInfo.getPlatAddress()); // 销货方公司地址
  182. request.setReceiverPhone( platformInfo.getPlatTel() );
  183. // request.setReceiverBankName( "jingdong" );
  184. // request.setReceiverBankAccount( "jingdong" );
  185. request.setDrawer( platformInfo.getPlatBilling()); // 开票人platBilling
  186. request.setPayee( platformInfo.getPlatPayee()); // 收款人platPayee
  187. try {
  188. PopInvoiceSelfApplyResponse response = client.execute(request);
  189. String msg = response.getMsg();
  190. InvoiceOwnResult invoiceOwnResult = response.getApplyinvoiceforownResult();
  191. if(invoiceOwnResult.getSuccess()){
  192. ps.setPushLogResultMsg(invoiceOwnResult.getMessage());
  193. ps.setPushLogStatus(1);
  194. pushLogService.save(ps);
  195. }else{
  196. ps.setPushLogResultMsg("发票上传失败!");
  197. ps.setPushLogStatus(2);
  198. }
  199. } catch (JdException e) {
  200. e.printStackTrace();
  201. }
  202. return ps;
  203. }
  204. }