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

import org.springblade.core.tool.exception.ServiceException;
import org.springblade.datasource.enums.DsTypeEnum;
import org.springblade.datasource.transform.ColumnEnum;
import org.springblade.datasource.transform.ColumnInfo;
import org.springblade.datasource.transform.ITransform;
import org.springblade.datasource.transform.TableInfo;
import org.springblade.datasource.transform.general.GeneralEnum;
import org.springblade.datasource.transform.general.GeneralModel;
import org.springblade.etl.entity.SqlColumn;
import org.springblade.etl.entity.bi.ReportDatasource;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * @author wookvn
 */
@Component
public class KingbaseTransform implements ITransform {

    @Override
    public List<ColumnInfo> transformColumns(List<GeneralModel> model, List<SqlColumn> sqlColumns) {
        return model.stream().map(c -> {
            ColumnInfo columnInfo = new ColumnInfo();
            columnInfo.setColumnComments(c.getColumnComments());
            columnInfo.setColumnName(c.getColumnName());
            columnInfo.setColumnSize(c.getColumnSize());
            columnInfo.setDecimalDigits(c.getDecimalDigits());
            if (c.getSourceColumn() != null && c.getSourceColumn()) {
                SqlColumn sqlColumn = sqlColumns.stream().filter(v -> v.getColumnType().equalsIgnoreCase(c.getType())).findAny().orElseThrow(() -> new ServiceException("转换模型源端字段类型错误!"));
                columnInfo.setColumnType(ColumnEnum.valueOf(sqlColumn.getSqlColumnType().toUpperCase(Locale.ROOT)));
            } else {
                columnInfo.setColumnType(GeneralEnum.valueOf(c.getType().toUpperCase()).getColumnEnum());
            }

            String columnType = KingbaseCreateTableHandle.getColumnType(columnInfo);
            columnInfo.setDataType(columnType);
            return columnInfo;
        }).collect(Collectors.toList());
    }

    @Override
    public void creatTable(TableInfo tableInfo, ReportDatasource targetDatasource, boolean deleteSourceTable) {
        new KingbaseCreateTableHandle().creatTable(tableInfo, targetDatasource);
    }

    @Override
    public TableInfo parseTableInfo(ReportDatasource sourceDatasource, List<GeneralModel> model, String tableViewName,
                                    String tableComment) {
        return new KingbaseParseTableHandle().parseTableInfo(sourceDatasource, model, tableViewName, tableComment);
    }

    /**
     * 获取原表字段信息
     *
     * @param tableViewName
     * @param datasource
     * @return {@link List< ColumnInfo>}
     * @author 王瑞
     * @date 2023/12/25
     */
    @Override
    public List<ColumnInfo> getSourceColumn(String tableViewName, ReportDatasource datasource) throws SQLException {
        return null;
    }

    @Override
    public DsTypeEnum dsType() {
        return DsTypeEnum.kingbasees;
    }

}
