package com.yeshi.fanli.util.pinduoduo;
|
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import javax.script.Invocable;
|
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngineManager;
|
|
import org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.jsoup.select.Elements;
|
import org.yeshi.utils.BigDecimalUtil;
|
|
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
|
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
|
public class PinDuoDuoUtil {
|
|
|
/**
|
* 商品佣金计算
|
* @param goods
|
* @param rate
|
* @return
|
*/
|
public static BigDecimal getGoodsFanLiMoney(PDDGoodsDetail goods, BigDecimal rate) {
|
BigDecimal money = null;
|
BigDecimal price = new BigDecimal(goods.getMinNormalPrice());
|
BigDecimal promotionRate = new BigDecimal(goods.getPromotionRate());
|
|
Boolean hasCoupon = goods.getHasCoupon();
|
if (hasCoupon == null || !hasCoupon) {
|
money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(
|
MoneyBigDecimalUtil.mul(price, promotionRate),new BigDecimal("0.001")),
|
MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
|
} else {
|
BigDecimal amount = new BigDecimal(goods.getCouponDiscount());
|
BigDecimal startFree = new BigDecimal(goods.getCouponMinOrderAmount());
|
if (startFree.compareTo(price) <= 0 && price.compareTo(amount) > 0) {
|
BigDecimal finalPrice = price.subtract(amount);
|
money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil
|
.mul(MoneyBigDecimalUtil.mul(finalPrice, promotionRate), new BigDecimal("0.001")),
|
MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
|
|
} else {// 不能用券
|
money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(
|
MoneyBigDecimalUtil.mul(price,promotionRate), new BigDecimal("0.001")),
|
MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
|
}
|
}
|
return BigDecimalUtil.getWithNoZera(money);
|
}
|
|
|
/**
|
* 计算商品券后价,没有券则返回原价
|
*
|
* @param goodsBrief
|
* @return
|
*/
|
public static BigDecimal getQuanPrice(PDDGoodsDetail goods) {
|
BigDecimal price = new BigDecimal(goods.getMinNormalPrice());
|
Boolean hasCoupon = goods.getHasCoupon();
|
if (hasCoupon == null || !hasCoupon) {
|
return price;
|
}
|
|
BigDecimal amount = new BigDecimal(goods.getCouponDiscount());
|
BigDecimal startFree = new BigDecimal(goods.getCouponMinOrderAmount());
|
if (startFree.compareTo(price) <= 0) {
|
BigDecimal quanPrice = MoneyBigDecimalUtil.sub(price, amount);
|
return quanPrice;
|
} else {
|
return price;
|
}
|
}
|
|
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> getDetailImages(Long id) {
|
List<String> imgList = new ArrayList<>();
|
try {
|
Document doc = Jsoup.connect("http://yangkeduo.com/goods.html?goods_id="+id)
|
.userAgent(
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36")
|
.get();
|
Elements els = doc.getElementsByTag("script");
|
for (int i = 0; i < els.size(); i++) {
|
if (els.get(i).html().contains("window.rawData")) {
|
String dataJS = els.get(i).html().replace("window.", "var ");
|
dataJS += "function getData(){return JSON.stringify(rawData);}";
|
|
ScriptEngineManager manager = new ScriptEngineManager();
|
ScriptEngine engine = manager.getEngineByName("javascript");
|
try {
|
engine.eval(dataJS);
|
if (engine instanceof Invocable) {
|
Invocable in = (Invocable) engine;
|
String jsonStr = in.invokeFunction("getData").toString();
|
JSONObject json = JSONObject.fromObject(jsonStr);
|
JSONArray array = json.optJSONObject("store").optJSONObject("initDataObj")
|
.optJSONObject("goods").optJSONArray("detailGallery");
|
for (int j = 0; j < array.size(); j++) {
|
imgList.add("http:" + array.optJSONObject(j).optString("url"));
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
break;
|
}
|
}
|
} catch (Exception e1) {
|
e1.printStackTrace();
|
}
|
|
return imgList;
|
|
}
|
|
}
|