package cn.qinys.platform.upms.api.controller;

import cn.qinys.platform.base.response.Result;
import cn.qinys.platform.base.security.annotation.HasPermission;
import cn.qinys.platform.base.utils.excel.ExcelElement;
import cn.qinys.platform.base.utils.excel.ExcelUtils;
import cn.qinys.platform.upms.api.req.GaoDeGetAddressReq;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.qinys.platform.upms.api.req.GaoDeKeyPageReq;
import cn.qinys.platform.upms.api.resp.GaoDeKeyPageResp;
import cn.qinys.platform.upms.api.service.IGaoDeKeyService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *
 * @author tanlie
 * Date: 2024/4/26 17:21
 */
@AllArgsConstructor
@RestController
@RequestMapping("/upms/gaodekey")
public class GaoDeKeyController extends DevBaseController {

    private final IGaoDeKeyService gaoDeKeyService;


    /**
     * 网关日志分页
     *
     * @return
     */
    @HasPermission({"dev.gaodekey"})
    @GetMapping("/page")
    public Result<Page<GaoDeKeyPageResp>> queryDictionaries(GaoDeKeyPageReq req) {
        Page<GaoDeKeyPageResp> respPage = gaoDeKeyService.getPage(req);
        return new Result<>(respPage);
    }


    /**
     * 操作日志导出
     *
     * @param
     * @param response
     */
    @HasPermission({"dev.gaodekey"})
    @PostMapping("/download")
    public void userExport(@RequestBody(required = false) GaoDeKeyPageReq query, HttpServletResponse response) {
        query = query == null ? new GaoDeKeyPageReq() : query;
        query.setCurrent(1);
        query.setSize(5000);
        Page<GaoDeKeyPageResp> voPage = gaoDeKeyService.getPage(query);
        List<ExcelElement> excelElements = new ArrayList<>();
        excelElements.add(new ExcelElement("key", "geoKey"));
        excelElements.add(new ExcelElement("日期", "date"));
        excelElements.add(new ExcelElement("使用量", "useCount"));
        excelElements.add(new ExcelElement("备注", "remark"));
        excelElements.add(new ExcelElement("开始时间", "createTime"));
        excelElements.add(new ExcelElement("结束时间", "updateTime"));
        ExcelUtils.normalExport("高德key使用量统计", voPage.getRecords(), excelElements, response);
    }


    /**
     * 根据经纬度获取地址
     *
     * @return
     */
    @PostMapping("/address")
    public Result<String> getAddressByAltitudeAndLongitude(@RequestBody @Valid GaoDeGetAddressReq req) {
        String address = gaoDeKeyService.getAddress(req);
        return new Result<>(address);
    }

    @PostMapping("/info")
    public JSONObject getInfo(@RequestBody @Valid GaoDeGetAddressReq req) {
        return gaoDeKeyService.getGaoDeResult("", req.getLongitude(), req.getLatitude(), 10);
    }


}
