admin
2021-06-11 bc9d4bf3fddcb3107f8509159a250f7a5a5f8ae5
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
package com.yeshi.fanli.util.jd;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
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.Connection;
import org.jsoup.Jsoup;
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 org.yeshi.utils.NumberUtil;
 
import com.yeshi.fanli.dto.GoodsClassDTO;
import com.yeshi.fanli.dto.jd.JDCommissionInfo;
import com.yeshi.fanli.dto.jd.JDCouponInfo;
import com.yeshi.fanli.dto.jd.JDPingouInfo;
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.log.LogHelper;
import com.yeshi.common.entity.PageEntity;
import org.yeshi.utils.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class JDUtil {
 
    public static final String TM_PHONE_URL = "https://item.m.jd.com/product/%s.html";
 
    public void test() {
        Map<String, String> systemParams = new HashMap<>();
        systemParams.put("method", "");
        systemParams.put("access_token", "");
        systemParams.put("app_key", "");
        systemParams.put("timestamp", "");
        systemParams.put("sign", "");
    }
 
    public void test2() {
        try {
            Document doc = Jsoup.connect("https://media.jd.com/gotoadv/goods?pageSize=50")
                    .header("Cookie",
                            "user-key=35f673a3-8deb-4db1-90bf-43d2b685b870; cn=0; ipLocation=%u91CD%u5E86; ipLoc-djd=4-51026-103-0; _jrda=1; 3AB9D23F7A4B3C9B=LYNZAJWVCG3JB2Y2DAFLL7AU7LJEKYHJJVLNSRH6XTXA6BG5LOLHDS7DGMOLFAGSC7IR522A23USQUITRAJPNEJG6Y; PCSYCityID=4; __jdu=525081455; pinId=KulFKBzJkdU; _tp=ixPyXPOm1mi0q7RQ93P3dvT9y0vNqhMLigKbC%2FovsFI%3D; _pst=%E5%B8%83%E4%B8%B8; ssid=\"mj3oPLd+SUW8dlOtEKP2Dg==\"; TrackID=1qW8pF973UBR96X_tYTsSeUzJpzCyFhzt9sA3a6Mmq9ztdVWo5vBXEpT5JLCT0cmznSYeBNsCxWDFBLLGPhICNH9_RVURX7r0ud7IxzKWt6w; pin=%E5%B8%83%E4%B8%B8; unick=%E5%B8%83%E4%B8%B8lee; ceshi3.com=000; unpl=V2_ZzNtbUYESkZ3CBFRL05aA2IHRQlLAEtBdwwTA3xMVFc3UBVcclRCFXwUR1ZnGFgUZwQZXkRcQBBFCENkexhdBGcDEVRKXnMldDhFVEsZbAFkBxNUQlJHEUU4QWR4GFwEbgAUXXKDyqeggc9kfRFVAW4EEVxGZ0IldQhOUHkQXwdvAyIPLI%2f1o62t90ivs9PQ75RfWUFTQhx1DUJQSxpdBWYKEVtCZ0A%3d; CCC_SE=ADC_AyccHFydhcYX3HBT1BSvsyipO8wTsbjSSsujA2jAK5w%2bm8Iw5Gtega%2fRtr3slwp4KzjI1sa3HD3nE9tum3OdhoBJ8WATI2GH%2bvbd7pwRdITE5P9e0O3KGdseb5exhIOQLJyA93d7oYLtirch7zucDeGuAnElGVxmftsByoqKA%2f0cLBXMvKm2amPVmroXA0ZWbXsx3fANkJA9cRgeOOlrsvTjsE8uPxTnRExR6IqNFiWXWXweJ2YD0%2bC826PxMVoXbStGvcs9XLcE7hrDJgZSvXmgJyMOLDkD4Fnwkt5dgJ6d2DrF89eSexsEicNzheQZe5l%2fA8XijdVruEk1dPTlRme3o4ccLKuO8Tgrcuamh232JjY4ROYEkXrpRMZbvbNBBmBFN%2bVYgzuXzlPDwiwgRCtGgEN%2fWdbLJTxaJWAS3AuAH2eZ0%2beCG4ZdC0Hwk3S6cefylnLDL0cvQp78Uv%2fty6c9KYxKLzohxBPcu8YYfiwnHz%2fb4V2Ou8GX8myp6iAZ; areaId=4; showNoticeWindow=false; assist=LGXT4ZUUMHZXTL3LU6SHBXHIRZLI6OXJQWZEEYHFZIWHLDNNBDTCNH3Q4XXGSXEMN7NE6HJKSX7HI43OROSTWQXGAC6CJKJAW6CNTU3DIFRBQ7IAX22A74W2JHBTDLWPYNWRIL24YXC2ZGEVTP53CLVCZ73NS5ZOZ74Z5FQSBVDOU6I6APXRILVG5MCYPSLUYUT2WXRPWFZZKHAXGAWASAHGPI; isvName=%E5%B8%83%E4%B8%B8; masterClose=yes; thor=A13BE3D5245C7CE481E9909D3D6A771DC2FCAFB44DF9FD097DDFAD8F40A3B03D20FCD92E6728B2BB5512DCEABCBA620E2CFA885DBA2B4ECDD4ACCA0B6B1E1385C2D293F5F314639621BFE86734829975B7446B875B5F50055F10D1DDB48C16C166F0F33ECDB6036EA53BFC8BCAF98978A7E4E6AB5444C31D973161E1E0EE9556; __jdv=108460702|baidu|-|organic|not set|1520243688557; __jda=108460702.525081455.1480070037.1520238446.1520243689.43; __jdc=108460702; __jdb=108460702.4.525081455|43.1520243689")
                    .header("Referer", "https://media.jd.com/index/overview").header("Upgrade-Insecure-Requests", "1")
                    .header("User-Agent",
                            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36")
                    .get();
            Elements els = doc.getElementsByClass("product-list-b").get(0).getElementsByTag("ul").get(0)
                    .getElementsByTag("li");
            for (int i = 0; i < els.size(); i++) {
                System.out.println(els.get(0).attr("skuid"));
                getGoodsDetail("https://item.jd.com/" + els.get(i).attr("skuid") + ".html");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
    public void getGoodsDetail(String url) {
        try {
            Document doc = Jsoup.connect(url).get();
 
            String skuName = doc.getElementsByClass("itemInfo-wrap").get(0).getElementsByClass("sku-name").text();
            Element els = doc.getElementById("choose-attrs");
            Elements items = els.getElementsByClass("p-choose");
            System.out.println(skuName);
            for (int i = 0; i < items.size(); i++) {
 
                String title = items.get(i).getElementsByClass("dt").text();
                String content = items.get(i).getElementsByClass("selected").get(0).attr("data-value");
                System.out.println("--" + title + ":" + content);
            }
 
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
    public void getCoupon() {
        Map<String, String> map = new HashMap<>();
        map.put("wareId", "6008133");
        Map<String, String> headers = new HashMap<>();
        headers.put("origin", "https://item.m.jd.com");
        // headers.put("", "");
        // headers.put("", "");
        String result = HttpUtil.post("https://item.m.jd.com/coupon/coupon.json", map, headers);
        System.out.println(result);
    }
 
    /**
     * 搜索网页
     *
     * @param searchFilter
     * @return
     */
    @SuppressWarnings("deprecation")
    public static JDSearchResult searchByKey(JDSearchFilter searchFilter) {
 
        JDSearchResult searchResult = new JDSearchResult();
 
        List<JDGoods> goodsList = new ArrayList<>();
        JSONObject params = new JSONObject();
        params.put("pageNo", searchFilter.getPageNo());
        params.put("pageSize", searchFilter.getPageSize());
        params.put("searchUUID", StringUtil.Md5(System.currentTimeMillis() + ""));
 
        JSONObject data = new JSONObject();
        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("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");
        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);
            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++) {
                            JDGoods parseGoods = parseGoods(array.optJSONArray(i).optJSONObject(0).toString());
                            if (parseGoods != null) {
                                goodsList.add(parseGoods);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        searchResult.setGoodsList(goodsList);
        return searchResult;
    }
 
    /**
     * 搜索网页
     *
     * @param searchFilter
     * @return
     */
    @SuppressWarnings("deprecation")
    public static List<JDGoods> getRecommendGoodsById(Long skuId) {
        List<JDGoods> goodsList = new ArrayList<>();
        JSONObject params = new JSONObject();
        params.put("pageNo", 1);
        params.put("pageSize", 60);
        params.put("searchUUID", StringUtil.Md5(System.currentTimeMillis() + ""));
 
        JSONObject data = new JSONObject();
        data.put("lock", "0");
        data.put("deliveryType", 0);
        data.put("hasCoupon", 0);
        data.put("isCare", 0);
        data.put("isPinGou", 0);
        data.put("isZY", 0);
        data.put("orientationFlag", 0);
 
        data.put("key", skuId);
        data.put("keywordType", "kt0");
        data.put("searchType", "st2");
        params.put("data", data);
 
        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");
        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);
            if (json.optInt("code") == 200) {
                if (json.optJSONObject("data") != null) {
                    JSONArray array = json.optJSONObject("data").optJSONArray("unionRecommendGoods");
                    if (array != null) {
                        for (int i = 0; i < array.size(); i++) {
                            JDGoods parseGoods = parseGoods(array.optJSONArray(i).optJSONObject(0).toString());
                            if (parseGoods != null) {
                                goodsList.add(parseGoods);
                            }
                        }
                    }
                }
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return goodsList;
    }
 
    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);
 
        BigDecimal wlCommissionRatio = new BigDecimal(json.optString("wlCommissionRatio"));
        if (wlCommissionRatio.compareTo(new BigDecimal(0)) > 0) {
            JDCommissionInfo commission = new JDCommissionInfo();
            commission.setCommission(new BigDecimal(json.optString("wlCommission")));
            commission.setCommissionShare(wlCommissionRatio);
            goods.setCommissionInfo(commission);
        } else {
            return null; // 过滤无返利商品
        }
 
        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);
        goods.setPrice(new BigDecimal(json.optString("wlPrice")));
 
        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"));
 
        goods.setGoodCommentsShare(new BigDecimal(json.optString("goodCommentsShare")));
 
        // 团购信息
        if (json.optString("isPinGou") != null && json.optInt("isPinGou") == 1) {
            JDPingouInfo jdPinGouInfo = new JDPingouInfo();
            jdPinGouInfo.setPingouPrice(new BigDecimal(json.optString("pingouPrice")));
            jdPinGouInfo.setPingouTmCount(json.optLong("pingouTmCount"));
            goods.setPinGouInfo(jdPinGouInfo);
        }
 
        String finalPrice = json.optString("finalPrice");
        if (!StringUtil.isNullOrEmpty(finalPrice)) {
            // 券信息
            BigDecimal finalPriceDecimal = new BigDecimal(json.optString("finalPrice"));
            if (finalPriceDecimal != null && json.optString("hasCoupon") != null && json.optInt("hasCoupon") == 1
                    && json.optInt("couponRemainCnt") > 0) {
                List<JDCouponInfo> couponList = new ArrayList<>();
                JDCouponInfo coupon = new JDCouponInfo();
                coupon.setDiscount(new BigDecimal(json.optString("couponDiscount")));
                coupon.setQuota(new BigDecimal(json.optString("couponQuota")));
                coupon.setLink("https:" + json.optString("couponLink"));
                couponList.add(coupon);
                goods.setCouponInfoList(couponList);
            }
        }
 
        return goods;
    }
 
    /**
     * 商品佣金计算
     *
     * @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;
        }
 
        BigDecimal price = null;
        JDPingouInfo pinGouInfo = goods.getPinGouInfo();
        if (pinGouInfo == null) {
            price = goods.getPrice();
        } else {
            price = pinGouInfo.getPingouPrice();
        }
 
        JDCouponInfo couponInfo = getShowCouponInfo(goods.getCouponInfoList(), price);
        if (couponInfo == null) {
            money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil
                            .mul(MoneyBigDecimalUtil.mul(price, commissionInfo.getCommissionShare()), new BigDecimal("0.01")),
                    MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
        } else {
            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)));
            }
        }
        // 返利比例为原来的90%
        return BigDecimalUtil.getWithNoZera(MoneyBigDecimalUtil.mul(money, new BigDecimal("0.9")));
    }
 
    /**
     * 商品佣金计算
     *
     * @param goods
     * @param rate
     * @return
     */
    public static BigDecimal getGoodsFanLiMoneyPlus(JDGoods goods, BigDecimal rate) {
        BigDecimal money = new BigDecimal(0);
        BigDecimal commissionPlus = goods.getCommissionPlus();
        if (commissionPlus == null) {
            return money;
        }
 
        BigDecimal price = null;
        JDPingouInfo pinGouInfo = goods.getPinGouInfo();
        if (pinGouInfo == null) {
            price = goods.getPrice();
        } else {
            price = pinGouInfo.getPingouPrice();
        }
 
        JDCouponInfo couponInfo = getShowCouponInfo(goods.getCouponInfoList(), price);
        if (couponInfo == null) {
            money = MoneyBigDecimalUtil.mul(
                    MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(price, commissionPlus), new BigDecimal("0.01")),
                    MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
        } else {
            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, commissionPlus), new BigDecimal("0.01")),
                        MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
 
            } else {// 不能用券
                money = MoneyBigDecimalUtil.mul(
                        MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(price, commissionPlus), new BigDecimal("0.01")),
                        MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
            }
        }
        // 返利比例为原来的90%
        return BigDecimalUtil.getWithNoZera(MoneyBigDecimalUtil.mul(money, new BigDecimal("0.9")));
    }
 
    /**
     * 计算商品券后价,没有券则返回原价
     *
     * @param goodsBrief
     * @return
     */
    public static BigDecimal getCouponPrice(JDGoods jdGoods) {
        BigDecimal price = null;
        JDPingouInfo pinGouInfo = jdGoods.getPinGouInfo();
        if (pinGouInfo == null) {
            price = jdGoods.getPrice();
        } else {
            price = pinGouInfo.getPingouPrice();
        }
        JDCouponInfo couponInfo = getShowCouponInfo(jdGoods.getCouponInfoList(), price);
        if (couponInfo == null) {
            return jdGoods.getPrice();
        }
 
        BigDecimal discount = couponInfo.getDiscount();
        // 计算券后价
        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;
    }
 
    /**
     * 搜索候选词
     *
     * @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(
                    "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;
    }
 
    public static boolean isJDLink(String url) {
        return url.startsWith("https://item.m.jd.com/product/") || url.startsWith("http://item.m.jd.com/product/")
                || url.startsWith("https://item.jd.com/") || url.startsWith("http://item.jd.com/")||url.startsWith("https://u.jd.com/");
    }
 
    /**
     * 根据链接提取商品ID
     *
     * @param url
     * @return
     */
    public static String parseJDSkuIdByUrl(String url) {
        try {
            if (url.startsWith("https://item.m.jd.com/product/") || url.startsWith("http://item.m.jd.com/product/")
                    || url.startsWith("https://item.jd.com/") || url.startsWith("http://item.jd.com/")) {
                String preUrl = url.split("\\?")[0];
                String index = preUrl.split("/")[preUrl.split("/").length - 1];
                index = index.split("\\.")[0];
                return index.trim();
            }
        } catch (Exception e) {
        }
        return null;
    }
 
    public static JDGoods getGoodsFromWeb(Long skuId) {
        String url = String.format("https://item.m.jd.com/product/%s.html", skuId + "");
        try {
            Document doc = Jsoup.connect(url).get();
            Elements scripts = doc.getElementsByTag("script");
            for (int i = 0; i < scripts.size(); i++) {
                String content = scripts.get(i).html();
                if (content.contains("window._itemOnly =")) {
                    try {
                        String jsContent = "var windowData={};function getData(){return  JSON.stringify(windowData._itemOnly);}"
                                + "var _itemOnly={}; " + content.replace("window.", "windowData.");
                        System.out.println(jsContent);
                        ScriptEngineManager m = new ScriptEngineManager();
                        ScriptEngine engine = m.getEngineByName("JavaScript");
                        engine.eval(jsContent);
                        if (engine instanceof Invocable) {
                            Invocable in = (Invocable) engine;
                            String data = in.invokeFunction("getData").toString();
                            JSONObject root = JSONObject.fromObject(data);
                            root = root.optJSONObject("item");
                            JDGoods goods = new JDGoods();
                            goods.setSkuId(root.optLong("skuId"));
                            goods.setPicUrl("http://img14.360buyimg.com/n1/" + root.optJSONArray("image").optString(0));
                            goods.setSkuName(root.optString("skuName"));
                            return goods;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
 
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    public static String getJDGoodsJS() {
        InputStream input = JDUtil.class.getClassLoader().getResourceAsStream("jdGoods.js");
        StringBuilder sb = new StringBuilder();
        String line;
        BufferedReader br = new BufferedReader(new InputStreamReader(input));
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
 
    static String jdGoodsJs = null;
    static ScriptEngine engine = null;
 
    static {
        if (jdGoodsJs == null)
            jdGoodsJs = getJDGoodsJS();
        ScriptEngineManager manager = new ScriptEngineManager();
        engine = manager.getEngineByName("javascript");
        try {
            engine.eval(jdGoodsJs);
        } catch (Exception e) {
        }
    }
 
    public static String getJDGoodsId(String url) {
        try {
            if (engine instanceof Invocable) {
                Invocable in = (Invocable) engine;
                Object goodsId = in.invokeFunction("getGoodsId", url);
                if (goodsId != null)
                    return goodsId.toString().trim();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 通过链接id
     *
     * @param url
     * @return
     */
    public static String getJDGoodsIdByWeiXin(String url) {
        if (StringUtil.isNullOrEmpty(url) || url.indexOf("jd.com") < 0)
            return null;
 
        try {
            String id = null;
            int indexOf = url.indexOf("?");
            url = url.substring(indexOf + 1, url.length() - 1);
            String[] temp = url.split("&");
            for (int i = 0; i < temp.length; i++) {
                String content = temp[i];
                if (content.startsWith("sku")) {
                    id = content.split("=")[content.split("=").length - 1];
                    break;
                }
            }
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 根据 u.jd短连接获取真实的商品id
     *
     * @param url
     * @return
     */
    public static String getJDGoodsIdByUJD(String url) {
        try {
            String body = HttpUtil.get(url);
            Document doc = Jsoup.parse(body);
            Elements els = doc.getElementsByTag("script");
            for (int i = 0; i < els.size(); i++) {
                String content = els.get(i).html();
                if (content.indexOf("var hrl") > -1) {
                    ScriptEngineManager manager = new ScriptEngineManager();
                    ScriptEngine engine = manager.getEngineByName("javascript");
 
                    int indexOf = content.indexOf("var hrl");
                    content = content.substring(indexOf, content.length());
 
                    int indexOf2 = content.indexOf(";");
                    content = content.substring(0, indexOf2 + 1);
 
                    String js = "function getData(){return  JSON.stringify(hrl);}" + content;
 
                    engine.eval(js);
                    if (engine instanceof Invocable) {
                        Invocable in = (Invocable) engine;
                        String data = in.invokeFunction("getData").toString();
                        String link = data.substring(1, data.length() - 1);
                        // 将链接中未encode的参数encode
                        String param = link.split("\\?")[1];
                        String host = link.split("\\?")[0];
                        String[] params = param.split("&");
                        String paramStr = "";
                        for (String p : params) {
                            String value = p.substring(p.indexOf("=") + 1);
                            String key = p.substring(0, p.indexOf("="));
                            if (URLDecoder.decode(value, "UTF-8").equalsIgnoreCase(value)) {
                                value = URLEncoder.encode(value, "UTF-8");
                            }
                            paramStr += key + "=" + value + "&";
                        }
                        link = host + "?" + paramStr;
                        String realLink = HttpUtil.getLocation(link);
                        Map<String, String> paramsMap = new HashMap<>();
 
                        param = realLink.split("\\?")[1];
                        params = param.split("&");
                        for (String p : params) {
                            String value = p.substring(p.indexOf("=") + 1);
                            String key = p.substring(0, p.indexOf("="));
                            paramsMap.put(key, value);
                        }
                        if (!StringUtil.isNullOrEmpty(paramsMap.get("sku"))) {
                            return paramsMap.get("sku");
                        }
 
                        if (!StringUtil.isNullOrEmpty(paramsMap.get("wareId"))) {
                            return paramsMap.get("wareId");
                        }
 
                        // 判断纯数字大于4位的返回
                        for (Iterator<String> its = paramsMap.keySet().iterator(); its.hasNext(); ) {
                            String key = its.next();
                            String value = paramsMap.get(key);
                            if (NumberUtil.isNumeric(value) && value.length() >= 4) {
                                return value;
                            }
                        }
                        String id = JDUtil.getJDGoodsId(realLink);
                        if (!StringUtil.isNullOrEmpty(id)) {
                            return id;
                        }
                        LogHelper.error("京东短接解析失败:" + url);
                        return null;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogHelper.errorDetailInfo(e, "京东短链获取商品ID失败:", url);
        }
        return null;
    }
 
    /**
     * 查询天猫商品图片、标题
     *
     * @param auctionId
     * @return
     */
    public static JDGoods getSimpleGoodsInfo(String goodsId) {
        JDGoods jdGoods = null;
        try {
            Connection connect = Jsoup.connect(String.format(TM_PHONE_URL, goodsId));
            Document document = connect.get();
            Elements elements = document.getElementsByTag("script");
            if (elements.size() >= 0) {
                for (int i = 0; i < elements.size(); i++) {
                    String content = elements.get(i).toString();
                    if (content.contains("window._itemOnly")) {
                        System.out.println("存在");
                        content = content.replace("<script>", "");
                        content = content.replace("</script>", "");
                        int indexOf = content.indexOf("window._isLogin");
                        content = content.substring(0, indexOf);
 
                        int indexItem = content.indexOf("\"item\":");
                        content = content.substring(indexItem, content.length() - 1);
                        content = content.replace("\"item\":", "var item =");
                        content = content.replace("});", "").trim();
 
                        content += ";function getData() {return item;}";
                        ScriptEngineManager manager = new ScriptEngineManager();
                        ScriptEngine engine = manager.getEngineByName("javascript");
                        engine.eval(content);
 
                        if (engine instanceof Invocable) {
                            Invocable in = (Invocable) engine;
                            JSONObject json = JSONObject.fromObject(in.invokeFunction("getData"));
 
                            jdGoods = new JDGoods();
                            jdGoods.setSkuName(json.getString("skuName"));
                            JSONObject jsonImage = json.getJSONObject("image");
                            if (jsonImage != null && jsonImage.size() > 0) {
                                String picUrl = jsonImage.getString("0");
                                jdGoods.setPicUrl("http://m.360buyimg.com/mobilecms/s750x750_" + picUrl);
                            }
                        }
 
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jdGoods;
    }
 
    /**
     * 获取类目
     *
     * @param level    等级
     * @param parentId 上级id
     * @return
     */
    public static List<GoodsClassDTO> getCategoryList(int level, int parentId) {
        try {
            // 请求地址
            String requestUrl = "https://union.jd.com/api/help/getCategoryList?level=%s&parentId=%s";
            // 执行请求
            String result = HttpUtil.get(String.format(requestUrl, level, parentId));
 
            if (!StringUtil.isNullOrEmpty(result)) {
                JSONObject json = JSONObject.fromObject(result);
                JSONArray jsonArray = json.getJSONArray("data");
                if (jsonArray != null && jsonArray.size() > 0) {
                    List<GoodsClassDTO> list = new ArrayList<GoodsClassDTO>();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        Object object = jsonArray.get(i);
                        if (object == null)
                            continue;
                        JSONObject jsonClass = JSONObject.fromObject(object);
 
                        GoodsClassDTO goodsClass = new GoodsClassDTO();
                        goodsClass.setId(jsonClass.optString("id"));
                        goodsClass.setName(jsonClass.optString("categoryName"));
                        list.add(goodsClass);
                    }
                    return list;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 获取类目
     *
     * @param level    等级
     * @param parentId 上级id
     * @return
     */
    public static List<GoodsClassDTO> getCategoryCommiPlus(int level, int parentId) {
        try {
            String body = "{\"funName\":\"getCategoryCommiPlus\",\"param\":{\"level\":" + level + ",\"parentId\":" + parentId + "}}";
 
            // 请求地址
            String requestUrl = "https://api.m.jd.com/api?functionId=union_help_center_logout&appid=unionpc&_=1613812219436&loginType=3&body=" + URLEncoder.encode(body);
            // 执行请求
            Map<String, String> headers = new HashMap<>();
            headers.put("cookie", "shshshfpa=329d2e56-7a79-19b5-0076-8c0b65ea461f-1547450580; shshshfpb=sS4IX%20CPaOczXlTOfurTlNw%3D%3D; abtest=20190619150948087_34; whwswswws=; __jdu=1547450598944823318591; jcap_dvzw_fp=4183dc16d888b5af62f3a2b1fc5e2d64$924110342461; commonAddress=2273642881; regionAddress=2273642881; pinId=2tU67_7e0-vDzUcicjyiuw; unpl=V2_ZzNtbUEDRUAnWkVWLx5YUmJTFA0RXkBHfQpDU3oRDwwyUBpaclRCFnUUR1dnGFwUZwMZXURcRxxFCEdkeBBVAWMDE1VGZxBFLV0CFSNGF1wjU00zQwBBQHcJFF0uSgwDYgcaDhFTQEJ2XBVQL0oMDDdRFAhyZ0AVRQhHZHsdWQNiBxNZSl5LEHABRVZ5GVUBbwMibUVncyV2CURVehhsBFcCIh8WC0AWcA1PUTYZWABhBhZcRl9KHXANT1d5G1wMYwsSbUNnQA%3d%3d; user-key=973b1365-9426-4c3f-8617-7c219d55f8be; unick=hexiaohui1011; _tp=yjKYjqrqLmDwvds6adwZZg%3D%3D; _pst=hexiaohui1011; shshshfp=b2b376c84d4d98fc6eefa16c183e3ecf; 3AB9D23F7A4B3C9B=HQOEEEVUVVKHO34WBW6L7LUQ4JICUD5UAQNK5WFDW3QLS5EEEOLXYJU2LJFHKI7HYA5HDNMAS53JKWR6H3HARY6VUM; ipLoc-djd=4-50950-88-0.529243028; ipLocation=%u91cd%u5e86; cn=11; __jdv=209449046|direct|-|none|-|1613812108092; TrackID=1wkaJObNR4rHfhHI9qTocAgfsjDkEzoYwJdM_UmonQDkjxkN4swvtM5uIjhnz4Z5AzYz3a_LnGX0vkEf47jroQ0RwyLe4DClJ03azrRMDZJU; thor=19CF5E87007461885622A30596B17A2F3C57C503EE774E94B972584FC02948D4D82E5B2B6336022DB5AA71BD4DFD63BE26D1E9E933D138913A74B2BAA94CFA501C2AB817E199FC195E6985F2C7B4B1E296C20453A72C294649EB97F0C8F58926D89D01B0962672F4C87B2E3BE4671E3955DB85389E4AF78FAC243DAC0CB77563996DFB7426157A070D70CFB88909FBA8; pin=hexiaohui1011; ceshi3.com=201; __jda=209449046.1547450598944823318591.1547450598.1612152289.1613812108.401; __jdb=209449046.5.1547450598944823318591|401.1613812108; __jdc=209449046; RT=\"z=1&dm=jd.com&si=mnu0prq1z7m&ss=kldi27s8&sl=2&tt=18e&ld=66y\"");
            headers.put("origin", "https://union.jd.com");
            headers.put("referer", "https://union.jd.com/");
            String result = HttpUtil.get(requestUrl, headers);
 
            if (!StringUtil.isNullOrEmpty(result)) {
                JSONObject json = JSONObject.fromObject(result);
                JSONArray jsonArray = json.getJSONArray("result");
                if (jsonArray != null && jsonArray.size() > 0) {
                    List<GoodsClassDTO> list = new ArrayList<GoodsClassDTO>();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        Object object = jsonArray.get(i);
                        if (object == null)
                            continue;
                        JSONObject jsonClass = JSONObject.fromObject(object);
 
                        GoodsClassDTO goodsClass = new GoodsClassDTO();
                        goodsClass.setId(jsonClass.optString("id"));
                        goodsClass.setName(jsonClass.optString("name"));
 
                        String selfMobileComm = jsonClass.optString("selfMobileComm");
                        if (!StringUtil.isNullOrEmpty(selfMobileComm))
                            goodsClass.setSelfComm(new BigDecimal(selfMobileComm));
 
                        String popMobileComm = jsonClass.optString("popMobileComm");
                        if (!StringUtil.isNullOrEmpty(popMobileComm))
                            goodsClass.setPopComm(new BigDecimal(popMobileComm));
 
                        list.add(goodsClass);
                    }
                    return list;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 获取需要展示的券
     *
     * @param couponInfoList 券列表
     * @param price          商品价格
     * @return
     */
    public static JDCouponInfo getShowCouponInfo(List<JDCouponInfo> couponInfoList, BigDecimal price) {
        if (couponInfoList != null && couponInfoList.size() > 0) {
            // 根据消费条件排序
            Comparator<JDCouponInfo> cm = new Comparator<JDCouponInfo>() {
                @Override
                public int compare(JDCouponInfo o1, JDCouponInfo o2) {
                    return o1.getQuota().compareTo(o2.getQuota()) == 0 ? o1.getDiscount().compareTo(o2.getDiscount()) : o1.getQuota().compareTo(o2.getQuota());
                }
            };
            Collections.sort(couponInfoList, cm);
 
            // 设置券信息为最接近的一个
            for (int i = couponInfoList.size() - 1; i >= 0; i--) {
                if (couponInfoList.get(i).getQuota().compareTo(price) <= 0) {
                    return couponInfoList.get(i);
                }
            }
            return couponInfoList.get(0);
        }
        return null;
    }
 
    public static JDCouponInfo getShowCouponInfo(JDGoods goods) {
        if (goods == null)
            return null;
        BigDecimal price = null;
        JDPingouInfo pinGouInfo = goods.getPinGouInfo();
        if (pinGouInfo == null) {
            price = goods.getPrice();
        } else {
            price = pinGouInfo.getPingouPrice();
        }
        return getShowCouponInfo(goods.getCouponInfoList(), price);
    }
 
    public static List<String> getJDShortLinksFromText(String text) {
        String regex = "(https://u\\.jd\\.com/)[0-9A-Za-z]{1,20}";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(text);
        List<String> urlList = new ArrayList<>();
        while (m.find()) {
            urlList.add(m.group());
        }
        return urlList;
    }
 
}