admin
2019-07-10 a4cc097c7a5bd6a0b8397fb232484df9e01ed4f8
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
package com.yeshi.fanli.service.impl.goods.recommend;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
import javax.annotation.Resource;
 
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dto.dataoke.DaTaoKeGoodsResult;
import com.yeshi.fanli.entity.taobao.TLJBuyGoods;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.entity.taobao.dataoke.DaTaoKeDetailV2;
import com.yeshi.fanli.service.inter.goods.recommend.HomeRecommendGoodsService;
import com.yeshi.fanli.service.inter.goods.recommend.RecommendGoodsDeleteHistoryService;
import com.yeshi.fanli.service.inter.taobao.TLJBuyGoodsService;
import com.yeshi.fanli.service.inter.taobao.dataoke.DaTaoKeGoodsDetailService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.dataoke.DaTaoKeApiUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
@Service
public class HomeRecommendGoodsServiceImpl implements HomeRecommendGoodsService {
 
    @Resource
    private RecommendGoodsDeleteHistoryService recommendGoodsDeleteHistoryService;
 
    @Resource
    private DaTaoKeGoodsDetailService daTaoKeGoodsDetailService;
 
    @Resource
    private EhCacheCacheManager ehCacheCacheManager;
 
    @Resource
    private TLJBuyGoodsService tljBuyGoodsService;
 
    @Override
    public List<TaoBaoGoodsBrief> listGoodsByPage(Long uid, String device, String imei, String idfa, int page) {
        List<TaoBaoGoodsBrief> finalList = new ArrayList<>();
        if (page == 1) {
            List<TaoBaoGoodsBrief> tempList = listDTKTimeRankGoods(device, StringUtil.Md5(device));
            if (tempList != null && tempList.size() > 0)
                finalList.addAll(tempList);
            tempList = guessDeviceLike(device, imei, idfa);
            if (tempList != null && tempList.size() > 0)
                finalList.addAll(tempList);
            tempList = listDTKHotRankGoods(device, StringUtil.Md5(device));
            if (tempList != null && tempList.size() > 0)
                finalList.addAll(tempList);
        } else if (page == 2) {
            List<TaoBaoGoodsBrief> tempList = listDTKDayRankGoods(device);
            if (tempList != null && tempList.size() > 0)
                finalList.addAll(tempList);
        } else if (page >= 3) {
            List<TaoBaoGoodsBrief> tempList = taoKeGoodsCouponList(page - 2);
            if (tempList != null && tempList.size() > 0)
                finalList.addAll(tempList);
        }
        return finalList;
    }
 
    // 大淘客实时热销
    public List<TaoBaoGoodsBrief> listDTKTimeRankGoods(String device, String deviceMd5) {
        String name = "recommendUserCache";
        String key = "listDTKTimeRankGoods-" + StringUtil.Md5(device);
        List<TaoBaoGoodsBrief> fList = getCacheObjList(name, key, TaoBaoGoodsBrief.class);
        if (fList != null && fList.size() > 0)
            return fList;
        List<TaoBaoGoodsBrief> list = listRankGoods(DaTaoKeApiUtil.RANK_TYPE_TIME);
        list = recommendGoodsDeleteHistoryService.filterGoods(device, list);
        List<TaoBaoGoodsBrief> randomList = new ArrayList<>();
        randomList.addAll(list);
        Collections.shuffle(randomList);
        fList = randomList.size() < 5 ? randomList : randomList.subList(0, 5);
        if (fList != null && fList.size() > 0)
            saveObjList(fList, name, key);
        return fList;
    }
 
    // 大淘客热推榜
    public List<TaoBaoGoodsBrief> listDTKHotRankGoods(String device, String deviceMd5) {
        String name = "recommendUserCache";
        String key = "listDTKHotRankGoods-" + StringUtil.Md5(device);
        List<TaoBaoGoodsBrief> fList = getCacheObjList(name, key, TaoBaoGoodsBrief.class);
        if (fList != null && fList.size() > 0)
            return fList;
        List<TaoBaoGoodsBrief> list = listRankGoods(DaTaoKeApiUtil.RANK_TYPE_HOT);
        list = recommendGoodsDeleteHistoryService.filterGoods(device, list);
        List<TaoBaoGoodsBrief> randomList = new ArrayList<>();
        randomList.addAll(list);
        Collections.shuffle(randomList);
        fList = randomList.size() < 5 ? randomList : randomList.subList(0, 5);
        if (fList != null && fList.size() > 0)
            saveObjList(fList, name, key);
        return fList;
    }
 
    // 大淘客全天热榜
    public List<TaoBaoGoodsBrief> listDTKDayRankGoods(String device) {
        String name = "recommendUserCache";
        String key = "listDTKDayRankGoods-" + StringUtil.Md5(device);
        List<TaoBaoGoodsBrief> fList = getCacheObjList(name, key, TaoBaoGoodsBrief.class);
        if (fList != null && fList.size() > 0)
            return fList;
 
        List<TaoBaoGoodsBrief> list = listRankGoods(DaTaoKeApiUtil.RANK_TYPE_DAY);
        list = recommendGoodsDeleteHistoryService.filterGoods(device, list);
        List<TaoBaoGoodsBrief> randomList = new ArrayList<>();
        randomList.addAll(list);
        Collections.shuffle(randomList);
        fList = randomList.size() < 20 ? randomList : randomList.subList(0, 20);
        if (fList != null && fList.size() > 0)
            saveObjList(fList, name, key);
        return fList;
    }
 
