package shop.alien.api.controller;

import shop.alien.api.feign.GaoDeFeign;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import shop.alien.entity.result.R;

/**
 * Api_高德
 *
 * @author ssk
 * @version 1.0
 * @date 2024/10/24 10:23
 */
@Api(tags = {"Api_高德"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/gaode")
@RequiredArgsConstructor
public class GaoDeController {

    private final GaoDeFeign gaoDeFeign;

    @Value("${api.gaode.web}")
    private String gaoDeKey;

    /**
     * 高德-地理编码
     * 每日配额5000,并发每秒30
     * https://lbs.amap.com/api/webservice/guide/api/georegeo
     *
     * @param address 结构化地址信息, 规则遵循：国家、省份、城市、区县、城镇、乡村、街道、门牌号码、屋邨、大厦，如：北京市朝阳区阜通东大街6号。
     * @return R<Object>
     */
    @ApiOperation("地理编码")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({@ApiImplicitParam(name = "address", value = "结构化地址信息", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/geo")
    public R<Object> geo(String address) {
        String output = "JSON";
        return R.data(gaoDeFeign.geo(output, address, gaoDeKey));
    }

    /**
     * 高德-逆地理编码
     * 每日配额5000,并发每秒30
     * https://lbs.amap.com/api/webservice/guide/api/georegeo
     *
     * @param longitude 经度
     * @param latitude  纬度
     * @return R<Object>
     */
    @ApiOperation(value = "逆地理编码", notes = "每日配额5000,并发每秒30, 可用经纬度: longitude=121.573163&latitude=39.006614")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "longitude", value = "经度", dataType = "String", paramType = "query", required = true), @ApiImplicitParam(name = "latitude", value = "纬度", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/regeo")
    public R<Object> regeo(String longitude, String latitude) {
        String output = "JSON";
        String location = longitude + "," + latitude;
        String radius = "1000";
        String extensions = "all";
        return R.data(gaoDeFeign.reGeo(output, location, gaoDeKey, radius, extensions));
    }

    /**
     * 高德-天气查询
     * 每日配额300000,并发每秒200
     * https://lbs.amap.com/api/webservice/guide/api/weatherinfo
     *
     * @param areaCode   区域code
     * @param extensions 查询类型(base:查询当天,all:查询当天加未来)
     * @return R<Object>
     */
    @ApiOperation(value = "天气查询", notes = "每日配额300000,并发每秒200, 可用参数: areaCode=210200&extensions=base")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "areaCode", value = "区域code", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "extensions", value = "查询类型(base:查询当天,all:查询当天加未来)", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/weatherInfo")
    public R<Object> weatherInfo(String areaCode, String extensions) {
        String output = "JSON";
        JSONObject jsonObject = gaoDeFeign.weatherInfo(output, gaoDeKey, areaCode, extensions);
        if ("all".equals(extensions)) {
            //预报天气信息数据
            JSONArray forecastsArray = jsonObject.getJSONArray("forecasts");
            return R.data(forecastsArray.getJSONObject(0));
        } else {
            //实况天气数据信息
            JSONArray lives = jsonObject.getJSONArray("lives");
            if (lives != null && !lives.isEmpty()) {
                return R.data(lives.getJSONObject(0));
            }
            return null;
        }
    }

    /**
     * 高德-IP定位
     * 每日配额5000,并发每秒3
     * https://lbs.amap.com/api/webservice/guide/api/ipconfig
     *
     * @param ip ip地址, 需要搜索的 IP 地址（仅支持国内）若用户不填写 IP，则取客户 http 之中的请求来进行定位
     * @return R<Object>
     */
    @ApiOperation("IP定位")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(name = "ip", value = "ip地址", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/ip")
    public R<Object> ip(String ip) {
        return R.data(gaoDeFeign.ip(ip, gaoDeKey));
    }
}
