package com.gyee.runeconomy.model;


import cn.hutool.core.date.DateUtil;
import com.gyee.runeconomy.dto.result.PowerPointData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 数据预处理算法
 */
public class PowerProcessALG {


    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";


    /**
     * 数据预处理
     *
     * @param list   预处理的数据
     * @param map    风速对应的保证功率
     * @param maxs   最大风速
     * @param mins   最小风速
     * @param maxp   最大功率
     * @param minp   最小功率
     * @param isfbw  是否并网
     * @param isfhl  是否合理值
     * @param isbw   并网后10分钟
     * @param istj   停机前10分钟
     * @param isglpc 功率曲线偏差
     * @param isqfh  是否欠符合
     * @param qfhdj  欠符合等级
     * @return
     */
    public static List<PowerPointData> dataProcess(List<PowerPointData> list, Map<Double, Double> map,
                                                   Double maxs, Double mins, Double maxp, Double minp, Boolean isfbw,
                                                   Boolean isfhl, Boolean isbw, Boolean istj, Boolean isglpc, Boolean isqfh, Integer qfhdj){
        String timeBW = DateUtil.format(new Date(0), DATE_TIME_PATTERN);
        List<PowerPointData> tempei = new ArrayList<>();
        List<PowerPointData> tempqf = new ArrayList<>();

        //TODO 数据过滤  // 0正常，1过滤掉
        for (PowerPointData item : list) {
            int filter = 0;
            int fjstatus = 0;
            if (timeBW == DateUtil.format(new Date(0), DATE_TIME_PATTERN)){
                timeBW = item.getTime();
            }
            // 过滤非并网值  风机状态不等于2
            if (filter == 0 && isfbw && item.getMxzt() != 2) {
                filter = 1;
                fjstatus = 1;
                timeBW = item.getTime();
            } else {
                filter = 0;
                fjstatus = 0;
            }
            // 按给定风速功率过滤
            if (item.getSpeed() < mins || item.getSpeed() > maxs || item.getPower() < minp || item.getPower() > maxp) {
                filter = 1;
            }
            // 过滤非合理值 并网状态下功率小于等于0
            if (filter == 0 && isfhl && item.getSpeed() < 0 && item.getPower() <= minp) {
                filter = 1;
            }
            // 过滤并网后十分钟
            if (filter == 0 && isbw) {
                if (getTimeDiff(item.getTime(), timeBW) <= 10)
                    filter = 1;
            }
            // 过滤停机前十分钟
            if (istj) {
                if (fjstatus == 0) {
                    if (tempei.size() > 0) {
                        if (getTimeDiff(tempei.get(0).getTime(), timeBW) >= 10) {
                            tempei.remove(0);
                        }
                    }
                    tempei.add(item);
                } else {
                    for (PowerPointData temp : tempei) {
                        temp.setFilter(1);
                    }
                    tempei.clear();
                }
            }
            //欠发
            if (filter == 0 && item.getSpeed() >= 6 && item.getSpeed() < 12.5 && isqfh && qfhdj < item.getQfzt()) {
                filter = 1;
                for (PowerPointData temp : tempqf) {
                    temp.setFilter(1);
                }
            } else if (filter == 0 && item.getSpeed() >= 12.5 && isqfh && qfhdj < 1) {
                filter = 1;
                for (PowerPointData temp : tempqf) {
                    temp.setFilter(1);
                }
            }
            if (tempqf.size() > 0) {
                if (getTimeDiff(tempqf.get(0).getTime(), item.getTime()) >= 5) {
                    tempqf.remove(0);
                }
            }
            tempqf.add(0, item);
            item.setFilter(filter);

            //功率曲线偏差
            if (isglpc) {
                if (map.containsKey(item.getSpeed())) {
                    double power = map.get(item.getSpeed());  //不同风速对应的保证功率
                    double maxPower = map.get(24.0);     //最大保证功率
                    double k = item.getPower() / power;  //保证功率/实际功率   k:偏差百分比
                    if (item.getPower() > 0) {
                        if (k < 0.95 && maxPower <= power) {
                            item.setFilter(1);
                        }
                        if (k < 0.9 && maxPower > power) {
                            item.setFilter(1);
                        }
                        if (k < 0.85 && item.getSpeed() < 6 && item.getSpeed() > 4) {
                            item.setFilter(1);
                        }

                        if (k < 0.9 && item.getSpeed() <= 4 && item.getSpeed() > 3.5) {
                            item.setFilter(1);
                        }
//                        if (k < 0.85 && item.getSpeed() <= 3.5 && item.getSpeed() > 3) {
//                            item.setFilter(1);
//                        }
//                        if (k < 0.4 && item.getSpeed() <= 3 && item.getSpeed() > 0) {
//                            item.setFilter(1);
//                        }
                    }
                }
            }
        }

        return list;
    }