    // 设备猜你喜欢
    public List<TaoBaoGoodsBrief> guessDeviceLike(String device, String imei, String idfa) {
 
        String name = "recommendUserCache";
        String key = "guessDeviceLike-" + StringUtil.Md5(device);
        List<TaoBaoGoodsBrief> fList = getCacheObjList(name, key, TaoBaoGoodsBrief.class);
        if (fList != null && fList.size() > 0)
            return fList;
 
        TaoBaoSearchResult result = TaoKeApiUtil.guessDeviceLike(1, 50, imei, idfa);
        List<TaoBaoGoodsBrief> finalList = new ArrayList<>();
 
        if (result != null && result.getTaoBaoGoodsBriefs() != null) {
            List<TaoBaoGoodsBrief> list = result.getTaoBaoGoodsBriefs();
            // 设备推荐
            if (list != null)
                for (TaoBaoGoodsBrief goods : list) {
                    if (!StringUtil.isNullOrEmpty(goods.getCouponInfo()) && goods.getCouponAmount() != null
                            && goods.getCouponAmount().compareTo(new BigDecimal("5")) >= 0 && goods.getBiz30day() > 1000
                            && !StringUtil.isNullOrEmpty(goods.getPictUrlWhite())) {
                        finalList.add(goods);
                    }
                }
        }
        finalList = recommendGoodsDeleteHistoryService.filterGoods(device, finalList);
        Collections.shuffle(finalList);
        fList = finalList.size() < 5 ? finalList : finalList.subList(0, 5);
        if (fList != null && fList.size() > 0)
            saveObjList(fList, name, key);
        return fList;
    }
 
    /**
     * 获取大淘客的商品厂库
     * 
     * @return
     */
    public List<TaoBaoGoodsBrief> listRankGoods(int type) {
 
        String name = "recommendLibraryCache";
        String key = "listRankGoods-" + type;
        List<TaoBaoGoodsBrief> fList = getCacheObjList(name, key, TaoBaoGoodsBrief.class);
        if (fList != null && fList.size() > 0)
            return fList;
 
        DaTaoKeGoodsResult result = DaTaoKeApiUtil.getRankingList(type, null);
        List<DaTaoKeDetailV2> list = result.getGoodsList();
        List<TaoBaoGoodsBrief> goodsList = new ArrayList<>();
        if (list != null)
            for (DaTaoKeDetailV2 v2 : list)
                goodsList.add(TaoBaoUtil.convert(v2));
        if (goodsList == null || goodsList.size() == 0) {
            if (1 / 0 > 0)
                return null;
        }
        if (goodsList != null && goodsList.size() > 0)
            saveObjList(goodsList, name, key);
 
        return goodsList;
    }
 
    /**
     * 淘客好券直播
     * 
     * @param page
     * @return
     */
    public List<TaoBaoGoodsBrief> taoKeGoodsCouponList(int page) {
        String name = "recommendLibraryCache";
        String key = "taoKeGoodsCouponList-" + page;
        List<TaoBaoGoodsBrief> list = getCacheObjList(name, key, TaoBaoGoodsBrief.class);
        if (list != null && list.size() > 0)
            return list;
 
        TaoBaoSearchResult result = TaoKeApiUtil.getMaterialByMaterialId(3756, page, Constant.PAGE_SIZE);
        if (result != null) {
            list = daTaoKeGoodsDetailService.filterTaoBaoGoods(result.getTaoBaoGoodsBriefs());
            saveObjList(list, name, key);
            return list;
        }
        return null;
    }
 
    @SuppressWarnings("unchecked")
    private <T> List<T> getCacheObjList(String name, String key, Class<T> clazz) {
        Cache cache = ehCacheCacheManager.getCache(name);
        ValueWrapper vw = cache.get(key);
        if (vw != null)
            return (List<T>) vw.get();
        return null;
    }
 
    private <T> T saveObjList(List<T> clazzList, String name, String key) {
        if (clazzList == null)
            return null;
        List<T> tList = new ArrayList<>();
        for (T t : clazzList)
            tList.add(t);
        Cache cache = ehCacheCacheManager.getCache(name);
        cache.put(key, tList);
        return null;
    }
 
    @Override
    public List<TLJBuyGoods> getZiGouLiJianHotGoods(String device) {
 
        String name = "recommendUserCache";
        String key = "getZiGouLiJianHotGoods-" + StringUtil.Md5(device);
        List<TLJBuyGoods> fList = getCacheObjList(name, key, TLJBuyGoods.class);
        if (fList != null && fList.size() > 0)
            return fList;
 
        List<TLJBuyGoods> list = tljBuyGoodsService
                .listByDay(TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd"));
        List<TaoBaoGoodsBrief> finalList = new ArrayList<>();
        Map<Long, TLJBuyGoods> map = new ConcurrentHashMap<>();
        if (list != null)
            for (TLJBuyGoods goods : list) {
                map.put(goods.getGoods().getAuctionId(), goods);
                finalList.add(goods.getGoods());
            }
 
        finalList = recommendGoodsDeleteHistoryService.filterGoods(device, finalList);
 
        if (finalList != null && finalList.size() > 0) {
            Collections.shuffle(finalList);
            if (finalList.size() >= 3)
                finalList = finalList.subList(0, 3);
            list = new ArrayList<>();
            for (TaoBaoGoodsBrief goods : finalList) {
                list.add(map.get(goods.getAuctionId()));
            }
            // 缓存
            if (list.size() > 0) {
                saveObjList(list, name, key);
            }
            return list;
        }
 
        return null;
    }
 
}