package com.gyee.wisdom.alarm.sharding.service;

import com.gyee.wisdom.alarm.sharding.entity.AlarmStat1;
import com.gyee.wisdom.alarm.sharding.entity.FeatureStat;
import com.gyee.wisdom.alarm.sharding.mapper.FeatureStatMapper;
import com.gyee.wisdom.alarm.sharding.util.SnowflakeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @descrition:液压运行服务类
 * @author:Wanghs
 * @date:2019-11-19
 */
@Service
public class FeatureStatService {

    @Autowired
    private FeatureStatMapper featurePressureStatMapper;

    public List<AlarmStat1> findByCondition(String stationId, Date startTime, Date endTime) {
        List<AlarmStat1> resultList = new ArrayList<>();

        List<FeatureStat> lst = featurePressureStatMapper.searchByCondition(stationId, startTime, endTime);

        //根据统计类型分组  YYBYX,LWYC,ZPH,YPH
        Map<String, List<FeatureStat>> typeGorupMap = lst.stream().collect(Collectors.groupingBy(FeatureStat::getTypeCode));


        // region  存储偏航类统计,将左偏航和右偏航进行合并
        Map<String, List<FeatureStat>> yawStatMap = new HashMap<>();

        for (Map.Entry<String, List<FeatureStat>> entry : typeGorupMap.entrySet()) {

            //如果统计信息不是左偏航和右偏航
            if (!entry.getKey().contains("ZPH") && !entry.getKey().contains("YPH")) {
                //根据风机编号分组
                Map<String, List<FeatureStat>> groupMap = entry.getValue().stream().collect(Collectors.groupingBy(FeatureStat::getWtId));

                for (Map.Entry<String, List<FeatureStat>> item : groupMap.entrySet()) {
                    AlarmStat1 d = new AlarmStat1();
                    if(entry.getKey().equals("JLDZ")){
                        d.setRelateParts("PHXT");
                        d.setRelatePartsText("偏航系统");
                    }else{
                        d.setRelateParts("YYXT");
                        d.setRelatePartsText("液压系统");
                    }

                    if (item.getValue() != null && item.getValue().size() > 0) {
                        FeatureStat stat = item.getValue().get(0);
                        d.setAlertText(stat.getTypeName());
                    } else {
                        if (entry.getKey().equals("YYBYX")) {
                            d.setAlertText("液压泵运行");
                        } else if (entry.getKey().equals("LWYC")) {
                            d.setAlertText("滤网压差");
                        }
                    }

                    double time = 0;
                    int count = 0;
                    time = item.getValue().stream().mapToDouble(FeatureStat::getTotalSeconds).sum();
                    count = item.getValue().stream().mapToInt(FeatureStat::getTotal).sum();
                    d.setCount(count);
                    //总时长以分钟为单位,不到一分钟则记为一分钟
                    double finalTime = Math.ceil(time / 60.0);

                    d.setTime(finalTime);
                    d.setWindturbineId(item.getKey());
                    d.setStationId(stationId);
                    d.setSnapId(SnowflakeGenerator.generateId());
                    resultList.add(d);
                }
            } else {
                if (!yawStatMap.containsKey("PH")) {
                    yawStatMap.put("PH", entry.getValue());
                } else {
                    List<FeatureStat> yawList = yawStatMap.get("PH");
                    yawList.addAll(entry.getValue());
                }
            }
        }
     //endregion
        //针对偏航类信息再次进行整合并将结果合并到resultList

        for (Map.Entry<String, List<FeatureStat>> entry : yawStatMap.entrySet()) {
            //根据风机编号进行分组
            Map<String, List<FeatureStat>> groupMapByWindturbineId = entry.getValue().stream().collect(Collectors.groupingBy(FeatureStat::getWtId));
            for (Map.Entry<String, List<FeatureStat>> item : groupMapByWindturbineId.entrySet()) {
                AlarmStat1 d = new AlarmStat1();
                d.setRelateParts("PHXT");
                d.setRelatePartsText("偏航系统");
                if (entry.getKey().equals("PH")) {
                    d.setAlertText("偏航");
                }


                double time = 0;
                int count = 0;
                time = item.getValue().stream().mapToDouble(FeatureStat::getTotalSeconds).sum();
                count = item.getValue().stream().mapToInt(FeatureStat::getTotal).sum();
                d.setCount(count);
                //总时长以分钟为单位,不到一分钟则记为一分钟
                double finalTime = Math.ceil(time / 60.0);

                d.setTime(finalTime);
                d.setWindturbineId(item.getKey());
                d.setStationId(stationId);
                d.setSnapId(SnowflakeGenerator.generateId());
                resultList.add(d);
            }

        }

        return resultList;
    }



