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

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.util.DateUtils;
import com.gyee.frame.util.golden.EdosUtil;
import com.gyee.frame.util.golden.IEdosUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;

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

@Service
public class TargetdetailService extends SocketToolService {

    IEdosUtil mongodb = new EdosUtil();
    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<>();

    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();
            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",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));
            Long count = (endTime.getTime() - beginTime.getTime()) / 1000L / 3600L;
            List<PointData> ls = mongodb.getHistoryDatasSnap(targetdetail_curve_pointMap.get(ip),
                    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",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);
//查看历史快照数据
            List<PointData> ls = mongodb.getHistoryDatasSnap(targetdetail_histogram_pointMap.get(ip),
                    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",pointDataMap, Messagetype.MESSAGE);
    }


}
