package com.gyee.frame.controller.monitor;

import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.auto.Forecastwindspeed;
import com.gyee.frame.model.auto.ForecastwindspeedLongid;
import com.gyee.frame.model.auto.Weatherfd;
import com.gyee.frame.service.ForecastwindspeedService;
import com.gyee.frame.service.WeatherfdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.CacheRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/decision")
public class PredictionController {
    @Autowired
    private WeatherfdService weatherfdService;
    @Autowired
    private ForecastwindspeedService forecastwindspeedService;
    @GetMapping("/ycfsdl/{nianyue}")
    public AjaxResult ycfsdc(@PathVariable long nianyue) {
        Map<String, Date> map = timeInMillisToDate(nianyue);
        if(map==null) return AjaxResult.error("时间错误！");
        List<Weatherfd> wrd = weatherfdService.findWeatherfdRemoveDuplicates(map.get("StartDate"), map.get("EndDate"));
        List<ForecastwindspeedLongid> byTime = forecastwindspeedService.findByTime("forecastwindspeed",map.get("StartDate"), map.get("EndDate"));
        Map<String, Object> mapObj = new HashMap<>();
        mapObj.put("Weatherfd", wrd);
        mapObj.put("Forecastwindspeed", byTime);
        return AjaxResult.successData(AjaxStatus.success.code, mapObj);
    }
    @GetMapping("/ycfsdlxz/{nianyue}")
    public AjaxResult ycfsdcxz(@PathVariable long nianyue) {
        Map<String, Date> map = timeInMillisToDate(nianyue);
        if(map==null) return AjaxResult.error("时间错误！");
        List<Weatherfd> wrd = weatherfdService.findWeatherfdRemoveDuplicates(map.get("StartDate"), map.get("EndDate"));
        List<ForecastwindspeedLongid> byTime = forecastwindspeedService.findByTime("forecastwindspeedamended",map.get("StartDate"), map.get("EndDate"));
        Map<String, Object> mapObj = new HashMap<>();
        mapObj.put("Weatherfd", wrd);
        mapObj.put("Forecastwindspeed", byTime);
        return AjaxResult.successData(AjaxStatus.success.code, mapObj);
    }
    @GetMapping("/ycfsdlnh/{nianyue}")
    public AjaxResult ycfsdcnh(@PathVariable long nianyue) {
        Map<String, Date> map = timeInMillisToDate(nianyue);
        if(map==null) return AjaxResult.error("时间错误！");
        List<Weatherfd> wrd = weatherfdService.findWeatherfdRemoveDuplicates(map.get("StartDate"), map.get("EndDate"));
        List<ForecastwindspeedLongid> byTime = forecastwindspeedService.findByTime("forecastwindspeedfitting",map.get("StartDate"), map.get("EndDate"));
        Map<String, Object> mapObj = new HashMap<>();
        mapObj.put("Weatherfd", wrd);
        mapObj.put("Forecastwindspeed", byTime);
        return AjaxResult.successData(AjaxStatus.success.code, mapObj);
    }
    private Map<String,Date> timeInMillisToDate(long tim){
        if (tim == 0.0) return null;
        Map<String, Date> map = new HashMap<>();
        Calendar c= Calendar.getInstance();
        c.setTimeInMillis(tim);
        c.set(c.get(Calendar.YEAR),c.get(Calendar.MONTH)+1,1,0,0,0);
        Date startTime=c.getTime();

        c.add(Calendar.MONTH,1);
        c.add(Calendar.DATE,-1);
        Date endTime=c.getTime();

        map.put("StartDate", startTime);
        map.put("EndDate", endTime);
        return map;
    }
}
