package com.gyee.frame.service.app.index;

import com.gyee.frame.model.auto.Forecastwindspeedamended;
import com.gyee.frame.model.auto.ForecastwindspeedamendedExample;
import com.gyee.frame.netty.websocket.util.SocketToolService;
import com.gyee.frame.service.ForecastwindspeedamendedService;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class FocastpowerService extends SocketToolService {

    @Resource
    private ForecastwindspeedamendedService forecastwindspeedamendedService;


    public double focastpower(String name){


        ForecastwindspeedamendedExample forecastwindspeedamendedExample=new ForecastwindspeedamendedExample();
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date begin=c.getTime();
        c.add(Calendar.DAY_OF_MONTH,1);
        Date end=c.getTime();

        if(name.equals("0"))
        {
            forecastwindspeedamendedExample.createCriteria().andRecodedateBetween(begin,end);

        }else
        {
            forecastwindspeedamendedExample.createCriteria().andWpidEqualTo(name).andRecodedateBetween(begin,end);

        }
        List<Forecastwindspeedamended> wtls = forecastwindspeedamendedService.selectByExample(forecastwindspeedamendedExample);
        double tmp = 0;
        for(Forecastwindspeedamended wt:wtls)
        {
            tmp = tmp + wt.getDaypower().doubleValue()+wt.getNightpower().doubleValue();
        }
        return tmp;
    }

    public double focastspeed(String name){


        ForecastwindspeedamendedExample forecastwindspeedamendedExample=new ForecastwindspeedamendedExample();
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date begin=c.getTime();
        c.add(Calendar.DAY_OF_MONTH,1);
        Date end=c.getTime();

        if(name.equals("0"))
        {
            forecastwindspeedamendedExample.createCriteria().andRecodedateBetween(begin,end);

        }else
        {
            forecastwindspeedamendedExample.createCriteria().andWpidEqualTo(name).andRecodedateBetween(begin,end);

        }
        List<Forecastwindspeedamended> wtls = forecastwindspeedamendedService.selectByExample(forecastwindspeedamendedExample);
        double tmp = 0;

        if(!wtls.isEmpty())
        {
            for(Forecastwindspeedamended wt:wtls)
            {
                tmp = tmp + wt.getDayspeed().doubleValue()+wt.getNightspeed().doubleValue();

            }
            tmp= StringUtils.round(tmp/(wtls.size()*2),2);
        }

        return tmp;
    }

}
