package com.ruoyi.interfaces.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.interfaces.domain.vo.SysMetaQueryVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.interfaces.domain.SysMetaDatasource;
import com.ruoyi.interfaces.service.ISysMetaDatasourceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据源管理Controller
 *
 * @author 朱得糠
 * @date 2025-08-02
 */
@RestController
@RequestMapping("/meta/datasource")
public class SysMetaDatasourceController extends BaseController {
    @Autowired
    private ISysMetaDatasourceService sysMetaDatasourceService;

    /**
     * 查询数据源管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysMetaDatasource sysMetaDatasource) {
        startPage();
        List<SysMetaDatasource> list = sysMetaDatasourceService.selectSysMetaDatasourceList(sysMetaDatasource);
        return getDataTable(list);
    }

    /**
     * 导出数据源管理列表
     */
    @Log(title = "数据源管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMetaDatasource sysMetaDatasource) {
        List<SysMetaDatasource> list = sysMetaDatasourceService.selectSysMetaDatasourceList(sysMetaDatasource);
        ExcelUtil<SysMetaDatasource> util = new ExcelUtil<SysMetaDatasource>(SysMetaDatasource.class);
        util.exportExcel(response, list, "数据源管理数据");
    }

    /**
     * 获取数据源管理详细信息
     */
    @GetMapping(value = "/{dsCode}")
    public AjaxResult getInfo(@PathVariable("dsCode") String dsCode) {
        return success(sysMetaDatasourceService.selectSysMetaDatasourceByDsCode(dsCode));
    }

    /**
     * 新增数据源管理
     */
    @Log(title = "数据源管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMetaDatasource sysMetaDatasource) {
        return toAjax(sysMetaDatasourceService.insertSysMetaDatasource(sysMetaDatasource));
    }

    /**
     * 修改数据源管理
     */
    @Log(title = "数据源管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMetaDatasource sysMetaDatasource) {
        return toAjax(sysMetaDatasourceService.updateSysMetaDatasource(sysMetaDatasource));
    }

    /**
     * 删除数据源管理
     */
    @Log(title = "数据源管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dsCodes}")
    public AjaxResult remove(@PathVariable String[] dsCodes) {
        return toAjax(sysMetaDatasourceService.deleteSysMetaDatasourceByDsCodes(dsCodes));
    }

    @GetMapping("/testConnection/{dsCode}")
    public AjaxResult testConnection(@PathVariable String dsCode) {
        Integer integer = 0;
        try {
            integer = sysMetaDatasourceService.testConnection(dsCode);
        } catch (Exception e) {
            return error(e.getMessage());
        }
        if (integer.equals(1)) return success("连接成功");
        if (integer.equals(0)) return error("连接失败");
        return error("未知错误");
    }

    @GetMapping("/getTableNames/{dsCode}")
    public AjaxResult getTableNames(@PathVariable String dsCode) {

        return success(
                sysMetaDatasourceService.getTableNames(dsCode));
    }

    @PostMapping("/query")
    public AjaxResult query(@RequestBody SysMetaQueryVo queryVo){
        return success(sysMetaDatasourceService.queryTableData(queryVo));
    }

}
