| | |
| | | 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);
|
| | | |
| | | goodsEvaluateService.offlineTaoBaoGoods(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);
|
| | |
|
| | | }
|
| | |
|
| | | }
|
| | | 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.goods.facade.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; |
| | | |
| | | @Lazy |
| | | @Resource |
| | | private TLJBuyGoodsUpdateService tljBuyGoodsUpdateService; |
| | | |
| | | @Resource |
| | | private TaoBaoGoodsCacheUtil taoBaoGoodsCacheUtil; |
| | | |
| | | @Resource |
| | | private TaoBaoGoodsUpdateDao taoBaoGoodsUpdateDao; |
| | | |
| | | @Lazy |
| | | @Resource |
| | | private TLJFreeBuyGoodsUpdateService tljFreeBuyGoodsUpdateService; |
| | | @Lazy |
| | | @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(String auctionId) { |
| | | // 删除精选库相关的数据 |
| | | qualityFactoryGoodsUpdateService.deleteByTaoBaoGoodsId(auctionId); |
| | | |
| | | // 删除商品本身数据 |
| | | taoBaoGoodsBriefMapper.deleteByAuctionId(auctionId); |
| | | } |
| | | |
| | | @Transactional |
| | | @Override |
| | | public void offlineTaoBaoGoods(String auctionId) { |
| | | // 更新动态数据 |
| | | activityGoodsUpdateService.downTaoBaoGoods(auctionId); |
| | | |
| | | // 删除精选库相关的数据 |
| | | qualityFactoryGoodsUpdateService.deleteByTaoBaoGoodsId(auctionId); |
| | | |
| | | // 删除商品本身数据 |
| | | taoBaoGoodsBriefMapper.deleteByAuctionId(auctionId); |
| | | |
| | | // 更新简版商品状态为下架 |
| | | commonGoodsService.offlineCommonGoods(auctionId, CommonGoods.GOODS_TYPE_TB); |
| | | |
| | | // 分享爆款数据删除 |
| | | shareHotGoodsService.deleteByGoodsId(auctionId); |
| | | |
| | | goodsEvaluateService.offlineTaoBaoGoods(auctionId + ""); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public void updateTaoBaoGoods(TaoBaoGoodsBrief goods) throws TaobaoGoodsUpdateException { |
| | | if (goods == null) |
| | | throw new TaobaoGoodsUpdateException(1, "商品为空"); |
| | | |
| | | if (StringUtil.isNullOrEmpty( goods.getAuctionId())) |
| | | throw new TaobaoGoodsUpdateException(2, "商品ID为空"); |
| | | |
| | | // 设置商品正常上线 |
| | | goods.setState(0); |
| | | // 更新收藏信息 |
| | | try { |
| | | commonGoodsService.updateCommonGoods(CommonGoodsFactory.create(goods)); |
| | | } catch (CommonGoodsException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | TaoBaoGoodsBrief tb = taoBaoGoodsBriefMapper.selectOneByAuctionId(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<String> list = taoBaoGoodsBriefMapper |
| | | .queryCanDeleteGoods(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 2), 0, 2000); |
| | | for (String 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<String> listNeedUpdateGoodsId(long start, int count, int hour) { |
| | | return taoBaoGoodsBriefMapper.listNeedUpdateGoodsId(start, count, hour); |
| | | } |
| | | |
| | | @Override |
| | | public void updateByTaoKeGoodsDetail(String goodsId) { |
| | | try { |
| | | TaoBaoGoodsBrief oldGoods = taoBaoGoodsBriefMapper.selectOneByAuctionId(goodsId); |
| | | if (oldGoods == null) { |
| | | return; |
| | | } |
| | | |
| | | TaoBaoGoodsBrief newGoods = TaoKeApiUtil.searchGoodsDetail(oldGoods.getAuctionId()); |
| | | if (newGoods == null) { |
| | | return; |
| | | } |
| | | |
| | | // 更新精选商品 |
| | | newGoods.setId(oldGoods.getId()); |
| | | 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(goodsId); |
| | | } 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()); |
| | | goods.setMid(goods.getAuctionId()); |
| | | taoBaoGoodsUpdateDao.save(goods); |
| | | taoBaoGoodsCacheUtil.addUpdateHistory(goods.getAuctionId()); |
| | | } |
| | | |
| | | @Async |
| | | @Override |
| | | public void addUpdateQueueAsync(String auctionId) { |
| | | addUpdateQueueSync(auctionId); |
| | | } |
| | | |
| | | /** |
| | | * 同步加入 |
| | | * |
| | | * @param auctionId |
| | | */ |
| | | private void addUpdateQueueSync(String 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<String, 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<String> auctionIds = new ArrayList<>(); |
| | | for (TaoBaoGoodsBrief goods : tempGoodsList) { |
| | | auctionIds.add(goods.getAuctionId()); |
| | | } |
| | | List<TaoBaoGoodsBrief> mList =null; |
| | | try { |
| | | mList = taoBaoGoodsUpdateDao.listByAuctionIds(auctionIds); |
| | | }catch(Exception e){ |
| | | } |
| | | 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 List<CommonGoods> filterImportantTaoBaoGoodsWithCommonGoods(List<CommonGoods> goodsList) { |
| | | |
| | | // 商品过滤不允许出错 |
| | | try { |
| | | Map<String, CommonGoods> finalMap = new HashMap<>(); |
| | | // 原始数据全覆盖 |
| | | List<CommonGoods> tempGoodsList = new ArrayList<>(); |
| | | for (CommonGoods goods : goodsList) { |
| | | if (goods.getGoodsType() == CommonGoods.SHOP_TYPE_TB) { |
| | | finalMap.put(goods.getGoodsId(), goods); |
| | | tempGoodsList.add(goods); |
| | | } |
| | | } |
| | | |
| | | // 先从redis里面取覆盖 |
| | | for (int i = 0; i < tempGoodsList.size(); i++) { |
| | | TaoBaoGoodsBrief goods = taoBaoGoodsCacheUtil |
| | | .getCommonTaoBaoGoodsInfo(tempGoodsList.get(i).getGoodsId()); |
| | | if (goods != null) { |
| | | finalMap.put(goods.getAuctionId(), CommonGoodsFactory.create(goods)); |
| | | tempGoodsList.remove(i--); |
| | | } |
| | | } |
| | | |
| | | // 再从mongodb中取覆盖 |
| | | List<String> auctionIds = new ArrayList<>(); |
| | | for (CommonGoods goods : tempGoodsList) { |
| | | auctionIds.add(goods.getGoodsId()); |
| | | } |
| | | 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(), CommonGoodsFactory.create(goods)); |
| | | } |
| | | |
| | | List<CommonGoods> finalList = new ArrayList<>(); |
| | | for (CommonGoods goods : goodsList) { |
| | | if (goods.getGoodsType() == CommonGoods.GOODS_TYPE_TB) { |
| | | CommonGoods newGoods = finalMap.get(goods.getGoodsId()); |
| | | // 过滤价格,返利比例,券信息 |
| | | goods.setPrice(newGoods.getPrice()); |
| | | goods.setRate(newGoods.getRate()); |
| | | goods.setCouponAmount(newGoods.getCouponAmount()); |
| | | goods.setCouponInfo(newGoods.getCouponInfo()); |
| | | goods.setCouponLeftCount(newGoods.getCouponLeftCount()); |
| | | goods.setCouponStartPrice(newGoods.getCouponStartPrice()); |
| | | goods.setCouponTotalCount(newGoods.getCouponTotalCount()); |
| | | } |
| | | |
| | | 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); |
| | | |
| | | } |
| | | |
| | | } |