admin
2019-09-20 db88b87df461820152d68d3d55a28f30c2da83e6
fanli/src/main/java/com/yeshi/fanli/service/impl/brand/BrandGoodsCaheServiceImpl.java
@@ -1,6 +1,5 @@
package com.yeshi.fanli.service.impl.brand;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@@ -12,6 +11,7 @@
import com.yeshi.fanli.dto.jd.JDFilter;
import com.yeshi.fanli.dto.jd.JDSearchFilter;
import com.yeshi.fanli.dto.jd.JDSearchResult;
import com.yeshi.fanli.dto.jd.JDShopInfo;
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.dto.pdd.PDDGoodsResult;
import com.yeshi.fanli.dto.pdd.PDDSearchFilter;
@@ -22,10 +22,10 @@
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.service.inter.brand.BrandGoodsCaheService;
import com.yeshi.fanli.service.inter.brand.BrandShopCaheService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.jd.JDApiUtil;
import com.yeshi.fanli.util.jd.JDUtil;
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil;
@@ -43,6 +43,9 @@
   
   @Resource
   private BrandGoodsCaheDao brandGoodsCaheDao;
   @Resource
   private BrandShopCaheService brandShopCaheService;
   
   
   @Override
@@ -65,30 +68,48 @@
    * @return
    */
   private int addBrandGoodsTB(BrandInfo brandInfo) {
      Date date = new Date();
      SearchFilter filter = new SearchFilter();
      filter.setKey(brandInfo.getName());
      filter.setPage(1);
      filter.setPageSize(50);
      filter.setPageSize(100);
      filter.setTmall(true);
      filter.setSort(TaoBaoUtil.SORT_SALE_HIGH_TO_LOW);
      TaoBaoSearchResult searchResult = TaoKeApiUtil.searchWuLiao(filter);
      if (searchResult == null || searchResult.getTaoBaoGoodsBriefs() ==  null
            || searchResult.getTaoBaoGoodsBriefs().size() == 0)
         return 0;
      
      BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
      BigDecimal shareRate = hongBaoManageService.getShareRate();
      List<TaoBaoGoodsBrief> listGoods = searchResult.getTaoBaoGoodsBriefs();
      for (TaoBaoGoodsBrief goods: listGoods) {
         BrandGoodsCahe brandGoods = new BrandGoodsCahe();
         brandGoods.setBrandId(brandInfo.getId());
         brandGoods.setWeight((int) (Math.random() * 1000));
         brandGoods.setGoods(GoodsDetailVOFactory.convertTaoBao(goods, null, fanLiRate, shareRate));
         brandGoods.setCreateTime(new Date());
         brandGoods.setUpdateTime(new Date());
         brandGoodsCaheDao.save(brandGoods);
      int count = 0;
      if (searchResult != null && searchResult.getTaoBaoGoodsBriefs() != null
            && searchResult.getTaoBaoGoodsBriefs().size() > 0) {
         boolean addShop = true;
         List<TaoBaoGoodsBrief> listGoods = searchResult.getTaoBaoGoodsBriefs();
         for (TaoBaoGoodsBrief goods: listGoods) {
            // 添加店铺
            String shopTitle = goods.getShopTitle();
            if (addShop && !StringUtil.isNullOrEmpty(shopTitle) && shopTitle.contains("旗舰店")
                  && shopTitle.contains(brandInfo.getName())) {
               addShop = brandShopCaheService.addBrandShopTB(brandInfo.getId(), goods.getAuctionId(),
                     goods.getSellerId());
            }
            // 添加商品
            if (count < 50) {
               BrandGoodsCahe brandGoods = new BrandGoodsCahe();
               brandGoods.setBrandId(brandInfo.getId());
               brandGoods.setWeight((int) (Math.random() * 1000));
               brandGoods.setGoodsType(1);
               brandGoods.setGoodsTB(goods);
               brandGoods.setCreateTime(date);
               brandGoodsCaheDao.insert(brandGoods);
               count ++;
            } else if (!addShop) {
               break;
            }
         }
      }
      return listGoods.size();
      // 删除之前的
       brandGoodsCaheDao.removeByDate(brandInfo.getId(), 1 , date);
      return count;
   }
   
   
@@ -99,49 +120,61 @@
    * @return
    */
   private int addBrandGoodsJD(BrandInfo brandInfo) {
      int count = 0;
      JDSearchResult result = null;
      String way = configService.get("jd_api_search_key");
      if ("1".equals(way)) {
         JDFilter filterAPI = new JDFilter();
         filterAPI.setKeyword(SearchFilterUtil.filterSearchContent(brandInfo.getName()));
         filterAPI.setPageIndex(1);
         filterAPI.setPageSize(100);
         filterAPI.setSort(JDFilter.SORT_DESC);
         filterAPI.setSortName(JDFilter.SORTNAME_ORDER_COUNT_30DAYS);
         result = JDApiUtil.queryByKey(filterAPI);
      } else {
         // 网页爬取
         JDSearchFilter jdfilter = new JDSearchFilter();
         jdfilter.setKey(SearchFilterUtil.filterSearchContent(brandInfo.getName()));
         jdfilter.setPageNo(1);
         jdfilter.setPageSize(100);
         jdfilter.setSort(JDSearchFilter.SORT_DESC);
         jdfilter.setSortName(JDSearchFilter.SORTNAME_ORDER_COUNT_30DAYS);
         result = JDUtil.searchByKey(jdfilter);
      }
      int count = 0;
      if (result != null) {
         PageEntity pageEntity = result.getPageEntity();
         if (pageEntity != null) {
            count = (int) pageEntity.getTotalCount();
      Date date = new Date();
      for (int i=0; i < 2;i ++) {
         if ("1".equals(way)) {
            JDFilter filterAPI = new JDFilter();
            filterAPI.setKeyword(SearchFilterUtil.filterSearchContent(brandInfo.getName()));
            filterAPI.setPageIndex(1);
            filterAPI.setPageSize(30);
            filterAPI.setSort(JDFilter.SORT_DESC);
            filterAPI.setSortName(JDFilter.SORTNAME_ORDER_COUNT_30DAYS);
            result = JDApiUtil.queryByKey(filterAPI);
         } else {
            // 网页爬取
            JDSearchFilter jdfilter = new JDSearchFilter();
            jdfilter.setKey(SearchFilterUtil.filterSearchContent(brandInfo.getName()));
            jdfilter.setPageNo(1);
            jdfilter.setPageSize(30);
            jdfilter.setSort(JDSearchFilter.SORT_DESC);
            jdfilter.setSortName(JDSearchFilter.SORTNAME_ORDER_COUNT_30DAYS);
            result = JDUtil.searchByKey(jdfilter);
         }
         List<JDGoods> goodsList = result.getGoodsList();
         if (goodsList != null && goodsList.size() > 0) {
            BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
            BigDecimal shareRate = hongBaoManageService.getShareRate();
            for (JDGoods goods : goodsList) {
               BrandGoodsCahe brandGoods = new BrandGoodsCahe();
               brandGoods.setBrandId(brandInfo.getId());
               brandGoods.setWeight((int) (Math.random() * 1000));
               brandGoods.setGoods(GoodsDetailVOFactory.convertJDGoods(goods, fanLiRate, shareRate));
               brandGoods.setCreateTime(new Date());
               brandGoods.setUpdateTime(new Date());
               brandGoodsCaheDao.save(brandGoods);
         if (result != null) {
            List<JDGoods> goodsList = result.getGoodsList();
            if (goodsList != null && goodsList.size() > 0) {
               for (JDGoods goods : goodsList) {
                  BrandGoodsCahe brandGoods = new BrandGoodsCahe();
                  brandGoods.setBrandId(brandInfo.getId());
                  brandGoods.setWeight((int) (Math.random() * 1000));
                  brandGoods.setGoodsJD(goods);
                  brandGoods.setGoodsType(2);
                  brandGoods.setCreateTime(date);
                  brandGoodsCaheDao.insert(brandGoods);
                  if (count == 0) {
                     JDShopInfo shopInfo = goods.getShopInfo();
                     if(shopInfo != null) {
                        brandShopCaheService.addBrandShopJD(brandInfo, shopInfo);
                     }
                  }
                  count ++;
                  if (count >= 50) {
                     break;
                  }
               }
            }
         }
      }
      // 删除之前
       brandGoodsCaheDao.removeByDate(brandInfo.getId(), 2 , date);
        return count;
   }
   
@@ -155,28 +188,47 @@
      PDDSearchFilter pddfilter = new PDDSearchFilter();
      pddfilter.setKw(brandInfo.getName());
      pddfilter.setPage(1);
      pddfilter.setPageSize(50);
      pddfilter.setPageSize(100);
      pddfilter.setSortType(6);
      Date date = new Date();
      int count = 0;
      PDDGoodsResult result = PinDuoDuoApiUtil.searchGoods(pddfilter);
      if (result != null) {
         count = result.getTotalCount();
         List<PDDGoodsDetail> goodsList = result.getGoodsList();
         if (goodsList != null && goodsList.size() > 0) {
            BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
            BigDecimal shareRate = hongBaoManageService.getShareRate();
            for (PDDGoodsDetail goods : goodsList) {
               BrandGoodsCahe brandGoods = new BrandGoodsCahe();
               brandGoods.setBrandId(brandInfo.getId());
               brandGoods.setWeight((int) (Math.random() * 1000));
               brandGoods.setGoods(GoodsDetailVOFactory.convertPDDGoods(goods, fanLiRate, shareRate));
               brandGoods.setCreateTime(new Date());
               brandGoods.setUpdateTime(new Date());
               brandGoodsCaheDao.save(brandGoods);
               brandGoods.setGoodsType(3);
               brandGoods.setGoodsPDD(goods);
               brandGoods.setCreateTime(date);
               brandGoodsCaheDao.insert(brandGoods);
               count ++;
               if (count >= 50) {
                  break;
               }
            }
         }
      }
      // 添加店铺
      brandShopCaheService.addBrandShopPDD(brandInfo);
      // 删除之前
      brandGoodsCaheDao.removeByDate(brandInfo.getId(), 3, date);
      return count;
   }
   @Override
   public List<BrandGoodsCahe> getByBrandId(int start, int count, Long brandId){
      return brandGoodsCaheDao.getByBrandId(start, count, brandId);
   }
   @Override
   public long countByBrandId(Long brandId){
      return brandGoodsCaheDao.countByBrandId(brandId);
   }
}