    public List<AlarmStat1> findByCondition2(String stationId, Date currentDate) {
        List<AlarmStat1> resultList = new ArrayList<>();

        List<FeatureStat> lst = featurePressureStatMapper.searchByCondition2(stationId, currentDate);

        //根据统计类型分组  YYBYX,LWYC,ZPH,YPH
        Map<String, List<FeatureStat>> typeGorupMap = lst.stream().collect(Collectors.groupingBy(FeatureStat::getTypeCode));


        // region  存储偏航类统计,将左偏航和右偏航进行合并
        Map<String, List<FeatureStat>> yawStatMap = new HashMap<>();

        for (Map.Entry<String, List<FeatureStat>> entry : typeGorupMap.entrySet()) {

            //如果统计信息不是左偏航和右偏航
            if (!entry.getKey().contains("ZPH") && !entry.getKey().contains("YPH")) {
                //根据风机编号分组
                Map<String, List<FeatureStat>> groupMap = entry.getValue().stream().collect(Collectors.groupingBy(FeatureStat::getWtId));

                for (Map.Entry<String, List<FeatureStat>> item : groupMap.entrySet()) {
                    AlarmStat1 d = new AlarmStat1();
                    d.setRelateParts("YYXT");
                    d.setRelatePartsText("液压系统");
                    if (item.getValue() != null && item.getValue().size() > 0) {
                        FeatureStat stat = item.getValue().get(0);
                        d.setAlertText(stat.getTypeName());
                    } else {
                        if (entry.getKey().equals("YYBYX")) {
                            d.setAlertText("液压泵运行");
                        } else if (entry.getKey().equals("LWYC")) {
                            d.setAlertText("滤网压差");
                        }
                    }

                    double time = 0;
                    int count = 0;
                    time = item.getValue().stream().mapToDouble(FeatureStat::getTotalSeconds).sum();
                    count = item.getValue().stream().mapToInt(FeatureStat::getTotal).sum();
                    d.setCount(count);
                    //总时长以分钟为单位,不到一分钟则记为一分钟
                    double finalTime = Math.ceil(time / 60.0);

                    d.setTime(finalTime);
                    d.setWindturbineId(item.getKey());
                    d.setStationId(stationId);
                    d.setSnapId(SnowflakeGenerator.generateId());
                    resultList.add(d);
                }
            } else {
                if (!yawStatMap.containsKey("PH")) {
                    yawStatMap.put("PH", entry.getValue());
                } else {
                    List<FeatureStat> yawList = yawStatMap.get("PH");
                    yawList.addAll(entry.getValue());
                }
            }
        }
        //endregion
        //针对偏航类信息再次进行整合并将结果合并到resultList

        for (Map.Entry<String, List<FeatureStat>> entry : yawStatMap.entrySet()) {
            //根据风机编号进行分组
            Map<String, List<FeatureStat>> groupMapByWindturbineId = entry.getValue().stream().collect(Collectors.groupingBy(FeatureStat::getWtId));
            for (Map.Entry<String, List<FeatureStat>> item : groupMapByWindturbineId.entrySet()) {
                AlarmStat1 d = new AlarmStat1();
                d.setRelateParts("PHXT");
                d.setRelatePartsText("偏航系统");
                if (entry.getKey().equals("PH")) {
                    d.setAlertText("偏航");
                }


                double time = 0;
                int count = 0;
                time = item.getValue().stream().mapToDouble(FeatureStat::getTotalSeconds).sum();
                count = item.getValue().stream().mapToInt(FeatureStat::getTotal).sum();
                d.setCount(count);
                //总时长以分钟为单位,不到一分钟则记为一分钟
                double finalTime = Math.ceil(time / 60.0);

                d.setTime(finalTime);
                d.setWindturbineId(item.getKey());
                d.setStationId(stationId);
                d.setSnapId(SnowflakeGenerator.generateId());
                resultList.add(d);
            }

        }

        return resultList;
    }

    public List<AlarmStat1> findAll() {
        List<AlarmStat1> resultList = new ArrayList<>();

        List<FeatureStat> lst = featurePressureStatMapper.searchAll();

        for (FeatureStat data : lst) {
            AlarmStat1 d = new AlarmStat1();
            d.setAlertText("液压泵运行");
            d.setCount(data.getTotal());
            d.setTime(data.getTotalSeconds());
            d.setStationId(data.getStationId());
            d.setWindturbineId(data.getWtId());
            d.setSnapId(data.getId());

            resultList.add(d);
        }

        return resultList;
    }


}
