Forráskód Böngészése

任务基本信息 添加完成

soft5566 2 éve
szülő
commit
de0a4d3a77

+ 25 - 1
src/main/java/com/template/api/SmartDataTaskControllerAPI.java

@@ -43,7 +43,31 @@ public interface SmartDataTaskControllerAPI {
     @ApiOperation(value = "运行一次定时任务", notes = "运行一次定时任务", httpMethod = "POST")
     CommonResult runOnceJob(@Validated @RequestBody SmartDataTask smartDataTask, HttpServletRequest httpServletRequest, BindingResult bindingResult);
 
-    @PostMapping(value = "/insertSmartDataTask")
+    @GetMapping(value = "/getDepart")
+    @ApiOperation(value = "获取部门", notes = "获取部门", httpMethod = "GET")
+    CommonResult getDepart();
+
+    @GetMapping(value = "/getSyncPolicy")
+    @ApiOperation(value = "同步策略", notes = "同步策略", httpMethod = "GET")
+    CommonResult getSyncPolicy();
+
+    @GetMapping(value = "/getExchangeType")
+    @ApiOperation(value = "交换方式", notes = "交换方式", httpMethod = "GET")
+    CommonResult getExchangeType();
+
+    @PostMapping(value = "/testSql")
+    @ApiOperation(value = "测试sql", notes = "测试sql", httpMethod = "POST")
+    CommonResult testSql(@RequestBody String json);
+
+    @PostMapping(value = "/getTables")
+    @ApiOperation(value = "根据数据源获取所有的表", notes = "根据数据源获取所有的表", httpMethod = "POST")
+    CommonResult getTables(@RequestBody String json);
+
+    @PostMapping(value = "/getViews")
+    @ApiOperation(value = "根据数据源获取所有的表", notes = "根据数据源获取所有的表", httpMethod = "POST")
+    CommonResult getViews(@RequestBody String json);
+
+    @PostMapping(value = "/insertSmartDataTask1")
     @ApiOperation(value = "添加数据源任务管理数据", notes = "添加数据源任务管理数据", httpMethod = "POST")
     CommonResult insertSmartDataTask(@Validated @RequestBody SmartDataTask smartDataTask, HttpServletRequest httpServletRequest, BindingResult bindingResult);
 

+ 12 - 0
src/main/java/com/template/common/utils/CommonUtil.java

@@ -25,6 +25,18 @@ import java.util.regex.Pattern;
 @Slf4j
 public class CommonUtil {
     /**
+     * 检查字符串是否符合正则表达式
+     */
+    public static boolean checkStrByRegx(String regex, String str) {
+        //使用Pattern类的compile()方法编译正则表达式
+        Pattern pattern = Pattern.compile(regex);
+        //使用Matcher类的matcher()方法将正则表达式和字符串进行匹配
+        Matcher matcher = pattern.matcher(str);
+        //返回匹配结果
+        return matcher.matches();
+    }
+
+    /**
      * 检测客户端参数
      *
      * @param code   状态码

+ 241 - 0
src/main/java/com/template/common/utils/DBUtil.java

@@ -0,0 +1,241 @@
+package com.template.common.utils;
+
+
+import io.swagger.models.auth.In;
+
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class DBUtil {
+    private String url;
+    private String user;
+    private String password;
+    private String driver;
+
+    private String str_link = "?useUnicode=tru&characterEncoding=UTF-8&useSSL=false&useAffectedRows=true&allowPublicKeyRetrieval=true&allowMultiQueries=true&rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai";
+
+
+    public DBUtil(String url, String user, String password, String driver) {
+        this.url = url;
+        this.user = user;
+        this.password = password;
+        this.driver = driver;
+    }
+
+    public Map<String, Object> getConnection() {
+        try {
+            Class.forName(driver);
+            Connection connection = DriverManager.getConnection(url + str_link, user, password);
+            return CommonUtil.getReturnMap("0", connection);
+        } catch (SQLException | ClassNotFoundException e) {
+            return CommonUtil.getReturnMap("1", e.getMessage());
+        }
+    }
+
+    public Map<String, Object> query(Connection conn, String sql) {
+        PreparedStatement stmt = null;
+        ResultSet rs = null;
+        Map<String, Object> map = new HashMap<>();
+        try {
+            stmt = conn.prepareStatement(sql);
+            rs = stmt.executeQuery();
+        } catch (Exception e) {
+            return CommonUtil.getReturnMap("1", e.getMessage());
+        } finally {
+            map.put("rs", rs);
+            map.put("stmt", stmt);
+        }
+
+        return CommonUtil.getReturnMap("0", map);
+    }
+
+    public Map<String, Object> update(String sql) {
+        Connection conn = null;
+        PreparedStatement stmt = null;
+        int count = 0;
+        Map<String, Object> map = new HashMap<>();
+        try {
+            Map<String, Object> map_return = getConnection();
+            if (map_return.get("code") == "0") {
+                return CommonUtil.getReturnMap("1", map_return.get("msg"));
+            }
+            conn = (Connection) map_return.get("msg");
+            stmt = conn.prepareStatement(sql);
+            count = stmt.executeUpdate();
+        } catch (Exception e) {
+            return CommonUtil.getReturnMap("1", e.getMessage());
+
+        } finally {
+            map.put("count", count);
+            map.put("stmt", stmt);
+            map.put("conn", conn);
+        }
+
+        return CommonUtil.getReturnMap("0", map);
+    }
+
+    public Map<Integer, Object> getMetaDataByTable(Connection conn, String tableName) {
+        // 获取数据表的列信息
+        DatabaseMetaData dbMetaData;
+        ResultSet rs = null;
+        Map<Integer, Object> map = new HashMap<>();
+        try {
+            dbMetaData = conn.getMetaData();
+            rs = dbMetaData.getColumns(null, null, tableName, null);
+            int i = 1;
+            // 遍历列信息
+            while (rs.next()) {
+                Map<String, String> tmpMap = new HashMap<>();
+                // 获取列名称
+                String columnName = rs.getString("COLUMN_NAME");
+
+                // 获取列类型
+                String columnType = rs.getString("TYPE_NAME");
+
+                // 获取列备注
+                String columnComment = rs.getString("REMARKS");
+
+                tmpMap.put("COLUMN_NAME", columnName);
+                tmpMap.put("TYPE_NAME", columnType);
+                tmpMap.put("REMARKS", columnComment);
+
+                map.put(i++, tmpMap);
+
+                // 打印列信息
+                System.out.println("Column name: " + columnName);
+                System.out.println("Column type: " + columnType);
+                System.out.println("Column comment: " + columnComment);
+            }
+        } catch (SQLException e) {
+            throw new RuntimeException(e);
+        } finally {
+            this.closeResultSet(rs);
+            this.closeConnection(conn);
+        }
+
+        return map;
+    }
+
+    public Map<String, Object> getTableMetaData(Connection conn) {
+        try {
+            // 获取数据库元数据
+            DatabaseMetaData metaData = conn.getMetaData();
+
+            // 获取所有表名
+            ResultSet tables = metaData.getTables(null, null, "%", new String[]{"TABLE"});
+            List<String> list = new ArrayList<>();
+            while (tables.next()) {
+                String tableName = tables.getString("TABLE_NAME");
+                String comment = getTableComment(metaData, tableName);
+                System.out.println(tableName + "[" + comment + "]");
+                list.add(tableName + "[" + comment + "]");
+            }
+
+            // 关闭资源
+            tables.close();
+            conn.close();
+            return CommonUtil.getReturnMap("0", list);
+        } catch (SQLException e) {
+            return CommonUtil.getReturnMap("1", e.getMessage());
+        }
+    }
+
+    /**
+     * 获取表的备注
+     * @param metaData 数据库元数据
+     * @param tableName 表名
+     * @return 表的备注
+     * @throws SQLException
+     */
+    private static String getTableComment(DatabaseMetaData metaData, String tableName) throws SQLException {
+        String comment = null;
+        ResultSet resultSet = metaData.getTables(null, null, tableName, null);
+        if (resultSet.next()) {
+            comment = resultSet.getString("REMARKS");
+        }
+        resultSet.close();
+        return comment;
+    }
+
+    public Map<String, Object> getMetaDataBySql(Connection conn, String sql) {
+        // 执行SELECT语句
+        Statement stmt;
+        ResultSet rs;
+        ResultSetMetaData rsmd;
+        int columnCount;
+        try {
+            stmt = conn.createStatement();
+            rs = stmt.executeQuery(sql);
+            rsmd = rs.getMetaData();
+            // 获取字段数量
+            columnCount = rsmd.getColumnCount();
+        } catch (SQLException e) {
+            return CommonUtil.getReturnMap("1", e.getMessage());
+        }
+
+        List<Object> list = new ArrayList<>();
+        // 遍历字段信息
+        for (int i = 1; i <= columnCount; i++) {
+            Map<String, String> tmpMap = new HashMap<>();
+            try {
+                String columnName = rsmd.getColumnName(i);
+                String columnType = rsmd.getColumnTypeName(i);
+                String columnDisplaySize = String.valueOf(rsmd.getColumnDisplaySize(i));
+                String columnComment = rsmd.getColumnLabel(i);
+
+                tmpMap.put("COLUMN_NAME", columnName);
+                tmpMap.put("COLUMN_TYPE", columnType);
+                tmpMap.put("COLUMN_COMMENT", columnComment);
+                tmpMap.put("COLUMN_SIZE", columnDisplaySize);
+
+                list.add(tmpMap);
+            } catch (SQLException e) {
+                return CommonUtil.getReturnMap("1", e.getMessage());
+            } finally {
+                this.closeResultSet(rs);
+                this.closeStatement(stmt);
+                this.closeConnection(conn);
+            }
+        }
+
+        return CommonUtil.getReturnMap("0", list);
+    }
+
+    public void closeResultSet(ResultSet rs) {
+        if (rs != null) {
+            try {
+                rs.close();
+            } catch (SQLException e) {
+                e.printStackTrace();
+                System.out.println(e.getMessage());
+            }
+        }
+    }
+
+    public void closeStatement(Statement stmt) {
+        if (stmt != null) {
+            try {
+                stmt.close();
+            } catch (SQLException e) {
+                e.printStackTrace();
+                System.out.println(e.getMessage());
+            }
+        }
+    }
+
+    public void closeConnection(Connection conn) {
+        if (conn != null) {
+            try {
+                conn.close();
+            } catch (SQLException e) {
+                e.printStackTrace();
+                System.out.println(e.getMessage());
+            }
+        }
+    }
+}

+ 28 - 4
src/main/java/com/template/common/utils/QuartzJobUtils.java

@@ -1,5 +1,6 @@
 package com.template.common.utils;
 
+import com.template.model.pojo.SmartDataSourceJobParams;
 import com.template.model.pojo.SmartDataTask;
 import org.quartz.*;
 
@@ -18,12 +19,16 @@ public class QuartzJobUtils {
      * @param scheduler     调度器
      * @param smartDataTask 定时任务信息类
      */
-    public static Map<String, Object> createScheduleJob(Scheduler scheduler, SmartDataTask smartDataTask) {
-        // 判断是否存在该任务
+    public static Map<String, Object> createScheduleJob(Scheduler scheduler,
+                                                        SmartDataTask smartDataTask,
+                                                        SmartDataSourceJobParams dsSourceInfo,
+                                                        SmartDataSourceJobParams dsDestinationInfo) {
+        // 判断是否存在该任务,存在则不创建任务,直接返回信息提示
         Map<String, Object> exists = exists(scheduler, smartDataTask.getTkName());
         if ("0".equals(exists.get("code"))) {
             return exists;
         }
+
         try {
             Class<? extends Job> jobClass = (Class<? extends Job>) Class.forName(JobClass);
             // 构建定时任务信息
@@ -31,6 +36,25 @@ public class QuartzJobUtils {
                     .withIdentity(smartDataTask.getTkName())
                     .withDescription(smartDataTask.getTkDescrition())
                     .build();
+
+            // 创建一个JobDataMap对象,并设置参数
+            JobDataMap jobDataMap = new JobDataMap();
+            // 来源数据源参数
+            jobDataMap.put("sourceDriver", dsSourceInfo.getDsClsDriver());
+            jobDataMap.put("sourceUrl", dsSourceInfo.getDsUrl());
+            jobDataMap.put("sourceUser", dsSourceInfo.getDsUser());
+            jobDataMap.put("sourcePassword", dsSourceInfo.getDsPassword());
+            jobDataMap.put("sourceSql", dsSourceInfo.getSql());
+            // 目标数据源参数
+            jobDataMap.put("destinationDriver", dsDestinationInfo.getDsClsDriver());
+            jobDataMap.put("destinationUrl", dsDestinationInfo.getDsUrl());
+            jobDataMap.put("destinationUser", dsDestinationInfo.getDsUser());
+            jobDataMap.put("destinationPassword", dsDestinationInfo.getDsPassword());
+            jobDataMap.put("destinationSql", dsDestinationInfo.getSql());
+
+            // 将JobDataMap对象与JobDetail关联
+            jobDetail.getJobDataMap().putAll(jobDataMap);
+
             // 设置定时任务的执行方式
             CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(smartDataTask.getTkCron());
             // 构建触发器
@@ -139,9 +163,9 @@ public class QuartzJobUtils {
         JobKey jobKey = JobKey.jobKey(jobName);
         try {
             scheduler.triggerJob(jobKey);
-            return CommonUtil.getReturnMap("0","定时任务【" + jobName + "】立即运行1次成功!");
+            return CommonUtil.getReturnMap("0", "定时任务【" + jobName + "】立即运行1次成功!");
         } catch (SchedulerException e) {
-            return CommonUtil.getReturnMap("1","定时任务【" + jobName + "】立即运行1次出错:" + e.getMessage());
+            return CommonUtil.getReturnMap("1", "定时任务【" + jobName + "】立即运行1次出错:" + e.getMessage());
         }
     }
 

+ 76 - 0
src/main/java/com/template/controller/SmartDataTaskController.java

@@ -3,6 +3,7 @@ package com.template.controller;
 
 import com.template.api.SmartDataTaskControllerAPI;
 import com.template.common.utils.CommonUtil;
+import com.template.common.utils.DBUtil;
 import com.template.common.utils.paramUtils;
 import com.template.model.pojo.SmartDataSourceLog;
 import com.template.model.pojo.SmartDataTask;
@@ -12,6 +13,7 @@ import com.template.services.SmartDataSourceLogService;
 import com.template.services.SmartDataTaskService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.BindingResult;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.servlet.http.HttpServletRequest;
@@ -148,6 +150,79 @@ public class SmartDataTaskController implements SmartDataTaskControllerAPI {
     }
 
     /**
+     * 部门
+     */
+    public CommonResult getDepart() {
+        Map<String, Object> result = smartDataTaskService.getDepart();
+        if (Integer.parseInt((String) result.get("code")) == 0) {
+            return CommonResult.ok(result.get("msg"));
+        } else {
+            return CommonResult.fail((String) result.get("msg"));
+        }
+    }
+
+    /**
+     * 同步策略
+     */
+    @Override
+    public CommonResult getSyncPolicy() {
+        Map<String, Object> result = smartDataTaskService.getSyncPolicy();
+        if (Integer.parseInt((String) result.get("code")) == 0) {
+            return CommonResult.ok(result.get("msg"));
+        } else {
+            return CommonResult.fail((String) result.get("msg"));
+        }
+    }
+
+    /**
+     * 交换方式
+     */
+    @Override
+    public CommonResult getExchangeType() {
+        Map<String, Object> result = smartDataTaskService.getExchangeType();
+        if (Integer.parseInt((String) result.get("code")) == 0) {
+            return CommonResult.ok(result.get("msg"));
+        } else {
+            return CommonResult.fail((String) result.get("msg"));
+        }
+    }
+
+    /**
+     * 测试sql
+     */
+    public CommonResult testSql(String json) {
+        // 获取数据源id对应的数据源
+        Map<String, Object> result = smartDataTaskService.testSql(json);
+        if (Integer.parseInt((String) result.get("code")) == 0) {
+            return CommonResult.ok(result.get("msg"));
+        } else {
+            return CommonResult.fail((String) result.get("msg"));
+        }
+    }
+
+    @Override
+    public CommonResult getTables(String json) {
+        // 获取数据源id对应的数据源
+        Map<String, Object> result = smartDataTaskService.getTables(json);
+        if (Integer.parseInt((String) result.get("code")) == 0) {
+            return CommonResult.ok(result.get("msg"));
+        } else {
+            return CommonResult.fail((String) result.get("msg"));
+        }
+    }
+
+    @Override
+    public CommonResult getViews(String json) {
+        // 获取数据源id对应的数据源
+        Map<String, Object> result = smartDataTaskService.getViews(json);
+        if (Integer.parseInt((String) result.get("code")) == 0) {
+            return CommonResult.ok(result.get("msg"));
+        } else {
+            return CommonResult.fail((String) result.get("msg"));
+        }
+    }
+
+    /**
      * 新增任务数据库数据
      *
      * @param smartDataTask 任务数据
@@ -218,6 +293,7 @@ public class SmartDataTaskController implements SmartDataTaskControllerAPI {
 
     /**
      * 删除任务数据库数据
+     *
      * @param id id
      * @return
      */

+ 36 - 6
src/main/java/com/template/controller/Task.java

@@ -5,6 +5,7 @@ import com.template.annotation.PassToken;
 import com.template.common.utils.QuartzJobUtils;
 import com.template.mapper.SmartDataTaskMapper;
 import com.template.model.pojo.SmartDataTask;
+import org.quartz.JobDataMap;
 import org.quartz.JobDetail;
 import org.quartz.JobExecutionContext;
 import org.quartz.JobKey;
@@ -25,14 +26,14 @@ public class Task extends QuartzJobBean {
     @PassToken
     protected void executeInternal(JobExecutionContext jobExecutionContext) {
         // 输出当前时间
-        Date date = new Date();
-        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-        String dateString = dateFormat.format(date);
+//        Date date = new Date();
+//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+//        String dateString = dateFormat.format(date);
+//
+//        System.out.println("定时任务名称:【" + key.getName() + "】,执行时间:" + dateString);
 
         JobDetail jobDetail = jobExecutionContext.getJobDetail();
         JobKey key = jobDetail.getKey();
-        // 工作内容
-        System.out.println("定时任务名称:【" + key.getName() + "】,执行时间:" + dateString);
         // 及时更新下次更新时间
         QueryWrapper<SmartDataTask> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq(key.getName() != null, "tk_name", key.getName());
@@ -42,12 +43,41 @@ public class Task extends QuartzJobBean {
             String nextExeTime = QuartzJobUtils.getNextExeTime(smartDataTask.getTkCron());
             smartDataTask.setTkNextExeTime(nextExeTime);
             try {
-                smartDataTaskMapper.updateById(smartDataTask);
+                smartDataTaskMapper.markTaskById(smartDataTask);
             } catch (Exception e) {
                 System.out.println(e.getMessage());
             }
         } else {
             System.out.println("【下次执行的时间】无法更新至数据库中!");
         }
+
+        // 工作内容
+        JobDataMap jobDataMap = jobDetail.getJobDataMap();
+        // 来源数据源参数
+        String sourceDriver = (String) jobDataMap.get("sourceDriver");
+        String sourceUrl = (String) jobDataMap.get("sourceUrl");
+        String sourceUser = (String) jobDataMap.get("sourceUser");
+        String sourcePassword = (String) jobDataMap.get("sourcePassword");
+        String sourceSql = (String) jobDataMap.get("sourceSql");
+        // 目标数据源参数
+        String destinationDriver = (String) jobDataMap.get("destinationDriver");
+        String destinationUrl = (String) jobDataMap.get("destinationUrl");
+        String destinationUser = (String) jobDataMap.get("destinationUser");
+        String destinationPassword = (String) jobDataMap.get("destinationPassword");
+        String destinationSql = (String) jobDataMap.get("destinationSql");
+
+        System.out.println("来源库参数:==============================" + "\n" +
+                "    driver:" + sourceDriver + "\n" +
+                "       url:" + sourceUrl + "\n" +
+                "      user:" + sourceUser + "\n" +
+                "  password:" + sourcePassword + "\n" +
+                "       sql:" + sourceSql);
+        System.out.println("目标库参数:==============================" + "\n" +
+                "    driver:" + destinationDriver + "\n" +
+                "       url:" + destinationUrl + "\n" +
+                "      user:" + destinationUser + "\n" +
+                "  password:" + destinationPassword + "\n" +
+                "       sql:" + destinationSql);
+
     }
 }

+ 33 - 0
src/main/java/com/template/mapper/SmartDataSourceMapper.java

@@ -2,8 +2,14 @@ package com.template.mapper;
 
 import com.template.model.pojo.SmartDataSource;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.template.model.pojo.SmartDataSourceJobParams;
+import com.template.model.pojo.SmartDepartment;
+import com.template.model.pojo.SmartDsColumnCor;
+import org.apache.ibatis.annotations.Select;
 import org.springframework.stereotype.Repository;
 
+import java.util.List;
+
 /**
  * <p>
  * 数据源配置 Mapper 接口
@@ -15,4 +21,31 @@ import org.springframework.stereotype.Repository;
 @Repository
 public interface SmartDataSourceMapper extends BaseMapper<SmartDataSource> {
 
+    @Select("SELECT " +
+            "    ds_id " +
+            "    ,c.ds_cls_id ds_cls_id " +
+            "    ,ds_cls_name " +
+            "    ,ds_cls_driver " +
+            "    ,ds_name " +
+            "    ,ds_url " +
+            "    ,ds_user " +
+            "    ,ds_password " +
+            "    ,ds_status " +
+            "    ,ds_descrition " +
+            "    ,ds_source " +
+            "    ,ds_target " +
+            "FROM " +
+            "    smart_data_class c, " +
+            "    smart_data_source s  " +
+            "WHERE " +
+            "    s.ds_id = #{id} " +
+            "    AND c.ds_cls_id = s.ds_cls_id ")
+    SmartDataSourceJobParams getDataSourceInfo(Integer id);
+
+    @Select("SELECT " +
+            "    *  " +
+            "FROM " +
+            "    smart_department ")
+    List<SmartDepartment> getDepart();
+
 }

+ 41 - 0
src/main/java/com/template/model/pojo/SmartDataSourceJobParams.java

@@ -0,0 +1,41 @@
+package com.template.model.pojo;
+
+import lombok.Data;
+
+/**
+ * <p>
+ * 数据源配置
+ * </p>
+ *
+ * @author ceshi
+ * @since 2023-12-05
+ */
+@Data
+public class SmartDataSourceJobParams {
+
+    private Integer dsId;
+
+    private Integer dsClsId;
+
+    private String dsName;
+
+    private String dsClsName;
+
+    private String dsClsDriver;
+
+    private String dsUrl;
+
+    private String dsUser;
+
+    private String sql;
+
+    private String dsPassword;
+
+    private Integer dsStatus;
+
+    private String dsDescrition;
+
+    private Integer dsSource;
+
+    private Integer dsTarget;
+}

+ 31 - 4
src/main/java/com/template/model/pojo/SmartDataTask.java

@@ -36,6 +36,9 @@ public class SmartDataTask implements Serializable {
     @ApiModelProperty(value = "任务名称")
     private String tkName;
 
+    @ApiModelProperty(value = "部门id")
+    private Integer tkDtId;
+
     @ApiModelProperty(value = "来源数据源id")
     private Integer tkDsIdSource;
 
@@ -45,7 +48,7 @@ public class SmartDataTask implements Serializable {
     @ApiModelProperty(value = "交换方式:0自定义SQL语句,1数据视图,2数据表")
     private Integer tkExchangeType;
 
-    @ApiModelProperty(value = "任务使用的SQL语")
+    @ApiModelProperty(value = "任务使用的SQL语")
     private String tkSql;
 
     @ApiModelProperty(value = "目标数据源id")
@@ -54,6 +57,30 @@ public class SmartDataTask implements Serializable {
     @ApiModelProperty(value = "目标数据表")
     private String tkDestTable;
 
+    @ApiModelProperty(value = "交换服务器:0自动,1手动")
+    private Integer tkExchangeServer;
+
+    @ApiModelProperty(value = "交换服务器id")
+    private Integer tkExchangeServerId;
+
+    @ApiModelProperty(value = "运行参数配置:0自动,1手动")
+    private Integer tkOptCfgAutoManual;
+
+    @ApiModelProperty(value = "运行参数配置:记录数")
+    private Integer tkOptCfgRsNum;
+
+    @ApiModelProperty(value = "运行参数配置:线程数")
+    private Integer tkOptCfgThreadsNum;
+
+    @ApiModelProperty(value = "是否记录错误数据:0是,1否")
+    private Integer tkRsIncorrectData;
+
+    @ApiModelProperty(value = "来源数据源字符集:UTF8 或 GBK")
+    private String tkDsSourceCharset;
+
+    @ApiModelProperty(value = "目的数据源字符集:UTF8 或 GBK")
+    private String tkDsDestinationCharset;
+
     @ApiModelProperty(value = "任务定时表达式")
     private String tkCron;
 
@@ -66,12 +93,12 @@ public class SmartDataTask implements Serializable {
     @ApiModelProperty(value = "重复时间")
     private String tkRepetTime;
 
-    @ApiModelProperty(value = "任务描述")
-    private String tkDescrition;
-
     @ApiModelProperty(value = "是否激活")
     private Integer tkActivation;
 
+    @ApiModelProperty(value = "任务描述")
+    private String tkDescrition;
+
     @ApiModelProperty(value = "删除标记")
     private Integer tkDeleted;
 

+ 13 - 0
src/main/java/com/template/model/pojo/SmartDsColumnCor.java

@@ -0,0 +1,13 @@
+package com.template.model.pojo;
+
+import lombok.Data;
+
+@Data
+public class SmartDsColumnCor {
+    private String colSource;
+    private String colDestination;
+    private int updatePrimaryKey;
+    private int isExchange;
+    private int isUpdate;
+    private int markIncrement;
+}

+ 12 - 0
src/main/java/com/template/services/SmartDataTaskService.java

@@ -38,4 +38,16 @@ public interface SmartDataTaskService extends IService<SmartDataTask> {
     Map<String, Object> deleteJob(SmartDataTask smartDataTask);
 
     Map<String, Object> runOnceJob(SmartDataTask smartDataTask);
+
+    Map<String, Object> testSql(String json);
+
+    Map<String, Object> getDepart();
+
+    Map<String, Object> getSyncPolicy();
+
+    Map<String, Object> getExchangeType();
+
+    Map<String, Object> getTables(String json);
+
+    Map<String, Object> getViews(String json);
 }

+ 10 - 10
src/main/java/com/template/services/impl/SmartDataSourceServiceImpl.java

@@ -71,12 +71,12 @@ public class SmartDataSourceServiceImpl extends ServiceImpl<SmartDataSourceMappe
 
         QueryWrapper<SmartDataSource> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq(smartDataSource.getDsClsId() != null, "ds_cls_id", smartDataSource.getDsClsId());
-        queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsName()), "ds_name", smartDataSource.getDsName());
-        queryWrapper.like(StringUtils.hasText(smartDataSource.getDsUrl()), "ds_url", smartDataSource.getDsUrl());
-        queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsUser()), "ds_user", smartDataSource.getDsUser());
-        queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsPassword()), "ds_password", smartDataSource.getDsPassword());
+        queryWrapper.eq(smartDataSource.getDsName() != null, "ds_name", smartDataSource.getDsName());
+        queryWrapper.eq(smartDataSource.getDsUrl() != null, "ds_url", smartDataSource.getDsUrl());
+        queryWrapper.eq(smartDataSource.getDsUser() != null, "ds_user", smartDataSource.getDsUser());
+        queryWrapper.eq(smartDataSource.getDsPassword() != null, "ds_password", smartDataSource.getDsPassword());
         queryWrapper.eq(smartDataSource.getDsStatus() != null, "ds_status", smartDataSource.getDsStatus());
-        queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsDescrition()), "ds_descrition", smartDataSource.getDsDescrition());
+        queryWrapper.eq(smartDataSource.getDsDescrition() != null, "ds_descrition", smartDataSource.getDsDescrition());
         List<SmartDataSource> smartDataSources = smartDataSourceMapper.selectList(queryWrapper);
         if (smartDataSources.size() > 0) {
             return CommonUtil.getReturnMap("1", "有重复记录!");
@@ -135,12 +135,12 @@ public class SmartDataSourceServiceImpl extends ServiceImpl<SmartDataSourceMappe
         QueryWrapper<SmartDataSource> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq(smartDataSource.getDsId() != null, "ds_id", smartDataSource.getDsId());
         queryWrapper.eq(smartDataSource.getDsClsId() != null, "ds_cls_id", smartDataSource.getDsClsId());
-        queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsName()), "ds_name", smartDataSource.getDsName());
-        queryWrapper.like(StringUtils.hasText(smartDataSource.getDsUrl()), "ds_url", smartDataSource.getDsUrl());
-        queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsUser()), "ds_user", smartDataSource.getDsUser());
-        queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsPassword()), "ds_password", smartDataSource.getDsPassword());
+        queryWrapper.eq(smartDataSource.getDsName() != null, "ds_name", smartDataSource.getDsName());
+        queryWrapper.eq(smartDataSource.getDsUrl() != null, "ds_url", smartDataSource.getDsUrl());
+        queryWrapper.eq(smartDataSource.getDsUser() != null, "ds_user", smartDataSource.getDsUser());
+        queryWrapper.eq(smartDataSource.getDsPassword() != null, "ds_password", smartDataSource.getDsPassword());
         queryWrapper.eq(smartDataSource.getDsStatus() != null, "ds_status", smartDataSource.getDsStatus());
