yj
2020-03-10 da172401669413a57b92c681740056dfd75d295e
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
package com.yeshi.fanli.service.impl.taobao;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.google.gson.Gson;
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoGoodsBriefMapper;
import com.yeshi.fanli.dao.taobao.TaoBaoGoodsUpdateDao;
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.goods.CommonGoodsException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsUpdateException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.activity.ActivityGoodsUpdateService;
import com.yeshi.fanli.service.inter.dynamic.GoodsEvaluateService;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.service.inter.lable.QualityFactoryGoodsUpdateService;
import com.yeshi.fanli.service.inter.taobao.ShareHotGoodsService;
import com.yeshi.fanli.service.inter.taobao.TLJBuyGoodsUpdateService;
import com.yeshi.fanli.service.inter.taobao.TLJFreeBuyGoodsUpdateService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoGoodsUpdateService;
import com.yeshi.fanli.util.CMQManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.cache.TaoBaoGoodsCacheUtil;
import com.yeshi.fanli.util.factory.CommonGoodsFactory;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
@Service
public class TaoBaoGoodsUpdateServiceImpl implements TaoBaoGoodsUpdateService {
 
    @Resource
    private ActivityGoodsUpdateService activityGoodsUpdateService;
 
    @Resource
    private QualityFactoryGoodsUpdateService qualityFactoryGoodsUpdateService;
 
    @Resource
    private TaoBaoGoodsBriefMapper taoBaoGoodsBriefMapper;
 
    @Resource
    private CommonGoodsService commonGoodsService;
 
    @Lazy
    @Resource
    private ShareHotGoodsService shareHotGoodsService;
 
    @Resource
    private TLJBuyGoodsUpdateService tljBuyGoodsUpdateService;
 
    @Resource
    private TaoBaoGoodsCacheUtil taoBaoGoodsCacheUtil;
 
    @Resource
    private TaoBaoGoodsUpdateDao taoBaoGoodsUpdateDao;
 
    @Resource
    private TLJFreeBuyGoodsUpdateService tljFreeBuyGoodsUpdateService;
    
    @Resource
    private GoodsEvaluateService goodsEvaluateService;
 
