package com.nq.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.github.pagehelper.PageInfo; import com.nq.common.ServerResponse; import com.nq.service.ConvertBondService; import com.nq.service.IStockService; import com.nq.service.StockDzService; import com.nq.utils.CacheUtil; import com.nq.utils.HttpClientRequest; import com.nq.utils.redis.RedisShardedPoolUtils; import com.nq.utils.stock.lide.LiDeDataUtils; import com.nq.utils.stock.lide.LideDataVo; import com.nq.utils.stock.lide.LideRankDataVo; import com.nq.vo.stock.ChartCellVO; import com.nq.vo.stock.StockListVO; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.concurrent.TimeUnit; @Controller @RequestMapping({"/api/stock/"}) public class StockApiController { private static final Logger log = LoggerFactory.getLogger(StockApiController.class); @Autowired IStockService iStockService; @Autowired StockDzService stockDzService; @Autowired private ConvertBondService converBondService; //查询 股票指数、大盘指数信息 @RequestMapping({"getMarket.do"}) @ResponseBody public ServerResponse getMarket() { return this.iStockService.getMarket(); } //查询官网PC端交易 所有股票信息及指定股票信息 @RequestMapping({"getStock.do"}) @ResponseBody public ServerResponse getStock(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, @RequestParam(value = "stockPlate", required = false) String stockPlate, @RequestParam(value = "stockType", required = false) String stockType, @RequestParam(value = "keyWords", required = false) String keyWords, HttpServletRequest request) { return this.iStockService.getStock(pageNum, pageSize, keyWords, stockPlate, stockType, request); } //通过股票代码查询股票信息 @RequestMapping({"getSingleStock.do"}) @ResponseBody public ServerResponse getSingleStock(@RequestParam("stockType") String stockType,@RequestParam("code") String code, HttpServletRequest request) { return this.iStockService.getSingleStock(stockType,code, request); } @RequestMapping({"getMinK.do"}) @ResponseBody public ServerResponse getMinK(@RequestParam("code") String code, @RequestParam("time") Integer time, @RequestParam("ma") Integer ma, @RequestParam("size") Integer size) { return this.iStockService.getMinK(code, time, ma, size); } /*查询股票日线*/ @RequestMapping({"getDayK.do"}) @ResponseBody public ServerResponse getDayK(@RequestParam("code") String code) { return this.iStockService.getDayK_Echarts(code); } //查询股票历史数据数据 @RequestMapping({"getMinK_Echarts.do"}) @ResponseBody public ServerResponse getMinK_Echarts(@RequestParam("code") String code, @RequestParam("time") Integer time, @RequestParam("ma") Integer ma, @RequestParam("size") Integer size) { return this.iStockService.getMinK_Echarts(code, time, ma, size); } /*期货分时-k线*/ @RequestMapping({"getFuturesMinK_Echarts.do"}) @ResponseBody public ServerResponse getFuturesMinK_Echarts(@RequestParam("code") String code, @RequestParam("time") Integer time, @RequestParam("size") Integer size) { return this.iStockService.getFuturesMinK_Echarts(code, time, size); } /*指数分时-k线*/ @RequestMapping({"getIndexMinK_Echarts.do"}) @ResponseBody public ServerResponse getIndexMinK_Echarts(@RequestParam("code") String code, @RequestParam("time") Integer time, @RequestParam("size") Integer size) { return this.iStockService.getIndexMinK_Echarts(code, time, size); } /*查询期货日线*/ @RequestMapping({"getFuturesDayK.do"}) @ResponseBody public ServerResponse getFuturesDayK(@RequestParam("code") String code) { return this.iStockService.getFuturesDayK(code); } /*指数日线*/ @RequestMapping({"getIndexDayK.do"}) @ResponseBody public ServerResponse getIndexDayK(@RequestParam("code") String code) { return this.iStockService.getIndexDayK(code); } // //查询股票需要换数据源的股票 // @RequestMapping({"stockDataBase.do"}) // @ResponseBody // public ServerResponse stockDataBase() { // return this.iStockService.stockDataBase(); // } /** * 龙虎榜 */ @RequestMapping({"getlhb.do"}) @ResponseBody public ServerResponse getlhb() { return this.iStockService.lhb(); } /** * 十大成交股 */ @RequestMapping({"getTop.do"}) @ResponseBody public ServerResponse getTop(@RequestParam(value = "content")Integer content) { return this.iStockService.top(content); } /** * 每日停牌 */ @RequestMapping({"getStop.do"}) @ResponseBody public ServerResponse getStop() { return this.iStockService.stop(); } /** * 获取涨跌数据 * @return */ @PostMapping({"getZdfNumber.do"}) @ResponseBody public ServerResponse getZdfNumber() { ServerResponse zdfb = this.iStockService.getZdfb(); ArrayList data = (ArrayList) zdfb.getData(); if(CollectionUtils.isEmpty(data)){ return ServerResponse.createByError(); } ChartCellVO chartCellVO = data.get(0); ChartCellVO chartCellVO1 = data.get(3); ChartCellVO chartCellVO3 = data.get(2); ChartCellVO chartCellVO4 = data.get(5); HashMap resultMap = new HashMap<>(); resultMap.put("zhang",Integer.parseInt(chartCellVO.getyAxis())+ Integer.parseInt(chartCellVO1.getyAxis())); resultMap.put("die",Integer.parseInt(chartCellVO3.getyAxis())+ Integer.parseInt(chartCellVO4.getyAxis())); return ServerResponse.createBySuccess(resultMap); } /** * 获取股票根据 涨跌幅 成交额 价格排序 * @return * sort ("根据什么排序 changepercent 涨跌幅 pricechange 涨跌额 volume 成交量 amount 成交额") * asc ("是否升序 0否 1是") * node ("排序的主板类型 hs_bjs 北交所 cyb 创业板 kcb 科创板 hs_a 沪深a股") */ @PostMapping({"getStockSort.do"}) @ResponseBody public ServerResponse getStockMarketZDFB(Integer pageNo,Integer pageSize, String sort, Integer asc, String node,String stockType){ // String key=pageNo+pageSize+sort+asc+node; // String result = (String) CacheUtil.get(key); // if(StringUtils.isEmpty(result)){ ServerResponse stockSort = this.iStockService.getStockSort(pageNo, pageSize, sort, asc, node,stockType); // CacheUtil.set(key, JSON.toJSONString(stockSort.getData()),5000); // return stockSort; // } // log.info("命中缓存:{}",key); return stockSort; } // /** // * 涨停板 // */ // // @PostMapping({"getztb.do"}) // @ResponseBody // public ServerResponse getztb() { // return this.iStockService.ztb(); // } /** * 涨停板 */ @PostMapping({"getztb.do"}) @ResponseBody public ServerResponse getztb() { // String zdfb_stock = RedisShardedPoolUtils.get("ZDFB_STOCK"); // if(!StringUtils.isEmpty(zdfb_stock)){ // JSONArray jsonArray = JSON.parseArray(zdfb_stock); // return ServerResponse.createBySuccess(jsonArray); // }else { // ArrayList o = LiDeDataUtils.pullRankData(); // return ServerResponse.createBySuccess(o); // } return this.iStockService.ztb(); } /** *大宗交易 列表 */ @PostMapping({"getDzList.do"}) @ResponseBody public ServerResponse getDzList() { return this.stockDzService.getDzList(); } // /** // * vip 抢筹列表 // */ // @PostMapping({"getVipList.do"}) // @ResponseBody // public ServerResponse getVipList() { // return this.iStockService.ztb(); // } /** * vip 抢筹列表 */ @PostMapping({"getVipList.do"}) @ResponseBody public ServerResponse getVipList() { ServerResponse stockSort = this.iStockService.getStockSort(1, 15, "changepercent", 0, "hs_a","hs"); if(!ObjectUtils.isEmpty(stockSort.getData())){ JSONArray data = (JSONArray)stockSort.getData(); ArrayList list = new ArrayList<>(); for(int i=0;i map = new HashMap<>(); map.put("c",data.getJSONObject(i).getString("symbol").substring(2)); map.put("p",data.getJSONObject(i).getFloatValue("trade")*1000); map.put("n",data.getJSONObject(i).getString("name")); map.put("zdp",data.getJSONObject(i).getFloatValue("changepercent")); list.add(map); } return ServerResponse.createBySuccess(list); } return ServerResponse.createByError(); } // /** // * vip 抢筹根据股票代码查询 // * @param code // * @return // */ // // @PostMapping({"getVipByCode.do"}) // @ResponseBody // public ServerResponse getVipByCode(String code) { // return this.iStockService.getVipByCode(code); // } /** * vip 抢筹根据股票代码查询 * @param code * @return */ @PostMapping({"getVipByCode.do"}) @ResponseBody public ServerResponse getVipByCode(String code,HttpServletRequest request) { ServerResponse stock = this.iStockService.getStock(1, 15, code, "", "", request); PageInfo data = (PageInfo)stock.getData(); List stockListVOS = data.getList(); ArrayList list = new ArrayList<>(); if(!CollectionUtils.isEmpty(stockListVOS)){ for(StockListVO stockListVO:stockListVOS){ HashMap map = new HashMap<>(); map.put("c",stockListVO.getCode()); map.put("p",Float.parseFloat(stockListVO.getNowPrice())*1000); map.put("n",stockListVO.getName()); map.put("zdp",stockListVO.getHcrate()); list.add(map); } } return ServerResponse.createBySuccess(list); } /** * 手动同步股票数据 * @return */ @PostMapping({"sycnStockData.do"}) @ResponseBody public ServerResponse getStockMarketZDFB(String pass,Integer pageNo,Integer endPage){ if(!StringUtils.isEmpty(pass)&&pass.equals("a888888")){ for( ;pageNo<=endPage;pageNo++){ String market_url = "https://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page="+pageNo+"&num=100&sort=changepercent&asc=0&node=hs_a&symbol=&_s_r_a=setlen"; String result = null; try { log.info("新浪接口:{}",market_url); result = HttpClientRequest.doGet(market_url); JSONArray jsonArray = JSON.parseArray(result); if(jsonArray!=null&&jsonArray.size()>0){ for(int i=0;i