Просмотр исходного кода

处理缺少的表 增加数据库迁移的处理配置。

CodeLife Leno 2 месяцев назад
Родитель
Сommit
c5888a2ad5

+ 15 - 1
DBScript/李庚谱/001-DDL.sql

@@ -1099,4 +1099,18 @@ create table if not exists sys_task_all_execute (
 	val text not null comment '执行任务的标记, SQL 就是执行的SQL, BEAN 就是要执行任务的Service 一定是通过 @Service 标记的才可以',
 	valtype varchar(60) not null comment '执行任务的类型 SQL 或 BEAN 扩展请继承 TaskAllExecuteMain',
 	primary key (id)
-)comment='补录全部完成的任务执行表';
+)comment='补录全部完成的任务执行表';
+
+
+-- --------------------------
+-- TABLE structure for sys_input_suptaskrun_taskallstatus
+-- --------------------------
+drop TABLE if exists sys_input_suptaskrun_taskallstatus;
+create table if not exists sys_input_suptaskrun_taskallstatus (
+	term varchar(60) not null comment '期次',
+	termDT varchar(60) not null comment '期次,年月日格式,一般为月底',
+	taskCode varchar(60) not null comment '补录任务的编号',
+	isover varchar(60) not null comment '补录任务是否完成 YESORNO',
+	primary key (term, taskCode)
+)
+comment='补录任务按照频率的补录任务状态,在同一起次完成时,才会继续下一步';

+ 57 - 30
Procedure/backend/project/src/main/java/com/sundata/config/DefaultDataSourceConfig.java

@@ -1,11 +1,16 @@
 package com.sundata.config;
 
-import javax.sql.DataSource;
-
+import cn.hutool.setting.Setting;
+import com.sundata.datasource.DataSources;
+import com.sundata.datasource.EncryptedDatasource;
+import com.zaxxer.hikari.HikariConfig;
+import com.zaxxer.hikari.HikariDataSource;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.mybatis.spring.SqlSessionFactoryBean;
 import org.mybatis.spring.SqlSessionTemplate;
 import org.mybatis.spring.annotation.MapperScan;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -15,36 +20,58 @@ import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Primary;
 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 
