package com.yami.trading.api.controller;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
import com.yami.trading.bean.data.domain.Realtime;
|
import com.yami.trading.bean.data.domain.TradeDetails;
|
import com.yami.trading.bean.item.domain.Item;
|
import com.yami.trading.common.domain.Result;
|
import com.yami.trading.common.exception.YamiShopBindException;
|
import com.yami.trading.common.lang.LangUtils;
|
import com.yami.trading.common.util.StringUtils;
|
import com.yami.trading.common.web.ResultObject;
|
import com.yami.trading.huobi.data.DataCache;
|
import com.yami.trading.service.data.DataService;
|
import com.yami.trading.service.item.ItemService;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.compress.utils.Lists;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.text.DecimalFormat;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 行情实时价格 http接口
|
*
|
*/
|
@RestController
|
@CrossOrigin
|
@Slf4j
|
@Api(tags = "实时行情数据")
|
public class RealtimeController {
|
|
public static final String HOBI = "api/hobi!";
|
@Qualifier("dataService")
|
@Autowired
|
private DataService dataService;
|
@Autowired
|
private ItemService itemService;
|
|
@ApiOperation(value = "行情")
|
@GetMapping(HOBI + "getRealtime.action")
|
public Result<List<Realtime>> getRealtime(@RequestParam(value = "",required = false) String symbol) {
|
|
try {
|
// if(StringUtils.isEmptyString(symbol)){
|
// return new Result<>();
|
// }
|
List<Realtime> data = this.dataService.realtime(symbol);
|
data.forEach(d->{
|
Item bySymbol = itemService.findBySymbol(d.getSymbol());
|
if(LangUtils.isEnItem()){
|
bySymbol.transName();
|
}
|
d.setType(bySymbol.getType());
|
d.setName(bySymbol.getName());
|
Integer decimals = bySymbol.getDecimals();
|
d.setClose(d.getClose().setScale(decimals, RoundingMode.HALF_UP));
|
d.setOpen(d.getOpen().setScale(decimals, RoundingMode.HALF_UP));
|
d.setHigh(d.getHigh().setScale(decimals, RoundingMode.HALF_UP));
|
d.setLow(d.getLow().setScale(decimals, RoundingMode.HALF_UP));
|
BigDecimal bigDecimal = BigDecimal.valueOf(1, decimals);
|
d.setAsk(d.getClose().add(bigDecimal).setScale(decimals, RoundingMode.HALF_UP));
|
d.setBid(d.getClose().subtract(bigDecimal).setScale(decimals, RoundingMode.HALF_UP));
|
if(d.getVolume()!=null){
|
d.setVolume(d.getVolume().setScale(2, RoundingMode.HALF_UP));
|
}
|
if(d.getAmount()!=null){
|
d.setAmount(d.getAmount().setScale(2, RoundingMode.HALF_UP));
|
}
|
d.setSymbolData(bySymbol.getSymbolData());
|
});
|
|
List<Realtime> result = new ArrayList<>();
|
if (!data.isEmpty()) {
|
Realtime realtime = data.stream().filter(x -> x.getSymbol().equalsIgnoreCase("btcusdt")).findFirst().orElse(null);
|
if (realtime != null) {
|
result.add(realtime);
|
data.remove(realtime);
|
}
|
Realtime realtime2 = data.stream().filter(x -> x.getSymbol().equalsIgnoreCase("ethusdt")).findFirst().orElse(null);
|
if (realtime2 != null) {
|
result.add(realtime2);
|
data.remove(realtime2);
|
}
|
Realtime realtime3 = data.stream().filter(x -> x.getSymbol().equalsIgnoreCase("xrpusdt")).findFirst().orElse(null);
|
if (realtime3 != null) {
|
result.add(realtime3);
|
data.remove(realtime3);
|
}
|
if (!data.isEmpty()) {
|
result.addAll(data);
|
}
|
}
|
return Result.ok(result);
|
} catch (Exception e) {
|
log.error("c", e);
|
throw new YamiShopBindException("生成实时数据失败");
|
}
|
}
|
|
|
@ApiOperation(value = "行情")
|
@GetMapping(HOBI + "getStockTradeList.action")
|
public Result<List<TradeDetails>> getTradeDetails(@RequestParam String symbol) {
|
try {
|
List<TradeDetails> stockTradeList = DataCache.getStockTradeList(symbol);
|
|
Integer decimal = itemService.getDecimal(symbol);
|
String format = "";
|
if (decimal == 0) {
|
format = "#";
|
} else {
|
format = "#.";
|
for (int j = 0; j < decimal; j++) {
|
format = format + "#";
|
}
|
}
|
DecimalFormat df = new DecimalFormat(format);
|
df.setRoundingMode(RoundingMode.FLOOR);// 向下取整
|
if(CollectionUtil.isEmpty(stockTradeList)){
|
return Result.ok(Lists.newArrayList());
|
|
}
|
for (TradeDetails tradeDetails : stockTradeList) {
|
tradeDetails.setCurrentStr(df.format(tradeDetails.getCurrent()));
|
}
|
|
return Result.ok(stockTradeList);
|
} catch (Exception e) {
|
log.error("生成实时数据失败", e);
|
throw new YamiShopBindException("生成实时数据失败");
|
}
|
}
|
|
|
public static void main(String[] args) {
|
BigDecimal bigDecimal = BigDecimal.valueOf(1, 4);
|
System.out.println(bigDecimal);
|
|
}
|
|
}
|