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

import com.gyee.frame.model.auto.Weatherfd;
import com.gyee.frame.model.auto.Windpowerinfoday;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.model.enumeration.Messagetype;
import com.gyee.frame.netty.websocket.util.SocketToolService;
import com.gyee.frame.service.WeatherfdService;
import com.gyee.frame.service.WindpowerinfodayService;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import com.gyee.frame.util.StringUtils;
import com.gyee.frame.util.golden.StringUtil;
import org.springframework.stereotype.Service;

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

/**
 * @author Wang Jiawen
 * @purpose
 * @date 2020-05-14 09:58
 */

@Service
public class TargetdetailService extends SocketToolService {

    @Resource
    private WeatherfdService  weatherfdService;
    @Resource
    private WindpowerinfodayService windpowerinfodayService;
    public static Map<String,String> targetdetail_target_pointMap = new HashMap<>();
    public static Map<String,String> targetdetail_curve_pointMap = new HashMap<>();
    public static Map<String,String> targetdetail_histogram_pointMap = new HashMap<>();
    IRealTimeDataBaseUtil mongodb= RealTimeDataBaseFactory.createRealTimeDataBase();
    private double decimal(int num,double value){
        return new BigDecimal(value).setScale(num, RoundingMode.HALF_EVEN).doubleValue();
    }
    public void targetdetail_target() throws Exception {
        Set set = targetdetail_target_pointMap.keySet();
        Iterator<String>  it =  set.iterator();
        Map<String,Double> pointDataMap = new HashMap();
        while(it.hasNext()){
            String ip = it.next();
            String pointid=targetdetail_target_pointMap.get(ip);
            if(pointid.indexOf("ora_")!=-1)
            {
                pointid=pointid.substring(4);
                String []temp=pointid.split("_");
                if(temp.length==2)
                {
                    String id=temp[0];
                    String wpid=temp[1];

                    switch (id) {
                        case "ycfs":
                            if(wpid.equals("0"))
                            {
                                wpid="MHS_FDC";
                            }
                            List<Weatherfd> list=weatherfdService.listDay(wpid);
                            if(!list.isEmpty())
                            {
                                Weatherfd wd=list.get(0);
                                double value =wd.getSpeed1();
                                pointDataMap.put(ip, StringUtils.round(value,2));
                            }else
                            {
                                pointDataMap.put(ip, 0.0);
                            }
                            break;
                        case "ylyxs":

                            List<Windpowerinfoday> wdls=windpowerinfodayService.listByDate(wpid);
                            if(!wdls.isEmpty())
                            {
                                Windpowerinfoday wd=wdls.get(0);
                                double value =wd.getMonthlyxs();
                                pointDataMap.put(ip, StringUtils.round(value,2));
                            }else
                            {
                                pointDataMap.put(ip, 0.0);
                            }
                            break;
                        case "nlyxs":

                            List<Windpowerinfoday> wdls2=windpowerinfodayService.listByDate(wpid);
                            if(!wdls2.isEmpty())
                            {
                                Windpowerinfoday wd=wdls2.get(0);
                                double value =wd.getYearlyxs();
                                pointDataMap.put(ip, StringUtils.round(value,2));
                            }else
                            {
                                pointDataMap.put(ip, 0.0);
                            }
                            break;
                        case "zhcydl":

                            List<Windpowerinfoday> wdls3=windpowerinfodayService.listByDate(wpid);
                            if(!wdls3.isEmpty())
                            {
                                Windpowerinfoday wd=wdls3.get(0);
                                double value =wd.getMonthzhcydl();
                                pointDataMap.put(ip, StringUtils.round(value,2));
                            }else
                            {
                                pointDataMap.put(ip, 0.0);
                            }
                            break;
                        case "sbklyl":

                            List<Windpowerinfoday> wdls4=windpowerinfodayService.listByDate(wpid);
                            if(!wdls4.isEmpty())
                            {
                                Windpowerinfoday wd=wdls4.get(0);
                                double value =wd.getMonthsbklyl();
                                pointDataMap.put(ip, StringUtils.round(value,2));
                            }else
                            {
                                pointDataMap.put(ip, 0.0);
                            }
                            break;
                        default:
                            break;
                    }
                }

            }else
            {
                PointData real=mongodb.getRealData(targetdetail_target_pointMap.get(ip));
                double pointValueInDouble =this.decimal(2,real.getPointValueInDouble()) ;
                pointDataMap.put(ip,pointValueInDouble);
            }

        }

        this.pushToWeb("pageNumber_8","functionNumber_1","all",pointDataMap, Messagetype.MESSAGE);
    }

    public void targetdetail_curve() throws Exception {
        Set set = targetdetail_curve_pointMap.keySet();
        Iterator<String>  it =  set.iterator();
        Map<String,List<PointData>> pointDataMap = new HashMap();
        while(it.hasNext()) {
            String ip = it.next();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
            Date nowTime = DateUtils.getNowDate();
            Date endTime = sdf.parse(sdf.format(nowTime));
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            Date beginTime = sdf1.parse(sdf1.format(nowTime));

            String pointid=targetdetail_curve_pointMap.get(ip);
            if(pointid.indexOf("ora_")!=-1)
            {
                pointid=pointid.substring(4);
                String []temp=pointid.split("_");
                if(temp.length==2)
                {
                    String id=temp[0];
                    String wpid=temp[1];

                    set7DayValues(pointDataMap, ip, id, wpid);
                }

            }else
            {
                Long count = (endTime.getTime() - beginTime.getTime()) / 1000L / 3600L;
                List<PointData> ls = mongodb.getHistoryDatasSnap(pointid,
                        beginTime.getTime() / 1000, endTime.getTime() / 1000 + 3600L,
                        (long) count+1, 3600L);
                for (int i = 0; i < ls.size(); i++) {
                    ls.get(i).setPointTime(ls.get(i).getPointTime() - 3600);
                    ls.get(i).setPointValueInDouble(this.decimal(2,ls.get(i).getPointValueInDouble()));
                }
                if(ls.size()>0) {
                    ls.remove(0);
                }
                pointDataMap.put(ip,ls);
            }

        }
/*        if (!ls.isEmpty()) {
            ls.forEach(obj -> //System.out.println(obj.getEdnaId() + "___" +obj.getPointTime() + "___" + obj.getPointValueInDouble()));
        }
        for(PointData obj:ls){
            //System.out.println(obj.getEdnaId() + "___" +obj.getPointTime() + "___" + obj.getPointValueInDouble());
        }*/
        this.pushToWeb("pageNumber_8","functionNumber_2","all",pointDataMap, Messagetype.MESSAGE);
    }

