From 5e7b0ed4a154ad067cbcf4aa1a1c7cce32f9864c Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期五, 26 四月 2024 18:02:17 +0800
Subject: [PATCH] 唯品会链接解析升级

---
 fanli/src/main/java/com/yeshi/fanli/util/jd/JDApiUtil.java |  296 +++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 235 insertions(+), 61 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/util/jd/JDApiUtil.java b/fanli/src/main/java/com/yeshi/fanli/util/jd/JDApiUtil.java
index 040ea66..bad19de 100644
--- a/fanli/src/main/java/com/yeshi/fanli/util/jd/JDApiUtil.java
+++ b/fanli/src/main/java/com/yeshi/fanli/util/jd/JDApiUtil.java
@@ -4,35 +4,26 @@
 import java.lang.reflect.Type;
 import java.math.BigDecimal;
 import java.net.URLEncoder;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
-import com.sun.istack.NotNull;
+import com.yeshi.fanli.dto.jd.*;
+import com.yeshi.fanli.entity.jd.JDBackUpOrder;
+import com.yeshi.fanli.util.Constant;
+import com.yeshi.fanli.util.taobao.SearchFilterUtil;
+import com.yeshi.goods.facade.utils.taobao.DaTaoKeApiUtil;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.methods.PostMethod;
 
 import com.google.gson.Gson;
 import com.google.gson.reflect.TypeToken;
-import com.yeshi.fanli.dto.jd.JDCategoryInfo;
-import com.yeshi.fanli.dto.jd.JDCommissionInfo;
-import com.yeshi.fanli.dto.jd.JDCouponInfo;
-import com.yeshi.fanli.dto.jd.JDFilter;
-import com.yeshi.fanli.dto.jd.JDOrderResult;
-import com.yeshi.fanli.dto.jd.JDPingouInfo;
-import com.yeshi.fanli.dto.jd.JDSearchResult;
-import com.yeshi.fanli.dto.jd.JDShopInfo;
 import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
 import com.yeshi.fanli.entity.jd.JDGoods;
 import com.yeshi.fanli.entity.jd.JDOrder;
 import com.yeshi.fanli.entity.jd.JDOrderItem;
 import com.yeshi.fanli.log.LogHelper;
 import com.yeshi.common.entity.PageEntity;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.yeshi.utils.MoneyBigDecimalUtil;
 import com.yeshi.fanli.util.StringUtil;
 import org.yeshi.utils.TimeUtil;
@@ -41,14 +32,13 @@
 import net.sf.json.JSONObject;
 
 public class JDApiUtil {
+
+    final static Logger logger = LoggerFactory.getLogger("jdApiLogger");
+
     public static String APP_ID = "1864778416";
     public static String APP_KEY = "388f5133d13a5ea290aa5c44f3ebcc00";
-    public static String SECRET_KEY = "e22367fc866746d98b50d47b4c521781";
+    public static String SECRET_KEY = "85e7e72110564efab6e13d302a4419da";
     private static String SERVER_URL = "https://router.jd.com/api";
-
-    public static long POSITION_FANLI = 1869704794L;
-    public static long POSITION_SHARE = 1869542990L;
-    public static long POSITION_COUPON = 1869704796L;
 
     // 璁㈠崟鏌ヨ绫诲瀷
     public static int ORDER_TYPE_CREATETIME = 1;// 涓嬪崟鏃堕棿
@@ -146,47 +136,41 @@
         return resultJson.optJSONObject("data").optString("clickURL");
     }
 
