package com.zx.dataservice.service.impl; import com.alibaba.fastjson.JSON; import com.zx.dataservice.controller.WebSocketController; import com.zx.dataservice.mapper1.ChoiceStockMapper; import com.zx.dataservice.pojo.StockRestPojo; import com.zx.dataservice.service.ChoiceTempIndexService; import com.zx.dataservice.utils.*; import com.zx.dataservice.vo.StockRestRedisVO; import com.zx.dataservice.vo.StockRestVO; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.File; import java.math.BigDecimal; import java.util.*; @Service public class ChoiceTempIndexServiceImpl implements ChoiceTempIndexService { private static final Logger log = LoggerFactory.getLogger(ChoiceTempIndexServiceImpl.class); private static final String sourceFileUrl = "/www/wwwroot/csqdata/tempindex"; private static final String goalFileUrl = "/www/wwwroot/csqdata/tempindex/backup"; private static final String url = "http://qwer16888.vip/api/stock/updateBatchStockInfo.do"; private static final String urlRedis = "http://qwer16888.vip/init/redis/updateInfo.do"; private static final String sourceAfterFileUrl = "/www/wwwroot/csqdata/tempindex/after"; private static final String goalAfterFileUrl = "/www/wwwroot/csqdata/tempindex/after/backup"; @Autowired private ChoiceStockMapper choiceStockMapper; @Override public void analysisStockRestData(String type) { // 1.获取文件夹文件 按照时间顺序拿到一个 List files = FileTempIndexUtil.getFile(sourceFileUrl); if(null == files || 0 == files.size()){ return; } List stockRestPojoList = new ArrayList<>(); List stockRestVOList = new ArrayList<>(); List stockRestRedisVOList = new ArrayList<>(); StockRestPojo stockRestPojo; StockRestVO stockRestVO; StockRestRedisVO stockRestRedisVO; for (File file : files){ // 2.移动文件去另外一个文件夹 FileTempIndexUtil.moveFile(sourceFileUrl + File.separator + file.getName(), goalFileUrl); // 3.读取文件内容 String str = FileTempIndexUtil.readFileContent(goalFileUrl + File.separator + file.getName()); // 4.并解析 if(StringUtils.isEmpty(str)){ continue; } Map> map = new HashMap>(); map = JSON.parseObject(str, HashMap.class); str = null; for (Map.Entry> entry : map.entrySet()) { try { stockRestPojo = new StockRestPojo(); stockRestPojo.setDate(String.valueOf(entry.getValue().get(0))); if(String.valueOf(entry.getValue().get(1)).equals("0")){ continue; } stockRestPojo.setTime(String.valueOf(entry.getValue().get(1))); stockRestPojo.setNow(String.valueOf(entry.getValue().get(2))); stockRestPojo.setHigh(String.valueOf(entry.getValue().get(3))); stockRestPojo.setLow(String.valueOf(entry.getValue().get(4))); stockRestPojo.setOpen(String.valueOf(entry.getValue().get(5))); stockRestPojo.setPreclose(String.valueOf(entry.getValue().get(6))); stockRestPojo.setRoundlot(String.valueOf(entry.getValue().get(7))); stockRestPojo.setVolume(String.valueOf(entry.getValue().get(8))); stockRestPojo.setAmount(String.valueOf(entry.getValue().get(9))); stockRestPojo.setChange(String.valueOf(entry.getValue().get(10))); stockRestPojo.setPctchange(String.valueOf(entry.getValue().get(11))); stockRestPojo.setSpeed(String.valueOf(entry.getValue().get(12))); stockRestPojo.setVolumeratio(""); stockRestPojo.setCommissionratio(""); stockRestPojo.setCommissiondiff(""); stockRestPojo.setTradestatus(""); stockRestPojo.setOutvolume(""); stockRestPojo.setInvolume(""); stockRestPojo.setHighlimit(""); stockRestPojo.setLowlimit(""); stockRestPojo.setAverageprice(""); stockRestPojo.setBuyprice1(""); stockRestPojo.setBuyprice2(""); stockRestPojo.setBuyprice3(""); stockRestPojo.setBuyprice4(""); stockRestPojo.setBuyprice5(""); stockRestPojo.setBuyvolume1(""); stockRestPojo.setBuyvolume2(""); stockRestPojo.setBuyvolume3(""); stockRestPojo.setBuyvolume4(""); stockRestPojo.setBuyvolume5(""); stockRestPojo.setSellprice1(""); stockRestPojo.setSellprice2(""); stockRestPojo.setSellprice3(""); stockRestPojo.setSellprice4(""); stockRestPojo.setSellprice5(""); stockRestPojo.setSellvolume1(""); stockRestPojo.setSellvolume2(""); stockRestPojo.setSellvolume3(""); stockRestPojo.setSellvolume4(""); stockRestPojo.setSellvolume5(""); stockRestPojo.setClosedtime(""); stockRestPojo.setClosedvolume(""); stockRestPojo.setClosedamount(""); StringBuffer dateStr = new StringBuffer(stockRestPojo.getDate()); if (StringUtils.isEmpty(stockRestPojo.getTime())) { dateStr = dateStr.append(000000); } else { if (5 == stockRestPojo.getTime().length()) { // 长度为5表示 小时前面需要补0 dateStr = dateStr.append(0).append(stockRestPojo.getTime()); } else { dateStr = dateStr.append(stockRestPojo.getTime()); } } Date date = TimeUtil.strToDate(dateStr.toString()); // 毫秒转成秒 stockRestPojo.setRealTime(date.getTime() / 1000); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int min = calendar.get(Calendar.MINUTE); // 获取分钟 int sec = calendar.get(Calendar.SECOND); // 获取秒数 // 1分钟数据 if (sec != 0) { stockRestPojo.setDateOne(stockRestPojo.getRealTime() + 60 - sec); } else { stockRestPojo.setDateOne(stockRestPojo.getRealTime()); } // 5分钟数据 if (sec != 0 || min % 5 != 0) { stockRestPojo.setDateFive(stockRestPojo.getRealTime() + (5 * 60) - sec - (min % 5 * 60)); } else { stockRestPojo.setDateFive(stockRestPojo.getRealTime()); } // 15分钟数据 if (sec != 0 || min % 15 != 0) { stockRestPojo.setDateFifteen(stockRestPojo.getRealTime() + (15 * 60) - sec - (min % 15 * 60)); } else { stockRestPojo.setDateFifteen(stockRestPojo.getRealTime()); } // 30分钟数据 if (sec != 0 || min % 30 != 0) { stockRestPojo.setDateThirty(stockRestPojo.getRealTime() + (30 * 60) - sec - (min % 30 * 60)); } else { stockRestPojo.setDateThirty(stockRestPojo.getRealTime()); } // 60分钟数据 if (sec != 0 || min % 60 != 0) { stockRestPojo.setDateSixty(stockRestPojo.getRealTime() + (60 * 60) - sec - (min % 60 * 60)); } else { stockRestPojo.setDateSixty(stockRestPojo.getRealTime()); } StringBuffer tableName = new StringBuffer("data_rt_"); tableName.append(entry.getKey().replace(".", "_").toLowerCase()); stockRestPojo.setCode(entry.getKey().toUpperCase()); stockRestPojo.setTableName(tableName.toString()); if(new BigDecimal(stockRestPojo.getNow()+"").compareTo(BigDecimal.ZERO)!=0) { stockRestPojoList.add(stockRestPojo); stockRestVO = new StockRestVO(); BeanUtils.copyProperties(stockRestPojo, stockRestVO); stockRestVOList.add(stockRestVO); stockRestRedisVO = new StockRestRedisVO(); stockRestRedisVO.setCode(entry.getKey().toUpperCase()); stockRestRedisVO.setNowPrice(stockRestPojo.getNow()); stockRestRedisVO.setChange(stockRestPojo.getChange()); stockRestRedisVO.setHcrate(stockRestPojo.getPctchange()); stockRestRedisVO.setHighlimit(stockRestPojo.getHighlimit()); stockRestRedisVO.setLowlimit(stockRestPojo.getLowlimit()); stockRestRedisVOList.add(stockRestRedisVO); } }catch (Exception e){ log.error("错误数据导致失败" + e); } } map = null; } // 5.入库 insertList(stockRestPojoList, stockRestVOList, stockRestRedisVOList,type); stockRestPojoList = null; } @Override public void analysisStockRestDataAfter(String type) { // 1.获取文件夹文件 按照时间顺序拿到一个 List files = FileTempIndexUtil.getFile(sourceAfterFileUrl); if(null == files || 0 == files.size()){ return; } List stockRestPojoList = new ArrayList<>(); List stockRestVOList = new ArrayList<>(); List stockRestRedisVOList = new ArrayList<>(); StockRestPojo stockRestPojo; StockRestVO stockRestVO; StockRestRedisVO stockRestRedisVO; for (File file : files){ // 2.移动文件去另外一个文件夹 FileTempIndexUtil.moveFile(sourceAfterFileUrl + File.separator + file.getName(), goalAfterFileUrl); // 3.读取文件内容 String str = FileTempIndexUtil.readFileContent(goalAfterFileUrl + File.separator + file.getName()); // 4.并解析 if(StringUtils.isEmpty(str)){ continue; } Map> map = new HashMap>(); map = JSON.parseObject(str, HashMap.class); str = null; for (Map.Entry> entry : map.entrySet()) { try { stockRestPojo = new StockRestPojo(); stockRestPojo.setDate(String.valueOf(entry.getValue().get(0))); if(String.valueOf(entry.getValue().get(1)).equals("0")){ continue; } stockRestPojo.setTime("150000"); stockRestPojo.setNow(String.valueOf(entry.getValue().get(2))); stockRestPojo.setHigh(String.valueOf(entry.getValue().get(3))); stockRestPojo.setLow(String.valueOf(entry.getValue().get(4))); stockRestPojo.setOpen(String.valueOf(entry.getValue().get(5))); stockRestPojo.setPreclose(String.valueOf(entry.getValue().get(6))); stockRestPojo.setRoundlot(String.valueOf(entry.getValue().get(7))); stockRestPojo.setVolume(String.valueOf(entry.getValue().get(8))); stockRestPojo.setAmount(String.valueOf(entry.getValue().get(9))); stockRestPojo.setChange(String.valueOf(entry.getValue().get(10))); stockRestPojo.setPctchange(String.valueOf(entry.getValue().get(11))); stockRestPojo.setSpeed(String.valueOf(entry.getValue().get(12))); stockRestPojo.setVolumeratio(""); stockRestPojo.setCommissionratio(""); stockRestPojo.setCommissiondiff(""); stockRestPojo.setTradestatus(""); stockRestPojo.setOutvolume(""); stockRestPojo.setInvolume(""); stockRestPojo.setHighlimit(""); stockRestPojo.setLowlimit(""); stockRestPojo.setAverageprice(""); stockRestPojo.setBuyprice1(""); stockRestPojo.setBuyprice2(""); stockRestPojo.setBuyprice3(""); stockRestPojo.setBuyprice4(""); stockRestPojo.setBuyprice5(""); stockRestPojo.setBuyvolume1(""); stockRestPojo.setBuyvolume2(""); stockRestPojo.setBuyvolume3(""); stockRestPojo.setBuyvolume4(""); stockRestPojo.setBuyvolume5(""); stockRestPojo.setSellprice1(""); stockRestPojo.setSellprice2(""); stockRestPojo.setSellprice3(""); stockRestPojo.setSellprice4(""); stockRestPojo.setSellprice5(""); stockRestPojo.setSellvolume1(""); stockRestPojo.setSellvolume2(""); stockRestPojo.setSellvolume3(""); stockRestPojo.setSellvolume4(""); stockRestPojo.setSellvolume5(""); stockRestPojo.setClosedtime(""); stockRestPojo.setClosedvolume(""); stockRestPojo.setClosedamount(""); StringBuffer dateStr = new StringBuffer(stockRestPojo.getDate()); if (StringUtils.isEmpty(stockRestPojo.getTime())) { dateStr = dateStr.append(000000); } else { if (5 == stockRestPojo.getTime().length()) { // 长度为5表示 小时前面需要补0 dateStr = dateStr.append(0).append(stockRestPojo.getTime()); } else { dateStr = dateStr.append(stockRestPojo.getTime()); } } Date date = TimeUtil.strToDate(dateStr.toString()); // 毫秒转成秒 stockRestPojo.setRealTime(date.getTime() / 1000); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int min = calendar.get(Calendar.MINUTE); // 获取分钟 int sec = calendar.get(Calendar.SECOND); // 获取秒数 // 1分钟数据 if (sec != 0) { stockRestPojo.setDateOne(stockRestPojo.getRealTime() + 60 - sec); } else { stockRestPojo.setDateOne(stockRestPojo.getRealTime()); } // 5分钟数据 if (sec != 0 || min % 5 != 0) { stockRestPojo.setDateFive(stockRestPojo.getRealTime() + (5 * 60) - sec - (min % 5 * 60)); } else { stockRestPojo.setDateFive(stockRestPojo.getRealTime()); } // 15分钟数据 if (sec != 0 || min % 15 != 0) { stockRestPojo.setDateFifteen(stockRestPojo.getRealTime() + (15 * 60) - sec - (min % 15 * 60)); } else { stockRestPojo.setDateFifteen(stockRestPojo.getRealTime()); } // 30分钟数据 if (sec != 0 || min % 30 != 0) { stockRestPojo.setDateThirty(stockRestPojo.getRealTime() + (30 * 60) - sec - (min % 30 * 60)); } else { stockRestPojo.setDateThirty(stockRestPojo.getRealTime()); } // 60分钟数据 if (sec != 0 || min % 60 != 0) { stockRestPojo.setDateSixty(stockRestPojo.getRealTime() + (60 * 60) - sec - (min % 60 * 60)); } else { stockRestPojo.setDateSixty(stockRestPojo.getRealTime()); } StringBuffer tableName = new StringBuffer("data_rt_"); tableName.append(entry.getKey().replace(".", "_").toLowerCase()); stockRestPojo.setCode(entry.getKey().toUpperCase()); stockRestPojo.setTableName(tableName.toString()); if(new BigDecimal(stockRestPojo.getNow()+"").compareTo(BigDecimal.ZERO)!=0) { stockRestPojoList.add(stockRestPojo); stockRestVO = new StockRestVO(); BeanUtils.copyProperties(stockRestPojo, stockRestVO); stockRestVOList.add(stockRestVO); stockRestRedisVO = new StockRestRedisVO(); stockRestRedisVO.setCode(entry.getKey().toUpperCase()); stockRestRedisVO.setNowPrice(stockRestPojo.getNow()); stockRestRedisVO.setChange(stockRestPojo.getChange()); stockRestRedisVO.setHcrate(stockRestPojo.getPctchange()); stockRestRedisVO.setHighlimit(stockRestPojo.getHighlimit()); stockRestRedisVO.setLowlimit(stockRestPojo.getLowlimit()); stockRestRedisVOList.add(stockRestRedisVO); } }catch (Exception e){ log.error("错误数据导致失败" + e); } } map = null; } // 5.入库 insertListAfter(stockRestPojoList, stockRestVOList, stockRestRedisVOList,type); stockRestPojoList = null; } private void insertListAfter(List list, List stockRestVOList, List stockRestRedisVOList,String type) { int insertLength = list.size(); int i = 0; int insertSize = 500; while (insertLength > insertSize) { choiceStockMapper.insertBatchByAfter(list.subList(i, i + insertSize)); List subList = stockRestVOList.subList(i, i + insertSize); List subRedisList = stockRestRedisVOList.subList(i, i + insertSize); i = i + insertSize; insertLength = insertLength - insertSize; Thread thread=new Thread(new Runnable() { @Override public void run() { // HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList)); // if(BuyAndSellUtils.isTransState()){ try { final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key for (int j = 0; j < webArr.length; j++) { new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList)); } // HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList)); }catch (Exception e){ log.error("插入redis错误" + e); } // } } }); thread.start(); } if (insertLength > 0) { choiceStockMapper.insertBatchByAfter(list.subList(i, i + insertLength)); List subList = stockRestVOList.subList(i, i + insertLength); List subRedisList = stockRestRedisVOList.subList(i, i + insertLength); Thread thread=new Thread(new Runnable() { @Override public void run() { // HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList)); // if(BuyAndSellUtils.isTransState()){ try { final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key for (int j = 0; j < webArr.length; j++) { new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList)); } // HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList)); }catch (Exception e){ log.error("插入redis错误" + e); } // } } }); thread.start(); } } private void insertList(List list, List stockRestVOList, List stockRestRedisVOList,String type) { int insertLength = list.size(); int i = 0; int insertSize = 500; while (insertLength > insertSize) { if("now".equals(type)){ choiceStockMapper.insertBatch(list.subList(i, i + insertSize)); }else { choiceStockMapper.insertBatchByAfter(list.subList(i, i + insertSize)); } List subList = stockRestVOList.subList(i, i + insertSize); List subRedisList = stockRestRedisVOList.subList(i, i + insertSize); i = i + insertSize; insertLength = insertLength - insertSize; Thread thread=new Thread(new Runnable() { @Override public void run() { // HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList)); // if(BuyAndSellUtils.isTransState()){ try { final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key for (int j = 0; j < webArr.length; j++) { new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList)); } // HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList)); }catch (Exception e){ log.error("插入redis错误" + e); } // } } }); thread.start(); } if (insertLength > 0) { if("now".equals(type)){ choiceStockMapper.insertBatch(list.subList(i, i + insertLength)); }else { choiceStockMapper.insertBatchByAfter(list.subList(i, i + insertLength)); } List subList = stockRestVOList.subList(i, i + insertLength); List subRedisList = stockRestRedisVOList.subList(i, i + insertLength); Thread thread=new Thread(new Runnable() { @Override public void run() { // HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList)); // if(BuyAndSellUtils.isTransState()){ try { final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key for (int j = 0; j < webArr.length; j++) { new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList)); } // HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList)); }catch (Exception e){ log.error("插入redis错误" + e); } // } } }); thread.start(); } } }