package com.elephant.framework.galaxy.aroad.bigdata;

import com.google.gson.Gson;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 自定义elasticsearch工具类，
 * 1、实现elasticsearch集群的连接
 * 2、路段、桥梁索引数据插入
 * 3、路段、桥梁分词检索
 * 4、分词检索出来的数据是否是路段数据或者桥梁数据
 * @Param:
 * @return:
 * @Author: dongjili
 * @Date:
 */
@Component
public class EsUtil_fanshe {
    public static TransportClient transportClient;
    public static Gson gson = new Gson();
    public static String host1;
    public static String host2;
    public static String host3;
    public static int port;
    @Value("${connect.host_1}")
    public  void setHost1(String host_1){
        host1 =host_1;
    }
    @Value("${connect.host_2}")
    public  void setHost2(String host_2){
        host2 =host_2;
    }

    @Value("${connect.host_3}")
    public  void setHost3(String hos_t3){
        host3 =hos_t3;
    }

    @Value("${connect.port_net}")
    public  void setPort(int port_net){
        port =port_net;
    }

    /**
     * @Description: 创建连接
     * @Param:
     * @return: transportClient
     * @Author: dongjili
     * @Date:
     */
    public static TransportClient getClient() throws Exception {
        Settings settings = Settings.builder().put("cluster.name", "myes").build();
        if (transportClient == null) {
            synchronized (EsUtil_fanshe.class) {
                // 创建Client连接对象
                transportClient = new PreBuiltTransportClient(settings)
                        .addTransportAddress(new TransportAddress(InetAddress.getByName(host1), port))
                        .addTransportAddress(new TransportAddress(InetAddress.getByName(host2), port))
                        .addTransportAddress(new TransportAddress(InetAddress.getByName(host3), port));
            }

        }

        return transportClient;
    }