-        queryWrapper.eq(StringUtils.hasText(smartDataSource.getDsDescrition()), "ds_descrition", smartDataSource.getDsDescrition());
+        queryWrapper.eq(smartDataSource.getDsDescrition() != null, "ds_descrition", smartDataSource.getDsDescrition());
         List<SmartDataSource> smartDataSources = smartDataSourceMapper.selectList(queryWrapper);
         if (smartDataSources.size() > 0) {
             return CommonUtil.getReturnMap("1", "数据未修改,请修改后再提交!");

+ 285 - 35
src/main/java/com/template/services/impl/SmartDataTaskServiceImpl.java

@@ -1,16 +1,18 @@
 package com.template.services.impl;
 
+import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
 import com.template.common.utils.CommonUtil;
+import com.template.common.utils.DBUtil;
 import com.template.common.utils.QuartzJobUtils;
 import com.template.mapper.SmartDataSourceMapper;
 import com.template.mapper.SmartDataTaskMapper;
-import com.template.model.pojo.SmartDataSource;
-import com.template.model.pojo.SmartDataSourceLog;
-import com.template.model.pojo.SmartDataTask;
+import com.template.model.pojo.*;
 import com.template.model.result.CommonResult;
 import com.template.model.result.PageUtils;
 import com.template.services.SmartDataTaskService;
@@ -24,6 +26,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 
 import javax.servlet.http.HttpServletRequest;
+import java.sql.*;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeParseException;
@@ -54,16 +57,26 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
     @Override
     public Map<String, Object> insertSmartDataTask(SmartDataTask smartDataTask) {
         // 检测参数,还有是否存在重复记录
+        // 任务属性
         if (smartDataTask.getTkName() == null) {
             return CommonUtil.getReturnMap("1", "【任务名称】不能为空!");
         }
+        if (!CommonUtil.checkStrByRegx("^[a-zA-Z0-9]{5,32}$", smartDataTask.getTkName())) {
+            return CommonUtil.getReturnMap("1", "【任务名称】只能包含字母、数字,且长度为5-32位!");
+        }
         QueryWrapper<SmartDataTask> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq(smartDataTask.getTkName() != null, "tk_name", smartDataTask.getTkName());
         SmartDataTask sdt = smartDataTaskMapper.selectOne(queryWrapper);
         if (sdt != null) {
             return CommonUtil.getReturnMap("1", "任务名有重名!");
         }
-        // 检测必要参数是否为null
+        if (smartDataTask.getTkDtId() == null) {
+            return CommonUtil.getReturnMap("1", "【部门id】不能为空!");
+        }
+        if (smartDataTask.getTkSyncPolicy() == null) {
+            return CommonUtil.getReturnMap("1", "【同步策略】不能为空!");
+        }
+        // 来源库设置
         if (smartDataTask.getTkDsIdSource() == null) {
             return CommonUtil.getReturnMap("1", "【来源数据源id】不能为空!");
         }
@@ -71,15 +84,13 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
         if (smartDataSource == null) {
             return CommonUtil.getReturnMap("1", "选择的【来源数据源】不存在!");
         }
-        if (smartDataTask.getTkSyncPolicy() == null) {
-            return CommonUtil.getReturnMap("1", "【同步策略】不能为空!");
-        }
         if (smartDataTask.getTkExchangeType() == null) {
             return CommonUtil.getReturnMap("1", "【交换方式】不能为空!");
         }
         if (smartDataTask.getTkSql() == null) {
-            return CommonUtil.getReturnMap("1", "【数据来源SQL语句】不能为空!");
+            return CommonUtil.getReturnMap("1", "【自定义SQL语句】不能为空!");
         }
+        // 目的库设置
         if (smartDataTask.getTkDsIdDestination() == null) {
             return CommonUtil.getReturnMap("1", "【目标数据源id】不能为空!");
         }
@@ -90,42 +101,54 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
         if (smartDataTask.getTkDestTable() == null) {
             return CommonUtil.getReturnMap("1", "【目标数据表】不能为空!");
         }
-        if (smartDataTask.getTkManualOrAuto() == null) {
-            return CommonUtil.getReturnMap("1", "【手动或定时执行】不能为空!");
+        // 高级设置
+        if (smartDataTask.getTkExchangeServer() == null) {
+            return CommonUtil.getReturnMap("1", "【交换服务器】不能为空!");
         }
-        if (smartDataTask.getTkManualOrAuto() == 0) {
-            if (smartDataTask.getTkExeType() == null) {
-                return CommonUtil.getReturnMap("1", "【执行方式】不能为空!");
+        if (smartDataTask.getTkExchangeServer() == 1) {
+            if (smartDataTask.getTkExchangeServerId() == null) {
+                return CommonUtil.getReturnMap("1", "【指定服务器id】不能为空!");
             }
-            if (smartDataTask.getTkRepetTime() == null) {
-                return CommonUtil.getReturnMap("1", "【重复时间】不能为空!");
-            }
-            // 生成cron表达式
-            Map<String, Object> stringObjectMap = generateCron(smartDataTask.getTkExeType(), smartDataTask.getTkRepetTime());
-            if (stringObjectMap.get("code") == "1") {
-                return stringObjectMap;
+        }
+        if (smartDataTask.getTkOptCfgAutoManual()== null) {
+            return CommonUtil.getReturnMap("1", "【运行参数配置】不能为空!");
+        }
+        if (smartDataTask.getTkOptCfgAutoManual() == 1){
+            if (smartDataTask.getTkOptCfgRsNum() == null) {
+                return CommonUtil.getReturnMap("1", "【运行参数配置:记录数】不能为空!");
             }
-            String cron = (String) stringObjectMap.get("msg");
-            if (CronExpression.isValidExpression(cron)) {
-                smartDataTask.setTkCron(cron);
-            } else {
-                return CommonUtil.getReturnMap("1", "生成的【定时表达式】不正确!请联系管理员!" + cron);
+            if (smartDataTask.getTkOptCfgThreadsNum() == null) {
+                return CommonUtil.getReturnMap("1", "【运行参数配置:线程数】不能为空!");
             }
-        } else {
-            smartDataTask.setTkExeType(-1);
-            smartDataTask.setTkRepetTime("");
         }
-        if (smartDataTask.getTkDescrition() == null) {
-            return CommonUtil.getReturnMap("1", "【任务描述】不能为空!");
+        if (smartDataTask.getTkRsIncorrectData() == null) {
+            return CommonUtil.getReturnMap("1", "【是否记录错误数据】不能为空!");
+        }
+        if (smartDataTask.getTkDsSourceCharset() == null) {
+            return CommonUtil.getReturnMap("1", "【来源数据源字符集】不能为空!");
         }
+        if (smartDataTask.getTkDsDestinationCharset() == null) {
+            return CommonUtil.getReturnMap("1", "【目标数据源字符集】不能为空!");
+        }
+
+        queryWrapper.eq(smartDataTask.getTkDtId() != null, "tk_dt_id", smartDataTask.getTkDtId());
         queryWrapper.eq(smartDataTask.getTkDsIdSource() != null, "tk_ds_id_source", smartDataTask.getTkDsIdSource());
         queryWrapper.eq(smartDataTask.getTkSyncPolicy() != null, "tk_sync_policy", smartDataTask.getTkSyncPolicy());
         queryWrapper.eq(smartDataTask.getTkExchangeType() != null, "tk_exchange_type", smartDataTask.getTkExchangeType());
         queryWrapper.eq(StringUtils.hasText(smartDataTask.getTkSql()), "tk_sql", smartDataTask.getTkSql());
         queryWrapper.eq(smartDataTask.getTkDsIdDestination() != null, "tk_ds_id_destination", smartDataTask.getTkDsIdDestination());
         queryWrapper.eq(smartDataTask.getTkDestTable() != null, "tk_dest_table", smartDataTask.getTkDestTable());
-        queryWrapper.eq(smartDataTask.getTkManualOrAuto() != null, "tk_manual_or_auto", smartDataTask.getTkManualOrAuto());
-        queryWrapper.eq(StringUtils.hasText(smartDataTask.getTkDescrition()), "tk_descrition", smartDataTask.getTkDescrition());
+        queryWrapper.eq(smartDataTask.getTkExchangeServer() != null, "tk_exchange_server", smartDataTask.getTkExchangeServer());
+        queryWrapper.eq(smartDataTask.getTkExchangeServer() != null && smartDataTask.getTkExchangeServerId() != null,
+                "tk_exchange_server_id", smartDataTask.getTkExchangeServerId());
+        queryWrapper.eq(smartDataTask.getTkOptCfgAutoManual() != null, "tk_opt_cfg_auto_manual", smartDataTask.getTkOptCfgAutoManual());
+        queryWrapper.eq(smartDataTask.getTkOptCfgAutoManual() != null && smartDataTask.getTkOptCfgRsNum() != null,
+                "tk_opt_cfg_rs_num", smartDataTask.getTkOptCfgRsNum());
+        queryWrapper.eq(smartDataTask.getTkOptCfgAutoManual() != null && smartDataTask.getTkOptCfgThreadsNum() != null,
+                "tk_opt_cfg_threads_num", smartDataTask.getTkOptCfgThreadsNum());
+        queryWrapper.eq(smartDataTask.getTkRsIncorrectData() != null, "tk_rs_incorrect_data", smartDataTask.getTkRsIncorrectData());
+        queryWrapper.eq(smartDataTask.getTkDsSourceCharset() != null, "tk_ds_source_charset", smartDataTask.getTkDsSourceCharset());
+        queryWrapper.eq(StringUtils.hasText(smartDataTask.getTkDsDestinationCharset()), "tk_ds_destination_charset", smartDataTask.getTkDsDestinationCharset());
         sdt = smartDataTaskMapper.selectOne(queryWrapper);
         if (sdt != null) {
             return CommonUtil.getReturnMap("1", "有重复记录!");
@@ -137,6 +160,35 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
         } else {
             return CommonUtil.getReturnMap("1", "任务添加失败!");
         }
+
+//        if (smartDataTask.getTkManualOrAuto() == null) {
+//            return CommonUtil.getReturnMap("1", "【手动或定时执行】不能为空!");
+//        }
+//        if (smartDataTask.getTkManualOrAuto() == 0) {
+//            if (smartDataTask.getTkExeType() == null) {
+//                return CommonUtil.getReturnMap("1", "【执行方式】不能为空!");
+//            }
+//            if (smartDataTask.getTkRepetTime() == null) {
+//                return CommonUtil.getReturnMap("1", "【重复时间】不能为空!");
+//            }
+//            // 生成cron表达式
+//            Map<String, Object> stringObjectMap = generateCron(smartDataTask.getTkExeType(), smartDataTask.getTkRepetTime());
+//            if (stringObjectMap.get("code") == "1") {
+//                return stringObjectMap;
+//            }
+//            String cron = (String) stringObjectMap.get("msg");
+//            if (CronExpression.isValidExpression(cron)) {
+//                smartDataTask.setTkCron(cron);
+//            } else {
+//                return CommonUtil.getReturnMap("1", "生成的【定时表达式】不正确!请联系管理员!" + cron);
+//            }
+//        } else {
+//            smartDataTask.setTkExeType(-1);
+//            smartDataTask.setTkRepetTime("");
+//        }
+//        if (smartDataTask.getTkDescrition() == null) {
+//            return CommonUtil.getReturnMap("1", "【任务描述】不能为空!");
+//        }
     }
 
     // 生成cron表达式
@@ -214,7 +266,7 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
                 int minute = Integer.parseInt(hour_minute[1]);
 
                 return CommonUtil.getReturnMap("0", String.format("0 %d %d ? * %s", minute, hour, weekday));
-            } else  {
+            } else {
                 return CommonUtil.getReturnMap("1", "【重复时间】格式错误3,要求:周日 09:40");
             }
         } else if (exeType == 4) {
@@ -234,7 +286,7 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
                 int minute = Integer.parseInt(hour_minute[1]);
 
                 return CommonUtil.getReturnMap("0", String.format("0 %d %d %d * ?", minute, hour, day));
-            } else  {
+            } else {
                 return CommonUtil.getReturnMap("1", "【重复时间】格式错误3,要求:19 09:40");
             }
         } else {
@@ -414,7 +466,17 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
         }
 
         SmartDataTask smartDataTask_return = (SmartDataTask) tmp_map.get("msg");
-        Map<String, Object> returnMap = QuartzJobUtils.createScheduleJob(scheduler, smartDataTask_return);
+        // 来源数据源id
+        Integer tkDsIdSource = smartDataTask_return.getTkDsIdSource();
+        // 目标数据源id
+        Integer tkDsIdDestination = smartDataTask_return.getTkDsIdDestination();
+        // 根据id,获取数据源url、user、password、driver等
+        SmartDataSourceJobParams dsSourceInfo = smartDataSourceMapper.getDataSourceInfo(tkDsIdSource);
+        dsSourceInfo.setSql(smartDataTask_return.getTkSql());
+        SmartDataSourceJobParams dsDestinationInfo = smartDataSourceMapper.getDataSourceInfo(tkDsIdDestination);
+        dsDestinationInfo.setSql(smartDataTask_return.getTkSql());
+
+        Map<String, Object> returnMap = QuartzJobUtils.createScheduleJob(scheduler, smartDataTask_return, dsSourceInfo, dsDestinationInfo);
         if ("0".equals(returnMap.get("code"))) {
             smartDataTask.setTkId(smartDataTask_return.getTkId());
             // 下次执行的时间
@@ -510,4 +572,192 @@ public class SmartDataTaskServiceImpl extends ServiceImpl<SmartDataTaskMapper, S
 
         return QuartzJobUtils.runOnce(scheduler, smartDataTask_return.getTkName());
     }
+
+    @Override
+    public Map<String, Object> getDepart() {
+        // 获取部门
+        List<SmartDepartment> depart = smartDataSourceMapper.getDepart();
+        if (depart != null) {
+            return CommonUtil.getReturnMap("0", depart);
+        } else {
+            return CommonUtil.getReturnMap("1", "部门为空");
+        }
+    }
+
+    @Override
+    public Map<String, Object> getSyncPolicy() {
+        // 获取同步策略: 0插入更新,1更新标记,2清空插入
+        Map<String, Object> syncPolicy1 = new HashMap<>();
+        Map<String, Object> syncPolicy2 = new HashMap<>();
+        Map<String, Object> syncPolicy3 = new HashMap<>();
+        syncPolicy1.put("name", "插入更新");
+        syncPolicy1.put("value", 0);
+
+        syncPolicy2.put("name", "更新标记");
+        syncPolicy2.put("value", 1);
+
+        syncPolicy3.put("name", "清空插入");
+        syncPolicy3.put("value", 2);
+
+        List<Map<String, Object>> list = new ArrayList<>();
+        list.add(syncPolicy1);
+        list.add(syncPolicy2);
+        list.add(syncPolicy3);
+
+        return CommonUtil.getReturnMap("0", list);
+    }
+
+    @Override
+    public Map<String, Object> getExchangeType() {
+        // 获取同步策略: 0插入更新,1更新标记,2清空插入
+        Map<String, Object> syncPolicy1 = new HashMap<>();
+        Map<String, Object> syncPolicy2 = new HashMap<>();
+        Map<String, Object> syncPolicy3 = new HashMap<>();
+        syncPolicy1.put("name", "自定义SQL语句");
+        syncPolicy1.put("value", 0);
+
+        syncPolicy2.put("name", "数据视图");
+        syncPolicy2.put("value", 1);
+
+        syncPolicy3.put("name", "数据表");
+        syncPolicy3.put("value", 2);
+
+        List<Map<String, Object>> list = new ArrayList<>();
+        list.add(syncPolicy1);
+        list.add(syncPolicy2);
+        list.add(syncPolicy3);
+
+        return CommonUtil.getReturnMap("0", list);
+    }
+
+    @Override
+    public Map<String, Object> testSql(String json) {
+        if (json == null) {
+            return CommonUtil.getReturnMap("1", "json参数为空");
+        }
+
+        int dsIdSource, exchangeType;
+        String sql;
+        try {
+            JSONObject jsonObject = JSONObject.parseObject(json);
+            dsIdSource = jsonObject.getInteger("dsIdSource");
+            exchangeType = jsonObject.getInteger("exchangeType");
+            sql = jsonObject.getString("sql");
+        } catch (Exception e) {
+            return CommonUtil.getReturnMap("1", "请检查参数:【数据源id】、【交换方式】、【自定义sql语句】是否为空");
+        }
+        // 获取数据源id对应的数据源
+        SmartDataSourceJobParams dataSourceInfo = smartDataSourceMapper.getDataSourceInfo(dsIdSource);
+        // 只有sql语句的交换方式才需要检查
+        if (exchangeType == 0) {
+            // sql语句
+            if (dataSourceInfo != null) {
+                DBUtil dbUtil = new DBUtil(dataSourceInfo.getDsUrl(), dataSourceInfo.getDsUser(), dataSourceInfo.getDsPassword(), dataSourceInfo.getDsClsDriver());
+                Map<String, Object> map_return = dbUtil.getConnection();
+                if (map_return.get("code") == "0") {
+                    Map<String, Object> metaDataBySql = dbUtil.getMetaDataBySql((Connection) map_return.get("msg"), sql);
+                    if (metaDataBySql.get("code") == "0") {
+                        return CommonUtil.getReturnMap("0", "SQL语句正确");
+                        // 解析字段名称、字段类型、字段大小等
+//                        return CommonUtil.getReturnMap("0", metaDataBySql.get("msg"));
+                    } else {
+                        return CommonUtil.getReturnMap("1", metaDataBySql.get("msg"));
+                    }
+                } else {
+                    return CommonUtil.getReturnMap("1", map_return.get("msg"));
+                }
+            } else {
+                return CommonUtil.getReturnMap("1", "来源数据源不存在");
+            }
+        } else {
+            return CommonUtil.getReturnMap("1", "只有【自定义SQL语句】的交换方式才需要测试sql");
+        }
+    }
+
+    @Override
+    // 获取表
+    public Map<String, Object> getTables(String json) {
+        // 查询数据库中的表
+        String sql = "SELECT TABLE_NAME tname,TABLE_COMMENT tcomment FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{db}';";
+        // 调用getTablesOrViews方法,传入json和sql,获取表
+        return getTablesOrViews(json, sql);
+    }
+
+    @Override
+    //获取视图
+    public Map<String, Object> getViews(String json) {
+        // 查询数据库中指定数据库的视图
+        String sql = "SELECT TABLE_NAME tname,TABLE_COMMENT tcomment FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{db}' AND TABLE_TYPE = 'VIEW';";
+        // 返回视图
+        return getTablesOrViews(json, sql);
+    }
+
+    // 获取表或视图信息
+    private Map<String, Object> getTablesOrViews(String json, String sql) {
+        // 判断json是否为空
+        if (json == null) {
+            // 如果为空,返回提示信息
+            return CommonUtil.getReturnMap("1", "json参数为空");
+        }
+
+        int dsIdSource;
+        try {
+            // 将json字符串转换为JSONObject对象
+            JSONObject jsonObject = JSONObject.parseObject(json);
+            // 从JSONObject对象中获取dsIdSource的值
+            dsIdSource = jsonObject.getInteger("dsIdSource");
+        } catch (Exception e) {
+            // 如果获取失败,返回提示信息
+            return CommonUtil.getReturnMap("1", "请检查参数:【数据源id】是否为空");
+        }
+        // 获取数据源信息
+        SmartDataSourceJobParams dataSourceInfo = smartDataSourceMapper.getDataSourceInfo(dsIdSource);
+        if (dataSourceInfo != null) {
+            // 创建DBUtil对象
+            DBUtil dbUtil = new DBUtil(dataSourceInfo.getDsUrl(), dataSourceInfo.getDsUser(), dataSourceInfo.getDsPassword(), dataSourceInfo.getDsClsDriver());
+            // 获取连接
+            Map<String, Object> map_return = dbUtil.getConnection();
+            if (map_return.get("code") == "0") {
+                // 获取连接
+                Connection conn = (Connection) map_return.get("msg");
+                // 获取数据库名
+                String db = dataSourceInfo.getDsUrl().substring(dataSourceInfo.getDsUrl().lastIndexOf("/") + 1);
+                // 查询表或视图信息
+                sql = sql.replace("{db}", db);
+                Map<String, Object> tableMetaData = dbUtil.query(conn, sql);
+                if (tableMetaData.get("code") == "0") {
+                    Map<String, Object> map = (Map<String, Object>) tableMetaData.get("msg");
+                    ResultSet rs = (ResultSet) map.get("rs");
+                    PreparedStatement stmt = (PreparedStatement) map.get("stmt");
+                    try {
+                        List<String> list = new ArrayList<>();
+                        while (rs.next()) {
+                            // 将表名和表注释添加到list中
+                            list.add(rs.getString("tname") + " [" + rs.getString("tcomment") + "]");
+                        }
+                        // 关闭结果集
+                        dbUtil.closeResultSet(rs);
+                        // 关闭预处理语句
+                        dbUtil.closeStatement(stmt);
+                        // 关闭连接
+                        dbUtil.closeConnection(conn);
+                        // 返回表信息
+                        return CommonUtil.getReturnMap("0", list);
+                    } catch (SQLException e) {
+                        // 如果出现异常,返回异常信息
+                        return CommonUtil.getReturnMap("1", e.getMessage());
+                    }
+                } else {
+                    // 如果查询失败,返回查询失败信息
+                    return CommonUtil.getReturnMap("1", tableMetaData.get("msg"));
+                }
+            } else {
+                // 如果获取连接失败,返回获取连接失败信息
+                return CommonUtil.getReturnMap("1", map_return.get("msg"));
+            }
+        } else {
+            // 如果数据源不存在,返回提示信息
+            return CommonUtil.getReturnMap("1", "数据源不存在");
+        }
+    }
 }