From 8949f2e9a06b800bdb3d3310a3229c2a1f70c52c Mon Sep 17 00:00:00 2001
From: huangqizhen <15552608129@163.com>
Date: Wed, 9 Jul 2025 21:06:47 +0800
Subject: [PATCH 1/3] =?UTF-8?q?7.9=E6=95=B0=E6=8D=AE=E5=BA=93=E5=90=8C?=
=?UTF-8?q?=E6=AD=A5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 5 +
.../java/com/example/demo/DemoApplication.java | 2 +
.../com/example/demo/Mysql/MysqlController.java | 64 ++---
.../java/com/example/demo/Mysql/MysqlService.java | 2 +
.../com/example/demo/Mysql/MysqlServiceImpl.java | 305 ++++++++++++++++-----
.../java/com/example/demo/config/AppConfig.java | 14 +
.../demo/config/SqlServer1DataSourceConfig.java | 82 +++---
src/main/resources/application.yml | 22 +-
8 files changed, 348 insertions(+), 148 deletions(-)
create mode 100644 src/main/java/com/example/demo/config/AppConfig.java
diff --git a/pom.xml b/pom.xml
index feb0b53..385c0d8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -47,6 +47,11 @@
4.5.14
+ com.microsoft.sqlserver
+ mssql-jdbc
+ 12.4.1.jre11
+
+
cn.hutool
hutool-all
5.8.24
diff --git a/src/main/java/com/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java
index b5f983a..d895f10 100644
--- a/src/main/java/com/example/demo/DemoApplication.java
+++ b/src/main/java/com/example/demo/DemoApplication.java
@@ -1,11 +1,13 @@
package com.example.demo;
+import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling // 启用调度功能
+@MapperScan(basePackages = "com.example.demo.mapper", sqlSessionTemplateRef = "mysql1SqlSessionTemplate")
public class DemoApplication {
public static void main(String[] args) {
diff --git a/src/main/java/com/example/demo/Mysql/MysqlController.java b/src/main/java/com/example/demo/Mysql/MysqlController.java
index b7fb131..6bd0bd8 100644
--- a/src/main/java/com/example/demo/Mysql/MysqlController.java
+++ b/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: 2025−07-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: 2025−07-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();
+ }
+}
diff --git a/src/main/java/com/example/demo/Mysql/MysqlService.java b/src/main/java/com/example/demo/Mysql/MysqlService.java
index ae1c5c5..ae5a715 100644
--- a/src/main/java/com/example/demo/Mysql/MysqlService.java
+++ b/src/main/java/com/example/demo/Mysql/MysqlService.java
@@ -6,4 +6,6 @@ package com.example.demo.Mysql;/**
@create: 2025−07-08 15:52
@Version 1.0
**/public interface MysqlService {
+
+ void getSqlserverData() throws Exception;
}
diff --git a/src/main/java/com/example/demo/Mysql/MysqlServiceImpl.java b/src/main/java/com/example/demo/Mysql/MysqlServiceImpl.java
index 5afd64c..a2a3f6b 100644
--- a/src/main/java/com/example/demo/Mysql/MysqlServiceImpl.java
+++ b/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);
-// }
-// }
-//}
\ No newline at end of file
+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 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 validOneTypes = new HashSet<>(Arrays.asList(9, 15, 17, 25, 27, 37, 41, 42, 43, 50, 51, 62));
+ Set validTwoTypes = new HashSet<>(Arrays.asList(52, 55, 56, 57, 58, 59, 61));
+ Set 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
+ //退款类型 61:ERP退款(退金币)
+ 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 requestBody = new HashMap<>();
+ requestBody.put("jwcode", desjwcode);
+
+ // 设置请求头
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_JSON);
+
+ // 创建 HttpEntity
+ HttpEntity