zj
2025-02-25 dd315d5732e14fcf3df71e0cf213cc442bd8607b
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package project.web.api;
 
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
import javax.servlet.http.HttpServletRequest;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
 
import kernel.exception.BusinessException;
import kernel.util.StringUtils;
import kernel.web.ResultObject;
import project.data.DataService;
import project.data.model.Realtime;
import project.item.ItemService;
import project.item.model.Item;
 
/**
 * 行情实时价格 http接口
 *
 */
@RestController
@CrossOrigin
public class RealtimeController {
    
    private Logger logger = LoggerFactory.getLogger(RealtimeController.class);
 
    @Autowired
    private DataService dataService;
 
    @Autowired
    private ItemService itemService;
 
    @RequestMapping("api/hobi!getRealtime.action")
    public String getRealtime(HttpServletRequest request) {
        ResultObject resultObject = new ResultObject();
        String symbol = request.getParameter("symbol");
        // asc升序 desc 降序
        String order = request.getParameter("order");
        try {
 
            if (StringUtils.isNullOrEmpty(symbol)) {
                resultObject.setCode("400");
                resultObject.setMsg("[symbol]参数为空");
                return JSON.toJSONString(resultObject);
            }
 
            // 数据处理
            List<Realtime> data = this.dataService.realtime(symbol);
 
            if (!StringUtils.isNullOrEmpty(order)) {
                List<Realtime> list_clone = new ArrayList<Realtime>();
 
                for (int i = 0; i < data.size(); i++) {
                    Realtime realtime = (Realtime) data.get(i).clone();
                    realtime.setOrder(order);
                    list_clone.add(realtime);
                }
                data = list_clone.stream().sorted(Comparator.comparing(Realtime::getClose).reversed()).collect(Collectors.toList());
            }
            resultObject.setData(this.revise(data));
            return JSONObject.toJSONString(resultObject);
        } catch (BusinessException e) {
            resultObject.setCode("402");
            resultObject.setMsg(e.getMessage());
            return JSONObject.toJSONString(resultObject);
        } catch (Throwable e) {
            resultObject.setCode("500");
            resultObject.setMsg("服务器错误(500)");
            logger.error("error:", e);
            return JSONObject.toJSONString(resultObject);
        }
    }
 
    private List<Map<String, Object>> revise(List<Realtime> data) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < data.size(); i++) {
            Realtime realtime = data.get(i);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("symbol", realtime.getSymbol());
            map.put("ts", realtime.getTs());
            String currentTime = realtime.getCurrent_time();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a", Locale.ENGLISH);
            // 设置时区为台湾(Asia/Taipei)
            sdf.setTimeZone(TimeZone.getTimeZone("Asia/Taipei"));
            map.put("current_time",sdf.format(currentTime));
            map.put("name", realtime.getName());
            map.put("change_ratio", realtime.getChange_ratio());
            Item item = this.itemService.cacheBySymbol(realtime.getSymbol(), true);
            if (item.getDecimals() == null || item.getDecimals() < 0) {
                map.put("open", realtime.getOpen());
                map.put("close", realtime.getClose());
                map.put("high", realtime.getHigh());
                map.put("low", realtime.getLow());
                map.put("volume", realtime.getVolume());
                map.put("amount", realtime.getAmount());
            } else {
                String format = "";
                if (item.getDecimals() == 0) {
                    format = "#";
                } else {
                    format = "#.";
                    for (int j = 0; j < item.getDecimals(); j++) {
                        format = format + "#";
                    }
                }
 
                DecimalFormat df = new DecimalFormat(format);
                df.setRoundingMode(RoundingMode.FLOOR);// 向下取整
 
                map.put("open", df.format(realtime.getOpen()));
                map.put("close", df.format(realtime.getClose()));
                map.put("high", df.format(realtime.getHigh()));
                map.put("low", df.format(realtime.getLow()));
                map.put("volume", df.format(realtime.getVolume()));
                map.put("amount", df.format(realtime.getAmount()));
 
            }
            list.add(map);
 
        }
 
        return list;
    }
}