package project.web.api;
|
|
import java.math.RoundingMode;
|
import java.text.DecimalFormat;
|
import java.text.ParseException;
|
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) throws ParseException {
|
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();
|
// 设置时区为纽约的冬令时(Eastern Standard Time, EST)
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
|
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
|
Date parsedDate = sdf.parse(currentTime);
|
map.put("current_time",sdf.format(parsedDate));
|
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;
|
}
|
}
|