liuchaojie пре 4 година
родитељ
комит
d38d3e80e2

+ 27 - 0
src/main/java/com/xc/controller/InitStockListRedisController.java

@@ -0,0 +1,27 @@
+package com.xc.controller;
+
+import com.xc.common.ServerResponse;
+import com.xc.service.InitStockListService;
+import com.xc.vo.stock.StockVO;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import java.util.List;
+
+@Controller
+@RequestMapping("/init/redis/")
+public class InitStockListRedisController {
+
+    @Autowired
+    private InitStockListService initStockListService;
+
+    @RequestMapping({"initStockToRedis.do"})
+    @ResponseBody
+    public ServerResponse initStockToRedis() {
+        initStockListService.initStockToRedis();
+        return ServerResponse.createBySuccess();
+    }
+}

+ 61 - 21
src/main/java/com/xc/service/impl/StockServiceImpl.java

@@ -9,6 +9,7 @@ import com.xc.dao.RealTimeMapper;
 import com.xc.dao.StockIndexMapper;
 import com.xc.dao.StockMapper;
 import com.xc.pojo.*;
+import com.xc.pub.redismessage.util.RedisUtil;
 import com.xc.service.DbStockService;
 import com.xc.service.IStockMarketsDayService;
 import com.xc.service.IStockService;
@@ -41,6 +42,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
 
 @Service("iStockService")
 public class StockServiceImpl implements IStockService {
@@ -67,6 +69,9 @@ public class StockServiceImpl implements IStockService {
     @Autowired
     DbStockService dbStockService;
 
+    @Autowired
+    private RedisUtil redisUtil;
+
     public ServerResponse getMarket() {
         String market_url = PropertiesUtil.getProperty("sina.market.url");
         String result = null;
@@ -100,28 +105,63 @@ public class StockServiceImpl implements IStockService {
     }
 
     public ServerResponse getStock(int num, int pageNum, int pageSize, String keyWords, String stockPlate, String stockType) {
-        List<Stock> stockList = null;
-        PageHelper.startPage(pageNum, pageSize);
-        stockList = this.stockMapper.findStockListByKeyWords(keyWords, stockPlate, stockType, 0);
-        List<StockListVO> stockListVOS = Lists.newArrayList();
-        for (Stock stock : stockList) {
-            StockListVO stockListVO = dbStockService.getStockListVO(stock.getStockGid());
-            stockListVO.setName(stock.getStockName());
-            stockListVO.setId(stock.getId());
-            stockListVO.setCode(stock.getStockCode());
-            stockListVO.setSpell(stock.getStockSpell());
-            stockListVO.setGid(stock.getStockGid());
-            BigDecimal day3Rate = (BigDecimal) selectRateByDaysAndStockCode(stock.getStockCode(), 3).getData();
-            stockListVO.setDay3Rate(day3Rate);
-            stockListVO.setStock_plate(stock.getStockPlate());
-            stockListVO.setStock_type(stock.getStockType());
-//            stockListVO.setTransState(isTransState);
-//            stockListVO.setBuyLimit(stock.getBuyLimit());
-            stockListVOS.add(stockListVO);
+        String redisKey = String.format("data_coin");
+//        String redisData = null;
+        String redisData = redisUtil.getItem(redisKey);
+        List<StockVO> stockVOList = new ArrayList<>();
+        if (StringUtils.isNotEmpty(redisData) && !("[]".equals(redisData))) {
+            stockVOList = com.alibaba.fastjson.JSONArray.parseArray(redisData, StockVO.class);
+        }
+        if (CollectionUtils.isEmpty(stockVOList)) {
+            //1、走数据库
+            List<Stock> stockList = null;
+            PageHelper.startPage(pageNum, pageSize);
+            stockList = this.stockMapper.findStockListByKeyWords(keyWords, stockPlate, stockType, 0);
+            List<StockListVO> stockListVOS = Lists.newArrayList();
+            for (Stock stock : stockList) {
+                StockListVO stockListVO = dbStockService.getStockListVO(stock.getStockGid());
+                stockListVO.setName(stock.getStockName());
+                stockListVO.setId(stock.getId());
+                stockListVO.setCode(stock.getStockCode());
+                stockListVO.setSpell(stock.getStockSpell());
+                stockListVO.setGid(stock.getStockGid());
+                BigDecimal day3Rate = (BigDecimal) selectRateByDaysAndStockCode(stock.getStockCode(), 3).getData();
+                stockListVO.setDay3Rate(day3Rate);
+                stockListVO.setStock_plate(stock.getStockPlate());
+                stockListVO.setStock_type(stock.getStockType());
+                stockListVOS.add(stockListVO);
+            }
+            PageInfo pageInfo = new PageInfo(stockList);
+            pageInfo.setList(stockListVOS);
+            return ServerResponse.createBySuccess(pageInfo);
+        }else{
+            //2、走redis
+            List<StockListVO> stockListVOS = new ArrayList<>();
+            StockListVO stockListVO;
+            for (StockVO stockVO : stockVOList) {
+                    stockListVO = new StockListVO();
+                    BeanUtils.copyProperties(stockVO, stockListVO);
+                    stockListVOS.add(stockListVO);
+            }
+            int pageIndex = pageNum;
+            int pageSizes = pageSize;
+            if (0 != pageNum) {
+                pageIndex = (pageNum - 1) * pageSize;
+                pageSizes = pageNum * pageSize;
+            }
+            PageInfo pageInfo = new PageInfo(stockListVOS);
+            pageInfo.setPageSize(pageSize);
+            pageInfo.setSize(pageSize);
+            pageInfo.setPageNum(pageNum);
+            pageInfo.setPages((int)Math.ceil((double)stockListVOS.size() / (double)pageSize));
+            if(!CollectionUtils.isEmpty(stockListVOS) && pageSizes <= stockListVOS.size()) {
+                stockListVOS = stockListVOS.subList(pageIndex, pageSizes);
+            }else if(pageSizes > stockListVOS.size()){
+                stockListVOS = stockListVOS.subList(pageIndex, stockListVOS.size());
+            }
+            pageInfo.setList(stockListVOS);
+            return ServerResponse.createBySuccess(pageInfo);
         }
-        PageInfo pageInfo = new PageInfo(stockList);
-        pageInfo.setList(stockListVOS);
-        return ServerResponse.createBySuccess(pageInfo);
     }
 
     public boolean isTransState(Stock stock){