/**
 *
 */
package org.springblade.datasource.transform.kingbase;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springblade.datasource.transform.*;
import org.springblade.etl.entity.bi.ReportDatasource;

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

/**
 * @author wookvn
 */
public class KingbaseCreateTableHandle extends JdbcCreartTableAbstract {

    public static String getColumnType(ColumnInfo columnInfo) {
        ColumnEnum columnEnum = columnInfo.getColumnType();
        switch (columnEnum) {
            case DECIMAL:
                return "decimal(" + columnInfo.getColumnSize() + ", " + columnInfo.getDecimalDigits() + ")";
            case STRING:
                Integer columnSize = columnInfo.getColumnSize();
                if (columnSize == null) {
                    return "varchar";
                }
                if (columnSize != null && columnSize > 4000) {
                    return "text";
                }
                if (columnSize != null && columnSize < 1) {
                    return "text";
                }
                return String.format("varchar(%s)", columnSize);
            case LONG:
                return "bigint";
            case INTEGER:
                return "int";
            case DOUBLE:
                return "double";
            case FLOAT:
                return "float";
            case DATE:
                return "date";
            case BOOL:
                return "bit(1)";
            default:
                return "";
        }

    }


    @Override
    protected String buildColumnSql(TableInfo tableInfo, ReportDatasource targetDatasource) {
        String creatTableSql = "CREATE TABLE %s.%s (%s) ;";

        StringBuilder fieldSql = new StringBuilder();
        tableInfo.getColumnInfo().stream().forEach(columnInfo -> fieldSql.append(String.format(
                "%s %s ,",
                columnInfo.getColumnName(),
                getColumnType(columnInfo)
        )));

        StringBuilder primaryKey = new StringBuilder();
        List<IndexInfo> collect = null;
        if (CollectionUtil.isNotEmpty(tableInfo.getIndexInfo())) {
            collect = tableInfo.getIndexInfo().stream().filter(IndexInfo::getPrimaryKey).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(collect)) {
                collect.get(0).getIndexColumn().stream().forEach(v -> {
                    primaryKey.append(v.getColumnName() + ",");
                });
                if (StringUtils.isNotBlank(primaryKey)) {
                    primaryKey.deleteCharAt(primaryKey.length() - 1);
                    fieldSql.append(String.format("PRIMARY KEY (%s)", primaryKey));
                }
            }
        } else {
            fieldSql.deleteCharAt(fieldSql.length() - 1);
        }
        return String.format(creatTableSql, targetDatasource.getDbSchema(), tableInfo.getTableName(), fieldSql);
    }

    @Override
    protected String buildTableCommentSql(TableInfo tableInfo, ReportDatasource targetDatasource) {
        return String.format("COMMENT ON TABLE %s is '%s' ", tableInfo.getTableName(), tableInfo.getTableComment());
    }

    @Override
    protected List<String> buildColumnCommentSql(TableInfo tableInfo, ReportDatasource targetDatasource) {
        return tableInfo.getColumnInfo().stream().map(columnInfo ->
                String.format(
                        "COMMENT ON COLUMN %s.%s is '%s' ",
                        tableInfo.getTableName(),
                        columnInfo.getColumnName(),
                        columnInfo.getColumnComments()
                )
        ).collect(Collectors.toList());
    }

    @Override
    protected List<String> buildIndexSql(TableInfo tableInfo, ReportDatasource targetDatasource) {
        return Lists.newArrayList();
    }
}
