package com.ruoyi.adapter.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.DateTimeUtil;
import com.ruoyi.predict.api.ShareController;
import com.ruoyi.predict.domain.WfFileConfig;
import com.ruoyi.predict.domain.WfNwpData;
import com.ruoyi.predict.service.IWfFileConfigService;
import com.ruoyi.predict.service.IWfNwpDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hlf
 * @date 2022/11/10 9:13
 * 文件说明：图迹数值天气预报数据
 * 风速、风向、温度、湿度、压强
 */
@Api(value = "图迹数值天气预报数据Controller", tags = {"图迹数值天气预报数据操作接口"})
@Controller
@RequestMapping("/adapter/api/tjnwfdc")
public class TuJiNumericalWeatherForecastDataController extends BaseController {

    @Autowired
    private IWfFileConfigService wfFileConfigService;

    @Autowired
    private IWfNwpDataService wfNwpDataService;

    @ApiOperation("数值天气预报")
    @GetMapping("/numericalWeatherForecast")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNumber", value = "场站编号", required = true),
            @ApiImplicitParam(name = "beginDataTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endDataTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "dataTime", value = "时间"),
            @ApiImplicitParam(name = "windSpeed", value = "风速"),
            @ApiImplicitParam(name = "windDir", value = "风向"),
            @ApiImplicitParam(name = "temperature", value = "温度"),
            @ApiImplicitParam(name = "humidity", value = "湿度"),
            @ApiImplicitParam(name = "airPressure", value = "压强"),
    })
    public AjaxResult numericalWeatherForecast(@RequestParam(value = "stationNumber") String stationNumber,
                                               @RequestParam(value = "beginDataTime") String beginDataTime,
                                               @RequestParam(value = "endDataTime") String endDataTime) {
        JSONObject json = new JSONObject();
        //获取所有时间
        List<Date> dateList = DateTimeUtil.getDateList15(DateUtil.parse(beginDataTime), DateUtil.parse(endDataTime));
        LinkedMultiValueMap<String, Double> mapWindSpeed = new LinkedMultiValueMap<String, Double>();
        LinkedMultiValueMap<String, Double> mapWindDir = new LinkedMultiValueMap<String, Double>();
        LinkedMultiValueMap<String, Double> mapTemperature = new LinkedMultiValueMap<String, Double>();
        LinkedMultiValueMap<String, Double> mapHumidity = new LinkedMultiValueMap<String, Double>();
        LinkedMultiValueMap<String, Double> mapAirPressure = new LinkedMultiValueMap<String, Double>();
        Map<String, Object> params = new HashMap<>();
        params.put("beginDataTime", beginDataTime);
        params.put("endDataTime", endDataTime);
        WfFileConfig wfFileConfig = wfFileConfigService.selectWfFileConfigById(Convert.toLong(stationNumber));
        if (ObjectUtil.isNotNull(wfFileConfig)) {
            WfNwpData wfNwpData = new WfNwpData();
            wfNwpData.setWindplantNo(Convert.toLong(stationNumber));
            wfNwpData.setNwpType(wfFileConfig.getUspNwpNo());
            wfNwpData.setParams(params);
            List<WfNwpData> wfNwpDataList = wfNwpDataService.selectWfNwpDataList(wfNwpData);
            Map<Date, List<WfNwpData>> map1 = wfNwpDataList.stream().collect(Collectors.groupingBy(WfNwpData::getDataTime));
            TreeMap<Date, List<WfNwpData>> treeMap = new TreeMap<Date, List<WfNwpData>>(map1);
            for (Map.Entry<Date, List<WfNwpData>> entry : treeMap.entrySet()) {
                List<WfNwpData> value = entry.getValue();
                if (value.size() > 0) {
                    //排序
                    value.sort((o1, o2) -> o2.getFileTime().compareTo(o1.getFileTime()));
                    double windSpeed = value.get(0).getWindSpeed();
                    double windDir = value.get(0).getWindDir();
                    double temperature = value.get(0).getTemperature();
                    double humidity = value.get(0).getHumidity();
                    double airPressure = value.get(0).getAirPressure();
                    String dataTime = DateUtil.format(value.get(0).getDataTime(), "yyyy-MM-dd HH:mm");
                    ShareController.addMapValue(mapWindSpeed, windSpeed, dataTime, 0, 1);
                    ShareController.addMapValue(mapWindDir, windDir, dataTime, 0, 1);
                    ShareController.addMapValue(mapTemperature, temperature, dataTime, 0, 1);
                    ShareController.addMapValue(mapHumidity, humidity, dataTime, 0, 1);
                    ShareController.addMapValue(mapAirPressure, airPressure, dataTime, 0, 1);
                }
            }
        }
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        for (Date date : dateList) {
            String dateTime = DateUtil.format(date, "yyyy-MM-dd HH:mm");
            TjDataFormat tjDataFormat = new TjDataFormat();
            tjDataFormat.setDataTime(dateTime);
            List<Double> windSpeedList = mapWindSpeed.get(dateTime);
            List<Double> windDirList = mapWindDir.get(dateTime);
            List<Double> temperatureList = mapTemperature.get(dateTime);
            List<Double> humidityList = mapHumidity.get(dateTime);
            List<Double> airPressureList = mapAirPressure.get(dateTime);
            if (windSpeedList != null) {
                if (windSpeedList.size() > 0) {
                    if (windSpeedList.get(0) != null) {
                        //风速
                        tjDataFormat.setWindSpeed(windSpeedList.get(0));
                    }
                }
            }
            if (windDirList != null) {
                if (windDirList.size() > 0) {
                    if (windDirList.get(0) != null) {
                        //风向
                        tjDataFormat.setWindDir(windDirList.get(0));
                    }
                }
            }
            if (temperatureList != null) {
                if (temperatureList.size() > 0) {
                    if (temperatureList.get(0) != null) {
                        //温度
                        tjDataFormat.setTemperature(temperatureList.get(0));
                    }
                }
            }
            if (humidityList != null) {
                if (humidityList.size() > 0) {
                    if (humidityList.get(0) != null) {
                        //湿度
                        tjDataFormat.setHumidity(humidityList.get(0));
                    }
                }
            }
            if (airPressureList != null) {
                if (airPressureList.size() > 0) {
                    if (airPressureList.get(0) != null) {
                        //压强
                        tjDataFormat.setAirPressure(airPressureList.get(0));
                    }
                }
            }
            tjDataFormatList.add(tjDataFormat);
        }
        json.put("list", tjDataFormatList);
        return AjaxResult.success(json);
    }

    public static class TjDataFormat {

        //时间
        private String dataTime;

        //风速
        private Double windSpeed;

        //风向
        private Double windDir;

        //温度
        private Double temperature;

        //湿度
        private Double humidity;

        //压强
        private Double airPressure;

        public String getDataTime() {
            return dataTime;
        }

        public void setDataTime(String dataTime) {
            this.dataTime = dataTime;
        }

        public Double getWindSpeed() {
            return windSpeed;
        }

        public void setWindSpeed(Double windSpeed) {
            this.windSpeed = windSpeed;
        }

        public Double getWindDir() {
            return windDir;
        }

        public void setWindDir(Double windDir) {
            this.windDir = windDir;
        }

        public Double getTemperature() {
            return temperature;
        }

        public void setTemperature(Double temperature) {
            this.temperature = temperature;
        }

        public Double getHumidity() {
            return humidity;
        }

        public void setHumidity(Double humidity) {
            this.humidity = humidity;
        }

        public Double getAirPressure() {
            return airPressure;
        }

        public void setAirPressure(Double airPressure) {
            this.airPressure = airPressure;
        }
    }
}
