admin
2020-07-15 fe646416d8d46de7b896ddbf65a3ad0cd30b729b
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
package com.yeshi.fanli.util.dataoke;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.yeshi.utils.HttpUtil;
 
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.dto.dataoke.DaTaoKeGoodsResult;
import com.yeshi.fanli.dto.taobao.TaoBaoShopDTO;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoKeAppInfo;
import com.yeshi.goods.facade.entity.taobao.DaTaoKeDetailV2;
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 DaTaoKeApiUtil {
    final static String API_KEY = "a083abb893";
    final static String API_KEY_2 = "b7a5ea2cd9";
 
    final static String APP_KEY = "5cf764636d373";
    final static String APP_KEY_SECRET = "5ea3c24900743f3aa531fb264f9824f2";
 
    final static String APP_KEY_2 = "5cf75b0f2c0e4";
    final static String APP_KEY_SECRET_2 = "b14f1fa115129a447937ca998b311d1e";
 
    // 排序值
 
    final public static int SORT_DEFAULT = 0;
    final public static int SORT_CREATETIME = 1;
    final public static int SORT_SALES = 2;
    final public static int SORT_COUPON_NUM = 3;
    final public static int SORT_COMMISSION = 4;
    final public static int SORT_PRICE_HIGH_TO_LOW = 5;
    final public static int SORT_PRICE_LOW_TO_HIGH = 6;
 
    final static TaoKeAppInfo[] APP_KEYS = new TaoKeAppInfo[] { new TaoKeAppInfo(APP_KEY, APP_KEY_SECRET),
            new TaoKeAppInfo(APP_KEY_2, APP_KEY_SECRET_2) };
 
    final static String[] APP_SECRETS = new String[] { APP_KEY_SECRET, APP_KEY_SECRET_2 };
 
    static Gson gson = null;
    static {
        gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                return f.getName().equals("createTime");// 忽略createTime的解析
            }
 
            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        }).create();
    }
 
    private static String get(String url) {
        HttpClient client = new HttpClient();
        try {
            client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
            client.getHttpConnectionManager().getParams().setSoTimeout(5000);
            GetMethod method = new GetMethod(url);
            client.executeMethod(method);
            return method.getResponseBodyAsString();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    private static String request(String url) {
        String result = null;
 
        while (result == null) {
            try {
                result = get(url);
                if ((result != null && result.startsWith("<html>")) || StringUtil.isNullOrEmpty(result))
                    result = null;
            } catch (Exception e) {
                try {
                    Thread.sleep(400);
                } catch (InterruptedException e1) {
                }
            }
        }
 
        return result;
    }
 
    /**
     * 获取大淘客品牌优选 品牌id
     * 
     * @return
     */
    public static List<String> getBrandIdList() {
        List<String> listId = new ArrayList<>();
        try {
            Document doc = Jsoup.connect("http://www.dataoke.com/brandFilter?cid=0&sort=1&main=0").get();
            Element root = doc.getElementsByClass("brand-list").get(0);
            Elements items = root.getElementsByTag("a");
            for (int i = 0; i < items.size(); i++) {
                String id = items.get(i).attr("href").split("\\?")[1].replace("id=", "").trim();
                listId.add(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listId;
    }
 
    /**
     * 获取店铺下前4个商品、店铺介绍
     * 
     * @param brandId
     * @return
     */
    public static TaoBaoShopDTO getDynamicShopInfo(String brandId) {
        if (brandId == null) {
            return null;
        }
        TaoBaoShopDTO taoBaoShopDTO = null;
 
        Document doc;
        try {
            doc = Jsoup.connect("http://www.dataoke.com/brandSingle?id=" + brandId).get();
            Elements els = doc.getElementsByTag("script");
            for (int i = 0; i < els.size(); i++) {
                if (els.get(i).html().contains("var brandData")) {
 
                    JSONObject data = JSONObject
                            .fromObject(els.get(i).html().replace("var brandData =", "").trim().split("};")[0] + "}");
                    taoBaoShopDTO = new TaoBaoShopDTO();
                    JSONObject item = data.optJSONObject("act");
                    taoBaoShopDTO.setSellerId(item.optLong("seller_id"));
                    taoBaoShopDTO.setBrandId(item.optString("brand_id"));
                    taoBaoShopDTO.setBrandDes(item.optString("brand_des"));
                    taoBaoShopDTO.setShopIcon(item.optString("brand_logo"));
                    taoBaoShopDTO.setUserType(0);
 
                    // 商品信息
                    taoBaoShopDTO = getDynamicShopGoods(brandId, taoBaoShopDTO);
 
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return taoBaoShopDTO;
    }
 
    private static TaoKeAppInfo getRandomApp() {
        int pos = (int) (Math.random() * APP_KEYS.length);
        return APP_KEYS[pos];
    }
 
    public static DaTaoKeGoodsResult listAll(String pageId) {
        TaoKeAppInfo app = getRandomApp();
        DaTaoKeGoodsResult daTaoKeGoodsResult = new DaTaoKeGoodsResult();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.1.0");
        params.put("appKey", app.getAppKey());
        params.put("pageSize", 200 + "");
        // params.put("sort", "1");
        if (!StringUtil.isNullOrEmpty(pageId)) {
            params.put("pageId", pageId);
        }
        params.put("sign", getSign(params, app.getAppSecret()));
        String result = HttpUtil.get("https://openapi.dataoke.com/api/goods/get-goods-list", params, new HashMap<>());
        JSONObject json = JSONObject.fromObject(result);
        JSONObject dataJson = json.optJSONObject("data");
        if (dataJson != null) {
            JSONArray array = dataJson.optJSONArray("list");
            if (array != null) {
                List<DaTaoKeDetailV2> list = parseDaTaoKeDetailV2List(array);
                daTaoKeGoodsResult.setGoodsList(list);
            }
            daTaoKeGoodsResult.setPageId(dataJson.optString("pageId"));
            daTaoKeGoodsResult.setTotalCount(dataJson.optLong("totalNum"));
        }
        return daTaoKeGoodsResult;
    }
 
    /**
     * 失效商品
     * 
     * @param pageId
     * @return
     */
    public static DaTaoKeGoodsResult getInvalidGoodsList(String pageId, String startTime, String endTime) {
        TaoKeAppInfo app = getRandomApp();
        DaTaoKeGoodsResult daTaoKeGoodsResult = new DaTaoKeGoodsResult();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.0.1");
        params.put("appKey", app.getAppKey());
        params.put("pageSize", 200 + "");
        if (!StringUtil.isNullOrEmpty(startTime))
            params.put("startTime", startTime);
 
        if (!StringUtil.isNullOrEmpty(endTime))
            params.put("endTime", endTime);
 
        if (!StringUtil.isNullOrEmpty(pageId))
            params.put("pageId", pageId);
 
        params.put("sign", getSign(params, app.getAppSecret()));
        String result = HttpUtil.get("https://openapi.dataoke.com/api/goods/get-stale-goods-by-time", params,
                new HashMap<>());
        System.out.println(result);
        JSONObject json = JSONObject.fromObject(result);
        JSONObject dataJson = json.optJSONObject("data");
        if (dataJson != null) {
            JSONArray array = dataJson.optJSONArray("list");
            if (array != null) {
                List<DaTaoKeDetailV2> list = parseDaTaoKeDetailV2List(array);
                daTaoKeGoodsResult.setGoodsList(list);
            }
            daTaoKeGoodsResult.setPageId(dataJson.optString("pageId"));
            daTaoKeGoodsResult.setTotalCount(dataJson.optLong("totalNum"));
        }
        return daTaoKeGoodsResult;
    }
 
    public static DaTaoKeGoodsResult getNewGoodsList(String pageId, String startTime, String endTime) {
        TaoKeAppInfo app = getRandomApp();
        DaTaoKeGoodsResult daTaoKeGoodsResult = new DaTaoKeGoodsResult();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.1.0");
        params.put("appKey", app.getAppKey());
        params.put("pageSize", 200 + "");
        if (!StringUtil.isNullOrEmpty(startTime))
            params.put("startTime", startTime);
 
        if (!StringUtil.isNullOrEmpty(endTime))
            params.put("endTime", endTime);
 
        if (!StringUtil.isNullOrEmpty(pageId))
            params.put("pageId", pageId);
 
        params.put("sign", getSign(params, app.getAppSecret()));
        String result = HttpUtil.get("https://openapi.dataoke.com/api/goods/pull-goods-by-time", params,
                new HashMap<>());
        JSONObject json = JSONObject.fromObject(result);
        JSONObject dataJson = json.optJSONObject("data");
        if (dataJson != null) {
            JSONArray array = dataJson.optJSONArray("list");
            if (array != null) {
 
                List<DaTaoKeDetailV2> list = parseDaTaoKeDetailV2List(array);
                daTaoKeGoodsResult.setGoodsList(list);
            }
            daTaoKeGoodsResult.setPageId(dataJson.optString("pageId"));
            daTaoKeGoodsResult.setTotalCount(dataJson.optLong("totalNum"));
        }
        return daTaoKeGoodsResult;
    }
 
    public static DaTaoKeGoodsResult getUpdateGoodsList(String pageId, String startTime, String endTime) {
        TaoKeAppInfo app = getRandomApp();
        DaTaoKeGoodsResult daTaoKeGoodsResult = new DaTaoKeGoodsResult();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.1.0");
        params.put("appKey", app.getAppKey());
        params.put("pageSize", 200 + "");
        if (!StringUtil.isNullOrEmpty(startTime))
            params.put("startTime", startTime);
 
        if (!StringUtil.isNullOrEmpty(endTime))
            params.put("endTime", endTime);
 
        if (!StringUtil.isNullOrEmpty(pageId))
            params.put("pageId", pageId);
 
        params.put("sign", getSign(params, app.getAppSecret()));
        String result = HttpUtil.get("https://openapi.dataoke.com/api/goods/get-newest-goods", params, new HashMap<>());
        JSONObject json = JSONObject.fromObject(result);
        JSONObject dataJson = json.optJSONObject("data");
        if (dataJson != null) {
            JSONArray array = dataJson.optJSONArray("list");
            if (array != null) {
                List<DaTaoKeDetailV2> list = parseDaTaoKeDetailV2List(array);
                daTaoKeGoodsResult.setGoodsList(list);
            }
            daTaoKeGoodsResult.setPageId(dataJson.optString("pageId"));
            daTaoKeGoodsResult.setTotalCount(dataJson.optLong("totalNum"));
        }
        return daTaoKeGoodsResult;
    }
 
    public static DaTaoKeDetailV2 getGoodsDetail(Long id) {
        TaoKeAppInfo app = getRandomApp();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.1.0");
        params.put("appKey", app.getAppKey());
        params.put("id", id + "");
        params.put("sign", getSign(params, app.getAppSecret()));
        String result = HttpUtil.get("https://openapi.dataoke.com/api/goods/get-goods-details", params,
                new HashMap<>());
        JSONObject json = JSONObject.fromObject(result);
        JSONObject dataJson = json.optJSONObject("data");
        if (dataJson != null) {
            return parseDaTaoKeDetailV2(dataJson);
        }
        return null;
    }
 
    public static DaTaoKeDetailV2 getGoodsDetailByGoodsId(Long goodsId) {
        TaoKeAppInfo app = getRandomApp();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.1.1");
        params.put("appKey", app.getAppKey());
        params.put("goodsId", goodsId + "");
        params.put("sign", getSign(params, app.getAppSecret()));
        String result = HttpUtil.get("https://openapi.dataoke.com/api/goods/get-goods-details", params,
                new HashMap<>());
        System.out.println(result);
        JSONObject json = JSONObject.fromObject(result);
        JSONObject dataJson = json.optJSONObject("data");
        if (dataJson != null) {
            return parseDaTaoKeDetailV2(dataJson);
        }
        return null;
    }
 
    private static String requestGet(String url, Map<String, String> params) {
        Iterator<String> keys = params.keySet().iterator();
        url += "?";
        while (keys.hasNext()) {
            String key = keys.next();
            try {
                url += String.format("%s=%s&", key, URLEncoder.encode(params.get(key), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return HttpUtil.get(url, 10000);
    }
 
    public static DaTaoKeGoodsResult search(String key, List<Integer> cidList, BigDecimal priceLowerLimit,
            BigDecimal priceUpperLimit, int page, int pageSize, Integer sort) {
 
        TaoKeAppInfo app = getRandomApp();
        DaTaoKeGoodsResult daTaoKeGoodsResult = new DaTaoKeGoodsResult();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v2.1.0");
        params.put("appKey", app.getAppKey());
        params.put("pageSize", pageSize + "");
        params.put("pageId", page + "");
        if (priceLowerLimit != null)
            params.put("priceLowerLimit", priceLowerLimit + "");
        if (priceUpperLimit != null)
            params.put("priceUpperLimit", priceUpperLimit + "");
        String cids = "";
        if (cidList != null && cidList.size() > 0)
            for (Integer cid : cidList) {
                cids += cid + ",";
            }
 
        if (cids.endsWith(","))
            cids = cids.substring(0, cids.length() - 1);
        if (!StringUtil.isNullOrEmpty(cids))
            params.put("cids", cids);
 
        if (StringUtil.isNullOrEmpty(key))
            key = "";
        params.put("keyWords", key);
 
        if (sort != null)
            params.put("sort", sort + "");
        params.put("sign", getSign(params, app.getAppSecret()));
        String result = requestGet("https://openapi.dataoke.com/api/goods/get-dtk-search-goods", params);
 
        JSONObject json = JSONObject.fromObject(result);
        JSONObject dataJson = json.optJSONObject("data");
        if (dataJson != null) {
            JSONArray array = dataJson.optJSONArray("list");
            if (array != null) {
                List<DaTaoKeDetailV2> list = parseDaTaoKeDetailV2List(array);
                daTaoKeGoodsResult.setGoodsList(list);
            }
            daTaoKeGoodsResult.setPageId(dataJson.optString("pageId"));
            daTaoKeGoodsResult.setTotalCount(dataJson.optLong("totalNum"));
        }
        return daTaoKeGoodsResult;
    }
 
    public static DaTaoKeGoodsResult search(String key, List<Integer> cidList, BigDecimal priceLowerLimit,
            BigDecimal priceUpperLimit, Integer couponPriceLowerLimit, int page, int pageSize, Integer sort) {
 
        TaoKeAppInfo app = getRandomApp();
        DaTaoKeGoodsResult daTaoKeGoodsResult = new DaTaoKeGoodsResult();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v2.1.0");
        params.put("appKey", app.getAppKey());
        params.put("pageSize", pageSize + "");
        params.put("pageId", page + "");
        if (priceLowerLimit != null)
            params.put("priceLowerLimit", priceLowerLimit + "");
        if (priceUpperLimit != null)
            params.put("priceUpperLimit", priceUpperLimit + "");
 
        if (couponPriceLowerLimit != null)
            params.put("couponPriceLowerLimit", couponPriceLowerLimit + "");
 
        String cids = "";
        if (cidList != null && cidList.size() > 0)
            for (Integer cid : cidList) {
                cids += cid + ",";
            }
 
        if (cids.endsWith(","))
            cids = cids.substring(0, cids.length() - 1);
        if (!StringUtil.isNullOrEmpty(cids))
            params.put("cids", cids);
 
        if (StringUtil.isNullOrEmpty(key))
            key = "%";
        params.put("keyWords", key);
 
        if (sort != null)
            params.put("sort", sort + "");
        params.put("sign", getSign(params, app.getAppSecret()));
        String result = requestGet("https://openapi.dataoke.com/api/goods/get-dtk-search-goods", params);
 
        JSONObject json = JSONObject.fromObject(result);
        JSONObject dataJson = json.optJSONObject("data");
        if (dataJson != null) {
            JSONArray array = dataJson.optJSONArray("list");
            if (array != null) {
                List<DaTaoKeDetailV2> list = parseDaTaoKeDetailV2List(array);
                daTaoKeGoodsResult.setGoodsList(list);
            }
            daTaoKeGoodsResult.setPageId(dataJson.optString("pageId"));
            daTaoKeGoodsResult.setTotalCount(dataJson.optLong("totalNum"));
        }
        return daTaoKeGoodsResult;
    }
 
    /**
     * 商品列表
     * 
     * @param key
     * @param cidList
     * @param priceLowerLimit
     * @param priceUpperLimit
     * @param couponPriceLowerLimit
     * @param pageId
     * @param pageSize
     * @param sort
     * @return
     */
    public static DaTaoKeGoodsResult getGoodsList(String key, List<Integer> cidList, BigDecimal priceLowerLimit,
            BigDecimal priceUpperLimit, Integer couponPriceLowerLimit, int pageId, int pageSize, Integer sort) {
        TaoKeAppInfo app = getRandomApp();
        DaTaoKeGoodsResult daTaoKeGoodsResult = new DaTaoKeGoodsResult();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.1.0");
        params.put("appKey", app.getAppKey());
        params.put("pageSize", pageSize + "");
        params.put("pageId", pageId + "");
 
        if (priceLowerLimit != null)
            params.put("priceLowerLimit", priceLowerLimit + "");
        if (priceUpperLimit != null)
            params.put("priceUpperLimit", priceUpperLimit + "");
 
        if (couponPriceLowerLimit != null)
            params.put("couponPriceLowerLimit", couponPriceLowerLimit + "");
 
        if (sort != null)
            params.put("sort", sort + "");
 
        if (cidList != null && cidList.size() > 0) {
            String cids = "";
            for (int cid : cidList)
                cids += cid + ",";
            cids = cids.endsWith(",") ? cids.substring(0, cids.length() - 1) : cids;
 
            params.put("cids", cids + "");
        }
 
        params.put("sign", getSign(params, app.getAppSecret()));
        String result = HttpUtil.get("https://openapi.dataoke.com/api/goods/get-goods-list", params, new HashMap<>());
        JSONObject json = JSONObject.fromObject(result);
        JSONObject dataJson = json.optJSONObject("data");
        if (dataJson != null) {
            JSONArray array = dataJson.optJSONArray("list");
            if (array != null) {
                List<DaTaoKeDetailV2> list = parseDaTaoKeDetailV2List(array);
                daTaoKeGoodsResult.setGoodsList(list);
            }
            daTaoKeGoodsResult.setPageId(dataJson.optString("pageId"));
            daTaoKeGoodsResult.setTotalCount(dataJson.optLong("totalNum"));
        }
        return daTaoKeGoodsResult;
    }
 
    private static String getSign(Map<String, String> map, String secretKey) {
        if (map.size() == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer("");
        Set<String> keySet = map.keySet();
        Iterator<String> iter = keySet.iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            sb.append("&" + key + "=" + map.get(key));
        }
        sb.deleteCharAt(0);
        String signStr = "";
        signStr = sb.toString() + "&key=" + secretKey;
        return StringUtil.Md5(signStr).toUpperCase();
    }
 
    /**
     * 获取店铺下前4个商品、店铺介绍
     * 
     * @param brandId
     * @return
     */
    public static TaoBaoShopDTO getDynamicShopGoods(String brandId, TaoBaoShopDTO taoBaoShopDTO) {
        if (brandId == null) {
            return taoBaoShopDTO;
        }
 
        int userType = 0;
        List<TaoBaoGoodsBrief> listGoods = new ArrayList<TaoBaoGoodsBrief>();
 
        String result = HttpUtil.get("http://www.dataoke.com/brandGoods?id=" + brandId + "&page=1");
        JSONObject json = JSONObject.fromObject(result);
        JSONArray arrayGoods = json.optJSONArray("goods");
 
        for (int i = 0; i < arrayGoods.size(); i++) {
            if (i >= 2) {
                break;
            }
            JSONObject itemGoods = arrayGoods.optJSONObject(i);
 
            TaoBaoGoodsBrief taoBaoGoods = new TaoBaoGoodsBrief();
            taoBaoGoods.setAuctionId(itemGoods.optLong("goodsid"));
            taoBaoGoods.setBiz30day(itemGoods.optInt("xiaoliang"));
            taoBaoGoods.setCouponAmount(new BigDecimal(itemGoods.optString("quan_jine")));
            taoBaoGoods.setCouponInfo(String.format("满%s元减%s元", itemGoods.optString("quan_tiaojian"),
                    MoneyBigDecimalUtil.getWithNoZera(new BigDecimal(itemGoods.optString("quan_jine")))));
            taoBaoGoods.setCouponLeftCount(itemGoods.optInt("quan_num"));
            taoBaoGoods.setCouponStartFee(new BigDecimal(itemGoods.optString("quan_tiaojian")));
            taoBaoGoods.setCouponTotalCount(itemGoods.optInt("quan_num"));
            taoBaoGoods.setPictUrl(itemGoods.optString("pic"));
            taoBaoGoods.setPictUrlWhite(itemGoods.optString("pic"));
            taoBaoGoods.setSellerId(itemGoods.optLong("seller_id"));
            taoBaoGoods.setShopTitle("");
            taoBaoGoods.setTitle(itemGoods.optString("title"));
            taoBaoGoods.setUserType(itemGoods.optInt("istmall"));
            taoBaoGoods.setZkPrice(new BigDecimal(itemGoods.optString("yuanjia")));
            taoBaoGoods.setTkRate(new BigDecimal(itemGoods.optString("yongjin")));
            taoBaoGoods.setTkCommFee(new BigDecimal("0"));
            taoBaoGoods.setState(0);
 
            listGoods.add(taoBaoGoods);
 
            userType = itemGoods.optInt("istmall");
        }
        taoBaoShopDTO.setUserType(userType);
        taoBaoShopDTO.setListGoods(listGoods);
        return taoBaoShopDTO;
    }
 
    public static int RANK_TYPE_TIME = 1;// 实时
    public static int RANK_TYPE_DAY = 2;// 全天榜
    public static int RANK_TYPE_HOT = 3;// 热推榜
    public static int RANK_TYPE_REBUY = 4;// 复购榜
 
    /**
     * 各大榜单
     * 
     * @param type
     * @return
     */
    public static DaTaoKeGoodsResult getRankingList(int type, Integer cid) {
        TaoKeAppInfo app = getRandomApp();
        DaTaoKeGoodsResult daTaoKeGoodsResult = new DaTaoKeGoodsResult();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.1.0");
        params.put("appKey", app.getAppKey());
        params.put("rankType", type + "");
        if (cid != null)
            params.put("cid", cid + "");
        params.put("sign", getSign(params, app.getAppSecret()));
        // 做2次请求
        String result = null;
        try {
            result = HttpUtil.get("https://openapi.dataoke.com/api/goods/get-ranking-list", params, new HashMap<>());
        } catch (Exception e) {
            result = HttpUtil.get("https://openapi.dataoke.com/api/goods/get-ranking-list", params, new HashMap<>());
        }
        JSONObject json = JSONObject.fromObject(result);
        if (json != null) {
            JSONArray array = json.optJSONArray("data");
 
            List<DaTaoKeDetailV2> list = new ArrayList<>();
            for (int i = 0; i < array.size(); i++) {
                JSONObject itemData = array.optJSONObject(i);
                DaTaoKeDetailV2 item = parseDaTaoKeDetailV2(itemData);
                item.setCouponConditions(itemData.optString("couponConditions"));
                item.setShopType(itemData.optInt("shopType"));
                item.setOriginalPrice(new BigDecimal(itemData.optString("originalPrice")));
                item.setMainPic(itemData.optString("mainPic"));
                list.add(item);
            }
            daTaoKeGoodsResult.setGoodsList(list);
        }
        return daTaoKeGoodsResult;
    }
 
    private static List<DaTaoKeDetailV2> parseDaTaoKeDetailV2List(JSONArray array) {
        List<DaTaoKeDetailV2> detailList = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            DaTaoKeDetailV2 detail = parseDaTaoKeDetailV2(array.optJSONObject(i));
            detailList.add(detail);
        }
        return detailList;
    }
 
    private static DaTaoKeDetailV2 parseDaTaoKeDetailV2(JSONObject json) {
        DaTaoKeDetailV2 detail = gson.fromJson(json.toString(), DaTaoKeDetailV2.class);
        if (StringUtil.isNullOrEmpty(json.optString("createTime"))) {
            detail.setCreateTime(
                    new Date(TimeUtil.convertToTimeTemp(json.optString("onSaleTime"), "yyyy-MM-dd HH:mm:ss")));
        } else {
            detail.setCreateTime(
                    new Date(TimeUtil.convertToTimeTemp(json.optString("createTime"), "yyyy-MM-dd HH:mm:ss")));
        }
        return detail;
    }
 
    /**
     * 各大榜单
     * 
     * @param type
     * @return
     */
    public static String getHotWords() {
        TaoKeAppInfo app = getRandomApp();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.0.1");
        params.put("appKey", app.getAppKey());
        params.put("sign", getSign(params, app.getAppSecret()));
 
        String result = null;
        try {
            result = HttpUtil.get("https://openapi.dataoke.com/api/category/get-top100", params, new HashMap<>());
        } catch (Exception e) {
            result = HttpUtil.get("https://openapi.dataoke.com/api/category/get-top100", params, new HashMap<>());
        }
 
        String hotWords = null;
        JSONObject json = JSONObject.fromObject(result);
        if (json != null) {
            json = json.getJSONObject("data");
            if (json != null) {
                hotWords = json.optString("hotWords");
            }
        }
        return hotWords;
    }
 
    /**
     * 高效转链
     * @Title: convertLink
     * @Description: 
     * @param auctionId
     * @param pid 
     * void 返回类型
     * @throws
     */
    public static String convertLink(Long auctionId, String pid) {
        TaoKeAppInfo app = APP_KEYS[0];
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.1.1");
        params.put("goodsId", auctionId + "");
        params.put("pid", pid);
        params.put("appKey", app.getAppKey());
        params.put("sign", getSign(params, app.getAppSecret()));
 
        String result = null;
        try {
            result = HttpUtil.get("https://openapi.dataoke.com/api/tb-service/get-privilege-link", params,
                    new HashMap<>());
        } catch (Exception e) {
            result = HttpUtil.get("https://openapi.dataoke.com/api/tb-service/get-privilege-link", params,
                    new HashMap<>());
        }
        
        System.out.println(result);
 
        JSONObject json = JSONObject.fromObject(result);
        if (json != null) {
            json = json.getJSONObject("data");
            if (json != null) {
                return json.optString("couponClickUrl");
            }
        }
        return null;
    }
    
    /**
     * 朋友圈
     * @param pageId
     * @return
     */
    public static DaTaoKeGoodsResult getFriendsFircleList(String pageId, int pageSize) {
        DaTaoKeGoodsResult daTaoKeGoodsResult = new DaTaoKeGoodsResult();
        
        TaoKeAppInfo app = getRandomApp();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.2.2");
        params.put("appKey", app.getAppKey());
        params.put("pageSize", pageSize + "");
        if (!StringUtil.isNullOrEmpty(pageId))
            params.put("pageId", pageId);
        params.put("sign", getSign(params, app.getAppSecret()));
 
        String result = null;
        try {
            result = HttpUtil.get("https://openapi.dataoke.com/api/goods/friends-circle-list", params, new HashMap<>());
        } catch (Exception e) {
            result = HttpUtil.get("https://openapi.dataoke.com/api/goods/friends-circle-list", params, new HashMap<>());
        }
        JSONObject json = JSONObject.fromObject(result);
        JSONObject dataJson = json.optJSONObject("data");
        if (dataJson != null) {
            JSONArray array = dataJson.optJSONArray("list");
            if (array != null) {
                List<DaTaoKeDetailV2> list = parseDaTaoKeDetailV2List(array);
                daTaoKeGoodsResult.setGoodsList(list);
            }
            daTaoKeGoodsResult.setPageId(dataJson.optString("pageId"));
            daTaoKeGoodsResult.setTotalCount(dataJson.optLong("totalNum"));
        }
        return daTaoKeGoodsResult;
    }
    
    
 
    /**
     * 9.9包邮精选
     * @param pageId
     * @return
     */
    public static DaTaoKeGoodsResult getNineGoodsList(Integer pageId, int pageSize) {
        DaTaoKeGoodsResult daTaoKeGoodsResult = new DaTaoKeGoodsResult();
        
        TaoKeAppInfo app = getRandomApp();
        Map<String, String> params = new TreeMap<>();
        params.put("version", "v1.2.2");
        params.put("appKey", app.getAppKey());
        params.put("pageSize", pageSize + "");
        params.put("nineCid", "-1");
        
        if (pageId != null)
            params.put("pageId", pageId + "");
        params.put("sign", getSign(params, app.getAppSecret()));
 
        String result = null;
        try {
            result = HttpUtil.get("https://openapi.dataoke.com/api/goods/nine/op-goods-list", params, new HashMap<>());
        } catch (Exception e) {
            result = HttpUtil.get("https://openapi.dataoke.com/api/goods/nine/op-goods-list", params, new HashMap<>());
        }
        JSONObject json = JSONObject.fromObject(result);
        JSONObject dataJson = json.optJSONObject("data");
        if (dataJson != null) {
            JSONArray array = dataJson.optJSONArray("list");
            if (array != null) {
                List<DaTaoKeDetailV2> list = parseDaTaoKeDetailV2List(array);
                daTaoKeGoodsResult.setGoodsList(list);
            }
            daTaoKeGoodsResult.setPageId(dataJson.optString("pageId"));
            daTaoKeGoodsResult.setTotalCount(dataJson.optLong("totalNum"));
        }
        return daTaoKeGoodsResult;
    }
}