package com.njcn.harmonic.service.impl;

import cn.hutool.core.date.DateUtil;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.common.pojo.enums.common.ServerEnum;
import com.njcn.device.pq.api.GeneralDeviceInfoClient;
import com.njcn.device.pq.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.harmonic.mapper.NormLimitMapper;
import com.njcn.harmonic.pojo.param.HistoryHarmOverLimitParam;
import com.njcn.harmonic.pojo.vo.HistoryHarmOverLimitVO;
import com.njcn.harmonic.service.NormLimitService;
import com.njcn.web.utils.RequestUtil;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 类介绍
 *
 * @author denghuajun
 * @version 1.0.0
 * @date 2022/3/18
 */
@Service
@AllArgsConstructor
public class NormLimitServiceImpl implements NormLimitService {

    private final GeneralDeviceInfoClient generalDeviceInfoClient;

    private final NormLimitMapper normLimitMapper;

    @Override
    public List<HistoryHarmOverLimitVO> getHistoryData(HistoryHarmOverLimitParam historyHarmOverLimitParam, int type) {
        List<HistoryHarmOverLimitVO> historyHarmOverLimitVOList = new ArrayList<>();
        List<HistoryHarmOverLimitVO> historyHarmOverLimitVO;
        List<String> lineIds = new ArrayList<>();
        if (type == 0) {
            //处理接口数据
            for (int i = 0; i < historyHarmOverLimitParam.getLineId().length; i++) {
                lineIds.addAll(Collections.singleton(historyHarmOverLimitParam.getLineId()[i]));
            }
        } else {
            DeviceInfoParam deviceInfoParam = new DeviceInfoParam();
            deviceInfoParam.setDeptIndex(RequestUtil.getDeptIndex());
            deviceInfoParam.setPowerFlag(2);
            deviceInfoParam.setMonitorFlag(2);
            deviceInfoParam.setServerName(ServerEnum.HARMONIC.getName());
            SimpleDTO simpleDTO = new SimpleDTO();
            simpleDTO.setName("电网拓扑");
            simpleDTO.setSort(0);
            simpleDTO.setCode("Power_Network");
            deviceInfoParam.setStatisticalType(simpleDTO);
            //处理监测点
            List<GeneralDeviceDTO> deviceDataList = generalDeviceInfoClient.getPracticalRunDeviceInfo(deviceInfoParam).getData();
            for (GeneralDeviceDTO generalDeviceDTO : deviceDataList) {
                lineIds.addAll(generalDeviceDTO.getLineIndexes());
            }
        }
        int[] types = new int[historyHarmOverLimitParam.getCondition().length];
        int[] inde = new int[0];
        int[] inharm = new int[0];

        for (int i = 0; i < historyHarmOverLimitParam.getCondition().length; i++) {
            types[i] = Integer.parseInt(historyHarmOverLimitParam.getCondition()[i]);
        }

        if (historyHarmOverLimitParam.getHarmonics() != null) {
            inde = new int[historyHarmOverLimitParam.getHarmonics().length];
            for (int i = 0; i < historyHarmOverLimitParam.getHarmonics().length; i++) {
                inde[i] = Integer.parseInt(historyHarmOverLimitParam.getHarmonics()[i]);
            }
        }

        if (historyHarmOverLimitParam.getInHarmonics() != null) {
            inharm = new int[historyHarmOverLimitParam.getInHarmonics().length];
            for (int i = 0; i < historyHarmOverLimitParam.getInHarmonics().length; i++) {
                inharm[i] = Integer.parseInt(historyHarmOverLimitParam.getInHarmonics()[i]);
            }
        }
        for (int i : types) {
            historyHarmOverLimitVO = getMyCondition(historyHarmOverLimitParam.getSearchBeginTime(), historyHarmOverLimitParam.getSearchEndTime(), lineIds, String.valueOf(i), inde, inharm);
            historyHarmOverLimitVOList.addAll(historyHarmOverLimitVO);
        }
        return historyHarmOverLimitVOList;
    }