-import com.sundata.datasource.EncryptedDatasource;
+import javax.sql.DataSource;
 
 @Configuration
 @MapperScan(basePackages = "com.sundata.**.mapper", sqlSessionTemplateRef = "sqlSessionTemplate")
 public class DefaultDataSourceConfig {
-	@Value("${spring.datasource.encrypted:false}")
-	private Boolean encrypted;
-
-	@Bean
-	@Primary
-	@ConfigurationProperties(prefix = "spring.datasource")
-	public DataSource dataSource() {
-		if (this.encrypted) return new EncryptedDatasource();
-		return DataSourceBuilder.create().build();
-	}
-
-	@Bean
-	public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource datasource)
-			throws Exception {
-		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
-		bean.setDataSource(datasource);
-		bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
-		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/sundata/**/mybatis/*.xml"));
-		return bean.getObject();// mybatis xml所在
-	}
-
-	@Bean
-	@Primary
-	public SqlSessionTemplate sqlSessionTemplate(
-			@Qualifier("sqlSessionFactory") SqlSessionFactory sessionfactory) {
-		return new SqlSessionTemplate(sessionfactory);
-	}
+    private static final Logger log = LoggerFactory.getLogger(DefaultDataSourceConfig.class);
+    @Value("${spring.datasource.encrypted:false}")
+    private Boolean encrypted;
+
+    @Bean
+    @Primary
+    @ConfigurationProperties(prefix = "spring.datasource")
+    public DataSource dataSource() {
+        if (this.encrypted) return new EncryptedDatasource();
+        return DataSourceBuilder.create().build();
+    }
+
+    @Bean
+    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource datasource)
+            throws Exception {
+        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
+        bean.setDataSource(datasource);
+        bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
+        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/sundata/**/mybatis/*.xml"));
+        return bean.getObject();// mybatis xml所在
+    }
+
+    @Bean
+    @Primary
+    public SqlSessionTemplate sqlSessionTemplate(
+            @Qualifier("sqlSessionFactory") SqlSessionFactory sessionfactory) {
+        return new SqlSessionTemplate(sessionfactory);
+    }
+
+    @Bean
+    public DataSources initOtherDatasource() {
+        DataSources dataSources = new DataSources();
+        Setting setting = new Setting("OtherDBConfig.setting");
+//		String group = "GBaseDB";
+        setting.getGroups().forEach(group -> {
+            HikariConfig config = new HikariConfig();
+            config.setDriverClassName(setting.getByGroup("driver", group));
+            config.setJdbcUrl(setting.getByGroup("url", group));
+            config.setUsername(setting.getByGroup("user", group));
+            config.setPassword(setting.getByGroup("pass", group));
+            config.setMaximumPoolSize(5);
+            config.setMinimumIdle(1);
+            config.setAutoCommit(true);//自动提交
+            HikariDataSource ds = new HikariDataSource(config);
+            dataSources.setDataSource(group, ds);
+        });
+
+        return dataSources;
+    }
 }

+ 45 - 0
Procedure/backend/project/src/main/java/com/sundata/datasource/DataSources.java

@@ -0,0 +1,45 @@
+package com.sundata.datasource;
+
+import javax.sql.DataSource;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.StringJoiner;
+
+public class DataSources {
+    Map<String , DataSource> dataSources = new HashMap<String , DataSource>();
+    public DataSource getDataSource(String typeOrId){
+        return dataSources.get(typeOrId);
+    }
+    public void setDataSource(String typeOrId, DataSource dataSource){
+        dataSources.put(typeOrId, dataSource);
+    }
+
+    public void setDataSources(Map<String, DataSource> dataSources) {
+        this.dataSources = dataSources;
+    }
+
+    public Map<String, DataSource> getDataSources() {
+        return dataSources;
+    }
+
+    @Override
+    public String toString() {
+        return new StringJoiner(", ", DataSources.class.getSimpleName() + "[", "]")
+                .add("dataSources=" + dataSources)
+                .toString();
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (o == null || getClass() != o.getClass()) return false;
+
+        DataSources that = (DataSources) o;
+        return Objects.equals(getDataSources(), that.getDataSources());
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(getDataSources());
+    }
+}

+ 84 - 0
Procedure/backend/project/src/main/java/com/sundata/product/taskExecute/service/KingBase2GBaseService.java

@@ -0,0 +1,84 @@
+package com.sundata.product.taskExecute.service;
+
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.StrUtil;
+import com.sundata.datasource.DataSources;
+import com.sundata.product.rwa.calc.service.implement.extend.UpperMapRowMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
+import org.springframework.stereotype.Service;
+
+import javax.sql.DataSource;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+@Service
+public class KingBase2GBaseService {
+    private static final Logger log = LoggerFactory.getLogger(KingBase2GBaseService.class);
+    @Autowired
+    DataSources otherDataSources;
+    @Autowired
+    DataSource thisDatasource;
+
+    public void doExecute(String typeOrId, String tableName) {
+        DataSource otherDataSource = otherDataSources.getDataSource(typeOrId);
+        if (otherDataSource == null) {
+            log.error("未发现对应的数据库配置链接,当前数据库配置包含:{}", otherDataSources.getDataSources().keySet());
+            return;
+        }
+        if (StrUtil.isBlank(tableName)) {
+            log.error("传入的数据库表名为空,请检查方法参数");
+            return;
+//            throw new IllegalArgumentException("传入的数据库表名为空,请检查方法参数");
+        }
+
+
+        try {
+            ResultSet set = thisDatasource.getConnection().getMetaData().getTables(null, null, tableName, List.of("TABLE", "VIEW").toArray(new String[0]));
+            if (set == null) {
+                throw new IllegalArgumentException("本数据库中不存在对应的数据库表【{}】,请检查");
+            }
+        } catch (SQLException e) {
+            log.error("本数据库中不存在对应的数据库表【{}】,请检查", tableName);
+            throw new IllegalArgumentException("本数据库中不存在对应的数据库表【{}】,请检查", e);
+
+        }
+        try {
+            ResultSet set = otherDataSource.getConnection().getMetaData().getTables(null, null, tableName, List.of("TABLE", "VIEW").toArray(new String[0]));
+            if (set == null) {
+                throw new IllegalArgumentException("目标数据库中不存在对应的数据库表【{}】,请检查");
+            }
+        } catch (SQLException e) {
+            log.error("目标数据库中不存在对应的数据库表【{}】,请检查", tableName);
+            throw new IllegalArgumentException("目标数据库中不存在对应的数据库表【{}】,请检查", e);
+        }
+        JdbcTemplate thisJdbc = new JdbcTemplate(thisDatasource);
+        JdbcTemplate otherJdbc = new JdbcTemplate(otherDataSource);
+
+
+        List<Map<String ,Object>> data = thisJdbc.query("select * from "+tableName,new UpperMapRowMapper());
+        if (data.isEmpty()) {
+            log.warn("{}表中数据为空,不尽兴转换,请确认实际为空真实性",tableName);
+            return;
+        }
+        Set<String> keys = data.get(0).keySet();
+        List<String> sortedKeys = keys.stream().sorted().toList();
+        String insertSqlKey = CollectionUtil.join(sortedKeys,",");
+        String insertSqlValue = ":"+CollectionUtil.join(sortedKeys,", :");
+        String sql = "insert into "+tableName+"("+insertSqlKey+") values("+insertSqlValue+")";
+        log.debug("执行的SQL为:{}",sql);
+        MapSqlParameterSource sqlParam = new MapSqlParameterSource();
+        otherJdbc.update("delete from "+tableName,sqlParam);
+        for (Map<String ,Object> map : data) {
+            otherJdbc.update(sql,map);;
+        }
+    }
+
+
+}

+ 12 - 0
Procedure/backend/project/src/main/java/com/sundata/product/tempFile/task/flow/WorkFlow0001Service.java

@@ -9,6 +9,7 @@ import com.sundata.common.exception.ServiceException;
 import com.sundata.common.login.ActiveUser;
 import com.sundata.common.util.DBExecutor;
 import com.sundata.common.util.LogUtil;
+import com.sundata.product.taskExecute.service.KingBase2GBaseService;
 import com.sundata.product.taskExecute.service.TaskAllExecuteServer;
 import com.sundata.product.tempFile.task.model.*;
 import com.sundata.product.tempFile.task.service.ImportTaskBussinessService;
@@ -37,6 +38,9 @@ public class WorkFlow0001Service extends AWorkFlowService {
     @Autowired
     NounManageService nounManageService;
 
+    @Autowired
+    KingBase2GBaseService kingBase2GBaseService;
+
     @Override
     public void afterStartOfStartRun(String businessinsid, String busstatime, Object businessData)
             throws ServiceException {
@@ -304,6 +308,14 @@ public class WorkFlow0001Service extends AWorkFlowService {
 //      update sys_input_suptaskrun_taskallstatus set isover = '1' where term = '' and taskCode = ''
         DBExecutor.doModify("update sys_input_suptaskrun_taskallstatus set isover = '1' where term = '" + businfo.getTerm() + "' and taskCode = '" + businfo.getTaskCode() + "'");
         LOG.debug("流程:{},终审完成", businessinsid);
+
+//        new Thread(new Runnable() {
+//            public void run() {
+//
+//            }
+//        });
+        // TODO 确定表,如果有多张表则添加行 ,数据库连接池用的是自动提交。
+        kingBase2GBaseService.doExecute("GBaseDB","");
 //        String counts = DBExecutor.doQuery("select count(1) from sys_input_suptaskrun_taskallstatus where term = '" + businfo.getTerm() + "' and isover = '0'");
 //        if ("0".equals(counts)) {
 //            Map<String, Object> paramMap = new HashMap<>();

+ 15 - 0
Procedure/backend/project/src/main/resources/OtherDBConfig.setting

@@ -0,0 +1,15 @@
+#中括表示一个分组,其下面的所有属性归属于这个分组,在此分组名为 GBaseDB ,理论上来说也可以没有分组,但代码中使用了 group 所以分组此处必须存在
+[GBaseDB]
+#自定义数据源设置文件,这个文件会针对当前分组生效,用于给当前分组配置单独的数据库连接池参数,没有则使用全局的配置
+#数据库驱动名,如果不指定,则会根据url自动判定
+driver = com.mysql.jdbc.Driver
+
+#JDBC url,必须
+url = jdbc:mysql://localhost:3306/rmcdb
+
+#用户名,必须
+user = rmcdb
+
+#密码,必须,如果密码为空,请填写 pass =
+pass = rmcdb123
+