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

import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.vo.monitor.AgcVo;
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.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.stream.Collectors;

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

    @Resource
    private TokenService tokenService;

    public List<AgcVo> monitor(String company, String type) {

        List<AgcVo> resultList = new ArrayList<>();


        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        List<ProBasicSubStation> subwpls = tokenService.getSubwpls(request);
        Map<String, ProBasicPowerstation> wpmap = CacheContext.wpmap;
        Map<String, Map<String, ProBasicPowerstationPoint>> subwppointmap = CacheContext.subwppointmap;
        if (company.endsWith("ZGS")) {
            subwpls = subwpls.stream().filter(sub -> wpmap.get(sub.getWindpowerstationId()).getCompanyId().equals(company)).collect(Collectors.toList());
        }
        if (type.equals("-1")) {
            subwpls = subwpls.stream().filter(sub -> wpmap.get(sub.getWindpowerstationId()).getWindType().equals("-1")).collect(Collectors.toList());
        } else if (type.equals("-2")) {
            subwpls = subwpls.stream().filter(sub -> wpmap.get(sub.getWindpowerstationId()).getWindType().equals("-2")).collect(Collectors.toList());
        }
        subwpls.stream().forEach(sub -> {
            List<String> points = new ArrayList<>();
            Map<String, ProBasicPowerstationPoint> powerstationPointMap = subwppointmap.get(sub.getId());
            points.add(powerstationPointMap.get(ContantXk.TPOINT_WP_CXGL).getNemCode());
            points.add(powerstationPointMap.get(ContantXk.TPOINT_WP_AGC).getNemCode());
            points.add(powerstationPointMap.get(ContantXk.TPOINT_WP_YGSX).getNemCode());
            points.add(powerstationPointMap.get(ContantXk.TPOINT_WP_YGXX).getNemCode());
            points.add(powerstationPointMap.get(ContantXk.TPOINT_WP_YF).getNemCode());
            points.add(powerstationPointMap.get(ContantXk.TPOINT_WP_TT).getNemCode());
            points.add(powerstationPointMap.get(ContantXk.TPOINT_WP_JBS).getNemCode());
            points.add(powerstationPointMap.get(ContantXk.TPOINT_WP_ZBS).getNemCode());
            points.add(powerstationPointMap.get(ContantXk.TPOINT_WP_XDZT).getNemCode());
            points.add(powerstationPointMap.get(ContantXk.TPOINT_WP_ZSGL).getNemCode());
            points.add(powerstationPointMap.get(ContantXk.TPOINT_WP_YCGL).getNemCode());
            List<PointData> realData = new ArrayList<>();
            try {
                realData = edosUtil.getRealData(points);
            } catch (Exception e) {
                e.printStackTrace();
            }
            AgcVo vo = new AgcVo();
            vo.setWpid(sub.getId());
            vo.setWpname(sub.getAname());
            vo.setAgc001(realData.get(0).getPointValueInDouble());
            vo.setAgc002(realData.get(1).getPointValueInDouble());
            vo.setAgc003(realData.get(2).getPointValueInDouble());
            vo.setAgc004(realData.get(3).getPointValueInDouble());
            vo.setAgc005(realData.get(4).getPointValueInDouble());
            vo.setAgc006(realData.get(5).getPointValueInDouble());
            vo.setAgc007(realData.get(6).getPointValueInDouble());
            vo.setAgc008(realData.get(7).getPointValueInDouble());
            vo.setAgc009(realData.get(8).getPointValueInDouble());
            vo.setAgc010(realData.get(9).getPointValueInDouble());
            vo.setYcgl(realData.get(10).getPointValueInDouble());
            resultList.add(vo);
        });

        return resultList;
    }
}
