package com.gyee.frame.service.websocket;


import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.Warning;
import com.gyee.frame.model.auto.WarningClassify;
import com.gyee.frame.model.auto.WindTurbineTestingPointDi;
import com.gyee.frame.model.auto.Windturbine;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.model.custom.ValueVo;
import com.gyee.frame.service.WindTurbineTestingPointDiService;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

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

@Service
public class WtWarnPushService {


    @Resource
    private WindTurbineTestingPointDiService windTurbineTestingPointDiService;


    IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();

    String QS_FDC = "QS_FDC";

    public Map<String, Object> genreSetMap() throws Exception {

        Map<String, Object> map = findWtWarnInfo("SG01_01");

        return map;

    }

    public Map<String, Object> findWtWarnInfo(String id) throws Exception {

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

        if (StringUtils.notEmp(id)) {

            if (InitialRunner.wtmap.containsKey(id)) {

                Windturbine wt = InitialRunner.wtmap.get(id);

                List<Warning> warnls=InitialRunner.warnmap.get(wt.getModelid());

                if (!warnls.isEmpty()) {

                        if(!warnls.isEmpty())
                        {
                            List<String> unicodels = new ArrayList<>();
                            Map<String, Warning> wmap=new HashMap<>();
                            for(Warning wr :warnls)
                            {
                                StringBuilder sb = new StringBuilder();
                                sb.setLength(0);
                                sb.append("DI").append(wr.getEdnavalue());
                                unicodels.add(String.valueOf(sb));
                                //生成统一编码
                                wmap.put(String.valueOf(sb),wr);
                            }

                            Map<String,String> dimap=new HashMap<>();

                            List<WindTurbineTestingPointDi> list=windTurbineTestingPointDiService.getWindTurbineTestingPointDi2List(id,unicodels);
                            if(!list.isEmpty())
                            {
                                List<ValueVo> vos = new ArrayList<>();
                                List<String> pointls = new ArrayList<>();
                                for(WindTurbineTestingPointDi di:list)
                                {

                                    if(null!=di && null!=di.getId())
                                    {
                                        if(dimap.containsKey(di.getUniformcode()))
                                        {
                                            continue;
                                        }
                                        dimap.put(di.getUniformcode(),di.getUniformcode());
                                        pointls.add(di.getId());
                                        ValueVo vo = new ValueVo();
                                        vo.setName(di.getName());
                                        if(wmap.containsKey(di.getUniformcode()))
                                        {
                                            Warning wr=wmap.get(di.getUniformcode());
                                            vo.setTypeId(wr.getWarningclassifyid());
                                            vo.setName(wr.getChinesetext());
                                            vo.setValue(wr.getEnglishtext());
                                            vo.setWarningId(String.valueOf(wr.getEdnavalue()));
                                        }
                                        vos.add(vo);
                                    }

                                }
                                List<PointData> pointlist = realApiUtil.getRealData(pointls);


                                if (!pointlist.isEmpty() && pointlist.size() == pointls.size() && pointlist.size() == vos.size()) {

                                    for (int i = 0; i < pointlist.size(); i++) {

                                        vos.get(i).setData1(pointlist.get(i).getPointValueInDouble());
                                    }
                                }
                                Map<String,List<ValueVo>> typemap=new HashMap<>();
                                for(ValueVo vo:vos)
                                {

                                    if(typemap.containsKey(vo.getTypeId()))
                                    {
                                        List<ValueVo> ls =typemap.get(vo.getTypeId());
                                        ls.add(vo);
                                        typemap.put(vo.getTypeId(), ls);
                                    }else
                                    {
                                        List<ValueVo> ls = new ArrayList<>();
                                        ls.add(vo);
                                        typemap.put(vo.getTypeId(), ls);
                                    }
                                }


                                Set<Map.Entry<String, List<ValueVo>>> set = typemap.entrySet();
                                for (Iterator<Map.Entry<String, List<ValueVo>>> it = set.iterator(); it.hasNext();) {
                                    Map.Entry<String, List<ValueVo>> entry = (Map.Entry<String, List<ValueVo>>) it.next();

                                    if(InitialRunner.warningClassifymap.containsKey(entry.getKey()))
                                    {
                                        WarningClassify classify=InitialRunner.warningClassifymap.get(entry.getKey());
                                        Map<String,Object> tempmap=new HashMap<>();
                                        tempmap.put("id",entry.getKey());
                                        tempmap.put("name",classify.getName());
                                        tempmap.put("vos",entry.getValue());
                                        map.put(entry.getKey(),tempmap);
                                    }
                                  // System.out.println(entry.getKey() + "--->" + entry.getValue());
                                }

                            }

                        }
                    }


                }
            }


        return map;
    }
}