package com.gyee.runeconomy.service.monitor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.runeconomy.dto.EquipmentJzVo;
import com.gyee.runeconomy.dto.EquipmentZcVo;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.*;
import com.gyee.runeconomy.service.auto.IProBasicPowerPointService;
import com.gyee.runeconomy.service.auto.impl.ProBasicEquipmentPointServiceImpl;
import com.gyee.runeconomy.service.auto.impl.ProBasicPowerstationPointServiceImpl;
import com.gyee.runeconomy.service.auto.impl.ProBasicSquareServiceImpl;
import com.gyee.runeconomy.util.MathUtil;
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.*;
import java.util.stream.Collectors;

@Service
public class PhotovoltaicmatrixService {
    @Resource
    private IEdosUtil edosUtil;

    @Resource
    private ProBasicEquipmentPointServiceImpl proBasicEquipmentPointService;

    @Resource
    private ProBasicPowerstationPointServiceImpl proBasicPowerstationPointService;
    @Resource
    private ProBasicSquareServiceImpl proBasicSquareService;


    @Resource
    private IProBasicPowerPointService powerPointService;

    public Map getcockpit(String wpId, String type) throws Exception {
        Map<String, Object> map = new HashMap<>();
        List<ProBasicPowerstation> wpls = CacheContext.wplsG;
        List<ProBasicEquipment> wtls = CacheContext.wtls;
        //        List<ProBasicSquare> sqls = CacheContext.sqls;

        //        Page<ProBasicSquare> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        Page<ProBasicSquare> page = new Page<>();
        QueryWrapper<ProBasicSquare> qw = new QueryWrapper<>();
        if (type.equals("JZ")) {
            qw.lambda().eq(ProBasicSquare::getTypes, "JZ");
        } else {
            qw.lambda().eq(ProBasicSquare::getTypes, "ZC");
        }
        qw.lambda().eq(ProBasicSquare::getWindpowerstationId, wpId);
        List<ProBasicSquare> sqls = proBasicSquareService.getBaseMapper().selectList(qw);

        //        List<ProBasicSquare> sqls = ProBasicLine.getRecords();

        List<String> gfztls = new ArrayList<>();
        Map<String, Double> mxztmap = new LinkedHashMap<>();
        Map<String, Object> qtsjmap = new LinkedHashMap<>();


        Map<String, List> zcmap = new LinkedHashMap<>();
        List<EquipmentZcVo> zclist;

        double jzjldl = 0.0;
        double jzjldy = 0.0;
        double jzmxzt = 0.0;

        double ssgl = 0.0;

        double zcjldl = 0.0;
        double zcjldy = 0.0;
        double zcmxzt = 0.0;


        double gzqd = 0.0;
        double ycgl = 0.0;
        double bzgl = 0.0;
        double sjgl = 0.0;

        double totalRfdl = 0;
        double totalZfdl = 0;

        //待风
        double gf_dfnum = 0;
        //手动停机
        double gf_sdtjnum = 0;
        //正常发电
        double gf_zcfdnum = 0;
        //降出力运行
        double gf_jclyxnum = 0;
        //限电降出力
        double gf_xdjclnum = 0;
        //停机
        double gf_tjnum = 0;
        //故障停机
        double gf_gztjnum = 0;
        //故障场内受累
        double gf_gzcnslnum = 0;
        //检修停机
        double gf_jxtjnum = 0;
        //检修场内受累
        double gf_jxclslnum = 0;
        //受累电网
        double gf_sldwnum = 0;
        //受累环境
        double gf_slhjnum = 0;
        //离线
        double gf_lxnum = 0;
        //未知
        double gf_wznum = 0;
        Map<Object, Object> jzmap = new LinkedHashMap<>();
        for (ProBasicPowerstation wp : wpls) {
            if (wp.getId().contains(wpId)) {
                List<ProBasicEquipment> sbls = wtls.stream().filter(w -> w.getWindpowerstationId().equals(wpId)).collect(Collectors.toList());

                if (type.equals("JZ")) {


                    for (ProBasicSquare jz : sqls) {
                        List<EquipmentJzVo> jzlisttemp = new ArrayList<>();
                        for (ProBasicEquipment wt : sbls) {
                            if (jz.getId().equals(wt.getSquareId())) {
                                EquipmentJzVo vo = new EquipmentJzVo();
                                ProBasicEquipmentPoint jldlvalue = proBasicEquipmentPointService.getEquipmentPoint(wt.getId(), ContantXk.JLDL);
                                jzjldl = edosUtil.getRealData(jldlvalue).getPointValueInDouble();
                                ProBasicEquipmentPoint jldyvalue = proBasicEquipmentPointService.getEquipmentPoint(wt.getId(), ContantXk.JLDY);
                                jzjldy = edosUtil.getRealData(jldyvalue).getPointValueInDouble();
                                ProBasicEquipmentPoint ssglvalue = proBasicEquipmentPointService.getEquipmentPoint(wt.getId(), ContantXk.CJ_SSGL);
                                ssgl = edosUtil.getRealData(ssglvalue).getPointValueInDouble();
                                ProBasicEquipmentPoint mxztvalue = proBasicEquipmentPointService.getEquipmentPoint(wt.getId(), ContantXk.MXZT);
                                jzmxzt = edosUtil.getRealData(mxztvalue).getPointValueInDouble();

                                vo.setName(wt.getAname());
                                vo.setId(wt.getId());
                                vo.setJldl(MathUtil.twoBit(jzjldl));
                                vo.setJldy(MathUtil.twoBit(jzjldy));
                                vo.setSsgl(MathUtil.twoBit(ssgl));
                                vo.setYxzt(MathUtil.twoBit(jzmxzt));
                                jzlisttemp.add(vo);
                            }
                        }
                        jzmap.put(jz.getAname(), jzlisttemp);
                    }

                    qtsjmap.put("zjsl", wp.getJrquantityJz() + wp.getJrquantityZc());
                    qtsjmap.put("czmc", wp.getName());
                    qtsjmap.put("czid", wp.getId());
                    map.put("jzzqsl", sqls.size());
                    map.put("ZQ", jzmap);
                }
                if (type.equals("ZC")) {
                    zcmap = new LinkedHashMap<>();
                    for (ProBasicSquare zc : sqls) {
                        zclist = new ArrayList<>();
                        for (ProBasicEquipment wt : sbls) {
                            if (wt.getSquareId().equals(zc.getId())) {
                                EquipmentZcVo vo = new EquipmentZcVo();
                                ProBasicEquipmentPoint zcjldlvalue = proBasicEquipmentPointService.getEquipmentPoint(wt.getId(), ContantXk.JLDL);
                                zcjldl = edosUtil.getRealData(zcjldlvalue).getPointValueInDouble();

                                ProBasicEquipmentPoint zcjldyvalue = proBasicEquipmentPointService.getEquipmentPoint(wt.getId(), ContantXk.JLDY);
                                zcjldy = edosUtil.getRealData(zcjldyvalue).getPointValueInDouble();

                                ProBasicEquipmentPoint ssglvalue = proBasicEquipmentPointService.getEquipmentPoint(wt.getId(), ContantXk.CJ_SSGL);
                                ssgl = edosUtil.getRealData(ssglvalue).getPointValueInDouble();

                                ProBasicEquipmentPoint mxztvalue = proBasicEquipmentPointService.getEquipmentPoint(wt.getId(), ContantXk.MXZT);
                                zcmxzt = edosUtil.getRealData(mxztvalue).getPointValueInDouble();

                                vo.setName(wt.getAname());
                                vo.setId(wt.getId());
                                vo.setJldl(MathUtil.twoBit(zcjldl));
                                vo.setJldy(MathUtil.twoBit(zcjldy));
                                vo.setSsgl(MathUtil.twoBit(ssgl));
                                vo.setYxzt(MathUtil.twoBit(zcmxzt));
                                zclist.add(vo);
                                //                                totalRfdl += zcrfdl;
                                //                                totalZfdl += zfdl;
                                map.put("model", wt.getModelId());
                            }
                        }
                        zcmap.put(zc.getAname(), zclist);
                    }
                    qtsjmap.put("zjsl", wp.getJrquantityJz() + wp.getJrquantityZc());
                    qtsjmap.put("czmc", wp.getName());
                    qtsjmap.put("czid", wp.getId());
                    map.put("zczqsl", sqls.size());
                    //                    map.put("rfdlzs", totalRfdl);
                    //                    map.put("zfdlzs", totalZfdl);
                    map.put("ZQ", zcmap);
                }
            }
        }
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.GZTSMX).getNemCode());    //故障台数
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.CNSLGZTSMX).getNemCode()); //场内受累故障台数
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.JXTSMX).getNemCode());    //检修台数
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.CNSLJXTSMX).getNemCode());//场内受累检修台数
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.DJTSMX).getNemCode());//待机台数
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.QXJCLTSMX).getNemCode());//缺陷降出力台数
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SDTJTSMX).getNemCode());//手动停机台数
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.XNTSMX).getNemCode());//性能台数
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.XDTJTSMX).getNemCode());//限电停机台数
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.XDJCLTSMX).getNemCode());//限电降出力台数
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.CWSLDWTSMX).getNemCode());//场外受累电网台数
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.CWSLTQTSMX).getNemCode());//场外受累天气台数
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.LXTSMX).getNemCode());//离线台数
        gfztls.add(proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.TXZDTSMX).getNemCode());//通讯中断台数

        ProBasicPowerstationPoint gzqdvalue = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.GCGZQD);
        gzqd = edosUtil.getRealData(gzqdvalue).getPointValueInDouble();
        ProBasicPowerstationPoint bzglvalue = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSZBZGL);
        bzgl = edosUtil.getRealData(bzglvalue).getPointValueInDouble();
        //        ProBasicPowerstationPoint ycglvalue = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSZNHGLZS);
        //        ycgl = edosUtil.getRealData(ycglvalue).getPointValueInDouble();


        //预测功率取值
        List<ProBasicPowerstation> collect = CacheContext.wpls.stream().filter(cc -> wpId.equals(cc.getId())).collect(Collectors.toList());
        QueryWrapper<ProBasicPowerPoint> qw1 = new QueryWrapper<>();
        List<ProBasicPowerPoint> powerls = new ArrayList<>();
        List<ProBasicPowerPoint> qyls = null;
        List<ProBasicProject> qcls = null;
        powerls = powerPointService.getBaseMapper().selectList(qw1);

        qyls = powerls.stream().filter(c -> c.getWindpowerstationId().equals(collect.get(0).getId())).collect(Collectors.toList());

        if (qyls.isEmpty() || qyls.size() <= 0) {
            qcls = CacheContext.pjls.stream().filter(p -> collect.get(0).getId().equals(p.getWindpowerstationId())).collect(Collectors.toList());
            for (ProBasicProject p : qcls) {
                for (ProBasicPowerPoint a : powerls) {
                    if (a.getWindpowerstationId().equals(p.getId())) {
                        qyls.add(a);
                    }
                }
            }
        }

        ProBasicPowerstationPoint ycglcd = null;

        double sum = 0.0;
        for (ProBasicPowerPoint pp : qyls) {

            if (collect.get(0).getId().contains("MLJ_FDC_STA")) continue;
            ycglcd = proBasicPowerstationPointService.getPowerstationPoint(pp.getWindpowerstationId(), ContantXk.GLYC);

            double aDouble = 0.0;
            try {
                aDouble = edosUtil.getHistMatrix(ycglcd.getNemCode(), new Date().getTime() / 1000).getPointValueInDouble();
                ycgl = (aDouble + sum);
                sum += aDouble;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        ProBasicPowerstationPoint sjglvalue = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSZGL);
        sjgl = edosUtil.getRealData(sjglvalue).getPointValueInDouble();

        qtsjmap.put("gzqd", MathUtil.twoBit(gzqd));
        qtsjmap.put("bzgl", MathUtil.twoBit(bzgl));
        qtsjmap.put("ycgl", MathUtil.twoBit(ycgl));
        qtsjmap.put("sjgl", MathUtil.twoBit(sjgl / 1000));
        map.put("qtsj", qtsjmap);
        gfztls = gfztls.stream().filter(a -> a != null).collect(Collectors.toList());
        if (StringUtils.isNotEmpty(gfztls)) {
            List<PointData> gfztlist = edosUtil.getRealData(gfztls);

            gf_gztjnum += gfztlist.get(0).getPointValueInDouble();
            gf_gzcnslnum += gfztlist.get(1).getPointValueInDouble();
            gf_jxtjnum += gfztlist.get(2).getPointValueInDouble();
            gf_jxclslnum += gfztlist.get(3).getPointValueInDouble();
            gf_dfnum += gfztlist.get(4).getPointValueInDouble();
            gf_jclyxnum += gfztlist.get(5).getPointValueInDouble();
            gf_sdtjnum += gfztlist.get(6).getPointValueInDouble();
            gf_zcfdnum += gfztlist.get(7).getPointValueInDouble();
            gf_tjnum += gfztlist.get(8).getPointValueInDouble();
            gf_xdjclnum += gfztlist.get(9).getPointValueInDouble();
            gf_sldwnum += gfztlist.get(10).getPointValueInDouble();
            gf_slhjnum += gfztlist.get(11).getPointValueInDouble();
            gf_lxnum += gfztlist.get(12).getPointValueInDouble();
            gf_wznum += gfztlist.get(13).getPointValueInDouble();
        }
        mxztmap.put("gf_dfnum", gf_dfnum);
        mxztmap.put("gf_sdtjnum", gf_sdtjnum);
        mxztmap.put("gf_zcfdnum", gf_zcfdnum);
        mxztmap.put("gf_jclyxnum", gf_jclyxnum);
        mxztmap.put("gf_gztjnum", gf_gztjnum);
        mxztmap.put("gf_gzcnslnum", gf_gzcnslnum);
        mxztmap.put("gf_jxtjnum", gf_jxtjnum);
        mxztmap.put("gf_jxclslnum", gf_jxclslnum);
        mxztmap.put("gf_xdjclnum", gf_xdjclnum);
        mxztmap.put("gf_tjnum", gf_tjnum);
        mxztmap.put("gf_sldwnum", gf_sldwnum);
        mxztmap.put("gf_slhjnum", gf_slhjnum);
        mxztmap.put("gf_wznum", gf_wznum);
        mxztmap.put("gf_lxnum", gf_lxnum);

        mxztmap.put("gf_djnum", gf_dfnum + gf_sdtjnum);
        mxztmap.put("gf_yxnum", gf_zcfdnum + gf_jclyxnum);
        mxztmap.put("gf_gznum", gf_gzcnslnum + gf_gztjnum);
        mxztmap.put("gf_jxnum", gf_jxtjnum + gf_jxclslnum);
        mxztmap.put("gf_xdnum", gf_xdjclnum + gf_tjnum);
        mxztmap.put("gf_slnum", gf_slhjnum + gf_sldwnum);
        mxztmap.put("gf_lxyznum", gf_wznum + gf_lxnum);

        map.put("mxzt", mxztmap);
        return map;
    }
}
