8 changed files with 348 additions and 148 deletions
-
5pom.xml
-
2src/main/java/com/example/demo/DemoApplication.java
-
64src/main/java/com/example/demo/Mysql/MysqlController.java
-
2src/main/java/com/example/demo/Mysql/MysqlService.java
-
305src/main/java/com/example/demo/Mysql/MysqlServiceImpl.java
-
14src/main/java/com/example/demo/config/AppConfig.java
-
82src/main/java/com/example/demo/config/SqlServer1DataSourceConfig.java
-
22src/main/resources/application.yml
@ -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(); |
||||
|
} |
||||
|
} |
@ -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 |
||||
|
//退款类型 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<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); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -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(); |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue