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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.gyee.wisdom.alarm.sharding.entity.*;
import com.gyee.wisdom.alarm.sharding.mapper.ScadafaultMapper;
import com.gyee.wisdom.alarm.sharding.mapper.ShutdownEventMapper;
import com.gyee.wisdom.alarm.sharding.mapper.StopTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service
public class ShutdownEventService  extends ServiceImpl<ShutdownEventMapper, ShutdownEvent> {

    @Autowired
    private ShutdownEventMapper  shutdownEventMapper;

    @Autowired
    private StopTypeMapper stopTypeMapper;


   public List<Map.Entry<String, List<StopTypeCount>>> queryShutdownEventList(String stId, Date startdate, Date edtend) throws Exception{

       List<StopType> stopTypes =   stopTypeMapper.queryStopTypeList("gz");
       List<String> oritypes =  stopTypes.stream().map(StopType::getId).collect(Collectors.toList());
       Map<String, String> maps = stopTypes.stream().collect(Collectors.toMap(StopType::getId, StopType::getName, (key1, key2) -> key2));
       List<ShutdownEvent> list = shutdownEventMapper.queryShutdownEventList(stId,startdate,edtend);
       Map<String, List<ShutdownEvent>> groupbyWindturbineId = list.stream().collect(Collectors.groupingBy(ShutdownEvent::getWindturbineId));
       double hours = 0;
       Map<String, List<StopTypeCount>> resultMap = new TreeMap<>();
       List<StopTypeCount>  arrays = null;
       StopTypeCount count = null;
       for (Map.Entry<String, List<ShutdownEvent>> entry : groupbyWindturbineId.entrySet()) {
           arrays = new ArrayList<>();
           Map<String, List<ShutdownEvent>> groupbyTypes = entry.getValue().stream().collect(Collectors.groupingBy(ShutdownEvent::getStoptypeId));
           for (Map.Entry<String, List<ShutdownEvent>> groupbyType : groupbyTypes.entrySet()) {
               count = new StopTypeCount();
               hours = groupbyType.getValue().stream().mapToDouble(ShutdownEvent::getStopHours).sum();
               count.setStationId(groupbyType.getValue().get(0).getWindPowerStationId());
               count.setWindturbineId(groupbyType.getValue().get(0).getWindturbineId());
               count.setTime(hours);
               count.setCount(groupbyType.getValue().size());
               count.setType(null == maps.get(groupbyType.getKey())? "": maps.get(groupbyType.getKey()));
               count.setTypeID(groupbyType.getKey());
               arrays.add(count);
           }
           resultMap.put(entry.getKey(),arrays);
       }

       for (Map.Entry<String, List<StopTypeCount>> m : resultMap.entrySet()) {

           List<String> types = m.getValue().stream().map(StopTypeCount::getTypeID).collect(Collectors.toList());

           List<String> subList = oritypes.stream().filter(item -> !types.contains(item)).collect(Collectors.toList());
           arrays = new ArrayList<>();
           for (String s:subList) {
               if("gzbmq".equals(s)){
                   continue;
               }
               count = new StopTypeCount();
               count.setStationId(m.getValue().get(0).getStationId());
               count.setWindturbineId(m.getValue().get(0).getWindturbineId());
               count.setTime(0);
               count.setCount(0);
               count.setType(null == maps.get(s)? "": maps.get(s));
               count.setTypeID(s);
               arrays.add(count);
           }
           //合并两个list
           List<StopTypeCount> hb = new ArrayList<>();
           hb.addAll(m.getValue());
           hb.addAll(arrays);
//           List<String> hblist =  hb.stream().map(StopTypeCount::getTypeID).collect(Collectors.toList());
//           List<String> subhblist = hblist.stream().filter(x -> !oritypes.contains(x)).collect(Collectors.toList());
//           if(subhblist.size()>0){
//
//           }

           resultMap.put(m.getKey(),hb);
       }

       //根据风机编号进行排序
       List<Map.Entry<String, List<StopTypeCount>>> list1 = new ArrayList<Map.Entry<String, List<StopTypeCount>>>(resultMap.entrySet());
       Collections.sort(list1, new Comparator<Map.Entry<String, List<StopTypeCount>>>() {
           @Override
           public int compare(Map.Entry<String, List<StopTypeCount>> o1, Map.Entry<String, List<StopTypeCount>> o2) {
               int num1 = Integer.valueOf(o1.getKey().substring(5));
               int num2 = Integer.valueOf(o2.getKey().substring(5));
               int diff = num1 - num2;
               if (diff > 0) {
                   return 1;
               } else if (diff < 0) {
                   return -1;
               }
               return 0; //相等为0
           }
       });
       return list1;
    }


    public Map<String, StopTypeCount> queryShutdownEventCurrentDate(String stId,Date currentDate){
        List<ShutdownEvent> list = shutdownEventMapper.queryShutdownEventCurrentDate(stId,currentDate);

        List<StopType> stopTypes =   stopTypeMapper.queryStopTypeList("gz");
        List<String> oritypes =  stopTypes.stream().map(StopType::getId).collect(Collectors.toList());
        Map<String, String> maps = stopTypes.stream().collect(Collectors.toMap(StopType::getId, StopType::getName, (key1, key2) -> key2));
        Map<String, List<ShutdownEvent>> groupbyTypes = list.stream().collect(Collectors.groupingBy(ShutdownEvent::getStoptypeId));
        double hours = 0;
        Map<String, StopTypeCount> resultMap = new TreeMap<>();
        List <String> types = new ArrayList<>();
        StopTypeCount count = null;
        for (Map.Entry<String, List<ShutdownEvent>> groupbyType : groupbyTypes.entrySet()) {
            count = new StopTypeCount();
            hours = groupbyType.getValue().stream().mapToDouble(ShutdownEvent::getStopHours).sum();
            count.setTime(hours);
            count.setCount(groupbyType.getValue().size());
            count.setType(null == maps.get(groupbyType.getKey())? "": maps.get(groupbyType.getKey()));
            count.setTypeID(groupbyType.getKey());
            resultMap.put(groupbyType.getKey(),count);
            types.add(groupbyType.getKey());
        }

        List<String> subList = oritypes.stream().filter(item -> !types.contains(item)).collect(Collectors.toList());

        for (String s:subList) {
            if("gzbmq".equals(s)){
                continue;
            }
            count = new StopTypeCount();
            count.setTime(0);
            count.setCount(0);
            count.setType(null == maps.get(s)? "": maps.get(s));
            count.setTypeID(s);
            resultMap.put(s,count);
        }
        return resultMap;
    }

}
