Browse Source

轮播图 按顺序排序

wangdy 8 years ago
parent
commit
d6ec0186b0

+ 2 - 1
src/main/java/com/uas/platform/b2c/advertise/ad/api/CarouselsController.java

@@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.LinkedHashMap;
 import java.util.List;
 
 /**
@@ -30,7 +31,7 @@ public class CarouselsController {
      * @return the carousels
      */
     @RequestMapping(value = "/{module}", method = RequestMethod.GET)
-    public List<JSONObject> getCarousels(@PathVariable("module") String module) {
+    public List<LinkedHashMap> getCarousels(@PathVariable("module") String module) {
         return carouselService.getCarousels(module);
     }
 }

+ 2 - 1
src/main/java/com/uas/platform/b2c/advertise/ad/service/CarouselService.java

@@ -2,6 +2,7 @@ package com.uas.platform.b2c.advertise.ad.service;
 
 import net.sf.json.JSONObject;
 
+import java.util.LinkedHashMap;
 import java.util.List;
 
 /**
@@ -18,5 +19,5 @@ public interface CarouselService {
      * @param usedFor the used for
      * @return carousels carousels
      */
-    List<JSONObject> getCarousels(String usedFor);
+    List<LinkedHashMap> getCarousels(String usedFor);
 }

+ 24 - 4
src/main/java/com/uas/platform/b2c/advertise/ad/service/impl/CarouselsServiceImpl.java

@@ -3,12 +3,13 @@ package com.uas.platform.b2c.advertise.ad.service.impl;
 import com.uas.platform.b2c.advertise.ad.service.CarouselService;
 import com.uas.platform.b2c.core.config.MicroServicesConf;
 import com.uas.platform.b2c.core.utils.JsonUtil;
+import net.sf.json.JSONException;
 import net.sf.json.JSONObject;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
-import java.util.List;
+import java.util.*;
 
 /**
  * The type Carousels service implementation.
@@ -30,10 +31,29 @@ public class CarouselsServiceImpl implements CarouselService{
 	}
 
 	@Override
-	public List<JSONObject> getCarousels(String usedFor) {
+	public List<LinkedHashMap> getCarousels(String usedFor) {
 		String url = conf.getRequestUrlForCarousel(20030, "/carousels?usedFor=" + usedFor);
 		String result = restTemplate.getForEntity(url, String.class).getBody();
-		List<JSONObject> r = JsonUtil.parseJsonToObject(result);
-		return r;
+		List<LinkedHashMap> jsonValues = JsonUtil.parseJsonToObject(result);
+
+		Collections.sort( jsonValues, new Comparator<LinkedHashMap>() {
+			private static final String KEY_NAME = "orderNumber";
+
+			@Override
+			public int compare(LinkedHashMap a, LinkedHashMap b) {
+				String valA = new String();
+				String valB = new String();
+
+				try {
+					valA = String.valueOf(a.get(KEY_NAME));
+					valB = String.valueOf(b.get(KEY_NAME));
+				}
+				catch (JSONException e) {
+				}
+				return valA.compareTo(valB);
+			}
+		});
+
+		return jsonValues;
 	}
 }