    public void targetdetail_histogram() throws Exception {


        Set set = targetdetail_histogram_pointMap.keySet();
        Iterator<String>  it =  set.iterator();
        Map<String,List<PointData>> pointDataMap = new HashMap();
        while(it.hasNext()) {
            String ip = it.next();
            Date nowTime = DateUtils.getNowDate();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            Date endTime = sdf1.parse(sdf1.format(nowTime));
            Date beginTime = DateUtils.addDays(endTime, -7);

            String pointid=targetdetail_histogram_pointMap.get(ip);
            if(pointid.indexOf("ora_")!=-1)
            {
                pointid=pointid.substring(4);
                String []temp=pointid.split("_");
                if(temp.length==2)
                {
                    String id=temp[0];
                    String wpid=temp[1];

                    set7DayValues(pointDataMap, ip, id, wpid);
                }

            }else
            {
                List<PointData> ls = mongodb.getHistoryDatasSnap(pointid,
                        beginTime.getTime() / 1000, endTime.getTime() / 1000 + 3600L*24,
                        8L, 3600 * 24L);
                for (int i = 0; i < ls.size(); i++) {
                    ls.get(i).setPointTime(ls.get(i).getPointTime() - 24 * 3600);
                    ls.get(i).setPointValueInDouble(this.decimal(2,ls.get(i).getPointValueInDouble()));
                }
                if(ls.size()>0) {
                    ls.remove(0);
                }
                pointDataMap.put(ip,ls);
            }

        }

        /*if (!ls.isEmpty()) {
            ls.forEach(obj -> //System.out.println(obj.getEdnaId() + "___" +obj.getPointTime() + "___" + obj.getPointValueInDouble()));
        }*/
        this.pushToWeb("pageNumber_8","functionNumber_3","all",pointDataMap, Messagetype.MESSAGE);
    }

    private void set7DayValues(Map<String, List<PointData>> pointDataMap, String ip, String id, String wpid) throws ParseException {
        switch (id) {
            case "ycfs":
                if(wpid.equals("0"))
                {
                    wpid="MHS_FDC";
                }
                List<Weatherfd> list=weatherfdService.list7Day(wpid);
                List<PointData> vos=new ArrayList<>();
                if(!list.isEmpty())
                {
                    for (Weatherfd fd:list)
                    {
                        PointData vo=new PointData();
                        vo.setPointTime(fd.getRecodedata().getTime());
                        vo.setPointValueInDouble(fd.getSpeed1());
                        vos.add(vo);
                    }

                }
                pointDataMap.put(ip, vos);
                break;
            case "ylyxs":
                List<Windpowerinfoday> wdls=windpowerinfodayService.listBy7Date(wpid);;
                List<PointData> vos1=new ArrayList<>();
                if(!wdls.isEmpty())
                {
                    for (Windpowerinfoday fd:wdls)
                    {
                        PointData vo=new PointData();
                        vo.setPointTime(fd.getRecorddate().getTime());
                        vo.setPointValueInDouble(fd.getMonthlyxs());
                        vos1.add(vo);
                    }

                }
                pointDataMap.put(ip, vos1);
                break;
            case "nlyxs":
                List<Windpowerinfoday> wdls2=windpowerinfodayService.listBy7Date(wpid);;
                List<PointData> vos2=new ArrayList<>();
                if(!wdls2.isEmpty())
                {
                    for (Windpowerinfoday fd:wdls2)
                    {
                        PointData vo=new PointData();
                        vo.setPointTime(fd.getRecorddate().getTime());
                        vo.setPointValueInDouble(fd.getYearlyxs());
                        vos2.add(vo);
                    }

                }
                pointDataMap.put(ip, vos2);
                break;
            case "zhcydl":
                List<Windpowerinfoday> wdls3=windpowerinfodayService.listBy7Date(wpid);;
                List<PointData> vos3=new ArrayList<>();
                if(!wdls3.isEmpty())
                {
                    for (Windpowerinfoday fd:wdls3)
                    {
                        PointData vo=new PointData();
                        vo.setPointTime(fd.getRecorddate().getTime());
                        vo.setPointValueInDouble(fd.getMonthzhcydl());
                        vos3.add(vo);
                    }

                }
                pointDataMap.put(ip, vos3);
                break;
            case "sbklyl":
                List<Windpowerinfoday> wdls4=windpowerinfodayService.listBy7Date(wpid);;
                List<PointData> vos4=new ArrayList<>();
                if(!wdls4.isEmpty())
                {
                    for (Windpowerinfoday fd:wdls4)
                    {
                        PointData vo=new PointData();
                        vo.setPointTime(fd.getRecorddate().getTime());
                        vo.setPointValueInDouble(fd.getMonthsbklyl());
                        vos4.add(vo);
                    }

                }
                pointDataMap.put(ip, vos4);
                break;

            default:
                break;
        }
    }


}
