|
@@ -0,0 +1,178 @@
|
|
|
|
|
+package com.fs.tenant;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
|
|
+import com.fs.common.utils.DateUtils;
|
|
|
|
|
+import com.fs.framework.datasource.TenantDataSourceManager;
|
|
|
|
|
+import com.fs.tenant.domain.TenantExecuteTask;
|
|
|
|
|
+import com.fs.tenant.domain.TenantInfo;
|
|
|
|
|
+import com.fs.tenant.service.TenantAsyncService;
|
|
|
|
|
+import com.fs.tenant.service.TenantInfoService;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
|
+import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
+import com.fs.common.annotation.Log;
|
|
|
|
|
+import com.fs.common.core.controller.BaseController;
|
|
|
|
|
+import com.fs.common.core.domain.AjaxResult;
|
|
|
|
|
+import com.fs.common.enums.BusinessType;
|
|
|
|
|
+import com.fs.tenant.domain.TenantSqlExecuteRecord;
|
|
|
|
|
+import com.fs.tenant.service.ITenantSqlExecuteRecordService;
|
|
|
|
|
+import com.fs.common.utils.poi.ExcelUtil;
|
|
|
|
|
+import com.fs.common.core.page.TableDataInfo;
|
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 租户SQL执行记录Controller
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author fs
|
|
|
|
|
+ * @date 2026-04-21
|
|
|
|
|
+ */
|
|
|
|
|
+@RestController
|
|
|
|
|
+@RequestMapping("/tenant/record")
|
|
|
|
|
+public class TenantSqlExecuteRecordController extends BaseController
|
|
|
|
|
+{
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private ITenantSqlExecuteRecordService tenantSqlExecuteRecordService;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private TenantDataSourceManager tenantDataSourceManager;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private TenantInfoService tenantInfoService;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private TenantAsyncService tenantAsyncService;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查询租户SQL执行记录列表
|
|
|
|
|
+ */
|
|
|
|
|
+ @PreAuthorize("@ss.hasPermi('tenant:record:list')")
|
|
|
|
|
+ @GetMapping("/list")
|
|
|
|
|
+ public TableDataInfo list(TenantSqlExecuteRecord tenantSqlExecuteRecord)
|
|
|
|
|
+ {
|
|
|
|
|
+ startPage();
|
|
|
|
|
+ List<TenantSqlExecuteRecord> list = tenantSqlExecuteRecordService.selectTenantSqlExecuteRecordList(tenantSqlExecuteRecord);
|
|
|
|
|
+ return getDataTable(list);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 导出租户SQL执行记录列表
|
|
|
|
|
+ */
|
|
|
|
|
+ @PreAuthorize("@ss.hasPermi('tenant:record:export')")
|
|
|
|
|
+ @Log(title = "租户SQL执行记录", businessType = BusinessType.EXPORT)
|
|
|
|
|
+ @GetMapping("/export")
|
|
|
|
|
+ public AjaxResult export(TenantSqlExecuteRecord tenantSqlExecuteRecord)
|
|
|
|
|
+ {
|
|
|
|
|
+ List<TenantSqlExecuteRecord> list = tenantSqlExecuteRecordService.selectTenantSqlExecuteRecordList(tenantSqlExecuteRecord);
|
|
|
|
|
+ ExcelUtil<TenantSqlExecuteRecord> util = new ExcelUtil<TenantSqlExecuteRecord>(TenantSqlExecuteRecord.class);
|
|
|
|
|
+ return util.exportExcel(list, "租户SQL执行记录数据");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取租户SQL执行记录详细信息
|
|
|
|
|
+ */
|
|
|
|
|
+ @PreAuthorize("@ss.hasPermi('tenant:record:query')")
|
|
|
|
|
+ @GetMapping(value = "/{id}")
|
|
|
|
|
+ public AjaxResult getInfo(@PathVariable("id") Long id)
|
|
|
|
|
+ {
|
|
|
|
|
+ return AjaxResult.success(tenantSqlExecuteRecordService.selectTenantSqlExecuteRecordById(id));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 新增租户SQL执行记录
|
|
|
|
|
+ */
|
|
|
|
|
+ @PreAuthorize("@ss.hasPermi('tenant:record:add')")
|
|
|
|
|
+ @Log(title = "租户SQL执行记录", businessType = BusinessType.INSERT)
|
|
|
|
|
+ @PostMapping
|
|
|
|
|
+ public AjaxResult add(@RequestBody TenantSqlExecuteRecord tenantSqlExecuteRecord)
|
|
|
|
|
+ {
|
|
|
|
|
+ return toAjax(tenantSqlExecuteRecordService.insertTenantSqlExecuteRecord(tenantSqlExecuteRecord));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 上传 SQL 脚本文件
|
|
|
|
|
+ */
|
|
|
|
|
+ @Log(title = "上传SQL脚本", businessType = BusinessType.INSERT)
|
|
|
|
|
+ @PostMapping("/uploadScript")
|
|
|
|
|
+ public AjaxResult uploadScript(
|
|
|
|
|
+ @RequestParam("file") MultipartFile file,
|
|
|
|
|
+ @RequestParam(value = "tenantId", required = false) Integer tenantId) throws Exception {
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 基础校验
|
|
|
|
|
+ if (file.isEmpty()) {
|
|
|
|
|
+ return AjaxResult.error("请上传SQL脚本文件");
|
|
|
|
|
+ }
|
|
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
|
|
+ if (!filename.toLowerCase().endsWith(".sql")) {
|
|
|
|
|
+ return AjaxResult.error("只能上传 .sql 后缀的脚本文件");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 获取要执行的租户列表
|
|
|
|
|
+ List<TenantInfo> tenantInfos;
|
|
|
|
|
+ if (tenantId != null) {
|
|
|
|
|
+ // 单个租户
|
|
|
|
|
+ TenantInfo tenantInfo = tenantInfoService.selectTenantInfoById(tenantId.toString());
|
|
|
|
|
+ tenantInfos = Collections.singletonList(tenantInfo);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 全部租户
|
|
|
|
|
+ tenantInfos = tenantInfoService.selectTenantInfoList(null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 构建执行记录
|
|
|
|
|
+ List<TenantSqlExecuteRecord> recordList = new ArrayList<>();
|
|
|
|
|
+ for (TenantInfo info : tenantInfos) {
|
|
|
|
|
+ TenantSqlExecuteRecord record = new TenantSqlExecuteRecord();
|
|
|
|
|
+ record.setTenantId(info.getId().toString());
|
|
|
|
|
+ record.setTenantCode(info.getTenantCode());
|
|
|
|
|
+ record.setDbName(info.getDbName());
|
|
|
|
|
+ record.setSqlFile(filename);
|
|
|
|
|
+ record.setStatus(0);
|
|
|
|
|
+ record.setExecuteTime(DateUtils.getNowDate());
|
|
|
|
|
+ recordList.add(record);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 批量插入记录(自动回填ID)
|
|
|
|
|
+ tenantSqlExecuteRecordService.saveBatch(recordList);
|
|
|
|
|
+
|
|
|
|
|
+ // 5. 构建任务列表
|
|
|
|
|
+ List<TenantExecuteTask> tasks = new ArrayList<>();
|
|
|
|
|
+ for (int i = 0; i < recordList.size(); i++) {
|
|
|
|
|
+ TenantExecuteTask task = new TenantExecuteTask();
|
|
|
|
|
+ task.setRecordId(recordList.get(i).getId());
|
|
|
|
|
+ task.setTenantInfo(tenantInfos.get(i));
|
|
|
|
|
+ tasks.add(task);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 6. 异步执行
|
|
|
|
|
+ tenantAsyncService.executeSqlScriptAsync(file, tasks);
|
|
|
|
|
+
|
|
|
|
|
+ return AjaxResult.success("上传成功,后台正在执行SQL脚本...");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 修改租户SQL执行记录
|
|
|
|
|
+ */
|
|
|
|
|
+ @PreAuthorize("@ss.hasPermi('tenant:record:edit')")
|
|
|
|
|
+ @Log(title = "租户SQL执行记录", businessType = BusinessType.UPDATE)
|
|
|
|
|
+ @PutMapping
|
|
|
|
|
+ public AjaxResult edit(@RequestBody TenantSqlExecuteRecord tenantSqlExecuteRecord)
|
|
|
|
|
+ {
|
|
|
|
|
+ return toAjax(tenantSqlExecuteRecordService.updateTenantSqlExecuteRecord(tenantSqlExecuteRecord));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 删除租户SQL执行记录
|
|
|
|
|
+ */
|
|
|
|
|
+ @PreAuthorize("@ss.hasPermi('tenant:record:remove')")
|
|
|
|
|
+ @Log(title = "租户SQL执行记录", businessType = BusinessType.DELETE)
|
|
|
|
|
+ @DeleteMapping("/{ids}")
|
|
|
|
|
+ public AjaxResult remove(@PathVariable Long[] ids)
|
|
|
|
|
+ {
|
|
|
|
|
+ return toAjax(tenantSqlExecuteRecordService.deleteTenantSqlExecuteRecordByIds(ids));
|
|
|
|
|
+ }
|
|
|
|
|
+}
|