package com.jeelowcode.service.infra.api.impl;

import cn.hutool.core.bean.BeanUtil;
import com.jeelowcode.service.infra.api.IApiDataSourceConfigApi;
import com.jeelowcode.service.infra.vo.TableInfoVo;
import com.jeelowcode.service.infra.entity.DataSourceConfigDO;
import com.jeelowcode.service.infra.service.IDataSourceConfigService;
import com.jeelowcode.service.infra.service.IDatabaseTableService;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文件 API 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ApiDataSourceApiImpl implements IApiDataSourceConfigApi {

    @Resource
    private IDataSourceConfigService dataSourceConfigService;

    @Resource
    private IDatabaseTableService databaseTableService;

    //初始化连接池
    public void initDataSource2Pool(){
        dataSourceConfigService.initDataSource2Pool();
    }


    //获取所有配置列表
    @Override
    public List getTableList(String code){
        DataSourceConfigDO dataSourceConfigByCode = dataSourceConfigService.getDataSourceConfigByCode(code);
        if(dataSourceConfigByCode==null){
            return null;
        }
        List<TableInfo> tableList = databaseTableService.getTableList(dataSourceConfigByCode.getId(), null, null);
        List<TableInfoVo> voList = BeanUtil.copyToList(tableList, TableInfoVo.class);
        return voList;
    }

}
