package com.ff.system;

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

import com.ff.base.system.domain.SysDatasource;
import com.ff.base.system.service.ISysDatasourceService;
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.ff.base.annotation.Log;
import com.ff.base.core.controller.BaseController;
import com.ff.base.core.domain.AjaxResult;
import com.ff.base.enums.BusinessType;
import com.ff.base.utils.poi.ExcelUtil;
import com.ff.base.core.page.TableDataInfo;

/**
 * 租户数据源Controller
 *
 * @author liukang
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/system/datasource")
public class SysDatasourceController extends BaseController
{
    @Autowired
    private ISysDatasourceService sysDatasourceService;

    /**
     * 查询租户数据源列表
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDatasource sysDatasource)
    {
        startPage();
        List<SysDatasource> list = sysDatasourceService.selectSysDatasourceList(sysDatasource);
        return getDataTable(list);
    }

    /**
     * 导出租户数据源列表
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:export')")
    @Log(title = "租户数据源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDatasource sysDatasource)
    {
        List<SysDatasource> list = sysDatasourceService.selectSysDatasourceList(sysDatasource);
        ExcelUtil<SysDatasource> util = new ExcelUtil<SysDatasource>(SysDatasource.class);
        util.exportExcel(response, list, "租户数据源数据");
    }

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

    /**
     * 新增租户数据源
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:add')")
    @Log(title = "租户数据源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDatasource sysDatasource)
    {
        return toAjax(sysDatasourceService.insertSysDatasource(sysDatasource));
    }

    /**
     * 修改租户数据源
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:edit')")
    @Log(title = "租户数据源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDatasource sysDatasource)
    {
        return toAjax(sysDatasourceService.updateSysDatasource(sysDatasource));
    }

    /**
     * 删除租户数据源
     */
    @PreAuthorize("@ss.hasPermi('system:datasource:remove')")
    @Log(title = "租户数据源", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysDatasourceService.deleteSysDatasourceByIds(ids));
    }
}
