| | |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.google.common.collect.Lists; |
| | |
| | | import com.nq.enums.EStockType; |
| | | import com.nq.pojo.*; |
| | | import com.nq.pojo.reponse.RPageInfo; |
| | | import com.nq.pojo.reponse.kResponse; |
| | | import com.nq.service.*; |
| | | import com.nq.utils.http.HttpClientRequest; |
| | | import com.nq.utils.PropertiesUtil; |
| | |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.concurrent.CompletableFuture; |
| | | import java.util.stream.Collectors; |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import static com.nq.utils.timeutil.DateTimeUtil.getWeekDay; |
| | | import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user; |
| | | |
| | | @Service("iStockService") |
| | | public class StockServiceImpl implements IStockService { |
| | |
| | | if (stockList.size() > 0){ |
| | | stockListVOS.addAll(Objects.requireNonNull(StockApi.getStockReailTimes(stockList))); |
| | | } |
| | | RPageInfo pageInfo = new RPageInfo(); |
| | | RPageInfo<StockListVO> pageInfo = new RPageInfo<StockListVO>(); |
| | | |
| | | |
| | | if(null != orderBy && orderBy.equals("asc")){ |
| | | stockListVOS = stockListVOS.stream().sorted(Comparator.comparing(StockListVO::getHcrate)).collect(Collectors.toList()); |
| | | } |
| | | |
| | | if(null != orderBy && orderBy.equals("desc")){ |
| | | stockListVOS = stockListVOS.stream() |
| | | .sorted(Comparator.comparing(StockListVO::getHcrate).reversed()) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | pageInfo.setList(stockListVOS); |
| | | pageInfo.setStockType(stockType); |
| | | return ServerResponse.createBySuccess(pageInfo); |
| | |
| | | } |
| | | |
| | | |
| | | public ServerResponse getSingleStock(String code, HttpServletRequest request) { |
| | | public ServerResponse getSingleStock(String code, HttpServletRequest request) throws JsonProcessingException { |
| | | |
| | | if(code.equals("GOLD")){ |
| | | String price = RedisShardedPoolUtils.get(RedisKeyConstant.gold); |
| | | if(code.equals("XAUUSD")){ |
| | | String price = RedisShardedPoolUtils.get(RedisKeyConstant.XAUUSD); |
| | | getKData(null, "1", code);//只是为了得到下面redis的值 |
| | | StockVO stockVO = new StockVO(); |
| | | stockVO.setNowPrice(price); |
| | | stockVO.setName(code); |
| | | stockVO.setHcrate(RedisShardedPoolUtils.get(code+"_H")); |
| | | Map map = Maps.newHashMap(); |
| | | map.put("stock", stockVO); |
| | | return ServerResponse.createBySuccess(map); |
| | | }else if(code.equals("CRUDE OIL")){ |
| | | String price = RedisShardedPoolUtils.get(RedisKeyConstant.crude_oil); |
| | | }else if(code.equals("USOIL")){ |
| | | String price = RedisShardedPoolUtils.get(RedisKeyConstant.USOIL); |
| | | StockVO stockVO = new StockVO(); |
| | | stockVO.setNowPrice(price); |
| | | stockVO.setName(code); |
| | | stockVO.setHcrate(RedisShardedPoolUtils.get(code+"_H")); |
| | | Map map = Maps.newHashMap(); |
| | | map.put("stock", stockVO); |
| | | return ServerResponse.createBySuccess(map); |
| | |
| | | } |
| | | |
| | | |
| | | //黄金 |
| | | private static final String gold_API_URL = "http://139.196.211.109/ldMetal_k.action?username=Qq112233&password=3ce25a66d5b3a8cd661024fea6c79388&id=AUUSDO&num=-100&period="; |
| | | |
| | | //原油 |
| | | private static final String crude_oil_API_URL = "http://47.112.169.122/fOption_k.action?username=Qq112233&password=3ce25a66d5b3a8cd661024fea6c79388&id=@CL0W&num=-100&period="; |
| | | |
| | | /*股票日线-K线*/ |
| | | @Override |
| | | public Object getKData(String pid, String interval, String stockType) { |
| | | if(stockType.equals("GOLD")){ |
| | | public Object getKData(String pid, String interval, String stockType) throws JsonProcessingException { |
| | | if(stockType.equals("XAUUSD")){ |
| | | // 使用RestTemplate发起HTTP请求 |
| | | String response = RedisShardedPoolUtils.get("k_gold_"+interval.toLowerCase()); |
| | | return parseData(interval,response, stockType); |
| | | }else if(stockType.equals("CRUDE OIL")){ |
| | | }else if(stockType.equals("USOIL")){ |
| | | // 使用RestTemplate发起HTTP请求 |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | String response = RedisShardedPoolUtils.get("k_crude_oil_"+interval.toLowerCase()); |
| | | return parseData(interval,response, stockType); |
| | | }else{ |
| | |
| | | } |
| | | } |
| | | |
| | | public List<kData> parseData(String interval,String data,String key) { |
| | | public List<kData> parseData(String interval,String data,String key) throws JsonProcessingException { |
| | | String price = null; |
| | | if(key.equals("GOLD")){ |
| | | price = RedisShardedPoolUtils.get(RedisKeyConstant.gold); |
| | | }else if(key.equals("CRUDE OIL")){ |
| | | price = RedisShardedPoolUtils.get(RedisKeyConstant.crude_oil); |
| | | if(key.equals("XAUUSD")){ |
| | | price = RedisShardedPoolUtils.get(RedisKeyConstant.XAUUSD); |
| | | }else if(key.equals("USOIL")){ |
| | | price = RedisShardedPoolUtils.get(RedisKeyConstant.USOIL); |
| | | } |
| | | List<kData> kDataList = new ArrayList<>(); |
| | | // 使用 Gson 解析 JSON 字符串 |
| | | Gson gson = new Gson(); |
| | | kResponse kResponse = gson.fromJson(data, kResponse.class); |
| | | |
| | | // 将数据按行分割 |
| | | String[] lines = data.split("\n"); |
| | | |
| | | // 跳过第一行(列名) |
| | | for (int i = 1; i < lines.length; i++) { |
| | | String line = lines[i].trim(); |
| | | // 确保行不为空 |
| | | if (!line.isEmpty()) { |
| | | String[] fields = line.split(","); |
| | | |
| | | // 确保每行有至少7个字段:日期、开盘价、最高价、最低价、收盘价、成交量、持仓量 |
| | | if ((key.equals("CRUDE OIL") && fields.length == 7) || (key.equals("GOLD") && fields.length == 5)) { |
| | | kData kData = new kData(); |
| | | kData.setT(convDate(fields[0],interval)); |
| | | kData.setC(price); |
| | | kData.setO(fields[1]); |
| | | kData.setH(fields[2]); |
| | | kData.setL(fields[3]); |
| | | if(key.equals("CRUDE OIL")){ |
| | | kData.setV(fields[4]); |
| | | kData.setVo(fields[5]); |
| | | }else{ |
| | | kData.setV("0"); |
| | | kData.setVo("0"); |
| | | } |
| | | // 将每一条 KData 对象添加到列表中 |
| | | kDataList.add(kData); |
| | | } |
| | | } |
| | | // 打印 kline_list 的内容 |
| | | for (kResponse.KlineData item : kResponse.getData().getKlineList()) { |
| | | kData kData = new kData(); |
| | | kData.setT(item.getTimestamp()); |
| | | kData.setC(item.getClose_price()); |
| | | kData.setO(item.getOpen_price()); |
| | | kData.setH(item.getHigh_price()); |
| | | kData.setL(item.getLow_price()); |
| | | kData.setV(item.getVolume()); |
| | | kData.setVo(item.getTurnover()); |
| | | kDataList.add(kData); |
| | | } |
| | | kDataList.get(kDataList.size() - 1).setC(price); |
| | | return kDataList; |
| | | } |
| | | |
| | | |
| | | // 常量,定义日期格式 |
| | | private static final String DATE_PATTERN = "yyyyMMdd"; |
| | | private static final String DATE_TIME_PATTERN = "yyyyMMdd HH:mm"; |
| | | |
| | | public long convDate(String dateStr, String interval) { |
| | | // 输入校验,确保 dateStr 和 interval 不为 null 或空 |
| | | if (dateStr == null || dateStr.trim().isEmpty() || interval == null || interval.trim().isEmpty()) { |
| | | throw new IllegalArgumentException("日期字符串或时间间隔不能为空。"); |
| | | } |
| | | |
| | | // 根据 interval 的值,选择日期格式 |
| | | String pattern = (interval.equalsIgnoreCase("D") || interval.equalsIgnoreCase("W") || interval.equalsIgnoreCase("M")) |
| | | ? DATE_PATTERN : DATE_TIME_PATTERN; |
| | | |
| | | // 创建 DateTimeFormatter,使用合适的格式 |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); |
| | | |
| | | try { |
| | | // 根据是否包含时间部分,选择 LocalDate 或 LocalDateTime 进行解析 |
| | | if (pattern.equals(DATE_PATTERN)) { |
| | | LocalDate localDate = LocalDate.parse(dateStr, formatter); |
| | | Instant timestamp = localDate.atStartOfDay(ZoneOffset.UTC).toInstant(); |
| | | return timestamp.getEpochSecond(); |
| | | } else { |
| | | LocalDateTime localDateTime = LocalDateTime.parse(dateStr, formatter); |
| | | Instant timestamp = localDateTime.toInstant(ZoneOffset.UTC); |
| | | return timestamp.getEpochSecond(); |
| | | } |
| | | } catch (Exception e) { |
| | | throw new IllegalArgumentException("无效的日期格式: " + dateStr, e); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public ServerResponse getOptionStock(HttpServletRequest request) { |