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 VarcharCondition implements Condition {
    private String operator;
    public VarcharCondition(String operator) {
        this.operator = operator;
    }
    @Override
    public String buildSql(CstTableIndicator indicator, String... value) {
        if (StringUtils.isEmpty(operator)) return null;
        String value1 = value[0];
        String sql = "";
        String column = indicator.getLabelColumn();
        if (operator.equals(CstOperator.EQUAL) && StringUtils.isNotEmpty(value1)) {
            sql += " and " + indicator.getTableId() + "." + column + " = '" + value1 + "'";
        }
        if (operator.equals(CstOperator.NOT_EQUAL) && StringUtils.isNotEmpty(value1)) {
            sql += " and " + indicator.getTableId() + "." + column + " != '" + value1 + "'";
        }
        if (operator.equals(CstOperator.LIKE) && StringUtils.isNotEmpty(value1)) {
            sql += " and " + indicator.getTableId() + "." + column + " like '%" + value1 + "%'";
        }
        if (operator.equals(CstOperator.NOT_LIKE) && StringUtils.isNotEmpty(value1)) {
            sql += " and " + indicator.getTableId() + "." + column + " not like '%" + value1 + "%'";
        }
        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;
    }
}
