package com.cm.common.plugin.service.database.table.impl;

import com.cm.common.base.AbstractService;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
import com.cm.common.exception.UpdateException;
import com.cm.common.plugin.dao.database.table.ITableDao;
import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO;
import com.cm.common.plugin.pojo.dtos.database.table.TableDTO;
import com.cm.common.plugin.pojo.vos.database.table.TableColumnVO;
import com.cm.common.plugin.pojo.vos.database.table.TableOldColumnVO;
import com.cm.common.plugin.pojo.vos.database.table.TableVO;
import com.cm.common.plugin.service.database.IDatabaseService;
import com.cm.common.plugin.service.database.table.ITableService;
import com.cm.common.result.SuccessResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: TableServiceImpl
 * @Description: 数据库表处理
 * @Author: WangGeng
 * @Date: 2019-11-14 10:43
 * @Version: 1.0
 **/
@Service
public class TableServiceImpl extends AbstractService implements ITableService {

    @Autowired
    private ITableDao tableDao;

    @Override
    public synchronized SuccessResult saveTable(TableVO tableVO) throws SearchException, SaveException {
        String tableName = tableVO.getTableName();
        TableDTO tableDTO = getTableByTableName(tableName);
        if (tableDTO != null) {
            throw new SaveException("表已存在");
        }
        createTable(tableVO);
        return new SuccessResult();
    }

    @Override
    public void createTable(TableVO tableVO) throws SaveException {
        Connection connection = getConnectionFromSession();
        String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString();
        String tableName = tableVO.getTableName();
        tableDao.saveTable(connection, databaseName, tableName, tableVO.getTableColumns());
    }

    @Override
    public SuccessResult removeTable(String tableName) throws RemoveException {
        Connection connection = getConnectionFromSession();
        String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString();
        tableDao.removeTable(connection, databaseName, tableName);
        return new SuccessResult();
    }

    @Override
    public SuccessResult saveTableColumn(String tableName, TableColumnVO tableColumnVO) throws SaveException {
        Connection connection = getConnectionFromSession();
        String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString();
        tableDao.saveTableColumn(connection, databaseName, tableName, tableColumnVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult removeTableColumn(String tableName, String columnName) throws RemoveException {
        Connection connection = getConnectionFromSession();
        String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString();
        tableDao.removeTableColumn(connection, databaseName, tableName, columnName);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateTableColumn(String tableName, TableOldColumnVO tableOldColumnVO) throws UpdateException {
        Connection connection = getConnectionFromSession();
        String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString();
        tableDao.updateTableColumn(connection, databaseName, tableName, tableOldColumnVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateTableColumnType(String tableName, TableColumnVO tableColumnVO) throws UpdateException {
        Connection connection = getConnectionFromSession();
        String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString();
        tableDao.updateTableColumnType(connection, databaseName, tableName, tableColumnVO);
        return new SuccessResult();
    }

    @Override
    public TableDTO getTableByTableName(String tableName) throws SearchException {
        Connection connection = getConnectionFromSession();
        String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString();
        return tableDao.getTableByTableName(connection, databaseName, tableName);
    }

    @Override
    public List<TableDTO> listTables(Map<String, Object> params) throws SearchException {
        Connection connection = getConnectionFromSession();
        params.put("databaseName", getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString());
        return tableDao.listTables(connection, params);
    }

    @Override
    public List<TableColumnDTO> listTableColumns(Map<String, Object> params) throws SearchException {
        Connection connection = getConnectionFromSession();
        params.put("databaseName", getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString());
        return tableDao.listTableColumns(connection, params);
    }

    /**
     * 从session中获取connection
     *
     * @return
     */
    private Connection getConnectionFromSession() {
        Object connectionObj = getHttpSession().getAttribute(IDatabaseService.DATABASE_CONNECTION);
        if (connectionObj == null) {
            throw new SearchException("数据库连接无效");
        }
        return (Connection) connectionObj;
    }
}
