package me.chyxion.tigon.mybatis;

import lombok.Getter;
import lombok.Setter;
import lombok.NonNull;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * 2017/1/23 16:58
 */
@Getter
@RequiredArgsConstructor
public class SearchProcessArgs {
    private final String table;
    @NonNull
    private final List<Object> result;
    @Setter
    private Criterion criterion;
    @Setter
    private boolean hasPrevCol;
    @Setter
    private boolean hasPrevOrCol;

    public SearchProcessArgs append(String sqlFragment) {
        result.add(new SqlFragment(sqlFragment));
        return this;
    }

    public SearchProcessArgs addValue(Object value) {
        result.add(value);
        return this;
    }

    public String getCol() {
        String col = criterion.getCol();
        return StringUtils.isNotBlank(table)
            && !col.contains(".") ?
                table + "." + col : col;
    }

    public <T> T getValue() {
        return criterion.getValue();
    }
}
