yujian
2019-07-30 c6f32fd5fd0cf1cf0758765a697c9e9ad5bbba13
fanli/src/main/java/com/yeshi/fanli/util/jd/JDApiUtil.java
@@ -13,15 +13,21 @@
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.yeshi.utils.HttpUtil;
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.jd.JDGoods;
import com.yeshi.fanli.entity.jd.JDOrder;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
@@ -37,6 +43,7 @@
   public static Long POSITION_FANLI = 1834339426L;
   public static Long POSITION_SHARE = 1834289924L;
   public static Long POSITION_COUPON = 1859510742L;
   // 订单查询类型
   public static int ORDER_TYPE_CREATETIME = 1;// 下单时间
@@ -62,6 +69,31 @@
         client.executeMethod(pm);
         String result = pm.getResponseBodyAsString();
         return new String(result.getBytes("ISO-8859-1"), "UTF-8");
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }
   private static String post2(String url, Map<String, String> params) {
      String baseUrl = url;
      List<String> paramsList = new ArrayList<>();
      if (params != null)
         for (Iterator<String> its = params.keySet().iterator(); its.hasNext();) {
            String key = its.next();
            try {
               paramsList.add(key + "=" + URLEncoder.encode(params.get(key), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
               e.printStackTrace();
            }
         }
      baseUrl += "?" + StringUtil.concat(paramsList, "&");
      HttpClient client = new HttpClient();
      PostMethod pm = new PostMethod(baseUrl);
      try {
         client.executeMethod(pm);
         String result = pm.getResponseBodyAsString();
         return result;
      } catch (Exception e) {
         e.printStackTrace();
      }
@@ -99,30 +131,344 @@
      return result;
   }
   private static String baseRequest2(String method, String accessToken, JSONObject params) {
      Map<String, String> baseMap = new HashMap<String, String>();
      baseMap.put("param_json", params.toString());
      baseMap.put("app_key", APP_KEY);
      baseMap.put("method", method);
      if (accessToken != null)
         baseMap.put("access_token", accessToken);
      baseMap.put("timestamp", TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
      baseMap.put("format", "json");
      baseMap.put("v", "1.0");
      baseMap.put("sign_method", "md5");
      baseMap.put("sign", getSign(baseMap));
      String result = post2(SERVER_URL, baseMap);
      return result;
   }
   /**
    * 转链
    * 
    * @return
    */
   public static String convertLink(String materialId, String couponUrl, String pid) {
   public static String convertLink(String materialId, String couponUrl, String positionId, String ext1) {
      JSONObject json = new JSONObject();
      json.put("materialId", materialId);
      json.put("siteId", APP_ID);
      if (couponUrl != null)
      json.put("positionId", positionId);
      if (!StringUtil.isNullOrEmpty(couponUrl))
         json.put("couponUrl", couponUrl);
      // json.put("ext1", "437032_12");
      json.put("pid", pid);
      if (!StringUtil.isNullOrEmpty(ext1))
          json.put("ext1", ext1);
      JSONObject root = new JSONObject();
      root.put("promotionCodeReq", json);
      String result = baseRequest("jd.union.open.promotion.common.get", null, json);
      String result = baseRequest("jd.union.open.promotion.common.get", null, root);
      JSONObject resultJson = JSONObject.fromObject(result);
      result = resultJson.optJSONObject("jd_union_open_promotion_common_get_response").optString("result");
      if (result == null) {
         return null;
      }
      resultJson = JSONObject.fromObject(result);
      return resultJson.optJSONObject("data").optString("clickURL");
   }
   /**
    * 转链接-短连接
    * @param materialId
    * @param couponUrl
    * @param positionId
    * @param ext1
    * @return
    */
   public static String convertShortLink(String materialId, String couponUrl, String positionId, String ext1) {
      String url = convertLink(materialId, couponUrl, positionId, ext1);
      if (!StringUtil.isNullOrEmpty(url)) {
         url = HttpUtil.getShortLink(url);
      }
      return url;
   }
   public static JDGoods queryGoodsDetail(Long skuId) {
      List<Long> skuIdList = new ArrayList<>();
      skuIdList.add(skuId);
      JDFilter filter = new JDFilter();
      filter.setPageIndex(1);
      filter.setPageSize(20);
      filter.setListId(skuIdList);
      JDSearchResult searchResult = queryByKey(filter);
      if (searchResult != null) {
         List<JDGoods> list = searchResult.getGoodsList();
         if (list != null && list.size() > 0)
            return list.get(0);
      }
      return null;
   }
   /**
    * 关键词商品查询接口【申请】
    * @param skuIdList
    * @return
    */
   public static JDSearchResult queryByKey(JDFilter filter) {
      JDSearchResult searchResult = new JDSearchResult();
      List<JDGoods> list = new ArrayList<>();
      JSONObject json = new JSONObject();
      json.put("pageIndex", filter.getPageIndex());
      json.put("pageSize", filter.getPageSize());
      if (filter.getCid1() != null)
         json.put("cid1", filter.getCid1());
      if (filter.getCid2() != null)
         json.put("cid2", filter.getCid2());
      if (filter.getCid3() != null)
         json.put("cid3", filter.getCid3());
      if (filter.getKeyword() != null)
         json.put("keyword", filter.getKeyword());
      if (filter.getPricefrom() != null)
         json.put("pricefrom", filter.getPricefrom());
      if (filter.getPriceto() != null)
         json.put("priceto", filter.getPriceto());
      if (filter.getCommissionShareStart() != null)
         json.put("commissionShareStart", filter.getCommissionShareStart());
      if (filter.getCommissionShareEnd() != null)
         json.put("commissionShareEnd", filter.getCommissionShareEnd());
      if (filter.getSortName() != null)
         json.put("sortName", filter.getSortName());
      if (filter.getSort() != null)
         json.put("sort", filter.getSort());
      if (filter.getIsCoupon() != null)
         json.put("isCoupon", filter.getIsCoupon());
      if (filter.getListId() != null)
         json.put("skuIds", "[" + StringUtil.concat(filter.getListId(), ",") + "]");
      if (filter.getIsPG() != null)
         json.put("isPG", filter.getIsPG());
      if (filter.getPingouPriceStart() != null)
         json.put("pingouPriceStart", filter.getPingouPriceStart());
      if (filter.getPingouPriceEnd() != null)
         json.put("pingouPriceEnd", filter.getPingouPriceEnd());
      if (filter.getIsHot() != null)
         json.put("isHot", filter.getIsHot());
      if (filter.getBrandCode() != null)
         json.put("brandCode", filter.getBrandCode());
      if (filter.getShopId() != null)
         json.put("shopId", filter.getShopId());
      if (filter.getOwner() != null)
         json.put("owner", filter.getOwner());
      System.out.println(json.toString());
      JSONObject jsonDTO = new JSONObject();
      jsonDTO.put("goodsReqDTO", json);
      String 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) {
         result = resultJson.optString("result");
         resultJson = JSONObject.fromObject(result);
         String message = resultJson.optString("message");
         if ("SUCCESS".equalsIgnoreCase(message)) {
            JSONArray array = resultJson.optJSONArray("data");
            if (array != null) {
               for (int i = 0; i < array.size(); i++) {
                  JDGoods goods = parseJDGoods(array.optJSONObject(i));
                  if (goods != null)
                     list.add(goods);
               }
            }
         }
         long totalCount = resultJson.optLong("totalCount");
         PageEntity pageEntity = new PageEntity();
         pageEntity.setTotalCount(totalCount);
         searchResult.setPageEntity(pageEntity);
      }
      searchResult.setGoodsList(list);
      return searchResult;
   }
   private static JDGoods parseJDGoods(JSONObject json) {
      JDGoods goods = new JDGoods();
      // 佣金信息
      Object commission = json.get("commissionInfo");
      JSONObject commissionJson = JSONObject.fromObject(commission);
      BigDecimal commissionShare = new BigDecimal(commissionJson.optString("commissionShare"));
      if (commissionShare.compareTo(new BigDecimal(0)) > 0) {
         JDCommissionInfo commissionInfo = new JDCommissionInfo();
         commissionInfo.setCommission(new BigDecimal(commissionJson.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"));
      // 价格信息
      Object priceInfo = json.get("priceInfo");
      JSONObject priceInfoJson = JSONObject.fromObject(priceInfo);
      goods.setPrice(new BigDecimal(priceInfoJson.optString("price")));
      // 店铺信息
      Object shopInfo = json.get("shopInfo");
      JSONObject shopInfoJson = JSONObject.fromObject(shopInfo);
      JDShopInfo jdshopInfo = new JDShopInfo();
      jdshopInfo.setShopId(shopInfoJson.optInt("shopId"));
      jdshopInfo.setShopName(shopInfoJson.optString("shopName"));
      goods.setShopInfo(jdshopInfo);
      // 分类信息
      Object category = json.get("categoryInfo");
      JSONObject categoryJson = JSONObject.fromObject(category);
      JDCategoryInfo categoryInfo = new JDCategoryInfo();
      categoryInfo.setCid1(categoryJson.optLong("cid1"));
      categoryInfo.setCid1Name(categoryJson.optString("cid1Name"));
      categoryInfo.setCid2(categoryJson.optLong("cid2"));
      categoryInfo.setCid2Name(categoryJson.optString("cid2Name"));
      categoryInfo.setCid3(categoryJson.optLong("cid3"));
      categoryInfo.setCid3Name(categoryJson.optString("cid3Name"));
      goods.setCategoryInfo(categoryInfo);
      // 图片信息
      List<String> imageList = new ArrayList<String>();
      Object images = json.get("imageInfo");
      JSONObject imagesJson = JSONObject.fromObject(images);
      JSONArray imagesArray = imagesJson.optJSONArray("imageList");
      for (int i = 0; i < imagesArray.size(); i++) {
         imageList.add(imagesArray.optJSONObject(i).optString("url"));
      }
      goods.setImageList(imageList);
      if (imageList.size() > 0) {
         goods.setPicUrl(imageList.get(0));
      }
      BigDecimal price = new BigDecimal(priceInfoJson.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) {
         JSONObject couponJson = JSONObject.fromObject(coupon);
         JSONArray couponArray = couponJson.optJSONArray("couponList");
         if (couponArray != null) {
            BigDecimal discount_temp = null;
            JDCouponInfo couponInfo = null;
            for (int i = 0; i < couponArray.size(); i++) {
               boolean add = false;
               JSONObject jdcoupon = couponArray.optJSONObject(i);
               BigDecimal quota = new BigDecimal(jdcoupon.optString("quota"));
               BigDecimal sub = MoneyBigDecimalUtil.sub(price, quota);
               if (sub.compareTo(new BigDecimal(0)) < 0) {
                  continue; // 商品价格小于优惠券价格限制
               }
               // 券面额
               BigDecimal discount = new BigDecimal(jdcoupon.optString("discount"));
               if (discount_temp == null) {
                  add = true;
                  discount_temp = discount;
               } else if (discount_temp.compareTo(discount) > 0) { // 券面额大
                  add = true;
               }
               if (add) {
                  if (couponInfo == null) {
                     couponInfo = new JDCouponInfo();
                  }
                  couponInfo.setBindType(jdcoupon.optInt("bindType"));
                  couponInfo.setDiscount(new BigDecimal(jdcoupon.optString("discount")));
                  couponInfo.setQuota(new BigDecimal(jdcoupon.optString("quota")));
                  couponInfo.setPlatformType(jdcoupon.optInt("platformType"));
                  couponInfo.setGetEndTime(jdcoupon.optLong("getEndTime"));
                  couponInfo.setGetStartTime(jdcoupon.optLong("getStartTime"));
                  couponInfo.setUseEndTime(jdcoupon.optLong("useEndTime"));
                  couponInfo.setUseStartTime(jdcoupon.optLong("useStartTime"));
                  couponInfo.setLink(jdcoupon.optString("link"));
               }
            }
            goods.setCouponInfo(couponInfo);
         }
      }
      return goods;
   }
   /**
    * 获取推广商品信息接口
    * @param skuIdList
    * @return
    */
   public static List<JDGoods> getGoodsDetail(List<Long> skuIdList) {
      List<JDGoods> list = new ArrayList<>();
      JSONObject json = new JSONObject();
@@ -133,7 +479,6 @@
      if (resultJson.optInt("code") == 0) {
         result = resultJson.optString("result");
         resultJson = JSONObject.fromObject(result);
         System.out.println(resultJson);
         JSONArray array = resultJson.optJSONArray("data");
         if (array != null)
            for (int i = 0; i < array.size(); i++) {
@@ -147,7 +492,21 @@
   private static JDGoods parseSimpleJDGoods(JSONObject json) {
      JDGoods goods = new JDGoods();
      goods.setPrice(StringUtil.isNullOrEmpty(json.optString("wlUnitPrice"))
            ? new BigDecimal(json.optString("unitPrice")) : new BigDecimal(json.optString("wlUnitPrice")));
      BigDecimal commisionRatioWl = new BigDecimal(json.optString("commisionRatioWl"));
      if (commisionRatioWl.compareTo(new BigDecimal(0)) > 0) {
         JDCommissionInfo commissionInfo = new JDCommissionInfo();
         commissionInfo.setCommissionShare(new BigDecimal(json.optString("commisionRatioWl")));
         commissionInfo.setCommission(MoneyBigDecimalUtil
               .div(goods.getPrice().multiply(commissionInfo.getCommissionShare()), new BigDecimal(100)));
         goods.setCommissionInfo(commissionInfo);
      } else {
         return null;
      }
      JDCategoryInfo categoryInfo = new JDCategoryInfo();
      categoryInfo.setCid1(json.optLong("cid1"));
      categoryInfo.setCid1Name(json.optString("cid1Name"));
@@ -158,19 +517,17 @@
      goods.setCategoryInfo(categoryInfo);
      goods.setComments(null);
      goods.setPrice(StringUtil.isNullOrEmpty(json.optString("wlUnitPrice"))
            ? new BigDecimal(json.optString("unitPrice")) : new BigDecimal(json.optString("wlUnitPrice")));
      JDCommissionInfo commissionInfo = new JDCommissionInfo();
      commissionInfo.setCommissionShare(new BigDecimal(json.optString("commisionRatioWl")));
      commissionInfo.setCommission(MoneyBigDecimalUtil
            .div(goods.getPrice().multiply(commissionInfo.getCommissionShare()), new BigDecimal(100)));
      goods.setCommissionInfo(commissionInfo);
      String isFreeShipping = json.optString("isFreeShipping");
      if (!StringUtil.isNullOrEmpty(isFreeShipping)) {
         goods.setIsFreeShipping(Integer.parseInt(isFreeShipping));
      }
      goods.setCouponInfo(null);
      goods.setGoodCommentsShare(null);
      List<String> imageList = new ArrayList<>();
      imageList.add(json.optString("imgUrl"));
      goods.setPicUrl(json.optString("imgUrl"));
      goods.setImageList(imageList);
      goods.setInOrderCount30Days(0L);
      goods.setIsHot(null);
@@ -188,6 +545,92 @@
      return goods;
   }
   public static JDSearchResult getJingFenGoods(int pageIndex, int eliteId) {
      JDSearchResult searchResult = new JDSearchResult();
      List<JDGoods> list = new ArrayList<>();
      JSONObject json = new JSONObject();
      json.put("pageIndex", pageIndex);
      json.put("pageSize", 20);
      json.put("sortName", "inOrderCount30DaysSku");
      json.put("sort", "desc");
      json.put("eliteId", eliteId);
      JSONObject jsonDTO = new JSONObject();
      jsonDTO.put("goodsReq", json);
      String result = baseRequest2("jd.union.open.goods.jingfen.query", null, jsonDTO);
      System.out.println(result);
      JSONObject resultJson = JSONObject.fromObject(result);
      resultJson = resultJson.optJSONObject("jd_union_open_goods_jingfen_query_response");
      if (resultJson.optInt("code") == 0) {
         result = resultJson.optString("result");
         resultJson = JSONObject.fromObject(result);
         String message = resultJson.optString("message");
         if ("SUCCESS".equalsIgnoreCase(message)) {
            JSONArray array = resultJson.optJSONArray("data");
            if (array != null) {
               for (int i = 0; i < array.size(); i++) {
                  JDGoods goods = parseJDGoods(array.optJSONObject(i));
                  if (goods != null)
                     list.add(goods);
               }
            }
         }
         long totalCount = resultJson.optLong("totalCount");
         PageEntity pageEntity = new PageEntity();
         pageEntity.setTotalCount(totalCount);
         searchResult.setPageEntity(pageEntity);
      }
      searchResult.setGoodsList(list);
      return searchResult;
   }
   public static JDSearchResult getGoodsClass() {
      JDSearchResult searchResult = new JDSearchResult();
      List<JDGoods> list = new ArrayList<>();
      JSONObject json = new JSONObject();
      json.put("parentId", 0);
      json.put("grade", 0);
      JSONObject jsonDTO = new JSONObject();
      jsonDTO.put("req", json);
      String result = baseRequest2("jd.union.open.category.goods.get", null, jsonDTO);
      System.out.println(result);
      JSONObject resultJson = JSONObject.fromObject(result);
      resultJson = resultJson.optJSONObject("jd_union_open_goods_jingfen_query_response");
      if (resultJson.optInt("code") == 0) {
         result = resultJson.optString("result");
         resultJson = JSONObject.fromObject(result);
         String message = resultJson.optString("message");
         if ("SUCCESS".equalsIgnoreCase(message)) {
            JSONArray array = resultJson.optJSONArray("data");
            if (array != null) {
               for (int i = 0; i < array.size(); i++) {
                  JDGoods goods = parseJDGoods(array.optJSONObject(i));
                  if (goods != null)
                     list.add(goods);
               }
            }
         }
         long totalCount = resultJson.optLong("totalCount");
         PageEntity pageEntity = new PageEntity();
         pageEntity.setTotalCount(totalCount);
         searchResult.setPageEntity(pageEntity);
      }
      searchResult.setGoodsList(list);
      return searchResult;
   }
   public static JDGoods getGoodsDetail(Long skuId) {
      List<Long> skuIdList = new ArrayList<>();