yujian
2020-01-07 1b3856e8f5431508d944a3b9c7a444dac3536bb6
fanli/src/main/java/com/yeshi/fanli/controller/client/v1/SearchController.java
@@ -3,14 +3,12 @@
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import javax.print.attribute.standard.NumberUp;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
@@ -21,13 +19,13 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.dto.dataoke.DaTaoKeGoodsResult;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.lable.QualityFactory;
import com.yeshi.fanli.entity.bus.recommend.Honest;
import com.yeshi.fanli.entity.bus.search.HistorySearch;
import com.yeshi.fanli.entity.bus.search.HotSearch;
import com.yeshi.fanli.entity.bus.su.search.SuperHotSearch;
import com.yeshi.fanli.entity.system.BusinessSystem;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.entity.taobao.PidUser;
import com.yeshi.fanli.entity.taobao.SearchFilter;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
@@ -36,23 +34,26 @@
import com.yeshi.fanli.entity.taobao.TaoBaoSearchNav;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.entity.taobao.TaoBaoUnionConfig;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.entity.taobao.dataoke.DaTaoKeDetailV2;
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.config.SuperHotSearchService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.config.HotSearchService;
import com.yeshi.fanli.service.inter.lable.QualityGoodsService;
import com.yeshi.fanli.service.inter.lable.TaoKeGoodsService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoShopService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionConfigService;
import com.yeshi.fanli.service.inter.taobao.dataoke.DaTaoKeGoodsDetailV2Service;
import com.yeshi.fanli.service.inter.user.HistorySearchService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.EhcacheUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.ThreadUtil;
import com.yeshi.fanli.util.cache.TaoBaoGoodsCacheUtil;
import com.yeshi.fanli.util.dataoke.DaTaoKeApiUtil;
import com.yeshi.fanli.util.taobao.SearchFilterUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import com.yeshi.fanli.vo.brand.TaoBaoShopVO;
import net.sf.ehcache.Element;
import net.sf.json.JSONArray;
@@ -69,10 +70,10 @@
   private ConfigService configService;
   @Resource
   private SuperHotSearchService superHotSearchSerivce;
   private HotSearchService hotSearchService;
   @Resource
   private HistorySearchService historySearchService;
   private HistorySearchService  historySearchService;
   @Resource
   private HongBaoManageService manageService;
