package com.jy.framework.mybatis.join;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

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

@Getter(lombok.AccessLevel.PROTECTED)
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class ConnectTable extends AbstractSql {

    private final String joinType;
    private final AbstractTable abstractTable;
    private final List<AbstractSql> queryConditionList = new ArrayList<>();

    @Override
    protected String format() {

        String on = queryConditionList.isEmpty() ? StringPool.EMPTY :
                SqlConstant.ON + queryConditionList.stream()
                        .filter(ObjectUtil::isNotNull)
                        .map(AbstractSql::format)
                        .filter(StrUtil::isNotBlank)
                        .collect(Collectors.joining(SqlConstant.AND));

        return joinType + abstractTable.format() + on;
    }

}
