From 6f3ee199558c79d840137c1a77efe462aca63178 Mon Sep 17 00:00:00 2001
From: yujian <yujian@123.com>
Date: 星期五, 26 七月 2019 18:04:21 +0800
Subject: [PATCH] 京东专题 + 拼多多

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/jd/JDGoodsServiceImpl.java |  121 ++++++++++++++++++++++++++-------------
 1 files changed, 80 insertions(+), 41 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/jd/JDGoodsServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/jd/JDGoodsServiceImpl.java
index ce87ced..ff6731f 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/jd/JDGoodsServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/jd/JDGoodsServiceImpl.java
@@ -1,6 +1,5 @@
 package com.yeshi.fanli.service.impl.jd;
 
-import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -8,12 +7,10 @@
 
 import javax.annotation.Resource;
 
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.yeshi.utils.JsonUtil;
 
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
 import com.yeshi.fanli.dto.jd.JDFilter;
 import com.yeshi.fanli.dto.jd.JDSearchFilter;
 import com.yeshi.fanli.dto.jd.JDSearchResult;
@@ -26,13 +23,8 @@
 import com.yeshi.fanli.tag.PageEntity;
 import com.yeshi.fanli.util.Constant;
 import com.yeshi.fanli.util.StringUtil;
-import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory;
 import com.yeshi.fanli.util.jd.JDApiUtil;
 import com.yeshi.fanli.util.jd.JDUtil;
-import com.yeshi.fanli.vo.goods.GoodsDetailVO;
-
-import net.sf.json.JSONArray;
-import net.sf.json.JSONObject;
 
 @Service
 public class JDGoodsServiceImpl implements JDGoodsService {
@@ -44,11 +36,11 @@
 	private HongBaoManageService hongBaoManageService;
 	
 	
+	private Map<String, Long> countMap = new HashMap<String, Long>();
 	private static final Map<Long, String> cidMap = new HashMap<Long, String>();
-	private static final Map<String, Long> countMap = new HashMap<String, Long>();
 
 	static {
-		cidMap.put(1L, "nzjh");// 绮鹃��
+		cidMap.put(1L, "2,16,1,3");// 绮鹃��
 		cidMap.put(2L, "737");// 鐢靛櫒锛� 瀹剁敤鐢靛櫒
 		cidMap.put(3L, "652,670");// 鏁扮爜锛� 鏁扮爜/鐢佃剳銆佸姙鍏�
 		cidMap.put(4L, "9987");// 鎵嬫満锛氭墜鏈洪�氳 
@@ -93,9 +85,11 @@
 		return list; 
 	}
 	
+	
+	@Cacheable(value = "jdSpecialCache", key = "'specialSearch-'+#page+'-'+#cid")
 	@Override
 	@Transactional
-	public JDSearchResult specialSearch(Integer page, Long cid) throws JDGoodsException{
+	public List<JDGoods> specialSearch(Integer page, Long cid) throws JDGoodsException{
 		if (cid == null) {
 			throw new JDGoodsException(1, "鍒嗙被id涓虹┖");
 		}
@@ -105,55 +99,100 @@
 			throw new JDGoodsException(1, "鍒嗙被id涓嶅瓨鍦�");
 		}
 		
-		Long jdCid1 = null;
-		Integer categoryId = null;
-		long totalCount = (long) (page * Constant.PAGE_SIZE);
-		
-		
-		if (jdcid.contains(",")) {
+		// 绮鹃��
+		if (cid == 1) {
+			Integer categoryId = null;
+			int pageTemp = 0;
+			long addCount = 0;
+			long totalCount = (long) (page * Constant.PAGE_SIZE);
 			String[] arrayId = jdcid.split(",");
 			for (int i = 0; i < arrayId.length; i++) {
-				Long count = countMap.get(cid + "" + arrayId[i]);
-				if (count != null && totalCount > count) {
+				String id = arrayId[i];
+				Long count = countMap.get(cid + "-" + id);
+				if (count != null) {
+					addCount += count;
+				}
+				
+				if (count != null && totalCount > addCount) {
+					pageTemp = (int)(addCount / Constant.PAGE_SIZE);
 					continue; // 褰撳墠鍒嗙被鍟嗗搧涓嶈冻
 				} else {
-					jdCid1 =  Long.parseLong(jdcid);
-					categoryId = Integer.parseInt(jdcid);
+					page = page - pageTemp; 
+					categoryId = Integer.parseInt(id);
+					break;
 				}
 			}
-		} else {
-			jdCid1 =  Long.parseLong(jdcid);
-			categoryId = Integer.parseInt(jdcid);
+			
+			if (categoryId == null) {
+				return null;
+			}
+			
+			JDSearchResult result = JDApiUtil.getJingFenGoods(page, categoryId);
+			
+			long count = 0;
+			if (result != null) {
+				PageEntity pageEntity = result.getPageEntity();
+				if (pageEntity != null) {
+					count = pageEntity.getTotalCount();
+				}
+			}
+			
+			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();
+			}
+		} 
+		
+		
+		if (!jdcid.contains(",")) {
+			return search(page, Constant.PAGE_SIZE, jdcid);
+		} 
+		
+		// 澶氫釜鍒嗙被id澶勭悊
+		List<JDGoods> list = new ArrayList<JDGoods>();
+		String[] arrayId = jdcid.split(",");
+		for (int i = 0; i < arrayId.length; i++) {
+			if (i >= 3) {
+				break;
+			}
+			List<JDGoods> listGoods = search(page, 10, arrayId[i]);
+			if (listGoods != null && listGoods.size() > 0) {
+				list.addAll(listGoods);
+			}
 		}
-		
-		
-		// 鎼滅储
+		return list;
+	}
+
+	
+	private List<JDGoods> search(int page, int pageSize, String jdcid) {
 		JDSearchResult result = null;
 		String way = configService.get("jd_api_search_key");
 		if ("1".equals(way)) { // API鎼滅储
 			JDFilter filterAPI = new JDFilter();
 			filterAPI.setPageIndex(page);
-			filterAPI.setPageSize(Constant.PAGE_SIZE);
-			filterAPI.setCid1(jdCid1);
+			filterAPI.setPageSize(pageSize);
+			filterAPI.setCid1(Long.parseLong(jdcid));
 			result = JDApiUtil.queryByKey(filterAPI);
 		} else { // 缃戦〉鐖彇
 			JDSearchFilter jdfilter = new JDSearchFilter();
 			jdfilter.setPageNo(page);
-			jdfilter.setPageSize(Constant.PAGE_SIZE);
-			jdfilter.setCategoryId(categoryId);
+			jdfilter.setPageSize(pageSize);
+			jdfilter.setCategoryId(Integer.parseInt(jdcid));
 			result = JDUtil.searchByKey(jdfilter);
 		}
 		
-		long count = 0;
-		if (result != null) {
-			PageEntity pageEntity = result.getPageEntity();
-			if (pageEntity != null) {
-				count = pageEntity.getTotalCount();
-			}
+		if (result == null) {
+			return null;
+		} else {
+			return result.getGoodsList();
 		}
-		countMap.put(cid + "" + jdCid1, count);
-		
-		return result;
 	}
 
 }

--
Gitblit v1.8.0