From e4a56b57d3e10b9b46eaefe5194bbf98fcc45698 Mon Sep 17 00:00:00 2001
From: yujian <yujian@123.com>
Date: 星期四, 11 七月 2019 18:41:09 +0800
Subject: [PATCH] Merge branch 'master' into div

---
 fanli/src/main/java/com/yeshi/fanli/util/jd/JDApiUtil.java |  293 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 293 insertions(+), 0 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 b0f70d9..768f339 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
@@ -18,7 +18,10 @@
 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.JDShopInfo;
 import com.yeshi.fanli.entity.jd.JDGoods;
 import com.yeshi.fanli.entity.jd.JDOrder;
@@ -67,6 +70,31 @@
 		}
 		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();
+		}
+		return null;
+	}
 
 	private static String getSign(Map<String, String> params) {
 		List<String> list = new ArrayList<>();
@@ -99,6 +127,23 @@
 		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;
+	}
+	
 	/**
 	 * 杞摼
 	 * 
@@ -122,7 +167,254 @@
 		resultJson = JSONObject.fromObject(result);
 		return resultJson.optJSONObject("data").optString("clickURL");
 	}
+	
+	
+	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);
+		List<JDGoods> list = queryByKey(filter);
+		if (list != null && list.size() > 0)
+			return list.get(0);
+		return null;
+	}
+	
+	/**
+	 * 鍏抽敭璇嶅晢鍝佹煡璇㈡帴鍙c�愮敵璇枫��
+	 * @param skuIdList
+	 * @return
+	 */
+	public static List<JDGoods> queryByKey(JDFilter filter) {
+		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.getIsCoupon() != null)
+			json.put("isCoupon", filter.getIsCoupon());
+
+		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());
+		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);
+					}
+				}
+			}
+		}
+		return list;
+	}
+	
+	
+	
+	
+	private static JDGoods parseJDGoods(JSONObject json) {
+		JDGoods goods = new JDGoods();
+		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);
+		BigDecimal price = new BigDecimal(priceInfoJson.optString("price"));
+		goods.setPrice(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);
+		
+		
+		// 浣i噾淇℃伅
+		Object commission = json.get("commissionInfo");
+		JSONObject commissionJson = JSONObject.fromObject(commission);
+		JDCommissionInfo commissionInfo = new JDCommissionInfo();
+		commissionInfo.setCommission(new BigDecimal(commissionJson.optString("commission")));
+		commissionInfo.setCommissionShare(new BigDecimal(commissionJson.optString("commissionShare")));
+		goods.setCommissionInfo(commissionInfo);
+		
+		
+		// 鍒镐俊鎭�
+		Object coupon = json.get("couponInfo");
+		if (coupon != null) {
+			JSONObject couponJson = JSONObject.fromObject(coupon);
+			JSONArray couponArray = couponJson.optJSONArray("couponList");
+			if (couponArray != null) {
+				BigDecimal gap = null;
+				JDCouponInfo couponInfo = null;
+				for (int i = 0; i < couponArray.size(); i++) {
+					boolean add = false;
+					JSONObject jdcoupon = couponArray.optJSONObject(i);
+					if (couponInfo == null) {
+						couponInfo = new JDCouponInfo();
+						add = true;
+					}
+					
+					BigDecimal quota = new BigDecimal(jdcoupon.optString("quota"));
+					BigDecimal sub = MoneyBigDecimalUtil.sub(price, quota);
+					if (gap == null) {
+						gap = sub;
+					} else if (sub.compareTo(gap) == 0) {
+						BigDecimal discount = new BigDecimal(jdcoupon.optString("discount"));
+						if (discount.compareTo(couponInfo.getDiscount()) > 0) {
+							add = true;
+						}
+					} else if (sub.compareTo(gap) > 0) {
+						add = true;
+					}
+					
+					if (add) {
+						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);
+			}
+		}
+
+		
+		// 鍥剧墖淇℃伅
+		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));
+		}
+		
+		// 鎷艰喘淇℃伅
+		Object pinGouInfo = json.get("pinGouInfo");
+		if (pinGouInfo != null) {
+			JSONObject pinGouInfoJson = JSONObject.fromObject(pinGouInfo);
+			if (!pinGouInfoJson.isEmpty()) {
+				JDPingouInfo jdPinGouInfo = new JDPingouInfo();
+				jdPinGouInfo.setPingouPrice(new BigDecimal(pinGouInfoJson.optString("pingouPrice")));
+				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"));
+			}
+		}
+		
+		return goods;
+	}
+
+
+	/**
+	 * 鑾峰彇鎺ㄥ箍鍟嗗搧淇℃伅鎺ュ彛
+	 * @param skuIdList
+	 * @return
+	 */
 	public static List<JDGoods> getGoodsDetail(List<Long> skuIdList) {
 		List<JDGoods> list = new ArrayList<>();
 		JSONObject json = new JSONObject();
@@ -171,6 +463,7 @@
 		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);

--
Gitblit v1.8.0