package com.njcn.gather.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.njcn.gather.storage.pojo.vo.RawResultDataVO;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author caozehui
 * @data 2025-08-21
 */
public class StorageUtil {

    public static Double isHarmOrInHarm(Double value) {
        if (value == value.longValue()) {
            return value;
        } else {
            return value + 0.5;
        }
    }

    public static String unit(String code) {
        String unit = "";
        switch (code) {
            /**
             * 频率
             */
            case "FREQ":
                unit = "Hz";
                break;
            /**
             * 电压
             */
            case "V":
            case "VRMS":
                unit = "V";
                break;
            /**
             * 电流
             */
            case "I":
            case "IRMS":
                unit = "A";
                break;
            /**
             * 谐波电压
             */
            case "V2-50":
                /**
                 * 谐波电流
                 */
            case "I2-50":
                /**
                 * 间谐波电压
                 */
            case "SV_1-49":
                /**
                 * 间谐波电流
                 */
            case "SI_1-49":
                /**
                 * 三相电压不平衡度
                 */
            case "V_UNBAN":
            case "IMBV":
                /**
                 * 三相电流不平衡度
                 */
            case "I_UNBAN":
            case "IMBA":
                unit = "%";
                break;
            case "SeqV":
                unit = "V";
                break;
            case "SeqA":
                unit = "A";
                break;
            /**
             * 谐波有功功率
             */
            case "P2-50":
                unit = "W";
                break;
            /**
             * 功率
             */
            case "P":
            case "W":
                unit = "W";
                break;
            case "VARW":
                unit = "Var";
                break;
            case "VAW":
                unit = "VA";
                break;
            /**
             * 闪变
             */
            case "PST":
                break;
            /**
             * 暂态-电压幅值
             */
            case "MAG":
                unit = "%";
                break;
            /**
             * 暂态-持续时间
             */
            case "DUR":
                unit = "s";
                break;
            /**
             * 相角
             */
            case "VA":
            case "IA":
            case "U1A":
            case "I1A":
                unit = "°";
                break;
            /**
             * 电压偏差
             */
            case "DELTA_V":
                unit = "%";
                break;
        }
        return unit;
    }

    public static Integer setResultFlag(List<RawResultDataVO.DetectionData> numbers) {
        List<Integer> isData = numbers.stream().filter(Objects::nonNull)
                .filter(x -> ObjectUtil.isNotNull(x.getData()))
                .map(RawResultDataVO.DetectionData::getIsData)
                .distinct()
                .collect(Collectors.toList());
        return getInteger(isData);
    }

    public static Integer getInteger(List<Integer> isData) {
        if (CollUtil.isNotEmpty(isData)) {
            List<Integer> isQualified = isData.stream().filter(x -> 1 == x || 2 == x).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(isQualified)) {
                if (isQualified.size() > 1) {
                    if (isQualified.contains(2)) {
                        return 2;
                    }
                }
                return isQualified.get(0);
            } else {
                if (isData.contains(4)) {
                    return 4;
                }
                if (isData.contains(5)) {
                    return 5;
                }
            }
        }
        return 4;
    }
}
