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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.model.PointData;
import com.gyee.common.vo.monitor.CompreVo;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.*;
import com.gyee.runeconomy.service.TokenService;
import com.gyee.runeconomy.service.auto.IProEconTestingPointService;
import com.gyee.runeconomy.util.StringUtils;
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.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ComprehensiveService {
    @Resource
    private IProEconTestingPointService proEconTestingPointService;
    @Resource
    private IEdosUtil edosUtil;
    @Resource
    private TokenService tokenService;

    public List<ProEconTestingPoint> targets(String type) {
        QueryWrapper<ProEconTestingPoint> qw = new QueryWrapper<>();
        if (type.equals("-1")) {
            qw.select(" DISTINCT uniform_code,name,value_unit ").lambda().eq(ProEconTestingPoint::getTypeId, "fcc").ne(ProEconTestingPoint::getUniformCode, "ZYCGL");
        } else if (type.equals("-2")) {
            qw.select(" DISTINCT uniform_code,name,value_unit ").lambda().eq(ProEconTestingPoint::getTypeId, "gcc").ne(ProEconTestingPoint::getUniformCode, "ZYCGL");
        } else {
            qw.select(" DISTINCT uniform_code,name,value_unit ").lambda().in(ProEconTestingPoint::getTypeId, "gcc", "fcc").ne(ProEconTestingPoint::getUniformCode, "ZYCGL");
        }
        qw.lambda().eq(ProEconTestingPoint::getSpare3, "1");
        List<ProEconTestingPoint> list = proEconTestingPointService.list(qw);
        return list;
    }

    public List<ProEconTestingPoint> wttargets(String type, String part) {
        QueryWrapper<ProEconTestingPoint> qw = new QueryWrapper<>();
        qw.select(" DISTINCT uniform_code,name,value_unit ").lambda().like(ProEconTestingPoint::getUniformCode, "AI");
        if (type.equals("-1")) {
            qw.lambda().eq(ProEconTestingPoint::getTypeId, "F");
        } else if (type.equals("-2")) {
            qw.lambda().eq(ProEconTestingPoint::getTypeId, "G");
        }
        if (StringUtils.isNotEmpty(part)) {
            qw.lambda().eq(ProEconTestingPoint::getLogicalUnitId, part);
        }
        qw.lambda().eq(ProEconTestingPoint::getSpare3, "1");
        List<ProEconTestingPoint> list = proEconTestingPointService.list(qw);


        return list;

    }

    public List<CompreVo> listing(String company, String type, String targets) {

        List<CompreVo> resultList = new ArrayList<>();
        //        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        //        List<ProBasicPowerstation> wpls = tokenService.getWpls(request);
        List<ProBasicPowerstation> wpls = CacheContext.wpls;
        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.getWindType().equals("-1")).collect(Collectors.toList());
        } else if (type.equals("-2")) {
            wpls = wpls.stream().filter(wp -> wp.getWindType().equals("-2")).collect(Collectors.toList());
        }
        String[] targetArray = targets.split(",");

        wpls.stream().forEach(wp -> {
            CompreVo vo = new CompreVo();
            vo.setWpname(wp.getAname());
            vo.setCapacity(wp.getJrCapacity() + wp.getJrwindCapacity());
            List<String> targetList = new ArrayList<>();
            List<String> glList = new ArrayList<>();
            List<String> dlList = new ArrayList<>();
            Map<String, ProBasicPowerstationPoint> powerstationPointMap = wppointmap.get(wp.getId());
            Arrays.stream(targetArray).forEach(t -> {
                if (powerstationPointMap.containsKey(t)) {
                    targetList.add(powerstationPointMap.get(t).getNemCode());
                    if (powerstationPointMap.get(t).getName().contains("功率")) {
                        glList.add(powerstationPointMap.get(t).getNemCode());
                    }
                    if (powerstationPointMap.get(t).getName().contains("电量")) {
                        dlList.add(powerstationPointMap.get(t).getNemCode());
                    }
                }
            });
            List<PointData> pointDataList = null;
            try {
                pointDataList = edosUtil.getRealData(targetList);
                List<String> arr = Arrays.asList(targetArray);
                for (PointData a : pointDataList) {
                    for (String c : glList) {
                        if (a.getPointName().equals(c)) {
                            a.setPointValueInDouble(a.getPointValueInDouble() / 1000);
                        }
                    }
                    for (String c : dlList) {
                        if (a.getPointName().equals(c)) {
                            a.setPointValueInDouble(a.getPointValueInDouble() / 10000);
                        }
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            vo.setDataList(pointDataList);
            resultList.add(vo);
        });
        return resultList;

    }

    public List<CompreVo> wtlisting(String company, String type, String targets, String wpid, String pageSize, String pageNum) {
        List<CompreVo> resultList = new ArrayList<>();
        Map<String, List<ProBasicEquipment>> wpwtmap = CacheContext.wpwtmap;
        Map<String, Double> modelpower = CacheContext.modelpower;
        List<ProBasicEquipment> equipments = wpwtmap.get(wpid);
        Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;
        String[] targetArray = targets.split(",");
        equipments = equipments.stream().skip((Integer.parseInt(pageNum) - 1) * Integer.parseInt(pageSize)).limit(Long.parseLong(pageSize)).collect(Collectors.toList());
        //        for (int i= 0;i<equipments.size();i++){
        equipments.stream().forEach(wt -> {
            CompreVo vo = new CompreVo();
            vo.setWpname(wt.getAname());
            vo.setCapacity(modelpower.get(wt.getId()));
            List<String> targetList = new ArrayList<>();
            Map<String, ProBasicEquipmentPoint> equipmentPointMap = wtpAimap.get(wt.getId());
            Arrays.stream(targetArray).forEach(t -> {
                targetList.add(equipmentPointMap.get(t).getNemCode());
            });
            List<PointData> pointDataList = null;
            try {
                pointDataList = edosUtil.getRealData(targetList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            vo.setDataList(pointDataList);
            resultList.add(vo);
        });
        return resultList;

    }
}
