Commit de33f9b6 by liulianglang

手机数据排序

parent d22456d8
......@@ -19,12 +19,10 @@ import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Service
public class SjServiceImpl implements SjService {
......@@ -144,6 +142,11 @@ public class SjServiceImpl implements SjService {
}
}
}
sortByFeild(result,"openDate","desc");
result = result.stream()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing((o)-> o.get("idCard") + ";" ))), ArrayList::new));
returnMap.put("total",result.size());
returnMap.put("rows", result);
}
......@@ -157,6 +160,21 @@ public class SjServiceImpl implements SjService {
private static List<Map<String, Object>> sortByFeild(List<Map<String, Object>> list, String feild, String sortTyp) {
if(list!=null && list.size()>0){
list.sort((m1, m2) -> {
if (StringUtils.equals(sortTyp, "desc")) {
return String.valueOf(m2.get(feild)).compareTo(String.valueOf(m1.get(feild)));
} else {
return String.valueOf(m1.get(feild)).compareTo(String.valueOf(m2.get(feild)));
}
});
// 或者 Collections类里面的sort方法也是list.sort()与上面一样
// Collections.sort(list, (m1, m2)-> String.valueOf(m1.get(feild)).compareTo(String.valueOf(m2.get(feild)))); // lamuda排序
}
return list;
}
@Override
......@@ -261,6 +279,12 @@ public class SjServiceImpl implements SjService {
}
}
}
sortByFeild(result,"openDate","desc");
result = result.stream()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing((o)-> o.get("phoneNo") + ";" ))), ArrayList::new));
returnMap.put("total",result.size());
returnMap.put("rows", result);
}
......
package com.founder.util; import java.util.*; import com.alibaba.fastjson.JSONObject; import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.lang3.StringUtils;import org.apache.http.client.params.CookiePolicy; import javax.servlet.http.HttpServletRequest;import java.net.URLEncoder; /** * @auther wangyang * @date 2019/12/24 2:40 PM */public class EsUtils { //es登陆URI private static final String ES_LOGIN_URI = "http://10.142.77.46:8001/login2?username=xz&password=123456"; //es查询URI private static final String ES_QUERY_URI = "http://10.142.77.46:8001/sql"; /** * 新版查询ES * @param request * @param sql * @return */ public static Map<String,Object> newQueryEs(HttpServletRequest request,String sql){ String cookie = (String) request.getSession().getAttribute("EsCookie"); if(StringUtils.isBlank(cookie)){ //进行Es登录 cookie = newLoginEs(); request.getSession().setAttribute("EsCookie",cookie); } Map<String, Object> result = query(sql, cookie); return result; } /** * 将查询参数转义 * * @param json * @return */ public static String getTaskCreateJsonReq(String json) { String req = ""; try { String encode = URLEncoder.encode(json, "UTF-8"); req = "?sql=" + encode; } catch (Exception e) { req = "?sql="; } return req.replaceAll("\\+", "%20"); } /** * 登录ES并返回cookie * @return */ public static String newLoginEs() { String result = ""; HttpClient httpClient = new HttpClient(); // 模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式 GetMethod postMethod = new GetMethod(ES_LOGIN_URI); try { // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略 postMethod.addRequestHeader( "Connection", "close"); httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); httpClient.executeMethod(postMethod); // 获得登陆后的 Cookie org.apache.commons.httpclient.Cookie[] cookies = httpClient.getState().getCookies(); StringBuffer tmpcookies = new StringBuffer(); for (org.apache.commons.httpclient.Cookie c : cookies) { tmpcookies.append(c.toString() + ";"); } result = postMethod.getResponseBodyAsString(); System.out.println("模拟登录成功"); result = tmpcookies.toString(); } catch (Exception e) { e.printStackTrace(); }finally { postMethod.releaseConnection();//释放资源 //释放HttpClient 资源防止报 java.net.socketexception:connection reset 错误 } return result; } /** * 根据cookie查询ES * @param sql * @param tmpcookies * @return */ public static Map<String,Object> query(String sql,String tmpcookies){ String queryParam = getTaskCreateJsonReq(sql); String queryUrl = ES_QUERY_URI+queryParam; String result = ""; HttpClient httpClient = new HttpClient(); GetMethod getMethod = null; try{ getMethod = new GetMethod(queryUrl); // 设置关闭 getMethod.addRequestHeader( "Connection", "close"); // 每次访问需授权的网址时需带上前面的 cookie 作为通行证 getMethod.setRequestHeader("cookie", tmpcookies); // 你还可以通过 PostMethod/GetMethod 设置更多的请求后数据 // 例如,referer 从哪里来的,UA 像搜索引擎都会表名自己是谁,无良搜索引擎除外 httpClient.executeMethod(getMethod); //获取返回数据 result = getMethod.getResponseBodyAsString(); System.out.println("result---"+result); }catch (Exception e){ System.out.println("查询失败:"+e); System.out.println("查询SQL:"+sql); }finally{ getMethod.releaseConnection();//释放资源 //释放HttpClient 资源防止报 java.net.socketexception:connection reset 错误 httpClient.getHttpConnectionManager().closeIdleConnections(0); } Map<String,Object> map = (Map<String,Object>)JSONObject.parse(result); return map; } }
\ No newline at end of file
package com.founder.util; import java.net.SocketTimeoutException;import java.util.*; import com.alibaba.fastjson.JSONObject; import org.apache.commons.httpclient.ConnectTimeoutException;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.lang3.StringUtils;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.params.CookiePolicy; import javax.servlet.http.HttpServletRequest;import java.net.URLEncoder; /** * @auther wangyang * @date 2019/12/24 2:40 PM */public class EsUtils { //es登陆URI private static final String ES_LOGIN_URI = "http://10.142.77.46:8001/login2?username=xz&password=123456"; //es查询URI private static final String ES_QUERY_URI = "http://10.142.77.46:8001/sql"; /** * 新版查询ES * @param request * @param sql * @return */ public static Map<String,Object> newQueryEs(HttpServletRequest request,String sql){ String cookie = (String) request.getSession().getAttribute("EsCookie"); if(StringUtils.isBlank(cookie)){ //进行Es登录 cookie = newLoginEs(); request.getSession().setAttribute("EsCookie",cookie); } Map<String, Object> result = query(sql, cookie); return result; } /** * 将查询参数转义 * * @param json * @return */ public static String getTaskCreateJsonReq(String json) { String req = ""; try { String encode = URLEncoder.encode(json, "UTF-8"); req = "?sql=" + encode; } catch (Exception e) { req = "?sql="; } return req.replaceAll("\\+", "%20"); } /** * 登录ES并返回cookie * @return */ public static String newLoginEs() { String result = ""; HttpClient httpClient = new HttpClient(); // 模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式 GetMethod postMethod = new GetMethod(ES_LOGIN_URI); try { // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略 postMethod.addRequestHeader( "Connection", "close"); httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); //设置超时时间 //设置超时时间 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); httpClient.getHttpConnectionManager().getParams().setSoTimeout(5000); httpClient.executeMethod(postMethod); // 获得登陆后的 Cookie org.apache.commons.httpclient.Cookie[] cookies = httpClient.getState().getCookies(); StringBuffer tmpcookies = new StringBuffer(); for (org.apache.commons.httpclient.Cookie c : cookies) { tmpcookies.append(c.toString() + ";"); } result = postMethod.getResponseBodyAsString(); System.out.println("模拟登录成功"); result = tmpcookies.toString(); }catch (SocketTimeoutException | ConnectTimeoutException e) { e.printStackTrace(); System.out.println("请求连接超时:" + e.getMessage()); } catch (Exception e) { e.printStackTrace(); }finally { postMethod.releaseConnection();//释放资源 //释放HttpClient 资源防止报 java.net.socketexception:connection reset 错误 } return result; } /** * 根据cookie查询ES * @param sql * @param tmpcookies * @return */ public static Map<String,Object> query(String sql,String tmpcookies){ String queryParam = getTaskCreateJsonReq(sql); String queryUrl = ES_QUERY_URI+queryParam; String result = ""; HttpClient httpClient = new HttpClient(); GetMethod getMethod = null; try{ getMethod = new GetMethod(queryUrl); // 设置关闭 getMethod.addRequestHeader( "Connection", "close"); // 每次访问需授权的网址时需带上前面的 cookie 作为通行证 getMethod.setRequestHeader("cookie", tmpcookies); // 你还可以通过 PostMethod/GetMethod 设置更多的请求后数据 // 例如,referer 从哪里来的,UA 像搜索引擎都会表名自己是谁,无良搜索引擎除外 httpClient.executeMethod(getMethod); //获取返回数据 result = getMethod.getResponseBodyAsString(); System.out.println("result---"+result); }catch (Exception e){ System.out.println("查询失败:"+e); System.out.println("查询SQL:"+sql); }finally{ getMethod.releaseConnection();//释放资源 //释放HttpClient 资源防止报 java.net.socketexception:connection reset 错误 httpClient.getHttpConnectionManager().closeIdleConnections(0); } Map<String,Object> map = (Map<String,Object>)JSONObject.parse(result); return map; } }
\ No newline at end of file
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment