package com.ruoyi.ibs.tabs.enums;

import com.ruoyi.common.exception.ServiceException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;

/**
 * 指标数值类型阈值判断规则
 */
@Slf4j
@AllArgsConstructor
@Getter
public enum IndexT1lThresholdEnum {
    R1("1", ">"),
    R2("2", ">="),
    R3("3", "="),
    R4("4", "<="),
    R5("5", "<"),
    R6("6", "<>");

    private final String thresholdRule;
    private final String desc;

    public static Integer getFlgVal(String thresholdRule, BigDecimal threshold, BigDecimal val) {
        if (R1.thresholdRule.equals(thresholdRule)) {
            return val.compareTo(threshold) > 0 ? 1 : 0;
        }
        if (R2.thresholdRule.equals(thresholdRule)) {
            return val.compareTo(threshold) >= 0 ? 1 : 0;
        }
        if (R3.thresholdRule.equals(thresholdRule)) {
            return val.compareTo(threshold) == 0 ? 1 : 0;
        }
        if (R4.thresholdRule.equals(thresholdRule)) {
            return val.compareTo(threshold) <= 0 ? 1 : 0;
        }
        if (R5.thresholdRule.equals(thresholdRule)) {
            return val.compareTo(threshold) < 0 ? 1 : 0;
        }
        if (R6.thresholdRule.equals(thresholdRule)) {
            return val.compareTo(threshold) < 0 ? 1 : 0;
        }
        return 0;
    }

    public static String getSql(String thresholdRule, BigDecimal threshold, String indexColumnName) {
        if (R1.thresholdRule.equals(thresholdRule)) {
            return String.format(" %s > %s ", indexColumnName, threshold);
        }
        if (R2.thresholdRule.equals(thresholdRule)) {
            return String.format(" %s >= %s ", indexColumnName, threshold);
        }
        if (R3.thresholdRule.equals(thresholdRule)) {
            return String.format(" %s = %s ", indexColumnName, threshold);
        }
        if (R4.thresholdRule.equals(thresholdRule)) {
            return String.format(" %s <= %s ", indexColumnName, threshold);
        }
        if (R5.thresholdRule.equals(thresholdRule)) {
            return String.format(" %s < %s ", indexColumnName, threshold);
        }
        if (R6.thresholdRule.equals(thresholdRule)) {
            return String.format(" %s <> %s ", indexColumnName, threshold);
        }
        log.error("阈值thresholdRule不存在，thresholdRule={}，threshold={}，indexColumnName={}", thresholdRule, threshold, indexColumnName);
        throw new ServiceException("服务异常");
    }
}
