package com.elephant.framework.galaxy.aroad.module.szgl.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.elephant.framework.galaxy.aroad.bigdata.*;
import com.elephant.framework.galaxy.aroad.module.codegen.response.R;
import com.elephant.framework.galaxy.aroad.module.indexES.entity.Baseindexdata;
import com.elephant.framework.galaxy.aroad.module.indexES.service.BaseindexdataService;
import com.elephant.framework.galaxy.aroad.module.szgl.entity.Line;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/szgl")
@AllArgsConstructor

public class SzglController {

    private final BaseindexdataService baseindexdataService;


    /**
     * 全局搜索路段、收费站、桥梁、隧道
     *
     * @Param keyword 搜索条件
     */
    @GetMapping("/bigdata")
    public R getBigData(String keyword, Page page) throws Exception {
        List<Map<String, Object>> responseList = new ArrayList<>();
        Baseindexdata ql = baseindexdataService.selectLatesByTableName("basebridgealldata");
        Baseindexdata lx = baseindexdataService.selectLatesByTableName("basehighwayalldata");
        Baseindexdata sd = baseindexdataService.selectLatesByTableName("basetunnelalldata");
        long total = 0;
        // 路线

        Map<String, Object> lukehighways = EsUtil_fanshe.queryHighWay(keyword, "basehighwayalldata", lx.getIndexType(), (int) ((page.getCurrent() - 1) * page.getSize()), (int) page.getSize());
        for (Basehighwayalldata basehighwayalldata : (List<Basehighwayalldata>) lukehighways.get("list")) {
            responseList.add(new HashMap<String, Object>() {{
                put("name", basehighwayalldata.getLuxianmingcheng());
                put("code", basehighwayalldata.getLuxianbianhao());
                put("iden", "luxian");
                put("idenVal", "路线");
                put("data", basehighwayalldata);
                put("total", lukehighways.get("total"));
            }});
        }
        total += (long) lukehighways.get("total");
        // 桥梁
        Map<String, Object> luKeBridges = EsUtil_fanshe.queryBridge(keyword, "basebridgealldata", ql.getIndexType(), (int) ((page.getCurrent() - 1) * page.getSize()), (int) page.getSize());
        for (Basebridgealldata lnKeBridge : (List<Basebridgealldata>) luKeBridges.get("list")) {
            responseList.add(new HashMap<String, Object>() {{
                put("name", lnKeBridge.getQiaoliangmingcheng());
                put("iden", "qiaoliang");
                put("idenVal", "桥梁");
                put("data", lnKeBridge);
            }});
        }
        total += (long) luKeBridges.get("total");

        // 隧道
        Map<String, Object> basicTunnels = EsUtil_fanshe.queryTunnel(keyword, "basetunnelalldata", sd.getIndexType(), (int) ((page.getCurrent() - 1) * page.getSize()), (int) page.getSize());
        for (Basetunnelalldata basicTunnel : (List<Basetunnelalldata>) basicTunnels.get("list")) {
            responseList.add(new HashMap<String, Object>() {{
                put("name", basicTunnel.getSuidaomingcheng());
                put("iden", "suidao");
                put("idenVal", "隧道");
                put("data", basicTunnel);
                put("total", basicTunnels.get("total"));
            }});
        }
        total += (long) basicTunnels.get("total");
//        // 收费站
//        Map<String,Object> baseRoadTollStrings = EsUtil_fanshe.queryBaseRoadTollString(keyword, "baseroadtolldata", "2018", (int)((page.getCurrent()-1)*page.getSize()), (int)page.getSize());
//        for(BaseRoadToll_String baseRoadToll_String : (List<BaseRoadToll_String>)baseRoadTollStrings.get("list")){
//            responseList.add(new HashMap<String,Object>(){{
//                put("name",baseRoadToll_String.getShortName());
//                put("iden","shoufeizhan");
//                put("idenVal", "收费站");
//                put("data", baseRoadToll_String);
//                put("total", baseRoadTollStrings.get("total"));
//            }});
//        }
//        total+=(long)baseRoadTollStrings.get("total");
//        // 服务区
//        Map<String,Object> baseHighWayServices = EsUtil_fanshe.queryBaseHighWayService(keyword, "basehighwayservice", "2018", (int)((page.getCurrent()-1)*page.getSize()), (int)page.getSize());
//        for(BaseHighWayService baseHighWayService : (List<BaseHighWayService>)baseHighWayServices.get("list")){
//            responseList.add(new HashMap<String,Object>(){{
//                put("name",baseHighWayService.getFwssmc());
//                put("iden","fuwuqu");
//                put("idenVal", "服务区");
//                put("data", baseHighWayService);
//            }});
//        }
//        total+=(long)baseHighWayServices.get("total");
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("result", responseList);
        responseMap.put("total", total);
        return new R<>(responseMap);
    }
//    /**
//     * 查询桥梁
//     * @Param keyword 搜索条件
//     */
//    @GetMapping("/bridge")
//    public R getBridge(@RequestParam("keyword") String keyword) throws Exception {
//        ArrayList<Basebridgealldata> basebridgealldataList = EsUtil_fanshe.queryBridge(keyword, "basicbridgedata", "2018", 0, 2000000000);
//        return new R<>(basebridgealldataList);
//    }
//    /**
//     * 查询路段
//     * @Param keyword 搜索条件
//     */
//    @GetMapping("/highWay")
//    public R getHighWay(@RequestParam("keyword") String keyword) throws Exception {
//        ArrayList<Basehighwayalldata> basehighwayalldataList = EsUtil_fanshe.queryHighWay(keyword, "basichighwaydata", "2018", 0, 2000000000);
//        return new R<>(basehighwayalldataList);
//    }
//    /**
//     * 查询隧道
//     * @Param keyword 搜索条件
//     */
//    @GetMapping("/tunnel")
//    public R getTunnel(@RequestParam("keyword") String keyword) throws Exception {
//        List<Basetunnelalldata> basetunnelalldataList = EsUtil_fanshe.queryTunnel("广州桥", "basictunneldata", "2018", 0, 2000000000);
//        return new R<>(basetunnelalldataList);
//    }
//    /**
//     * 查询收费站
//     * @Param keyword 搜索条件
//     */
//    @GetMapping("/baseRoadToll")
//    public R getBaseRoadToll(@RequestParam("keyword") String keyword) throws Exception {
//        ArrayList<BaseRoadToll_String> baseRoadTollStrings = EsUtil_fanshe.queryBaseRoadTollString(keyword, "baseroadtolldata", "2018", 0, 2000000000);
//        return new R<>(baseRoadTollStrings);
//    }

