package com.gyee.generation.task.thread;

import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.ProBasicEquipment;
import com.gyee.generation.model.auto.ProBasicEquipmentPoint;
import com.gyee.generation.model.auto.ProEconEquipmentInfoDay5;
import com.gyee.generation.util.DateUtils;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

public class EquipmentInfo5ThreadPool implements Callable<String>, Serializable {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final long serialVersionUID = 0;

    // 保存任务所需要的数据

    private Object threadPoolTaskData;
    private ProBasicEquipment wt;
    private Map<String, ProEconEquipmentInfoDay5> rates;

    private Map<Double, Double> stateDescription1;
    private Map<Double, Double> stateDescription2;
    private Date recordDate;
    private Date endDate;

    private Date beginDate;

    private IEdosUtil edosUtil;


    public EquipmentInfo5ThreadPool(Object tasks, ProBasicEquipment wt, Map<String, ProEconEquipmentInfoDay5> rates,
                                    Date endDate, Date beginDate,
                                    IEdosUtil edosUtil, Map<Double, Double> stateDescription1,
                                    Map<Double, Double> stateDescription2
    ) {

        this.threadPoolTaskData = tasks;
        this.wt = wt;
        this.rates = rates;

        this.endDate = endDate;
        this.beginDate = beginDate;

        this.edosUtil = edosUtil;
        this.stateDescription1 = stateDescription1;
        this.stateDescription2 = stateDescription2;

    }

