package com.ruoyi.generator.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.generator.domain.Datasource;
import com.ruoyi.generator.service.IDatasourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 数据源信息Controller
 *
 * @author ruoyi
 * @date 2023-11-22
 */
@RestController
@RequestMapping("/generator/datasource")
public class DatasourceController extends BaseController {
    @Autowired
    private IDatasourceService datasourceService;

    /**
     * 查询数据源信息列表
     */
    @PreAuthorize("@ss.hasPermi('generator:datasource:list')")
    @GetMapping("/list")
    public TableDataInfo list(Datasource datasource) {
        startPage();
        List<Datasource> list = datasourceService.selectDatasourceList(datasource);
        return getDataTable(list);
    }

    /**
     * 查询数据源信息列表
     */
    @PreAuthorize("@ss.hasPermi('generator:datasource:list')")
    @GetMapping("/select")
    public AjaxResult listSelect(Datasource datasource) {
        List<Datasource> list = datasourceService.selectDatasourceList(datasource);
        return success(list);
    }

    /**
     * 导出数据源信息列表
     */
    @PreAuthorize("@ss.hasPermi('generator:datasource:export')")
    @Log(title = "数据源信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Datasource datasource) {
        List<Datasource> list = datasourceService.selectDatasourceList(datasource);
        ExcelUtil<Datasource> util = new ExcelUtil<Datasource>(Datasource.class);
        util.exportExcel(response, list, "数据源信息数据");
    }

    /**
     * 获取数据源信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('generator:datasource:query')")
    @GetMapping(value = "/{datasourceId}")
    public AjaxResult getInfo(@PathVariable("datasourceId") Long datasourceId) {
        return success(datasourceService.selectDatasourceByDatasourceId(datasourceId));
    }

    /**
     * 新增数据源信息
     */
    @PreAuthorize("@ss.hasPermi('generator:datasource:add')")
    @Log(title = "数据源信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Datasource datasource) {
        return toAjax(datasourceService.insertDatasource(datasource));
    }

    /**
     * 修改数据源信息
     */
    @PreAuthorize("@ss.hasPermi('generator:datasource:edit')")
    @Log(title = "数据源信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Datasource datasource) {
        return toAjax(datasourceService.updateDatasource(datasource));
    }

    /**
     * 删除数据源信息
     */
    @PreAuthorize("@ss.hasPermi('generator:datasource:remove')")
    @Log(title = "数据源信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{datasourceIds}")
    public AjaxResult remove(@PathVariable Long[] datasourceIds) {
        return toAjax(datasourceService.deleteDatasourceByDatasourceIds(datasourceIds));
    }

    /**
     * 测试数据源连接
     */
    @PreAuthorize("@ss.hasPermi('generator:datasource:test')")
    @GetMapping(value = "/test/{datasourceId}")
    public AjaxResult testConnection(@PathVariable("datasourceId") Long datasourceId) {
        Datasource datasource = datasourceService.selectDatasourceByDatasourceId(datasourceId);
        boolean res = datasourceService.testConnection(datasource);
        if (!res) {
            return error("数据库连接失败");
        }
        return success("连接成功！");
    }
}