-    public static String convertLinkWithSubUnionId(@NotNull String materialId, String couponUrl, String giftCouponKey, @NotNull String positionId,
-                                                   String subUnionId) {
+    public static String convertLinkWithSubUnionId(String materialId, String couponUrl, String giftCouponKey, String positionId,
+                                                   String subUnionId) throws Exception {
         String log = "浜笢杞摼鍑洪敊:";
-        try {
-            JSONObject json = new JSONObject();
+        JSONObject json = new JSONObject();
 
-            json.put("siteId", APP_ID);
-            json.put("positionId", positionId);
-            json.put("materialId", materialId);
+        json.put("siteId", APP_ID);
+        json.put("positionId", positionId);
+        json.put("materialId", materialId);
 
-            if (!StringUtil.isNullOrEmpty(giftCouponKey)) {
-                json.put("giftCouponKey", giftCouponKey);
-            }
-            if (!StringUtil.isNullOrEmpty(couponUrl))
-                json.put("couponUrl", couponUrl);
+        if (!StringUtil.isNullOrEmpty(giftCouponKey)) {
+            json.put("giftCouponKey", giftCouponKey);
+        }
+        if (!StringUtil.isNullOrEmpty(couponUrl))
+            json.put("couponUrl", couponUrl);
 
-            if (!StringUtil.isNullOrEmpty(subUnionId))
-                json.put("subUnionId", subUnionId);
+        if (!StringUtil.isNullOrEmpty(subUnionId))
+            json.put("subUnionId", subUnionId);
 
-            // 1锛氶暱閾撅紝 2 锛氱煭閾� 锛�3锛� 闀块摼+鐭摼
-            json.put("chainType", 3);
-            JSONObject root = new JSONObject();
-            root.put("promotionCodeReq", json);
+        // 1锛氶暱閾撅紝 2 锛氱煭閾� 锛�3锛� 闀块摼+鐭摼
+        json.put("chainType", 3);
+        JSONObject root = new JSONObject();
+        root.put("promotionCodeReq", json);
 
-            String result = baseRequest2("jd.union.open.promotion.bysubunionid.get", null, root);
+        String result = baseRequest2("jd.union.open.promotion.bysubunionid.get", null, root);
 
-            log += json.toString() + "####" + result;
-            JSONObject resultJson = JSONObject.fromObject(result);
-            result = resultJson.optJSONObject("jd_union_open_promotion_bysubunionid_get_response").optString("result");
-            System.out.println(result);
-            if (result == null) {
-                return null;
-            }
-
-            resultJson = JSONObject.fromObject(result);
-            return resultJson.optJSONObject("data").optString("shortURL");
-        } catch (Exception e) {
-            LogHelper.error(log);
+        log += json.toString() + "####" + result;
+        JSONObject resultJson = JSONObject.fromObject(result);
+        result = resultJson.optJSONObject("jd_union_open_promotion_bysubunionid_get_response").optString("result");
+        System.out.println(result);
+        if (result == null) {
+            return null;
         }
 
-        return null;
+        resultJson = JSONObject.fromObject(result);
+        return resultJson.optJSONObject("data").optString("shortURL");
     }
 
     public static JDGoods queryGoodsDetail(Long skuId) {
@@ -239,6 +223,8 @@
      * @return
      */
     public static JDSearchResult queryByKey(JDFilter filter) {
+        logger.info(String.format("queryByKey:%s  %s", filter.getKeyword(), StringUtil.concat(filter.getListId(), ",")));
+
         JDSearchResult searchResult = new JDSearchResult();
 
         List<JDGoods> list = new ArrayList<>();
@@ -304,15 +290,16 @@
             json.put("owner", filter.getOwner());
 
         System.out.println(json.toString());
+        String result = "";
+
 
         JSONObject jsonDTO = new JSONObject();
         jsonDTO.put("goodsReqDTO", json);
-
-        String result = baseRequest2("jd.union.open.goods.query", null, jsonDTO);
+        result = baseRequest2("jd.union.open.goods.query", null, jsonDTO);
         System.out.println(result);
         JSONObject resultJson = JSONObject.fromObject(result);
         resultJson = resultJson.optJSONObject("jd_union_open_goods_query_response");
-        if (resultJson.optInt("code") == 0) {
+        if (resultJson != null && resultJson.optInt("code") == 0) {
             result = resultJson.optString("result");
             resultJson = JSONObject.fromObject(result);
             String message = resultJson.optString("message");
@@ -326,13 +313,33 @@
                     }
                 }
             }
-
             long totalCount = resultJson.optLong("totalCount");
             PageEntity pageEntity = new PageEntity();
             pageEntity.setTotalCount(totalCount);
-
             searchResult.setPageEntity(pageEntity);
+        } else {
+            result = DaTaoKeApiUtil.searchJDGoods(json);
+            resultJson = JSONObject.fromObject(result);
+            if (resultJson.optInt("code") == 0) {
+                JSONObject data = resultJson.optJSONObject("data");
+                JSONArray array = data.optJSONArray("list");
+                for (int i = 0; i < array.size(); i++) {
+                    JSONObject item = array.optJSONObject(i);
+                    JDGoods goods = parseDaTaoKeJDGoods(item);
+                    if (goods != null)
+                        list.add(goods);
+                }
+
+                long totalCount = data.optLong("totalNum");
+                PageEntity pageEntity = new PageEntity();
+                pageEntity.setTotalCount(totalCount);
+                searchResult.setPageEntity(pageEntity);
+            }
+            System.out.printf(result);
+
         }
+
+
         searchResult.setGoodsList(list);
         return searchResult;
     }
@@ -482,6 +489,124 @@
         return goods;
     }
 