    /**
     * 按照给定风俗功率过滤
     * @param list
     * @param maxs  最大风速
     * @param mins  最小风速
     * @param maxp  最大功率
     * @param minp  最小风速
     * @return
     */
    public static List<PowerPointData> dataProcessPS(List<PowerPointData> list, Double maxs, Double mins, Double maxp, Double minp){
        //TODO 数据过滤  // 0正常，1过滤掉
        for (PowerPointData item : list) {
            int filter = 0;
            // 按给定风速功率过滤
            if (item.getSpeed() < mins || item.getSpeed() > maxs || item.getPower() < minp || item.getPower() > maxp) {
                filter = 1;
            }

            item.setFilter(filter);
        }

        return list;
    }

    /**
     * 过滤非并网值
     * @param list
     * @return
     */
    public static List<PowerPointData> dataProcessFBW(List<PowerPointData> list){
        //TODO 数据过滤  // 0正常，1过滤掉
        for (PowerPointData item : list) {
            int filter = 0;
            // 过滤非并网值  风机状态不等于2
            if (filter == 0 && item.getMxzt() != 2)
                filter = 1;

            item.setFilter(filter);
        }

        return list;
    }


    /**
     * 过滤非合理值
     * @param list
     * @return
     */
    public static List<PowerPointData> dataProcessFHLZ(List<PowerPointData> list){
        //TODO 数据过滤  // 0正常，1过滤掉
        for (PowerPointData item : list) {
            int filter = 0;
            // 过滤非合理值 并网状态下功率小于等于0
            if (filter == 0 && item.getMxzt() != 2 && item.getPower() <= 0) {
                filter = 1;
            }

            item.setFilter(filter);
        }

        return list;
    }

    /**
     * 过滤并网后几分钟内数据
     * @param list
     * @param minute  分钟
     * @return
     */
    public static List<PowerPointData> dataProcessBWH(List<PowerPointData> list, int minute){
        String timeBW = DateUtil.format(new Date(0), DATE_TIME_PATTERN);
        //TODO 数据过滤  // 0正常，1过滤掉
        for (PowerPointData item : list) {
            int filter = 0;
            if (timeBW == DateUtil.format(new Date(0), DATE_TIME_PATTERN)){
                timeBW = item.getTime();
            }
            // 过滤并网后十分钟
            if (filter == 0 && getTimeDiff(item.getTime(), timeBW) <= minute){
                filter = 1;
            }

            item.setFilter(filter);
        }

        return list;
    }


