yujian
2020-06-09 554de444b87aab5f93cb1593a8095612cf9479a7
fanli/src/main/java/com/yeshi/fanli/service/impl/goods/TaoBaoGoodsBriefServiceImpl.java
@@ -1,49 +1,27 @@
package com.yeshi.fanli.service.impl.goods;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
import org.yeshi.utils.NumberUtil;
import com.yeshi.fanli.dao.config.AddEveryDayTaskDao;
import com.yeshi.fanli.dao.goods.RecommendSectionGoodsDao;
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoGoodsBriefMapper;
import com.yeshi.fanli.dao.taobao.TaoBaoGoodsBriefDao;
import com.yeshi.fanli.entity.admin.EveryDayTask;
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
import com.yeshi.fanli.entity.bus.recommend.RecommendSection;
import com.yeshi.fanli.entity.bus.recommend.RecommendSectionGoods;
import com.yeshi.fanli.entity.taobao.PidUser;
import com.yeshi.fanli.entity.taobao.SearchFilter;
import com.yeshi.fanli.entity.taobao.TBPid;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoUnionConfig;
import com.yeshi.fanli.entity.taobao.TaoKeAppInfo;
import com.yeshi.fanli.exception.ExistObjectException;
import com.yeshi.fanli.exception.NotExistObjectException;
import com.yeshi.fanli.exception.ShareGoodsException;
import com.yeshi.fanli.exception.share.ShareGoodsException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.service.inter.goods.ClassRecommendGoodsService;
import com.yeshi.fanli.service.inter.goods.GoodsClassService;
import com.yeshi.fanli.service.inter.goods.RecommendSectionGoodsService;
import com.yeshi.fanli.service.inter.goods.RecommendSectionService;
import com.yeshi.fanli.service.inter.goods.TaoBaoCouponService;
import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionConfigService;
import com.yeshi.fanli.service.inter.user.TBPidService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
@@ -51,31 +29,7 @@
public class TaoBaoGoodsBriefServiceImpl implements TaoBaoGoodsBriefService {
   @Resource
   private TaoBaoGoodsBriefDao taoBaoGoodsBriefDao;
   @Resource
   private RecommendSectionGoodsDao recommendSectionGoodsDao;
   @Resource
   private RecommendSectionGoodsService recommendSectionGoodsService;
   @Resource
   private RecommendSectionService recommendSectionService;
   @Resource
   private ClassRecommendGoodsService classRecommendGoodsService;
   @Resource
   private GoodsClassService goodsClassService;
   @Resource
   private AddEveryDayTaskDao addEveryDayTaskDao;
   @Resource
   private HongBaoManageService hongBaoManageService;
   @Resource
   private TaoBaoCouponService taoBaoCouponService;
   @Resource
   private TaoBaoGoodsBriefMapper taoBaoGoodsBriefMapper;
@@ -86,220 +40,10 @@
   @Resource
   private TaoBaoUnionConfigService taoBaoUnionConfigService;
   public void save(TaoBaoGoodsBrief taoBaoGoodsBrief) throws ExistObjectException {
      long auctionId = taoBaoGoodsBrief.getAuctionId();
      List<TaoBaoGoodsBrief> list = taoBaoGoodsBriefDao.list("from TaoBaoGoodsBrief tb where tb.auctionId = ?",
            new Serializable[] { auctionId });
      if (list.size() > 0) {
         throw new ExistObjectException("已存在该商品");
      }
      taoBaoGoodsBrief.setCreatetime(new Date());
      taoBaoGoodsBriefDao.create(taoBaoGoodsBrief);
   }
   public List<TaoBaoGoodsBrief> getTBList(int index, String key) {
      int start = index * Constant.PAGE_SIZE;
      boolean b = NumberUtil.isNumeric(key);
      if (b) {
         return taoBaoGoodsBriefDao.list(
               "from TaoBaoGoodsBrief tb where tb.title like ? or tb.auctionId = ? order by tb.id desc", start,
               Constant.PAGE_SIZE, new Serializable[] { "%" + key + "%", Long.parseLong(key) });
      } else {
         return taoBaoGoodsBriefDao.list("from TaoBaoGoodsBrief tb where tb.title like ? order by tb.id desc", start,
               Constant.PAGE_SIZE, new Serializable[] { "%" + key + "%" });
      }
   }
   public int getCount(String key) {
      boolean b = NumberUtil.isNumeric(key);
      Long lcount = 0L;
      if (b) {
         lcount = taoBaoGoodsBriefDao.getCount(
               "select count(*) from TaoBaoGoodsBrief tb where tb.title like ? or tb.auctionId = ? ",
               new Serializable[] { "%" + key + "%", Long.parseLong(key) });
      } else {
         lcount = taoBaoGoodsBriefDao.getCount("select count(*) from TaoBaoGoodsBrief tb where tb.title like ? ",
               new Serializable[] { "%" + key + "%" });
      }
      return lcount.intValue();
   }
   public TaoBaoGoodsBrief getTaoBao(long id) {
      return taoBaoGoodsBriefDao.find(TaoBaoGoodsBrief.class, id);
   }
   public TaoBaoGoodsBrief getTaoBaoByAuctionId(long id) {
      List<TaoBaoGoodsBrief> list = taoBaoGoodsBriefDao
            .list("from  TaoBaoGoodsBrief goods where goods.auctionId=" + id);
      if (list == null || list.size() == 0)
         return null;
      else
         return list.get(0);
   }
   @Transactional
   public void deleteTaoBaoGoods(final long id) {
      taoBaoGoodsBriefDao.excute(new HibernateCallback() {
         public Object doInHibernate(Session session) throws HibernateException {
            recommendSectionGoodsService.deleteRecommendSectionGoodsByTB(id);
            classRecommendGoodsService.deleteRecommendGoodsByTB(id);
            Query query = session.createQuery("delete from TaoBaoGoodsBrief tb where tb.id = ?");
            query.setParameter(0, id);
            return query.executeUpdate();
         }
      });
   }
   @Transactional
   public void addRecommendSectionGoods(long rsid, long tbid) throws NotExistObjectException, ExistObjectException {
      RecommendSection recommendSection = recommendSectionService.getRecommendSection(rsid);
      TaoBaoGoodsBrief taobao = taoBaoGoodsBriefDao.find(TaoBaoGoodsBrief.class, tbid);
      if (recommendSection == null || taobao == null) {
         throw new NotExistObjectException("不存在该板块或者淘宝商品");
      }
      recommendSectionGoodsService.addRecommendSectionGoods(recommendSection, taobao);
   }
   /**
    * 添加商品到每日任务
    */
   @Override
   public void addEveryDayTask(long tbid) throws NotExistObjectException, ExistObjectException {
      TaoBaoGoodsBrief taobao = taoBaoGoodsBriefDao.find(TaoBaoGoodsBrief.class, tbid);
      if (taobao == null) {
         throw new NotExistObjectException("不存在该淘宝商品");
      }
      EveryDayTask everyDaoTask = new EveryDayTask();
      everyDaoTask.setEtGoods(taobao);
      everyDaoTask.setEtCreateTime(new Date());
      addEveryDayTaskDao.create(everyDaoTask);
   }
   @Transactional
   public void addClassRecommendGoods(long gcid, long tbid) throws NotExistObjectException, ExistObjectException {
      GoodsClass goodsClass = goodsClassService.getGoodsClass(gcid);
      TaoBaoGoodsBrief taobao = taoBaoGoodsBriefDao.find(TaoBaoGoodsBrief.class, tbid);
      if (goodsClass == null || taobao == null) {
         throw new NotExistObjectException("不存在该分类或者淘宝商品");
      }
      classRecommendGoodsService.addRecommendGoods(goodsClass, taobao);
   }
   public TaoBaoGoodsBrief getTaoBaoByAuctionId(String tbid) {
      List<TaoBaoGoodsBrief> list = taoBaoGoodsBriefDao.list("from TaoBaoGoodsBrief tb where tb.auctionId=?",
            new Serializable[] { Long.parseLong(tbid) });
      if (list.size() == 0) {
         return null;
      }
      return list.get(0);
   }
   public void updateTBGoods(TaoBaoGoodsBrief tb) throws NotExistObjectException {
      TaoBaoGoodsBrief find = taoBaoGoodsBriefDao.find(TaoBaoGoodsBrief.class, tb.getId());
      if (find == null) {
         throw new NotExistObjectException("不存在该对象");
      }
      find.setPictUrl(tb.getPictUrl());
      find.setTitle(tb.getTitle());
      taoBaoGoodsBriefDao.update(find);
   }
   @SuppressWarnings("unchecked")
   public List<TaoBaoGoodsBrief> getAllTaoBao() {
      return (List<TaoBaoGoodsBrief>) taoBaoGoodsBriefDao.excute(new HibernateCallback<List<TaoBaoGoodsBrief>>() {
         public List<TaoBaoGoodsBrief> doInHibernate(Session session) throws HibernateException {
            Query query = session.createQuery("from TaoBaoGoodsBrief ");
            return query.list();
         }
      });
   }
   public void getUpdateTaoBao() {
      List<TaoBaoGoodsBrief> list = getAllTaoBao();
      int len = list.size();
      for (TaoBaoGoodsBrief taoBaoGoodsBrief : list) {
         try {
            Thread.sleep(2000);
         } catch (InterruptedException e) {
            continue;
         }
         try {
            updateTaoBaoGoods(taoBaoGoodsBrief);
         } catch (TaobaoGoodsDownException e) {
            e.printStackTrace();
         }
         len--;
         System.out.println("还剩:" + len + "个商品未更新");
      }
      return;
   }
   public void updateTaoBaoGoods(TaoBaoGoodsBrief taoBaoGoodsBrief) throws TaobaoGoodsDownException {
      SearchFilter filter = new SearchFilter();
      filter.setKey("https://item.taobao.com/item.htm?id=" + taoBaoGoodsBrief.getAuctionId());
      TaoBaoGoodsBrief tb = TaoKeApiUtil.searchGoodsDetail(taoBaoGoodsBrief.getAuctionId());
      if (tb == null) {
         return;
      }
      taoBaoGoodsBrief.setCouponLink(tb.getCouponLink());
      taoBaoGoodsBrief.setCouponActivityId(tb.getCouponActivityId());
      taoBaoGoodsBrief.setCouponAmount(tb.getCouponAmount());
      taoBaoGoodsBrief.setCouponEffectiveStartTime(tb.getCouponEffectiveStartTime());
      taoBaoGoodsBrief.setCouponEffectiveEndTime(tb.getCouponEffectiveEndTime());
      taoBaoGoodsBrief.setCouponInfo(tb.getCouponInfo());
      taoBaoGoodsBrief.setCouponLeftCount(tb.getCouponLeftCount());
      taoBaoGoodsBrief.setCouponLinkTaoToken(tb.getCouponLinkTaoToken());
      taoBaoGoodsBrief.setCouponOriLink(tb.getCouponOriLink());
      taoBaoGoodsBrief.setCouponShortLink(tb.getCouponShortLink());
      taoBaoGoodsBrief.setCouponStartFee(tb.getCouponStartFee());
      taoBaoGoodsBrief.setCouponTotalCount(tb.getCouponTotalCount());
      taoBaoGoodsBrief.setBiz30day(tb.getBiz30day());
      taoBaoGoodsBrief.setTkRate(tb.getTkRate());
      taoBaoGoodsBrief.setZkPrice(tb.getZkPrice());
      taoBaoGoodsBrief.setEventRate(tb.getEventRate());
      taoBaoGoodsBrief.setTkMktStatus(tb.getTkMktStatus());
      taoBaoGoodsBrief.setReservePrice(tb.getReservePrice());
      taoBaoGoodsBrief.setCreatetime(new Date());
      taoBaoGoodsBriefDao.update(taoBaoGoodsBrief);
   }
   public int getInvalidCount() {
      Long count = taoBaoGoodsBriefDao.getCount("select count(tb.id) from TaoBaoGoodsBrief tb where tb.tkRate = 0");
      return count.intValue();
   }
   public List<TaoBaoGoodsBrief> getInvalidTB(int pageIndex) {
      int start = (pageIndex - 1) * Constant.PAGE_SIZE;
      return taoBaoGoodsBriefDao.list("from TaoBaoGoodsBrief tb where tb.tkRate = 0 ", start, Constant.PAGE_SIZE,
            new Serializable[] {});
   }
   /**
    * 从yeshi_ec_recommend_section_goods表中查询数据
    */
   @SuppressWarnings("unchecked")
   @Override
   public List<RecommendSectionGoods> listRecommendSectionGoods() {
      return recommendSectionGoodsDao.list("from RecommendSectionGoods");
   }
   @Override
   public String getGoodsUserHongBao(TaoBaoGoodsBrief goods) {
      BigDecimal rate = hongBaoManageService.getFanLiRate();
      return TaoBaoUtil.getGoodsHongBaoInfo(goods, rate);
      return TaoBaoUtil.getGoodsHongBaoInfo(goods, rate,false);
   }
   @Override
@@ -309,48 +53,36 @@
   }
   @Override
   public void deleteByPrimaryKey(Long id) {
      taoBaoGoodsBriefMapper.deleteByPrimaryKey(id);
   }
   @Override
   public void deleteBatchByGoodsId(List<Long> list) {
      taoBaoGoodsBriefMapper.deleteBatchByGoodsId(list);
   }
   @Override
   public List<TaoBaoGoodsBrief> queryByAuctionId(Long auctionId) {
      return taoBaoGoodsBriefMapper.queryByAuctionId(auctionId);
   }
   @Override
   public List<TaoBaoGoodsBrief> listQueryByAuctionId(List<Long> list) {
      return taoBaoGoodsBriefMapper.queryGoodsByAuctionId(list);
   }
   @Override
   public int insertSelective(TaoBaoGoodsBrief taoBaoGoodsBrief) {
      return taoBaoGoodsBriefMapper.insertSelective(taoBaoGoodsBrief);
   public List<Long> listExistById(List<Long> list) {
      if (list != null && list.size() > 0)
         return taoBaoGoodsBriefMapper.listExistById(list);
      else
         return null;
   }
   @Override
   public int insertBatch(List<TaoBaoGoodsBrief> list) {
      return taoBaoGoodsBriefMapper.insertBatch(list);
   }
   @Override
   public TaoBaoGoodsBrief selectByPrimaryKey(Long id) {
      return taoBaoGoodsBriefMapper.selectByPrimaryKey(id);
   }
   @Override
   public void updateTBGoodsWithNewInfo(TaoBaoGoodsBrief tb) throws NotExistObjectException {
      if (tb == null)
         return;
      List<TaoBaoGoodsBrief> findList = taoBaoGoodsBriefDao
            .list("from TaoBaoGoodsBrief tb where tb.auctionId=" + tb.getAuctionId());
      if (findList != null && findList.size() > 0) {
         for (TaoBaoGoodsBrief goods : findList) {
            tb.setId(goods.getId());
            if (!StringUtil.isNullOrEmpty(goods.getPictUrl()))
               tb.setPictUrl(goods.getPictUrl());
            taoBaoGoodsBriefDao.update(tb);
         }
      }
   }
   @Override
@@ -464,8 +196,46 @@
   }
   @Override
   public void updateBatchSelective(List<TaoBaoGoodsBrief> list) {
      taoBaoGoodsBriefMapper.updateBatchSelective(list);
   public List<TaoBaoGoodsBrief> listByShopId(long start, int count, Long shopId) {
      return taoBaoGoodsBriefMapper.listByShopId(start, count, shopId);
   }
   @Override
   public long countByShopId(Long shopId) {
      return taoBaoGoodsBriefMapper.countByShopId(shopId);
   }
   @Override
   public List<TaoBaoGoodsBrief> filterOffLineGoods(List<TaoBaoGoodsBrief> goodsList) {
      int page = goodsList.size() % 40 == 0 ? goodsList.size() / 40 -1 : goodsList.size() / 40;
      List<TaoBaoGoodsBrief> flist = new ArrayList<>();
      for (int i = 0; i <= page; i++) {
         List<TaoBaoGoodsBrief> list = goodsList.subList(i * 40,
               i * 40 + 40 > goodsList.size() ?goodsList.size(): i * 40 + 40 );
         String ids = "";
         for (TaoBaoGoodsBrief goods : list) {
            ids += goods.getAuctionId() + ",";
         }
         ids = ids.endsWith(",") ? ids.substring(0, ids.length() - 1) : ids;
         Map<Long, TaoBaoGoodsBrief> goodsMap = new HashMap<>();
         try {
            List<TaoBaoGoodsBrief> finalList = TaoKeApiUtil.getBatchGoodsInfos(ids);
            if (finalList != null)
               for (TaoBaoGoodsBrief ggods : finalList)
                  goodsMap.put(ggods.getAuctionId(), ggods);
            for (int j = 0; j < list.size(); j++) {
               if (goodsMap.get(list.get(j).getAuctionId()) == null) {
                  list.remove(j);
                  j--;
               }
            }
         } catch (TaobaoGoodsDownException e) {
            e.printStackTrace();
         }
         flist.addAll(list);
      }
      return flist;
   }
}