package com.ruoyi.app.utils;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.PosFreight;
import com.ruoyi.system.domain.UserFootprint;
import com.ruoyi.system.mapper.PosFreightMapper;
import com.ruoyi.system.service.IPosFreightService;
import com.ruoyi.system.utils.AddPush;
import com.ruoyi.system.utils.GetArea;
import com.ruoyi.web.controller.common.CommonController;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping("/utils")
public class SysFileInfoController extends BaseController {
    @Autowired
    private IPosFreightService posFreightService;
    @Autowired
    private PosFreightMapper posFreightMapper;
    private static final Logger log = LoggerFactory.getLogger(SysFileInfoController.class);

    //上传
    @Anonymous
    @PostMapping("/Upload")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file) throws IOException {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        return success(MessageUtils.message("no.upload.success"), fileName);
    }

    //返回附近地址
    @Anonymous
    @GetMapping("/getLocationAddr")
    public AjaxResult getLocationAddr(@RequestParam String latlng,
                                      @RequestParam Integer type,
                                      @RequestParam(defaultValue = "") String id) {
        Jedis jedis = new Jedis("localhost", 6379);
        GetArea area = new GetArea();
        String lang = jedis.get(id) == null ? "vi" : jedis.get(id);
        JSONObject reg;
        if (type == 0) {
            reg = area.getarearess(latlng, lang);
        } else {
            reg = area.getgoogle(latlng, lang);
        }
        return success(reg);
    }

    //地址逆向
    @Anonymous
    @GetMapping("/getAddr")
    public AjaxResult getAddr(@RequestParam String latlng,
                              @RequestParam(defaultValue = "") String id) {
        Jedis jedis = new Jedis("localhost", 6379);
        String lang = jedis.get(id) == null ? "zh-TW" : jedis.get(id);
        GetArea area = new GetArea();
        JSONObject reg = area.getgoogleadd(latlng, lang);
        return success(reg);
    }

    //坐标转换
    @Anonymous
    @GetMapping("/setLocat")
    public AjaxResult setLocat(@RequestParam String latlng,
                               @RequestParam Integer from,
                               @RequestParam Integer to) {
        GetArea area = new GetArea();
        JSONObject reg = area.setlatlng(latlng, from, to);
        return success(reg);
    }

    //坐标转换
    @Anonymous
    @GetMapping("/GCJ2TOWGS")
    public AjaxResult GCJ2TOWGS(@RequestParam Double lng,
                                @RequestParam Double lat) {
        GetArea area = new GetArea();
        String reg = area.convertGCJ2WGS(lng, lat);
        return success(MessageUtils.message("no.convert.success"), reg);
    }

    //获取短信
    @Anonymous
    @GetMapping("/addpush")
    public AjaxResult addpush(@RequestParam String cid, @RequestParam String title, @RequestParam String content) {
        AddPush push = new AddPush();
        Boolean reg = push.gopush(cid, title, content);
        if (reg) {
            return success(MessageUtils.message("no.send.success"));
        } else {
            return error(MessageUtils.message("no.send.fail"));
        }
    }

    //计算运费----直线
    @Anonymous
    @GetMapping("/getyunfei")
    public AjaxResult getyunfei(@RequestParam double lat1,
                                @RequestParam double lng1,
                                @RequestParam double lat2,
                                @RequestParam double lng2) {
        List<PosFreight> freights = posFreightMapper.getyunjia();
        Long yunjia;
        Long qyjvli;
        Long qyjiage;
        if (freights.size() == 0) {
            List<PosFreight> hts = posFreightMapper.getmiuyunjia();
            yunjia = hts.get(0).getFreight() / hts.get(0).getDistance();
            qyjvli = hts.get(0).getStartingDistance();
            qyjiage = hts.get(0).getStartingFare();
        } else {
            yunjia = freights.get(0).getFreight() / freights.get(0).getDistance();
            qyjvli = freights.get(0).getStartingDistance();
            qyjiage = freights.get(0).getStartingFare();
        }
        GetJvli jvli = new GetJvli();
        Double psjvli = jvli.getDistance(lat1, lng1, lat2, lng2);
        DecimalFormat df = new DecimalFormat("#");
        DecimalFormat jl = new DecimalFormat("#.0");
        Double jsjl = psjvli <= qyjvli ? 0 : psjvli - qyjvli;
        System.out.println("==========" + jsjl);
        String yunf = df.format(yunjia * (jsjl < 0.5 ? 0 : jsjl >= 0.5 && jsjl < 1 ? 1 : jsjl));
        Long sjyunf = Long.parseLong(yunf) + qyjiage;
        BigDecimal bb = BigDecimal.valueOf(sjyunf);
        BigDecimal Res = sjyunf < 1000 ? bb : bb.setScale(-3, BigDecimal.ROUND_HALF_UP);
        JSONObject org = new JSONObject();
        org.put("jvli", Double.valueOf(jl.format(psjvli)));
        org.put("freight", Res);
        org.put("yunf", sjyunf);
        return success(MessageUtils.message("no.obtained.permission"), org);
    }

    //计算运费
    @Anonymous
    @GetMapping("/getZuiyyfei")
    public AjaxResult getZuiyyfei(@RequestParam String quli) {
        List<PosFreight> freights = posFreightMapper.getyunjia();
        Long yunjia;
        Long qyjvli;
        Long qyjiage;
        if (freights.size() == 0) {
            List<PosFreight> hts = posFreightMapper.getmiuyunjia();
            yunjia = hts.get(0).getFreight() / hts.get(0).getDistance();
            qyjvli = hts.get(0).getStartingDistance();
            qyjiage = hts.get(0).getStartingFare();
        } else {
            yunjia = freights.get(0).getFreight() / freights.get(0).getDistance();
            qyjvli = freights.get(0).getStartingDistance();
            qyjiage = freights.get(0).getStartingFare();
        }
        Double psjvli = Double.valueOf(quli);
        DecimalFormat df = new DecimalFormat("#");
        DecimalFormat jl = new DecimalFormat("#.0");
        Double jsjl = psjvli <= qyjvli ? 0 : psjvli - qyjvli;
        System.out.println("==========" + jsjl);
        String yunf = df.format(yunjia * (jsjl < 0.5 ? 0 : jsjl >= 0.5 && jsjl < 1 ? 1 : jsjl));
        Long sjyunf = Long.parseLong(yunf) + qyjiage;
        BigDecimal bb = BigDecimal.valueOf(sjyunf);
        BigDecimal Res = sjyunf < 1000 ? bb : bb.setScale(-3, BigDecimal.ROUND_HALF_UP);
        JSONObject org = new JSONObject();
        org.put("jvli", Double.valueOf(jl.format(psjvli)));
        org.put("freight", Res);
        org.put("yunf", sjyunf);
        return success(MessageUtils.message("no.obtained.permission"), org);
    }

    //计算导航路径
    @Anonymous
    @GetMapping("/getluxianjvli")
    public AjaxResult getluxianjvli(@RequestParam String latlng1,
                                    @RequestParam String latlng2,
                                    @RequestParam(defaultValue = "") String id) {
        log.info("谷歌地图：开始处理路径计算请求 -> ID: {}, 坐标1: {}, 坐标2: {}", id, latlng1, latlng2);
        try(Jedis jedis=new Jedis("localhost", 6379);) {
            //Jedis jedis = new Jedis("localhost", 6379);
            String lang = jedis.get(id) == null ? "vi" : jedis.get(id);
            GetArea area = new GetArea();
            JSONObject reg = area.getluxian(latlng1, latlng2, lang);
            log.info("谷歌地图：返回---" + reg.toString());
            return success(reg.getJSONArray("routes"));
        } catch (Exception e) {
            // 异常捕获
            log.error("谷歌地图：处理路径计算请求异常 -> ID: {}, 错误信息: {}", id, e.getMessage(), e);
            return error(MessageUtils.message("no.map.exception"));
        }

    }

    /**
     * Mapbox 计算导航路径
     * @param lnglat1
     * @param lnglat2
     * @param id
     * @return
     */
    @Anonymous
    @GetMapping("/getluxianjvliMp")
    public AjaxResult getluxianjvliMp(@RequestParam String lnglat1,
                                    @RequestParam String lnglat2,
                                    @RequestParam(defaultValue = "") String id) {
        log.info("Mapbox地图：开始处理路径计算请求 -> ID: {}, 坐标1: {}, 坐标2: {}", id, lnglat1, lnglat2);
        try(Jedis jedis=new Jedis("localhost", 6379);) {
            //Jedis jedis = new Jedis("localhost", 6379);
            String lang = jedis.get(id) == null ? "vi" : jedis.get(id);
            GetArea area = new GetArea();
            JSONObject reg = area.getmpluxian(lnglat1, lnglat2);
            log.info("Mapbox地图：返回---" + reg.toString());
            return success(reg.getJSONArray("routes"));
        } catch (Exception e) {
            // 异常捕获
            log.error("Mapbox地图：处理路径计算请求异常 -> ID: {}, 错误信息: {}", id, e.getMessage(), e);
            return error(MessageUtils.message("no.map.exception"));
        }

    }
}
