From 30d8e227e8d823b6c38c3b9c90ac2df03b63befe Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 25 二月 2025 16:41:22 +0800 Subject: [PATCH] 淘宝转链接口更新 --- fanli/src/main/java/com/yeshi/fanli/util/jd/JDApiUtil.java | 1671 +++++++++++++++++++++++++++++++++++----------------------- 1 files changed, 1,002 insertions(+), 669 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 39cb1cf..7048cee 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 @@ -1,669 +1,1002 @@ -package com.yeshi.fanli.util.jd; - -import java.io.UnsupportedEncodingException; -import java.lang.reflect.Type; -import java.math.BigDecimal; -import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -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.entity.jd.JDOrderItem; -import com.yeshi.fanli.tag.PageEntity; -import com.yeshi.fanli.util.MoneyBigDecimalUtil; -import com.yeshi.fanli.util.StringUtil; -import com.yeshi.fanli.util.TimeUtil; - -import net.sf.json.JSONArray; -import net.sf.json.JSONObject; - -public class JDApiUtil { - public static String APP_ID = "1864778416"; - public static String APP_KEY = "388f5133d13a5ea290aa5c44f3ebcc00"; - public static String SECRET_KEY = "e22367fc866746d98b50d47b4c521781"; - 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;// 涓嬪崟鏃堕棿 - public static int ORDER_TYPE_FINISHTIME = 2;// 瀹屾垚鏃堕棿 - public static int ORDER_TYPE_UPDATETIME = 3;// 鏇存柊鏃堕棿 - - 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<>(); - Iterator<String> its = params.keySet().iterator(); - while (its.hasNext()) { - String key = its.next(); - list.add(key + params.get(key)); - } - String str = ""; - Collections.sort(list); - for (String st : list) { - str += st; - } - return StringUtil.Md5(SECRET_KEY + str + SECRET_KEY).toUpperCase(); - } - - 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 positionId, String ext1) { - JSONObject json = new JSONObject(); - json.put("materialId", materialId); - json.put("siteId", APP_ID); - json.put("positionId", positionId); - - if (!StringUtil.isNullOrEmpty(couponUrl)) - json.put("couponUrl", couponUrl); - - if (!StringUtil.isNullOrEmpty(ext1)) - json.put("ext1", ext1); - - JSONObject root = new JSONObject(); - root.put("promotionCodeReq", json); - - String result = baseRequest2("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"); - System.out.println(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 skuIds - * @return - */ - public static List<JDGoods> listGoodsDetail(List<Long> skuIds) { - JDFilter filter = new JDFilter(); - filter.setListId(skuIds); - JDSearchResult result = queryByKey(filter); - return result.getGoodsList(); - } - - /** - * 鍏抽敭璇嶅晢鍝佹煡璇㈡帴鍙c�愮敵璇枫�� - * - * @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(); - - // 浣i噾淇℃伅 - 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 && discount.compareTo(price) <= 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(); - json.put("skuIds", StringUtil.concat(skuIdList, ",")); - String result = baseRequest2("jd.union.open.goods.promotiongoodsinfo.query", null, json); - JSONObject resultJson = JSONObject.fromObject(result); - resultJson = resultJson.optJSONObject("jd_union_open_goods_promotiongoodsinfo_query_response"); - if (resultJson.optInt("code") == 0) { - result = resultJson.optString("result"); - resultJson = JSONObject.fromObject(result); - JSONArray array = resultJson.optJSONArray("data"); - if (array != null) - for (int i = 0; i < array.size(); i++) { - JDGoods goods = parseSimpleJDGoods(array.optJSONObject(i)); - if (goods != null) - list.add(goods); - } - } - return list; - } - - 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")); - categoryInfo.setCid2(json.optLong("cid2")); - categoryInfo.setCid2Name(json.optString("cid2Name")); - categoryInfo.setCid3(json.optLong("cid3")); - categoryInfo.setCid3Name(json.optString("cid3Name")); - - goods.setCategoryInfo(categoryInfo); - goods.setComments(null); - - 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); - goods.setMaterialUrl(json.optString("materialUrl")); - goods.setOwner(null); - goods.setPinGouInfo(null); - - JDShopInfo shopInfo = new JDShopInfo(); - shopInfo.setShopId(json.optInt("shopId")); - shopInfo.setShopName(null); - goods.setShopInfo(shopInfo); - goods.setSkuId(json.optLong("skuId")); - goods.setSkuName(json.optString("goodsName")); - goods.setTotalCount(null); - - 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<>(); - skuIdList.add(skuId); - List<JDGoods> list = getGoodsDetail(skuIdList); - if (list != null && list.size() > 0) - return list.get(0); - return null; - } - - /** - * 鑾峰彇璁㈠崟鍒楄〃 - * - * @param page - * @param pageSize - * @param time - * @param type - * 鏃堕棿绫诲瀷 1-涓嬪崟鏃堕棿 2-瀹屾垚鏃堕棿 3-鏇存柊鏃堕棿 - * @return - */ - public static JDOrderResult getOrderList(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) { - - String date = root.optJSONArray("data").toString(); - Type typeToken = new TypeToken<List<JDOrder>>() { - }.getType(); - List<JDOrder> orderList = new Gson().fromJson(date, typeToken); - if (orderList != null) - for (JDOrder order : orderList) { - Map<Long, List<JDOrderItem>> map = new HashMap<>(); - for (int i = 0; i < order.getOrderItemList().size(); i++) { - JDOrderItem orderItem = order.getOrderItemList().get(i); - if (map.get(orderItem.getSkuId()) == null) - map.put(orderItem.getSkuId(), new ArrayList<>()); - map.get(orderItem.getSkuId()).add(orderItem); - - order.getOrderItemList().get(i) - .setTradeId(order.getOrderId() + "-" + order.getOrderItemList().get(i).getSkuId()); - order.getOrderItemList().get(i).setOrderId(order.getOrderId()); - order.getOrderItemList().get(i).setOrderBy(i + 1); - } - // 鏌ヨ鍟嗗搧ID鐩稿悓鐨勫瓙璁㈠崟 - for (Iterator<Long> its = map.keySet().iterator(); its.hasNext();) { - Long skuId = its.next(); - if (map.get(skuId).size() > 1) { - int skuOrderBy = 1; - for (int i = 0; i < order.getOrderItemList().size(); i++) { - JDOrderItem orderItem = order.getOrderItemList().get(i); - if (orderItem.getSkuId().longValue() == skuId) { - order.getOrderItemList().get(i).setTradeId(order.getOrderId() + "-" - + order.getOrderItemList().get(i).getSkuId() + "-" + skuOrderBy++); - } - } - } - } - - } - return new JDOrderResult(hasMore, orderList); - } - } - return null; - } - -} +package com.yeshi.fanli.util.jd; + +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Type; +import java.math.BigDecimal; +import java.net.URLEncoder; +import java.util.*; + +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.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; + +import net.sf.json.JSONArray; +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 = "270eefea04d44e33b5c21511aa857458"; + private static String SERVER_URL = "https://router.jd.com/api"; + + // 璁㈠崟鏌ヨ绫诲瀷 + public static int ORDER_TYPE_CREATETIME = 1;// 涓嬪崟鏃堕棿 + public static int ORDER_TYPE_FINISHTIME = 2;// 瀹屾垚鏃堕棿 + public static int ORDER_TYPE_UPDATETIME = 3;// 鏇存柊鏃堕棿 + + 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<>(); + Iterator<String> its = params.keySet().iterator(); + while (its.hasNext()) { + String key = its.next(); + list.add(key + params.get(key)); + } + String str = ""; + Collections.sort(list); + for (String st : list) { + str += st; + } + return StringUtil.Md5(SECRET_KEY + str + SECRET_KEY).toUpperCase(); + } + + 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 convertLink1(String materialId, String couponUrl, String positionId, String ext1, String giftCouponKey) { + JSONObject json = new JSONObject(); + json.put("materialId", materialId); + json.put("siteId", APP_ID); + json.put("positionId", positionId); + + if (!StringUtil.isNullOrEmpty(couponUrl)) + json.put("couponUrl", couponUrl); + + if (!StringUtil.isNullOrEmpty(ext1)) + json.put("ext1", ext1); + if (!StringUtil.isNullOrEmpty(giftCouponKey)) + json.put("giftCouponKey", giftCouponKey); + + JSONObject root = new JSONObject(); + root.put("promotionCodeReq", json); + + String result = baseRequest2("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"); + System.out.println(result); + if (result == null) { + return null; + } + + resultJson = JSONObject.fromObject(result); + return resultJson.optJSONObject("data").optString("clickURL"); + } + + public static String convertLinkWithSubUnionId(String materialId, String couponUrl, String giftCouponKey, String positionId, + String subUnionId) throws Exception { + String log = "浜笢杞摼鍑洪敊:"; + JSONObject json = new JSONObject(); + + 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(subUnionId)) + json.put("subUnionId", subUnionId); + + // 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); + + 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"); + } + + public static JDGoods queryGoodsDetail(String itemId) { + List<String> itemIdList = new ArrayList<>(); + itemIdList.add(itemId); + JDFilter filter = new JDFilter(); + filter.setPageIndex(1); + filter.setPageSize(20); + filter.setListItemId(itemIdList); + JDSearchResult searchResult = queryByKey(filter); + if (searchResult != null) { + List<JDGoods> list = searchResult.getGoodsList(); + if (list != null && list.size() > 0) + return list.get(0); + } + return null; + } + + public static List<JDGoods> queryGoodsDetail(List<String> itemIdList) { + JDFilter filter = new JDFilter(); + filter.setPageIndex(1); + filter.setPageSize(20); + filter.setListItemId(itemIdList); + JDSearchResult searchResult = queryByKey(filter); + if (searchResult != null) { + List<JDGoods> list = searchResult.getGoodsList(); + if (list != null && list.size() > 0) + return list; + } + return null; + } + + /** + * 閫氳繃鍟嗗搧鎼滅储鎺ュ彛鎵归噺鑾峰彇璇︽儏 + * + * @param itemIds + * @return + */ + public static List<JDGoods> listGoodsDetail(List<String> itemIds) { + JDFilter filter = new JDFilter(); + filter.setListItemId(itemIds); + JDSearchResult result = queryByKey(filter); + return result.getGoodsList(); + } + + /** + * 鍏抽敭璇嶅晢鍝佹煡璇㈡帴鍙c�愮敵璇枫�� + * + * @param filter + * @return + */ + public static JDSearchResult queryByKey(JDFilter filter) { + logger.info(String.format("queryByKey:%s %s", filter.getKeyword(), StringUtil.concat(filter.getListItemId(), ","))); + + JDSearchResult searchResult = new JDSearchResult(); + + List<JDGoods> list = new ArrayList<>(); + JSONObject json = new JSONObject(); + json.put("pageIndex", filter.getPageIndex()); + json.put("pageSize", filter.getPageSize()); +// json.put("sceneId", 1); + + + 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.getListItemId() != null) + json.put("itemIds", new Gson().toJson(filter.getListItemId())); + + 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()); + String result = ""; + + + JSONObject jsonDTO = new JSONObject(); + jsonDTO.put("goodsReqDTO", json); + 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 != null && 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"); + System.out.println(array.toString()); + 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); + } 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; + } + + /** + * 瑙f瀽浜笢鐨勫埜淇℃伅 + * + * @param json + * @return + */ + private static JDCouponInfo parseJDCouponInfo(JSONObject json) { + JDCouponInfo couponInfo = new Gson().fromJson(json.toString(), JDCouponInfo.class); + return couponInfo; + } + + /** + * 瑙f瀽浜笢鍟嗗搧淇℃伅 + * + * @param json + * @return + */ + + private static JDGoods parseJDGoods(JSONObject json) { + JDGoods goods = new JDGoods(); + + // 浣i噾淇℃伅 + 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.setItemId(json.optString("itemId")); + 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.setSpuid(json.optLong("spuid")); + 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) { + 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; + } + + + 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; + } + + + /** + * 鑾峰彇鎺ㄥ箍鍟嗗搧淇℃伅鎺ュ彛 + * + * @param itemIdList + * @return + */ + public static List<JDGoods> getGoodsDetail(List<String> itemIdList) { + List<JDGoods> list = new ArrayList<>(); + JSONObject json = new JSONObject(); + json.put("itemIds", StringUtil.concat(itemIdList, ",")); + 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) { + result = resultJson.optString("result"); + resultJson = JSONObject.fromObject(result); + JSONArray array = resultJson.optJSONArray("data"); + if (array != null) + for (int i = 0; i < array.size(); i++) { + JDGoods goods = parseSimpleJDGoods(array.optJSONObject(i)); + if (goods != null) + list.add(goods); + } + } + return list; + } + + 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")); + categoryInfo.setCid2(json.optLong("cid2")); + categoryInfo.setCid2Name(json.optString("cid2Name")); + categoryInfo.setCid3(json.optLong("cid3")); + categoryInfo.setCid3Name(json.optString("cid3Name")); + + goods.setCategoryInfo(categoryInfo); + goods.setComments(null); + + String isFreeShipping = json.optString("isFreeShipping"); + if (!StringUtil.isNullOrEmpty(isFreeShipping)) { + goods.setIsFreeShipping(Integer.parseInt(isFreeShipping)); + } + String inOrderCount = json.optString("inOrderCount"); + if (!StringUtil.isNullOrEmpty(isFreeShipping)) { + goods.setInOrderCount30Days(Long.parseLong(inOrderCount)); + } + + goods.setGoodCommentsShare(null); + List<String> imageList = new ArrayList<>(); + imageList.add(json.optString("imgUrl")); + goods.setPicUrl(json.optString("imgUrl")); + goods.setImageList(imageList); + goods.setIsHot(null); + goods.setMaterialUrl(json.optString("materialUrl")); + goods.setOwner(null); + goods.setPinGouInfo(null); + + JDShopInfo shopInfo = new JDShopInfo(); + shopInfo.setShopId(json.optInt("shopId")); + shopInfo.setShopName(null); + goods.setShopInfo(shopInfo); + goods.setSkuId(json.optLong("skuId")); + goods.setSkuName(json.optString("goodsName")); + goods.setTotalCount(null); + + 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); + LogHelper.test(result); + 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 List<GoodsClass> getGoodsClass(int parentId, int grade) { + JDSearchResult searchResult = new JDSearchResult(); + List<GoodsClass> list = new ArrayList<>(); + JSONObject json = new JSONObject(); + json.put("parentId", parentId); + json.put("grade", grade); + + 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_category_goods_get_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++) { + GoodsClass gc = new GoodsClass(); + JSONObject item = array.optJSONObject(i); + gc.setName(item.optString("name")); + gc.setId(item.optLong("id")); + list.add(gc); + } + } + } + + long totalCount = resultJson.optLong("totalCount"); + PageEntity pageEntity = new PageEntity(); + pageEntity.setTotalCount(totalCount); + searchResult.setPageEntity(pageEntity); + } + return list; + } + + public static JDGoods getGoodsDetail(String itemId) { + List<String> skuIdList = new ArrayList<>(); + skuIdList.add(itemId); + List<JDGoods> list = getGoodsDetail(skuIdList); + if (list != null && list.size() > 0) + return list.get(0); + return null; + } + + /** + * 鑾峰彇璁㈠崟鍒楄〃 + * + * @param page + * @param pageSize + * @param time + * @param type 鏃堕棿绫诲瀷 1-涓嬪崟鏃堕棿 2-瀹屾垚鏃堕棿 3-鏇存柊鏃堕棿 + * @return + */ + public static JDOrderResult getOrderList(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) { + + JSONArray data = root.optJSONArray("data"); + Type typeToken = new TypeToken<List<JDOrder>>() { + }.getType(); + List<JDOrder> orderList = new Gson().fromJson(data.toString(), typeToken); + if (orderList != null) + for (JDOrder order : orderList) { + Map<String, List<JDOrderItem>> map = new HashMap<>(); + for (int i = 0; i < order.getOrderItemList().size(); i++) { + JDOrderItem orderItem = order.getOrderItemList().get(i); + String goodsId = getGoodsId(orderItem, order.getOrderItemList().size()); + if (map.get(goodsId) == null) + map.put(goodsId, new ArrayList<>()); + map.get(goodsId).add(orderItem); + + order.getOrderItemList().get(i) + .setTradeId(order.getOrderId() + "-" + getGoodsId(order.getOrderItemList().get(i),order.getOrderItemList().size())); + order.getOrderItemList().get(i).setOrderId(order.getOrderId()); + order.getOrderItemList().get(i).setOrderBy(i + 1); + } + // 鏌ヨ鍟嗗搧ID鐩稿悓鐨勫瓙璁㈠崟 + for (Iterator<String> its = map.keySet().iterator(); its.hasNext(); ) { + String goodsId = its.next(); + if (map.get(goodsId).size() > 1) { + int skuOrderBy = 1; + for (int i = 0; i < order.getOrderItemList().size(); i++) { + JDOrderItem orderItem = order.getOrderItemList().get(i); + if (getGoodsId(orderItem,order.getOrderItemList().size()).equalsIgnoreCase(goodsId)) { + order.getOrderItemList().get(i).setTradeId(order.getOrderId() + "-" + + getGoodsId(order.getOrderItemList().get(i), order.getOrderItemList().size()) + "-" + skuOrderBy++); + } + } + } + } + + } + return new JDOrderResult(hasMore, orderList); + } + } + return null; + } + + private static String getGoodsId(JDOrderItem item, int itemCount) { + if (itemCount <= 1) { + return item.getSkuId() + ""; + } + if (item.getSkuId() != null) { + return item.getSkuId() + ""; + } + return item.getItemId().split("_")[1]; + } + + + 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; + } + + + public static String createLiJin(String sku) { + JSONObject json = new JSONObject(); + JSONObject couponReq = new JSONObject(); + couponReq.put("skuMaterialId", sku); + couponReq.put("discount", 10.01); + couponReq.put("amount", 5); + couponReq.put("receiveStartTime", TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd HH")); + couponReq.put("receiveEndTime", TimeUtil.getGernalTime(System.currentTimeMillis() + 1000 * 60 * 2, "yyyy-MM-dd HH")); + couponReq.put("isSpu", 0); + couponReq.put("expireType", 2); + couponReq.put("useStartTime", TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd")); + couponReq.put("useEndTime", TimeUtil.getGernalTime(System.currentTimeMillis() + 1000 * 60 * 60L * 24, "yyyy-MM-dd")); + couponReq.put("share", 1); + couponReq.put("contentMatch", 0); + + json.put("couponReq", couponReq); + String result = baseRequest2("jd.union.open.coupon.gift.get", null, json); + System.out.println(result); + JSONObject root = JSONObject.fromObject(result).optJSONObject("jd_union_open_coupon_gift_get_response"); + if (root.optInt("code") == 0) { + boolean hasMore = root.optBoolean("hasMore"); + root = JSONObject.fromObject(root.optString("result")); + if (root.optInt("code") == 200 && root.optJSONObject("data") != null) { + JSONObject data = root.optJSONObject("data"); + String giftCouponKey = data.optString("giftCouponKey"); + return giftCouponKey; + } + } + return null; + } + + + public static void toolConvert() { + JSONObject json = new JSONObject(); + JSONObject couponReq = new JSONObject(); + couponReq.put("materialId", "https://item.jd.com/71751967284.html"); + couponReq.put("unionId", "2011217145"); +// couponReq.put("pid", "2011217145_4100059717_3003005722"); + couponReq.put("subUnionId", "123_437032"); + couponReq.put("giftCouponKey", "123123123123"); + json.put("promotionCodeReq", couponReq); + String result = baseRequest2("jd.union.open.promotion.byunionid.get", null, json); + System.out.println(result); + } + + public static void toolOrder() { + JSONObject json = new JSONObject(); + JSONObject couponReq = new JSONObject(); + couponReq.put("pageIndex", 1); + couponReq.put("pageSize", 20); + couponReq.put("type", 3); + couponReq.put("startTime", "2020-10-31 16:00:00"); + couponReq.put("endTime", "2020-10-31 16:55:00"); +// couponReq.put("key", "026eea846250e0dcbcacd750faf07de0"); + + +// couponReq.put("pid", "2011217145_4100059717_3003005722"); + couponReq.put("childUnionId", "2011217145"); + json.put("orderReq", couponReq); + String result = baseRequest2("jd.union.open.order.row.query", null, json); + System.out.println(result); + } + + + public static void test() { + JSONObject json = new JSONObject(); + json.put("materialId", "https://item.jd.com/32376790478.html"); + json.put("ext1", "437032"); + JSONObject root = new JSONObject(); + root.put("promotionCodeReq", json); + String result = baseRequest2("jd.union.open.promotion.bysubunionid.get", null, root); + System.out.println(result); + } + + public static void main(String[] args) { + Date startTime = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24L); + // 涓�涓皬鏃朵负涓�涓闀� + List<JDOrder> tempOrderList = new ArrayList<>(); + for (int i = 0; i < 24; i++) { + Date da = new Date(startTime.getTime() + 1000 * 60 * 60L * i); + if (da.getTime() >System.currentTimeMillis()) + break; + + + int pageSize = 200; + int page = 1; + JDOrderResult result = JDApiUtil.getOrderList(page++, pageSize, da, JDApiUtil.ORDER_TYPE_UPDATETIME); + if (result != null) { + tempOrderList.addAll(result.getOrderList()); + while (result != null && result.isHasMore()) { + result = JDApiUtil.getOrderList(page++, pageSize, da, JDApiUtil.ORDER_TYPE_UPDATETIME); + if (result != null) + tempOrderList.addAll(result.getOrderList()); + } + } + } + System.out.println(tempOrderList.size()); + } + +} -- Gitblit v1.8.0