From 8222e8b237c5dfb0affcdc03335bfadc06403bf8 Mon Sep 17 00:00:00 2001 From: yujian <yujian@123.com> Date: 星期五, 19 七月 2019 15:49:39 +0800 Subject: [PATCH] 奖励券使用优化类型 --- fanli/src/main/java/com/yeshi/fanli/util/jd/JDUtil.java | 270 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 257 insertions(+), 13 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/util/jd/JDUtil.java b/fanli/src/main/java/com/yeshi/fanli/util/jd/JDUtil.java index a49fd1b..b443d07 100644 --- a/fanli/src/main/java/com/yeshi/fanli/util/jd/JDUtil.java +++ b/fanli/src/main/java/com/yeshi/fanli/util/jd/JDUtil.java @@ -1,7 +1,12 @@ package com.yeshi.fanli.util.jd; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.math.BigDecimal; +import java.net.URLEncoder; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.commons.httpclient.HttpClient; @@ -10,10 +15,20 @@ import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import org.yeshi.utils.BigDecimalUtil; import org.yeshi.utils.HttpUtil; +import com.yeshi.fanli.dto.jd.JDCommissionInfo; +import com.yeshi.fanli.dto.jd.JDCouponInfo; +import com.yeshi.fanli.dto.jd.JDSearchFilter; +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.tag.PageEntity; +import com.yeshi.fanli.util.MoneyBigDecimalUtil; import com.yeshi.fanli.util.StringUtil; +import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JDUtil { @@ -86,23 +101,61 @@ } @SuppressWarnings("deprecation") - public static void searchByKey(String key) { + public static JDSearchResult searchByKey(JDSearchFilter searchFilter) { + + JDSearchResult searchResult = new JDSearchResult(); + + List<JDGoods> goodsList = new ArrayList<>(); JSONObject params = new JSONObject(); - params.put("pageNo", 1); - params.put("pageSize", 20); + params.put("pageNo", searchFilter.getPageNo()); + params.put("pageSize", searchFilter.getPageSize()); params.put("searchUUID", StringUtil.Md5(System.currentTimeMillis() + "")); + JSONObject data = new JSONObject(); - data.put("deliveryType", "0"); - data.put("hasCoupon", "0"); - data.put("isCare", "0"); - data.put("isPinGou", "0"); - data.put("isZY", "0"); - data.put("key", key); - data.put("keywordType", "kt1"); - data.put("lock", ""); - data.put("orientationFlag", ""); - data.put("searchType", "st1"); + data.put("lock", "0"); + data.put("deliveryType", searchFilter.getDeliveryType()); + data.put("hasCoupon", searchFilter.getHasCoupon()); + data.put("isCare", searchFilter.getIsCare()); + data.put("isPinGou", searchFilter.getIsPinGou()); + data.put("isZY", searchFilter.getIsZY()); + data.put("orientationFlag", searchFilter.getOrientationFlag()); + + // 鍒嗙被 + data.put("categoryId", searchFilter.getCategoryId()); + data.put("cat2Id", searchFilter.getCat2Id()); + data.put("cat3Id", searchFilter.getCat3Id()); + + data.put("fromCommissionRatio", searchFilter.getFromCommissionRatio()); + data.put("toCommissionRatio", searchFilter.getToCommissionRatio()); + data.put("fromPrice", searchFilter.getFromPrice()); + data.put("toPrice", searchFilter.getToPrice()); + + String key = searchFilter.getKey(); + if (StringUtil.isNullOrEmpty(key)) { + data.put("keywordType", "kt0"); + data.put("searchType", "st3"); + } else { + data.put("key", key); + data.put("keywordType", "kt1"); + data.put("searchType", "st3"); + } + + String sortName = searchFilter.getSortName(); + if (StringUtil.isNullOrEmpty(sortName)) { + data.put("sort", null); + data.put("sortName", null); + } else { + data.put("sortName", sortName); + String sort = searchFilter.getSort(); + if (StringUtil.isNullOrEmpty(sort)) { + sort = JDSearchFilter.SORT_DESC; + } + data.put("sort", sort); + } params.put("data", data); + + System.out.println(params.toString()); + HttpClient client = new HttpClient(); PostMethod pm = new PostMethod("https://union.jd.com/api/goods/search"); pm.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko"); @@ -112,10 +165,201 @@ try { client.executeMethod(pm); String result = pm.getResponseBodyAsString(); + JSONObject json = JSONObject.fromObject(result); + if (json.optInt("code") == 200) { + JSONObject pageJson = json.optJSONObject("data").optJSONObject("page"); + PageEntity entity = new PageEntity(pageJson.optInt("pageNo"), pageJson.optInt("pageSize"), + pageJson.optInt("totalCount")); + searchResult.setPageEntity(entity); + + if (json.optJSONObject("data") != null) { + JSONArray array = json.optJSONObject("data").optJSONArray("unionGoods"); + if (array != null) { + for (int i = 0; i < array.size(); i++) { + goodsList.add(parseGoods(array.optJSONArray(i).optJSONObject(0).toString())); + } + } + } + json.optJSONObject("data").optJSONArray("unionGoods"); + } + System.out.println(result); } catch (Exception e) { e.printStackTrace(); } + + searchResult.setGoodsList(goodsList); + return searchResult; + } + + public static JDGoods getGoodsDetail(Long skuId) { + JDSearchFilter sf = new JDSearchFilter(); + sf.setKey(skuId + ""); + sf.setPageNo(1); + sf.setPageSize(20); + JDSearchResult result = searchByKey(sf); + if (result != null && result.getGoodsList() != null && result.getGoodsList().size() > 0) + return result.getGoodsList().get(0); + return null; + } + + private static JDGoods parseGoods(String data) { + JDGoods goods = new JDGoods(); + JSONObject json = JSONObject.fromObject(data); + goods.setSkuId(json.optLong("skuId")); + goods.setSkuName(json.optString("skuName")); + + + JDShopInfo shopInfo = new JDShopInfo(); + shopInfo.setShopId(json.optInt("shopId")); + shopInfo.setShopName(json.optString("shopName")); + goods.setShopInfo(shopInfo); + + if (!StringUtil.isNullOrEmpty(json.optString("wlPrice"))) + goods.setPrice(new BigDecimal(json.optString("wlPrice"))); + else + goods.setPrice(new BigDecimal(json.optString("finalPrice"))); + + goods.setInOrderCount30Days(json.optLong("inOrderCount30Days")); + + if (json.optInt("isZY") == 1) + goods.setOwner("g"); + else + goods.setOwner("p"); + + goods.setMaterialUrl("https://item.jd.com/"+ json.optLong("skuId") +".html"); + goods.setPicUrl("http://img14.360buyimg.com/n1/" + json.optString("materialUrl")); + JDCommissionInfo commission = new JDCommissionInfo(); + commission.setCommission(new BigDecimal(json.optString("wlCommission"))); + commission.setCommissionShare(new BigDecimal(json.optString("wlCommissionRatio"))); + goods.setCommissionInfo(commission); + goods.setGoodCommentsShare(new BigDecimal(json.optString("goodCommentsShare"))); + + if (json.optString("hasCoupon") != null && json.optInt("hasCoupon") == 1 + && json.optInt("couponRemainCnt") > 0) { + // 鏈夊埜 + JDCouponInfo coupon = new JDCouponInfo(); + coupon.setDiscount(new BigDecimal(json.optString("couponDiscount"))); + coupon.setQuota(new BigDecimal(json.optString("couponQuota"))); + coupon.setLink("https:" + json.optString("couponLink")); + goods.setCouponInfo(coupon); + } else { + // 鏃犲埜 + } + return goods; + } + + + /** + * 鍟嗗搧浣i噾璁$畻 + * @param goods + * @param rate + * @return + */ + public static BigDecimal getGoodsFanLiMoney(JDGoods goods, BigDecimal rate) { + BigDecimal money = null; + + JDCommissionInfo commissionInfo = goods.getCommissionInfo(); + if (commissionInfo == null) { + return money; + } + + JDCouponInfo couponInfo = goods.getCouponInfo(); + if (couponInfo == null) { + // 浣i噾 + BigDecimal commission = commissionInfo.getCommission(); + return MoneyBigDecimalUtil.mul(commission, MoneyBigDecimalUtil.div(rate, new BigDecimal(100))); + } else { + BigDecimal price = goods.getPrice(); + BigDecimal quota = couponInfo.getQuota(); + BigDecimal discount = couponInfo.getDiscount(); + if (quota.compareTo(price) <= 0 && price.compareTo(discount) > 0) { + + BigDecimal finalPrice = price.subtract(discount); + money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil + .mul(MoneyBigDecimalUtil.mul(finalPrice, commissionInfo.getCommissionShare()), new BigDecimal("0.01")), + MoneyBigDecimalUtil.div(rate, new BigDecimal(100))); + + } else {// 涓嶈兘鐢ㄥ埜 + money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul( + MoneyBigDecimalUtil.mul(price,commissionInfo.getCommissionShare()), + new BigDecimal("0.01")), MoneyBigDecimalUtil.div(rate, new BigDecimal(100))); + } + + } + return BigDecimalUtil.getWithNoZera(money); + } + + + /** + * 璁$畻鍟嗗搧鍒稿悗浠凤紝娌℃湁鍒稿垯杩斿洖鍘熶环 + * + * @param goodsBrief + * @return + */ + public static BigDecimal getQuanPrice(JDGoods jdGoods) { + JDCouponInfo couponInfo = jdGoods.getCouponInfo(); + if (couponInfo == null) { + return jdGoods.getPrice(); + } + + BigDecimal discount = couponInfo.getDiscount(); + BigDecimal price = jdGoods.getPrice(); + // 璁$畻鍒稿悗浠� + BigDecimal sub = price.subtract(couponInfo.getQuota()); + if (sub.compareTo(BigDecimal.valueOf(0)) >= 0) { + BigDecimal quanPrice = MoneyBigDecimalUtil.sub(price, discount); + return quanPrice; + } else { + return jdGoods.getPrice(); + } + } + + + /** + * 閿�閲忓鐞� + * @param count + * @return + */ + public static String getSaleCount(long count) { + String salesCountMidea = null; + if (count < 10000) { + salesCountMidea = count + ""; + } else { + double sales = count; + salesCountMidea = String.format("%.1f", sales / 10000); + salesCountMidea = salesCountMidea + "涓�"; + } + return salesCountMidea; + } + + public static List<String> suggestSearch(String key) { + List<String> list = new ArrayList<>(); + if (StringUtil.isNullOrEmpty(key)) + return list; + String url = null; + try { + url = String.format( + "https://wq.jd.com/bases/searchdropdown/getdropdown?terminal=m&zip=1&key=%s&newjson=1&_=%s&sceneval=2", + URLEncoder.encode(key, "UTF-8"), System.currentTimeMillis() + ""); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + if (url == null) + return list; + try { + String result = HttpUtil.get(url); + result = result.replace("callback(", "").replace(")", ""); + JSONArray array = JSONArray.fromObject(result); + for (int i = 0; i < array.size(); i++) { + String sk = array.optJSONObject(i).optString("key"); + if (!StringUtil.isNullOrEmpty(sk)) + list.add(sk); + } + } catch (Exception e) { + } + return list; } } -- Gitblit v1.8.0