Browse Source

7.9数据库同步

huangqizheng/feature-20250710151401-数据同步完成
huangqizhen 1 month ago
parent
commit
8949f2e9a0
  1. 5
      pom.xml
  2. 2
      src/main/java/com/example/demo/DemoApplication.java
  3. 64
      src/main/java/com/example/demo/Mysql/MysqlController.java
  4. 2
      src/main/java/com/example/demo/Mysql/MysqlService.java
  5. 305
      src/main/java/com/example/demo/Mysql/MysqlServiceImpl.java
  6. 14
      src/main/java/com/example/demo/config/AppConfig.java
  7. 82
      src/main/java/com/example/demo/config/SqlServer1DataSourceConfig.java
  8. 22
      src/main/resources/application.yml

5
pom.xml

@ -47,6 +47,11 @@
<version>4.5.14</version> <version>4.5.14</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.4.1.jre11</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>
<version>5.8.24</version> <version>5.8.24</version>

2
src/main/java/com/example/demo/DemoApplication.java

@ -1,11 +1,13 @@
package com.example.demo; package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication @SpringBootApplication
@EnableScheduling // 启用调度功能 @EnableScheduling // 启用调度功能
@MapperScan(basePackages = "com.example.demo.mapper", sqlSessionTemplateRef = "mysql1SqlSessionTemplate")
public class DemoApplication { public class DemoApplication {
public static void main(String[] args) { public static void main(String[] args) {

64
src/main/java/com/example/demo/Mysql/MysqlController.java

@ -1,32 +1,32 @@
//package com.example.demo.Mysql;
//
//import com.example.demo.domain.vo.Result;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.CrossOrigin;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
///**
// * @program: GOLD
// * @ClassName MysqlController
// * @description:
// * @author: huangqizhen
// * @create: 202507-08 15:50
// * @Version 1.0
// **/
//@RestController
//@RequestMapping("/Mysql")
//@RequiredArgsConstructor
//@Slf4j
//@CrossOrigin
//public class MysqlController {
// @Autowired
// MysqlService mysqlService;
// @RequestMapping
// public Result Mysql () throws Exception {
// mysqlService.getSqlserverData();
// return Result.success();
// }
//}
package com.example.demo.Mysql;
import com.example.demo.domain.vo.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @program: GOLD
* @ClassName MysqlController
* @description:
* @author: huangqizhen
* @create: 202507-08 15:50
* @Version 1.0
**/
@RestController
@RequestMapping("/Mysql")
@RequiredArgsConstructor
@Slf4j
@CrossOrigin
public class MysqlController {
@Autowired
MysqlService mysqlService;
@RequestMapping
public Result Mysql () throws Exception {
mysqlService.getSqlserverData();
return Result.success();
}
}

2
src/main/java/com/example/demo/Mysql/MysqlService.java

@ -6,4 +6,6 @@ package com.example.demo.Mysql;/**
@create: 202507-08 15:52 @create: 202507-08 15:52
@Version 1.0 @Version 1.0
**/public interface MysqlService { **/public interface MysqlService {
void getSqlserverData() throws Exception;
} }

305
src/main/java/com/example/demo/Mysql/MysqlServiceImpl.java

@ -1,64 +1,241 @@
//package com.example.demo.Mysql;
//
//import com.example.demo.Mysql.MysqlService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Service;
//
//import javax.sql.DataSource;
//import java.sql.*;
//import java.time.LocalDateTime;
//
//@Service
//public class MysqlServiceImpl implements MysqlService {
//
// private static final Logger logger = LoggerFactory.getLogger(MysqlServiceImpl.class);
//
// private final DataSource sqlServerDataSource;
// private final DataSource mysqlDataSource;
//
// public MysqlServiceImpl(@Qualifier("sqlServerDataSource") DataSource sqlServerDataSource,
// @Qualifier("mysqlDataSource") DataSource mysqlDataSource) {
// this.sqlServerDataSource = sqlServerDataSource;
// this.mysqlDataSource = mysqlDataSource;
// }
//
// @Override
// @Scheduled(cron = "0 0 * * * ?") // 每小时执行一次
// public void getSqlserverData() {
// logger.info("开始从 SQL Server 同步数据到 MySQL...");
// try (Connection sqlServerConn = sqlServerDataSource.getConnection();
// Connection mysqlConn = mysqlDataSource.getConnection()) {
//
// // SQL Server 查询数据
// String querySql = "SELECT gtype,jwcode, created_at FROM user_gold_records WHERE created_at > ?";
// try (PreparedStatement sqlServerStmt = sqlServerConn.prepareStatement(querySql)) {
// sqlServerStmt.setTimestamp(1, Timestamp.valueOf(LocalDateTime.now().minusHours(1))); // 获取最近一小时的数据
// ResultSet resultSet = sqlServerStmt.executeQuery();
//
// // 插入到 MySQL
// String insertSql = "INSERT INTO target_table (id, name, created_at) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE name = ?";
// try (PreparedStatement mysqlStmt = mysqlConn.prepareStatement(insertSql)) {
// while (resultSet.next()) {
// int id = resultSet.getInt("id");
// String name = resultSet.getString("name");
// Timestamp createdAt = resultSet.getTimestamp("created_at");
//
// mysqlStmt.setInt(1, id);
// mysqlStmt.setString(2, name);
// mysqlStmt.setTimestamp(3, createdAt);
// mysqlStmt.setString(4, name); // 更新时的值
// mysqlStmt.addBatch();
// }
// mysqlStmt.executeBatch(); // 批量插入
// }
// }
// logger.info("数据同步完成");
// } catch (SQLException e) {
// logger.error("数据同步失败", e);
// throw new RuntimeException("数据同步失败", e);
// }
// }
//}
package com.example.demo.Mysql;
import com.example.demo.Util.BaseDES;
import com.example.demo.domain.entity.User;
import com.example.demo.service.AdminService;
import com.example.demo.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import javax.sql.DataSource;
import java.sql.*;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Service
@Transactional
public class MysqlServiceImpl implements MysqlService {
@Autowired
private RestTemplate restTemplate;
Set<Integer> validZeroTypes = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 18, 19, 20, 21, 22, 23, 24, 26, 28, 29, 35, 36, 40, 45, 46, 47, 48, 49, 53, 54, 60));
Set<Integer> validOneTypes = new HashSet<>(Arrays.asList(9, 15, 17, 25, 27, 37, 41, 42, 43, 50, 51, 62));
Set<Integer> validTwoTypes = new HashSet<>(Arrays.asList(52, 55, 56, 57, 58, 59, 61));
Set<Integer> validThreeTypes = new HashSet<>(Arrays.asList(10, 16, 30, 31, 32, 33, 34, 39, 44));
LocalDateTime now = LocalDateTime.now();
Month currentMonth = now.getMonth();
@Autowired
private AdminService adminService;
@Autowired
private UserService userService;
@Autowired
@Qualifier("sqlserver1DataSource")
private DataSource sqlserver1DataSource;
@Autowired
@Qualifier("mysql1DataSource")
private DataSource mysql1DataSource;
private static final Logger logger = LoggerFactory.getLogger(MysqlServiceImpl.class);
@Override
@Scheduled(cron = "0 0 * * * ?") // 每小时执行一次
public void getSqlserverData() throws Exception {
logger.info("开始从 SQL Server 同步数据到 MySQL");
try (Connection sqlServerConn = sqlserver1DataSource.getConnection();
Connection mysqlConn = mysql1DataSource.getConnection()) {
logger.info("开始查询数据...");
// SQL Server 查询数据
String querySql = "SELECT gtype,jwcode,free,core_jb,buy_jb,cz_time,cz_user,cz_bz,operation_platform,goods_name " +
"FROM user_gold_records WHERE flag=1 and created_at > ?";
try (PreparedStatement sqlServerStmt = sqlServerConn.prepareStatement(querySql)) {
sqlServerStmt.setTimestamp(1, Timestamp.valueOf(LocalDateTime.now().minusHours(1))); // 获取最近一小时的数据
ResultSet resultSet = sqlServerStmt.executeQuery();
logger.info("查询数据完毕!");
// 插入到 MySQL
//退款类型 61ERP退款退金币
String insertSql = "INSERT INTO user_gold_record (order_code,jwcode,sum_gold,permanent_gold,free_june,free_december," +
"task_gold,pay_platform,goods_name,refund_type,refund_model,remark,type,admin_id," +
"audit_status,create_time, VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE name = ?";
try (PreparedStatement mysqlStmt = mysqlConn.prepareStatement(insertSql)) {
while (resultSet.next()) {
int gtype = resultSet.getInt("gtype");
Integer jwcode = resultSet.getInt("jwcode");
int free = resultSet.getInt("free");
int core_jb = resultSet.getInt("core_jb");
int buy_jb = resultSet.getInt("buy_jb");
Timestamp created_at = resultSet.getTimestamp("cz_time");
String name = resultSet.getString("cz_user");
String remark = resultSet.getString("cz_bz");
String operation_platform = resultSet.getString("operation_platform");
String goods_name = resultSet.getString("goods_name");
String timestampPart = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
if(StringUtils.isNumeric(name)){
Integer admin_id = Integer.valueOf(adminService.getId(name));
logger.info("用户传输完毕");
mysqlStmt.setInt(14, admin_id);
}else {
mysqlStmt.setInt(14, 99999);
}
Random random = new Random();
int randomNumber = random.nextInt(900) + 100;
// 判断gtype
if(validZeroTypes.contains(gtype)){
mysqlStmt.setInt(13, 0);
mysqlStmt.setString(1, "ERPCZ"+timestampPart+randomNumber);
}
if(validOneTypes.contains(gtype)){
mysqlStmt.setInt(13, 1);
mysqlStmt.setString(1, "ERPXF"+timestampPart+randomNumber);
}
if(validTwoTypes.contains(gtype)){
mysqlStmt.setInt(13, 2);
mysqlStmt.setString(1, "ERPTK"+timestampPart+randomNumber);
}
if(validThreeTypes.contains(gtype)){
mysqlStmt.setInt(13, 3);
mysqlStmt.setString(1, "ERPQT"+timestampPart+randomNumber);
}
mysqlStmt.setInt(2, jwcode);
mysqlStmt.setInt(3, free+core_jb+buy_jb);
mysqlStmt.setInt(4,buy_jb);
// 判断月份
if(currentMonth.getValue() >= 7){
mysqlStmt.setInt(5, free);
}
if(currentMonth.getValue() < 7){
mysqlStmt.setInt(6, free);
}
mysqlStmt.setInt(7, core_jb);
if (operation_platform.equals("1")){
mysqlStmt.setString(8, "ERP");
}
if (operation_platform.equals("2")){
mysqlStmt.setString(8, "HomilyLink");
}
if(operation_platform.equals("3")){
mysqlStmt.setString(8, "HomilyChart");
}
if(operation_platform.equals("4")){
continue;
}
mysqlStmt.setString(9, goods_name);
mysqlStmt.setString(10,"退款商品");
mysqlStmt.setInt(11, 0);
mysqlStmt.setString(12, remark);
mysqlStmt.setInt(15, 3);
mysqlStmt.setTimestamp(16, created_at);
// 更新时的值
mysqlStmt.addBatch();
User user = userService.selectAllUser(String.valueOf(jwcode));
if(ObjectUtils.isEmpty(user)){
String country = null;
BaseDES des = new BaseDES();
String desjwcode= des.encrypt(String.valueOf(jwcode));
System.out.println("desjwcode:"+desjwcode);
// 创建 JSON 请求体
Map<String, String> requestBody = new HashMap<>();
requestBody.put("jwcode", desjwcode);
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 创建 HttpEntity
HttpEntity<Map<String, String>> entity = new HttpEntity<>(requestBody, headers);
// 发送 POST 请求
try {
ResponseEntity<Map> response = restTemplate.exchange(
"http://hwapi.rzfwq.com/hwjnApp/hwhc-login/hwhclogin/hc/login/clent/info",
HttpMethod.POST, entity, Map.class);
// 检查响应状态码
if (response.getStatusCode().is2xxSuccessful()) {
Map<String, Object> responseBody = response.getBody();
if (responseBody != null) {
// 获取data部分
Map<String, Object> data = (Map<String, Object>) responseBody.get("data");
if (data != null) {
// 提取name和country
name = (String) data.get("name");
country = (String) data.get("country");
// 打印获取到的数据
System.out.println("Name: " + name);
System.out.println("Country: " + country);
} else {
System.out.println("Data is null");
}
} else {
System.out.println("Response body is null");
}
} else {
System.out.println("Request failed with status code: " + response.getStatusCode());
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
// 设置默认的 country
country = "Unknown";
}
user.setJwcode(jwcode);
user.setName( name);
user.setMarket(country);
userService.addUser(user);
}
user.setSumPermanentGold(user.getSumPermanentGold()+buy_jb);
if(currentMonth.getValue() >= 7){
user.setSumFreeJune(user.getSumFreeJune()+free);
user.setCurrentFreeJune(user.getCurrentFreeJune()+free);
}
if(currentMonth.getValue() <7){
user.setSumFreeDecember(user.getSumFreeDecember()+free);
user.setCurrentFreeDecember(user.getCurrentFreeDecember()+free);
}
user.setSumTaskGold(user.getSumTaskGold()+core_jb);
user.setCurrentPermanentGold(user.getCurrentPermanentGold()+buy_jb);
user.setCurrentTaskGold(user.getCurrentTaskGold()+core_jb);
if(validZeroTypes.contains(gtype)) {
user.setRechargeNum(user.getRechargeNum() + 1);
}
if (validOneTypes.contains(gtype)){
user.setConsumeNum(user.getConsumeNum() + 1);
}
userService.updateAllGold(user);
}
mysqlStmt.executeBatch(); // 批量插入
}
}
logger.info("数据同步完成");
} catch (SQLException e) {
logger.error("数据连接失败", e.getMessage());
throw new RuntimeException("数据链接失败", e);
}
}
}

14
src/main/java/com/example/demo/config/AppConfig.java

@ -0,0 +1,14 @@
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

82
src/main/java/com/example/demo/config/SqlServer1DataSourceConfig.java

@ -1,33 +1,33 @@
//package com.example.demo.config;
//
//import com.zaxxer.hikari.HikariDataSource;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.mybatis.spring.SqlSessionTemplate;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.boot.jdbc.DataSourceBuilder;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//
//import javax.sql.DataSource;
//
//@Slf4j
//@Configuration
//public class SqlServer1DataSourceConfig {
//
// /**
// * 定义 SQL Server 数据源
// */
// @Bean(name = "sqlserver1DataSource")
// @ConfigurationProperties(prefix = "spring.datasource.sqlserver1")
// public DataSource sqlserver1DataSource() {
// log.info("Initializing SQL Server data source...");
// return DataSourceBuilder.create().type(HikariDataSource.class).build();
// }
//
package com.example.demo.config;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Slf4j
@Configuration
public class SqlServer1DataSourceConfig {
/**
* 定义 SQL Server 数据源
*/
@Bean(name = "sqlserver1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.sqlserver1")
public DataSource sqlserver1DataSource() {
log.info("Initializing SQL Server data source...");
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
// /** // /**
// * 定义 SQL Server SqlSessionFactory // * 定义 SQL Server SqlSessionFactory
// */ // */
@ -50,14 +50,14 @@
// log.info("Initializing SQL Server SqlSessionTemplate..."); // log.info("Initializing SQL Server SqlSessionTemplate...");
// return new SqlSessionTemplate(sqlSessionFactory); // return new SqlSessionTemplate(sqlSessionFactory);
// } // }
//
// /**
// * 定义全局 MyBatis 配置
// */
// @Bean
// @ConfigurationProperties(prefix = "mybatis.configuration.sqlserver1")
// public org.apache.ibatis.session.Configuration globalConfiguration() {
// log.info("Initializing SQL Server MyBatis global configuration...");
// return new org.apache.ibatis.session.Configuration();
// }
//}
/**
* 定义全局 MyBatis 配置
*/
@Bean
@ConfigurationProperties(prefix = "mybatis.configuration.sqlserver1")
public org.apache.ibatis.session.Configuration globalConfiguration() {
log.info("Initializing SQL Server MyBatis global configuration...");
return new org.apache.ibatis.session.Configuration();
}
}

22
src/main/resources/application.yml

@ -43,14 +43,14 @@ spring:
# hikari: # hikari:
# pool-name: mysql2HikariCP # pool-name: mysql2HikariCP
# maximum-pool-size: 10 # maximum-pool-size: 10
# sqlserver1:
# jdbc-url: jdbc:sqlserver://52.76.43.43:1433/hwhcGold?serverTimezone=Asia/Shanghai
# username: hwhc_gold_query
# password: hwhc_gold_query4564jkj
# driver-class-name: com.mysql.cj.jdbc.Driver
# hikari:
# pool-name: mysql2HikariCP
# maximum-pool-size: 10
sqlserver1:
url: jdbc:sqlserver://52.76.43.43:1433;databaseName=hwhcGold;encrypt=true;trustServerCertificate=true
username: hwhc_gold_query
password: hwhc_gold_query4564jkj
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
hikari:
pool-name: sqlserverHikariCP
maximum-pool-size: 10
application: application:
name: demo name: demo
@ -85,9 +85,9 @@ mybatis:
mysql1: mysql1:
map-underscore-to-camel-case: true map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# sqlserver1:
# map-underscore-to-camel-case: true
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
sqlserver1:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# mysql2: # mysql2:
# map-underscore-to-camel-case: true # map-underscore-to-camel-case: true
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

Loading…
Cancel
Save