    /**
     * 获取普通国省道隧道
     *
     * @return
     */
    @GetMapping("/getSdList")
    public R getSdList(Page page) throws Exception {
        int start = 0;
        int row = 10000000;
        if (page != null) {
            start = (int) ((page.getCurrent() - 1) * page.getSize());
            row = (int) (page.getSize());
        }
        Baseindexdata data = baseindexdataService.selectLatesByTableName("basetunnelalldata");
        List<Basetunnelalldata> sdList = EsUtil_fanshe.queryTunnelAll("basetunnelalldata", data.getIndexType(), start, row);

        return new R(sdList);
    }

    /**
     * 获取普通国省道收费站
     *
     * @return
     */
    @GetMapping("/getFwqList")
    public R getFwqList(Page page) throws Exception {
        int start = 0;
        int row = 10000000;
        if (page != null) {
            start = (int) ((page.getCurrent() - 1) * page.getSize());
            row = (int) (page.getSize());
        }

        Baseindexdata data = baseindexdataService.selectLatesByTableName("basehighwayservice");
        List<BaseHighWayService> fwqList = EsUtil_fanshe.queryBaseHighWayServiceAll("basehighwayservice", data.getIndexType(), start, row);
        return new R(fwqList);
    }

    /**
     * 获取普通国省道收费站
     *
     * @return
     */
    @GetMapping("/getSfzList")
    public R getSfzList(Page page) throws Exception {
        int start = 0;
        int row = 10000000;
        if (page != null) {
            start = (int) ((page.getCurrent() - 1) * page.getSize());
            row = (int) (page.getSize());
        }
        Baseindexdata data = baseindexdataService.selectLatesByTableName("baseroadtolldata");
        List<BaseRoadToll_String> sfzList = EsUtil_fanshe.queryBaseRoadTollStringAll("baseroadtolldata", data.getIndexType(), start, row);
        return new R(sfzList);
    }

    /**
     * 根据经纬度获取项目
     *
     * @param lng
     * @param lat
     * @return
     * @throws Exception
     */
    @GetMapping("/getObjByLngLat/{lng}/{lat}")
    public R getObjByLngLat(@PathVariable String lng, @PathVariable String lat) throws Exception {
        String uri = "http://47.105.99.44:8080/sdlkapi/LRS/IdentifyM?longitude=" + lng + "&latitude=" + lat;
        RestTemplate rest = new RestTemplate();
        ResponseEntity<String> response = rest.getForEntity(uri, String.class);
        String body = response.getBody().trim();
        System.out.println(body);
        Basehighwayalldata lx = null;
        if (!"\"null\"".equals(body)) {
            Baseindexdata road = baseindexdataService.selectLatesByTableName("basehighwayalldata");
            String text = body.substring(1, body.length() - 1);
            String mm = StringEscapeUtils.unescapeJava(text);
            JSONObject object = JSONObject.parseObject(mm);
            Line line = object.toJavaObject(Line.class);
           /* List<Basehighwayalldata> lines = EsUtil_fanshe.queryHighWayFiler(line.getCode(), line.getM(), road.getIndexType());
            if (lines != null && lines.size() > 0) {
                lx = lines.get(0);
            }*/

        }
        return new R(lx);
    }
}