    public synchronized String call() throws Exception {


        // 处理一个任务，这里的处理方式太简单了，仅仅是一个打印语句

        //System.out.println("开始执行任务：" + threadPoolTaskData);

        String result = "";

        // //便于观察，等待一段时间

        try {
            ProEconEquipmentInfoDay5 rate = rates.get(wt.getId());

            List<PointData> speedDataList = null;
            List<PointData> statusDataList = null;
            if (CacheContext.wtpAimap.containsKey(wt.getId())) {
                Map<String, ProBasicEquipmentPoint> wtpmap = CacheContext.wtpAimap.get(wt.getId());
                if (wtpmap.containsKey(ContantXk.CJ_SSFS)) {
                    ProBasicEquipmentPoint point = wtpmap.get(ContantXk.CJ_SSFS);
                    speedDataList = edosUtil.getHistoryDatasSnap(point.getNemCode(), beginDate.getTime() / 1000, endDate.getTime() / 1000);
                }
                if (wtpmap.containsKey(ContantXk.MXZT)) {
                    ProBasicEquipmentPoint point = wtpmap.get(ContantXk.MXZT);
                    statusDataList = edosUtil.getHistoryDatasSnap(point.getNemCode(), beginDate.getTime() / 1000, endDate.getTime() / 1000);
                }

            }


            if (speedDataList != null && statusDataList != null && statusDataList.size() == speedDataList.size() && !speedDataList.isEmpty()) {

                double laststatus = -1;
                Date begin = new Date();
                for (int i = 0; i < speedDataList.size(); i++) {

                    double status = statusDataList.get(i).getPointValueInDouble();
                    double speed = speedDataList.get(i).getPointValueInDouble();
                    if (i == 0) {
                        begin = new Date(speedDataList.get(i).getPointTime());
                        laststatus = status;
                    }

                    if (speed <= 3.0) {
                        if (laststatus != status &&
                                (status == stateDescription1.get(3.0) || status == stateDescription2.get(3.0))) {

                            Date end = new Date(speedDataList.get(i).getPointTime());
                            double zhcs = DateUtils.hoursDiff2(begin, end);
                            if (zhcs < 0.25)//5分钟
                            {

                                double temp = rate.getTimerate1();
                                temp = temp + 1;
                                rate.setTimerate1(temp);

                                temp = rate.getTimerate2();
                                temp = temp + 1;
                                rate.setTimerate2(temp);

                                temp = rate.getTimerate3();
                                temp = temp + 1;
                                rate.setTimerate3(temp);

                                temp = rate.getTimerate4();
                                temp = temp + 1;
                                rate.setTimerate4(temp);

                            } else if (zhcs < 0.33)//10分钟
                            {
                                double temp = rate.getTimerate2();
                                temp = temp + 1;
                                rate.setTimerate2(temp);

                                temp = rate.getTimerate3();
                                temp = temp + 1;
                                rate.setTimerate3(temp);

                                temp = rate.getTimerate4();
                                temp = temp + 1;
                                rate.setTimerate4(temp);
                            } else if (zhcs < 0.42)//15分钟
                            {
                                double temp = rate.getTimerate3();
                                temp = temp + 1;
                                rate.setTimerate3(temp);

                                temp = rate.getTimerate4();
                                temp = temp + 1;
                                rate.setTimerate4(temp);
                            } else if (zhcs < 0.5)//20分钟
                            {
                                double temp = rate.getTimerate4();
                                temp = temp + 1;
                                rate.setTimerate4(temp);
                            }


                            double temp = rate.getTimerate13();
                            temp = temp + 1;
                            rate.setTimerate13(temp);
                        }

                        laststatus = status;
                        begin = new Date(speedDataList.get(i).getPointTime());
                    } else if (speed <= 4.0 && speed > 3.0) {
                        if (laststatus != status &&
                                (status == stateDescription1.get(3.5) || status == stateDescription2.get(3.5))) {

                            Date end = new Date(speedDataList.get(i).getPointTime());
                            double zhcs = DateUtils.hoursDiff2(begin, end);
                            if (zhcs < 0.25)//5分钟
                            {

                                double temp = rate.getTimerate5();
                                temp = temp + 1;
                                rate.setTimerate5(temp);

                                temp = rate.getTimerate6();
                                temp = temp + 1;
                                rate.setTimerate6(temp);

                                temp = rate.getTimerate7();
                                temp = temp + 1;
                                rate.setTimerate7(temp);

                                temp = rate.getTimerate8();
                                temp = temp + 1;
                                rate.setTimerate8(temp);
                            } else if (zhcs < 0.33)//10分钟
                            {

                                double temp = rate.getTimerate6();
                                temp = temp + 1;
                                rate.setTimerate6(temp);

                                temp = rate.getTimerate7();
                                temp = temp + 1;
                                rate.setTimerate7(temp);

                                temp = rate.getTimerate8();
                                temp = temp + 1;
                                rate.setTimerate8(temp);
                            } else if (zhcs < 0.42)//15分钟
                            {
                                double temp = rate.getTimerate7();
                                temp = temp + 1;
                                rate.setTimerate7(temp);

                                temp = rate.getTimerate8();
                                temp = temp + 1;
                                rate.setTimerate8(temp);

                            } else if (zhcs < 0.5)//20分钟
                            {
                                double temp = rate.getTimerate8();
                                temp = temp + 1;
                                rate.setTimerate8(temp);
                            }

                            //}
                            double temp = rate.getTimerate14();
                            temp = temp + 1;
                            rate.setTimerate14(temp);

                        }

                        laststatus = status;
                        begin = new Date(speedDataList.get(i).getPointTime());
                    } else {
                        if (laststatus != status &&
                                (status == stateDescription1.get(4.0) || status == stateDescription2.get(4.0))) {

                            Date end = new Date(speedDataList.get(i).getPointTime());
                            double zhcs = DateUtils.hoursDiff2(begin, end);
                            if (zhcs < 0.25)//5分钟
                            {

                                double temp = rate.getTimerate9();
                                temp = temp + 1;
                                rate.setTimerate9(temp);

                                temp = rate.getTimerate10();
                                temp = temp + 1;
                                rate.setTimerate10(temp);

                                temp = rate.getTimerate11();
                                temp = temp + 1;
                                rate.setTimerate11(temp);

                                temp = rate.getTimerate12();
                                temp = temp + 1;
                                rate.setTimerate12(temp);
                            } else if (zhcs < 0.33)//10分钟
                            {

                                double temp = rate.getTimerate10();
                                temp = temp + 1;
                                rate.setTimerate10(temp);

                                temp = rate.getTimerate11();
                                temp = temp + 1;
                                rate.setTimerate11(temp);

                                temp = rate.getTimerate12();
                                temp = temp + 1;
                                rate.setTimerate12(temp);
                            } else if (zhcs < 0.42)//15分钟
                            {

                                double temp = rate.getTimerate11();
                                temp = temp + 1;
                                rate.setTimerate11(temp);

                                temp = rate.getTimerate12();
                                temp = temp + 1;
                                rate.setTimerate12(temp);
                            } else if (zhcs < 0.5)//20分钟
                            {
                                double temp = rate.getTimerate12();
                                temp = temp + 1;
                                rate.setTimerate12(temp);
                            }

                            double temp = rate.getTimerate15();
                            temp = temp + 1;
                            rate.setTimerate15(temp);


                            laststatus = status;
                            begin = new Date(speedDataList.get(i).getPointTime());
                        }
                    }
                }

            }

        } catch (Exception e) {

            e.printStackTrace();
        }

        threadPoolTaskData = null;

        return result;

    }


}

