
package com.lideeyunji.tool.framework.sql.core.conditions.segments;


import com.lideeyunji.tool.framework.sql.core.conditions.ISqlSegment;
import com.lideeyunji.tool.framework.sql.core.enums.SqlKeyword;
import com.lideeyunji.tool.framework.sql.core.toolkit.StringPool;

import java.util.List;

import static java.util.stream.Collectors.joining;

/**
 * @author JX
 * @create 2024-03-26 10:13
 * @dedescription:
 */
public class HavingSegmentList extends AbstractISegmentList {

    @Override
    protected boolean transformList(List<ISqlSegment> list, ISqlSegment firstSegment, ISqlSegment lastSegment) {
        if (!isEmpty()) {
            this.add(SqlKeyword.AND);
        }
        list.remove(0);
        return true;
    }

    @Override
    protected String childrenSqlSegment() {
        if (isEmpty()) {
            return StringPool.EMPTY;
        }
        return this.stream().map(ISqlSegment::getSqlSegment).collect(joining(StringPool.SPACE, StringPool.SPACE + SqlKeyword.HAVING.getSqlSegment() + StringPool.SPACE, StringPool.EMPTY));
    }
}
