package com.gyee.alarm.service.realtimelibrary;/*
@author   谢生杰
@date   2022/11/17-17:45
*/

import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.util.SortUtils;
import com.gyee.common.vo.matrix.MatrixVo;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.ProBasicEquipment;
import com.gyee.alarm.model.auto.ProBasicEquipmentPoint;
import com.gyee.alarm.util.realtimesource.IEdosUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class TestService {
    @Resource
    private IEdosUtil edosUtil;
    public Map<String,Object> matrixDatas()  {
        Map<String,Object> resultMap = new HashMap<>();
        Map<String,Map<String,Object>> wpresultMap = new HashMap<>();

        Map<String, List<MatrixVo>> wtMap = new HashMap<>();


        Map<String, Map<String, ProBasicEquipmentPoint>> wtPointmap = CacheContext.wtpAimap;

        List<ProBasicEquipment> wtls = CacheContext.wtls;
        List<String> synchronizedList = new ArrayList<>();
        Map<String,String> synchronizedMap = new HashMap<>();
        wtls.stream().forEach(wt->{
            ProBasicEquipmentPoint windturbinetestingpointnew = wtPointmap.get(wt.getId()).get(ContantXk.SBZT);
            if (!windturbinetestingpointnew.getNemCode().trim().equals("INITIAL")){
                synchronizedList.add(windturbinetestingpointnew.getNemCode());
            }else{
            }
            synchronizedMap.put(windturbinetestingpointnew.getNemCode(),wt.getId()+"!"+wt.getWindpowerstationId());
        });
        try {
            List<PointData> realData = edosUtil.getRealData(synchronizedList);
            realData.stream().forEach(i -> {
                String[] split = synchronizedMap.get(i.getEdnaId()).split("!");
                MatrixVo vo = new MatrixVo(split[0],i.getPointValueInDouble(),split[1]);
                if (wtMap.containsKey(split[1])){
                    wtMap.get(split[1]).add(vo);
                }else {
                    List<MatrixVo> wtResultList = new ArrayList<>();
                    wtResultList.add(vo);
                    wtMap.put(split[1],wtResultList);
                }
            });
            List<String> pointids = realData.stream().map(i -> i.getEdnaId()).collect(Collectors.toList());
            Set<String> totalpointids = synchronizedMap.keySet();
            Collection notexists=new ArrayList<String>(totalpointids);
            notexists.removeAll(pointids);
            notexists.stream().forEach(n->{
                String[] split = synchronizedMap.get(n).split("!");
                MatrixVo vo = new MatrixVo(split[0],0.0,split[1]);
                if (wtMap.containsKey(split[1])){
                    wtMap.get(split[1]).add(vo);
                }else {
                    List<MatrixVo> wtResultList = new ArrayList<>();
                    wtResultList.add(vo);
                    wtMap.put(split[1],wtResultList);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        Set<String> wpids = wtMap.keySet();
        wpids.stream().forEach(i->{
            SortUtils.sort(wtMap.get(i),"wtid",SortUtils.ASC);
        });
        resultMap.put("wt",wtMap);
        return resultMap;
    }
}
