From dedaaf738a1d483b4594d46622eaa9791f7e5bcd Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Wed, 14 Aug 2024 19:48:36 +0800
Subject: [PATCH] 1

---
 websocketSerivce/src/main/java/org/example/task/MexcStock.java |   20 ++++++++++++--------
 1 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/websocketSerivce/src/main/java/org/example/task/MexcStock.java b/websocketSerivce/src/main/java/org/example/task/MexcStock.java
index fb68ce6..cd3a00b 100644
--- a/websocketSerivce/src/main/java/org/example/task/MexcStock.java
+++ b/websocketSerivce/src/main/java/org/example/task/MexcStock.java
@@ -14,6 +14,7 @@
 import org.example.pojo.bo.CurrencyMexcBo;
 import org.example.server.impl.CurrencySerivceImpl;
 import org.example.util.ConverterUtil;
+import org.example.util.RedisUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
@@ -40,7 +41,7 @@
     /**
      * 同步mexc交易所交易对
      */
-//    @Scheduled(cron = "0 0/30 * * * ?")
+    @Scheduled(cron = "0 0/30 * * * ?")
 //    @Scheduled(cron = "0/10 * * * * ?")
     public void syncCurrency() {
         //  使用Lock来确保同步
@@ -74,16 +75,19 @@
             Gson gson = new Gson();
             List<CurrencyMexcBo> getList = gson.fromJson(symbolsJson, new TypeToken<List<CurrencyMexcBo>>() {
             }.getType());
-
-            //  获取数据库中已有的symbol列表
             List<Currency> dbList = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "mexc"));
             Set<String> symbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
-
-            //  比对接口返回的数据和数据库中已有的数据,找出新增的数据
-            List<CurrencyMexcBo> saveList = getList.stream()
-                    .filter(CurrencyMexcBo -> !symbolSet.contains(CurrencyMexcBo.getSymbol()))
+            List<Currency> removeList = dbList.stream()
+                    .filter(currency -> !symbolSet.contains(currency.getSymbol()))
                     .collect(Collectors.toList());
-            List<Currency> currencies = ConverterUtil.convertToList(saveList, Currency.class);
+
+            if(CollectionUtils.isNotEmpty(removeList)){
+                removeList.forEach(f->{
+                    RedisUtil.delete("mexc"+f.getSymbol());
+                });
+            }
+            currencyService.remove(new LambdaQueryWrapper<Currency>().eq(Currency::getSource,"mexc"));
+            List<Currency> currencies = ConverterUtil.convertToList(getList, Currency.class);
             //  批量保存新增数据到数据库
             if(CollectionUtils.isNotEmpty(currencies)){
                 currencyService.saveBatch(currencies);

--
Gitblit v1.9.3