package org.springblade.datasource.transform.general;

import lombok.extern.slf4j.Slf4j;
import org.springblade.datasource.enums.DsTypeEnum;
import org.springblade.datasource.transform.ColumnInfo;
import org.springblade.datasource.transform.ITransform;
import org.springblade.datasource.transform.TableInfo;
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.stream.Collectors;

/**
 * @author 喜欢番茄火锅
 * @date 2023/8/21 12:53
 */
@Slf4j
@Component
public class GeneralTransform implements ITransform {
    /**
     * 翻译数据库字段
     *
     * @param model
     * @return
     */
    @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());
            columnInfo.setColumnType(GeneralEnum.valueOf(c.getType().toUpperCase()).getColumnEnum());
            columnInfo.setDataType(c.getType());
            return columnInfo;
        }).collect(Collectors.toList());
    }

    /**
     * 根据通用表信息创建表对象
     *
     * @param tableInfo
     * @param targetDatasource
     * @return
     * @author 王瑞
     * @date 2023/8/21
     */
    @Override
    public void creatTable(TableInfo tableInfo, ReportDatasource targetDatasource, boolean deleteSourceTable) {

    }

    /**
     * 将源信息转换成通用表数据
     *
     * @param sourceDatasource
     * @param model
     * @param tableViewName
     * @param tableComment
     * @return {@link TableInfo}
     * @author 王瑞
     * @date 2023/8/16
     */
    @Override
    public TableInfo parseTableInfo(ReportDatasource sourceDatasource, List<GeneralModel> model, String tableViewName, String tableComment) {

        return new GeneralParseTableHandle().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;
    }

    /**
     * 数据源类型
     *
     * @return
     */
    @Override
    public DsTypeEnum dsType() {
        return DsTypeEnum.common;
    }
}
