package cn.ysk.cashier.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Api(tags = "通用组件")
@RequestMapping("/api")
public class CommonController {

    @GetMapping("geocode")
    @ApiOperation("通过经纬度查询位置信息")
    public ResponseEntity<Object> getGeocode(String location){
        return new ResponseEntity<>(geocode(location), HttpStatus.OK);
    }

    public JsonNode geocode(String location) {
        RestTemplate restTemplate = new RestTemplate();
        String s = restTemplate.getForObject("https://restapi.amap.com/v3/geocode/regeo?key=7a7f2e4790ea222660a027352ee3af39&location="+location, String.class);
        ObjectMapper objectMapper = new ObjectMapper();
        // 将 JSON 字符串解析为 JsonNode 对象
        try {
            JsonNode jsonNode = objectMapper.readTree(s).get("regeocode");
            return jsonNode;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
