package org.jeecg.sy.java;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.sy.temp.analysis$;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.MalformedURLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/sy")
public class Controller {

    //Redis 模板
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    // 爬虫
    @RequestMapping("/get")
    public Result test1() throws MalformedURLException {
        return Result.ok(analysis$.MODULE$.analysis2(getData.getDatas("")));
    }

    //分析数据
    @RequestMapping("/analysis")
    public Result test2() throws MalformedURLException {
        // 判断是否存在缓存，key为今天的年月日
        String key = new SimpleDateFormat("yyyyMMdd").format(new Date());
        if (redisTemplate.hasKey(key)) {
            return Result.ok(redisTemplate.opsForValue().get(key));
        }
        List<weather_1> list = analysis$.MODULE$.analysis2(getWeathers());
        // 三个结果
        JSONObject result = new JSONObject();
        result.put("temperature", getTemperature(list));
        result.put("wind", getWind(list));
        result.put("temperatureMap", getTemperatureMap(list));
        // 将结果放入缓存
        redisTemplate.opsForValue().set(key, result);
        return Result.ok(result);
    }


    // 预测未来一周的天气情况和最高温度
    @RequestMapping("/fe")
    public Result test3() throws MalformedURLException {
        List<weather_2> list = analysis$.MODULE$.analysis3(getData.getDatas2());
        // 三个结果
        JSONObject result = new JSONObject();
        result.put("data", list);
        return Result.ok(result);
    }

    @NotNull
    private static List<weather_1> getWeathers() throws MalformedURLException {
        // 获取一年的数据
        String times = "202204,202205,202206,202207,202208,202209,202210,202211,202212,202301,202302,202303,202304,202305,202306,202307,202308,202309,202310,202311,202312,202401,202402,202403,";
        List<weather_1> list = new ArrayList<>();
        String time[] = times.split(",");
        for (int i = time.length - 1; i >= 0; i--) {
            list.addAll(getData.getDatas(time[i]));
        }
        return list;
    }


    // 获取列表中的 最高气温和最低气温的变化曲线（两条线）
    public static JSONObject getTemperature(List<weather_1> list) {
        JSONObject result = new JSONObject();
        // 最高气温
        JSONArray highTemperature = new JSONArray();
        // 最低气温
        JSONArray lowTemperature = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            JSONObject temp1 = new JSONObject();
            // 格式为 date + temperature
            //为data加上年份2024，然后转为日期模式
            String data = list.get(i).getData();

            temp1.put("date", data);
            temp1.put("temperature", list.get(i).getHighTemperature().replaceAll("℃", ""));
            highTemperature.add(temp1);
            JSONObject temp2 = new JSONObject();
            temp2.put("date", data);
            temp2.put("temperature", list.get(i).getLowTemperature().replaceAll("℃", ""));
            lowTemperature.add(temp2);
        }
        // 最高气温
        result.put("highTemperature", highTemperature);
        // 最低气温
        result.put("lowTemperature", lowTemperature);
        return result;
    }

    // 获取列表中 风力+风向这对字符串的出现次数（并排序）
    public static JSONObject getWind(List<weather_1> list) {
        JSONObject result = new JSONObject();
        // 风力+风向
        HashMap<String, Integer> wind = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            String windSpeed = list.get(i).getWindSpeed();
            if (wind.containsKey(windSpeed)) {
                wind.put(windSpeed, wind.get(windSpeed) + 1);
            } else {
                wind.put(windSpeed, 1);
            }
        }
        // 对wind进行排序
        List<Map.Entry<String, Integer>> list1 = new ArrayList<>(wind.entrySet());
        list1.sort(Map.Entry.comparingByValue());
        // 塞入JSONArray
        JSONArray winds = new JSONArray();
        JSONArray counts = new JSONArray();
        for (Map.Entry<String, Integer> entry : list1) {
            winds.add(entry.getKey());
            counts.add(entry.getValue());
        }
        result.put("wind", winds);
        result.put("count", counts);
        return result;
    }

    // 天气类型出现次数饼图
    public static JSONArray getTemperatureMap(List<weather_1> list) {
        JSONArray temperatureMapArray = new JSONArray();
        HashMap<String, Integer> wind = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            String windSpeed = list.get(i).getDayOrNight().split("/")[0];
            if (wind.containsKey(windSpeed)) {
                wind.put(windSpeed, wind.get(windSpeed) + 1);
            } else {
                wind.put(windSpeed, 1);
            }
        }
        // 对wind进行排序
        List<Map.Entry<String, Integer>> list1 = new ArrayList<>(wind.entrySet());
        list1.sort(Map.Entry.comparingByValue());
        // 塞入JSONArray
        for (Map.Entry<String, Integer> entry : list1) {
            JSONObject data = new JSONObject();
            data.put("name", entry.getKey());
            data.put("value", entry.getValue());
            temperatureMapArray.add(data);
        }

        return temperatureMapArray;
    }
}
