admin
2020-11-09 2147d023563a7c9d05d97547c00d6b0162c0644c
fanli/src/main/java/com/yeshi/fanli/service/impl/jd/JDGoodsServiceImpl.java
@@ -1,12 +1,14 @@
package com.yeshi.fanli.service.impl.jd;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import com.yeshi.fanli.entity.SystemEnum;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -16,11 +18,12 @@
import com.yeshi.fanli.dto.jd.JDSearchResult;
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.exception.jd.JDGoodsException;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.jd.JDGoodsService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.common.entity.PageEntity;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.jd.JDApiUtil;
@@ -28,14 +31,13 @@
@Service
public class JDGoodsServiceImpl implements JDGoodsService {
   @Resource
   private ConfigService configService;
   @Resource
   private HongBaoManageService hongBaoManageService;
   private Map<String, Long> countMap = new HashMap<String, Long>();
   private static final Map<Long, String> cidMap = new HashMap<Long, String>();
@@ -43,10 +45,10 @@
      cidMap.put(1L, "2,16,1,3");// 精选
      cidMap.put(2L, "737");// 电器: 家用电器
      cidMap.put(3L, "652,670");// 数码: 数码/电脑、办公
      cidMap.put(4L, "9987");// 手机:手机通讯
      cidMap.put(4L, "9987");// 手机:手机通讯
      cidMap.put(5L, "12218");// 生鲜
      cidMap.put(6L, "1315");// 服饰: 服饰内衣
      cidMap.put(7L, "1319,6233");// 母婴: 母婴/玩具乐器
      cidMap.put(6L, "1315");// 服饰: 服饰内衣
      cidMap.put(7L, "1319,6233");// 母婴: 母婴/玩具乐器
      cidMap.put(8L, "1713");// 图书
      cidMap.put(9L, "6196");// 厨具
      cidMap.put(10L, "1320");// 食品: 食品饮料
@@ -60,7 +62,6 @@
      cidMap.put(18L, "6728");// 汽车:汽车用品
   }
   @Override
   public List<GoodsClass> getSpecialClass() {
      List<GoodsClass> list = new ArrayList<GoodsClass>();
@@ -82,23 +83,22 @@
      list.add(new GoodsClass(16L, "宠物"));
      list.add(new GoodsClass(17L, "家居"));
      list.add(new GoodsClass(18L, "汽车"));
      return list;
      return list;
   }
   @Cacheable(value = "jdSpecialCache", key = "'specialSearch-'+#page+'-'+#cid")
   @Override
   @Transactional
   public List<JDGoods> specialSearch(Integer page, Long cid) throws JDGoodsException{
   @Transactional(rollbackFor=Exception.class)
   public List<JDGoods> specialSearch(Integer page, Long cid,SystemEnum system) throws JDGoodsException {
      if (cid == null) {
         throw new JDGoodsException(1, "分类id为空");
      }
      String jdcid = cidMap.get(cid);
      if (StringUtil.isNullOrEmpty(jdcid)) {
         throw new JDGoodsException(1, "分类id不存在");
      }
      // 精选
      if (cid == 1) {
         Integer categoryId = null;
@@ -112,23 +112,23 @@
            if (count != null) {
               addCount += count;
            }
            if (count != null && totalCount > addCount) {
               pageTemp = (int)(addCount / Constant.PAGE_SIZE);
               pageTemp = (int) (addCount / Constant.PAGE_SIZE);
               continue; // 当前分类商品不足
            } else {
               page = page - pageTemp;
               page = page - pageTemp;
               categoryId = Integer.parseInt(id);
               break;
            }
         }
         if (categoryId == null) {
            return null;
         }
         JDSearchResult result = JDApiUtil.getJingFenGoods(page, categoryId);
         long count = 0;
         if (result != null) {
            PageEntity pageEntity = result.getPageEntity();
@@ -136,25 +136,24 @@
               count = pageEntity.getTotalCount();
            }
         }
         long mcount = (count/Constant.PAGE_SIZE) * Constant.PAGE_SIZE;
         long mcount = (count / Constant.PAGE_SIZE) * Constant.PAGE_SIZE;
         if (mcount == 0) {
            mcount = 20;
         }
         countMap.put(cid + "-" + categoryId, mcount);
         if (result == null) {
            return null;
         } else {
            return result.getGoodsList();
            return filterJDGoods(result.getGoodsList());
         }
      }
      }
      if (!jdcid.contains(",")) {
         return search(page, Constant.PAGE_SIZE, jdcid);
      }
         return search(page, Constant.PAGE_SIZE, jdcid,system);
      }
      // 多个分类id处理
      List<JDGoods> list = new ArrayList<JDGoods>();
      String[] arrayId = jdcid.split(",");
@@ -162,18 +161,34 @@
         if (i >= 3) {
            break;
         }
         List<JDGoods> listGoods = search(page, 10, arrayId[i]);
         List<JDGoods> listGoods = search(page, 10, arrayId[i],system);
         if (listGoods != null && listGoods.size() > 0) {
            list.addAll(listGoods);
         }
      }
      return list;
      return filterJDGoods(list);
   }
   private List<JDGoods> search(int page, int pageSize, String jdcid) {
   /**
    * 过滤商品
    *
    * @param goodsList
    * @return
    */
   private List<JDGoods> filterJDGoods(List<JDGoods> goodsList) {
      List<Long> skuIds = new ArrayList<>();
      for (JDGoods goods : goodsList) {
         skuIds.add(goods.getSkuId());
      }
      List<JDGoods> newGoodsList = JDApiUtil.listGoodsDetail(skuIds);
      if (newGoodsList != null && newGoodsList.size() > 0)
         return newGoodsList;
      return goodsList;
   }
   private List<JDGoods> search(int page, int pageSize, String jdcid, SystemEnum system) {
      JDSearchResult result = null;
      String way = configService.get("jd_api_search_key");
      String way = configService.getValue(ConfigKeyEnum.jdApiSearchKey.getKey(),system);
      if ("1".equals(way)) { // API搜索
         JDFilter filterAPI = new JDFilter();
         filterAPI.setPageIndex(page);
@@ -187,7 +202,7 @@
         jdfilter.setCategoryId(Integer.parseInt(jdcid));
         result = JDUtil.searchByKey(jdfilter);
      }
      if (result == null) {
         return null;
      } else {
@@ -195,4 +210,29 @@
      }
   }
   @Cacheable(value = "jdCache", key = "'getIndexJDGoods-'+#page")
   @Override
   public JDSearchResult getIndexJDGoods(int page,SystemEnum system) {
      JDSearchResult result = null;
      String way = configService.getValue(ConfigKeyEnum.jdApiSearchKey.getKey(),system);
      // TODO 需要调整规则
      way = "0";
      if ("1".equals(way)) {
         JDFilter filterAPI = new JDFilter();
         filterAPI.setPageIndex(page);
         filterAPI.setPageSize(20);
         result = JDApiUtil.queryByKey(filterAPI);
      } else {
         // 网页爬取
         JDSearchFilter jdfilter = new JDSearchFilter();
         jdfilter.setPageNo(page);
         jdfilter.setPageSize(Constant.PAGE_SIZE);
         result =JDApiUtil.getJingFenGoods(page,125); //JDUtil.searchByKey(jdfilter);
      }
      if (result != null && result.getGoodsList() != null)
         Collections.shuffle(result.getGoodsList());
      return result;
   }
}