You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
14 KiB

  1. package com.example.demo.Mysql;
  2. import com.example.demo.Util.BaseDES;
  3. import com.example.demo.domain.entity.User;
  4. import com.example.demo.service.AdminService;
  5. import com.example.demo.service.UserService;
  6. import org.apache.commons.lang3.ObjectUtils;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Qualifier;
  12. import org.springframework.boot.autoconfigure.security.SecurityProperties;
  13. import org.springframework.http.*;
  14. import org.springframework.scheduling.annotation.Scheduled;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import org.springframework.web.client.RestTemplate;
  18. import javax.sql.DataSource;
  19. import java.sql.*;
  20. import java.time.LocalDateTime;
  21. import java.time.Month;
  22. import java.time.format.DateTimeFormatter;
  23. import java.util.*;
  24. @Service
  25. @Transactional
  26. public class MysqlServiceImpl implements MysqlService {
  27. @Autowired
  28. private RestTemplate restTemplate;
  29. 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));
  30. Set<Integer> validOneTypes = new HashSet<>(Arrays.asList(9, 15, 17, 25, 27, 37, 41, 42, 43, 50, 51, 62));
  31. Set<Integer> validTwoTypes = new HashSet<>(Arrays.asList(52,61));
  32. Set<Integer> validThreeTypes = new HashSet<>(Arrays.asList(10, 16, 30, 31, 32, 33, 34, 39, 44));
  33. Set<Integer> validFourTypes = new HashSet<>(Arrays.asList(55, 56, 57, 58, 59, 63, 64, 65));
  34. LocalDateTime now = LocalDateTime.now();
  35. Month currentMonth = now.getMonth();
  36. @Autowired
  37. private AdminService adminService;
  38. @Autowired
  39. private UserService userService;
  40. @Autowired
  41. @Qualifier("sqlserver1DataSource")
  42. private DataSource sqlserver1DataSource;
  43. @Autowired
  44. @Qualifier("mysql1DataSource")
  45. private DataSource mysql1DataSource;
  46. private static final Logger logger = LoggerFactory.getLogger(MysqlServiceImpl.class);
  47. @Override
  48. @Scheduled(cron = "0 0 * * * ?") // 每小时执行一次
  49. public void getSqlserverData() throws Exception {
  50. logger.info("开始从 SQL Server 同步数据到 MySQL");
  51. try (Connection sqlServerConn = sqlserver1DataSource.getConnection();
  52. Connection mysqlConn = mysql1DataSource.getConnection()) {
  53. logger.info("开始查询数据...");
  54. // 从 SQL Server 查询数据
  55. String querySql = "SELECT gtype,jwcode,free,core_jb,buy_jb,cz_time,cz_user,cz_bz,operation_platform,goods_name " +
  56. "FROM user_gold_records WHERE flag=1 and cz_time> ?";
  57. try (PreparedStatement sqlServerStmt = sqlServerConn.prepareStatement(querySql)) {
  58. sqlServerStmt.setTimestamp(1, Timestamp.valueOf(LocalDateTime.now().minusHours(1))); // 获取最近一小时的数据
  59. ResultSet resultSet = sqlServerStmt.executeQuery();
  60. logger.info("查询数据完毕!");
  61. // 插入到 MySQL
  62. //退款类型 61:ERP退款(退金币)
  63. String insertSql = "INSERT INTO user_gold_record (order_code,jwcode,sum_gold,permanent_gold,free_june,free_december," +
  64. "task_gold,pay_platform,goods_name,refund_type,refund_model,remark,type,admin_id," +
  65. "audit_status,create_time,flag) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
  66. try (PreparedStatement mysqlStmt = mysqlConn.prepareStatement(insertSql)) {
  67. while (resultSet.next()) {
  68. int gtype = resultSet.getInt("gtype");
  69. Integer jwcode = resultSet.getInt("jwcode");
  70. int free = resultSet.getInt("free");
  71. int core_jb = resultSet.getInt("core_jb");
  72. int buy_jb = resultSet.getInt("buy_jb");
  73. Timestamp created_at = resultSet.getTimestamp("cz_time");
  74. String name = resultSet.getString("cz_user");
  75. String remark = resultSet.getString("cz_bz");
  76. String operation_platform = resultSet.getString("operation_platform");
  77. String goods_name = resultSet.getString("goods_name");
  78. String timestampPart = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
  79. if(StringUtils.isNumeric(name)){
  80. Integer admin_id = Integer.valueOf(adminService.getId(name));
  81. logger.info("用户传输完毕");
  82. mysqlStmt.setInt(14, admin_id);
  83. }else {
  84. mysqlStmt.setInt(14, 99999);
  85. }
  86. Random random = new Random();
  87. int randomNumber = random.nextInt(900) + 100;
  88. // 判断gtype
  89. if(validFourTypes.contains(gtype)){
  90. continue;
  91. }
  92. if(validZeroTypes.contains(gtype)){
  93. mysqlStmt.setInt(13, 0);
  94. mysqlStmt.setString(1, "ERPCZ"+timestampPart+randomNumber);
  95. }
  96. if(validOneTypes.contains(gtype)){
  97. mysqlStmt.setInt(13, 1);
  98. mysqlStmt.setString(1, "ERPXF"+timestampPart+randomNumber);
  99. }
  100. if(validTwoTypes.contains(gtype)){
  101. mysqlStmt.setInt(13, 2);
  102. mysqlStmt.setString(1, "ERPTK"+timestampPart+randomNumber);
  103. mysqlStmt.setString(10,"退款商品");
  104. mysqlStmt.setInt(11, 0);
  105. }
  106. if(validThreeTypes.contains(gtype)){
  107. mysqlStmt.setInt(13, 3);
  108. mysqlStmt.setString(1, "ERPQT"+timestampPart+randomNumber);
  109. }
  110. mysqlStmt.setInt(2, jwcode);
  111. mysqlStmt.setInt(3, free+core_jb+buy_jb);
  112. mysqlStmt.setInt(4,buy_jb);
  113. // 判断月份
  114. if(currentMonth.getValue() >= 7){
  115. mysqlStmt.setInt(5, free);
  116. mysqlStmt.setInt(6, 0);
  117. }
  118. if(currentMonth.getValue() < 7){
  119. mysqlStmt.setInt(5, 0);
  120. mysqlStmt.setInt(6, free);
  121. }
  122. mysqlStmt.setInt(7, core_jb);
  123. if (operation_platform.equals("1")){
  124. mysqlStmt.setString(8, "ERP");
  125. }
  126. if (operation_platform.equals("2")){
  127. mysqlStmt.setString(8, "HomilyLink");
  128. }
  129. if(operation_platform.equals("3")){
  130. mysqlStmt.setString(8, "HomilyChart");
  131. }
  132. if(operation_platform.equals("4")){
  133. continue;
  134. }if(operation_platform.equals("0")){
  135. mysqlStmt.setString(8, "初始化金币");
  136. }
  137. else {
  138. mysqlStmt.setString(8, "其他");
  139. }
  140. mysqlStmt.setString(9, goods_name);
  141. mysqlStmt.setString(12, remark);
  142. mysqlStmt.setInt(15, 3);
  143. mysqlStmt.setTimestamp(16, created_at);
  144. if(remark.contains("测试")){
  145. mysqlStmt.setInt(17, 0);
  146. }else {
  147. mysqlStmt.setInt(17, 1);
  148. }
  149. // 更新时的值
  150. mysqlStmt.addBatch();
  151. logger.info("查询用户是否存在");
  152. User user = userService.selectAllUser(String.valueOf(jwcode));
  153. if(ObjectUtils.isEmpty(user)){
  154. logger.info("用户不存在");
  155. user = new User();
  156. String country = "未知";
  157. BaseDES des = new BaseDES();
  158. String desjwcode= des.encrypt(String.valueOf(jwcode));
  159. System.out.println("desjwcode:"+desjwcode);
  160. // 创建 JSON 请求体
  161. Map<String, String> requestBody = new HashMap<>();
  162. requestBody.put("jwcode", desjwcode);
  163. // 设置请求头
  164. HttpHeaders headers = new HttpHeaders();
  165. headers.setContentType(MediaType.APPLICATION_JSON);
  166. // 创建 HttpEntity
  167. HttpEntity<Map<String, String>> entity = new HttpEntity<>(requestBody, headers);
  168. // 发送 POST 请求
  169. try {
  170. ResponseEntity<Map> response = restTemplate.exchange(
  171. "http://hwapi.rzfwq.com/hwjnApp/hwhc-login/hwhclogin/hc/login/clent/info",
  172. HttpMethod.POST, entity, Map.class);
  173. // 检查响应状态码
  174. if (response.getStatusCode().is2xxSuccessful()) {
  175. Map<String, Object> responseBody = response.getBody();
  176. if (responseBody != null) {
  177. // 获取data部分
  178. Map<String, Object> data = (Map<String, Object>) responseBody.get("data");
  179. if (data != null) {
  180. // 提取name和country
  181. name = (String) data.get("name");
  182. country = (String) data.get("country");
  183. // 打印获取到的数据
  184. System.out.println("Name: " + name);
  185. System.out.println("Country: " + country);
  186. } else {
  187. System.out.println("Data is null");
  188. }
  189. } else {
  190. System.out.println("Response body is null");
  191. }
  192. } else {
  193. System.out.println("Request failed with status code: " + response.getStatusCode());
  194. }
  195. } catch (Exception e) {
  196. System.out.println("Error: " + e.getMessage());
  197. // 设置默认的 country 值
  198. country = "未知";
  199. }
  200. user.setJwcode(jwcode);
  201. user.setName( name);
  202. user.setMarket(country);
  203. logger.info("新添用户");
  204. userService.addUser(user);
  205. logger.info("用户添加成功");
  206. user = userService.selectAllUser(String.valueOf(jwcode));
  207. }
  208. if(currentMonth.getValue() >= 7){
  209. user.setCurrentFreeJune(user.getCurrentFreeJune()+free);
  210. }
  211. if(currentMonth.getValue() <7){
  212. user.setCurrentFreeDecember(user.getCurrentFreeDecember()+free);
  213. }
  214. user.setCurrentPermanentGold(user.getCurrentPermanentGold()+buy_jb);
  215. user.setCurrentTaskGold(user.getCurrentTaskGold()+core_jb);
  216. if(validZeroTypes.contains(gtype)) {
  217. user.setRechargeNum(user.getRechargeNum() + 1);
  218. user.setSumPermanentGold(user.getSumPermanentGold()+buy_jb);
  219. user.setSumTaskGold(user.getSumTaskGold()+core_jb);
  220. if(currentMonth.getValue() >= 7){
  221. user.setSumFreeJune(user.getSumFreeJune()+free);
  222. }
  223. if(currentMonth.getValue() < 7){
  224. user.setSumFreeDecember(user.getSumFreeDecember()+free);
  225. }
  226. }
  227. if (validOneTypes.contains(gtype)){
  228. user.setConsumeNum(user.getConsumeNum() + 1);
  229. user.setSumConsumeGold(user.getSumConsumeGold() + buy_jb);
  230. user.setSumConsumeTaskGold(user.getSumConsumeTaskGold() + core_jb);
  231. if(currentMonth.getValue() >= 7){
  232. user.setSumConsumeJune(user.getSumConsumeJune()+free);
  233. }
  234. if(currentMonth.getValue() < 7){
  235. user.setSumConsumeDecember(user.getSumFreeDecember()+free);
  236. }
  237. }
  238. userService.updateAllGold(user);
  239. }
  240. mysqlStmt.executeBatch(); // 批量插入
  241. }
  242. }
  243. logger.info("数据同步完成");
  244. } catch (SQLException e) {
  245. logger.error("数据连接失败", e.getMessage());
  246. throw new RuntimeException("数据链接失败", e);
  247. }
  248. }
  249. }