package com.ruoyi.system.service.impl;


import com.ruoyi.common.core.domain.entity.DataSource;
import com.ruoyi.common.core.domain.entity.SysTenant;
import com.ruoyi.system.mapper.DataSourceMapper;
import com.ruoyi.system.service.IDataSourceService;
import com.ruoyi.system.service.ISysTenantService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class DataSourceServiceImpl implements IDataSourceService {

    @Resource
    private DataSourceMapper dataSourceMapper;

    @Resource
    private ISysTenantService sysTenantService;

    @Override
    public int selectDatabaseExist(String databaseIp, String databaseName, Long portNumber) {
        return dataSourceMapper.selectDatabaseExist(databaseIp,databaseName,portNumber);
    }

    @Override
    public int insertDataSource(DataSource dataSource,Long tenantId) {
        dataSourceMapper.insertDataSource(dataSource);
        //租户绑定数据源
        SysTenant sysTenant = new SysTenant();
        sysTenant.setTenantId(tenantId);
        sysTenant.setDatasourceId(dataSource.getId());
        return sysTenantService.updateSysTenant(sysTenant);
    }

    @Override
    public DataSource selectById(Long id) {
        return dataSourceMapper.selectById(id);
    }

}
