package com.gyee.runeconomy.service.monitor;/*
@author   谢生杰
@date   2023/4/4-9:53
*/

import com.gyee.common.contant.ContantXk;
import com.gyee.common.util.SortUtils;
import com.gyee.common.vo.monitor.StatusVo;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.ProBasicPowerstation;
import com.gyee.runeconomy.model.auto.ProBasicPowerstationPoint;
import com.gyee.runeconomy.model.auto.ProBasicSubStation;
import com.gyee.runeconomy.model.auto.ProBasicWeatherStation;
import com.gyee.runeconomy.service.TokenService;
import com.gyee.runeconomy.util.realtimesource.IEdosUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Service
public class StatusService {
    @Resource
    private IEdosUtil edosUtil;
    @Resource
    private TokenService tokenService;

    public List<StatusVo> monitor(String company, String type) {
        List<StatusVo> resultList = new ArrayList<>();
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        //        List<ProBasicPowerstation> wpls = tokenService.getWpls(request);
        List<ProBasicPowerstation> wpls = CacheContext.wpls;

        //        List<ProBasicSubStation> subwpls = tokenService.getSubwpls(request);
        List<ProBasicSubStation> subwpls = CacheContext.subwpls;
        //        List<ProBasicWeatherStation> weawpls = CacheContext.weawpls;

        //        List<ProBasicWeatherStation> weawpls = tokenService.getWeawpls(request);
        List<ProBasicWeatherStation> weawpls = CacheContext.weawpls;
        Map<String, Map<String, ProBasicPowerstationPoint>> subwppointmap = CacheContext.subwppointmap;
        Map<String, Map<String, ProBasicPowerstationPoint>> weatherwppointmap = CacheContext.weatherwppointmap;

        Map<String, Map<String, ProBasicPowerstationPoint>> wppointmap = CacheContext.wppointmap;
        if (company.endsWith("ZGS")) {
            wpls = wpls.stream().filter(wp -> wp.getCompanyId().equals(company)).collect(Collectors.toList());
        }
        if (type.equals("-1")) {
            wpls = wpls.stream().filter(wp -> wp.getId().contains("_FDC")).collect(Collectors.toList());
        } else if (type.equals("-2")) {
            wpls = wpls.stream().filter(wp -> wp.getId().contains("_GDC")).collect(Collectors.toList());
        }

        wpls.stream().forEach(wp -> {
            StatusVo vo = new StatusVo();
            vo.setWpname(wp.getAname());
            vo.setType(wp.getWindType());
            vo.setJrts(wp.getJrwindQuantityHs() + wp.getJrwindQuantityLd() + wp.getJrquantityJz() + wp.getJrquantityZc());
            Map<String, ProBasicPowerstationPoint> powerstationPointMap = wppointmap.get(wp.getId());
            ProBasicPowerstationPoint yxtsPoint = powerstationPointMap.get(ContantXk.YXTS);
            ProBasicPowerstationPoint djtsPoint = powerstationPointMap.get(ContantXk.DJTS);
            ProBasicPowerstationPoint gztsPoint = powerstationPointMap.get(ContantXk.GZTJTS);
            ProBasicPowerstationPoint jxtsPoint = powerstationPointMap.get(ContantXk.JXTJTS);
            ProBasicPowerstationPoint xdtsPoint = powerstationPointMap.get(ContantXk.XDTS);
            ProBasicPowerstationPoint sltsPoint = powerstationPointMap.get(ContantXk.SLTS);
            ProBasicPowerstationPoint lxtsPoint = powerstationPointMap.get(ContantXk.TXZDTS);
            ProBasicPowerstationPoint swdlPoint = powerstationPointMap.get(ContantXk.RSWDL);
            ProBasicPowerstationPoint agcPoint = powerstationPointMap.get(ContantXk.AGCJRZT);
            ProBasicPowerstationPoint fjPoint = powerstationPointMap.get(ContantXk.SBJRZT);
            ProBasicPowerstationPoint djlPoint = powerstationPointMap.get(ContantXk.DQJRZT);
            ProBasicPowerstationPoint glPoint = powerstationPointMap.get(ContantXk.GLYCJRZT);
            ProBasicPowerstationPoint qxzPoint = powerstationPointMap.get(ContantXk.QXZJRZT);
            ProBasicPowerstationPoint czPoint = powerstationPointMap.get(ContantXk.CZZT);
            double yxts = 0;
            double djts = 0;
            double gzts = 0;
            double jxts = 0;
            double xdts = 0;
            double slts = 0;
            double lxts = 0;
            double swdl = 0;
            double agc = 0;
            double fj = 0;
            double djl = 0;
            double gl = 0;
            double qxz = 0;
            double qc = 0;

            AtomicReference<Double> agc001 = new AtomicReference<>((double) 0);
            AtomicReference<Double> agc002 = new AtomicReference<>((double) 0);
            AtomicReference<Double> ycgl = new AtomicReference<>((double) 0);
            AtomicReference<Double> fs = new AtomicReference<>((double) 0);
            AtomicReference<Double> fx = new AtomicReference<>((double) 0);
            try {
                yxts = edosUtil.getRealData(yxtsPoint).getPointValueInDouble();
                djts = edosUtil.getRealData(djtsPoint).getPointValueInDouble();
                gzts = edosUtil.getRealData(gztsPoint).getPointValueInDouble();
                jxts = edosUtil.getRealData(jxtsPoint).getPointValueInDouble();
                xdts = edosUtil.getRealData(xdtsPoint).getPointValueInDouble();
                slts = edosUtil.getRealData(sltsPoint).getPointValueInDouble();
                lxts = edosUtil.getRealData(lxtsPoint).getPointValueInDouble();
                swdl = edosUtil.getRealData(swdlPoint).getPointValueInDouble();
                agc = edosUtil.getRealData(agcPoint).getPointValueInDouble();
                fj = edosUtil.getRealData(fjPoint).getPointValueInDouble();
                djl = edosUtil.getRealData(djlPoint).getPointValueInDouble();
                gl = edosUtil.getRealData(glPoint).getPointValueInDouble();
                qxz = edosUtil.getRealData(qxzPoint).getPointValueInDouble();
                qc = edosUtil.getRealData(czPoint).getPointValueInDouble();
            } catch (Exception e) {
                e.printStackTrace();
            }
            vo.setYxts(yxts);
            vo.setDjts(djts);
            vo.setGzts(gzts);
            vo.setJxts(jxts);
            vo.setXdts(xdts);
            vo.setSlts(slts);
            vo.setLxts(lxts);
            vo.setSwdl(swdl);
            vo.setAgcjrzt(agc);
            vo.setDjljrzt(djl);
            vo.setFjjrzt(fj);
            vo.setFggljrzt(gl);
            vo.setQxzjrzt(qxz);
            vo.setQcjrzt(qc);
            List<ProBasicSubStation> subs = subwpls.stream().filter(sub -> sub.getWindpowerstationId().equals(wp.getId())).collect(Collectors.toList());
            subs.stream().forEach(sub -> {
                Map<String, ProBasicPowerstationPoint> pointMap = subwppointmap.get(sub.getId());
                ProBasicPowerstationPoint agc002Point = pointMap.get(ContantXk.TPOINT_WP_AGC);
                ProBasicPowerstationPoint agc001Point = pointMap.get(ContantXk.TPOINT_WP_CXGL);
                ProBasicPowerstationPoint ycglPoint = pointMap.get(ContantXk.TPOINT_WP_YCGL);
                agc001.updateAndGet(v -> {
                    try {
                        return new Double((double) (v + edosUtil.getRealData(agc001Point).getPointValueInDouble()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return v;
                });
                agc002.updateAndGet(v -> {
                    try {
                        return new Double((double) (v + edosUtil.getRealData(agc002Point).getPointValueInDouble()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return v;
                });
                ycgl.updateAndGet(v -> {
                    try {
                        return new Double((double) (v + edosUtil.getRealData(ycglPoint).getPointValueInDouble()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return v;
                });

            });
            vo.setAgc001(agc001.get());
            vo.setAgc002(agc002.get());
            vo.setYcgl(ycgl.get());
            List<ProBasicWeatherStation> weas = weawpls.stream().filter(wea -> wea.getWindpowerstationId().equals(wp.getId())).collect(Collectors.toList());
            weas.stream().forEach(wea -> {
                Map<String, ProBasicPowerstationPoint> pointMap = weatherwppointmap.get(wea.getId());
                ProBasicPowerstationPoint fsPoint = null;
                ProBasicPowerstationPoint fxPoint = null;
                if (wp.getWindType().equals("-1")) {
                    fsPoint = pointMap.get(ContantXk.FCCFTFS10);
                    fxPoint = pointMap.get(ContantXk.FCCFTFX10);
                } else if (wp.getWindType().equals("-2")) {
                    fsPoint = pointMap.get(ContantXk.QXZFS);
                    fxPoint = pointMap.get(ContantXk.QXZFX);
                }
                ProBasicPowerstationPoint finalFsPoint = fsPoint;
                fs.updateAndGet(v -> {
                    try {
                        return new Double((double) (v + edosUtil.getRealData(finalFsPoint).getPointValueInDouble()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return v;
                });
                ProBasicPowerstationPoint finalFxPoint = fxPoint;
                fx.updateAndGet(v -> {
                    try {
                        return new Double((double) (v + edosUtil.getRealData(finalFxPoint).getPointValueInDouble()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return v;
                });

            });
            vo.setQxzfs(fs.get() / weas.size());
            vo.setQxzfx(fx.get() / weas.size());
            vo.setOrderNum(wp.getOrderNum());
            resultList.add(vo);
        });
        SortUtils.sort(resultList, "orderNum", SortUtils.ASC);
        return resultList;
    }
}