    /**
     * @Description: 释放资源
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date:
     */
    public static void close() {
        //释放资源
        if (transportClient != null) {
            transportClient.close();
        }
    }
/*
 * @Author dongjili
 * @Description 获取表中最大的年份
 * @Date 下午 5:58 2019/7/4 0004
 * @Param SearchRequestBuilder searchRequestBuilder
 * @return maxYear
 **/
    public static String getMaxYear(SearchRequestBuilder searchRequestBuilder) {
        SearchResponse responseStart = searchRequestBuilder.get();
        SearchHit[] hitsStart = responseStart.getHits().getHits();
        String maxYear = hitsStart[0].getSourceAsMap().get("year").toString();
        System.out.println(maxYear);
        return maxYear;
    }
    /*
     * @Author dongjili
     * @Description 获取桥梁属性字段
     * @Date 下午 5:59 2019/7/4 0004
     * @Param
     * @return
     **/
    public static String[] getbridgeNames() {
        return new String[]{
                "id",
                "quhuadaima",
                "quhuamingcheng",
                "qiaoliangmingcheng",
                "qiaoliangdaima",
                "zhongxinzhuanghao",
                "luxianbianma",
                "luxianmingcheng",
                "jishudengji",
                "quanchang",
                "kuajingzongchang",
                "dankongzuidakuajing",
                "qiaoliangquankuan",
                "qiaomianjingkuan",
                "kuajingfenleidaima",
                "kuajingfenleileixing",
                "nianxianfenleidaima",
                "nianxianfenleileixing",
                "yuanshangbujiegou1daima",
                "yuanshangbujiegou1xingshi",
                "yuanshangbujiegou2daima",
                "yuanshangbujiegou2xingshi",
                "shangbujiegoudaima",
                "shangbujiegouxingshi",
                "shangbujiegoucailiaodaima",
                "shangbujiegoucailiaomingcheng",
                "qiaodunleixingdaima",
                "qiaodunleixingmingcheng",
                "shejihezaidaima",
                "shejihezaidengji",
                "kangzhendengjidaima",
                "kangzhendengji",
                "kuayuediwudaima",
                "kuayuediwuleixing",
                "kuayuemingcheng",
                "tonghangdengji",
                "fangzhuangleixing",
                "lijiaoleibie",
                "jianshedanwei",
                "shejidanwei",
                "shigongdanwei",
                "jianlidanwei",
                "xiujianniandu",
                "tongcheriqi",
                "guanyangdaima",
                "guanyangmingcheng",
                "jianguandanweimingcheng",
                "shoufeixingzhidaima",
                "shoufeixingzhi",
                "pingdingdengjidaima",
                "pingdingdengji",
                "pingdingdanwei",
                "pingdingriqi",
                "gaizaoniandu",
                "wangongriqi",
                "gaizaobuwei",
                "gaizaoshigongdanwei",
                "gongchengxingzhi",
                "bubuxiangmu",
                "binghaidaima",
                "binghaibuwei",
                "binghaimiaoshu",
                "guanzhicuoshidaima",
                "guanzhicuoshi",
                "beizhu",
                "zhuqiaokongshu",
                "zhuqiaozhukua",
                "zhuqiaobiankua",
                "qianyinqiaochang",
                "houyinqiaochang",
                "qiaoxiajingkong",
                "xiabujiegoudaima",
                "xiabujiegoumingcheng",
                "qiaotaidaima",
                "qiaotaimingcheng",
                "zhizuodaima",
                "zhizuomingcheng",
                "qiaomianpuzhuangdaima",
                "qiaomianpuzhuangmingcheng",
                "shensuofengdaima",
                "shensuofengmingcheng",
                "wanpoxiedaima",
                "wanpoxiemingcheng",
                "yuyingliqiao",
                "zaixiuzaijian",
                "zhadaomianji",
                "shejihongshuipinlv",
                "zongzaojia",
                "maishezhuanghao",
                "shifouzhadaoqiao",
                "shifoushangkuaqiao",
                "guanlidanweimingcheng",
                "guanlidanweidaima",
                "biangengyuanyindaima",
                "biangengyuanyin",
                "biangengshijian",
                "xiangmuxingzhi",
                "xiangmumingcheng",
                "jihuaxiangmubianma",
                "xiangmuleibie",
                "xiangmudaima",
                "xiangmuluxianbianma",
                "xiangmuluxianmingcheng",
                "xiangmuqiaoliangzhuanghao",
                "xiangmubiangengyuanyin",
                "xiangmubiangengshijian",
                "touzilaiyuan",
                "qiaoliangsuozaiweizhi",
                "shifoukuanluzhaiqiao",
                "shifouzaizhangdaqiaolaingmuluzhong",
                "tianbaodanweimingcheng",
                "shape",
                "qiaokuazuhe",
                "year"
        };
    }
    /*
     * @Author dongjili
     * @Description 获取桥梁查询结果数据
     * @Date 下午 6:00 2019/7/4 0004
     * @Param
     * @return
     **/
    public static void getBaseBridgeDataList(String[] bridgeNames, SearchHits hits, List<Basebridgealldata> basebridgealldataList) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
        for (SearchHit hit : hits) {
            //System.out.println(hit.getSourceAsString());
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //LuKeBridge luKeBridge = new LuKeBridge();
            //获取该类在堆区的Class对象
            Class<?> clazz = Class.forName("com.elephant.framework.galaxy.aroad.bigdata.Basebridgealldata");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            Basebridgealldata basebridgealldata = (Basebridgealldata) constructor.newInstance();
            //获得私有的属性对象
            Field[] beanFiled = clazz.getDeclaredFields();
            basebridgealldata.setId(hit.getId());
            for (int z = 1; z < bridgeNames.length; z++) {
                try {
                    //强制反射,让private 的属性也可以访问
                    beanFiled[z].setAccessible(true);
                    //把得到的属性进行赋值，就是把读取到的单元格中的数据赋给对应的属性
                       /* Row.MissingCellPolicy RETURN_NULL_AND_BLANK
                        Row.MissingCellPolicy RETURN_BLANK_AS_NULL
                        Row.MissingCellPolicy CREATE_NULL_AS_BLANK*/
                    if (beanFiled[z].getType().equals(String.class)) {
                        beanFiled[z].set(basebridgealldata, sourceAsMap.get(bridgeNames[z]) == null ? "" : sourceAsMap.get(bridgeNames[z]).toString());
                    } else if (beanFiled[z].getType().equals(Integer.class)) {
                        beanFiled[z].set(basebridgealldata, sourceAsMap.get(bridgeNames[z]) == null ? 0 : Integer.valueOf(sourceAsMap.get(bridgeNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Long.class.getName())) {
                        beanFiled[z].set(basebridgealldata, sourceAsMap.get(bridgeNames[z]) == null ? "" : Long.valueOf(sourceAsMap.get(bridgeNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Float.class)) {
                        beanFiled[z].set(basebridgealldata, sourceAsMap.get(bridgeNames[z]) == null ? "" : Float.valueOf(sourceAsMap.get(bridgeNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Double.class)) {
                        beanFiled[z].set(basebridgealldata, sourceAsMap.get(bridgeNames[z]) == null ? 0.0 : Double.valueOf(sourceAsMap.get(bridgeNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Byte.class)) {
                        beanFiled[z].set(basebridgealldata, sourceAsMap.get(bridgeNames[z]) == null ? "" : Byte.valueOf(sourceAsMap.get(bridgeNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Boolean.class)) {
                        beanFiled[z].set(basebridgealldata, sourceAsMap.get(bridgeNames[z]) == null ? "" : Boolean.valueOf(sourceAsMap.get(bridgeNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Date.class)) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = null;
                        try {
                            date = sdf.parse(sourceAsMap.get(bridgeNames[z]) == null ? "" : sourceAsMap.get(bridgeNames[z]).toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        beanFiled[z].set(basebridgealldata, date);
                    } else {
                        beanFiled[z].set(basebridgealldata, sourceAsMap.get(bridgeNames[z]) == null ? "" : sourceAsMap.get(bridgeNames[z]).toString());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            basebridgealldataList.add(basebridgealldata);
        }
    }
/*
 * @Author dongjili
 * @Description 获取路段属性字段
 * @Date 下午 6:01 2019/7/4 0004
 * @Param
 * @return
 **/
    public static String[] getHighWayNames() {
        return new String[]{
                "id",
                "luxianbianhao",
                "quhuadaima",
                "luxianmingcheng",
                "qidianmingcheng",
                "zhidianmingcheng",
                "qidianzhuanghaoa",
                "zhidianzhuanghao",
                "licheng",
                "lichengjisuan",
                "sandlicheng",
                "dengjidaima",
                "dengjimingcheng",
                "chedaodaima",
                "chedaofenlei",
                "mianchengdaima",
                "mianchengfenlei",
                "lujikuandu",
                "lumiankuandu",
                "shejishisu",
                "xiujianniandu",
                "gaijianniandu",
                "chengguanlu",
                "duantoulu",
                "chongfubianma",
                "chongfuxuhao",
                "qidianzhuanghaob",
                "zhongdianzhuanghao",
                "yanghulicheng",
                "qingyulicheng",
                "kelvhaolicheng",
                "yilvhualicheng",
                "dimaodaima",
                "dimaohanzi",
                "handongshuliang",
                "maisheqidianzhuanghao",
                "maishezhidianzhuanghao",
                "quhuamingchengshi",
                "quhuamingchengxian",
                "quhuamingchengxiang",
                "luduanxuhao",
                "guanyangdanweileibie",
                "beizhu",
                "kaigongriqi",
                "jungongriqi",
                "qidianfenjiedianleixing",
                "zhidianfenjiedianleixing",
                "shengjichurukou",
                "gbmlicheng",
                "yangbanlicheng",
                "tongcheriqi",
                "luxiandifangmingcheng",
                "jianshexingzhidaima",
                "jianshexingzhihanzi",
                "duanlianleixing",
                "luduanleixingdaima",
                "luduanleixing",
                "guanyangdanweimingcheng",
                "mqi",
                "pingdingdengji",
                "shoufeixingzhi",
                "biangengyuanyindaima",
                "biangengyuanyin",
                "biangengshijian",
                "xiangmuxingzhi",
                "xiangmumingcheng",
                "xiangmudaima",
                "xiangmuqidianmingcheng",
                "xiangmuzhidianmingcheng",
                "xiangmuleibie",
                "jihuaxiangmubianma",
                "xiangmuluxianbianma",
                "xiangmuluxianmingcheng",
                "xiangmubiangengyuanyin",
                "xiangmubiangengshijian",
                "touzilaiyuan",
                "miancenghoudu",
                "zuijinyicidaxiuniandu",
                "zuijinyicizhongxiuniandu",
                "guodaotiaozhengqianluxianbianma",
                "guodaozhuanghaochuandiyuliulicheng",
                "nianpingjunrijiaotongliang",
                "shifouanganxiangongluguanlijieyang",
                "luxiantiaozhengleixing",
                "shangnianluxianbianma",
                "shangnianluxianmingcheng",
                "shangnianluxianqidianzhuanghao",
                "shangnianluxianzhidianzhuanghao",
                "tianbaodanweimingcheng",
                "shape",
                "year"
        };
    }
    /*
     * @Author dongjili
     * @Description 获取路段查询结果数据
     * @Date 下午 6:02 2019/7/4 0004
     * @Param
     * @return
     **/
    public static void getBaseHighWayAllDataList(String[] highwayNames, SearchHits hits, List<Basehighwayalldata> basehighwayalldataList) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //LuKeHighWay luKeHighWay = new LuKeHighWay();
            //获取该类在堆区的Class对象
            Class<?> clazz = Class.forName("com.elephant.framework.galaxy.aroad.bigdata.Basehighwayalldata");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            Basehighwayalldata basehighwayalldata = (Basehighwayalldata) constructor.newInstance();
            //获取私有的属性字段
            Field[] beanFiled = clazz.getDeclaredFields();
            basehighwayalldata.setId(hit.getId());
            for (int z = 1; z < highwayNames.length; z++) {
                //暴力破解
                // beanFiled[a].setAccessible(true);
                //给路段属性复制
                //beanFiled[a].set(basehighwayalldata, sourceAsMap.get(highwayNames[a]) == null ? "" : sourceAsMap.get(highwayNames[a]).toString());
                try {
                    //强制反射,让private 的属性也可以访问
                    beanFiled[z].setAccessible(true);
                    //把得到的属性进行赋值，就是把读取到的单元格中的数据赋给对应的属性
                       /* Row.MissingCellPolicy RETURN_NULL_AND_BLANK
                        Row.MissingCellPolicy RETURN_BLANK_AS_NULL
                        Row.MissingCellPolicy CREATE_NULL_AS_BLANK*/
                    if (beanFiled[z].getType().equals(String.class)) {
                        beanFiled[z].set(basehighwayalldata, sourceAsMap.get(highwayNames[z]) == null ? "" : sourceAsMap.get(highwayNames[z]).toString());
                    } else if (beanFiled[z].getType().equals(Integer.class)) {
                        beanFiled[z].set(basehighwayalldata, sourceAsMap.get(highwayNames[z]) == null ? 0 : Integer.valueOf(sourceAsMap.get(highwayNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Long.class.getName())) {
                        beanFiled[z].set(basehighwayalldata, sourceAsMap.get(highwayNames[z]) == null ? "" : Long.valueOf(sourceAsMap.get(highwayNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Float.class)) {
                        beanFiled[z].set(basehighwayalldata, sourceAsMap.get(highwayNames[z]) == null ? "" : Float.valueOf(sourceAsMap.get(highwayNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Double.class)) {
                        beanFiled[z].set(basehighwayalldata, sourceAsMap.get(highwayNames[z]) == null ? 0.0 : Double.valueOf(sourceAsMap.get(highwayNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Byte.class)) {
                        beanFiled[z].set(basehighwayalldata, sourceAsMap.get(highwayNames[z]) == null ? "" : Byte.valueOf(sourceAsMap.get(highwayNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Boolean.class)) {
                        beanFiled[z].set(basehighwayalldata, sourceAsMap.get(highwayNames[z]) == null ? "" : Boolean.valueOf(sourceAsMap.get(highwayNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Date.class)) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = null;
                        try {
                            date = sdf.parse(sourceAsMap.get(highwayNames[z]) == null ? "" : sourceAsMap.get(highwayNames[z]).toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        beanFiled[z].set(basehighwayalldata, date);
                    } else {
                        beanFiled[z].set(basehighwayalldata, sourceAsMap.get(highwayNames[z]) == null ? "" : sourceAsMap.get(highwayNames[z]).toString());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            basehighwayalldataList.add(basehighwayalldata);
        }
    }
/*
 * @Author dongjili
 * @Description 获取隧道属性字段
 * @Date 下午 6:02 2019/7/4 0004
 * @Param
 * @return
 **/
    public static String[] getBasicTunnelNames() {
        return new String[]{
                "id",
                "suidaomingcheng",
                "suidaodaima",
                "zhongxinzhuanghao",
                "luxianbianma",
                "luxianmingcheng",
                "jishudengji",
                "changdu",
                "jingkuan",
                "jinggao",
                "changdufenleidaima",
                "fenleimingcheng",
                "shuixiasuidao",
                "xiujianniandu",
                "jianshedanwei",
                "shejidanwei",
                "shigongdanwei",
                "jianlidanwei",
                "tongcheshijian",
                "shuidaoyanghudengji",
                "guanyangdanweixingzhi",
                "guanyangdanweimingcheng",
                "jianguandanwei",
                "pingdingdengji",
                "pingdingriqi",
                "pingdingdanwei",
                "pingdingdengjitujian",
                "pingdingriqitujian",
                "pingdingdanweitujian",
                "pingdingdengjijidian",
                "pingdingriqijidian",
                "pingdingdanweijidian",
                "pingdingdengjiqita",
                "pingdingriqiqita",
                "pingdingdanweiqita",
                "gaijianniandu",
                "wangongriqi",
                "gaizaibuwei",
                "gongchengxingzhi",
                "binghaibuwei",
                "binghaimiaoshu",
                "xianjizhengqudaima",
                "shifouzaizhangdasuidaomuluzhong",
                "beizhu",
                "shape",
                "year"
        };
    }
    /*
     * @Author dongjili
     * @Description 获取隧道查询结果集
     * @Date 下午 6:03 2019/7/4 0004
     * @Param
     * @return
     **/
    public static void getBaseTunnelAllDataList(String[] basicTunnelNames, SearchHits hits, List<Basetunnelalldata> basetunnelalldataList) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //LuKeHighWay luKeHighWay = new LuKeHighWay();
            //获取该类在堆区的Class对象
            Class<?> clazz = Class.forName("com.elephant.framework.galaxy.aroad.bigdata.Basetunnelalldata");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            Basetunnelalldata basetunnelalldata = (Basetunnelalldata) constructor.newInstance();
            //获取私有的属性字段
            Field[] beanFiled = clazz.getDeclaredFields();
            basetunnelalldata.setId(hit.getId());
            for (int z = 1; z < basicTunnelNames.length; z++) {
                try {
                    //强制反射,让private 的属性也可以访问
                    beanFiled[z].setAccessible(true);
                    //把得到的属性进行赋值，就是把读取到的单元格中的数据赋给对应的属性
                       /* Row.MissingCellPolicy RETURN_NULL_AND_BLANK
                        Row.MissingCellPolicy RETURN_BLANK_AS_NULL
                        Row.MissingCellPolicy CREATE_NULL_AS_BLANK*/
                    if (beanFiled[z].getType().equals(String.class)) {
                        beanFiled[z].set(basetunnelalldata, sourceAsMap.get(basicTunnelNames[z]) == null ? "" : sourceAsMap.get(basicTunnelNames[z]).toString());
                    } else if (beanFiled[z].getType().equals(Integer.class)) {
                        beanFiled[z].set(basetunnelalldata, sourceAsMap.get(basicTunnelNames[z]) == null ? 0 : Integer.valueOf(sourceAsMap.get(basicTunnelNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Long.class.getName())) {
                        beanFiled[z].set(basetunnelalldata, sourceAsMap.get(basicTunnelNames[z]) == null ? "" : Long.valueOf(sourceAsMap.get(basicTunnelNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Float.class)) {
                        beanFiled[z].set(basetunnelalldata, sourceAsMap.get(basicTunnelNames[z]) == null ? "" : Float.valueOf(sourceAsMap.get(basicTunnelNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Double.class)) {
                        beanFiled[z].set(basetunnelalldata, sourceAsMap.get(basicTunnelNames[z]) == null ? 0.0 : Double.valueOf(sourceAsMap.get(basicTunnelNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Byte.class)) {
                        beanFiled[z].set(basetunnelalldata, sourceAsMap.get(basicTunnelNames[z]) == null ? "" : Byte.valueOf(sourceAsMap.get(basicTunnelNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Boolean.class)) {
                        beanFiled[z].set(basetunnelalldata, sourceAsMap.get(basicTunnelNames[z]) == null ? "" : Boolean.valueOf(sourceAsMap.get(basicTunnelNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Date.class)) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = null;
                        try {
                            date = sdf.parse(sourceAsMap.get(basicTunnelNames[z]) == null ? "" : sourceAsMap.get(basicTunnelNames[z]).toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        beanFiled[z].set(basetunnelalldata, date);
                    } else {
                        beanFiled[z].set(basetunnelalldata, sourceAsMap.get(basicTunnelNames[z]) == null ? "" : sourceAsMap.get(basicTunnelNames[z]).toString());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            basetunnelalldataList.add(basetunnelalldata);
        }
    }
/*
 * @Author dongjili
 * @Description 获取收费站属性字段
 * @Date 下午 6:03 2019/7/4 0004
 * @Param
 * @return
 **/
    public static String[] getBaseRoadTollNames() {
        return new String[]{"id",
                "roadCode",
                "roadName",
                "number",
                "code",
                "name",
                "shortName",
                "tecLevel",
                "locationType",
                "tollDirection",
                "entNum",
                "entEtcNum",
                "entMtcNum",
                "entMixNum",
                "expNum",
                "expEtcNum",
                "expMtcNum",
                "expMixNum",
                "managementUnit",
                "longitude",
                "latitude",
                "year"
        };
    }
/*
 * @Author dongjili
 * @Description 获取收费站查询结果集
 * @Date 下午 6:04 2019/7/4 0004
 * @Param
 * @return
 **/
    public static void getBaseRoadTollStrings(String[] baseRoadTollNames, SearchHits hits, ArrayList<BaseRoadToll_String> baseRoadTollStrings) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //LuKeHighWay luKeHighWay = new LuKeHighWay();
            //获取该类在堆区的Class对象
            Class<?> clazz = Class.forName("com.pinxiang.domain.BaseRoadToll_String");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            BaseRoadToll_String baseRoadTollString = (BaseRoadToll_String) constructor.newInstance();
            //获取私有的属性字段
            Field[] beanFiled = clazz.getDeclaredFields();
            baseRoadTollString.setId(hit.getId());
            for (int z = 1; z < baseRoadTollNames.length; z++) {
                try {
                    //强制反射,让private 的属性也可以访问
                    beanFiled[z].setAccessible(true);
                    //把得到的属性进行赋值，就是把读取到的单元格中的数据赋给对应的属性
                       /* Row.MissingCellPolicy RETURN_NULL_AND_BLANK
                        Row.MissingCellPolicy RETURN_BLANK_AS_NULL
                        Row.MissingCellPolicy CREATE_NULL_AS_BLANK*/
                    if (beanFiled[z].getType().equals(String.class)) {
                        beanFiled[z].set(baseRoadTollString, sourceAsMap.get(baseRoadTollNames[z]) == null ? "" : sourceAsMap.get(baseRoadTollNames[z]).toString());
                    } else if (beanFiled[z].getType().equals(Integer.class)) {
                        beanFiled[z].set(baseRoadTollString, sourceAsMap.get(baseRoadTollNames[z]) == null ? 0 : Integer.valueOf(sourceAsMap.get(baseRoadTollNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Long.class.getName())) {
                        beanFiled[z].set(baseRoadTollString, sourceAsMap.get(baseRoadTollNames[z]) == null ? "" : Long.valueOf(sourceAsMap.get(baseRoadTollNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Float.class)) {
                        beanFiled[z].set(baseRoadTollString, sourceAsMap.get(baseRoadTollNames[z]) == null ? "" : Float.valueOf(sourceAsMap.get(baseRoadTollNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Double.class)) {
                        beanFiled[z].set(baseRoadTollString, sourceAsMap.get(baseRoadTollNames[z]) == null ? 0.0 : Double.valueOf(sourceAsMap.get(baseRoadTollNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Byte.class)) {
                        beanFiled[z].set(baseRoadTollString, sourceAsMap.get(baseRoadTollNames[z]) == null ? "" : Byte.valueOf(sourceAsMap.get(baseRoadTollNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Boolean.class)) {
                        beanFiled[z].set(baseRoadTollString, sourceAsMap.get(baseRoadTollNames[z]) == null ? "" : Boolean.valueOf(sourceAsMap.get(baseRoadTollNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Date.class)) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = null;
                        try {
                            date = sdf.parse(sourceAsMap.get(baseRoadTollNames[z]) == null ? "" : sourceAsMap.get(baseRoadTollNames[z]).toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        beanFiled[z].set(baseRoadTollString, date);
                    } else {
                        beanFiled[z].set(baseRoadTollString, sourceAsMap.get(baseRoadTollNames[z]) == null ? "" : sourceAsMap.get(baseRoadTollNames[z]).toString());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            baseRoadTollStrings.add(baseRoadTollString);

        }
    }
/*
 * @Author dongjili
 * @Description 获取服务区属性字段
 * @Date 下午 6:04 2019/7/4 0004
 * @Param
 * @return
 **/
    public static String[] getBaseHighWayServiceNames() {
        return new String[]{
                "id",
                "lxbh",
                "lxmc",
                "zh1",
                "fwsslx",
                "fwssmc",
                "csyysj",
                "bjxs",
                "jyms",
                "zdmj",
                "tccmj",
                "tcwsl",
                "fwssgnSFycs",
                "fwssgnSfyjyss",
                "fwssgnSfyjqss",
                "fwssgnSfyclcdss",
                "fwssgnSfycyss",
                "fwssgnSfycs",
                "fwssgnSfyzsss",
                "fwssgnSfyqcwx",
                "bz",
                "lxbm",
                "zh",
                "jd",
                "wd",
                "year"
        };
    }
/*
 * @Author dongjili
 * @Description 获取服务区查询结果集
 * @Date 下午 6:05 2019/7/4 0004
 * @Param
 * @return
 **/
    public static void getBaseHighWayServices(String[] baseHighWayServiceNames, SearchHits hits, List<BaseHighWayService> baseHighWayServices) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //LuKeHighWay luKeHighWay = new LuKeHighWay();
            //获取该类在堆区的Class对象
            Class<?> clazz = Class.forName("com.pinxiang.domain.BaseHighWayService");
            //获取私有的构造方法
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            //暴力破解
            constructor.setAccessible(true);
            //实例化对象
            BaseHighWayService baseHighWayService = (BaseHighWayService) constructor.newInstance();
            //获取私有的属性字段
            Field[] beanFiled = clazz.getDeclaredFields();
            baseHighWayService.setId(hit.getId());
            for (int z = 1; z < baseHighWayServiceNames.length; z++) {
                try {
                    //强制反射,让private 的属性也可以访问
                    beanFiled[z].setAccessible(true);
                    //把得到的属性进行赋值，就是把读取到的单元格中的数据赋给对应的属性
                       /* Row.MissingCellPolicy RETURN_NULL_AND_BLANK
                        Row.MissingCellPolicy RETURN_BLANK_AS_NULL
                        Row.MissingCellPolicy CREATE_NULL_AS_BLANK*/
                    if (beanFiled[z].getType().equals(String.class)) {
                        beanFiled[z].set(baseHighWayService, sourceAsMap.get(baseHighWayServiceNames[z]) == null ? "" : sourceAsMap.get(baseHighWayServiceNames[z]).toString());
                    } else if (beanFiled[z].getType().equals(Integer.class)) {
                        beanFiled[z].set(baseHighWayService, sourceAsMap.get(baseHighWayServiceNames[z]) == null ? 0 : Integer.valueOf(sourceAsMap.get(baseHighWayServiceNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Long.class.getName())) {
                        beanFiled[z].set(baseHighWayService, sourceAsMap.get(baseHighWayServiceNames[z]) == null ? "" : Long.valueOf(sourceAsMap.get(baseHighWayServiceNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Float.class)) {
                        beanFiled[z].set(baseHighWayService, sourceAsMap.get(baseHighWayServiceNames[z]) == null ? "" : Float.valueOf(sourceAsMap.get(baseHighWayServiceNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Double.class)) {
                        beanFiled[z].set(baseHighWayService, sourceAsMap.get(baseHighWayServiceNames[z]) == null ? 0.0 : Double.valueOf(sourceAsMap.get(baseHighWayServiceNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Byte.class)) {
                        beanFiled[z].set(baseHighWayService, sourceAsMap.get(baseHighWayServiceNames[z]) == null ? "" : Byte.valueOf(sourceAsMap.get(baseHighWayServiceNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Boolean.class)) {
                        beanFiled[z].set(baseHighWayService, sourceAsMap.get(baseHighWayServiceNames[z]) == null ? "" : Boolean.valueOf(sourceAsMap.get(baseHighWayServiceNames[z]).toString()));
                    } else if (beanFiled[z].getType().equals(Date.class)) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = null;
                        try {
                            date = sdf.parse(sourceAsMap.get(baseHighWayServiceNames[z]) == null ? "" : sourceAsMap.get(baseHighWayServiceNames[z]).toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        beanFiled[z].set(baseHighWayService, date);
                    } else {
                        beanFiled[z].set(baseHighWayService, sourceAsMap.get(baseHighWayServiceNames[z]) == null ? "" : sourceAsMap.get(baseHighWayServiceNames[z]).toString());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            baseHighWayServices.add(baseHighWayService);

        }
    }
    /**
     * @Description: 桥梁查询最新年份的所有数据
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/5/13 0013
     */
    public static List<Basebridgealldata> queryBridgeAll(String index,int start, int row) throws Exception {
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes("aroad").setFrom(start).setSize(row);
        BoolQueryBuilder boolQueryBuilderStart = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchAllQuery())
                .mustNot(QueryBuilders.matchPhraseQuery("jishudengji", "高速"));
        searchRequestBuilder.setQuery(boolQueryBuilderStart).addSort("year", SortOrder.DESC);
        String maxYear = getMaxYear(searchRequestBuilder);
        // 搜索数据
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchAllQuery())
                .must(QueryBuilders.matchPhraseQuery("year",maxYear))
                .mustNot(QueryBuilders.matchPhraseQuery("jishudengji", "高速"));
        searchRequestBuilder.setQuery(boolQueryBuilder);
        String[] bridgeNames = getbridgeNames();
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        List<Basebridgealldata> basebridgealldataList = new ArrayList<>();
        getBaseBridgeDataList(bridgeNames, hits, basebridgealldataList);
        return basebridgealldataList;
    }



    /**
     * @Description: 桥梁匹配桥梁名称，桥梁代码查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date:
     */
    public static Map<String, Object> queryBridge(String keyword, String index, int start, int row) throws Exception {
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes("aroad").setFrom(start).setSize(row);
        BoolQueryBuilder boolQueryBuilderStart = QueryBuilders.boolQuery()
                .must(QueryBuilders.multiMatchQuery(keyword, "qiaoliangmingcheng", "qiaoliangdaima"))
                .mustNot(QueryBuilders.matchPhraseQuery("jishudengji", "高速"));
        searchRequestBuilder.setQuery(boolQueryBuilderStart).addSort("year", SortOrder.DESC);
        String maxYear = getMaxYear(searchRequestBuilder);
        // 搜索数据
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.multiMatchQuery(keyword, "qiaoliangmingcheng", "qiaoliangdaima"))
                    .must(QueryBuilders.matchPhraseQuery("year",maxYear))
                    .mustNot(QueryBuilders.matchPhraseQuery("jishudengji", "高速"));
            searchRequestBuilder.setQuery(boolQueryBuilder);
        }
        String[] bridgeNames = getbridgeNames();
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        List<Basebridgealldata> basebridgealldataList = new ArrayList<>();
        getBaseBridgeDataList(bridgeNames, hits, basebridgealldataList);

        return new HashMap<String, Object>() {{
            put("total", hits.getTotalHits());
            put("list", basebridgealldataList);
        }};
    }

    /**
     * @Description: matchPhraseQuery()桥梁代码精确匹配查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/5/10 0010
     */
    public static List<Basebridgealldata> queryBridgeQLDM(String keyword, String index, int start, int row) throws Exception {
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes("aroad").setFrom(start).setSize(row);
        MatchPhraseQueryBuilder qiaoliangdaima = QueryBuilders.matchPhraseQuery("qiaoliangdaima", keyword);
        searchRequestBuilder.setQuery(qiaoliangdaima).addSort("year", SortOrder.DESC);
        String maxYear = getMaxYear(searchRequestBuilder);

        // 搜索数据
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                    .must(qiaoliangdaima)
                    .must(QueryBuilders.matchPhraseQuery("year", maxYear));
            searchRequestBuilder.setQuery(boolQueryBuilder);
        }
        String[] bridgeNames = getbridgeNames();
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        List<Basebridgealldata> basebridgealldataList = new ArrayList<>();
        getBaseBridgeDataList(bridgeNames, hits, basebridgealldataList);
        return basebridgealldataList;
    }



    /**
     * @Description: 路段查询最新年份所有数据过滤掉等级名称为高速和等外
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/5/13 0013
     */
    public static List<Basehighwayalldata> queryHighWayAll(String index,int start, int row) throws Exception {
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes("aroad").setFrom(start).setSize(row);
        BoolQueryBuilder boolQueryBuilderStart = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchAllQuery())
                .mustNot(QueryBuilders.matchPhraseQuery("dengjimingcheng", "高速"))
                .mustNot(QueryBuilders.matchPhraseQuery("dengjimingcheng", "等外"));
        searchRequestBuilder.setQuery(boolQueryBuilderStart).addSort("year", SortOrder.DESC);
        String maxYear = getMaxYear(searchRequestBuilder);

        //搜索数据
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchAllQuery())
                .must(QueryBuilders.matchPhraseQuery("year", maxYear))
                .mustNot(QueryBuilders.matchPhraseQuery("dengjimingcheng", "高速"))
                .mustNot(QueryBuilders.matchPhraseQuery("dengjimingcheng", "等外"));
        searchRequestBuilder.setQuery(boolQueryBuilder);
        String[] highwayNames = getHighWayNames();
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        List<Basehighwayalldata> basehighwayalldataList = new ArrayList<>();
        getBaseHighWayAllDataList(highwayNames, hits, basehighwayalldataList);
        return basehighwayalldataList;
    }

    /**
     * @Description: 路段匹配路线编号，路线名称查询过滤掉等级名称为高速和等外
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date:
     */
    public static Map<String, Object>  queryHighWay(String keyword, String index,int start, int row) throws Exception {

        SearchRequestBuilder searchRequestBuilderStart = getClient().prepareSearch(index).setTypes("aroad").setFrom(start).setSize(row);
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            BoolQueryBuilder boolQueryBuilderStart = QueryBuilders.boolQuery()
                    .must(QueryBuilders.multiMatchQuery(keyword, "luxianbianhao", "luxianmingcheng"))
                    .mustNot(QueryBuilders.matchPhraseQuery("dengjimingcheng", "高速"))
                    .mustNot(QueryBuilders.matchPhraseQuery("dengjimingcheng", "等外"));
            searchRequestBuilderStart.setQuery(boolQueryBuilderStart).addSort("year", SortOrder.DESC);
        }
        String maxYear = getMaxYear(searchRequestBuilderStart);

        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes("aroad").setFrom(start).setSize(row);
        //搜索数据
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.multiMatchQuery(keyword, "luxianbianhao", "luxianmingcheng"))
                    .must(QueryBuilders.matchPhraseQuery("year", maxYear))
                    .mustNot(QueryBuilders.matchPhraseQuery("dengjimingcheng", "高速"))
                    .mustNot(QueryBuilders.matchPhraseQuery("dengjimingcheng", "等外"));
            searchRequestBuilder.setQuery(boolQueryBuilder);

        }
        String[] highwayNames = getHighWayNames();
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        List<Basehighwayalldata> basehighwayalldataList = new ArrayList<>();
        getBaseHighWayAllDataList(highwayNames, hits, basehighwayalldataList);
        //分词查询结果的路段数据按照路线编号groupby,起点桩号aorderby
        List<Basehighwayalldata> basehighwayalldataListGroup = GroupUtils.groupByProperty(basehighwayalldataList);
        return new HashMap<String, Object>() {{
            put("total", hits.getTotalHits());
            put("list", basehighwayalldataListGroup);
        }};

    }
    /**
     * @Description: 通过路线编号匹配，查询字段在起点桩号和止点桩号之间范围查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/5/14 0014
     */
    public static List<Basehighwayalldata> queryHighWayFiler(String lxbh,double zh, String index) throws Exception {

        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes("aroad");
        if (org.apache.commons.lang.StringUtils.isNotBlank(lxbh)&&org.apache.commons.lang.StringUtils.isNotBlank(String.valueOf(zh))) {
            BoolQueryBuilder boolqueryStart = QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchPhraseQuery("luxianbianhao", lxbh))
                    .must(QueryBuilders.rangeQuery("qidianzhuanghaoa").lte(zh))
                    .must(QueryBuilders.rangeQuery("zhidianzhuanghao").gte(zh));
            searchRequestBuilder.setQuery(boolqueryStart).addSort("year", SortOrder.DESC);
        }
        String maxYear = getMaxYear(searchRequestBuilder);
        //搜索数据
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(lxbh)&&org.apache.commons.lang.StringUtils.isNotBlank(String.valueOf(zh))) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            BoolQueryBuilder boolquery = QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchPhraseQuery("luxianbianhao", lxbh))
                    .must(QueryBuilders.rangeQuery("qidianzhuanghaoa").lte(zh))
                    .must(QueryBuilders.rangeQuery("zhidianzhuanghao").gte(zh))
                    .must(QueryBuilders.matchPhraseQuery("year",maxYear));
            searchRequestBuilder.setQuery(boolquery);
        }
        String[] highwayNames = getHighWayNames();
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        List<Basehighwayalldata> basehighwayalldataList = new ArrayList<>();
        getBaseHighWayAllDataList(highwayNames, hits, basehighwayalldataList);
        return basehighwayalldataList;
    }
    /**
     * @Description: matchPhraseQuery()路线编号精确匹配查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/5/10 0010
     */
    public static List<Basehighwayalldata> queryHighWayLXBH(String keyword, String index,int start, int row) throws Exception {
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes("aroad").setFrom(start).setSize(row);
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            MatchPhraseQueryBuilder luxianbianhao = QueryBuilders.matchPhraseQuery("luxianbianhao", keyword);
            searchRequestBuilder.setQuery(luxianbianhao).addSort("year", SortOrder.DESC);
        }
        String maxYear = getMaxYear(searchRequestBuilder);
        //搜索数据
        //SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes(type).setFrom(start).setSize(row);
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchPhraseQuery("luxianbianhao", keyword))
                    .must(QueryBuilders.matchPhraseQuery("year", maxYear));
            searchRequestBuilder.setQuery(boolQueryBuilder);
        }
        String[] highwayNames = getHighWayNames();
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        List<Basehighwayalldata> basehighwayalldataList = new ArrayList<>();
        getBaseHighWayAllDataList(highwayNames, hits, basehighwayalldataList);
        return basehighwayalldataList;
    }


    /**
     * @Description: 隧道查询最大年份所有数据
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/5/13 0013
     */
    public static List<Basetunnelalldata> queryTunnelAll(String index, int start, int row) throws Exception {
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes("aroad").setFrom(start).setSize(row);
        BoolQueryBuilder boolQueryBuilderStart = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchAllQuery())
                .mustNot(QueryBuilders.matchPhraseQuery("jishudengji", "高速"));
        searchRequestBuilder.setQuery(boolQueryBuilderStart).addSort("year", SortOrder.DESC);

        String maxYear = getMaxYear(searchRequestBuilder);
        //搜索数据
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchAllQuery())
                .must(QueryBuilders.matchPhraseQuery("year",maxYear))
                .mustNot(QueryBuilders.matchPhraseQuery("jishudengji", "高速"));
        searchRequestBuilder.setQuery(boolQueryBuilder);
        String[] basicTunnelNames = getBasicTunnelNames();
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        List<Basetunnelalldata> basetunnelalldataList = new ArrayList<>();
        getBaseTunnelAllDataList(basicTunnelNames, hits, basetunnelalldataList);
        return basetunnelalldataList;
    }

    /**
     * @Description: 隧道匹配隧道名称查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/4/28 0028
     */
    public static HashMap<String, Object> queryTunnel(String keyword, String index,int start, int row) throws Exception {
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes("aroad").setFrom(start).setSize(row);
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            MatchQueryBuilder suidaomingcheng = QueryBuilders.matchQuery("suidaomingcheng", keyword);
            searchRequestBuilder.setQuery(suidaomingcheng).addSort("year", SortOrder.DESC);
        }
        String maxYear = getMaxYear(searchRequestBuilder);
        //搜索数据
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            BoolQueryBuilder boolquery = QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchQuery("suidaomingcheng", keyword))
                    .must(QueryBuilders.matchPhraseQuery("year", maxYear));
            searchRequestBuilder.setQuery(boolquery);
        }
        String[] basicTunnelNames = getBasicTunnelNames();
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        List<Basetunnelalldata> basetunnelalldataList = new ArrayList<>();
        getBaseTunnelAllDataList(basicTunnelNames, hits, basetunnelalldataList);
        return new HashMap<String, Object>() {{
            put("total", hits.getTotalHits());
            put("list", basetunnelalldataList);
        }};
    }


    /**
     * @Description: 收费站查询最大年份所有数据
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/5/13 0013
     */
    public static List<BaseRoadToll_String> queryBaseRoadTollStringAll(String index,int start, int row) throws Exception {
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes("aroad").setFrom(start).setSize(row);
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        searchRequestBuilder.setQuery(matchAllQueryBuilder).addSort("year", SortOrder.DESC);

        String maxYear = getMaxYear(searchRequestBuilder);
        //搜索数据
        BoolQueryBuilder boolquery = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchAllQuery())
                .must(QueryBuilders.matchPhraseQuery("year", maxYear));
        searchRequestBuilder.setQuery(boolquery);
        String[] baseRoadTollNames = getBaseRoadTollNames();
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        ArrayList<BaseRoadToll_String> baseRoadTollStrings = new ArrayList<>();
        getBaseRoadTollStrings(baseRoadTollNames, hits, baseRoadTollStrings);
        return baseRoadTollStrings;
    }

    /**
     * @Description: 收费站匹配名称查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/4/28 0028
     */
    public static HashMap<String, Object> queryBaseRoadTollString(String keyword, String index,int start, int row) throws Exception {
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes("aroad").setFrom(start).setSize(row);
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", keyword);
            searchRequestBuilder.setQuery(matchQuery).addSort("year", SortOrder.DESC);
        }
        String maxYear = getMaxYear(searchRequestBuilder);
        //搜索数据
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            //multiMatchQuery多个字段匹配某一个值
            //multiMatchQuery(text,fields)其中的fields是字段的名字，可以写好几个，每一个中间用逗号分隔
            BoolQueryBuilder boolquery = QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchQuery("name", keyword))
                    .must(QueryBuilders.matchPhraseQuery("year", maxYear));
            searchRequestBuilder.setQuery(boolquery);
        }
        String[] baseRoadTollNames = getBaseRoadTollNames();
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        ArrayList<BaseRoadToll_String> baseRoadTollStrings = new ArrayList<>();
        getBaseRoadTollStrings(baseRoadTollNames, hits, baseRoadTollStrings);
        return new HashMap<String, Object>() {{
            put("total", hits.getTotalHits());
            put("list", baseRoadTollStrings);
        }};
    }

    /**
     * @Description: 查询服务区最大年份的所有数据
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/5/13 0013
     */
    public static List<BaseHighWayService> queryBaseHighWayServiceAll(String index,int start, int row) throws Exception {
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes("aroad").setFrom(start).setSize(row);
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        searchRequestBuilder.setQuery(matchAllQueryBuilder).addSort("year", SortOrder.DESC);

        String maxYear = getMaxYear(searchRequestBuilder);
        //搜索数据
        BoolQueryBuilder boolquery = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchAllQuery())
                .must(QueryBuilders.matchPhraseQuery("year", maxYear));
        searchRequestBuilder.setQuery(boolquery);
        String[] baseHighWayServiceNames = getBaseHighWayServiceNames();
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        List<BaseHighWayService> baseHighWayServices = new ArrayList<>();
        getBaseHighWayServices(baseHighWayServiceNames, hits, baseHighWayServices);
        return baseHighWayServices;
    }

    /**
     * @Description: 服务区匹配服务设施名称查询
     * @Param:
     * @return:
     * @Author: dongjili
     * @Date: 2019/5/8 0008
     */
    public static HashMap<String, Object> queryBaseHighWayService(String keyword, String index,int start, int row) throws Exception {
        SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(index).setTypes("aroad").setFrom(start).setSize(row);
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            MatchQueryBuilder fwssmc = QueryBuilders.matchQuery("fwssmc", keyword);
            searchRequestBuilder.setQuery(fwssmc).addSort("year", SortOrder.DESC);
        }
        String maxYear = getMaxYear(searchRequestBuilder);
        //搜索数据
        //isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length> 0(去掉字符串两端的多余的空格的长度）
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
            BoolQueryBuilder boolquery = QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchQuery("fwssmc", keyword))
                    .must(QueryBuilders.matchPhraseQuery("year", maxYear));
            searchRequestBuilder.setQuery(boolquery);
        }
        String[] baseHighWayServiceNames = getBaseHighWayServiceNames();
        SearchResponse response = searchRequestBuilder.get();
        //获取命中次数，查询结果有多少对象
        SearchHits hits = response.getHits();
        List<BaseHighWayService> baseHighWayServices = new ArrayList<>();
        getBaseHighWayServices(baseHighWayServiceNames, hits, baseHighWayServices);
        return new HashMap<String, Object>() {{
            put("total", hits.getTotalHits());
            put("list", baseHighWayServices);
        }};
    }

}