package cn.com.victorysoft.business.analyze.service.condition;

import cn.com.victorysoft.business.analyze.constant.CstOperator;
import cn.com.victorysoft.business.analyze.entity.CstTableIndicator;
import cn.com.victorysoft.business.util.StringUtils;

/**
 * 数字-范围录入类型
 */
public class NumberCondition implements Condition {
    private String operator;
    public NumberCondition(String operator) {
        this.operator = operator;
    }
    @Override
    public String buildSql(CstTableIndicator indicator, String... value) {
        if (StringUtils.isEmpty(operator)) return null;
        String value1 = value[0];
        String value2 = value[1];
        String sql = "";
        String column = indicator.getLabelColumn();
        if (operator.equals(CstOperator.EQUAL) && StringUtils.isNotEmpty(value1)) {
            sql += " and " + indicator.getTableId() + "." + column + " = to_number('" + value1 + "')";
        }
        if (operator.equals(CstOperator.NOT_EQUAL) && StringUtils.isNotEmpty(value1)) {
            sql += " and " + indicator.getTableId() + "." + column + " != to_number('" + value1 + "')";
        }
        if (operator.equals(CstOperator.LT) && StringUtils.isNotEmpty(value1)) {
            sql += " and " + indicator.getTableId() + "." + column + " < to_number('" + value1 + "')";
        }
        if (operator.equals(CstOperator.LTE) && StringUtils.isNotEmpty(value1)) {
            sql += " and " + indicator.getTableId() + "." + column + " <= to_number('" + value1 + "')";
        }
        if (operator.equals(CstOperator.GT) && StringUtils.isNotEmpty(value1)) {
            sql += " and " + indicator.getTableId() + "." + column + " > to_number('" + value1 + "')";
        }
        if (operator.equals(CstOperator.GTE) && StringUtils.isNotEmpty(value1)) {
            sql += " and " + indicator.getTableId() + "." + column + " >= to_number('" + value1 + "')";
        }
        if (operator.equals(CstOperator.BETWEEN) && StringUtils.isNotEmpty(value1) && StringUtils.isNotEmpty(value2)) {
            sql += " and " + indicator.getTableId() + "." + column + " >= to_number('" + value1 + "')";
            sql += " and " + indicator.getTableId() + "." + column + " <= to_number('" + value2 + "')";
        }
        if (operator.equals(CstOperator.NULL)) {
            sql += " and " + indicator.getTableId() + "." + column + " is null";
        }
        if (operator.equals(CstOperator.NOT_NULL)) {
            sql += " and " + indicator.getTableId() + "." + column + " is not null";
        }

        return "".equals(sql) ? null : sql;
    }
}
