package com.jy.framework.mybatis.join;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class QueryColumns extends AbstractSql {

    List<QueryColumn> queryColumnList = new ArrayList<>();

    List<String> excludeColumnList = new ArrayList<>();

    public QueryColumns exclude(QueryColumn queryColumn) {
        QueryColumns queryColumns = new QueryColumns();
        queryColumns.queryColumnList.addAll(this.queryColumnList);
        queryColumns.excludeColumnList.addAll(this.excludeColumnList);
        queryColumns.excludeColumnList.add(queryColumn.field);
        return queryColumns;
    }

    protected void add(QueryColumn queryColumn) {
        queryColumnList.add(queryColumn);
    }

    @Override
    protected String format() {
        return queryColumnList.stream()
                .filter(item -> !excludeColumnList.contains(item.field))
                .map(QueryColumn::format)
                .collect(Collectors.joining(StringPool.COMMA));
    }

}
