package com.ruoyi.dxp.service;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.dxp.constant.DxpSyncStatusConstants;
import com.ruoyi.dxp.domain.Database;
import com.ruoyi.dxp.domain.Table;
import com.ruoyi.dxp.domain.TableColumn;
import com.ruoyi.dxp.dto.ResApiDbConnDto;
import com.ruoyi.dxp.mapper.DatabaseMapper;
import com.ruoyi.dxp.mapper.TableColumnMapper;
import com.ruoyi.dxp.mapper.TableMapper;
import com.ruoyi.dxp.utils.DatabaseUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class DatabaseInitThread extends Thread {
    private DatabaseMapper databaseMapper;
    private TableMapper tableMapper;
    private TableColumnMapper tableColumnMapper;
    private Database database;
    private String userName;

    public DatabaseInitThread(Database database,DatabaseMapper databaseMapper, TableMapper tableMapper, TableColumnMapper tableColumnMapper,String userName) {
        this.database = database;
        this.databaseMapper = databaseMapper;
        this.tableMapper = tableMapper;
        this.tableColumnMapper = tableColumnMapper;
        this.userName = userName;
    }

    @Override
    public void run() {
        //获取输入流
        try {
            // 获取数据库中的表
            R<List<Table>> tableListResult = DatabaseUtils.getTableList(database.getId());
            if (tableListResult.getCode() != HttpStatus.SUCCESS || tableListResult.getData() == null || tableListResult.getData().size() == 0) {
                // 更新状态为同步失败
                database.setStatus(DxpSyncStatusConstants.SYNC_FAIL);
                databaseMapper.updateDatabase(database);
                return;
            }

            // 保存表
            List<Table> tables = tableListResult.getData();
            for (Table table : tables) {
                // 查询表是否存在
                Table req = new Table();
                req.setDatabaseId(database.getId());
                req.setTableName(table.getTableName());
                List<Table> tableList = tableMapper.selectTableList(req);
                Long tableId = null;
                if (tableList != null && tableList.size() > 0) {
                    tableId = tableList.get(0).getId();
                }

                // 保存表
                table.setDatabaseId(database.getId());
                table.setCreateTime(DateUtils.getNowDate());
                table.setCreateBy(userName);
                if (tableId != null) {
                    table.setId(tableId);
                    tableMapper.updateTable(table);
                } else {
                    tableMapper.insertTable(table);
                }

                // 读取列
                R<List<TableColumn>> columnListResult = DatabaseUtils.getColumnList(database.getId(), table.getTableName());
                if (columnListResult.getCode() == HttpStatus.SUCCESS) {
                    // 保存列
                    List<TableColumn> columns = columnListResult.getData();
                    if (columns != null && columns.size() > 0) {
                        for (TableColumn column : columns) {
                            column.setDatabaseId(database.getId());
                            column.setTableId(table.getId());
                            column.setCreateTime(DateUtils.getNowDate());
                            column.setCreateBy(userName);
                            this.tableColumnMapper.insertTableColumn(column);
                        }
                    }
                }
            }

            // 更新状态为成功
            database.setStatus(DxpSyncStatusConstants.SYNC_COMPLETE);
            databaseMapper.updateDatabase(database);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
