浏览代码

update:新增 watchlist时init数据,并同步 CoinWatchlistOther数据v1

lvzhiqiang 1 年之前
父节点
当前提交
0e723cbe47

+ 1 - 1
src/main/java/top/lvzhiqiang/config/InitRunner.java

@@ -56,7 +56,7 @@ public class InitRunner implements ApplicationRunner {
 
         if (null == proxy) {
             if ("dev".equals(env)) {
-                proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 1080));
+                proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1081));
             } else {
                 proxy = Proxy.NO_PROXY;
             }

+ 4 - 1
src/main/java/top/lvzhiqiang/controller/CoinController.java

@@ -277,7 +277,7 @@ public class CoinController {
             }
 
             CoinWatchlist coinWatchlist = new CoinWatchlist();
-            coinWatchlist.setSymbol(symbol);
+            coinWatchlist.setSymbol(symbol.trim());
             coinWatchlist.setTrackCategory(trackCategory);
             coinWatchlist.setIssuingDate(LocalDate.parse(issuingDate, DateUtils.dateFormatter));
             coinWatchlist.setCmcId(cmcId);
@@ -287,6 +287,9 @@ public class CoinController {
             coinWatchlist.setFilterFlag(filterFlag);
 
             coinMapper.insertOrUpdateWatchlist(coinWatchlist);
+
+            // init data
+            coinService.initWatchlist(coinWatchlist);
         } else if ("2".equals(crudType)) {
             // 修改
             CoinWatchlist coinWatchlist = coinMapper.findWatchlistBySymbol(symbol);

+ 84 - 0
src/main/java/top/lvzhiqiang/entity/CoinWatchlistOther.java

@@ -0,0 +1,84 @@
+package top.lvzhiqiang.entity;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
+/**
+ * coin-自选清单other表
+ *
+ * @author lvzhiqiang
+ * 2024/03/18 16:23
+ */
+@Data
+public class CoinWatchlistOther implements Serializable {
+
+    /**
+     * 主键
+     */
+    private Long id;
+
+    /**
+     * 名称
+     */
+    private String symbol;
+
+    /**
+     * 最大供应量|这种加密货币的预期最大硬币限制。
+     */
+    private String maxSupply;
+
+    /**
+     * 流通供应量|这种加密货币流通的硬币的大致数量。
+     */
+    private String circulatingSupply;
+
+    /**
+     * 流通率|(流通供应量/最大供应量)*100%。
+     */
+    private String circulatingRate;
+
+    /**
+     * 总供应量|目前存在的硬币的大致总数(减去任何已验证已销毁的硬币)。
+     */
+    private String totalSupply;
+
+    /**
+     * 市值|指定货币的市值。
+     */
+    private String marketCap;
+
+    /**
+     * 完全稀释的市值|以指定货币完全摊薄的市值。
+     */
+    private String fullyDilutedMarketCap;
+
+    /**
+     * 中心化交易所现货
+     */
+    private String cexSpot;
+
+    /**
+     * 中心化交易所永续合约
+     */
+    private String cexPerpetual;
+
+    /**
+     * 创建时间
+     */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    private LocalDateTime createTime;
+
+    /**
+     * 最后修改时间
+     */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    private LocalDateTime modifyTime;
+
+    /**
+     * 删除标志(1:正常,2:已删除)
+     */
+    private Integer deleteFlag;
+}

+ 9 - 0
src/main/java/top/lvzhiqiang/mapper/CoinMapper.java

@@ -88,6 +88,9 @@ public interface CoinMapper {
             "<if test=\"keyword != null and keyword != ''\">" +
             "   and symbol like concat('%',#{keyword},'%')" +
             "</if>" +
+            "<if test=\"symbol != null and symbol != ''\">" +
+            "   and symbol = #{symbol}" +
+            "</if>" +
             "<if test=\"filterField != null and filterField != ''\">" +
             "   and filter_flag = #{filterField}" +
             "</if>" +
@@ -107,6 +110,12 @@ public interface CoinMapper {
             "issuing_days=#{issuingDays},coingecko_url=#{coingeckoUrl},cmc_url=#{cmcUrl},feixiaohao_url=#{feixiaohaoUrl},modify_time=now() where id = #{id}")
     int updateCoinWatchlist(CoinWatchlist coinWatchlist);
 
+    @Insert("INSERT INTO coin_watchlist_other(symbol, max_supply, circulating_supply, circulating_rate, total_supply, market_cap, fully_diluted_market_cap, cex_spot, cex_perpetual, create_time, modify_time) " +
+            "VALUES (#{symbol}, #{maxSupply}, #{circulatingSupply}, #{circulatingRate}, #{totalSupply}, #{marketCap}, #{fullyDilutedMarketCap}, #{cexSpot}, #{cexPerpetual}, now(), now())" +
+            " ON DUPLICATE KEY UPDATE max_supply=values(max_supply),circulating_supply=values(circulating_supply),circulating_rate=values(circulating_rate),total_supply=values(total_supply)" +
+            ",market_cap=values(market_cap),fully_diluted_market_cap=values(fully_diluted_market_cap),cex_spot=values(cex_spot),cex_perpetual=values(cex_perpetual),modify_time=now()")
+    int insertOrUpdateCoinWatchlistOther(CoinWatchlistOther coinWatchlistOther);
+
     @Update({"<script>" +
             "update coin_watchlist " +
             "<set>" +

+ 3 - 0
src/main/java/top/lvzhiqiang/service/CoinService.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import me.chanjar.weixin.cp.api.WxCpService;
 import me.chanjar.weixin.cp.bean.article.NewArticle;
+import top.lvzhiqiang.entity.CoinWatchlist;
 
 import java.util.Map;
 
@@ -54,4 +55,6 @@ public interface CoinService {
     Object mainSearchDetail(String nameEn, String id);
 
     void debugTest();
+
+    void initWatchlist(CoinWatchlist coinWatchlist);
 }

+ 63 - 8
src/main/java/top/lvzhiqiang/service/impl/CoinServiceImpl.java

@@ -844,13 +844,13 @@ public class CoinServiceImpl implements CoinService {
                 return;
             }
 
+            int MAX_NUMBER = 100;
             Map<String, Object> params = new HashMap<>();
             params.put("sortField", Collections.singletonList("create_time"));
             params.put("sort", "desc");
-            List<CoinWatchlist> watchlistList = coinMapper.findWatchlistList(params);
 
-            int MAX_NUMBER = 100;
-            Stream.iterate(0, n -> n + 1).limit((watchlistList.size() + MAX_NUMBER - 1) / MAX_NUMBER)
+            List<CoinWatchlist> watchlistListCKO = coinMapper.findWatchlistList(params);
+            Stream.iterate(0, n -> n + 1).limit((watchlistListCKO.size() + MAX_NUMBER - 1) / MAX_NUMBER)
                     .forEach(i -> {
                         try {
                             Thread.sleep(2000L);
@@ -858,10 +858,11 @@ public class CoinServiceImpl implements CoinService {
                             throw new RuntimeException(e);
                         }
 
-                        Map<String, CoinWatchlist> coinWatchlistMap4CoingeckoId = watchlistList.stream().skip((long) i * MAX_NUMBER).limit(MAX_NUMBER).collect(Collectors.toMap(CoinWatchlist::getCoingeckoId, coinWatchlist -> coinWatchlist));
+                        Map<String, CoinWatchlist> coinWatchlistMap4CoingeckoId = watchlistListCKO.stream().skip((long) i * MAX_NUMBER).limit(MAX_NUMBER).collect(Collectors.toMap(CoinWatchlist::getCoingeckoId, coinWatchlist -> coinWatchlist));
                         parseWatchlistMap4Coingecko(coinWatchlistMap4CoingeckoId);
                     });
-            Stream.iterate(0, n -> n + 1).limit((watchlistList.size() + MAX_NUMBER - 1) / MAX_NUMBER)
+            List<CoinWatchlist> watchlistListCMC = coinMapper.findWatchlistList(params);
+            Stream.iterate(0, n -> n + 1).limit((watchlistListCMC.size() + MAX_NUMBER - 1) / MAX_NUMBER)
                     .forEach(i -> {
                         try {
                             Thread.sleep(2000L);
@@ -869,7 +870,7 @@ public class CoinServiceImpl implements CoinService {
                             throw new RuntimeException(e);
                         }
 
-                        Map<Long, CoinWatchlist> coinWatchlistMap4CmcId = watchlistList.stream().skip((long) i * MAX_NUMBER).limit(MAX_NUMBER).collect(Collectors.toMap(CoinWatchlist::getCmcId, coinWatchlist -> coinWatchlist));
+                        Map<Long, CoinWatchlist> coinWatchlistMap4CmcId = watchlistListCMC.stream().skip((long) i * MAX_NUMBER).limit(MAX_NUMBER).collect(Collectors.toMap(CoinWatchlist::getCmcId, coinWatchlist -> coinWatchlist));
                         parseWatchlistMap4CmC(coinWatchlistMap4CmcId);
                     });
         }, 0, 1, TimeUnit.HOURS);
@@ -1064,6 +1065,38 @@ public class CoinServiceImpl implements CoinService {
                     }
 
                     coinMapper.updateCoinWatchlist(value);
+
+                    // 解析CoinWatchlistOther
+                    CoinWatchlistOther coinWatchlistOther = new CoinWatchlistOther();
+                    coinWatchlistOther.setSymbol(value.getSymbol());
+                    if (null != jsonObject.get("max_supply")) {
+                        coinWatchlistOther.setMaxSupply(jsonObject.getBigDecimal("max_supply").setScale(0, RoundingMode.HALF_UP).toPlainString());
+                    }
+                    if (null != jsonObject.get("circulating_supply")) {
+                        coinWatchlistOther.setCirculatingSupply(jsonObject.getBigDecimal("circulating_supply").setScale(0, RoundingMode.HALF_UP).toPlainString());
+                        if ("0".equals(coinWatchlistOther.getCirculatingSupply()) && null != jsonObject.get("self_reported_circulating_supply")) {
+                            coinWatchlistOther.setCirculatingSupply(jsonObject.getBigDecimal("self_reported_circulating_supply").setScale(0, RoundingMode.HALF_UP).toPlainString());
+                        }
+                    }
+
+                    if (null != coinWatchlistOther.getMaxSupply() && null != coinWatchlistOther.getCirculatingSupply()) {
+                        coinWatchlistOther.setCirculatingRate(new BigDecimal(coinWatchlistOther.getCirculatingSupply()).divide(new BigDecimal(coinWatchlistOther.getMaxSupply()), 3, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).toPlainString());
+                    }
+                    if (null != jsonObject.get("total_supply")) {
+                        coinWatchlistOther.setTotalSupply(jsonObject.getBigDecimal("total_supply").setScale(0, RoundingMode.HALF_UP).toPlainString());
+                    }
+                    JSONObject usdQuote = jsonObject.getJSONObject("quote").getJSONObject("USD");
+                    if (null != usdQuote.get("market_cap")) {
+                        coinWatchlistOther.setMarketCap(usdQuote.getBigDecimal("market_cap").setScale(0, RoundingMode.HALF_UP).toPlainString());
+                        if ("0".equals(coinWatchlistOther.getMarketCap()) && null != jsonObject.get("self_reported_market_cap")) {
+                            coinWatchlistOther.setMarketCap(jsonObject.getBigDecimal("self_reported_market_cap").setScale(0, RoundingMode.HALF_UP).toPlainString());
+                        }
+                    }
+                    if (null != usdQuote.get("fully_diluted_market_cap")) {
+                        coinWatchlistOther.setFullyDilutedMarketCap(usdQuote.getBigDecimal("fully_diluted_market_cap").setScale(0, RoundingMode.HALF_UP).toPlainString());
+                    }
+
+                    coinMapper.insertOrUpdateCoinWatchlistOther(coinWatchlistOther);
                 }
             });
         } catch (Exception e) {
@@ -1995,12 +2028,34 @@ public class CoinServiceImpl implements CoinService {
         Map<String, Object> params = new HashMap<>();
         params.put("sortField", Collections.singletonList("create_time"));
         params.put("sort", "desc");
+
+        ArrayList symbolList = new ArrayList();
+        symbolList.add("PYTH");
+        symbolList.add("SNX");
+        symbolList.add("REEF");
+
         List<CoinWatchlist> watchlistList = coinMapper.findWatchlistList(params);
+        watchlistList = watchlistList.stream().filter(x -> symbolList.contains(x.getSymbol())).collect(Collectors.toList());
+
+        //Map<String, CoinWatchlist> coinWatchlistMap4CoingeckoId = watchlistList.stream().collect(Collectors.toMap(CoinWatchlist::getCoingeckoId, coinWatchlist -> coinWatchlist));
+        //parseWatchlistMap4Coingecko(coinWatchlistMap4CoingeckoId);
+
+        Map<Long, CoinWatchlist> coinWatchlistMap4CmcId = watchlistList.stream().collect(Collectors.toMap(CoinWatchlist::getCmcId, coinWatchlist -> coinWatchlist));
+        parseWatchlistMap4CmC(coinWatchlistMap4CmcId);
+    }
+
+    @Override
+    @Async("coinTaskExecutor")
+    public void initWatchlist(CoinWatchlist coinWatchlist2) {
+        Map<String, Object> params = new HashMap<>();
+        params.put("symbol", coinWatchlist2.getSymbol());
+        List<CoinWatchlist> watchlistListCKO = coinMapper.findWatchlistList(params);
 
-        Map<String, CoinWatchlist> coinWatchlistMap4CoingeckoId = watchlistList.stream().collect(Collectors.toMap(CoinWatchlist::getCoingeckoId, coinWatchlist -> coinWatchlist));
+        Map<String, CoinWatchlist> coinWatchlistMap4CoingeckoId = watchlistListCKO.stream().collect(Collectors.toMap(CoinWatchlist::getCoingeckoId, coinWatchlist -> coinWatchlist));
         parseWatchlistMap4Coingecko(coinWatchlistMap4CoingeckoId);
 
-        Map<Long, CoinWatchlist> coinWatchlistMap4CmcId = coinWatchlistMap4CoingeckoId.values().stream().collect(Collectors.toMap(CoinWatchlist::getCmcId, coinWatchlist -> coinWatchlist));
+        List<CoinWatchlist> watchlistListCMC = coinMapper.findWatchlistList(params);
+        Map<Long, CoinWatchlist> coinWatchlistMap4CmcId = watchlistListCMC.stream().collect(Collectors.toMap(CoinWatchlist::getCmcId, coinWatchlist -> coinWatchlist));
         parseWatchlistMap4CmC(coinWatchlistMap4CmcId);
     }
 }