package com.ruoyi.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.config.datasource.config.DynamicDataSource;
import com.ruoyi.common.config.datasource.config.GenConfig;
import com.ruoyi.common.config.datasource.constant.DataSourceType;
import com.ruoyi.common.config.datasource.utils.AjaxResult;
import com.ruoyi.system.entity.DataSource;
import com.ruoyi.system.entity.TableInfo;
import com.ruoyi.system.entity.vo.TableInfoVO;
import com.ruoyi.system.mapper.DataSourceMapper;

import com.ruoyi.system.mapper.TableInfoMapper;
import com.ruoyi.system.service.IDataSourceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.print.Book;
import java.util.List;
import java.util.Map;

@Service
public class DataSourceServiceImpl implements IDataSourceService {

    @Resource
    private DataSourceMapper dataSourceMapper;

    @Resource
    private DynamicDataSource dynamicDataSource;


    @Resource
    private TableInfoMapper tableInfoMapper;

    @Override
    public void changeDataSource(DataSource dataSource) throws Exception {

        //切换数据源之前先清空
        DynamicDataSource.clearDataSource();
        //切换数据源
        dynamicDataSource.changeDataSource(changeDataSource1(dataSource));

    }


    public static GenConfig changeDataSource1(DataSource dataSource) {
        GenConfig genConfig = new GenConfig();
        genConfig.setDbtype(dataSource.getDatabaseType());
        genConfig.setUsername(dataSource.getUsername());
        genConfig.setPassword(dataSource.getPassword());
        if (dataSource.getDatabaseType().equals(DataSourceType.MYSQL.getDataSourceName())) {
            genConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
            genConfig.setUrl("jdbc:mysql://" + dataSource.getDatabaseIp() + ":" + dataSource.getPortNumber() + "/" + dataSource.getDatabaseName());
        } else if (dataSource.getDatabaseType().equals(DataSourceType.SQLSERVER.getDataSourceName())) {
            genConfig.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            genConfig.setUrl("jdbc:sqlserver://" + dataSource.getDatabaseIp() + ":" + dataSource.getPortNumber() + ";DatabaseName=" + dataSource.getDatabaseName());
        } else if (dataSource.getDatabaseType().equals(DataSourceType.ORACLE.getDataSourceName())) {
            genConfig.setDriverClassName("oracle.jdbc.driver.OracleDriver");
            genConfig.setUrl("jdbc:oracle:thin:@" + dataSource.getDatabaseIp() + ":" + dataSource.getPortNumber() + ":" + dataSource.getDatabaseName());
        } else if (dataSource.getDatabaseType().equals(DataSourceType.DM.getDataSourceName())) {
            genConfig.setDriverClassName("dm.jdbc.driver.DmDriver");
            genConfig.setUrl("jdbc:dm://" + dataSource.getDatabaseIp() + ":" + dataSource.getPortNumber() + "?schema=" + dataSource.getDatabaseName());
        }
        return genConfig;
    }

    @Override
    public AjaxResult tableInfoList(Map<String, Object> map) {
        //获取数据源信息

        DataSource dataSource = JSON.parseObject(JSON.toJSONString(map), DataSource.class);

//                JSON.parseObject(redisTemplate.opsForValue().get("DataSource").toString(), DataSource.class);
        if (dataSource.getDatabaseType().equals(DataSourceType.MYSQL.getDataSourceName())) {
            List<TableInfoVO> mysqlTableList = tableInfoMapper.tableInfoList(dataSource.getDatabaseName(), map);
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.put("code", 200)
                    .put("data", mysqlTableList)
                    .put("msg", "操作成功")
                    .put("total", mysqlTableList.size());
            return ajaxResult;

        } else if (dataSource.getDatabaseType().equals(DataSourceType.SQLSERVER.getDataSourceName())) {
            List<TableInfoVO> sqlServerTableList = tableInfoMapper.sqlServerTableInfoList(dataSource.getDatabaseName(), map);
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.put("code", 200)
                    .put("data", sqlServerTableList)
                    .put("msg", "操作成功")
                    .put("total", sqlServerTableList.size());
            return ajaxResult;

        } else if (dataSource.getDatabaseType().equals(DataSourceType.DM.getDataSourceName())) {
            List<TableInfoVO> dmTableList = tableInfoMapper.dmTableInfoList(dataSource.getDatabaseName(), map);
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.put("code", 200)
                    .put("data", dmTableList)
                    .put("msg", "操作成功")
                    .put("total", dmTableList.size());
            return ajaxResult;
        }

        List<TableInfoVO> mysqlTableList = tableInfoMapper.tableInfoList(map.get("url").toString().substring(map.get("url").toString().lastIndexOf("/") + 1), map);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("code", 200)
                .put("data", mysqlTableList.stream())
                .put("msg", "操作成功")
                .put("total", mysqlTableList.size());
        return ajaxResult;
    }

    @Override
    public List<TableInfo> tableFieldInfo(Map<String, Object> map) {
        DataSource dataSource = JSON.parseObject(JSON.toJSONString(map), DataSource.class);
        if (dataSource.getDatabaseType().equals(DataSourceType.MYSQL.getDataSourceName())) {
            return tableInfoMapper.mysqlTableFieldInfo(dataSource.getDatabaseName(), map.get("tableName").toString());
        } else if (dataSource.getDatabaseType().equals(DataSourceType.SQLSERVER.getDataSourceName())) {
            return tableInfoMapper.sqlserverTableFieldInfo(map.get("tableName").toString());
        } else if (dataSource.getDatabaseType().equals(DataSourceType.DM.getDataSourceName())) {
            return tableInfoMapper.dmTableFieldInfo(dataSource.getDatabaseName(), map.get("tableName").toString());
        }
        return tableInfoMapper.mysqlTableFieldInfo(dataSource.getDatabaseName(), map.get("tableName").toString());
    }


}
