package com.gyee.runeconomy.service.monitor;

/*
@author   谢生杰
@date   2023/5/10-16:09
*/

import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.util.ClassUtil;
import com.gyee.common.util.DoubleUtils;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.ProBasicBranch;
import com.gyee.runeconomy.model.auto.ProBasicEquipment;
import com.gyee.runeconomy.model.auto.ProBasicEquipmentPoint;
import com.gyee.runeconomy.model.auto.ProBasicSquare;
import com.gyee.runeconomy.model.vo.InverVo;
import com.gyee.runeconomy.model.vo.LandformsVo;
import com.gyee.runeconomy.util.StringUtils;
import com.gyee.runeconomy.util.realtimesource.IEdosUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LandformsService {


    @Resource
    private IEdosUtil edosUtil;

    public List<LandformsVo> data(String foreignkeyid) throws Exception {
        List<LandformsVo> resultList = new ArrayList<>();
        Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;

        Map<String, List<ProBasicEquipment>> pjwtmap = CacheContext.pjwtmap;

        Map<String, List<ProBasicEquipment>> sqwtmap = CacheContext.sqwtmap;
        List<ProBasicEquipment> equipments = null;

        if (foreignkeyid.endsWith("AG")) {
            equipments = sqwtmap.get(foreignkeyid);
            if (StringUtils.isNotEmpty(equipments)) {
                List<String> wtstring = new ArrayList<>();
                equipments.stream().forEach(wt -> {
                    Map<String, ProBasicEquipmentPoint> equipmentPointMap = wtpAimap.get(wt.getId());
                    //                    ProBasicEquipmentPoint powerPoint = equipmentPointMap.get(ContantXk.CJ_SSGL);
                    //                    ProBasicEquipmentPoint speedPoint = equipmentPointMap.get(ContantXk.CJ_SSFS);
                    ProBasicEquipmentPoint sbztPoint = equipmentPointMap.get(ContantXk.MXZT);
                    //                    wtstring.add(powerPoint.getNemCode());
                    //                    wtstring.add(speedPoint.getNemCode());
                    wtstring.add(sbztPoint.getNemCode());
                });
                List<PointData> realData = edosUtil.getRealData(wtstring);
                int index = 1;
                for (int i = 0; i < equipments.size(); i++) {
                    LandformsVo vo = new LandformsVo();
                    vo.setForeignkeyid(foreignkeyid);
                    vo.setWtid(equipments.get(i).getId());
                    vo.setWtname(equipments.get(i).getAname());
                    //                    vo.setPower(DoubleUtils.keepPrecision(realData.get(i*index).getPointValueInDouble(),2));
                    //                    vo.setSpeed(DoubleUtils.keepPrecision(realData.get(i*index+1).getPointValueInDouble(),2));
                    vo.setStatus((int) realData.get(i * index).getPointValueInDouble());
                    resultList.add(vo);
                }
            }
        } else if (foreignkeyid.endsWith("EG")) {
            equipments = pjwtmap.get(foreignkeyid);
            if (StringUtils.isNotEmpty(equipments)) {
                List<String> wtstring = new ArrayList<>();
                equipments.stream().forEach(wt -> {
                    Map<String, ProBasicEquipmentPoint> equipmentPointMap = wtpAimap.get(wt.getId());
                    ProBasicEquipmentPoint powerPoint = equipmentPointMap.get(ContantXk.CJ_SSGL);
                    ProBasicEquipmentPoint speedPoint = equipmentPointMap.get(ContantXk.CJ_SSFS);
                    ProBasicEquipmentPoint sbztPoint = equipmentPointMap.get(ContantXk.MXZT);
                    wtstring.add(powerPoint.getNemCode());
                    wtstring.add(speedPoint.getNemCode());
                    wtstring.add(sbztPoint.getNemCode());
                });
                List<PointData> realData = edosUtil.getRealData(wtstring);
                int index = 3;
                for (int i = 0; i < equipments.size(); i++) {
                    LandformsVo vo = new LandformsVo();
                    vo.setForeignkeyid(foreignkeyid);
                    vo.setWtid(equipments.get(i).getId());
                    vo.setWtname(equipments.get(i).getAname());
                    vo.setPower(DoubleUtils.keepPrecision(realData.get(i * index).getPointValueInDouble(), 2));
                    vo.setSpeed(DoubleUtils.keepPrecision(realData.get(i * index + 1).getPointValueInDouble(), 2));
                    vo.setStatus((int) realData.get(i * index + 2).getPointValueInDouble());
                    resultList.add(vo);
                }
            }
        }

        return resultList;

    }

    public List<ProBasicSquare> sqByPj(String pjid) {
        List<ProBasicSquare> sqls = CacheContext.sqls;
        return sqls.stream().filter(s -> s.getProjectId().equals(pjid)).collect(Collectors.toList());
    }

    public List<Object> inverData(String squareid) {

        List<Object> resultList = new ArrayList<>();
        Map<String, Double> modelpower = CacheContext.modelpower;
        Map<String, List<ProBasicEquipment>> sqwtmap = CacheContext.sqwtmap;
        Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;
        //        Map<String, List<ProBasicBranch>> wtbnmap = CacheContext.wtbnmap;
        //        Map<String, List<ProBasicBranch>> wtbnmap = CacheContext.wtbnmap;
        //        Map<String, Map<String, ProBasicEquipmentPoint>> bnAimap = CacheContext.bnAimap;
        List<ProBasicEquipment> equipments = sqwtmap.get(squareid);
        equipments.stream().forEach(wt -> {
            List<String> equipointids = new ArrayList<>();
            Map<String, ProBasicEquipmentPoint> equipmentPointMap = wtpAimap.get(wt.getId());
            ProBasicEquipmentPoint powerPoint = equipmentPointMap.get(ContantXk.CJ_SSGL);
            equipointids.add(powerPoint.getNemCode());
            //            List<ProBasicBranch> branches = wtbnmap.get(wt.getId());

            //            branches.stream().forEach(bn -> {
            //                Map<String, ProBasicEquipmentPoint> pointMap = bnAimap.get(bn.getId());
            //                ProBasicEquipmentPoint zldlPoint = pointMap.get(ContantXk.ZLDL);
            //                equipointids.add(zldlPoint.getNemCode());
            //            });
            List<PointData> realData = null;
            try {
                realData = edosUtil.getRealData(equipointids);
            } catch (Exception e) {
                e.printStackTrace();
            }
            InverVo vo = new InverVo();
            vo.setWtid(wt.getId());
            vo.setWtname(wt.getAname());
            vo.setZjrl(modelpower.get(wt.getId()));
            if (StringUtils.isNotEmpty(realData)) {

                LinkedHashMap addMap = new LinkedHashMap();
                LinkedHashMap addValMap = new LinkedHashMap();
                vo.setPower(DoubleUtils.keepPrecision(realData.get(0).getPointValueInDouble(), 2));
                for (int j = 1; j < realData.size(); j++) {
                    try {
                        addMap.put("PV" + j, Class.forName("java.lang.Double"));
                        addValMap.put("PV" + j, DoubleUtils.keepPrecision(realData.get(j).getPointValueInDouble(), 2));

                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Object o = new ClassUtil().dynamicClass(vo, addMap, addValMap);
                    resultList.add(o);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return resultList;
    }
}
