123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691 |
- package com.zx.dataservice.service.impl;
- import com.alibaba.fastjson.JSON;
- import com.jfinal.plugin.activerecord.Db;
- import com.zx.dataservice.controller.WebSocketController;
- import com.zx.dataservice.mapper1.ChoiceStockMapper;
- import com.zx.dataservice.options.DataOptions;
- import com.zx.dataservice.options.RiskOptions;
- import com.zx.dataservice.options.WSOptions;
- 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<File> files = FileTempIndexUtil.getFile(sourceFileUrl);
- if(null == files || 0 == files.size()){
- return;
- }
- List<StockRestPojo> stockRestPojoList = new ArrayList<>();
- List<StockRestRedisVO> stockRestRedisVOList = new ArrayList<>();
- Map<String,List<StockRestRedisVO>>mapws = new HashMap<>();
- // mapws.put("stockRestRedisVO",new ArrayList<>()); // 无风控数据
- for(String key: WSOptions.keySet()){ // 所有的需要推送的 客户
- mapws.put("stockRestRedisVO_"+ WSOptions.get(key).split("_")[0],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<String, List<String>> map = new HashMap<String, List<String>>();
- map = JSON.parseObject(str, HashMap.class);
- str = null;
- for (Map.Entry<String, List<String>> entry : map.entrySet()) {
- try {
- stockRestPojo = new StockRestPojo();
- stockRestPojo.setDate(String.valueOf(entry.getValue().get(0)));
- if(String.valueOf(entry.getValue().get(0)).equals("0")){
- continue;
- }
- 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("0");
- stockRestPojo.setLowlimit("0");
- stockRestPojo.setAverageprice("");
- stockRestPojo.setBuyprice1(randomBuySell(stockRestPojo.getNow(), 0));
- stockRestPojo.setBuyprice2(randomBuySell(stockRestPojo.getBuyprice1(), 0));
- stockRestPojo.setBuyprice3(randomBuySell(stockRestPojo.getBuyprice2(), 0));
- stockRestPojo.setBuyprice4(randomBuySell(stockRestPojo.getBuyprice3(), 0));
- stockRestPojo.setBuyprice5(randomBuySell(stockRestPojo.getBuyprice4(), 0));
- stockRestPojo.setBuyvolume1(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setBuyvolume2(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setBuyvolume3(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setBuyvolume4(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setBuyvolume5(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setSellprice1(randomBuySell(stockRestPojo.getNow(), 1));
- stockRestPojo.setSellprice2(randomBuySell(stockRestPojo.getSellprice1(), 1));
- stockRestPojo.setSellprice3(randomBuySell(stockRestPojo.getSellprice2(), 1));
- stockRestPojo.setSellprice4(randomBuySell(stockRestPojo.getSellprice3(), 1));
- stockRestPojo.setSellprice5(randomBuySell(stockRestPojo.getSellprice4(), 1));
- stockRestPojo.setSellvolume1(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setSellvolume2(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setSellvolume3(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setSellvolume4(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setSellvolume5(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setClosedtime("");
- stockRestPojo.setClosedvolume("");
- stockRestPojo.setClosedamount("");
- StringBuffer dateStr = new StringBuffer(stockRestPojo.getDate());
- if (StringUtils.isEmpty(stockRestPojo.getTime())) {
- dateStr = dateStr.append(000000);
- } else {
- dateStr.append( String.format("%06d", Integer.parseInt(stockRestPojo.getTime())));
- // 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);
- 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());
- stockRestRedisVO.setBuy1(stockRestPojo.getBuyprice1());
- stockRestRedisVO.setBuy2(stockRestPojo.getBuyprice2());
- stockRestRedisVO.setBuy3(stockRestPojo.getBuyprice3());
- stockRestRedisVO.setBuy4(stockRestPojo.getBuyprice4());
- stockRestRedisVO.setBuy5(stockRestPojo.getBuyprice5());
- stockRestRedisVO.setSell1(stockRestPojo.getSellprice1());
- stockRestRedisVO.setSell2(stockRestPojo.getSellprice2());
- stockRestRedisVO.setSell3(stockRestPojo.getSellprice3());
- stockRestRedisVO.setSell4(stockRestPojo.getSellprice4());
- stockRestRedisVO.setSell5(stockRestPojo.getSellprice5());
- stockRestRedisVO.setBuy1_num(stockRestPojo.getBuyvolume1());
- stockRestRedisVO.setBuy2_num(stockRestPojo.getBuyvolume2());
- stockRestRedisVO.setBuy3_num(stockRestPojo.getBuyvolume3());
- stockRestRedisVO.setBuy4_num(stockRestPojo.getBuyvolume4());
- stockRestRedisVO.setBuy5_num(stockRestPojo.getBuyvolume5());
- stockRestRedisVO.setSell1_num(stockRestPojo.getSellvolume1());
- stockRestRedisVO.setSell2_num(stockRestPojo.getSellvolume2());
- stockRestRedisVO.setSell3_num(stockRestPojo.getSellvolume3());
- stockRestRedisVO.setSell4_num(stockRestPojo.getSellvolume4());
- stockRestRedisVO.setSell5_num(stockRestPojo.getSellvolume5());
- // stockRestRedisVOList.add(stockRestRedisVO);// old
- for (String key2 : WSOptions.keySet()){ // 所有的需要推送的客户
- // 如果该股票需要风控
- if(DataOptions.get("keyall").contains((WSOptions.get(key2).split("_")[0]+"_"+entry.getKey().toUpperCase()))){ // 存在该用户的该股票的风控 记录
- for (String key : DataOptions.keySet()) { // 所有的需要风控的 客户 BTC_BTC_hy LTC_hy 000001.SZ_hy LTC_test test_BTC_BTC
- // 风控 逻辑
- if("keyall".equals(key)){
- continue;
- }
- if (key.contains((WSOptions.get(key2).split("_")[0]+"_"+entry.getKey().toUpperCase()))) {// 存在该股票的风控 LTC_hy . contains (LTC)
- StockRestPojo stockRestPojoRisk = getRiskNow(stockRestPojo, key, DataOptions.get(key)); // 风控后的数据 //tocode & mini & risk & multiples & status
- stockRestPojoList.add(stockRestPojoRisk);
- stockRestRedisVO = new StockRestRedisVO();
- stockRestRedisVO.setCode(entry.getKey().toUpperCase());
- stockRestRedisVO.setNowPrice(stockRestPojoRisk.getNow());
- stockRestRedisVO.setChange(stockRestPojoRisk.getChange());
- stockRestRedisVO.setHcrate(stockRestPojoRisk.getPctchange());
- stockRestRedisVO.setHighlimit(stockRestPojoRisk.getHighlimit());
- stockRestRedisVO.setLowlimit(stockRestPojoRisk.getLowlimit());
- stockRestRedisVO.setBuy1(stockRestPojo.getBuyprice1());
- stockRestRedisVO.setBuy2(stockRestPojo.getBuyprice2());
- stockRestRedisVO.setBuy3(stockRestPojo.getBuyprice3());
- stockRestRedisVO.setBuy4(stockRestPojo.getBuyprice4());
- stockRestRedisVO.setBuy5(stockRestPojo.getBuyprice5());
- stockRestRedisVO.setSell1(stockRestPojo.getSellprice1());
- stockRestRedisVO.setSell2(stockRestPojo.getSellprice2());
- stockRestRedisVO.setSell3(stockRestPojo.getSellprice3());
- stockRestRedisVO.setSell4(stockRestPojo.getSellprice4());
- stockRestRedisVO.setSell5(stockRestPojo.getSellprice5());
- stockRestRedisVO.setBuy1_num(stockRestPojo.getBuyvolume1());
- stockRestRedisVO.setBuy2_num(stockRestPojo.getBuyvolume2());
- stockRestRedisVO.setBuy3_num(stockRestPojo.getBuyvolume3());
- stockRestRedisVO.setBuy4_num(stockRestPojo.getBuyvolume4());
- stockRestRedisVO.setBuy5_num(stockRestPojo.getBuyvolume5());
- stockRestRedisVO.setSell1_num(stockRestPojo.getSellvolume1());
- stockRestRedisVO.setSell2_num(stockRestPojo.getSellvolume2());
- stockRestRedisVO.setSell3_num(stockRestPojo.getSellvolume3());
- stockRestRedisVO.setSell4_num(stockRestPojo.getSellvolume4());
- stockRestRedisVO.setSell5_num(stockRestPojo.getSellvolume5());
- mapws.get("stockRestRedisVO_" + WSOptions.get(key2).split("_")[0]).add(stockRestRedisVO); // stockRestRedisVO_hy
- }
- }
- }else {
- mapws.get("stockRestRedisVO_"+ WSOptions.get(key2).split("_")[0]).add(stockRestRedisVO);
- }
- }
- }
- }catch (Exception e){
- log.error("错误数据导致失败" + e);
- }
- }
- map = null;
- }
- // 5.入库
- insertList(stockRestPojoList, mapws,type);
- stockRestPojoList = null;
- }
- @Override
- public void analysisStockRestDataAfter(String type) {
- // 1.获取文件夹文件 按照时间顺序拿到一个
- List<File> files = FileTempIndexUtil.getFile(sourceAfterFileUrl);
- if(null == files || 0 == files.size()){
- return;
- }
- List<StockRestPojo> stockRestPojoList = new ArrayList<>();
- List<StockRestVO> stockRestVOList = new ArrayList<>();
- List<StockRestRedisVO> stockRestRedisVOList = new ArrayList<>();
- Map<String,List<StockRestRedisVO>>mapws = new HashMap<>();
- // mapws.put("stockRestRedisVO",new ArrayList<>()); // 无风控数据
- for(String key: WSOptions.keySet()){ // 所有的需要推送的 客户
- mapws.put("stockRestRedisVO_"+ WSOptions.get(key).split("_")[0],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<String, List<String>> map = new HashMap<String, List<String>>();
- map = JSON.parseObject(str, HashMap.class);
- str = null;
- for (Map.Entry<String, List<String>> 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(randomBuySell(stockRestPojo.getNow(), 0));
- stockRestPojo.setBuyprice2(randomBuySell(stockRestPojo.getBuyprice1(), 0));
- stockRestPojo.setBuyprice3(randomBuySell(stockRestPojo.getBuyprice2(), 0));
- stockRestPojo.setBuyprice4(randomBuySell(stockRestPojo.getBuyprice3(), 0));
- stockRestPojo.setBuyprice5(randomBuySell(stockRestPojo.getBuyprice4(), 0));
- stockRestPojo.setBuyvolume1(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setBuyvolume2(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setBuyvolume3(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setBuyvolume4(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setBuyvolume5(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setSellprice1(randomBuySell(stockRestPojo.getNow(), 1));
- stockRestPojo.setSellprice2(randomBuySell(stockRestPojo.getSellprice1(), 1));
- stockRestPojo.setSellprice3(randomBuySell(stockRestPojo.getSellprice2(), 1));
- stockRestPojo.setSellprice4(randomBuySell(stockRestPojo.getSellprice3(), 1));
- stockRestPojo.setSellprice5(randomBuySell(stockRestPojo.getSellprice4(), 1));
- stockRestPojo.setSellvolume1(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setSellvolume2(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setSellvolume3(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setSellvolume4(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojo.setSellvolume5(String.valueOf(new BigDecimal(randomInt())));
- 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) {
- stockRestPojo.setRealTime((System.currentTimeMillis() / 1000));
- 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);
- stockRestRedisVO.setBuy1(stockRestPojo.getBuyprice1());
- stockRestRedisVO.setBuy2(stockRestPojo.getBuyprice2());
- stockRestRedisVO.setBuy3(stockRestPojo.getBuyprice3());
- stockRestRedisVO.setBuy4(stockRestPojo.getBuyprice4());
- stockRestRedisVO.setBuy5(stockRestPojo.getBuyprice5());
- stockRestRedisVO.setSell1(stockRestPojo.getSellprice1());
- stockRestRedisVO.setSell2(stockRestPojo.getSellprice2());
- stockRestRedisVO.setSell3(stockRestPojo.getSellprice3());
- stockRestRedisVO.setSell4(stockRestPojo.getSellprice4());
- stockRestRedisVO.setSell5(stockRestPojo.getSellprice5());
- stockRestRedisVO.setBuy1_num(stockRestPojo.getBuyvolume1());
- stockRestRedisVO.setBuy2_num(stockRestPojo.getBuyvolume2());
- stockRestRedisVO.setBuy3_num(stockRestPojo.getBuyvolume3());
- stockRestRedisVO.setBuy4_num(stockRestPojo.getBuyvolume4());
- stockRestRedisVO.setBuy5_num(stockRestPojo.getBuyvolume5());
- stockRestRedisVO.setSell1_num(stockRestPojo.getSellvolume1());
- stockRestRedisVO.setSell2_num(stockRestPojo.getSellvolume2());
- stockRestRedisVO.setSell3_num(stockRestPojo.getSellvolume3());
- stockRestRedisVO.setSell4_num(stockRestPojo.getSellvolume4());
- stockRestRedisVO.setSell5_num(stockRestPojo.getSellvolume5());
- for (String key2 : WSOptions.keySet()){ // 所有的需要推送的客户
- // 如果该股票需要风控
- if(DataOptions.get("keyall").contains((WSOptions.get(key2).split("_")[0]+"_"+entry.getKey().toUpperCase()))){ // 存在该用户的该股票的风控 记录
- for (String key : DataOptions.keySet()) { // 所有的需要风控的 客户 BTC_BTC_hy LTC_hy 000001.SZ_hy LTC_test test_BTC_BTC
- // 风控 逻辑
- if("keyall".equals(key)){
- continue;
- }
- if (key.contains((WSOptions.get(key2).split("_")[0]+"_"+entry.getKey().toUpperCase()))) {// 存在该股票的风控 LTC_hy . contains (LTC)
- StockRestPojo stockRestPojoRisk = getRiskNow(stockRestPojo, key, DataOptions.get(key)); // 风控后的数据 //tocode & mini & risk & multiples & status
- stockRestPojoList.add(stockRestPojoRisk);
- stockRestRedisVO = new StockRestRedisVO();
- stockRestRedisVO.setCode(entry.getKey().toUpperCase());
- stockRestRedisVO.setNowPrice(stockRestPojoRisk.getNow());
- stockRestRedisVO.setChange(stockRestPojoRisk.getChange());
- stockRestRedisVO.setHcrate(stockRestPojoRisk.getPctchange());
- stockRestRedisVO.setHighlimit(stockRestPojoRisk.getHighlimit());
- stockRestRedisVO.setLowlimit(stockRestPojoRisk.getLowlimit());
- stockRestRedisVO.setBuy1(stockRestPojo.getBuyprice1());
- stockRestRedisVO.setBuy2(stockRestPojo.getBuyprice2());
- stockRestRedisVO.setBuy3(stockRestPojo.getBuyprice3());
- stockRestRedisVO.setBuy4(stockRestPojo.getBuyprice4());
- stockRestRedisVO.setBuy5(stockRestPojo.getBuyprice5());
- stockRestRedisVO.setSell1(stockRestPojo.getSellprice1());
- stockRestRedisVO.setSell2(stockRestPojo.getSellprice2());
- stockRestRedisVO.setSell3(stockRestPojo.getSellprice3());
- stockRestRedisVO.setSell4(stockRestPojo.getSellprice4());
- stockRestRedisVO.setSell5(stockRestPojo.getSellprice5());
- stockRestRedisVO.setBuy1_num(stockRestPojo.getBuyvolume1());
- stockRestRedisVO.setBuy2_num(stockRestPojo.getBuyvolume2());
- stockRestRedisVO.setBuy3_num(stockRestPojo.getBuyvolume3());
- stockRestRedisVO.setBuy4_num(stockRestPojo.getBuyvolume4());
- stockRestRedisVO.setBuy5_num(stockRestPojo.getBuyvolume5());
- stockRestRedisVO.setSell1_num(stockRestPojo.getSellvolume1());
- stockRestRedisVO.setSell2_num(stockRestPojo.getSellvolume2());
- stockRestRedisVO.setSell3_num(stockRestPojo.getSellvolume3());
- stockRestRedisVO.setSell4_num(stockRestPojo.getSellvolume4());
- stockRestRedisVO.setSell5_num(stockRestPojo.getSellvolume5());
- mapws.get("stockRestRedisVO_" + WSOptions.get(key2).split("_")[0]).add(stockRestRedisVO); // stockRestRedisVO_hy
- }
- }
- }else {
- mapws.get("stockRestRedisVO_"+ WSOptions.get(key2).split("_")[0]).add(stockRestRedisVO);
- }
- }
- }
- }catch (Exception e){
- log.error("错误数据导致失败" + e);
- }
- }
- map = null;
- }
- // 5.入库
- insertListAfter(stockRestPojoList, stockRestVOList, mapws,type);
- stockRestPojoList = null;
- }
- private void insertListAfter(List<StockRestPojo> list, List<StockRestVO> stockRestVOList, Map<String,List<StockRestRedisVO>>mapws ,String type) {
- int insertLength = list.size();
- int i = 0;
- int insertSize = 500;
- while (insertLength > insertSize) {
- choiceStockMapper.insertBatchByAfter(list.subList(i, i + insertSize));
- i = i + insertSize;
- insertLength = insertLength - insertSize;
- }
- if (insertLength > 0) {
- choiceStockMapper.insertBatchByAfter(list.subList(i, i + insertLength));
- }
- Thread thread=new Thread(new Runnable() {
- @Override
- public void run() {
- // HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
- // if(BuyAndSellUtils.isTransState()){
- try {
- new WebSocketController().sendSysMsgType("ZS",mapws);
- // HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
- }catch (Exception e){
- log.error("插入redis错误" + e);
- }
- // }
- }
- });
- thread.start();
- }
- private void insertList(List<StockRestPojo> list,Map<String,List<StockRestRedisVO>>mapws ,String type) { // List<StockRestRedisVO> stockRestRedisVOList
- 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));
- }
- i = i + insertSize;
- insertLength = insertLength - insertSize;
- }
- if (insertLength > 0) {
- if("now".equals(type)){
- choiceStockMapper.insertBatch(list.subList(i, i + insertLength));
- }else {
- choiceStockMapper.insertBatchByAfter(list.subList(i, i + insertLength));
- }
- }
- Thread thread=new Thread(new Runnable() {
- @Override
- public void run() {
- // HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
- // if(BuyAndSellUtils.isTransState()){
- try {
- new WebSocketController().sendSysMsgType("ZS",mapws);
- // HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
- }catch (Exception e){
- log.error("插入redis错误" + e);
- }
- // }
- }
- });
- thread.start();
- }
- public StockRestPojo getRiskNow (StockRestPojo stockRestPojo,String key,String value){
- // KEY = code _ hy // BTC_BTC_hy
- String [] valueArr = value.split("&"); //tocode & mini & risk & multiples & status
- String [] keyArr = key.split("_"); //code _ hy // BTC_BTC_hy CODE to tocode
- StringBuffer tableName = new StringBuffer("data_rt_");
- tableName.append(valueArr[0].replace(".", "_").toLowerCase());
- tableName.append("_"+keyArr[0]);
- if("1".equals(valueArr[4])){ // 如果此时没有开启风控 那么不变化
- StockRestPojo stockRestPojoNew = new StockRestPojo();
- BeanUtils.copyProperties(stockRestPojo,stockRestPojoNew,StockRestPojo.class);
- stockRestPojoNew.setTableName(tableName.toString());
- return stockRestPojoNew;
- }else {
- String tocode = valueArr[0]; // 新币
- String mini = valueArr[1];// 最小波动
- String risk1 = valueArr[2].substring(0, 1); // 风险 +-*/110
- String multiples = valueArr[3]; //倍数
- String risk2 = BigdecimalUtil.getNew(mini,valueArr[2].substring(1, valueArr[2].length()),"*",4,2) ; // 风险 +-*/110
- StockRestPojo stockRestPojoNew = new StockRestPojo();
- BeanUtils.copyProperties(stockRestPojo,stockRestPojoNew,StockRestPojo.class);
- String nnow = BigdecimalUtil.getNew(stockRestPojo.getNow() + "", risk2, risk1, 4, 2, multiples);
- stockRestPojoNew.setTableName(tableName.toString()); // 表
- stockRestPojoNew.setNow(nnow); // 现价
- // 高
- stockRestPojoNew.setHigh(BigdecimalUtil.getNew(stockRestPojo.getHigh() + "", risk2, risk1, 4, 2, multiples));
- // 低
- stockRestPojoNew.setLow(BigdecimalUtil.getNew(stockRestPojo.getLow() + "", risk2, risk1, 4, 2, multiples));
- //开 取当天第一条 存缓存
- if(StringUtils.isBlank(RiskOptions.get("open_"+key))){ // 为空就存 当前的now价为开盘价
- RiskOptions.set("open_"+key,stockRestPojoNew.getNow()+"");
- }
- stockRestPojoNew.setOpen(RiskOptions.get("open_"+key));
- //前收
- if(StringUtils.isBlank(RiskOptions.get("preclose_"+key))){ // 为空就存 当前的计算当前的 收盘价 为收盘价 因为 除非是系统上线 不然不会在这出现为空的情况
- String preclose = (BigdecimalUtil.getNew(stockRestPojo.getPreclose() + "", risk2, risk1, 4, 2, multiples)) ;
- RiskOptions.set("preclose_"+key,preclose);
- }
- stockRestPojoNew.setPreclose(RiskOptions.get("preclose_"+key));
- stockRestPojoNew.setChange((new BigDecimal(stockRestPojoNew.getNow() + "").
- subtract(new BigDecimal(stockRestPojoNew.getPreclose() + ""))).setScale(4, 2)+"");
- stockRestPojoNew.setPctchange(((!"0".equals(stockRestPojoNew.getPreclose())) ?
- ((new BigDecimal(stockRestPojoNew.getChange() + "").divide(new BigDecimal(stockRestPojoNew.getPreclose() + ""), 4, 2))) : 0)+"");
- stockRestPojoNew.setBuyprice1(randomBuySell(stockRestPojoNew.getNow(), 0));
- stockRestPojoNew.setBuyprice2(randomBuySell(stockRestPojoNew.getBuyprice1(), 0));
- stockRestPojoNew.setBuyprice3(randomBuySell(stockRestPojoNew.getBuyprice2(), 0));
- stockRestPojoNew.setBuyprice4(randomBuySell(stockRestPojoNew.getBuyprice3(), 0));
- stockRestPojoNew.setBuyprice5(randomBuySell(stockRestPojoNew.getBuyprice4(), 0));
- stockRestPojoNew.setBuyvolume1(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojoNew.setBuyvolume2(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojoNew.setBuyvolume3(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojoNew.setBuyvolume4(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojoNew.setBuyvolume5(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojoNew.setSellprice1(randomBuySell(stockRestPojoNew.getNow(), 1));
- stockRestPojoNew.setSellprice2(randomBuySell(stockRestPojoNew.getSellprice1(), 1));
- stockRestPojoNew.setSellprice3(randomBuySell(stockRestPojoNew.getSellprice2(), 1));
- stockRestPojoNew.setSellprice4(randomBuySell(stockRestPojoNew.getSellprice3(), 1));
- stockRestPojoNew.setSellprice5(randomBuySell(stockRestPojoNew.getSellprice4(), 1));
- stockRestPojoNew.setSellvolume1(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojoNew.setSellvolume2(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojoNew.setSellvolume3(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojoNew.setSellvolume4(String.valueOf(new BigDecimal(randomInt())));
- stockRestPojoNew.setSellvolume5(String.valueOf(new BigDecimal(randomInt())));
- return stockRestPojoNew;
- }
- }
- private String randomBuySell(String now, int type) {
- // 买
- if (0 == type) {
- return new BigDecimal(now).subtract(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
- } else {
- return new BigDecimal(now).add(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
- }
- }
- private int randomInt() {
- int max = 10000;
- int min = 5000;
- int ran = (int) (Math.random() * (max - min) + min);
- return ran;
- }
- }
|