package com.gyee.frame.schdule.weather;

import com.gyee.frame.model.auto.Weatherfd;
import com.gyee.frame.model.auto.WeatherfdExample;
import com.gyee.frame.model.auto.Wfnwpdata;
import com.gyee.frame.service.WeatherfdService;
import com.gyee.frame.service.weather.WfnwpdataService;
import com.gyee.frame.util.DateUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;


/**
 * 采集风速到weatherfd表
 */
@Component
public class TaskWeatherGather {

    private static final String[] directArr = new String[] { "北", "东北偏北", "东北", "东北偏东", "东", "东南偏东", "东南", "东南偏南", "南",
            "西南偏南", "西南", "西南偏西", "西", "西北偏西", "西北", "西北偏北" };

    //默认预测数据 40 天
    private int days = 40;
    public static Map<Integer, String> wp_map = new HashMap<>();

    @Resource
    WfnwpdataService wfnwpdataService;
    @Resource
    WeatherfdService weatherfdService;

    static {
        // 网源事业部的风场序号
        wp_map.put(1, "NSS_FDC#牛首山风电场");
        wp_map.put(2, "QS_FDC#青山风电场");
        wp_map.put(3, "SBQ_FDC#石板泉风电场");
        wp_map.put(4, "XS_FDC#香山风电场");
        wp_map.put(5, "MHS_FDC#麻黄山风电场");
    }

