|
|
@ -1,9 +1,14 @@
|
|
|
|
package com.example.demo.task;
|
|
|
|
package com.example.demo.task;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
@ -17,35 +22,30 @@ import java.util.Map;
|
|
|
|
public class DataUpdateTask {
|
|
|
|
public class DataUpdateTask {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
|
|
|
|
private JdbcTemplate masterJdbcTemplate;
|
|
|
|
private JdbcTemplate masterJdbcTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private JdbcTemplate slaveJdbcTemplate;
|
|
|
|
private JdbcTemplate slaveJdbcTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
// 每天早上7点执行一次任务
|
|
|
|
// 每天早上5点执行一次任务 更新检查记录表
|
|
|
|
@Scheduled(cron = "0 0 7 * * ?")
|
|
|
|
@Scheduled(cron = "0 0 5 * * ?")
|
|
|
|
public void updateData() {
|
|
|
|
public void updateData() {
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("hello");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取远程数据库所有表名
|
|
|
|
// 获取远程数据库所有表名
|
|
|
|
List<String> tableNames = masterJdbcTemplate.queryForList(
|
|
|
|
List<String> tableNames = masterJdbcTemplate.queryForList(
|
|
|
|
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'tc_zz'",
|
|
|
|
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'ying_ji'",
|
|
|
|
String.class);
|
|
|
|
String.class);
|
|
|
|
|
|
|
|
|
|
|
|
// 更新每个表的数据到另一个数据库
|
|
|
|
// 更新每个表的数据到另一个数据库
|
|
|
|
for (String tableName : tableNames) {
|
|
|
|
for (String tableName : tableNames) {
|
|
|
|
if (!tableName.equals("szs_enterprise_information") && tableName.startsWith("sz") ){
|
|
|
|
if (tableName.equals("sz_enfor_examine") ){
|
|
|
|
slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 0"); // 禁用外键检查
|
|
|
|
slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 0"); // 禁用外键检查
|
|
|
|
slaveJdbcTemplate.execute("delete FROM tc_zz." + tableName);
|
|
|
|
slaveJdbcTemplate.execute("delete FROM ying_ji." + tableName);
|
|
|
|
// ... 以此类推,根据实际表的数量进行删除操作
|
|
|
|
// ... 以此类推,根据实际表的数量进行删除操作
|
|
|
|
slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 1"); // 启用外键检查
|
|
|
|
slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 1"); // 启用外键检查
|
|
|
|
String selectQuery = "SELECT * FROM tc_zz." + tableName;
|
|
|
|
String selectQuery = "SELECT * FROM ying_ji." + tableName;
|
|
|
|
List<Map<String, Object>> tableData = masterJdbcTemplate.queryForList(selectQuery);
|
|
|
|
List<Map<String, Object>> tableData = masterJdbcTemplate.queryForList(selectQuery);
|
|
|
|
|
|
|
|
|
|
|
|
for (Map<String, Object> row : tableData) {
|
|
|
|
for (Map<String, Object> row : tableData) {
|
|
|
|
StringBuilder insertQuery = new StringBuilder("INSERT INTO tc_zz.")
|
|
|
|
StringBuilder insertQuery = new StringBuilder("INSERT INTO ying_ji.")
|
|
|
|
.append(tableName)
|
|
|
|
.append(tableName)
|
|
|
|
.append(" (");
|
|
|
|
.append(" (");
|
|
|
|
StringBuilder valuesClause = new StringBuilder(" VALUES (");
|
|
|
|
StringBuilder valuesClause = new StringBuilder(" VALUES (");
|
|
|
@ -69,17 +69,12 @@ public class DataUpdateTask {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 每周一早上5点执行一次任务 更新企业原表
|
|
|
|
|
|
|
|
@Scheduled(cron = "0 0 5 ? * MON")
|
|
|
|
// 每周日晚上5点执行一次任务
|
|
|
|
|
|
|
|
@Scheduled(cron = "0 0 17 ? * SUN")
|
|
|
|
|
|
|
|
public void updateInformationData() {
|
|
|
|
public void updateInformationData() {
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("hello");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取远程数据库所有表名
|
|
|
|
// 获取远程数据库所有表名
|
|
|
|
List<String> tableNames = masterJdbcTemplate.queryForList(
|
|
|
|
List<String> tableNames = masterJdbcTemplate.queryForList(
|
|
|
|
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'tc_zz'",
|
|
|
|
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'ying_ji'",
|
|
|
|
String.class);
|
|
|
|
String.class);
|
|
|
|
|
|
|
|
|
|
|
|
// 更新每个表的数据到另一个数据库
|
|
|
|
// 更新每个表的数据到另一个数据库
|
|
|
@ -87,14 +82,14 @@ public class DataUpdateTask {
|
|
|
|
if (tableName.equals("szs_enterprise_information") ) {
|
|
|
|
if (tableName.equals("szs_enterprise_information") ) {
|
|
|
|
// 删除本地数据库所有表的数据
|
|
|
|
// 删除本地数据库所有表的数据
|
|
|
|
slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 0"); // 禁用外键检查
|
|
|
|
slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 0"); // 禁用外键检查
|
|
|
|
slaveJdbcTemplate.execute("delete FROM tc_zz." + tableName);
|
|
|
|
slaveJdbcTemplate.execute("delete FROM ying_ji." + tableName);
|
|
|
|
// ... 以此类推,根据实际表的数量进行删除操作
|
|
|
|
// ... 以此类推,根据实际表的数量进行删除操作
|
|
|
|
slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 1"); // 启用外键检查
|
|
|
|
slaveJdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 1"); // 启用外键检查
|
|
|
|
String selectQuery = "SELECT * FROM tc_zz." + tableName;
|
|
|
|
String selectQuery = "SELECT * FROM ying_ji." + tableName;
|
|
|
|
List<Map<String, Object>> tableData = masterJdbcTemplate.queryForList(selectQuery);
|
|
|
|
List<Map<String, Object>> tableData = masterJdbcTemplate.queryForList(selectQuery);
|
|
|
|
|
|
|
|
|
|
|
|
for (Map<String, Object> row : tableData) {
|
|
|
|
for (Map<String, Object> row : tableData) {
|
|
|
|
StringBuilder insertQuery = new StringBuilder("INSERT INTO tc_zz.")
|
|
|
|
StringBuilder insertQuery = new StringBuilder("INSERT INTO ying_ji.")
|
|
|
|
.append(tableName)
|
|
|
|
.append(tableName)
|
|
|
|
.append(" (");
|
|
|
|
.append(" (");
|
|
|
|
StringBuilder valuesClause = new StringBuilder(" VALUES (");
|
|
|
|
StringBuilder valuesClause = new StringBuilder(" VALUES (");
|
|
|
@ -105,17 +100,14 @@ public class DataUpdateTask {
|
|
|
|
valuesClause.append("?, ");
|
|
|
|
valuesClause.append("?, ");
|
|
|
|
values.add(entry.getValue());
|
|
|
|
values.add(entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 删除最后一个逗号和空格
|
|
|
|
// 删除最后一个逗号和空格
|
|
|
|
insertQuery.delete(insertQuery.length() - 2, insertQuery.length());
|
|
|
|
insertQuery.delete(insertQuery.length() - 2, insertQuery.length());
|
|
|
|
valuesClause.delete(valuesClause.length() - 2, valuesClause.length());
|
|
|
|
valuesClause.delete(valuesClause.length() - 2, valuesClause.length());
|
|
|
|
|
|
|
|
|
|
|
|
insertQuery.append(")").append(valuesClause).append(")");
|
|
|
|
insertQuery.append(")").append(valuesClause).append(")");
|
|
|
|
slaveJdbcTemplate.update(insertQuery.toString(), values.toArray());
|
|
|
|
slaveJdbcTemplate.update(insertQuery.toString(), values.toArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|