package com.gyee.frame.service.health;

import com.gyee.frame.common.spring.Constant;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.Project;
import com.gyee.frame.model.auto.WindPowerStationTestingPoint;
import com.gyee.frame.model.auto.Windpowerstation;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.service.*;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HealthOperationService {

    @Resource
    private WindTurbineTestingPointAiService windTurbineTestingPointAiService;
    @Resource
    private WeatherfhService weatherfhService;
    @Resource
    private WeatherfdService weatherfdService;
    @Resource
    private ShutdowneventService shutdowneventService;
    @Resource
    private WindPowerstationTestingPoint2Service windPowerstationTestingPoint2Service;

    private final String QS = "0";

    private final int digit = 2;
    private final double NUM = 3.6;
    private final double XZNUM = 1.25;
    IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();

    /**
     * 获得全省各种状态数量
     * @return
     * @throws Exception
     */
    public Map<String, Integer> countWpNumStatus() throws Exception {

        Map<String, Integer> mapstatus = new HashMap<String, Integer>();

        List<String> idls=new ArrayList<>();

        WindPowerStationTestingPoint point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(QS, Constant.FJZCSL);
        idls.add(point.getCode());// 合格(正常)
        point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(QS, Constant.FJLHSL);
        idls.add(point.getCode());// 良好
        point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(QS, Constant.FJZYSL);
        idls.add(point.getCode());// 注意
        point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(QS, Constant.FJYZSL);
        idls.add(point.getCode());// 严重


        List<PointData> pols=realApiUtil.getRealData(idls);

        if(!pols.isEmpty() && pols.size()==idls.size())
        {
            Integer zc = ((Double) pols.get(0).getPointValueInDouble()).intValue();
            Integer lh = ((Double) pols.get(1).getPointValueInDouble()).intValue();
            Integer zy = ((Double) pols.get(2).getPointValueInDouble()).intValue();
            Integer yz = ((Double) pols.get(3).getPointValueInDouble()).intValue();

            mapstatus.put("zc", zc);
            mapstatus.put("lh", lh);
            mapstatus.put("zy", zy);
            mapstatus.put("yz", yz);
        }


        return mapstatus;
    }

    /**
     * 健康状态占比图表
     * @param type
     * @return
     * @throws Exception
     */
    public Map<String, Object> countWpOrProStatus(String type) throws Exception {

        Map<String, Object> mapstatus = new HashMap<String, Object>();


        List<Integer> hgList = new ArrayList<Integer>();
        List<Integer> lhList = new ArrayList<Integer>();
        List<Integer> zyList = new ArrayList<Integer>();
        List<Integer> yzList = new ArrayList<Integer>();
        List<String> name = new ArrayList<String>();
        if ("1".equals(type)) {
            List<Windpowerstation> wpList = InitialRunner.wpls;
            for (int i = 0; i < wpList.size(); i++) {
                if (wpList.get(i).getCode().indexOf("_FDC") > 0) {
                    List<String> idls=new ArrayList<>();

                    WindPowerStationTestingPoint point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpList.get(i).getId(), Constant.FJZCSL);
                    idls.add(point.getCode());// 合格(正常)
                    point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpList.get(i).getId(), Constant.FJLHSL);
                    idls.add(point.getCode());// 良好
                    point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpList.get(i).getId(), Constant.FJZYSL);
                    idls.add(point.getCode());// 注意
                    point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpList.get(i).getId(), Constant.FJYZSL);
                    idls.add(point.getCode());// 严重

                    List<PointData> pols=realApiUtil.getRealData(idls);

                    if(!pols.isEmpty() && pols.size()==idls.size())
                    {
                        Integer zc = ((Double) pols.get(0).getPointValueInDouble()).intValue();
                        Integer lh = ((Double) pols.get(1).getPointValueInDouble()).intValue();
                        Integer zy = ((Double) pols.get(2).getPointValueInDouble()).intValue();
                        Integer yz = ((Double) pols.get(3).getPointValueInDouble()).intValue();

                        hgList.add(zc);
                        lhList.add(lh);
                        zyList.add(zy);
                        yzList.add(yz);

                    }

                    name.add(wpList.get(i).getName());
                }
            }
            mapstatus.put("hgList", hgList);
            mapstatus.put("lhList", lhList);
            mapstatus.put("zyList", zyList);
            mapstatus.put("yzList", yzList);
            mapstatus.put("name", name);

        } else if ("2".equals(type)) {
            List<Project> pList = InitialRunner.pjls;
            for (int i = 0; i < pList.size(); i++) {
                if (pList.get(i).getWindpowerstationid().indexOf("_FDC") > 0) {
                    List<String> idls=new ArrayList<>();

                    WindPowerStationTestingPoint point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(pList.get(i).getId(), Constant.FJZCSL);
                    idls.add(point.getCode());// 合格(正常)
                    point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(pList.get(i).getId(), Constant.FJLHSL);
                    idls.add(point.getCode());// 良好
                    point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(pList.get(i).getId(), Constant.FJZYSL);
                    idls.add(point.getCode());// 注意
                    point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(pList.get(i).getId(), Constant.FJYZSL);
                    idls.add(point.getCode());// 严重

                    List<PointData> pols=realApiUtil.getRealData(idls);

                    if(!pols.isEmpty() && pols.size()==idls.size())
                    {
                        Integer zc = ((Double) pols.get(0).getPointValueInDouble()).intValue();
                        Integer lh = ((Double) pols.get(1).getPointValueInDouble()).intValue();
                        Integer zy = ((Double) pols.get(2).getPointValueInDouble()).intValue();
                        Integer yz = ((Double) pols.get(3).getPointValueInDouble()).intValue();

                        hgList.add(zc);
                        lhList.add(lh);
                        zyList.add(zy);
                        yzList.add(yz);

                    }
                    name.add(pList.get(i).getName());
                }
            }
            mapstatus.put("hgList", hgList);
            mapstatus.put("lhList", lhList);
            mapstatus.put("zyList", zyList);
            mapstatus.put("yzList", yzList);
            mapstatus.put("name", name);
        }

        return mapstatus;
    }


    public Map<String, Object> findWpOrProStatusForHistory(String type, String status) throws Exception {
        Map<String, Object> mapstatus = new HashMap<String, Object>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        List<Integer> lhList = new ArrayList<Integer>();
        List<Integer> zyList = new ArrayList<Integer>();
        List<Integer> yzList = new ArrayList<Integer>();
        Calendar cl = Calendar.getInstance();
        cl.set(Calendar.HOUR_OF_DAY, 0);
        cl.set(Calendar.MINUTE, 0);
        cl.set(Calendar.SECOND, 0);
        Long start = cl.getTimeInMillis();
        Long end = new Date().getTime();
        List<Map<String,String>> nameList = new ArrayList<Map<String,String>>();
        if ("1".equals(type)) {
            List<Windpowerstation> wpList = InitialRunner.wpls;
            Map<String,String> map=new HashMap<>();
            for (int i = 0; i < wpList.size(); i++) {
                List<Integer> hgList = new ArrayList<Integer>();
                List<String> tList = new ArrayList<String>();

                if (wpList.get(i).getCode().indexOf("_FDC") > 0) {
                    if ("1".equals(status)) {
                        WindPowerStationTestingPoint point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpList.get(i).getId(), Constant.FJZCSL);
                        String hgCode = point.getCode();// 合格(正常)
                         point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpList.get(i).getId(), Constant.FJLHSL);
                        String lhCode = point.getCode();// 良好
                        List<PointData> phgList = realApiUtil.getHistoryDatasSnap(hgCode, start / 1000, end / 1000, 1L,
                                15 * 60L);
                        List<PointData> plhList = realApiUtil.getHistoryDatasSnap(lhCode, start / 1000, end / 1000, 1L,
                                15 * 60L);

                        if (phgList.size() >= plhList.size()) {
                            for (int ii = 0; ii < phgList.size(); ii++) {
                                if (plhList.get(ii) == null) {
                                    Integer total = ((Double) phgList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(phgList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                } else {
                                    Integer total = ((Double) phgList.get(ii).getPointValueInDouble()).intValue()
                                            + ((Double) plhList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(phgList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                }


                            }

                            mapstatus.put(wpList.get(i).getCode(), hgList);
                            mapstatus.put("time", tList);

                        } else {
                            for (int ii = 0; ii < plhList.size(); ii++) {
                                if (phgList.get(ii) == null) {
                                    Integer total = ((Double) plhList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(plhList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                } else {
                                    Integer total = ((Double) plhList.get(ii).getPointValueInDouble()).intValue()
                                            + ((Double) phgList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(plhList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                }

                            }

                            mapstatus.put(wpList.get(i).getCode(), hgList);
                            mapstatus.put("time", tList);

                        }

                    } else {

                        WindPowerStationTestingPoint point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpList.get(i).getId(), Constant.FJZYSL);
                        String zyCode = point.getCode();// 注意
                        point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpList.get(i).getId(), Constant.FJYZSL);
                        String yzCode = point.getCode();// 严重
                        List<PointData> pzyList = realApiUtil.getHistoryDatasSnap(zyCode, start / 1000, end / 1000, 0L,
                                15 * 60L);
                        List<PointData> pyzList = realApiUtil.getHistoryDatasSnap(yzCode, start / 1000, end / 1000, 0L,
                                15 * 60L);

                        if (pzyList.size() >= pyzList.size()) {
                            for (int ii = 0; ii < pzyList.size(); ii++) {
                                if (pyzList.get(ii) == null) {
                                    Integer total = ((Double) pzyList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(pzyList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                } else {
                                    Integer total = ((Double) pzyList.get(ii).getPointValueInDouble()).intValue()
                                            + ((Double) pyzList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(pzyList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                }

                                mapstatus.put(wpList.get(i).getCode(), hgList);
                                mapstatus.put("time", tList);

                            }
                        } else {
                            for (int ii = 0; ii < pyzList.size(); ii++) {
                                if (pzyList.get(ii) == null) {
                                    Integer total = ((Double) pyzList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(pzyList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                } else {
                                    Integer total = ((Double) pyzList.get(ii).getPointValueInDouble()).intValue()
                                            + ((Double) pzyList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(pzyList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                }

                            }

                            mapstatus.put(wpList.get(i).getCode(), hgList);
                            mapstatus.put("time", tList);

                        }

                    }

                }
                map.put(wpList.get(i).getId(),wpList.get(i).getName());


            }
            nameList.add(map);
            mapstatus.put("name", nameList);

        } else if ("2".equals(type)) {
            List<Project> pList = InitialRunner.pjls;
            Map<String,String> map=new HashMap<>();
            for (int i = 0; i < pList.size(); i++) {
                List<Integer> hgList = new ArrayList<Integer>();
                List<String> tList = new ArrayList<String>();

                if (pList.get(i).getWindpowerstationid().indexOf("_FDC") > 0) {
                    if ("1".equals(status)) {
                        WindPowerStationTestingPoint point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(pList.get(i).getId(), Constant.FJZCSL);
                        String hgCode = point.getCode();// 合格(正常)
                        point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(pList.get(i).getId(), Constant.FJLHSL);
                        String lhCode = point.getCode();// 良好
                        List<PointData> phgList = realApiUtil.getHistoryDatasSnap(hgCode, start / 1000, end / 1000, 0L,
                                15 * 60L);
                        List<PointData> plhList = realApiUtil.getHistoryDatasSnap(lhCode, start / 1000, end / 1000, 0L,
                                15 * 60L);

                        if (phgList.size() >= plhList.size()) {
                            for (int ii = 0; ii < phgList.size(); ii++) {
                                if (plhList.get(ii) == null) {
                                    Integer total = ((Double) phgList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(phgList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                } else {
                                    Integer total = ((Double) phgList.get(ii).getPointValueInDouble()).intValue()
                                            + ((Double) plhList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(phgList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                }

                            }

                            mapstatus.put(pList.get(i).getCode(), hgList);
                            mapstatus.put("time", tList);

                        } else {
                            for (int ii = 0; ii < plhList.size(); ii++) {
                                if (phgList.get(ii) == null) {
                                    Integer total = ((Double) plhList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(plhList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                } else {
                                    Integer total = ((Double) plhList.get(ii).getPointValueInDouble()).intValue()
                                            + ((Double) phgList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(plhList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                }

                            }

                            mapstatus.put(pList.get(i).getCode(), hgList);
                            mapstatus.put("time", tList);

                        }

                    } else {
                        WindPowerStationTestingPoint point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(pList.get(i).getId(), Constant.FJZYSL);
                        String zyCode = point.getCode();// 注意
                        point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(pList.get(i).getId(), Constant.FJYZSL);
                        String yzCode = point.getCode();// 严重
                        List<PointData> pzyList = realApiUtil.getHistoryDatasSnap(zyCode, start / 1000, end / 1000, 0L,
                                15 * 60L);
                        List<PointData> pyzList = realApiUtil.getHistoryDatasSnap(yzCode, start / 1000, end / 1000, 0L,
                                15 * 60L);

                        if (pzyList.size() >= pyzList.size()) {
                            for (int ii = 0; ii < pzyList.size(); ii++) {
                                if (pyzList.get(ii) == null) {
                                    Integer total = ((Double) pzyList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(pzyList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                } else {
                                    Integer total = ((Double) pzyList.get(ii).getPointValueInDouble()).intValue()
                                            + ((Double) pyzList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(pzyList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                }
                            }

                            mapstatus.put(pList.get(i).getCode(), hgList);
                            mapstatus.put("time", tList);

                        } else {
                            for (int ii = 0; ii < pyzList.size(); ii++) {
                                if (pzyList.get(ii) == null) {
                                    Integer total = ((Double) pyzList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(pyzList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                } else {
                                    Integer total = ((Double) pyzList.get(ii).getPointValueInDouble()).intValue()
                                            + ((Double) pzyList.get(ii).getPointValueInDouble()).intValue();
                                    String time = sdf.format(new Date(pyzList.get(ii).getPointTime() * 1000));
                                    time = time.substring(time.length() - 5);
                                    hgList.add(total);
                                    tList.add(time);
                                }
                            }

                            mapstatus.put(pList.get(i).getCode(), hgList);
                            mapstatus.put("time", tList);

                        }

                    }

                }
                map.put(pList.get(i).getId(),pList.get(i).getName());


            }
            nameList.add(map);
            mapstatus.put("name", nameList);
        }
        return mapstatus;
    }

}