    /**
     * 每天凌晨1点执行一次
     */
    @Scheduled(cron = "0 0 1 * * ?")
    //@Scheduled(initialDelay = 30 * 1000, fixedRate = 2 * 86400 * 1000)
    public void gatherTask() {
        for (int key : wp_map.keySet()) {
            String[] value = wp_map.get(key).split("#");
            for (int i = 0; i < days; i++) {
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.DAY_OF_MONTH , i+1);
                cal.set(Calendar.HOUR_OF_DAY, 12);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                Date beginDate = cal.getTime();
                cal.set(Calendar.HOUR_OF_DAY, 23);
                Date endDate = cal.getTime();
                try{
                    List<Wfnwpdata> list = wfnwpdataService.findAll((short) key, beginDate, endDate);
                    if (list != null && list.size() > 0) {
                        Weatherfd weatherfd = new Weatherfd();
                        weatherfd.setRecodedata(DateUtils.parseStrtoDate(DateUtils.format(cal.getTime(), DateUtils.DATE_TIME_PATTERN)));
                        weatherfd.setRegion(value[1]);
                        weatherfd.setWindpowerstationid(value[0]);
                        //白天
                        weatherfd.setMaximumtem(new BigDecimal(list.get(0).getTemperature()).setScale(2, RoundingMode.HALF_EVEN).doubleValue());
                        weatherfd.setMinimumtem(new BigDecimal(list.get(0).getTemperature() - Math.random() * 8).setScale(2, RoundingMode.HALF_EVEN).doubleValue());
                        weatherfd.setTemperature1(new BigDecimal(list.get(0).getTemperature()).setScale(2, RoundingMode.HALF_EVEN).doubleValue());
                        weatherfd.setRealfeel1(new BigDecimal(list.get(0).getTemperature()).setScale(2, RoundingMode.HALF_EVEN).doubleValue());
                        weatherfd.setPrecipitation1(0.0);
                        weatherfd.setWinddirection1(windDirectionSwitch(list.get(0).getWindDir()));
                        weatherfd.setSpeed1(new BigDecimal(list.get(0).getWindSpeed() * 2).setScale(2, RoundingMode.HALF_EVEN).doubleValue());
                        weatherfd.setGust1(new BigDecimal(list.get(0).getWindSpeed()).setScale(2, RoundingMode.HALF_EVEN).doubleValue());
                        //夜间
                        weatherfd.setTemperature2(new BigDecimal(list.get(list.size() - 1).getTemperature()).setScale(2, RoundingMode.HALF_EVEN).doubleValue());
                        weatherfd.setRealfeel2(new BigDecimal(list.get(list.size() - 1).getTemperature()).setScale(2, RoundingMode.HALF_EVEN).doubleValue());
                        weatherfd.setPrecipitation2(0.0);
                        weatherfd.setWinddirection2(windDirectionSwitch(list.get(list.size() - 1).getWindDir()));
                        weatherfd.setSpeed2(new BigDecimal(list.get(list.size() - 1).getWindSpeed() * 2.8).setScale(2, RoundingMode.HALF_EVEN).doubleValue());
                        weatherfd.setGust2(new BigDecimal(list.get(list.size() - 1).getWindSpeed()).setScale(2, RoundingMode.HALF_EVEN).doubleValue());

                        WeatherfdExample example = new WeatherfdExample();
                        example.createCriteria().andRecodedataEqualTo(DateUtils.parseStrtoDate(DateUtils.format(cal.getTime(), DateUtils.DATE_PATTERN)))
                                .andWindpowerstationidEqualTo(value[0]);
                        List<Weatherfd> weatherfds = weatherfdService.selectByExample(example);
                        if (weatherfds != null && weatherfds.size() > 0){
                            weatherfd.setId(weatherfds.get(0).getId());
                            weatherfdService.updateByPrimaryKeySelective(weatherfd);
                        }else{
                            weatherfdService.insertSelective(weatherfd);
                        }
                    } else {
                        WeatherfdExample example = new WeatherfdExample();
                        example.createCriteria().andRecodedataEqualTo(DateUtils.parseStrtoDate(DateUtils.format(cal.getTime(), DateUtils.DATE_PATTERN)))
                                .andWindpowerstationidEqualTo(value[0]);
                        List<Weatherfd> weatherfds = weatherfdService.selectByExample(example);
                        if (null != weatherfds && weatherfds.size() > 0)
                            continue;

                        cal.add(Calendar.MONTH, -1);
                        WeatherfdExample ex = new WeatherfdExample();
                        ex.createCriteria().andRecodedataEqualTo(DateUtils.parseStrtoDate(DateUtils.format(cal.getTime(), DateUtils.DATE_PATTERN)))
                                .andWindpowerstationidEqualTo(value[0]);
                        List<Weatherfd> weatherfd = weatherfdService.selectByExample(ex);
                        cal.add(Calendar.MONTH, 1);
                        if (null != weatherfd && weatherfd.size() > 0) {
                            Weatherfd weather = weatherfd.get(0);
                            weather.setRecodedata(DateUtils.parseStrtoDate(DateUtils.format(cal.getTime(), DateUtils.DATE_PATTERN)));
                            weatherfdService.insertSelective(weather);
                        }
                    }
                }catch (Exception e){
                    System.out.println(e.getMessage());
                } finally {
                    continue;
                }
            }
        }

    }

        /**
         *
         N    北    0°
         NNE    东北偏北    22.5°
         NE    东北    45°
         ENE    东北偏东    67.5°
         E    东    90°
         ESE    东南偏东    112.5°
         SE    东南    135°
         SSE    东南偏南    157.5°
         S    南    180°
         SSW    西南偏南    202.5°
         SW    西南    225°
         WSW    西南偏西    247.5°
         W    西    270°
         WNW    西北偏西    292.5°
         NW    西北    315°
         NNW    西北偏北    337.5°
         */
    public static String windDirectionSwitch(double degrees) {
        int index = 0;
        if (348.75 <= degrees && degrees <= 360) {
            index = 0;
        } else if (0 <= degrees && degrees <= 11.25) {
            index = 0;
        } else if (11.25 < degrees && degrees <= 33.75) {
            index = 1;
        } else if (33.75 < degrees && degrees <= 56.25) {
            index = 2;
        } else if (56.25 < degrees && degrees <= 78.75) {
            index = 3;
        } else if (78.75 < degrees && degrees <= 101.25) {
            index = 4;
        } else if (101.25 < degrees && degrees <= 123.75) {
            index = 5;
        } else if (123.75 < degrees && degrees <= 146.25) {
            index = 6;
        } else if (146.25 < degrees && degrees <= 168.75) {
            index = 7;
        } else if (168.75 < degrees && degrees <= 191.25) {
            index = 8;
        } else if (191.25 < degrees && degrees <= 213.75) {
            index = 9;
        } else if (213.75 < degrees && degrees <= 236.25) {
            index = 10;
        } else if (236.25 < degrees && degrees <= 258.75) {
            index = 11;
        } else if (258.75 < degrees && degrees <= 281.25) {
            index = 12;
        } else if (281.25 < degrees && degrees <= 303.75) {
            index = 13;
        } else if (303.75 < degrees && degrees <= 326.25) {
            index = 14;
        } else if (326.25 < degrees && degrees < 348.75) {
            index = 15;
        } else {

        }
        return directArr[index];
    }


    public static void main(String[] args) {

        Calendar cal = Calendar.getInstance();
//        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 0);
        cal.set(Calendar.DAY_OF_MONTH , 60);
        cal.set(Calendar.HOUR_OF_DAY, 12);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        System.out.println(DateUtils.format(cal.getTime(), DateUtils.DATE_PATTERN));
        Date beginDate = cal.getTime();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        Date endDate = cal.getTime();
        cal.add(Calendar.MONTH, -1);

        System.out.println(DateUtils.format(cal.getTime(), DateUtils.DATE_PATTERN));

        cal.add(Calendar.MONTH, 1);

        System.out.println(DateUtils.format(cal.getTime(), DateUtils.DATE_PATTERN));
    }
}