    /**
     * 过滤停机前几分钟内数据
     * @param list
     * @param minute  分钟
     * @return
     */
    public static List<PowerPointData> dataProcessTJQ(List<PowerPointData> list, int minute){
        String timeBW = DateUtil.format(new Date(0), DATE_TIME_PATTERN);
        List<PowerPointData> tempei = new ArrayList<>();

        //TODO 数据过滤  // 0正常，1过滤掉
        for (PowerPointData item : list) {
            int filter = 0;
            int fjstatus = 0;
            if (timeBW == DateUtil.format(new Date(0), DATE_TIME_PATTERN)){
                timeBW = item.getTime();
            }
            if (filter == 0 && item.getMxzt() != 2) {
                filter = 1;
                fjstatus = 1;
                timeBW = item.getTime();
            } else {
                filter = 0;
                fjstatus = 0;
            }
            // 过滤停机前十分钟
            if (fjstatus == 0) {
                if (tempei.size() > 0) {
                    if (getTimeDiff(tempei.get(0).getTime(), timeBW) >= minute) {
                        tempei.remove(0);
                    }
                }
                tempei.add(item);
            } else {
                for (PowerPointData temp : tempei) {
                    temp.setFilter(1);
                }
                tempei.clear();
            }

            item.setFilter(filter);
        }

        return list;
    }

    /**
     * 曲线偏差率过滤
     * @param list
     * @param map  风速对应的保证功率
     * @return
     */
    public static List<PowerPointData> dataProcessQXPC(List<PowerPointData> list, Map<Double, Double> map){
        //TODO 数据过滤  // 0正常，1过滤掉
        for (PowerPointData item : list) {
            if (map.containsKey(item.getSpeed())) {
                double power = map.get(item.getSpeed());  //不同风速对应的保证功率
                double maxPower = map.get(24.0);     //最大保证功率
                double k = item.getPower() / power;  //保证功率/实际功率   k:偏差百分比
                if (item.getPower() > 0) {
                    if (k < 0.95 && maxPower <= power) {
                        item.setFilter(1);
                    }
                    if (k < 0.9 && maxPower > power) {
                        item.setFilter(1);
                    }
                    if (k < 0.85 && item.getSpeed() < 6 && item.getSpeed() > 4) {
                        item.setFilter(1);
                    }

                    if (k < 0.9 && item.getSpeed() <= 4 && item.getSpeed() > 3.5) {
                        item.setFilter(1);
                    }
                    if (k < 0.85 && item.getSpeed() <= 3.5 && item.getSpeed() > 3) {
                        item.setFilter(1);
                    }
                    if (k < 0.4 && item.getSpeed() <= 3 && item.getSpeed() > 0) {
                        item.setFilter(1);
                    }
                }
            }
        }

        return list;
    }

    /**
     * 根据欠发过滤
     * @param list
     * @param qfhdj 签发等级
     * @return
     */
    public static List<PowerPointData> dataProcessQF(List<PowerPointData> list, int qfhdj){
        List<PowerPointData> tempqf = new ArrayList<>();
        //TODO 数据过滤  // 0正常，1过滤掉
        for (PowerPointData item : list) {
            int filter = 0;
            //欠发
            if (filter == 0 && item.getSpeed() >= 6 && item.getSpeed() < 12.5 && qfhdj < item.getQfzt()) {
                filter = 1;
                for (PowerPointData temp : tempqf) {
                    temp.setFilter(1);
                }
            } else if (filter == 0 && item.getSpeed() >= 12.5 && qfhdj < 1) {
                filter = 1;
                for (PowerPointData temp : tempqf) {
                    temp.setFilter(1);
                }
            }
            if (tempqf.size() > 0) {
                if (getTimeDiff(tempqf.get(0).getTime(), item.getTime()) >= 5) {
                    tempqf.remove(0);
                }
            }
            tempqf.add(0, item);
            item.setFilter(filter);
        }

        return list;
    }


    public static int getTimeDiff(String oldTime, String newTime) {
        int diff = 0;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long NTime = 0;
        long OTime = 0;
        try {
            NTime = df.parse(newTime).getTime();
            //从对象中拿到时间
            OTime = df.parse(oldTime).getTime();
            diff = (int) (Math.abs((NTime-OTime))/1000/60);
        } catch (ParseException e) {
        }

        return diff;
    }
}
