package com.ruoyi.web.controller.dxp.system.base;

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

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.dxp.domain.Table;
import com.ruoyi.dxp.domain.TableColumn;
import com.ruoyi.dxp.service.ITableColumnService;
import com.ruoyi.dxp.service.ITableService;
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.dxp.domain.Database;
import com.ruoyi.dxp.service.IDatabaseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据源Controller
 * 
 * @author jy
 * @date 2022-11-06
 */
@RestController
@RequestMapping("/system/base/database")
public class DatabaseController extends BaseController
{
    @Autowired
    private IDatabaseService databaseService;

    @Autowired
    private ITableService tableService;

    @Autowired
    private ITableColumnService tableColumnService;

    /**
     * 查询数据源列表
     */
    @PreAuthorize("@ss.hasPermi('system/base:database:list')")
    @GetMapping("/list")
    public TableDataInfo list(Database database)
    {
        startPage();
        List<Database> list = databaseService.selectDatabaseList(database);
        return getDataTable(list);
    }

    /**
     * 根据数据库id获取表列表
     * @param databaseId 数据库id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system/base:database:list')")
    @GetMapping("/table/list")
    public AjaxResult tableList(Long databaseId) {
        Table req = new Table();
        req.setDatabaseId(databaseId);
        return success(tableService.selectTableList(req));
    }

    /**
     * 根据表id查询字段列表
     * @param tableId 表id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system/base:database:list')")
    @GetMapping("/column/list")
    public AjaxResult columnList(Long tableId) {
        TableColumn req = new TableColumn();
        req.setTableId(tableId);
        return success(tableColumnService.selectTableColumnList(req));
    }

    /**
     * 导出数据源列表
     */
    @PreAuthorize("@ss.hasPermi('system/base:database:export')")
    @Log(title = "数据源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Database database)
    {
        List<Database> list = databaseService.selectDatabaseList(database);
        ExcelUtil<Database> util = new ExcelUtil<Database>(Database.class);
        util.exportExcel(response, list, "数据源数据");
    }

    /**
     * 获取数据源详细信息
     */
    @PreAuthorize("@ss.hasPermi('system/base:database:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(databaseService.selectDatabaseById(id));
    }

    /**
     * 新增数据源
     */
    @PreAuthorize("@ss.hasPermi('system/base:database:add')")
    @Log(title = "数据源", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody Database database)
    {
        try {
            return databaseService.insertDatabase(database);
        } catch (Exception e) {
            return error("数据库连接失败，请检查");
        }
    }

    @PreAuthorize("@ss.hasPermi('system/base:database:edit')")
    @Log(title = "数据源", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/sync")
    @RepeatSubmit
    public AjaxResult sync(@RequestBody Database database) {
        return databaseService.sync(database.getId());
    }

    /**
     * 修改数据源
     */
    @PreAuthorize("@ss.hasPermi('system/base:database:edit')")
    @Log(title = "数据源", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody Database database)
    {
        return databaseService.updateDatabase(database);
    }

    /**
     * 删除数据源
     */
    @PreAuthorize("@ss.hasPermi('system/base:database:remove')")
    @Log(title = "数据源", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return databaseService.deleteDatabaseById(ids[0]);
    }
}