    @Override
    public void startUpdate() {
        // 更新时间超过6个小时的做更新
        List<TaoBaoGoodsBrief> list = taoBaoGoodsBriefMapper
                .queryGoodsByMaxUpdateTime(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 6L), 0, 5000);
        for (TaoBaoGoodsBrief goods : list) {
            try {
                TaoBaoGoodsBrief newGoods = TaoKeApiUtil.searchGoodsDetail(goods.getAuctionId());
                if (newGoods != null) {
                    try {
                        updateTaoBaoGoods(newGoods);
                    } catch (TaobaoGoodsUpdateException e) {
                        e.printStackTrace();
                    }
                }
 
            } catch (TaobaoGoodsDownException e) {
                offlineTaoBaoGoods(goods.getAuctionId());
            } catch (Exception e) {
 
            }
        }
    }
 
    @Transactional
    @Override
    public void deleteTaoBaoGoods(Long auctionId) {
        // 删除精选库相关的数据
        qualityFactoryGoodsUpdateService.deleteByTaoBaoGoodsId(auctionId);
 
        // 删除商品本身数据
        taoBaoGoodsBriefMapper.deleteByAuctionId(auctionId);
    }
 
    @Transactional
    @Override
    public void offlineTaoBaoGoods(Long auctionId) {
        // 更新动态数据
        activityGoodsUpdateService.downTaoBaoGoods(auctionId);
 
        // 删除精选库相关的数据
        qualityFactoryGoodsUpdateService.deleteByTaoBaoGoodsId(auctionId);
 
        // 删除商品本身数据
        taoBaoGoodsBriefMapper.deleteByAuctionId(auctionId);
 
        // 更新简版商品状态为下架
        commonGoodsService.offlineCommonGoods(auctionId, CommonGoods.GOODS_TYPE_TB);
 
        // 分享爆款数据删除
        shareHotGoodsService.deleteByGoodsId(auctionId);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateTaoBaoGoods(TaoBaoGoodsBrief goods) throws TaobaoGoodsUpdateException {
        if (goods == null)
            throw new TaobaoGoodsUpdateException(1, "商品为空");
 
        if (goods.getAuctionId() == null || goods.getAuctionId() == 0)
            throw new TaobaoGoodsUpdateException(2, "商品ID为空");
 
        // 设置商品正常上线
        goods.setState(0);
        // 更新收藏信息
        try {
            commonGoodsService.updateCommonGoods(CommonGoodsFactory.create(goods));
        } catch (CommonGoodsException e) {
            e.printStackTrace();
        }
 
        TaoBaoGoodsBrief tb = taoBaoGoodsBriefMapper.selectByPrimaryKey(goods.getAuctionId());
        if (tb != null) {
            goods.setId(tb.getId());
            TaoBaoGoodsBrief updateGoods = getUpdateTaoBaoGoodsBrief(goods);
            System.out.println("更细信息:" + new Gson().toJson(updateGoods));
            taoBaoGoodsBriefMapper.updateByPrimaryKeySelective(updateGoods);
        }
 
        // 更新动态商品
        activityGoodsUpdateService.updateRecommendActivityGoods(goods);
 
        // 更新分享库的商品
        shareHotGoodsService.updateShareGoods(goods);
 
        // 更新自购立减库商品
        tljBuyGoodsUpdateService.updateGoods(goods);
 
        // 更新新人免单库商品
        tljFreeBuyGoodsUpdateService.updateGoods(goods);
 
        goodsEvaluateService.updateTaoBaoGoods(goods);
        
        // 总要商品更新
        updateImplTBGoods(goods);
    }
 
    @Override
    public void deleteOutOfDate() {
        List<Long> list = taoBaoGoodsBriefMapper
                .queryCanDeleteGoods(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 2), 0, 2000);
        for (Long auctionId : list) {
            deleteTaoBaoGoods(auctionId);
        }
    }
 
    @Override
    public TaoBaoGoodsBrief getUpdateTaoBaoGoodsBrief(TaoBaoGoodsBrief goods) {
        String auctionUrl = "https://item.taobao.com/item.htm?id=" + goods.getAuctionId();
        String couponLink = "";
        TaoBaoGoodsBrief updateGoods = new TaoBaoGoodsBrief(goods.getId());
        updateGoods.setUpdatetime(new Date());
        updateGoods.setState(0);
        // 防止是转链的链接,数据过长
        updateGoods.setAuctionUrl(auctionUrl);
        updateGoods.setCouponLink(couponLink);
        // 更新标题,商品图片,销量,价格,券信息,返利信息,店铺信息,分类信息
        updateGoods.setTitle(goods.getTitle());
        updateGoods.setPictUrl(goods.getPictUrl());
        updateGoods.setPictUrlWhite(goods.getPictUrlWhite());
        updateGoods.setImgList(goods.getImgList());
        updateGoods.setBiz30day(goods.getBiz30day());
        updateGoods.setZkPrice(goods.getZkPrice());
        updateGoods.setReservePrice(goods.getReservePrice());
        updateGoods.setTkRate(goods.getTkRate());
        // 券信息
        if (StringUtil.isNullOrEmpty(goods.getCouponInfo()) && StringUtil.isNullOrEmpty(goods.getCouponActivityId())) {
            updateGoods.setCouponActivityId("");
            updateGoods.setCouponAmount(new BigDecimal(0));
            updateGoods.setCouponEffectiveEndTime("");
            updateGoods.setCouponEffectiveStartTime("");
            updateGoods.setCouponInfo("");
            updateGoods.setCouponLeftCount(0);
            updateGoods.setCouponLink("");
            updateGoods.setCouponStartFee(new BigDecimal(0));
            updateGoods.setCouponTotalCount(0); // 没有券了
        } else {// 还有券
            updateGoods.setCouponActivityId(goods.getCouponActivityId());
            updateGoods.setCouponAmount(goods.getCouponAmount());
            updateGoods.setCouponEffectiveEndTime(goods.getCouponEffectiveEndTime());
            updateGoods.setCouponEffectiveStartTime(goods.getCouponEffectiveStartTime());
            updateGoods.setCouponInfo(goods.getCouponInfo());
            updateGoods.setCouponLeftCount(goods.getCouponLeftCount());
            updateGoods.setCouponLink("");
            updateGoods.setCouponStartFee(goods.getCouponStartFee());
            updateGoods.setCouponTotalCount(goods.getCouponTotalCount());
        }
 
        // 店铺信息
        updateGoods.setShopTitle(goods.getShopTitle());
        updateGoods.setSellerId(goods.getSellerId());
 
        // 分类信息
        updateGoods.setRootCatId(goods.getRootCatId());
        updateGoods.setRootCategoryName(goods.getRootCategoryName());
        updateGoods.setLeafCatId(goods.getLeafCatId());
        updateGoods.setLeafName(goods.getLeafName());
        updateGoods.setMaterialLibType(goods.getMaterialLibType());
 
        return updateGoods;
    }
 
    @Override
    public void updateTaoBaoGoods(List<TaoBaoGoodsBrief> goodsList) throws TaobaoGoodsUpdateException {
        if (goodsList == null || goodsList.size() == 0)
            throw new TaobaoGoodsUpdateException(1, "商品列表为空");
 
        List<TaoBaoGoodsBrief> updateList = new ArrayList<>();
 
        for (TaoBaoGoodsBrief goods : goodsList) {
            if (goods.getId() == null || goods.getId() == 0L) {
                throw new TaobaoGoodsUpdateException(1, "商品主键ID为空");
            }
 
            // 需要更新的信息
            TaoBaoGoodsBrief updateGoods = getUpdateTaoBaoGoodsBrief(goods);
            updateGoods.setCreatetime(goods.getCreatetime());
            updateList.add(updateGoods);
        }
 
        taoBaoGoodsBriefMapper.updateBatchSelective(updateList);
    }
 
    @Override
    public List<Long> listNeedUpdateGoodsId(long start, int count, int hour) {
        return taoBaoGoodsBriefMapper.listNeedUpdateGoodsId(start, count, hour);
    }
 
    @Override
    public void updateByTaoKeGoodsDetail(Long id) {
        try {
            TaoBaoGoodsBrief oldGoods = taoBaoGoodsBriefMapper.selectByPrimaryKey(id);
            if (oldGoods == null) {
                return;
            }
 
            TaoBaoGoodsBrief newGoods = TaoKeApiUtil.searchGoodsDetail(id);
            if (newGoods == null) {
                return;
            }
 
            // 更新精选商品
            newGoods.setId(id);
            TaoBaoGoodsBrief updateGoods = getUpdateTaoBaoGoodsBrief(newGoods);
            taoBaoGoodsBriefMapper.updateByPrimaryKeySelective(updateGoods);
 
            // 更新动态商品
            activityGoodsUpdateService.updateRecommendActivityGoods(newGoods);
 
            // 更新收藏信息
            try {
                commonGoodsService.updateCommonGoods(CommonGoodsFactory.create(newGoods));
            } catch (CommonGoodsException e) {
                e.printStackTrace();
            }
 
        } catch (TaobaoGoodsDownException e) {
            offlineTaoBaoGoods(id);
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
    }
 
    /**
     * 更新重要的淘宝数据
     * 
     * @param goods
     */
    public void updateImplTBGoods(TaoBaoGoodsBrief goods) {
        if (goods == null || goods.getAuctionId() == null)
            return;
        if (!taoBaoGoodsCacheUtil.needUpdate(goods.getAuctionId())) {
            return;
        }
        goods.setUpdatetime(new Date());
        if (goods.getId() == null)
            goods.setId(goods.getAuctionId());
        taoBaoGoodsUpdateDao.save(goods);
        taoBaoGoodsCacheUtil.addUpdateHistory(goods.getAuctionId());
    }
 
    @Async
    @Override
    public void addUpdateQueueAsync(Long auctionId) {
        addUpdateQueueSync(auctionId);
    }
 
    /**
     * 同步加入
     * 
     * @param auctionId
     */
    private void addUpdateQueueSync(Long auctionId) {
        try {
            if (taoBaoGoodsCacheUtil.canAddToUpdateQueue(auctionId)) {
                // 添加到更新队列
                CMQManager.getInstance().addTBImpGoodsUpdate(auctionId);
                taoBaoGoodsCacheUtil.addAddToQueueHistory(auctionId);
            }
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
    }
 
    @Async
    @Override
    public void addUpdateQueueAsync(List<TaoBaoGoodsBrief> goodsList) {
        if (goodsList != null)
            for (TaoBaoGoodsBrief goods : goodsList) {
                if (goods != null && goods.getAuctionId() != null)
                    addUpdateQueueSync(goods.getAuctionId());
            }
    }
 
    @Override
    public List<TaoBaoGoodsBrief> filterImportantTaoBaoGoods(List<TaoBaoGoodsBrief> goodsList) {
 
        // 商品过滤不允许出错
        try {
            Map<Long, TaoBaoGoodsBrief> finalMap = new HashMap<>();
            // 原始数据全覆盖
            for (TaoBaoGoodsBrief goods : goodsList) {
                finalMap.put(goods.getAuctionId(), goods);
            }
 
            List<TaoBaoGoodsBrief> tempGoodsList = new ArrayList<>();
            tempGoodsList.addAll(goodsList);
            // 先从redis里面取覆盖
            for (int i = 0; i < tempGoodsList.size(); i++) {
                TaoBaoGoodsBrief goods = taoBaoGoodsCacheUtil
                        .getCommonTaoBaoGoodsInfo(tempGoodsList.get(i).getAuctionId());
                if (goods != null) {
                    finalMap.put(goods.getAuctionId(), goods);
                    tempGoodsList.remove(i--);
                }
            }
 
            // 再从mongodb中取覆盖
            List<Long> auctionIds = new ArrayList<>();
            for (TaoBaoGoodsBrief goods : tempGoodsList) {
                auctionIds.add(goods.getAuctionId());
            }
            List<TaoBaoGoodsBrief> mList = taoBaoGoodsUpdateDao.listByAuctionIds(auctionIds);
            if (mList != null)
                for (TaoBaoGoodsBrief goods : mList) {
                    if (goods.getUpdatetime().getTime() > System.currentTimeMillis() - 1000 * 60 * 60 * 24L)// 在1天内更新的数据才参与过滤
                        finalMap.put(goods.getAuctionId(), goods);
                }
 
            List<TaoBaoGoodsBrief> finalList = new ArrayList<>();
            for (TaoBaoGoodsBrief goods : goodsList) {
                TaoBaoGoodsBrief newGoods = finalMap.get(goods.getAuctionId());
                // 过滤价格,返利比例,券信息
                goods.setZkPrice(newGoods.getZkPrice());
                goods.setTkRate(newGoods.getTkRate());
                goods.setCouponAmount(newGoods.getCouponAmount());
                goods.setCouponActivityId(newGoods.getCouponActivityId());
                goods.setCouponEffectiveEndTime(newGoods.getCouponEffectiveEndTime());
                goods.setCouponEffectiveStartTime(newGoods.getCouponEffectiveStartTime());
                goods.setCouponStartFee(newGoods.getCouponStartFee());
                goods.setCouponTotalCount(newGoods.getCouponTotalCount());
                goods.setCouponLeftCount(newGoods.getCouponLeftCount());
 
                finalList.add(goods);
            }
            return finalList;
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
        return goodsList;
    }
 
    @Override
    public int listAndRemoveByMaxUpdateTime(Date maxTime, int page, int pageSize) {
        return taoBaoGoodsUpdateDao.listAndRemoveByMaxUpdateTime(maxTime, (page - 1) * pageSize, pageSize);
    }
 
    @Override
    public long countByMaxUpdateTime(Date maxTime) {
        return taoBaoGoodsUpdateDao.countByMaxUpdateTime(maxTime);
 
    }
 
}