|
|
|
|
@ -3,6 +3,7 @@ package org.springblade.plugin.operation.database.controller; |
|
|
|
|
|
|
|
|
|
import com.alibaba.excel.EasyExcel; |
|
|
|
|
import com.alibaba.nacos.common.utils.CollectionUtils; |
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
|
|
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; |
|
|
|
|
import io.swagger.annotations.Api; |
|
|
|
|
@ -24,22 +25,32 @@ import org.springblade.core.tool.utils.Func; |
|
|
|
|
import org.springblade.plugin.operation.database.context.CustomCellWriteHandler; |
|
|
|
|
import org.springblade.plugin.operation.database.context.CustomSheetWriteHandler; |
|
|
|
|
import org.springblade.plugin.operation.database.entity.DataBase; |
|
|
|
|
import org.springblade.plugin.operation.database.entity.DataTable; |
|
|
|
|
import org.springblade.plugin.operation.database.excel.DatabaseExcel; |
|
|
|
|
import org.springblade.plugin.operation.database.excel.UploadReturn; |
|
|
|
|
import org.springblade.plugin.operation.database.service.IDataBaseService; |
|
|
|
|
import org.springblade.plugin.operation.database.service.IDataTableService; |
|
|
|
|
import org.springblade.system.cache.DictBizCache; |
|
|
|
|
import org.springblade.system.entity.DictBiz; |
|
|
|
|
import org.springblade.system.feign.ISysClient; |
|
|
|
|
import org.springblade.system.user.entity.User; |
|
|
|
|
import org.springblade.system.user.feign.IUserClient; |
|
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream; |
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
import javax.validation.Valid; |
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
|
|
import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
@NonDS |
|
|
|
|
@ -51,16 +62,11 @@ public class DataBaseController extends BladeController { |
|
|
|
|
|
|
|
|
|
private final IDataBaseService dataBaseService; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 详情 |
|
|
|
|
*/ |
|
|
|
|
@GetMapping("/detail") |
|
|
|
|
@ApiOperationSupport(order = 1) |
|
|
|
|
@ApiOperation(value = "详情", notes = "传入dataScope") |
|
|
|
|
public R<DataBase> detail(DataBase dataBase) { |
|
|
|
|
DataBase detail = dataBaseService.getOne(Condition.getQueryWrapper(dataBase)); |
|
|
|
|
return R.data(detail); |
|
|
|
|
} |
|
|
|
|
private final IDataTableService dataTableService; |
|
|
|
|
|
|
|
|
|
private final ISysClient sysClient; |
|
|
|
|
|
|
|
|
|
private final IUserClient userClient; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 分页 |
|
|
|
|
@ -68,8 +74,23 @@ public class DataBaseController extends BladeController { |
|
|
|
|
@GetMapping("/list") |
|
|
|
|
@ApiOperationSupport(order = 2) |
|
|
|
|
@ApiOperation(value = "分页", notes = "传入dataScope") |
|
|
|
|
public R<IPage<DataBase>> list(DataBase dataBase, Query query) { |
|
|
|
|
IPage<DataBase> pages = dataBaseService.page(Condition.getPage(query), Condition.getQueryWrapper(dataBase)); |
|
|
|
|
public R<IPage<DataTable>> list(DataTable dataTable, Query query) { |
|
|
|
|
IPage<DataTable> pages = dataTableService.page(Condition.getPage(query.setDescs("create_time")), Condition.getQueryWrapper(dataTable)); |
|
|
|
|
List<DataTable> dataTableList = pages.getRecords(); |
|
|
|
|
if (CollectionUtils.isNotEmpty(dataTableList)) { |
|
|
|
|
for (DataTable table : dataTableList) { |
|
|
|
|
DataBase dataBase = dataBaseService.getById(table.getDataBaseId()); |
|
|
|
|
table.setDataBaseName(dataBase.getDataBaseName()); |
|
|
|
|
table.setDataBaseAlias(dataBase.getDataBaseAlias()); |
|
|
|
|
table.setDataBaseType(dataBase.getDataBaseType()); |
|
|
|
|
table.setDataBaseIp(dataBase.getDataBaseIp()); |
|
|
|
|
table.setDataBasePort(dataBase.getDataBasePort()); |
|
|
|
|
table.setSystemName(dataBase.getSystemName()); |
|
|
|
|
table.setDeptName(sysClient.getDeptName(table.getDeptId()).getData()); |
|
|
|
|
table.setCompanyName(userClient.userInfoById(table.getCompany()).getData().getRealName()); |
|
|
|
|
} |
|
|
|
|
pages.setRecords(dataTableList); |
|
|
|
|
} |
|
|
|
|
return R.data(pages); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -93,16 +114,6 @@ public class DataBaseController extends BladeController { |
|
|
|
|
return R.status(dataBaseService.updateById(dataBase)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 新增或修改 |
|
|
|
|
*/ |
|
|
|
|
@PostMapping("/submit") |
|
|
|
|
@ApiOperationSupport(order = 5) |
|
|
|
|
@ApiOperation(value = "新增或修改", notes = "传入dataScope") |
|
|
|
|
public R submit(@Valid @RequestBody DataBase dataBase) { |
|
|
|
|
return R.status(dataBaseService.saveOrUpdate(dataBase)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 删除 |
|
|
|
|
*/ |
|
|
|
|
@ -116,16 +127,27 @@ public class DataBaseController extends BladeController { |
|
|
|
|
/** |
|
|
|
|
* excel导入 |
|
|
|
|
*/ |
|
|
|
|
@PostMapping("dataImport") |
|
|
|
|
@PostMapping("/dataImport") |
|
|
|
|
public R importUser(MultipartFile file) { |
|
|
|
|
List<DatabaseExcel> excelList = ExcelUtil.read(file, DatabaseExcel.class); |
|
|
|
|
if (CollectionUtils.isNotEmpty(excelList)) { |
|
|
|
|
List<DataBase> dataBaseList = new ArrayList<>(); |
|
|
|
|
List<DataTable> dataTableList = new ArrayList<>(); |
|
|
|
|
excelList.forEach(item -> { |
|
|
|
|
// String data = sysClient.getDeptIds(AuthUtil.getTenantId(), item.getDeptId()).getData();
|
|
|
|
|
// User user = userClient.userInfoByName(AuthUtil.getTenantId(), item.getCompany()).getData();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 数据库
|
|
|
|
|
DataBase dataBase = BeanUtil.copy(item, DataBase.class); |
|
|
|
|
dataBase.setDataBaseType(Integer.parseInt(DictBizCache.getKey("database_type", item.getDataBaseType()))); |
|
|
|
|
dataBaseList.add(dataBase); |
|
|
|
|
// 数据表
|
|
|
|
|
DataTable dataTable = BeanUtil.copy(item, DataTable.class); |
|
|
|
|
dataTableList.add(dataTable); |
|
|
|
|
}); |
|
|
|
|
dataBaseService.saveBatch(dataBaseList); |
|
|
|
|
dataTableService.saveBatch(dataTableList); |
|
|
|
|
} |
|
|
|
|
return R.success("操作成功"); |
|
|
|
|
} |
|
|
|
|
@ -133,21 +155,18 @@ public class DataBaseController extends BladeController { |
|
|
|
|
/** |
|
|
|
|
* excel导出模板 |
|
|
|
|
*/ |
|
|
|
|
@GetMapping("exportTemplate") |
|
|
|
|
@GetMapping("/exportTemplate") |
|
|
|
|
public void exportUser(HttpServletResponse response) { |
|
|
|
|
List<DatabaseExcel> excels = new ArrayList<>(); |
|
|
|
|
DatabaseExcel excel = new DatabaseExcel(); |
|
|
|
|
// excel.setDataBaseIp("127.0.0.1");
|
|
|
|
|
// excel.setDataBasePort("1000");
|
|
|
|
|
// excel.setDataBaseName("work_order");
|
|
|
|
|
// excel.setDataBaseType("mysql");
|
|
|
|
|
// excel.setDataBaseAlias("工单数据库");
|
|
|
|
|
// excel.setSystemName("工单系统");
|
|
|
|
|
// excel.setSystemModuleName("数据运维");
|
|
|
|
|
// excel.setDeptId("数据科");
|
|
|
|
|
// excel.setCompany("A公司");
|
|
|
|
|
// excel.setDataTableName("t_task_info");
|
|
|
|
|
// excel.setDataTableAlias("任务种类");
|
|
|
|
|
excel.setDataBaseIp("127.0.0.1"); |
|
|
|
|
excel.setDataBasePort("1000"); |
|
|
|
|
excel.setDataBaseName("work_order"); |
|
|
|
|
excel.setDataBaseAlias("工单数据库"); |
|
|
|
|
excel.setSystemName("工单系统"); |
|
|
|
|
excel.setSystemModuleName("数据运维"); |
|
|
|
|
excel.setDataTableName("t_task_info"); |
|
|
|
|
excel.setDataTableAlias("任务种类"); |
|
|
|
|
excels.add(excel); |
|
|
|
|
// ExcelUtil.export(response, "数据库数据" + DateUtil.time(), "数据库数据", excels, DatabaseExcel.class);
|
|
|
|
|
|
|
|
|
|
@ -155,6 +174,108 @@ public class DataBaseController extends BladeController { |
|
|
|
|
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
|
|
|
|
|
EasyExcel.write(fileName, DatabaseExcel.class).registerWriteHandler(new CustomSheetWriteHandler()) |
|
|
|
|
.registerWriteHandler(new CustomCellWriteHandler()).sheet("模板").doWrite(excels); |
|
|
|
|
download(fileName, response); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 文件上传 |
|
|
|
|
*/ |
|
|
|
|
@PostMapping("/upload") |
|
|
|
|
public R<UploadReturn> upload(MultipartFile file) { |
|
|
|
|
// 1、获取文件的原始文件名, 通过原始文件名获取文件后缀 例如:abc.jpg
|
|
|
|
|
String originalFilename = file.getOriginalFilename(); |
|
|
|
|
String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")); |
|
|
|
|
|
|
|
|
|
// 2、使用UUID重新生成文件名,防止文件名称重复造成文件覆盖
|
|
|
|
|
String fileName = UUID.randomUUID().toString() + suffix; |
|
|
|
|
|
|
|
|
|
// 3、创建一个目录对象
|
|
|
|
|
String basePath = "D://用户签名//"; |
|
|
|
|
File dir = new File(basePath); |
|
|
|
|
//判断当前目录是否存在
|
|
|
|
|
if (!dir.exists()) { |
|
|
|
|
//目录不存在,需要创建
|
|
|
|
|
dir.mkdirs(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
// 4、将临时文件转存到指定位置
|
|
|
|
|
file.transferTo(new File(basePath + fileName)); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
UploadReturn uploadReturn = new UploadReturn(); |
|
|
|
|
uploadReturn.setName(originalFilename); |
|
|
|
|
uploadReturn.setPath(basePath + fileName); |
|
|
|
|
uploadReturn.setCreateUser(AuthUtil.getNickName()); |
|
|
|
|
uploadReturn.setCreateTime(new Date()); |
|
|
|
|
return R.data(uploadReturn); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 查看文件 |
|
|
|
|
*/ |
|
|
|
|
@GetMapping("/download") |
|
|
|
|
public void download(String path, HttpServletResponse resp) { |
|
|
|
|
// CtglPlaninfo ctglPlaninfo = ctglPlaninfoMapper.queryRecord(fid);
|
|
|
|
|
// String fapath = "//" + ctglPlaninfo.getFapath();
|
|
|
|
|
// 通过id 查询 数据表 地址
|
|
|
|
|
//获取配置文件地址
|
|
|
|
|
// 文件地址 = 配置文件地址+数据表地址
|
|
|
|
|
// File file = new File(path);
|
|
|
|
|
// if (!file.exists()) {
|
|
|
|
|
// try {
|
|
|
|
|
// file.mkdirs();
|
|
|
|
|
// } catch (Exception e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
File file2 = new File(path); |
|
|
|
|
//获取文件名
|
|
|
|
|
String[] split = path.split("//"); |
|
|
|
|
String fileName = split[split.length - 1]; |
|
|
|
|
// String[] split1 = fileName.split(".");
|
|
|
|
|
// String s = split1[0];
|
|
|
|
|
|
|
|
|
|
if (file2.exists()) { |
|
|
|
|
FileInputStream in = null; |
|
|
|
|
HttpServletResponse response = resp; |
|
|
|
|
response.setCharacterEncoding("UTF-8"); |
|
|
|
|
response.setContentType("application/octet-stream;charset=utf-8"); |
|
|
|
|
response.setHeader("Content-disposition", "attachment;filename=" + fileName); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
in = new FileInputStream(path); |
|
|
|
|
byte[] a = new byte[1024]; |
|
|
|
|
int b; |
|
|
|
|
while ((b = in.read(a)) != -1) { |
|
|
|
|
response.getOutputStream().write(a, 0, b); |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} finally { |
|
|
|
|
if (null != in) { |
|
|
|
|
try { |
|
|
|
|
in.close(); |
|
|
|
|
} catch (IOException e2) { |
|
|
|
|
System.out.println("关闭输入流错误"); |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
response.getOutputStream().close(); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
System.out.println("输出流关闭错误"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
try { |
|
|
|
|
resp.getWriter().println("查不到文件"); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
System.out.println("resp返回前端信息异常"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|