admin
2019-07-25 09476adf2ae2bcc2f6685dafe4707938fd82bc78
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.yeshi.fanli.util.pinduoduo;
 
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
 
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.yeshi.utils.BigDecimalUtil;
import org.yeshi.utils.HttpUtil;
 
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
 
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 hundred = new BigDecimal(100);
        rate = MoneyBigDecimalUtil.div(rate, hundred);
        BigDecimal price =  MoneyBigDecimalUtil.div(new BigDecimal(goods.getMinGroupPrice()), hundred).setScale(2);
        BigDecimal promotionRate =  MoneyBigDecimalUtil.div(new BigDecimal(goods.getPromotionRate()), new BigDecimal(1000));
        
        Boolean hasCoupon = goods.getHasCoupon();
        if (hasCoupon == null || !hasCoupon) {
            money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(price, promotionRate), rate);
        } else {
            BigDecimal amount =  MoneyBigDecimalUtil.div(new BigDecimal(goods.getCouponDiscount()), hundred);
            BigDecimal startFree =  MoneyBigDecimalUtil.div(new BigDecimal(goods.getCouponMinOrderAmount()), hundred);
            if (startFree.compareTo(price) <= 0 && price.compareTo(amount) > 0) {
                BigDecimal finalPrice = price.subtract(amount);
                money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(finalPrice, promotionRate),rate);
            } else {// 不能用券
                money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(price,promotionRate), rate);
            }
        }
        return BigDecimalUtil.getWithNoZera(money).setScale(2);
    }
 
    
    /**
     * 计算商品券后价,没有券则返回原价
     * 
     * @param goodsBrief
     * @return
     */
    public static BigDecimal getQuanPrice(PDDGoodsDetail goods) {
        BigDecimal hundred = new BigDecimal(100);
        BigDecimal price =  MoneyBigDecimalUtil.div(new BigDecimal(goods.getMinGroupPrice()), hundred);
        Boolean hasCoupon = goods.getHasCoupon();
        if (hasCoupon == null || !hasCoupon) {
            return price.setScale(2);
        }
        
        BigDecimal amount =  MoneyBigDecimalUtil.div(new BigDecimal(goods.getCouponDiscount()), hundred);
        BigDecimal startFree =  MoneyBigDecimalUtil.div(new BigDecimal(goods.getCouponMinOrderAmount()), hundred);
        if (startFree.compareTo(price) <= 0) {
            BigDecimal quanPrice = MoneyBigDecimalUtil.sub(price, amount);
            return quanPrice.setScale(2);
        } else {
            return price.setScale(2);
        }
    }
    
 
    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;
 
    }
    
    
    
    public static List<Long> getRecommendGoodsId(Long id) {
        List<Long> list = new ArrayList<Long>();
            JSONObject params = new JSONObject();
            params.put("pageNo", 1);
            params.put("show_tags", 1);
            params.put("goods_id", id);
            params.put("app_name", "goods_detail");
            params.put("list_id", "goods_detail_HgfiMc");
            params.put("pdduid", StringUtil.Md5(System.currentTimeMillis() + ""));
 
            HttpClient client = new HttpClient();
            PostMethod pm = new PostMethod("https://mobile.yangkeduo.com/proxy/api/api/tesla/query");
            pm.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
            pm.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
            pm.setRequestHeader("Referer", "https://union.jd.com/proManager/index?pageNo=1&keywords=%E9%9E%8B%E5%AD%90");
            pm.setRequestBody(params.toString());
            try {
                client.executeMethod(pm);
                String result = pm.getResponseBodyAsString();
                JSONObject json = JSONObject.fromObject(result);
                JSONArray array = json.optJSONArray("data");
                if (array != null) {
                    for (int i = 0; i < array.size(); i++) {
                        list.add(array.optJSONObject(i).optLong("goods_id"));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
 
        return list;
    }
 
    /**
     * 搜索候选词
     * @param key
     * @return
     */
    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("http://apiv3.yangkeduo.com/search_suggest?query=%s&pdduid=0",
                    URLEncoder.encode(key, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
 
        if (url == null)
            return list;
        try {
            String result = HttpUtil.get(url);
            JSONObject dataJSON = JSONObject.fromObject(result);
            JSONArray array = dataJSON.optJSONArray("suggest_list");
            for (int i = 0; i < array.size(); i++) {
                String sk = array.optJSONObject(i).optJSONObject("item_data").optString("suggestion");
                if (!StringUtil.isNullOrEmpty(sk))
                    list.add(sk);
            }
        } catch (Exception e) {
        }
        return list;
    }
 
}