
package com.jeelowcode.framework.plus.core.conditions.segments;



import com.jeelowcode.framework.plus.core.conditions.ISqlSegment;
import com.jeelowcode.framework.plus.core.enums.SqlKeyword;
import com.jeelowcode.framework.plus.core.toolkit.StringPool;

import java.util.ArrayList;
import java.util.List;

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

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

    @Override
    protected boolean transformList(List<ISqlSegment> list, ISqlSegment firstSegment, ISqlSegment lastSegment) {
        list.remove(0);
        final List<ISqlSegment> sqlSegmentList = new ArrayList<>(list);
        list.clear();
        list.add(() -> sqlSegmentList.stream().map(ISqlSegment::getSqlSegment).collect(joining(StringPool.SPACE)));
        return true;
    }

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