zj
2025-01-06 0e7b38c2b3af72ea2a7f8a2fcbaad4d78e2c1977
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.gear.swx.biz.impl;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.gear.common.utils.http.HttpUtils;
import com.gear.swx.biz.MarketBiz;
import com.gear.swx.domain.SwxMarket;
import com.gear.swx.domain.SwxMarketAll;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
 
@Component
public class MarketBizImpl implements MarketBiz {
    @Override
    public List<SwxMarketAll> listMarketByKeyName(String keyName) {
        if(Strings.isEmpty(keyName)){
            return null;
        }
        String result = HttpUtils.sendGet("https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords="+keyName+"&apikey=434JKX9989E8S5TJ");
        JSONObject resultJson = JSON.parseObject(result);
        JSONArray resultArrayJson = resultJson.getJSONArray("bestMatches");
        if(!resultArrayJson.isEmpty()){
            List<SwxMarketAll> list = new ArrayList<>();
            for (int i = 0; i < resultArrayJson.size(); i++) {
                JSONObject jo = resultArrayJson.getJSONObject(i);
                String  code = jo.getString("1. symbol");
                String name = jo.getString("2. name");
                SwxMarketAll swxMarketAll = new SwxMarketAll();
                swxMarketAll.setCode(code);
                swxMarketAll.setName(name);
                list.add(swxMarketAll);
            }
            return list;
        }
        return null;
    }
 
 
}