package com.sunwin.metro.bean;

import lombok.Data;

import java.math.BigDecimal;

/**
 * @author kane
 */
@Data
public class SimpleRule {

    private int ruleRelationId;

    private BigDecimal threshold;

    private String operator;

    private Long windowSeconds;

    private Long windowSecondsSplit;

    private String windowUnit;

    private String filed;

    private String aggFunction;

    private int windowCounts;

    private int windowCountsSplit;

    private String countState;

    private String timeState;

    private String controlCmd;

    public SimpleRule(WindowRule windowRule) {
        this.ruleRelationId = windowRule.getRuleRelationId();
        this.threshold = windowRule.getThreshold();
        this.operator = windowRule.getOperator();
        this.windowSeconds = windowRule.getWindowSeconds();
        this.filed = windowRule.getAggField();
        this.aggFunction = windowRule.getAggFunction();
        this.windowCounts = windowRule.getWindowCounts();
        this.windowCountsSplit = windowRule.getWindowCountsSplit();
        this.windowSecondsSplit = windowRule.getWindowSecondsSplit()*1000;
        this.countState = windowRule.getCountState();
        this.timeState = windowRule.getTimeState();
        this.controlCmd = windowRule.getControlCmd();
    }
    public boolean isCount(){
        return "1".equals(this.countState.trim());
    }
    public boolean isTime(){
        return "1".equals(this.timeState.trim());
    }
    public boolean apply(BigDecimal comparisonValue) {
        switch (operator) {
            case "=":
                return comparisonValue.compareTo(threshold) == 0;
            case "!=":
                return comparisonValue.compareTo(threshold) != 0;
            case ">":
                return comparisonValue.compareTo(threshold) > 0;
            case "<":
                return comparisonValue.compareTo(threshold) < 0;
            case "<=":
                return comparisonValue.compareTo(threshold) <= 0;
            case ">=":
                return comparisonValue.compareTo(threshold) >= 0;
            case "↑/↓":
                return "1".equals(comparisonValue.toString());
            default:
                throw new RuntimeException("不能解析比较符 : " + operator);
        }
    }
}
