From a1175313094799efcdbbecf2840a90350d3159a7 Mon Sep 17 00:00:00 2001
From: yujian <yujian@123.com>
Date: 星期一, 29 七月 2019 18:02:30 +0800
Subject: [PATCH] 首页加入缓存

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/jd/JDGoodsServiceImpl.java |   75 ++++++++++++++++++++++---------------
 1 files changed, 45 insertions(+), 30 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 ff6731f..dac16bf 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
@@ -28,14 +28,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 +42,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 +59,6 @@
 		cidMap.put(18L, "6728");// 姹借溅锛氭苯杞︾敤鍝�
 	}
 
-	
 	@Override
 	public List<GoodsClass> getSpecialClass() {
 		List<GoodsClass> list = new ArrayList<GoodsClass>();
@@ -82,23 +80,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{
+	public List<JDGoods> specialSearch(Integer page, Long cid) 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 +109,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 +133,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();
 			}
-		} 
-		
-		
+		}
+
 		if (!jdcid.contains(",")) {
 			return search(page, Constant.PAGE_SIZE, jdcid);
-		} 
-		
+		}
+
 		// 澶氫釜鍒嗙被id澶勭悊
 		List<JDGoods> list = new ArrayList<JDGoods>();
 		String[] arrayId = jdcid.split(",");
@@ -170,7 +166,6 @@
 		return list;
 	}
 
-	
 	private List<JDGoods> search(int page, int pageSize, String jdcid) {
 		JDSearchResult result = null;
 		String way = configService.get("jd_api_search_key");
@@ -187,7 +182,7 @@
 			jdfilter.setCategoryId(Integer.parseInt(jdcid));
 			result = JDUtil.searchByKey(jdfilter);
 		}
-		
+
 		if (result == null) {
 			return null;
 		} else {
@@ -195,4 +190,24 @@
 		}
 	}
 
+	@Cacheable(value = "jdCache", key = "'getIndexJDGoods-'+#page")
+	@Override
+	public JDSearchResult getIndexJDGoods(int page) {
+		JDSearchResult result = null;
+		String way = configService.get("jd_api_search_key");
+		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 = JDUtil.searchByKey(jdfilter);
+		}
+		return result;
+	}
+
 }

--
Gitblit v1.8.0