package jj.tech.paolu.utils.mybatis;
import org.mybatis.dynamic.sql.BindableColumn;
import org.mybatis.dynamic.sql.ColumnAndConditionCriterion;
import org.mybatis.dynamic.sql.SqlCriterion;
import org.mybatis.dynamic.sql.VisitableCondition;
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
import org.mybatis.dynamic.sql.select.function.AbstractUniTypeFunction;
import org.mybatis.dynamic.sql.where.render.CriterionRenderer;

public class Sum<T> extends AbstractUniTypeFunction<T, Sum<T>> {

    private final VisitableCondition<T> condition;

    private Sum(BindableColumn<T> column, VisitableCondition<T> condition) {
        super(column);
        this.condition = condition;
    }

    @Override
    public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
//    	 ColumnAndConditionCriterion.withColumn(column).withCondition(condition).build().
    	ColumnAndConditionCriterion<T>  criteion = ColumnAndConditionCriterion
    			.withColumn(column)
    			.withCondition(condition).build();

    	
        return "sum(" //$NON-NLS-1$
                + column.renderWithTableAlias(tableAliasCalculator)
//                + s
                + ")"; //$NON-NLS-1$
    }

    @Override
    protected Sum<T> copy() {
        return new Sum<>(column, condition);
    }

    public static <T> Sum<T> of(BindableColumn<T> column, VisitableCondition<T> condition) {
        return new Sum<>(column, condition);
    }
}