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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.*;
import com.gyee.runeconomy.model.vo.LineTreeVo;
import com.gyee.runeconomy.model.vo.ProTreeVo;
import com.gyee.runeconomy.model.vo.TreeVo;
import com.gyee.runeconomy.model.vo.WtStatusVo;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceDetailsService {
    @Resource
    private IEdosUtil edosUtil;
    @Resource
    private IProEconTestingPointService proEconTestingPointService;

    public List<ProTreeVo> tree(String wpid) {
        Map<String, Object> proMap = new HashMap<>();
        Map<String, List<ProBasicProject>> wppromap = CacheContext.wppromap;
        Map<String, List<ProBasicLine>> prolinemap = CacheContext.prolinemap;
        Map<String, List<ProBasicEquipment>> lnwtmap = CacheContext.lnwtmap;
        Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;
        List<ProBasicProject> projects = wppromap.get(wpid);
        TreeVo vo = new TreeVo();
        List<ProTreeVo> pjvos = new ArrayList<>();
        projects.stream().forEach(p -> {
            List<ProBasicLine> lines = prolinemap.get(p.getId());
            List<LineTreeVo> lnvos = new ArrayList<>();
            ProTreeVo pjvo = new ProTreeVo();
            pjvo.setPjid(p.getId());
            pjvo.setPjname(p.getAname());
            lines.stream().forEach(line -> {
                LineTreeVo lnvo = new LineTreeVo();
                lnvo.setLnid(line.getId());
                lnvo.setLnname(line.getAname());
                List<ProBasicEquipment> equipments = lnwtmap.get(line.getId());
                List<String> points = new ArrayList<>();
                equipments.stream().forEach(e -> {
                    ProBasicEquipmentPoint proBasicEquipmentPoint = wtpAimap.get(e.getId()).get(ContantXk.SBZT);
                    points.add(proBasicEquipmentPoint.getNemCode());
                });
                List<PointData> statusData = null;
                try {
                    statusData = edosUtil.getRealData(points);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                List<WtStatusVo> wtStatusVos = new ArrayList<>();
                for (int i = 0; i < equipments.size(); i++) {
                    WtStatusVo wtStatusVo = new WtStatusVo();
                    wtStatusVo.setWtid(equipments.get(i).getId());
                    wtStatusVo.setWtname(equipments.get(i).getAname());
                    wtStatusVo.setStatus((int) statusData.get(i).getPointValueInDouble());
                    wtStatusVos.add(wtStatusVo);
                }
                lnvo.setWts(wtStatusVos);
                lnvos.add(lnvo);
            });
            pjvo.setLns(lnvos);
            pjvos.add(pjvo);
        });
        return pjvos;
    }

    public Map<String, Double> info(String wtid) throws Exception {
        Map<String, Double> resultMap = new HashMap<>();
        Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;
        Map<String, ProBasicEquipmentPoint> equipmentPointMap = wtpAimap.get(wtid);
        List<String> codes = new ArrayList<>();
        codes.add(equipmentPointMap.get(ContantXk.RFDL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.CJ_SSFS).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.CJ_FX).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.CJ_SSGL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.CJ_WGGL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.ZSGL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.BZGL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.CJ_SSZS).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.RGZSSDL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.RCNSLGZSSDL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.RJXSSDL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.RCNSLJXSSDL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.RDJSSDL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.RQXJCLSSDL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.RSDTJSSDL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.RXNSSDL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.RXDTJSSDL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.RXDJCLSSDL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.RCWSLDWSSDL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.RCWSLTQSSDL).getNemCode());
        codes.add(equipmentPointMap.get(ContantXk.MXZT).getNemCode());
        List<PointData> realData = edosUtil.getRealData(codes);
        if (StringUtils.isNotEmpty(realData)) {
            resultMap.put("rfdl", realData.get(0).getPointValueInDouble());
            resultMap.put("fs", realData.get(1).getPointValueInDouble());
            resultMap.put("fx", realData.get(2).getPointValueInDouble());
            resultMap.put("ssgl", realData.get(3).getPointValueInDouble());
            resultMap.put("wggl", realData.get(4).getPointValueInDouble());
            resultMap.put("llgl", realData.get(5).getPointValueInDouble());
            resultMap.put("bzgl", realData.get(6).getPointValueInDouble());
            resultMap.put("ylzs", realData.get(7).getPointValueInDouble());
            resultMap.put("gzss", realData.get(8).getPointValueInDouble() + realData.get(9).getPointValueInDouble());
            resultMap.put("jxss", realData.get(10).getPointValueInDouble() + realData.get(11).getPointValueInDouble());
            resultMap.put("xnss", realData.get(12).getPointValueInDouble() + realData.get(13).getPointValueInDouble() + realData.get(14).getPointValueInDouble() + realData.get(15).getPointValueInDouble());
            resultMap.put("xdss", realData.get(16).getPointValueInDouble() + realData.get(17).getPointValueInDouble());
            resultMap.put("slss", realData.get(18).getPointValueInDouble() + realData.get(19).getPointValueInDouble());
            resultMap.put("status", realData.get(20).getPointValueInDouble());
        }

        return resultMap;
    }

    public Map<String, List<ProEconTestingPoint>> getuniformcode(String part, String wtid) {
        Map<String, List<ProEconTestingPoint>> resultMap = new HashMap<>();
        Map<String, ProBasicEquipment> wtmap = CacheContext.wtmap;
        QueryWrapper<ProEconTestingPoint> qw = new QueryWrapper<>();
        qw.lambda().eq(ProEconTestingPoint::getModel, wtmap.get(wtid).getModelId()).eq(ProEconTestingPoint::getLogicalUnitId, part);
        qw.lambda().eq(ProEconTestingPoint::getSpare3,"1");
        List<ProEconTestingPoint> list = proEconTestingPointService.list(qw);
        resultMap.put("AI", list.stream().filter(i -> i.getUniformCode().startsWith("AI")).collect(Collectors.toList()));
        resultMap.put("DI", list.stream().filter(i -> i.getUniformCode().startsWith("DI")).collect(Collectors.toList()));
        return resultMap;
    }
}