+
+    private static JDGoods parseDaTaoKeJDGoods(JSONObject json) {
+        JDGoods goods = new JDGoods();
+
+        // 浣i噾淇℃伅
+        BigDecimal commissionShare = new BigDecimal(json.optString("commissionShare"));
+        if (commissionShare.compareTo(new BigDecimal(0)) > 0) {
+            JDCommissionInfo commissionInfo = new JDCommissionInfo();
+            commissionInfo.setCommission(new BigDecimal(json.optString("commission")));
+            commissionInfo.setCommissionShare(commissionShare);
+            goods.setCommissionInfo(commissionInfo);
+        } else {
+            return null;
+        }
+
+        goods.setComments(json.optLong("comments"));
+        goods.setBrandCode(json.optString("brandCode"));
+        goods.setBrandName(json.optString("brandName"));
+        goods.setGoodCommentsShare(new BigDecimal(json.optString("goodCommentsShare")));
+        goods.setInOrderCount30Days(json.optLong("inOrderCount30Days"));
+        goods.setOwner(json.optString("owner"));
+        goods.setMaterialUrl(json.optString("materialUrl"));
+        goods.setSkuId(json.optLong("skuId"));
+        goods.setSkuName(json.optString("skuName"));
+        goods.setIsHot(json.optInt("isHot"));
+
+        // 浠锋牸淇℃伅
+        goods.setPrice(new BigDecimal(json.optString("price")));
+
+        // 搴楅摵淇℃伅
+        JDShopInfo jdshopInfo = new JDShopInfo();
+        jdshopInfo.setShopId(json.optInt("shopId"));
+        jdshopInfo.setShopName(json.optString("shopName"));
+        goods.setShopInfo(jdshopInfo);
+
+        // 鍒嗙被淇℃伅
+        JDCategoryInfo categoryInfo = new JDCategoryInfo();
+        categoryInfo.setCid1(json.optLong("cid1"));
+        categoryInfo.setCid1Name(json.optString("cid1Name"));
+        categoryInfo.setCid2(json.optLong("cid2"));
+        categoryInfo.setCid2Name(json.optString("cid2Name"));
+        categoryInfo.setCid3(json.optLong("cid3"));
+        categoryInfo.setCid3Name(json.optString("cid3Name"));
+        goods.setCategoryInfo(categoryInfo);
+
+        // 鍥剧墖淇℃伅
+        List<String> imageList = new ArrayList<>();
+        JSONArray imagesArray = json.optJSONArray("imageUrlList");
+        for (int i = 0; i < imagesArray.size(); i++) {
+            imageList.add(imagesArray.optString(i));
+        }
+        goods.setImageList(imageList);
+
+        if (imageList.size() > 0) {
+            goods.setPicUrl(imageList.get(0));
+        }
+
+        BigDecimal price = new BigDecimal(json.optString("price"));
+        // 鎷艰喘淇℃伅
+        Object pinGouInfo = json.get("pinGouInfo");
+        if (pinGouInfo != null) {
+            JSONObject pinGouInfoJson = JSONObject.fromObject(pinGouInfo);
+            if (!pinGouInfoJson.isEmpty()) {
+                JDPingouInfo jdPinGouInfo = new JDPingouInfo();
+                price = new BigDecimal(pinGouInfoJson.optString("pingouPrice"));
+                jdPinGouInfo.setPingouPrice(price);
+                jdPinGouInfo.setPingouTmCount(pinGouInfoJson.optLong("pingouTmCount"));
+                jdPinGouInfo.setPingouUrl(pinGouInfoJson.optString("pingouUrl"));
+                jdPinGouInfo.setTotalCount(pinGouInfoJson.optLong("totalCount"));
+                jdPinGouInfo.setPingouEndTime(pinGouInfoJson.optLong("pingouEndTime"));
+                jdPinGouInfo.setPingouStartTime(pinGouInfoJson.optLong("pingouStartTime"));
+                goods.setPinGouInfo(jdPinGouInfo);
+            }
+        }
+
+        // 鍒镐俊鎭�
+        Object coupon = json.get("couponInfo");
+        if (coupon != null) {
+            JSONArray couponArray = json.optJSONArray("couponList");
+            if (couponArray != null) {
+                List<JDCouponInfo> couponInfoList = new ArrayList<>();
+                for (int i = 0; i < couponArray.size(); i++) {
+                    JDCouponInfo couponInfo = parseJDCouponInfo(couponArray.optJSONObject(i));
+                    if (couponInfo != null)
+                        couponInfoList.add(couponInfo);
+                }
+
+                // 鏍规嵁娑堣垂鏉′欢鎺掑簭
+                Comparator<JDCouponInfo> cm = new Comparator<JDCouponInfo>() {
+                    @Override
+                    public int compare(JDCouponInfo o1, JDCouponInfo o2) {
+                        return o1.getQuota().compareTo(o2.getQuota());
+                    }
+                };
+                Collections.sort(couponInfoList, cm);
+                // if (couponInfoList.size() > 0) {
+                // // 璁剧疆鍒镐俊鎭负鏈�鎺ヨ繎鐨勪竴涓�
+                // for (int i = couponInfoList.size() - 1; i >= 0; i--) {
+                // if
+                // (couponInfoList.get(i).getQuota().compareTo(goods.getPrice())
+                // <= 0) {
+                // goods.setCouponInfo(couponInfoList.get(i));
+                // break;
+                // }
+                // }
+                // if (goods.getCouponInfo() == null)
+                // goods.setCouponInfo(couponInfoList.get(0));
+                // }
+                //
+                goods.setCouponInfoList(couponInfoList);
+
+            }
+        }
+
+        return goods;
+    }
+
+
     /**
      * 鑾峰彇鎺ㄥ箍鍟嗗搧淇℃伅鎺ュ彛
      *
@@ -493,6 +618,7 @@
         JSONObject json = new JSONObject();
         json.put("skuIds", StringUtil.concat(skuIdList, ","));
         String result = baseRequest2("jd.union.open.goods.promotiongoodsinfo.query", null, json);
+        System.out.println(result);
         JSONObject resultJson = JSONObject.fromObject(result);
         resultJson = resultJson.optJSONObject("jd_union_open_goods_promotiongoodsinfo_query_response");
         if (resultJson.optInt("code") == 0) {
@@ -683,10 +809,10 @@
             root = JSONObject.fromObject(root.optString("result"));
             if (root.optInt("code") == 200 && root.optJSONArray("data") != null) {
 
-                String date = root.optJSONArray("data").toString();
+                JSONArray data = root.optJSONArray("data");
                 Type typeToken = new TypeToken<List<JDOrder>>() {
                 }.getType();
-                List<JDOrder> orderList = new Gson().fromJson(date, typeToken);
+                List<JDOrder> orderList = new Gson().fromJson(data.toString(), typeToken);
                 if (orderList != null)
                     for (JDOrder order : orderList) {
                         Map<Long, List<JDOrderItem>> map = new HashMap<>();
@@ -718,6 +844,41 @@
 
                     }
                 return new JDOrderResult(hasMore, orderList);
+            }
+        }
+        return null;
+    }
+
+
+    public static JDOrderForBackUpResult getOrderListForBackUp(int page, int pageSize, Date time, int type) {
+        JSONObject json = new JSONObject();
+        JSONObject orderReq = new JSONObject();
+        orderReq.put("pageNo", page);
+        orderReq.put("pageSize", pageSize);
+        orderReq.put("type", type);
+        orderReq.put("time", TimeUtil.getGernalTime(time.getTime(), "yyyyMMddHH"));
+
+        json.put("orderReq", orderReq);
+        String result = baseRequest2("jd.union.open.order.query", null, json);
+        System.out.println(result);
+        JSONObject root = JSONObject.fromObject(result).optJSONObject("jd_union_open_order_query_response");
+        if (root.optInt("code") == 0) {
+            boolean hasMore = root.optBoolean("hasMore");
+            root = JSONObject.fromObject(root.optString("result"));
+            if (root.optInt("code") == 200 && root.optJSONArray("data") != null) {
+                List<JDBackUpOrder> orderList = new ArrayList<>();
+                JSONArray data = root.optJSONArray("data");
+                for (int i = 0; i < data.size(); i++) {
+                    JSONObject item = data.optJSONObject(i);
+                    String orderId = item.optString("orderId");
+                    Long orderTime = item.optLong("orderTime");
+                    JDBackUpOrder order = new JDBackUpOrder();
+                    order.setId(orderId);
+                    order.setContent(item.toString());
+                    order.setOrderTime(new Date(orderTime));
+                    orderList.add(order);
+                }
+                return new JDOrderForBackUpResult(hasMore, orderList);
             }
         }
         return null;
@@ -798,4 +959,17 @@
         System.out.println(result);
     }
 
+    public static void main(String[] args) {
+//        JDFilter filter=new JDFilter();
+//        filter.setKeyword("鑺卞崏");
+//        filter.setPageIndex(1);
+//        filter.setPageSize(20);
+//        queryByKey(filter);
+
+        long now = System.currentTimeMillis();
+
+        JDOrderResult result = JDApiUtil.getOrderList(1, 200, new Date(now), JDApiUtil.ORDER_TYPE_CREATETIME);
+        System.out.printf(result.toString());
+    }
+
 }

--
Gitblit v1.8.0