admin
2018-12-04 57b80e02ce56e3cacff75ba789fc633ab46b911a
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
package com.yeshi.fanli.service.impl.taobao;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
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.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.goods.CommonGoods;
import com.yeshi.fanli.service.inter.activity.ActivityService;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.service.inter.goods.RecommendSectionGoodsService;
import com.yeshi.fanli.service.inter.lable.QualityFactoryService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoGoodsUpdateService;
import com.yeshi.fanli.util.BeanUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.CommonGoodsFactory;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
@Service
public class TaoBaoGoodsUpdateServiceImpl implements TaoBaoGoodsUpdateService {
    @Resource
    private RecommendSectionGoodsService recommendSectionGoodsService;
 
    @Resource
    private ActivityService activityService;
 
    @Resource
    private QualityFactoryService qualityFactoryService;
 
    @Resource
    private TaoBaoGoodsBriefMapper taoBaoGoodsBriefMapper;
 
    @Resource
    private CommonGoodsService commonGoodsService;
 
    @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) {
                downTaoBaoGoods(goods.getAuctionId());
            } catch (Exception e) {
 
            }
        }
    }
 
    @Transactional
    @Override
    public void deleteTaoBaoGoods(Long auctionId) {
        // 删除首页推荐数据
        recommendSectionGoodsService.deleteRecommendSectionGoodsByTbAuctionId(auctionId);
 
        // 删除精选库相关的数据
        qualityFactoryService.deleteByTbAuctionId(auctionId);
 
        // 删除商品本身数据
        taoBaoGoodsBriefMapper.deleteByAuctionId(auctionId);
    }
 
    @Transactional
    @Override
    public void downTaoBaoGoods(Long auctionId) {
        // 删除首页推荐数据
        recommendSectionGoodsService.deleteRecommendSectionGoodsByTbAuctionId(auctionId);
        // 更新动态数据
        activityService.downTaoBaoGoods(auctionId);
 
        // 删除精选库相关的数据
        qualityFactoryService.deleteByTbAuctionId(auctionId);
 
        // 删除商品本身数据
        taoBaoGoodsBriefMapper.deleteByAuctionId(auctionId);
    }
 
    @Transactional
    @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为空");
 
        // 更新收藏信息
        try {
            commonGoodsService.updateCommonGoods(CommonGoodsFactory.create(goods));
        } catch (CommonGoodsException e) {
            e.printStackTrace();
        }
 
        List<TaoBaoGoodsBrief> goodsList = taoBaoGoodsBriefMapper.queryByAuctionId(goods.getAuctionId());
        if (goodsList != null)
            for (TaoBaoGoodsBrief tb : goodsList) {
                goods.setId(tb.getId());
                TaoBaoGoodsBrief updateGoods = getUpdateTaoBaoGoodsBrief(goods);
                System.out.println("更细信息:" + new Gson().toJson(updateGoods));
                taoBaoGoodsBriefMapper.updateByPrimaryKeySelective(updateGoods);
 
            }
    }
 
    @Override
    public void deleteOutOfDate() {
        TaoBaoGoodsBriefMapper taoBaoGoodsBriefMapper = BeanUtil.getBean(TaoBaoGoodsBriefMapper.class);
        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.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());
 
        return updateGoods;
    }
 
    @Override
    public void updateTaoBaoGoods(List<TaoBaoGoodsBrief> goodsList) throws TaobaoGoodsUpdateException {
        if (goodsList == null)
            return;
        List<TaoBaoGoodsBrief> updateList = new ArrayList<>();
        for (TaoBaoGoodsBrief goods : goodsList) {
            if (goods.getId() == null || goods.getId() == 0L)
                throw new TaobaoGoodsUpdateException(1, "商品主键ID为空");
            updateList.add(goods);
        }
        taoBaoGoodsBriefMapper.updateBatchSelective(updateList);
    }
 
}