admin
2019-09-18 b19d2e730b0ea707b500b307f58c46ba0cef695f
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
package com.yeshi.fanli.service.impl.brand;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.brand.BrandGoodsCaheDao;
import com.yeshi.fanli.dto.jd.JDFilter;
import com.yeshi.fanli.dto.jd.JDSearchFilter;
import com.yeshi.fanli.dto.jd.JDSearchResult;
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.brand.BrandGoodsCahe;
import com.yeshi.fanli.entity.brand.BrandInfo;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.fanli.entity.taobao.SearchFilter;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.service.inter.brand.BrandGoodsCaheService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory;
import com.yeshi.fanli.util.jd.JDApiUtil;
import com.yeshi.fanli.util.jd.JDUtil;
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil;
import com.yeshi.fanli.util.taobao.SearchFilterUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
@Service
public class BrandGoodsCaheServiceImpl implements BrandGoodsCaheService {
    @Resource
    private HongBaoManageService hongBaoManageService;
 
    @Resource
    private ConfigService configService;
    
    @Resource
    private BrandGoodsCaheDao brandGoodsCaheDao;
    
    
    @Override
    public int addBrandGoods(BrandInfo brandInfo) {
        int count = 0;
        // 淘宝
        count += addBrandGoodsTB(brandInfo);
        // 京东
        count += addBrandGoodsJD(brandInfo);
        // 拼多多
        count += addBrandGoodsPDD(brandInfo);
        
        return count;
    }
    
    
    /**
     * 淘宝商品
     * @param brandInfo
     * @return
     */
    private int addBrandGoodsTB(BrandInfo brandInfo) {
        SearchFilter filter = new SearchFilter();
        filter.setKey(brandInfo.getName());
        filter.setPage(1);
        filter.setPageSize(50);
        filter.setTmall(true);
        filter.setSort(TaoBaoUtil.SORT_SALE_HIGH_TO_LOW);
        TaoBaoSearchResult searchResult = TaoKeApiUtil.searchWuLiao(filter);
        if (searchResult == null || searchResult.getTaoBaoGoodsBriefs() ==  null 
                || searchResult.getTaoBaoGoodsBriefs().size() == 0)
            return 0;
        
        BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
        BigDecimal shareRate = hongBaoManageService.getShareRate();
        List<TaoBaoGoodsBrief> listGoods = searchResult.getTaoBaoGoodsBriefs();
        for (TaoBaoGoodsBrief goods: listGoods) {
            BrandGoodsCahe brandGoods = new BrandGoodsCahe();
            brandGoods.setBrandId(brandInfo.getId());
            brandGoods.setWeight((int) (Math.random() * 1000));
            brandGoods.setGoods(GoodsDetailVOFactory.convertTaoBao(goods, null, fanLiRate, shareRate));
            brandGoods.setCreateTime(new Date());
            brandGoods.setUpdateTime(new Date());
            brandGoodsCaheDao.save(brandGoods);
        }
        return listGoods.size();
    }
    
    
    /**
     *  京东商品
     *  
     * @param brandInfo
     * @return
     */
    private int addBrandGoodsJD(BrandInfo brandInfo) {
        JDSearchResult result = null;
        String way = configService.get("jd_api_search_key");
        if ("1".equals(way)) {
            JDFilter filterAPI = new JDFilter();
            filterAPI.setKeyword(SearchFilterUtil.filterSearchContent(brandInfo.getName()));
            filterAPI.setPageIndex(1);
            filterAPI.setPageSize(100);
            filterAPI.setSort(JDFilter.SORT_DESC);
            filterAPI.setSortName(JDFilter.SORTNAME_ORDER_COUNT_30DAYS);
            result = JDApiUtil.queryByKey(filterAPI);
        } else {
            // 网页爬取
            JDSearchFilter jdfilter = new JDSearchFilter();
            jdfilter.setKey(SearchFilterUtil.filterSearchContent(brandInfo.getName()));
            jdfilter.setPageNo(1);
            jdfilter.setPageSize(100);
            jdfilter.setSort(JDSearchFilter.SORT_DESC);
            jdfilter.setSortName(JDSearchFilter.SORTNAME_ORDER_COUNT_30DAYS);
            result = JDUtil.searchByKey(jdfilter);
        }
 
        int count = 0;
        if (result != null) {
            PageEntity pageEntity = result.getPageEntity();
            if (pageEntity != null) {
                count = (int) pageEntity.getTotalCount();
            }
 
            List<JDGoods> goodsList = result.getGoodsList();
            if (goodsList != null && goodsList.size() > 0) {
                BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
                BigDecimal shareRate = hongBaoManageService.getShareRate();
                for (JDGoods goods : goodsList) {
                    BrandGoodsCahe brandGoods = new BrandGoodsCahe();
                    brandGoods.setBrandId(brandInfo.getId());
                    brandGoods.setWeight((int) (Math.random() * 1000));
                    brandGoods.setGoods(GoodsDetailVOFactory.convertJDGoods(goods, fanLiRate, shareRate));
                    brandGoods.setCreateTime(new Date());
                    brandGoods.setUpdateTime(new Date());
                    brandGoodsCaheDao.save(brandGoods);
                }
            }
        }
          return count;
    }
    
    
    /**
     * 拼多多商品
     * @param brandInfo
     * @return
     */
    private int addBrandGoodsPDD(BrandInfo brandInfo) {
        PDDSearchFilter pddfilter = new PDDSearchFilter();
        pddfilter.setKw(brandInfo.getName());
        pddfilter.setPage(1);
        pddfilter.setPageSize(50);
        pddfilter.setSortType(6);
 
        int count = 0;
        PDDGoodsResult result = PinDuoDuoApiUtil.searchGoods(pddfilter);
        if (result != null) {
            count = result.getTotalCount();
            List<PDDGoodsDetail> goodsList = result.getGoodsList();
            if (goodsList != null && goodsList.size() > 0) {
                BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
                BigDecimal shareRate = hongBaoManageService.getShareRate();
                for (PDDGoodsDetail goods : goodsList) {
                    BrandGoodsCahe brandGoods = new BrandGoodsCahe();
                    brandGoods.setBrandId(brandInfo.getId());
                    brandGoods.setWeight((int) (Math.random() * 1000));
                    brandGoods.setGoods(GoodsDetailVOFactory.convertPDDGoods(goods, fanLiRate, shareRate));
                    brandGoods.setCreateTime(new Date());
                    brandGoods.setUpdateTime(new Date());
                    brandGoodsCaheDao.save(brandGoods);
                }
            }
        }
        return count;
    }
}