@@ -89,33 +90,36 @@
   @Resource
   private TaoBaoGoodsCacheUtil taoBaoGoodsCacheUtil;
   @Resource
   private TaoBaoShopService taoBaoShopService;
   @Resource
   private DaTaoKeGoodsDetailV2Service daTaoKeGoodsDetailV2Service;
   /**
    * 热门搜索
    * @param acceptData
    * @param goodsType
    * @param out
    */
   @RequestMapping(value = "gethotsearch", method = RequestMethod.POST)
   public void getHotSearch(AcceptData acceptData, PrintWriter out) {
      BusinessSystem system = businessSystemService.getBusinessSystemCache(acceptData.getPlatform(),
            acceptData.getPackages());
      if (system == null) {
         out.print(JsonUtil.loadFalseResult("系统不存在"));
         return;
   public void getHotSearch(AcceptData acceptData, Integer goodsType, PrintWriter out) {
      // 默认淘宝
      if (goodsType == null) {
         goodsType = Constant.SOURCE_TYPE_TAOBAO;
      }
      List<SuperHotSearch> superHotSearchList = superHotSearchSerivce
            .getSuperHotSearchBySystemIdCache(system.getId());
      if (superHotSearchList == null || superHotSearchList.size() == 0) {
      List<HotSearch> list = hotSearchService.getHotSearchCache(goodsType);
      if (list == null || list.size() == 0) {
         out.print(JsonUtil.loadFalseResult("没有数据"));
         return;
      }
      List<HotSearch> hotSearchList = new ArrayList<HotSearch>();
      for (SuperHotSearch superHotSearch : superHotSearchList) {
         HotSearch hotSearch = superHotSearch.getHotSearch();
         if (hotSearch != null)
            hotSearchList.add(hotSearch);
      }
      JSONObject data = new JSONObject();
      data.put("count", hotSearchList.size());
      data.put("hotSearchList", hotSearchList);
      String result = JsonUtil.loadTrueResult(data);
      out.print(result);
      return;
      data.put("count", list.size());
      data.put("hotSearchList", list);
      out.print(JsonUtil.loadTrueResult(data));
   }
   @RequestMapping("specialSearch")
@@ -410,15 +414,20 @@
      historySearchService.addHistorySearch(kw, bid);
      BusinessSystem system = businessSystemService.getBusinessSystemCache(acceptData.getPlatform(),
            acceptData.getPackages());
      if (system == null) {
         out.print(JsonUtil.loadFalseResult("系统不存在"));
         return;
      }
      // BusinessSystem system =
      // businessSystemService.getBusinessSystemCache(acceptData.getPlatform(),
      // acceptData.getPackages());
      // if (system == null) {
      // out.print(JsonUtil.loadFalseResult("系统不存在"));
      // return;
      // }
      if (!StringUtil.isNullOrEmpty(kw) && (kw.startsWith("http://") || kw.startsWith("https://"))) {
         out.print(JsonUtil.loadFalseResult("无法搜索链接"));
         JSONObject data = new JSONObject();
         data.put("result", new JSONArray());
         data.put("count", 0);
         data.put("nav", new JSONArray());
         out.print(JsonUtil.loadTrueResult(data));
         return;
      }
@@ -449,11 +458,14 @@
      JSONObject data = null;
      if (!recommend) {
         // 淘宝接口请求
         data = searchGoods(kw, page, filter, order, startprice, endprice);
      } else {
         // 推荐:精选库
         data = searchQualityGoods(kw, page, filter, order, startprice, endprice);
         // data = searchQualityGoods(kw, page, filter, order, startprice,
         // endprice);
         data = searchDaTaoKeGoods(kw, page, filter, order, startprice, endprice);
      }
      // 获取推荐词
@@ -463,6 +475,12 @@
         listRecommendWords = TaoBaoUtil.getSuguestSearch(kw);
         if (listRecommendWords == null || listRecommendWords.size() == 0) {
            listRecommendWords = new ArrayList<String>();
         }
         List<TaoBaoShopVO> listShop =taoBaoShopService.getShopByKey(kw);
         if (listShop != null && listShop.size() > 0 && listShop.get(0).getListGoods() != null
               && listShop.get(0).getListGoods().size() > 2) {
            data.put("shop", JsonUtil.getApiCommonGson().toJson(listShop.get(0)));
         }
      }
@@ -602,37 +620,44 @@
         sf.setParams(params);
      }
      // 搜索大淘客
      List<DaTaoKeDetailV2> daTaoKeList = null;
      if (page == 1)
         daTaoKeList = daTaoKeGoodsDetailV2Service.listByDtitle(kw);
      TaoBaoSearchResult result = TaoBaoUtil.search(sf);
      if (result == null) {
         return null;
      }
      // 搜索是否在营销主商品库
      if (result != null && result.getTaoBaoGoodsBriefs() != null && result.getTaoBaoGoodsBriefs().size() > 0) {
         String numIds = "";
         for (TaoBaoGoodsBrief goods : result.getTaoBaoGoodsBriefs()) {
            numIds += goods.getAuctionId() + ",";
         }
         if (numIds.endsWith(","))
            numIds = numIds.substring(0, numIds.length() - 1);
         try {
            List<TaoBaoGoodsBrief> goodsList = TaoKeApiUtil.getBatchGoodsInfos(numIds);
            Map<Long, Integer> map = new HashMap<>();
            for (TaoBaoGoodsBrief g : goodsList)
               map.put(g.getAuctionId(), g.getMaterialLibType());
            for (TaoBaoGoodsBrief goods : result.getTaoBaoGoodsBriefs())
               goods.setMaterialLibType(map.get(goods.getAuctionId()));
         // 暂时放弃更新到营销商品库
         // String numIds = "";
         // for (TaoBaoGoodsBrief goods : result.getTaoBaoGoodsBriefs()) {
         // numIds += goods.getAuctionId() + ",";
         // }
         // if (numIds.endsWith(","))
         // numIds = numIds.substring(0, numIds.length() - 1);
         // try {
         // List<TaoBaoGoodsBrief> goodsList =
         // TaoKeApiUtil.getBatchGoodsInfos(numIds);
         // Map<Long, Integer> map = new HashMap<>();
         // for (TaoBaoGoodsBrief g : goodsList)
         // map.put(g.getAuctionId(), g.getMaterialLibType());
         // for (TaoBaoGoodsBrief goods : result.getTaoBaoGoodsBriefs())
         // goods.setMaterialLibType(map.get(goods.getAuctionId()));
            ThreadUtil.run(new Runnable() {
               @Override
               public void run() {
                  // 更新到緩存
                  for (TaoBaoGoodsBrief goods : result.getTaoBaoGoodsBriefs())
                     taoBaoGoodsCacheUtil.saveCommonTaoBaoGoodsInfo(goods);
               }
            });
         } catch (TaobaoGoodsDownException e) {
            e.printStackTrace();
         }
         ThreadUtil.run(new Runnable() {
            @Override
            public void run() {
               // 更新到緩存
               for (TaoBaoGoodsBrief goods : result.getTaoBaoGoodsBriefs())
                  taoBaoGoodsCacheUtil.saveCommonTaoBaoGoodsInfo(goods);
            }
         });
         // } catch (Exception e) {
         // e.printStackTrace();
         // }
      }
@@ -644,14 +669,26 @@
      data.put("nav", gson.toJson(navList));
      List<TaoBaoGoodsBriefExtra> re = new ArrayList<TaoBaoGoodsBriefExtra>();
      List<TaoBaoGoodsBrief> taoBaoGoodsBriefs = result.getTaoBaoGoodsBriefs();
      if (taoBaoGoodsBriefs == null)
         taoBaoGoodsBriefs = new ArrayList<>();
      try {
         if (daTaoKeList != null && daTaoKeList.size() > 0) {
            Collections.reverse(daTaoKeList);
            for (DaTaoKeDetailV2 detail : daTaoKeList) {
               taoBaoGoodsBriefs.add(0, TaoBaoUtil.convert(detail));
            }
         }
      } catch (Exception e) {
      }
      int fq = sf.getQuan();
      int fh = sf.getHongbao();
      boolean ft = sf.isTmall();
      Map<String, String> map = manageService.convertMap();
      BigDecimal proportion = manageService.getFanLiRate();
      if (fq == 0 && fh == 0 && !ft) {
         if (result != null && result.getTaoBaoGoodsBriefs() != null)
            for (TaoBaoGoodsBrief bf : result.getTaoBaoGoodsBriefs()) {
         if (result != null && taoBaoGoodsBriefs != null)
            for (TaoBaoGoodsBrief bf : taoBaoGoodsBriefs) {
               int count = bf.getBiz30day();
               if (count >= 10000) { // 对销量数据做处理 页面展示7.3万
                  double sales = count;
@@ -751,7 +788,7 @@
               provinceId = array.optJSONObject(i).optInt("id");
            } else if ("tkRate1".equalsIgnoreCase(ty)) {
               // 获取对应参数
               String value = configService.get("tkRate_range");
               String value = configService.get(ConfigKeyEnum.tkRateRange.getKey());
               if (!StringUtil.isNullOrEmpty(value)) {
                  JSONArray tkRatearray = JSONArray.fromObject(value);
                  if (tkRatearray != null) {
@@ -767,7 +804,7 @@
               }
            } else if ("tkRate2".equalsIgnoreCase(ty)) {
               // 获取对应参数
               String value = configService.get("tkRate_range");
               String value = configService.get(ConfigKeyEnum.tkRateRange.getKey());
               if (!StringUtil.isNullOrEmpty(value)) {
                  JSONArray tkRatearray = JSONArray.fromObject(value);
                  if (tkRatearray != null) {
@@ -783,7 +820,7 @@
               }
            } else if ("tkRate3".equalsIgnoreCase(ty)) {
               // 获取对应参数
               String value = configService.get("tkRate_range");
               String value = configService.get(ConfigKeyEnum.tkRateRange.getKey());
               if (!StringUtil.isNullOrEmpty(value)) {
                  JSONArray tkRatearray = JSONArray.fromObject(value);
                  if (tkRatearray != null) {
@@ -799,7 +836,7 @@
               }
            } else if ("tkRate4".equalsIgnoreCase(ty)) {
               // 获取对应参数
               String value = configService.get("tkRate_range");
               String value = configService.get(ConfigKeyEnum.tkRateRange.getKey());
               if (!StringUtil.isNullOrEmpty(value)) {
                  JSONArray tkRatearray = JSONArray.fromObject(value);
                  if (tkRatearray != null) {
@@ -815,7 +852,7 @@
               }
            } else if ("tkRate5".equalsIgnoreCase(ty)) {
               // 获取对应参数
               String value = configService.get("tkRate_range");
               String value = configService.get(ConfigKeyEnum.tkRateRange.getKey());
               if (!StringUtil.isNullOrEmpty(value)) {
                  JSONArray tkRatearray = JSONArray.fromObject(value);
                  if (tkRatearray != null) {
@@ -908,6 +945,43 @@
      return data;
   }
   private JSONObject searchDaTaoKeGoods(String key, int page, String filter, String order, String startprice,
         String endprice) {
      page = page + 1;
      int sort = DaTaoKeApiUtil.SORT_DEFAULT;
      if ("5".equalsIgnoreCase(order)) {
         sort = DaTaoKeApiUtil.SORT_COMMISSION;
      } else if ("1".equalsIgnoreCase(order)) {
         sort = DaTaoKeApiUtil.SORT_SALES;
      } else if ("2".equalsIgnoreCase(order)) {
         sort = DaTaoKeApiUtil.SORT_PRICE_HIGH_TO_LOW;
      } else if ("3".equalsIgnoreCase(order)) {
         sort = DaTaoKeApiUtil.SORT_PRICE_LOW_TO_HIGH;
      }
      List<TaoBaoGoodsBriefExtra> listExtra = new ArrayList<TaoBaoGoodsBriefExtra>();
      BigDecimal proportion = manageService.getFanLiRate();
      DaTaoKeGoodsResult result = DaTaoKeApiUtil.search(key, null, null, null, page, 20, sort);
      if (result != null && result.getGoodsList() != null)
         for (DaTaoKeDetailV2 goods : result.getGoodsList()) {
            listExtra.add(
                  TaoBaoUtil.getTaoBaoGoodsBriefExtra(TaoBaoUtil.convert(goods), proportion.toString(), null));
         }
      List<TaoBaoSearchNav> navList = new ArrayList<>();
      Gson gson = new GsonBuilder().create();
      Gson gson2 = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder())
            .excludeFieldsWithoutExposeAnnotation().create();
      JSONObject data = new JSONObject();
      data.put("nav", gson.toJson(navList));
      data.put("result", gson2.toJson(listExtra));
      data.put("count", result.getTotalCount());
      return data;
   }
   /**
    * 设置查询佣金比例范围
    * 
@@ -916,7 +990,7 @@
    */
   public void setSearchTkRate(int i, SearchFilter sf) {
      // 获取对应参数
      String value = configService.get("tkRate_range");
      String value = configService.get(ConfigKeyEnum.tkRateRange.getKey());
      if (!StringUtil.isNullOrEmpty(value)) {