yujian
2019-08-03 9a82179d24ec670ba1aefef6906b8acadf58da6e
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
package com.yeshi.fanli.service.impl.pdd;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.dto.pdd.PDDGoodsResult;
import com.yeshi.fanli.dto.pdd.PDDSearchFilter;
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
import com.yeshi.fanli.exception.pdd.PDDOrderException;
import com.yeshi.fanli.service.inter.pdd.PDDGoodsService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil;
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoUtil;
 
@Service
public class PDDGoodsServiceImpl implements PDDGoodsService {
 
    private static final Map<Long, String> cidMap = new HashMap<Long, String>();
 
    static {
        cidMap.put(1L, "0");// 精选
        cidMap.put(2L, "-11");// 清仓
        cidMap.put(3L, "15");// 百货
        cidMap.put(4L, "4");// 母婴
        cidMap.put(5L, "1");// 食品
        cidMap.put(6L, "14");// 女装
        cidMap.put(7L, "18");// 电器
        cidMap.put(8L, "1281");// 鞋包
        cidMap.put(9L, "1282");// 内衣
        cidMap.put(10L, "16");// 美妆
        cidMap.put(11L, "743");// 男装
        cidMap.put(12L, "2048");// 汽车
        cidMap.put(13L, "13");// 水果
        cidMap.put(14L, "818,1917,2974");// 家居:家纺、家装 家具
        cidMap.put(15L, "2478");// 文具
        cidMap.put(16L, "1451");// 运动
        cidMap.put(17L, "590");// 虚拟
        cidMap.put(18L, "3279");// 医药
    }
 
    @Cacheable(value = "pddCache", key = "'getDetailImageList'+#id")
    @Override
    public List<String> getDetailImageList(Long id) {
        List<String> list = null;
        int count = 0;
        // 重试3次
        while ((list == null || list.size() == 0) && count < 3) {
            list = PinDuoDuoUtil.getDetailImages(id);
            count++;
        }
        if (list == null || list.size() == 0) {
            try {
                Document doc = Jsoup
                        .connect("https://dk.gaoyong666.com/gylm/h5details/v1/details?classtype=1&goodsId=" + id)
                        .userAgent(
                                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36")
                        .get();
                Elements els = doc.getElementsByTag("img");
                for (int i = 0; i < els.size(); i++) {
                    String src = els.get(i).attr("src");
                    if (src.contains("yangkeduo")) {
                        list.add(src);
                    }
                }
 
            } catch (IOException e) {
                e.printStackTrace();
            }
 
        }
 
        return list;
    }
 
    @Override
    public List<GoodsClass> getSpecialClass() {
        List<GoodsClass> list = new ArrayList<GoodsClass>();
        list.add(new GoodsClass(1L, "精选"));
        list.add(new GoodsClass(2L, "清仓"));
        list.add(new GoodsClass(3L, "百货"));
        list.add(new GoodsClass(4L, "母婴"));
        list.add(new GoodsClass(5L, "食品"));
        list.add(new GoodsClass(6L, "女装"));
        list.add(new GoodsClass(7L, "电器"));
        list.add(new GoodsClass(8L, "鞋包"));
        list.add(new GoodsClass(9L, "内衣"));
        list.add(new GoodsClass(10L, "美妆"));
        list.add(new GoodsClass(11L, "男装"));
        list.add(new GoodsClass(12L, "汽车"));
        list.add(new GoodsClass(13L, "水果"));
        list.add(new GoodsClass(14L, "家居"));
        list.add(new GoodsClass(15L, "文具"));
        list.add(new GoodsClass(16L, "运动"));
        list.add(new GoodsClass(17L, "虚拟"));
        list.add(new GoodsClass(18L, "医药"));
        return list;
    }
 
    @Override
    @Transactional
    @Cacheable(value = "pddSpecialCache", key = "'specialSearch-'+#page+'-'+#cid")
    public List<PDDGoodsDetail> specialSearch(Integer page, Long cid) throws PDDOrderException {
        if (cid == null) {
            throw new PDDOrderException(1, "分类id为空");
        }
 
        String pddcid = cidMap.get(cid);
        if (StringUtil.isNullOrEmpty(pddcid)) {
            throw new PDDOrderException(1, "分类id不存在");
        }
 
        PDDSearchFilter pddfilter = new PDDSearchFilter();
 
        // 精选
        if (cid == 1) {
            pddfilter.setPage(page);
            pddfilter.setPageSize(Constant.PAGE_SIZE);
            PDDGoodsResult result = PinDuoDuoApiUtil.searchGoods(pddfilter);
            if (result == null) {
                return null;
            } else {
                return result.getGoodsList();
            }
        }
 
        // 单个分类
        if (!pddcid.contains(",")) {
            pddfilter.setPage(page);
            pddfilter.setPageSize(Constant.PAGE_SIZE);
            pddfilter.setOptId(Long.parseLong(pddcid));
            PDDGoodsResult result = PinDuoDuoApiUtil.searchGoods(pddfilter);
            if (result == null) {
                return null;
            } else {
                return result.getGoodsList();
            }
        }
 
        // 多个分类id处理
        List<PDDGoodsDetail> list = new ArrayList<PDDGoodsDetail>();
        String[] arrayId = pddcid.split(",");
        for (int i = 0; i < arrayId.length; i++) {
            if (i >= 3) {
                break;
            }
            pddfilter.setPage(page);
            pddfilter.setPageSize(10);
            pddfilter.setOptId(Long.parseLong(arrayId[i]));
            PDDGoodsResult result = PinDuoDuoApiUtil.searchGoods(pddfilter);
            if (result != null) {
                List<PDDGoodsDetail> listGoods = result.getGoodsList();
                if (listGoods != null && listGoods.size() > 0) {
                    list.addAll(listGoods);
                }
            }
        }
        return list;
    }
 
    @Override
    @Cacheable(value = "pddSpecialCache", key = "'getTopGoodsList-' + #page + '-' + #sortType")
    public PDDGoodsResult getTopGoodsList(int page, Integer sortType) {
        return PinDuoDuoApiUtil.getTopList(PinDuoDuoApiUtil.PID_FANLI, page, Constant.PAGE_SIZE, sortType);
    }
 
    
    
    @Override
    @Cacheable(value = "pddSpecialCache", key = "'getTodaySaleGoodsList-'")
    public PDDGoodsResult getTodaySaleGoodsList() {
        return PinDuoDuoUtil.getTodaySaleGoods();
    }
}