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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.roadcondition.entity.RoadConditionEvent;
import com.elephant.framework.galaxy.aroad.module.roadcondition.service.RoadConditionEventService;
import com.elephant.framework.galaxy.aroad.module.szgl.entity.Line;
import com.elephant.framework.galaxy.aroad.module.szgl.service.SzglService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
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 {

    @Value("${shape.identify-url}")
    private final  String identify = "";
    private final BaseindexdataService baseindexdataService;
    private final RoadConditionEventService eventService;
    private final SzglService szglService;

    /**
     * 全局搜索路段、收费站、桥梁、隧道
     *
     * @Param keyword 搜索条件
     */
    @GetMapping("/bigdata")
    public R getBigData(String type,String keyword, Page page) throws Exception {
        List<Map<String, Object>> responseList = new ArrayList<>();
        long total = 0;
        switch (type) {
            case "路线":
                Baseindexdata lx = baseindexdataService.selectLatesByTableName("basehighwayalldata");
                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");
                break;
            case "桥梁":
                Baseindexdata ql = baseindexdataService.selectLatesByTableName("basebridgealldata");
                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");
                break;
            case "隧道":
                Baseindexdata sd = baseindexdataService.selectLatesByTableName("basetunnelalldata");
                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");
                break;
        }
        //        // 收费站
//        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 {
        System.out.println(identify);
        String uri = "http://47.105.99.44:8080/sdlkapi/LRS/IdentifyM?longitude=";
        uri += 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(),"basehighwayalldata", road.getIndexType());
            if (lines != null && lines.size() > 0) {
                lx = lines.get(0);
            }

        }
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(lx != null){
            resultMap.put("name", lx.getLuxianmingcheng());
            resultMap.put("code", lx.getLuxianbianhao());
            resultMap.put("iden", "luxian");
            resultMap.put("idenVal", "路线");
            resultMap.put("data", lx);
            resultMap.put("total", 1);
        }
        return new R(resultMap);
    }
    /**
     * 根据路线编号、中心桩号 查询路况事件
     * @param lxbh
     * @param zxzh
     * @return
     */
    @GetMapping("/getLksjForQS/{lxbh}/{zxzh}")
    public R getLksjForQS(@PathVariable String lxbh, @PathVariable String zxzh){
        return  new R(eventService.getLksjForQS(lxbh,Double.valueOf(zxzh)));
    }

    /**
     * 根据路线编号、起点桩号、止点桩号查询建养历史a
     * @param lxbh
     * @param qdzh
     * @param zdzh
     * @return
     */
    @GetMapping("/getJYlsForRoad/{lxbh}/{qdzh}/{zdzh}")
    public R getJYlsForRoad(@PathVariable String lxbh, @PathVariable String qdzh,@PathVariable String zdzh){
        return  new R(szglService.getJYlsForRoad(lxbh,Double.valueOf(qdzh),Double.valueOf(qdzh)));
    }
    /**
     * 根据路线编号、中心桩号 查询路况事件
     * @param lxbh
     * @param zxzh
     * @return
     */
    @GetMapping("/getJYlsForQS/{lxbh}/{zxzh}")
    public R getJYlsForQS(@PathVariable String lxbh, @PathVariable String zxzh){
        return  new R(szglService.getJYlsForQS(lxbh,Double.valueOf(zxzh)));
    }

    /**
     * 根据路线起点桩号与止点桩号查询路况事件
     */
    @GetMapping("/qzzhevent")
    public R getQzZhEvent(@RequestParam("roadCode") String roadCode, @RequestParam("startM") String startM, @RequestParam("endM") String endM){
        return new R<>(eventService.getQzdzhEvent(startM,endM,roadCode));
    }
    /**
     * 查询桥梁全部数据
     */
    @GetMapping("/qlall")
    public R getQlAll() throws Exception {
        return new R<>(EsUtil_fanshe.queryBridgeAll("basebridgealldata","2018",0,20000));
    }
}