package cc.mrbird.febs.server.generator.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.entity.QueryRequest;
import com.yida.data.common.core.entity.constant.FebsConstant;
import com.yida.data.common.core.entity.system.Column;
import com.yida.data.common.core.entity.system.Table;
import com.yida.data.common.core.utils.SortUtil;

import org.springframework.stereotype.Service;

import java.util.List;

import cc.mrbird.febs.server.generator.mapper.GeneratorMapper;
import cc.mrbird.febs.server.generator.service.GeneratorService;
import lombok.RequiredArgsConstructor;

/**
 * @author MrBird
 */
@Service
@RequiredArgsConstructor
public class GeneratorServiceImpl implements GeneratorService {

    private final GeneratorMapper generatorMapper;

    @Override
    public List<String> getDatabases(String databaseType) {
        return generatorMapper.getDatabases(databaseType);
    }

    @Override
    public IPage<Table> getTables(String tableName, QueryRequest request, String databaseType, String schemaName) {
        Page<Table> page = new Page<>(request.getPageNum(), request.getPageSize());
        SortUtil.handlePageSort(request, page, "createTime", FebsConstant.ORDER_ASC, false);
        return generatorMapper.getTables(page, tableName, databaseType, schemaName);
    }

    @Override
    public List<Column> getColumns(String databaseType, String schemaName, String tableName) {
        return generatorMapper.getColumns(databaseType, schemaName, tableName);
    }

    @Override
    public Table getTable(String databaseType, String schemaName, String tableName) {
        return generatorMapper.getTable(databaseType, schemaName, tableName);
    }
}
