yujian
2020-01-18 f4a0f2acc63d7785eab108419a4e16f5f688cb95
fanli/src/main/java/com/yeshi/fanli/service/impl/brand/BrandGoodsCaheServiceImpl.java
@@ -18,9 +18,11 @@
import com.yeshi.fanli.entity.brand.BrandGoodsCahe;
import com.yeshi.fanli.entity.brand.BrandInfo;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.entity.taobao.SearchFilter;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.brand.BrandGoodsCaheService;
import com.yeshi.fanli.service.inter.brand.BrandShopCaheService;
import com.yeshi.fanli.service.inter.config.ConfigService;
@@ -40,14 +42,13 @@
   @Resource
   private ConfigService configService;
   @Resource
   private BrandGoodsCaheDao brandGoodsCaheDao;
   @Resource
   private BrandShopCaheService brandShopCaheService;
   @Override
   public BrandInfo addBrandGoods(BrandInfo brandInfo) {
      // 淘宝
@@ -56,264 +57,273 @@
      BrandInfo goodsJD = addBrandGoodsJD(brandInfo);
      // 拼多多
      BrandInfo goodsPDD = addBrandGoodsPDD(brandInfo);
      int goodsTotal = goodsTB.getGoodsTotal() + goodsJD.getGoodsTotal() +  goodsPDD.getGoodsTotal();
      int shopTotal = goodsTB.getShopTotal() + goodsJD.getShopTotal() +  goodsPDD.getShopTotal();
      int goodsTotal = goodsTB.getGoodsTotal() + goodsJD.getGoodsTotal() + goodsPDD.getGoodsTotal();
      int shopTotal = goodsTB.getShopTotal() + goodsJD.getShopTotal() + goodsPDD.getShopTotal();
      goodsTB.setGoodsTotal(goodsTotal);
      goodsTB.setShopTotal(shopTotal);
      return goodsTB;
   }
   /**
    * 淘宝商品
    *
    * @param brandInfo
    * @return
    */
   private BrandInfo addBrandGoodsTB(BrandInfo brandInfo) {
      BrandInfo numInfo = new BrandInfo();
      Date date = new Date();
      String shopKey = brandInfo.getShopKey();
      if (StringUtil.isNullOrEmpty(shopKey)) {
         // 删除之前的
          brandGoodsCaheDao.removeByDate(brandInfo.getId(), 1 , date);
         // 删除店铺
          brandShopCaheService.removeByDateAndType(brandInfo.getId(), 11, date);
          numInfo.setShopTotal(0);
          numInfo.setGoodsTotal(0);
         return numInfo;
      }
      String searchKey = brandInfo.getSearchKey();
      if (StringUtil.isNullOrEmpty(searchKey))
         searchKey = brandInfo.getName();
      SearchFilter filter = new SearchFilter();
      filter.setKey(searchKey);
      filter.setPage(1);
      filter.setPageSize(100);
      filter.setTmall(true);
      filter.setSort(TaoBaoUtil.SORT_SALE_HIGH_TO_LOW);
      TaoBaoSearchResult searchResult = TaoKeApiUtil.searchWuLiao(filter);
      // 删除店铺
       brandShopCaheService.removeByDateAndType(brandInfo.getId(), 11, date);
       // 组织商品 + 添加店铺
      int count = 0;
      int countShop = 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.toLowerCase().contains(shopKey.toLowerCase())) {
               addShop = brandShopCaheService.addBrandShopTB(brandInfo, goods.getAuctionId(),
                     goods.getSellerId());
               if (!addShop)
                  countShop = 1;
            }
            // 添加商品
            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(new Date());
               brandGoodsCaheDao.insert(brandGoods);
               count ++;
            } else if (!addShop) {
               break;
            }
         }
      }
      // 删除之前的
       brandGoodsCaheDao.removeByDate(brandInfo.getId(), 1 , date);
       numInfo.setShopTotal(countShop);
       numInfo.setGoodsTotal(count);
      return numInfo;
   }
   /**
    *  京东商品
    *
    * @param brandInfo
    * @return
    */
   private BrandInfo addBrandGoodsJD(BrandInfo brandInfo) {
      BrandInfo numInfo = new BrandInfo();
      Date date = new Date();
      String shopKey = brandInfo.getShopKeyJD();
      if (StringUtil.isNullOrEmpty(shopKey)) {
         // 删除店铺
          brandShopCaheService.removeByDateAndType(brandInfo.getId(), 20, date);
         // 删除之前
          brandGoodsCaheDao.removeByDate(brandInfo.getId(), 2 , date);
          numInfo.setShopTotal(0);
          numInfo.setGoodsTotal(0);
          return numInfo;
      }
      String searchKey = brandInfo.getSearchKeyJD();
      if (StringUtil.isNullOrEmpty(searchKey))
         searchKey = brandInfo.getName();
      int count = 0;
      int countShop = 0;
      JDSearchResult result = null;
      String way = configService.get("jd_api_search_key");
      // 删除店铺
       brandShopCaheService.removeByDateAndType(brandInfo.getId(), 20, date);
       boolean addShop = true;
      for (int i=0; i < 2;i ++) {
         if ("1".equals(way)) {
            JDFilter filterAPI = new JDFilter();
            filterAPI.setKeyword(SearchFilterUtil.filterSearchContent(searchKey));
            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(searchKey));
            jdfilter.setPageNo(1);
            jdfilter.setPageSize(30);
            jdfilter.setSort(JDSearchFilter.SORT_DESC);
            jdfilter.setSortName(JDSearchFilter.SORTNAME_ORDER_COUNT_30DAYS);
            result = JDUtil.searchByKey(jdfilter);
      numInfo.setShopTotal(0);
      numInfo.setGoodsTotal(0);
      try {
         String shopKey = brandInfo.getShopKey();
         if (StringUtil.isNullOrEmpty(shopKey)) {
            // 删除之前的
            brandGoodsCaheDao.removeByDate(brandInfo.getId(), 1, date);
            // 删除店铺
            brandShopCaheService.removeByDateAndType(brandInfo.getId(), 11, date);
            return numInfo;
         }
         if (result != null && result.getGoodsList() != null && result.getGoodsList().size() > 0) {
            List<JDGoods> goodsList = result.getGoodsList();
            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(new Date());
               brandGoodsCaheDao.insert(brandGoods);
               JDShopInfo shopInfo = goods.getShopInfo();
               if (addShop && shopInfo != null) {
                  // 包含该品牌名称
                  String shopName = shopInfo.getShopName();
                  if (!StringUtil.isNullOrEmpty(shopName) && shopName.toLowerCase().contains(shopKey.toLowerCase())) {
                     addShop = false;
                     brandShopCaheService.addBrandShopJD(brandInfo, shopInfo);
         String searchKey = brandInfo.getSearchKey();
         if (StringUtil.isNullOrEmpty(searchKey))
            searchKey = brandInfo.getName();
         SearchFilter filter = new SearchFilter();
         filter.setKey(searchKey);
         filter.setPage(1);
         filter.setPageSize(100);
         filter.setTmall(true);
         filter.setSort(TaoBaoUtil.SORT_SALE_HIGH_TO_LOW);
         TaoBaoSearchResult searchResult = TaoKeApiUtil.searchWuLiao(filter);
         // 删除店铺
         brandShopCaheService.removeByDateAndType(brandInfo.getId(), 11, date);
         // 组织商品 + 添加店铺
         int count = 0;
         int countShop = 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.toLowerCase().contains(shopKey.toLowerCase())) {
                  addShop = brandShopCaheService.addBrandShopTB(brandInfo, goods.getAuctionId(),
                        goods.getSellerId());
                  if (!addShop)
                     countShop = 1;
                  }
               }
               count ++;
               if (count >= 50) {
               // 添加商品
               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(new Date());
                  brandGoodsCaheDao.insert(brandGoods);
                  count++;
               } else if (!addShop) {
                  break;
               }
            }
         }
         // 删除之前的
         brandGoodsCaheDao.removeByDate(brandInfo.getId(), 1, date);
         numInfo.setShopTotal(countShop);
         numInfo.setGoodsTotal(count);
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
      // 删除之前
       brandGoodsCaheDao.removeByDate(brandInfo.getId(), 2 , date);
       numInfo.setShopTotal(countShop);
       numInfo.setGoodsTotal(count);
      return numInfo;
      return numInfo;
   }
   /**
    * 京东商品
    *
    * @param brandInfo
    * @return
    */
   private BrandInfo addBrandGoodsJD(BrandInfo brandInfo) {
      Date date = new Date();
      BrandInfo numInfo = new BrandInfo();
      numInfo.setShopTotal(0);
      numInfo.setGoodsTotal(0);
      try {
         String shopKey = brandInfo.getShopKeyJD();
         if (StringUtil.isNullOrEmpty(shopKey)) {
            // 删除店铺
            brandShopCaheService.removeByDateAndType(brandInfo.getId(), 20, date);
            // 删除之前
            brandGoodsCaheDao.removeByDate(brandInfo.getId(), 2, date);
            return numInfo;
         }
         String searchKey = brandInfo.getSearchKeyJD();
         if (StringUtil.isNullOrEmpty(searchKey))
            searchKey = brandInfo.getName();
         int count = 0;
         int countShop = 0;
         JDSearchResult result = null;
         String way = configService.get(ConfigKeyEnum.jdApiSearchKey.getKey());
         // 删除店铺
         brandShopCaheService.removeByDateAndType(brandInfo.getId(), 20, date);
         boolean addShop = true;
         for (int i = 0; i < 2; i++) {
            if ("1".equals(way)) {
               JDFilter filterAPI = new JDFilter();
               filterAPI.setKeyword(SearchFilterUtil.filterSearchContent(searchKey));
               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(searchKey));
               jdfilter.setPageNo(1);
               jdfilter.setPageSize(30);
               jdfilter.setSort(JDSearchFilter.SORT_DESC);
               jdfilter.setSortName(JDSearchFilter.SORTNAME_ORDER_COUNT_30DAYS);
               result = JDUtil.searchByKey(jdfilter);
            }
            if (result != null && result.getGoodsList() != null && result.getGoodsList().size() > 0) {
               List<JDGoods> goodsList = result.getGoodsList();
               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(new Date());
                  brandGoodsCaheDao.insert(brandGoods);
                  JDShopInfo shopInfo = goods.getShopInfo();
                  if (addShop && shopInfo != null) {
                     // 包含该品牌名称
                     String shopName = shopInfo.getShopName();
                     if (!StringUtil.isNullOrEmpty(shopName)
                           && shopName.toLowerCase().contains(shopKey.toLowerCase())) {
                        addShop = false;
                        brandShopCaheService.addBrandShopJD(brandInfo, shopInfo);
                        countShop = 1;
                     }
                  }
                  count++;
                  if (count >= 50) {
                     break;
                  }
               }
            }
         }
         // 删除之前
         brandGoodsCaheDao.removeByDate(brandInfo.getId(), 2, date);
         numInfo.setShopTotal(countShop);
         numInfo.setGoodsTotal(count);
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
      return numInfo;
   }
   /**
    * 拼多多商品
    *
    * @param brandInfo
    * @return
    */
   private BrandInfo addBrandGoodsPDD(BrandInfo brandInfo) {
      Date date = new Date();
      BrandInfo numInfo = new BrandInfo();
      String shopKey = brandInfo.getShopKeyPDD();
      if (StringUtil.isNullOrEmpty(shopKey)) {
      numInfo.setShopTotal(0);
      numInfo.setGoodsTotal(0);
      try {
         String shopKey = brandInfo.getShopKeyPDD();
         if (StringUtil.isNullOrEmpty(shopKey)) {
            // 删除之前
            brandGoodsCaheDao.removeByDate(brandInfo.getId(), 3, date);
            // 删除之前店铺
            brandShopCaheService.removeByDateAndType(brandInfo.getId(), 30, date);
            return numInfo;
         }
         String searchKey = brandInfo.getSearchKeyPDD();
         if (StringUtil.isNullOrEmpty(searchKey))
            searchKey = brandInfo.getName();
         PDDSearchFilter pddfilter = new PDDSearchFilter();
         pddfilter.setKw(searchKey);
         pddfilter.setPage(1);
         pddfilter.setPageSize(100);
         pddfilter.setSortType(6);
         PDDGoodsResult result = PinDuoDuoApiUtil.searchGoods(pddfilter);
         int count = 0;
         if (result != null) {
            List<PDDGoodsDetail> goodsList = result.getGoodsList();
            if (goodsList != null && goodsList.size() > 0) {
               for (PDDGoodsDetail goods : goodsList) {
                  BrandGoodsCahe brandGoods = new BrandGoodsCahe();
                  brandGoods.setBrandId(brandInfo.getId());
                  brandGoods.setWeight((int) (Math.random() * 1000));
                  brandGoods.setGoodsType(3);
                  brandGoods.setGoodsPDD(goods);
                  brandGoods.setCreateTime(new Date());
                  brandGoodsCaheDao.insert(brandGoods);
                  count++;
                  if (count >= 50) {
                     break;
                  }
               }
            }
         }
         // 删除之前
         brandGoodsCaheDao.removeByDate(brandInfo.getId(), 3, date);
         // 删除之前店铺
         brandShopCaheService.removeByDateAndType(brandInfo.getId(), 30, date);
         numInfo.setShopTotal(0);
          numInfo.setGoodsTotal(0);
          return numInfo;
         // 添加店铺
         int countShop = brandShopCaheService.addBrandShopPDD(brandInfo);
         numInfo.setShopTotal(countShop);
         numInfo.setGoodsTotal(count);
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
      String searchKey = brandInfo.getSearchKeyPDD();
      if (StringUtil.isNullOrEmpty(searchKey))
         searchKey = brandInfo.getName();
      PDDSearchFilter pddfilter = new PDDSearchFilter();
      pddfilter.setKw(searchKey);
      pddfilter.setPage(1);
      pddfilter.setPageSize(100);
      pddfilter.setSortType(6);
      PDDGoodsResult result = PinDuoDuoApiUtil.searchGoods(pddfilter);
      int count = 0;
      if (result != null) {
         List<PDDGoodsDetail> goodsList = result.getGoodsList();
         if (goodsList != null && goodsList.size() > 0) {
            for (PDDGoodsDetail goods : goodsList) {
               BrandGoodsCahe brandGoods = new BrandGoodsCahe();
               brandGoods.setBrandId(brandInfo.getId());
               brandGoods.setWeight((int) (Math.random() * 1000));
               brandGoods.setGoodsType(3);
               brandGoods.setGoodsPDD(goods);
               brandGoods.setCreateTime(new Date());
               brandGoodsCaheDao.insert(brandGoods);
               count ++;
               if (count >= 50) {
                  break;
               }
            }
         }
      }
      // 删除之前
      brandGoodsCaheDao.removeByDate(brandInfo.getId(), 3, date);
      // 删除之前店铺
      brandShopCaheService.removeByDateAndType(brandInfo.getId(), 30, date);
      // 添加店铺
      int countShop = brandShopCaheService.addBrandShopPDD(brandInfo);
      numInfo.setShopTotal(countShop);
       numInfo.setGoodsTotal(count);
      return numInfo;
   }
   @Override
   public List<BrandGoodsCahe> getByBrandId(int start, int count, Long brandId){
   public List<BrandGoodsCahe> getByBrandId(int start, int count, Long brandId) {
      return brandGoodsCaheDao.getByBrandId(start, count, brandId);
   }
   @Override
   public long countByBrandId(Long brandId){
   public long countByBrandId(Long brandId) {
      return brandGoodsCaheDao.countByBrandId(brandId);
   }
   @Override
   public void removeAgoByDate(Date createTime) {
      brandGoodsCaheDao.removeAgoByDate(createTime);
   }
   @Override
   public void removeByBrandId(Long brandId) {
      brandGoodsCaheDao.removeByBrandId(brandId);