|
|
@@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import com.fs.common.annotation.Log;
|
|
|
import com.fs.common.core.controller.BaseController;
|
|
|
@@ -14,6 +15,7 @@ import com.fs.common.core.domain.AjaxResult;
|
|
|
import com.fs.common.core.page.TableDataInfo;
|
|
|
import com.fs.common.enums.BusinessType;
|
|
|
import com.fs.common.utils.poi.ExcelUtil;
|
|
|
+import com.fs.common.utils.spring.SpringUtils;
|
|
|
import com.fs.quartz.domain.SysJobLog;
|
|
|
import com.fs.quartz.service.ISysJobLogService;
|
|
|
|
|
|
@@ -31,60 +33,133 @@ public class SysJobLogController extends BaseController
|
|
|
|
|
|
/**
|
|
|
* 查询定时任务调度日志列表
|
|
|
+ * 支持 SaaS 平台后台跨租户查询:传入 tenantId 时自动切换到对应租户数据源执行查询,查询完成后自动还原。
|
|
|
*/
|
|
|
@PreAuthorize("@ss.hasPermi('monitor:job:list')")
|
|
|
@GetMapping("/list")
|
|
|
- public TableDataInfo list(SysJobLog sysJobLog)
|
|
|
+ public TableDataInfo list(SysJobLog sysJobLog, @RequestParam(value = "tenantId", required = false) Long tenantId)
|
|
|
{
|
|
|
- startPage();
|
|
|
- List<SysJobLog> list = jobLogService.selectJobLogList(sysJobLog);
|
|
|
- return getDataTable(list);
|
|
|
+ try {
|
|
|
+ switchIfTenant(tenantId);
|
|
|
+ startPage();
|
|
|
+ List<SysJobLog> list = jobLogService.selectJobLogList(sysJobLog);
|
|
|
+ return getDataTable(list);
|
|
|
+ } finally {
|
|
|
+ clearIfSwitched(tenantId);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 导出定时任务调度日志列表
|
|
|
+ * 支持传入 tenantId 切换租户库导出。
|
|
|
*/
|
|
|
@PreAuthorize("@ss.hasPermi('monitor:job:export')")
|
|
|
@Log(title = "任务调度日志", businessType = BusinessType.EXPORT)
|
|
|
@GetMapping("/export")
|
|
|
- public AjaxResult export(SysJobLog sysJobLog)
|
|
|
+ public AjaxResult export(SysJobLog sysJobLog, @RequestParam(value = "tenantId", required = false) Long tenantId)
|
|
|
{
|
|
|
- List<SysJobLog> list = jobLogService.selectJobLogList(sysJobLog);
|
|
|
- ExcelUtil<SysJobLog> util = new ExcelUtil<SysJobLog>(SysJobLog.class);
|
|
|
- return util.exportExcel(list, "调度日志");
|
|
|
+ try {
|
|
|
+ switchIfTenant(tenantId);
|
|
|
+ List<SysJobLog> list = jobLogService.selectJobLogList(sysJobLog);
|
|
|
+ ExcelUtil<SysJobLog> util = new ExcelUtil<SysJobLog>(SysJobLog.class);
|
|
|
+ return util.exportExcel(list, "调度日志");
|
|
|
+ } finally {
|
|
|
+ clearIfSwitched(tenantId);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据调度编号获取详细信息
|
|
|
+ * 支持 tenantId 切换到对应租户库查询。
|
|
|
*/
|
|
|
@PreAuthorize("@ss.hasPermi('monitor:job:query')")
|
|
|
- @GetMapping(value = "/{configId}")
|
|
|
- public AjaxResult getInfo(@PathVariable Long jobLogId)
|
|
|
+ @GetMapping(value = "/{jobLogId}")
|
|
|
+ public AjaxResult getInfo(@PathVariable Long jobLogId, @RequestParam(value = "tenantId", required = false) Long tenantId)
|
|
|
{
|
|
|
- return AjaxResult.success(jobLogService.selectJobLogById(jobLogId));
|
|
|
+ try {
|
|
|
+ switchIfTenant(tenantId);
|
|
|
+ return AjaxResult.success(jobLogService.selectJobLogById(jobLogId));
|
|
|
+ } finally {
|
|
|
+ clearIfSwitched(tenantId);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 删除定时任务调度日志
|
|
|
+ * 支持 tenantId,删除操作会在对应租户库执行(避免跨库误删)。
|
|
|
*/
|
|
|
@PreAuthorize("@ss.hasPermi('monitor:job:remove')")
|
|
|
@Log(title = "定时任务调度日志", businessType = BusinessType.DELETE)
|
|
|
@DeleteMapping("/{jobLogIds}")
|
|
|
- public AjaxResult remove(@PathVariable Long[] jobLogIds)
|
|
|
+ public AjaxResult remove(@PathVariable Long[] jobLogIds, @RequestParam(value = "tenantId", required = false) Long tenantId)
|
|
|
{
|
|
|
- return toAjax(jobLogService.deleteJobLogByIds(jobLogIds));
|
|
|
+ try {
|
|
|
+ switchIfTenant(tenantId);
|
|
|
+ return toAjax(jobLogService.deleteJobLogByIds(jobLogIds));
|
|
|
+ } finally {
|
|
|
+ clearIfSwitched(tenantId);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 清空定时任务调度日志
|
|
|
+ * 支持 tenantId:若指定则仅清空该租户库的日志;未指定则清空当前数据源(通常为主库平台日志)。
|
|
|
*/
|
|
|
@PreAuthorize("@ss.hasPermi('monitor:job:remove')")
|
|
|
@Log(title = "调度日志", businessType = BusinessType.CLEAN)
|
|
|
@DeleteMapping("/clean")
|
|
|
- public AjaxResult clean()
|
|
|
+ public AjaxResult clean(@RequestParam(value = "tenantId", required = false) Long tenantId)
|
|
|
{
|
|
|
- jobLogService.cleanJobLog();
|
|
|
- return AjaxResult.success();
|
|
|
+ try {
|
|
|
+ switchIfTenant(tenantId);
|
|
|
+ jobLogService.cleanJobLog();
|
|
|
+ return AjaxResult.success();
|
|
|
+ } finally {
|
|
|
+ clearIfSwitched(tenantId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 内部辅助:SaaS 租户数据源临时切换(运行时通过 SpringUtils 获取,避免 fs-quartz 模块直接依赖 framework 导致循环) ====================
|
|
|
+
|
|
|
+ private Object getTenantDataSourceManager() {
|
|
|
+ try {
|
|
|
+ return SpringUtils.getBean("tenantDataSourceManager");
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void switchIfTenant(Long tenantId) {
|
|
|
+ if (tenantId == null) return;
|
|
|
+ Object mgr = getTenantDataSourceManager();
|
|
|
+ if (mgr == null) return;
|
|
|
+ try {
|
|
|
+ // 调用 ensureSwitchByTenantId(Long)
|
|
|
+ java.lang.reflect.Method m = mgr.getClass().getMethod("ensureSwitchByTenantId", Long.class);
|
|
|
+ m.invoke(mgr, tenantId);
|
|
|
+ } catch (Exception ignore) {
|
|
|
+ // 切换失败则继续用当前 ds(不影响主流程)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void clearIfSwitched(Long tenantId) {
|
|
|
+ if (tenantId == null) return;
|
|
|
+ Object mgr = getTenantDataSourceManager();
|
|
|
+ if (mgr == null) return;
|
|
|
+ try {
|
|
|
+ // 优先用 mgr.clear() 如果有,否则直接清 holder
|
|
|
+ try {
|
|
|
+ java.lang.reflect.Method clearM = mgr.getClass().getMethod("clear");
|
|
|
+ clearM.invoke(mgr);
|
|
|
+ return;
|
|
|
+ } catch (NoSuchMethodException nsme) {
|
|
|
+ // fallback
|
|
|
+ }
|
|
|
+ // fallback: 直接清 ThreadLocal
|
|
|
+ Class<?> holder = Class.forName("com.fs.framework.datasource.DynamicDataSourceContextHolder");
|
|
|
+ java.lang.reflect.Method clearHolder = holder.getMethod("clearDataSourceType");
|
|
|
+ clearHolder.invoke(null);
|
|
|
+ } catch (Exception ignore) {}
|
|
|
}
|
|
|
}
|