package com.ruoyi.ibs.grid.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.ibs.grid.domain.vo.AddressResultVO;
import com.ruoyi.ibs.grid.service.AddressAnalyseService;
import com.ruoyi.ibs.grid.service.BaiduApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "地址解析接口")
@RestController
@RequestMapping("/address/analyse")
public class AddressAnalyseController extends BaseController {
    @Resource
    private AddressAnalyseService adminDivisionService;

    @Resource
    private BaiduApiService baiduApiService;


    @ApiOperation("点入围栏")
    @GetMapping("/pointInGeometry")
    public AddressResultVO pointInGeometry(double lng, double lat) {
        return adminDivisionService.pointInGeometry(lng, lat);
    }

    @ApiOperation("地址标准化")
    @GetMapping("/addressStandardize")
    public AddressResultVO addressStandardize(String address) {
        return adminDivisionService.addressStandardize(address);
    }

    @ApiOperation("百度地解码接口测试 L7KaAZUYPVSD40nYT09rWWgIdZKUesiX")
    @GetMapping("/geocoding")
    public AjaxResult geocoding(@RequestParam String address, @RequestParam String ak) {
        return AjaxResult.success(baiduApiService.geocoding(address));
    }



    @ApiOperation("百度地点检索")
    @GetMapping("/placeSearch")
    @Log(title = "百度地点检索")
    public AjaxResult placeSearch(@RequestParam String region, @RequestParam String address) {
        JsonNode jsonNode = baiduApiService.placeSearch(region, address);
        if(jsonNode.get("status").equals("2")){
            throw new ServiceException("请求参数非法");
        }else if(jsonNode.get("status").equals("4")){
            throw new ServiceException("服务当日调用次数已达上线");
        }else if (jsonNode.get("status").equals("5")){
            throw new ServiceException("ak不存在或非法");
        }else if (jsonNode.get("status").equals("8")){
            throw new ServiceException("数据解析失败，参数中包含特殊符号");
        }
        return AjaxResult.success(jsonNode.get("results"));
    }


    @ApiOperation("地址标准化(包裹)")
    @GetMapping("/addressStandardize1")
    public AjaxResult addressStandardize1(String address) {
        return AjaxResult.success(adminDivisionService.addressStandardize(address));
    }
}