    /**
     * mysql相关操作
     * 查询稳态数据分析
     */
    @SneakyThrows
    private List<HistoryHarmOverLimitVO> getMyCondition(String startTime, String endTime, List<String> lineId, String contion, int[] number, int[] inharm) {
        List<HistoryHarmOverLimitVO> historyHarmOverLimitVOListData = new ArrayList<>();
        //数据的转化
        String targetName;//指标名称
        String unit;//单位
        if (!lineId.isEmpty()) {
            switch (Integer.parseInt(contion)) {
                case 12:
                    targetName = "电压偏差";
                    unit = "%";
                    List<HistoryHarmOverLimitVO> historyHarmOverLimitVOList = normLimitMapper.getDyPc(lineId, DateUtil.beginOfDay(DateUtil.parse(startTime)), DateUtil.endOfDay(DateUtil.parse(endTime)));
                    for (HistoryHarmOverLimitVO historyHarmOverLimitVO : historyHarmOverLimitVOList) {
                        if (historyHarmOverLimitVO.getMaxData() != -3.14159f) {
                            historyHarmOverLimitVO.setTargetName(targetName);
                            historyHarmOverLimitVO.setUnit(unit);
                            historyHarmOverLimitVO.setTargetCode(contion);
                            historyHarmOverLimitVO.setStatisticalType(Math.abs(historyHarmOverLimitVO.getMaxData()) > Math.abs(historyHarmOverLimitVO.getMinData()) ? 3 : 2);
                            historyHarmOverLimitVOListData.add(historyHarmOverLimitVO);
                        }
                    }
                    break;
                case 13:
                    targetName = "三相电压不平衡度";
                    unit = "/";
                    List<HistoryHarmOverLimitVO> historyHarmOverLimitVOListS = normLimitMapper.getSxBpHd(lineId, DateUtil.beginOfDay(DateUtil.parse(startTime)), DateUtil.endOfDay(DateUtil.parse(endTime)));
                    for (HistoryHarmOverLimitVO historyHarmOverLimitVO : historyHarmOverLimitVOListS) {
                        if (historyHarmOverLimitVO.getMaxData() != -3.14159f) {
                            historyHarmOverLimitVO.setTargetName(targetName);
                            historyHarmOverLimitVO.setUnit(unit);
                            historyHarmOverLimitVO.setTargetCode(contion);
                            historyHarmOverLimitVO.setStatisticalType(Math.abs(historyHarmOverLimitVO.getMaxData()) > Math.abs(historyHarmOverLimitVO.getCp95Data()) ? 3 : 4);
                            historyHarmOverLimitVOListData.add(historyHarmOverLimitVO);
                        }
                    }
                    break;
                case 15:
                    targetName = "电压总谐波畸变率";
                    unit = "%";
                    List<HistoryHarmOverLimitVO> historyHarmOverLimitVOListD = normLimitMapper.getDyZjBl(lineId, DateUtil.beginOfDay(DateUtil.parse(startTime)), DateUtil.endOfDay(DateUtil.parse(endTime)));
                    for (HistoryHarmOverLimitVO historyHarmOverLimitVO : historyHarmOverLimitVOListD) {
                        if (historyHarmOverLimitVO.getMaxData() != -3.14159f) {
                            historyHarmOverLimitVO.setTargetName(targetName);
                            historyHarmOverLimitVO.setUnit(unit);
                            historyHarmOverLimitVO.setTargetCode(contion);
                            historyHarmOverLimitVO.setStatisticalType(4);
                            historyHarmOverLimitVOListData.add(historyHarmOverLimitVO);
                        }
                    }
                    break;
                case 22:
                    targetName = "负序电流";
                    unit = "/";
                    List<HistoryHarmOverLimitVO> historyHarmOverLimitVOListF = normLimitMapper.getFxDl(lineId, DateUtil.beginOfDay(DateUtil.parse(startTime)), DateUtil.endOfDay(DateUtil.parse(endTime)));
                    for (HistoryHarmOverLimitVO historyHarmOverLimitVO : historyHarmOverLimitVOListF) {
                        if (historyHarmOverLimitVO.getMaxData() != -3.14159f) {
                            historyHarmOverLimitVO.setTargetName(targetName);
                            historyHarmOverLimitVO.setUnit(unit);
                            historyHarmOverLimitVO.setTargetCode(contion);
                            historyHarmOverLimitVO.setStatisticalType(Math.abs(historyHarmOverLimitVO.getMaxData()) > Math.abs(historyHarmOverLimitVO.getCp95Data()) ? 3 : 4);
                            historyHarmOverLimitVOListData.add(historyHarmOverLimitVO);
                        }
                    }
                    break;
                case 30:
                    targetName = "频率";
                    unit = "Hz";
                    List<HistoryHarmOverLimitVO> historyHarmOverLimitVOListP = normLimitMapper.getPl(lineId, DateUtil.beginOfDay(DateUtil.parse(startTime)), DateUtil.endOfDay(DateUtil.parse(endTime)));
                    for (HistoryHarmOverLimitVO historyHarmOverLimitVO : historyHarmOverLimitVOListP) {
                        if (historyHarmOverLimitVO.getMaxData() != -3.14159f) {
                            historyHarmOverLimitVO.setTargetName(targetName);
                            historyHarmOverLimitVO.setUnit(unit);
                            historyHarmOverLimitVO.setTargetCode(contion);
                            historyHarmOverLimitVO.setStatisticalType(Math.abs(historyHarmOverLimitVO.getMaxData()) > Math.abs(historyHarmOverLimitVO.getMinData()) ? 3 : 2);
                            historyHarmOverLimitVOListData.add(historyHarmOverLimitVO);
                        }
                    }
                    break;
                case 40:
                    targetName = "谐波电压含有率";
                    unit = "%";
                    for (int i : number) {
                        List<HistoryHarmOverLimitVO> historyHarmOverLimitVOListX = normLimitMapper.getXbDyHyl(lineId, DateUtil.beginOfDay(DateUtil.parse(startTime)), DateUtil.endOfDay(DateUtil.parse(endTime)), i);
                        for (HistoryHarmOverLimitVO historyHarmOverLimitVO : historyHarmOverLimitVOListX) {
                            if (historyHarmOverLimitVO.getMaxData() != -3.14159f) {
                                historyHarmOverLimitVO.setTargetName(targetName);
                                historyHarmOverLimitVO.setUnit(unit);
                                historyHarmOverLimitVO.setNumber(i);
                                historyHarmOverLimitVO.setTargetCode(contion);
                                historyHarmOverLimitVO.setStatisticalType(4);
                                historyHarmOverLimitVOListData.add(historyHarmOverLimitVO);
                            }
                        }
                    }
                    break;
                case 43:
                    targetName = "谐波电流幅值";
                    unit = "A";
                    for (int i : number) {
                        List<HistoryHarmOverLimitVO> historyHarmOverLimitVOListQ = normLimitMapper.getXbDlFz(lineId, DateUtil.beginOfDay(DateUtil.parse(startTime)), DateUtil.endOfDay(DateUtil.parse(endTime)), i);
                        for (HistoryHarmOverLimitVO historyHarmOverLimitVO : historyHarmOverLimitVOListQ) {
                            if (historyHarmOverLimitVO.getMaxData() != -3.14159f) {
                                historyHarmOverLimitVO.setTargetName(targetName);
                                historyHarmOverLimitVO.setUnit(unit);
                                historyHarmOverLimitVO.setNumber(i);
                                historyHarmOverLimitVO.setTargetCode(contion);
                                historyHarmOverLimitVO.setStatisticalType(4);
                                historyHarmOverLimitVOListData.add(historyHarmOverLimitVO);
                            }
                        }
                    }
                    break;
                case 46:
                    targetName = "间谐波电压含有率";
                    unit = "%";
                    for (int i : inharm) {
                        List<HistoryHarmOverLimitVO> historyHarmOverLimitVOListJ = normLimitMapper.getJxbHyl(lineId, DateUtil.beginOfDay(DateUtil.parse(startTime)), DateUtil.endOfDay(DateUtil.parse(endTime)), i);
                        for (HistoryHarmOverLimitVO historyHarmOverLimitVO : historyHarmOverLimitVOListJ) {
                            if (historyHarmOverLimitVO.getMaxData() != -3.14159f) {
                                historyHarmOverLimitVO.setTargetName(targetName);
                                historyHarmOverLimitVO.setUnit(unit);
                                historyHarmOverLimitVO.setNumber(i);
                                historyHarmOverLimitVO.setTargetCode(contion);
                                historyHarmOverLimitVO.setStatisticalType(4);
                                historyHarmOverLimitVOListData.add(historyHarmOverLimitVO);
                            }
                        }
                    }
                    break;
                case 61:
                    //长时闪变
                    targetName = "长时闪变";
                    unit = "/";
                    List<HistoryHarmOverLimitVO> historyHarmOverLimitVOListC = normLimitMapper.getCsSb(lineId, DateUtil.beginOfDay(DateUtil.parse(startTime)), DateUtil.endOfDay(DateUtil.parse(endTime)));
                    for (HistoryHarmOverLimitVO historyHarmOverLimitVO : historyHarmOverLimitVOListC) {
                        if (historyHarmOverLimitVO.getAvgData() != -3.14159f) {
                            historyHarmOverLimitVO.setTargetName(targetName);
                            historyHarmOverLimitVO.setUnit(unit);
                            historyHarmOverLimitVO.setMaxData(3.14159f);
                            historyHarmOverLimitVO.setMinData(3.14159f);
                            historyHarmOverLimitVO.setCp95Data(3.14159f);
                            historyHarmOverLimitVO.setTargetCode(contion);
                            historyHarmOverLimitVO.setStatisticalType(1);
                            historyHarmOverLimitVOListData.add(historyHarmOverLimitVO);
                        }
                    }
                    break;
                default:
                    break;
            }
        } else {
            return historyHarmOverLimitVOListData;
        }
        return historyHarmOverLimitVOListData;
    }
}
