185 lines
5.6 KiB
Java
185 lines
5.6 KiB
Java
package com.muyu.source.controller;
|
||
|
||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||
import com.dtflys.forest.springboot.annotation.ForestScannerRegister;
|
||
import com.muyu.common.core.domain.Result;
|
||
import com.muyu.common.core.utils.poi.ExcelUtil;
|
||
import com.muyu.common.core.web.controller.BaseController;
|
||
import com.muyu.common.core.web.page.TableDataInfo;
|
||
import com.muyu.common.security.annotation.RequiresPermissions;
|
||
import com.muyu.common.security.utils.SecurityUtils;
|
||
import com.muyu.source.domain.DataSource;
|
||
import com.muyu.source.domain.req.DataSourceQueryReq;
|
||
import com.muyu.source.service.DataSourceService;
|
||
import io.swagger.v3.oas.annotations.Operation;
|
||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||
import jakarta.servlet.http.HttpServletResponse;
|
||
import lombok.extern.log4j.Log4j2;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.validation.annotation.Validated;
|
||
import org.springframework.web.HttpRequestHandler;
|
||
import org.springframework.web.bind.annotation.*;
|
||
|
||
import java.util.Arrays;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* @author Lenovo
|
||
* @ Tool:IntelliJ IDEA
|
||
* @ Author:CHX
|
||
* @ Date:2024-08-20-18:34
|
||
* @ Version:1.0
|
||
* @ Description:数据源控制层
|
||
*/
|
||
@Log4j2
|
||
@RestController
|
||
@RequestMapping("/source")
|
||
@Tag(name = "数据源控制层", description = "进行数据源管理,查看等相关操作")
|
||
public class DataSourceController extends BaseController {
|
||
public DataSourceController() {
|
||
log.info("forest扫描路径:{}", ForestScannerRegister.getBasePackages());
|
||
}
|
||
|
||
/**
|
||
* 数据源业务层
|
||
*/
|
||
@Autowired
|
||
private DataSourceService dataSourceService;
|
||
|
||
/**
|
||
* 测试连接
|
||
* @param dataSource 数据源
|
||
* @return 是否连接成功
|
||
*/
|
||
@PostMapping("/test")
|
||
public Result testConnection(@RequestBody DataSource dataSource){
|
||
Boolean b =dataSourceService.testConnection(dataSource);
|
||
if(b){
|
||
dataSourceService.update(new LambdaUpdateWrapper<>() {{
|
||
set(DataSource::getIsInit, "Y");
|
||
eq(DataSource::getId, dataSource.getId());
|
||
}});
|
||
return Result.success(b,"连接成功");
|
||
}
|
||
return Result.error("连接失败");
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 获取数据源列表
|
||
* @return 数据源列表
|
||
*/
|
||
@GetMapping("/getDataSourceList")
|
||
public Result<List<DataSource>> getDataSourceList(){
|
||
List<DataSource> dataSourceList =dataSourceService.list();
|
||
return success(dataSourceList);
|
||
}
|
||
|
||
/**
|
||
* 查询数据源列表
|
||
*/
|
||
@RequiresPermissions("source:source:list")
|
||
@GetMapping("/list")
|
||
public Result<TableDataInfo<DataSource>> list(DataSource dataSource)
|
||
{
|
||
startPage();
|
||
List<DataSource> list = dataSourceService.selectDataSourceList(dataSource);
|
||
return getDataTable(list);
|
||
}
|
||
|
||
/**
|
||
* 导出数据源列表
|
||
*/
|
||
@RequiresPermissions("source:source: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, "数据源数据");
|
||
}
|
||
|
||
/**
|
||
* 获取数据源详细信息
|
||
*/
|
||
@RequiresPermissions("source:source:query")
|
||
@GetMapping(value = "/getDataSourceById/{id}")
|
||
public Result<List<DataSource>> getInfo(@PathVariable("id") Long id)
|
||
{
|
||
return success(dataSourceService.selectDataSourceById(id));
|
||
}
|
||
|
||
/**
|
||
* 新增数据源
|
||
*/
|
||
@RequiresPermissions("source:source:add")
|
||
@PostMapping
|
||
public Result<Integer> add(
|
||
@Validated @RequestBody DataSource dataSource)
|
||
{
|
||
if (dataSourceService.checkIdUnique(dataSource)) {
|
||
return error("新增 数据源 '" + dataSource + "'失败,数据源已存在");
|
||
}
|
||
dataSource.setCreateBy(SecurityUtils.getUsername());
|
||
return toAjax(dataSourceService.save(dataSource));
|
||
}
|
||
|
||
/**
|
||
* 修改数据源
|
||
*/
|
||
@RequiresPermissions("source:source:edit")
|
||
@PutMapping
|
||
public Result<Integer> edit(
|
||
@Validated @RequestBody DataSource dataSource)
|
||
{
|
||
if (!dataSourceService.checkIdUnique(dataSource)) {
|
||
return error("修改 数据源 '" + dataSource + "'失败,数据源不存在");
|
||
}
|
||
dataSource.setUpdateBy(SecurityUtils.getUsername());
|
||
return toAjax(dataSourceService.updateById(dataSource));
|
||
}
|
||
|
||
/**
|
||
* 删除数据源
|
||
*/
|
||
@RequiresPermissions("source:source:remove")
|
||
@DeleteMapping("/{ids}")
|
||
public Result<Integer> remove(@PathVariable("ids") Long[] ids)
|
||
{
|
||
dataSourceService.removeBatchByIds(Arrays.asList(ids));
|
||
return success();
|
||
}
|
||
|
||
/**
|
||
* 根据ID获取数据
|
||
*
|
||
* @param id 数据源ID
|
||
* @return 数据源
|
||
*/
|
||
@GetMapping("/getDataSource/{id}")
|
||
@Operation(summary = "根据ID获取数据", description = "根据ID获取数据")
|
||
public Result<DataSource> getDataSourceById(@PathVariable("id") Long id) {
|
||
DataSource dataSource = dataSourceService.getById(id);
|
||
return Result.success(dataSource);
|
||
}
|
||
|
||
/**
|
||
* 查询表数据总数
|
||
*
|
||
* @return
|
||
*/
|
||
@GetMapping("/selectTableDataCount")
|
||
@Operation(summary = "查询接入的数据库的总数", description = "查询接入的数据库的总数")
|
||
public Result selectTableDataCount() {
|
||
//TODO 统计出数据接入的数量
|
||
return null;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
}
|