admin
2019-07-09 531d93708df8017e59830f15b41f3cc42d6126e6
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
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.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.ActivityService;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.service.inter.lable.QualityFactoryService;
import com.yeshi.fanli.service.inter.taobao.ShareHotGoodsService;
import com.yeshi.fanli.service.inter.taobao.TLJBuyGoodsService;
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 ActivityService activityService;
 
    @Resource
    private QualityFactoryService qualityFactoryService;
 
    @Resource
    private TaoBaoGoodsBriefMapper taoBaoGoodsBriefMapper;
 
    @Resource
    private CommonGoodsService commonGoodsService;
 
    @Resource
    private ShareHotGoodsService shareHotGoodsService;
 
    @Resource
    private TLJBuyGoodsService tljBuyGoodsService;
 
    @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) {
        // 删除精选库相关的数据
        qualityFactoryService.deleteByTaoBaoGoodsId(auctionId);
 
        // 删除商品本身数据
        taoBaoGoodsBriefMapper.deleteByAuctionId(auctionId);
    }
 
    @Transactional
    @Override
    public void offlineTaoBaoGoods(Long auctionId) {
        // 更新动态数据
        activityService.downTaoBaoGoods(auctionId);
 
        // 删除精选库相关的数据
        qualityFactoryService.deleteByTaoBaoGoodsId(auctionId);
 
        // 删除商品本身数据
        taoBaoGoodsBriefMapper.deleteByAuctionId(auctionId);
 
        // 更新简版商品状态为下架
        commonGoodsService.offlineCommonGoods(auctionId, CommonGoods.GOODS_TYPE_TB);
 
        // 分享爆款数据删除
        shareHotGoodsService.deleteByGoodsId(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();
        }
 
        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);
        }
 
        // 更新动态商品
        activityService.updateRecommendActivityGoods(goods);
 
        // 更新分享库的商品
        shareHotGoodsService.updateShareGoods(goods);
 
        // 更新自购立减库商品
        tljBuyGoodsService.updateGoods(goods);
    }
 
    @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.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);
 
            // 更新动态商品
            activityService.updateRecommendActivityGoods(newGoods);
 
            // 更新收藏信息
            try {
                commonGoodsService.updateCommonGoods(CommonGoodsFactory.create(newGoods));
            } catch (CommonGoodsException e) {
                e.printStackTrace();
            }
 
        } catch (TaobaoGoodsDownException e) {
            offlineTaoBaoGoods(id);
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
    }
}