ChoiceTempIndexServiceImpl.java 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. package com.zx.dataservice.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.jfinal.plugin.activerecord.Db;
  4. import com.zx.dataservice.controller.WebSocketController;
  5. import com.zx.dataservice.mapper1.ChoiceStockMapper;
  6. import com.zx.dataservice.options.DataOptions;
  7. import com.zx.dataservice.options.RiskOptions;
  8. import com.zx.dataservice.options.WSOptions;
  9. import com.zx.dataservice.pojo.StockRestPojo;
  10. import com.zx.dataservice.redismessage.util.RedisUtil;
  11. import com.zx.dataservice.service.ChoiceTempIndexService;
  12. import com.zx.dataservice.utils.*;
  13. import com.zx.dataservice.vo.StockRestRedisVO;
  14. import com.zx.dataservice.vo.StockRestVO;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.beans.BeanUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import java.io.File;
  22. import java.math.BigDecimal;
  23. import java.util.*;
  24. import java.util.concurrent.TimeUnit;
  25. @Service
  26. public class ChoiceTempIndexServiceImpl implements ChoiceTempIndexService {
  27. private static final Logger log = LoggerFactory.getLogger(ChoiceTempIndexServiceImpl.class);
  28. private static final String sourceFileUrl = "/www/wwwroot/csqdata/tempindex";
  29. private static final String goalFileUrl = "/www/wwwroot/csqdata/tempindex/backup";
  30. private static final String url = "http://qwer16888.vip/api/stock/updateBatchStockInfo.do";
  31. private static final String urlRedis = "http://qwer16888.vip/init/redis/updateInfo.do";
  32. private static final String sourceAfterFileUrl = "/www/wwwroot/csqdata/tempindex/after";
  33. private static final String goalAfterFileUrl = "/www/wwwroot/csqdata/tempindex/after/backup";
  34. @Autowired
  35. private ChoiceStockMapper choiceStockMapper;
  36. @Autowired
  37. private RedisUtil redisUtil;
  38. @Override
  39. public void analysisStockRestData(String type) {
  40. // 1.获取文件夹文件 按照时间顺序拿到一个
  41. List<File> files = FileTempIndexUtil.getFile(sourceFileUrl);
  42. if(null == files || 0 == files.size()){
  43. return;
  44. }
  45. List<StockRestPojo> stockRestPojoList = new ArrayList<>();
  46. List<StockRestRedisVO> stockRestRedisVOList = new ArrayList<>();
  47. Map<String,List<StockRestRedisVO>>mapws = new HashMap<>();
  48. // mapws.put("stockRestRedisVO",new ArrayList<>()); // 无风控数据
  49. for(String key: WSOptions.keySet()){ // 所有的需要推送的 客户
  50. mapws.put("stockRestRedisVO_"+ WSOptions.get(key).split("_")[0],new ArrayList<>());
  51. }
  52. StockRestPojo stockRestPojo;
  53. StockRestVO stockRestVO;
  54. StockRestRedisVO stockRestRedisVO;
  55. for (File file : files){
  56. // 2.移动文件去另外一个文件夹
  57. FileTempIndexUtil.moveFile(sourceFileUrl + File.separator + file.getName(), goalFileUrl);
  58. // 3.读取文件内容
  59. String str = FileTempIndexUtil.readFileContent(goalFileUrl + File.separator + file.getName());
  60. // 4.并解析
  61. if(StringUtils.isEmpty(str)){
  62. continue;
  63. }
  64. Map<String, List<String>> map = new HashMap<String, List<String>>();
  65. map = JSON.parseObject(str, HashMap.class);
  66. str = null;
  67. for (Map.Entry<String, List<String>> entry : map.entrySet()) {
  68. try {
  69. stockRestPojo = new StockRestPojo();
  70. stockRestPojo.setDate(String.valueOf(entry.getValue().get(0)));
  71. if(String.valueOf(entry.getValue().get(0)).equals("0")){
  72. continue;
  73. }
  74. if(String.valueOf(entry.getValue().get(1)).equals("0")){
  75. continue;
  76. }
  77. stockRestPojo.setTime(String.valueOf(entry.getValue().get(1)));
  78. stockRestPojo.setNow(String.valueOf(entry.getValue().get(2)));
  79. stockRestPojo.setHigh(String.valueOf(entry.getValue().get(3)));
  80. stockRestPojo.setLow(String.valueOf(entry.getValue().get(4)));
  81. stockRestPojo.setOpen(String.valueOf(entry.getValue().get(5)));
  82. stockRestPojo.setPreclose(String.valueOf(entry.getValue().get(6)));
  83. stockRestPojo.setRoundlot(String.valueOf(entry.getValue().get(7)));
  84. stockRestPojo.setVolume(String.valueOf(entry.getValue().get(8)));
  85. stockRestPojo.setAmount(String.valueOf(entry.getValue().get(9)));
  86. stockRestPojo.setChange(String.valueOf(entry.getValue().get(10)));
  87. stockRestPojo.setPctchange(String.valueOf(entry.getValue().get(11)));
  88. stockRestPojo.setSpeed(String.valueOf(entry.getValue().get(12)));
  89. stockRestPojo.setVolumeratio("");
  90. stockRestPojo.setCommissionratio("");
  91. stockRestPojo.setCommissiondiff("");
  92. stockRestPojo.setTradestatus("");
  93. stockRestPojo.setOutvolume("");
  94. stockRestPojo.setInvolume("");
  95. stockRestPojo.setHighlimit("0");
  96. stockRestPojo.setLowlimit("0");
  97. stockRestPojo.setAverageprice("");
  98. stockRestPojo.setBuyprice1(randomBuySell(stockRestPojo.getNow(), 0));
  99. stockRestPojo.setBuyprice2(randomBuySell(stockRestPojo.getBuyprice1(), 0));
  100. stockRestPojo.setBuyprice3(randomBuySell(stockRestPojo.getBuyprice2(), 0));
  101. stockRestPojo.setBuyprice4(randomBuySell(stockRestPojo.getBuyprice3(), 0));
  102. stockRestPojo.setBuyprice5(randomBuySell(stockRestPojo.getBuyprice4(), 0));
  103. stockRestPojo.setBuyvolume1(String.valueOf(new BigDecimal(randomInt())));
  104. stockRestPojo.setBuyvolume2(String.valueOf(new BigDecimal(randomInt())));
  105. stockRestPojo.setBuyvolume3(String.valueOf(new BigDecimal(randomInt())));
  106. stockRestPojo.setBuyvolume4(String.valueOf(new BigDecimal(randomInt())));
  107. stockRestPojo.setBuyvolume5(String.valueOf(new BigDecimal(randomInt())));
  108. stockRestPojo.setSellprice1(randomBuySell(stockRestPojo.getNow(), 1));
  109. stockRestPojo.setSellprice2(randomBuySell(stockRestPojo.getSellprice1(), 1));
  110. stockRestPojo.setSellprice3(randomBuySell(stockRestPojo.getSellprice2(), 1));
  111. stockRestPojo.setSellprice4(randomBuySell(stockRestPojo.getSellprice3(), 1));
  112. stockRestPojo.setSellprice5(randomBuySell(stockRestPojo.getSellprice4(), 1));
  113. stockRestPojo.setSellvolume1(String.valueOf(new BigDecimal(randomInt())));
  114. stockRestPojo.setSellvolume2(String.valueOf(new BigDecimal(randomInt())));
  115. stockRestPojo.setSellvolume3(String.valueOf(new BigDecimal(randomInt())));
  116. stockRestPojo.setSellvolume4(String.valueOf(new BigDecimal(randomInt())));
  117. stockRestPojo.setSellvolume5(String.valueOf(new BigDecimal(randomInt())));
  118. stockRestPojo.setClosedtime("");
  119. stockRestPojo.setClosedvolume("");
  120. stockRestPojo.setClosedamount("");
  121. StringBuffer dateStr = new StringBuffer(stockRestPojo.getDate());
  122. if (StringUtils.isEmpty(stockRestPojo.getTime())) {
  123. dateStr = dateStr.append(000000);
  124. } else {
  125. dateStr.append( String.format("%06d", Integer.parseInt(stockRestPojo.getTime())));
  126. // if (5 == stockRestPojo.getTime().length()) {
  127. // // 长度为5表示 小时前面需要补0
  128. // dateStr = dateStr.append(0).append(stockRestPojo.getTime());
  129. // } else {
  130. // dateStr = dateStr.append(stockRestPojo.getTime());
  131. // }
  132. }
  133. Date date = TimeUtil.strToDate(dateStr.toString());
  134. // 毫秒转成秒
  135. stockRestPojo.setRealTime(date.getTime() / 1000);
  136. Calendar calendar = Calendar.getInstance();
  137. calendar.setTime(date);
  138. int min = calendar.get(Calendar.MINUTE); // 获取分钟
  139. int sec = calendar.get(Calendar.SECOND); // 获取秒数
  140. // 1分钟数据
  141. if (sec != 0) {
  142. stockRestPojo.setDateOne(stockRestPojo.getRealTime() + 60 - sec);
  143. } else {
  144. stockRestPojo.setDateOne(stockRestPojo.getRealTime());
  145. }
  146. // 5分钟数据
  147. if (sec != 0 || min % 5 != 0) {
  148. stockRestPojo.setDateFive(stockRestPojo.getRealTime() + (5 * 60) - sec - (min % 5 * 60));
  149. } else {
  150. stockRestPojo.setDateFive(stockRestPojo.getRealTime());
  151. }
  152. // 15分钟数据
  153. if (sec != 0 || min % 15 != 0) {
  154. stockRestPojo.setDateFifteen(stockRestPojo.getRealTime() + (15 * 60) - sec - (min % 15 * 60));
  155. } else {
  156. stockRestPojo.setDateFifteen(stockRestPojo.getRealTime());
  157. }
  158. // 30分钟数据
  159. if (sec != 0 || min % 30 != 0) {
  160. stockRestPojo.setDateThirty(stockRestPojo.getRealTime() + (30 * 60) - sec - (min % 30 * 60));
  161. } else {
  162. stockRestPojo.setDateThirty(stockRestPojo.getRealTime());
  163. }
  164. // 60分钟数据
  165. if (sec != 0 || min % 60 != 0) {
  166. stockRestPojo.setDateSixty(stockRestPojo.getRealTime() + (60 * 60) - sec - (min % 60 * 60));
  167. } else {
  168. stockRestPojo.setDateSixty(stockRestPojo.getRealTime());
  169. }
  170. StringBuffer tableName = new StringBuffer("data_rt_");
  171. tableName.append(entry.getKey().replace(".", "_").toLowerCase());
  172. stockRestPojo.setCode(entry.getKey().toUpperCase());
  173. stockRestPojo.setTableName(tableName.toString());
  174. if(new BigDecimal(stockRestPojo.getNow()+"").compareTo(BigDecimal.ZERO)!=0) {
  175. stockRestPojoList.add(stockRestPojo);
  176. stockRestVO = new StockRestVO();
  177. BeanUtils.copyProperties(stockRestPojo, stockRestVO);
  178. stockRestRedisVO = new StockRestRedisVO();
  179. stockRestRedisVO.setCode(entry.getKey().toUpperCase());
  180. stockRestRedisVO.setNowPrice(stockRestPojo.getNow());
  181. stockRestRedisVO.setChange(stockRestPojo.getChange());
  182. stockRestRedisVO.setHcrate(stockRestPojo.getPctchange());
  183. stockRestRedisVO.setHighlimit(stockRestPojo.getHighlimit());
  184. stockRestRedisVO.setLowlimit(stockRestPojo.getLowlimit());
  185. stockRestRedisVO.setBuy1(stockRestPojo.getBuyprice1());
  186. stockRestRedisVO.setBuy2(stockRestPojo.getBuyprice2());
  187. stockRestRedisVO.setBuy3(stockRestPojo.getBuyprice3());
  188. stockRestRedisVO.setBuy4(stockRestPojo.getBuyprice4());
  189. stockRestRedisVO.setBuy5(stockRestPojo.getBuyprice5());
  190. stockRestRedisVO.setSell1(stockRestPojo.getSellprice1());
  191. stockRestRedisVO.setSell2(stockRestPojo.getSellprice2());
  192. stockRestRedisVO.setSell3(stockRestPojo.getSellprice3());
  193. stockRestRedisVO.setSell4(stockRestPojo.getSellprice4());
  194. stockRestRedisVO.setSell5(stockRestPojo.getSellprice5());
  195. stockRestRedisVO.setBuy1_num(stockRestPojo.getBuyvolume1());
  196. stockRestRedisVO.setBuy2_num(stockRestPojo.getBuyvolume2());
  197. stockRestRedisVO.setBuy3_num(stockRestPojo.getBuyvolume3());
  198. stockRestRedisVO.setBuy4_num(stockRestPojo.getBuyvolume4());
  199. stockRestRedisVO.setBuy5_num(stockRestPojo.getBuyvolume5());
  200. stockRestRedisVO.setSell1_num(stockRestPojo.getSellvolume1());
  201. stockRestRedisVO.setSell2_num(stockRestPojo.getSellvolume2());
  202. stockRestRedisVO.setSell3_num(stockRestPojo.getSellvolume3());
  203. stockRestRedisVO.setSell4_num(stockRestPojo.getSellvolume4());
  204. stockRestRedisVO.setSell5_num(stockRestPojo.getSellvolume5());
  205. // stockRestRedisVOList.add(stockRestRedisVO);// old
  206. for (String key2 : WSOptions.keySet()){ // 所有的需要推送的客户
  207. // 如果该股票需要风控
  208. if(DataOptions.get("keyall").contains((WSOptions.get(key2).split("_")[0]+"_"+entry.getKey().toUpperCase()))){ // 存在该用户的该股票的风控 记录
  209. for (String key : DataOptions.keySet()) { // 所有的需要风控的 客户 BTC_BTC_hy LTC_hy 000001.SZ_hy LTC_test test_BTC_BTC
  210. // 风控 逻辑
  211. if("keyall".equals(key)){
  212. continue;
  213. }
  214. if (key.contains((WSOptions.get(key2).split("_")[0]+"_"+entry.getKey().toUpperCase()))) {// 存在该股票的风控 LTC_hy . contains (LTC)
  215. StockRestPojo stockRestPojoRisk = getRiskNow(stockRestPojo, key, DataOptions.get(key)); // 风控后的数据 //tocode & mini & risk & multiples & status
  216. stockRestPojoList.add(stockRestPojoRisk);
  217. StockRestRedisVO stockRestRedisVOf = new StockRestRedisVO();
  218. stockRestRedisVOf.setCode(stockRestPojoRisk.getCode().toUpperCase());
  219. stockRestRedisVOf.setNowPrice(stockRestPojoRisk.getNow());
  220. stockRestRedisVOf.setChange(stockRestPojoRisk.getChange());
  221. stockRestRedisVOf.setHcrate(stockRestPojoRisk.getPctchange());
  222. stockRestRedisVOf.setHighlimit(stockRestPojoRisk.getHighlimit());
  223. stockRestRedisVOf.setLowlimit(stockRestPojoRisk.getLowlimit());
  224. stockRestRedisVOf.setBuy1(stockRestPojo.getBuyprice1());
  225. stockRestRedisVOf.setBuy2(stockRestPojo.getBuyprice2());
  226. stockRestRedisVOf.setBuy3(stockRestPojo.getBuyprice3());
  227. stockRestRedisVOf.setBuy4(stockRestPojo.getBuyprice4());
  228. stockRestRedisVOf.setBuy5(stockRestPojo.getBuyprice5());
  229. stockRestRedisVOf.setSell1(stockRestPojo.getSellprice1());
  230. stockRestRedisVOf.setSell2(stockRestPojo.getSellprice2());
  231. stockRestRedisVOf.setSell3(stockRestPojo.getSellprice3());
  232. stockRestRedisVOf.setSell4(stockRestPojo.getSellprice4());
  233. stockRestRedisVOf.setSell5(stockRestPojo.getSellprice5());
  234. stockRestRedisVOf.setBuy1_num(stockRestPojo.getBuyvolume1());
  235. stockRestRedisVOf.setBuy2_num(stockRestPojo.getBuyvolume2());
  236. stockRestRedisVOf.setBuy3_num(stockRestPojo.getBuyvolume3());
  237. stockRestRedisVOf.setBuy4_num(stockRestPojo.getBuyvolume4());
  238. stockRestRedisVOf.setBuy5_num(stockRestPojo.getBuyvolume5());
  239. stockRestRedisVOf.setSell1_num(stockRestPojo.getSellvolume1());
  240. stockRestRedisVOf.setSell2_num(stockRestPojo.getSellvolume2());
  241. stockRestRedisVOf.setSell3_num(stockRestPojo.getSellvolume3());
  242. stockRestRedisVOf.setSell4_num(stockRestPojo.getSellvolume4());
  243. stockRestRedisVOf.setSell5_num(stockRestPojo.getSellvolume5());
  244. redisUtil.saveItem("index_"+WSOptions.get(key2).split("_")[0]+"_"+stockRestRedisVOf.getCode(),JSON.toJSONString(stockRestRedisVOf), 10, TimeUnit.DAYS);
  245. // mapws.get("stockRestRedisVO_" + WSOptions.get(key2).split("_")[0]).add(stockRestRedisVOf); // stockRestRedisVO_hy
  246. }
  247. }
  248. }else {
  249. redisUtil.saveItem("index_"+WSOptions.get(key2).split("_")[0]+"_"+stockRestRedisVO.getCode(),JSON.toJSONString(stockRestRedisVO), 10, TimeUnit.DAYS);
  250. // mapws.get("stockRestRedisVO_"+ WSOptions.get(key2).split("_")[0]).add(stockRestRedisVO);
  251. }
  252. }
  253. }
  254. }catch (Exception e){
  255. log.error("错误数据导致失败" + e);
  256. }
  257. }
  258. map = null;
  259. }
  260. // 5.入库
  261. insertList(stockRestPojoList, mapws,type);
  262. stockRestPojoList = null;
  263. }
  264. @Override
  265. public void analysisStockRestDataAfter(String type) {
  266. // 1.获取文件夹文件 按照时间顺序拿到一个
  267. List<File> files = FileTempIndexUtil.getFile(sourceAfterFileUrl);
  268. if(null == files || 0 == files.size()){
  269. return;
  270. }
  271. List<StockRestPojo> stockRestPojoList = new ArrayList<>();
  272. List<StockRestVO> stockRestVOList = new ArrayList<>();
  273. List<StockRestRedisVO> stockRestRedisVOList = new ArrayList<>();
  274. Map<String,List<StockRestRedisVO>>mapws = new HashMap<>();
  275. // mapws.put("stockRestRedisVO",new ArrayList<>()); // 无风控数据
  276. // for(String key: WSOptions.keySet()){ // 所有的需要推送的 客户
  277. // mapws.put("stockRestRedisVO_"+ WSOptions.get(key).split("_")[0],new ArrayList<>());
  278. // }
  279. StockRestPojo stockRestPojo;
  280. StockRestVO stockRestVO;
  281. StockRestRedisVO stockRestRedisVO;
  282. for (File file : files){
  283. // 2.移动文件去另外一个文件夹
  284. FileTempIndexUtil.moveFile(sourceAfterFileUrl + File.separator + file.getName(), goalAfterFileUrl);
  285. // 3.读取文件内容
  286. String str = FileTempIndexUtil.readFileContent(goalAfterFileUrl + File.separator + file.getName());
  287. // 4.并解析
  288. if(StringUtils.isEmpty(str)){
  289. continue;
  290. }
  291. Map<String, List<String>> map = new HashMap<String, List<String>>();
  292. map = JSON.parseObject(str, HashMap.class);
  293. str = null;
  294. for (Map.Entry<String, List<String>> entry : map.entrySet()) {
  295. try {
  296. stockRestPojo = new StockRestPojo();
  297. stockRestPojo.setDate(String.valueOf(entry.getValue().get(0)));
  298. if(String.valueOf(entry.getValue().get(1)).equals("0")){
  299. continue;
  300. }
  301. stockRestPojo.setTime("150000");
  302. stockRestPojo.setNow(String.valueOf(entry.getValue().get(2)));
  303. stockRestPojo.setHigh(String.valueOf(entry.getValue().get(3)));
  304. stockRestPojo.setLow(String.valueOf(entry.getValue().get(4)));
  305. stockRestPojo.setOpen(String.valueOf(entry.getValue().get(5)));
  306. stockRestPojo.setPreclose(String.valueOf(entry.getValue().get(6)));
  307. stockRestPojo.setRoundlot(String.valueOf(entry.getValue().get(7)));
  308. stockRestPojo.setVolume(String.valueOf(entry.getValue().get(8)));
  309. stockRestPojo.setAmount(String.valueOf(entry.getValue().get(9)));
  310. stockRestPojo.setChange(String.valueOf(entry.getValue().get(10)));
  311. stockRestPojo.setPctchange(String.valueOf(entry.getValue().get(11)));
  312. stockRestPojo.setSpeed(String.valueOf(entry.getValue().get(12)));
  313. stockRestPojo.setVolumeratio("");
  314. stockRestPojo.setCommissionratio("");
  315. stockRestPojo.setCommissiondiff("");
  316. stockRestPojo.setTradestatus("");
  317. stockRestPojo.setOutvolume("");
  318. stockRestPojo.setInvolume("");
  319. stockRestPojo.setHighlimit("");
  320. stockRestPojo.setLowlimit("");
  321. stockRestPojo.setAverageprice("");
  322. stockRestPojo.setBuyprice1(randomBuySell(stockRestPojo.getNow(), 0));
  323. stockRestPojo.setBuyprice2(randomBuySell(stockRestPojo.getBuyprice1(), 0));
  324. stockRestPojo.setBuyprice3(randomBuySell(stockRestPojo.getBuyprice2(), 0));
  325. stockRestPojo.setBuyprice4(randomBuySell(stockRestPojo.getBuyprice3(), 0));
  326. stockRestPojo.setBuyprice5(randomBuySell(stockRestPojo.getBuyprice4(), 0));
  327. stockRestPojo.setBuyvolume1(String.valueOf(new BigDecimal(randomInt())));
  328. stockRestPojo.setBuyvolume2(String.valueOf(new BigDecimal(randomInt())));
  329. stockRestPojo.setBuyvolume3(String.valueOf(new BigDecimal(randomInt())));
  330. stockRestPojo.setBuyvolume4(String.valueOf(new BigDecimal(randomInt())));
  331. stockRestPojo.setBuyvolume5(String.valueOf(new BigDecimal(randomInt())));
  332. stockRestPojo.setSellprice1(randomBuySell(stockRestPojo.getNow(), 1));
  333. stockRestPojo.setSellprice2(randomBuySell(stockRestPojo.getSellprice1(), 1));
  334. stockRestPojo.setSellprice3(randomBuySell(stockRestPojo.getSellprice2(), 1));
  335. stockRestPojo.setSellprice4(randomBuySell(stockRestPojo.getSellprice3(), 1));
  336. stockRestPojo.setSellprice5(randomBuySell(stockRestPojo.getSellprice4(), 1));
  337. stockRestPojo.setSellvolume1(String.valueOf(new BigDecimal(randomInt())));
  338. stockRestPojo.setSellvolume2(String.valueOf(new BigDecimal(randomInt())));
  339. stockRestPojo.setSellvolume3(String.valueOf(new BigDecimal(randomInt())));
  340. stockRestPojo.setSellvolume4(String.valueOf(new BigDecimal(randomInt())));
  341. stockRestPojo.setSellvolume5(String.valueOf(new BigDecimal(randomInt())));
  342. stockRestPojo.setClosedtime("");
  343. stockRestPojo.setClosedvolume("");
  344. stockRestPojo.setClosedamount("");
  345. StringBuffer dateStr = new StringBuffer(stockRestPojo.getDate());
  346. if (StringUtils.isEmpty(stockRestPojo.getTime())) {
  347. dateStr = dateStr.append(000000);
  348. } else {
  349. if (5 == stockRestPojo.getTime().length()) {
  350. // 长度为5表示 小时前面需要补0
  351. dateStr = dateStr.append(0).append(stockRestPojo.getTime());
  352. } else {
  353. dateStr = dateStr.append(stockRestPojo.getTime());
  354. }
  355. }
  356. Date date = TimeUtil.strToDate(dateStr.toString());
  357. // 毫秒转成秒
  358. stockRestPojo.setRealTime(date.getTime() / 1000);
  359. Calendar calendar = Calendar.getInstance();
  360. calendar.setTime(date);
  361. int min = calendar.get(Calendar.MINUTE); // 获取分钟
  362. int sec = calendar.get(Calendar.SECOND); // 获取秒数
  363. // 1分钟数据
  364. if (sec != 0) {
  365. stockRestPojo.setDateOne(stockRestPojo.getRealTime() + 60 - sec);
  366. } else {
  367. stockRestPojo.setDateOne(stockRestPojo.getRealTime());
  368. }
  369. // 5分钟数据
  370. if (sec != 0 || min % 5 != 0) {
  371. stockRestPojo.setDateFive(stockRestPojo.getRealTime() + (5 * 60) - sec - (min % 5 * 60));
  372. } else {
  373. stockRestPojo.setDateFive(stockRestPojo.getRealTime());
  374. }
  375. // 15分钟数据
  376. if (sec != 0 || min % 15 != 0) {
  377. stockRestPojo.setDateFifteen(stockRestPojo.getRealTime() + (15 * 60) - sec - (min % 15 * 60));
  378. } else {
  379. stockRestPojo.setDateFifteen(stockRestPojo.getRealTime());
  380. }
  381. // 30分钟数据
  382. if (sec != 0 || min % 30 != 0) {
  383. stockRestPojo.setDateThirty(stockRestPojo.getRealTime() + (30 * 60) - sec - (min % 30 * 60));
  384. } else {
  385. stockRestPojo.setDateThirty(stockRestPojo.getRealTime());
  386. }
  387. // 60分钟数据
  388. if (sec != 0 || min % 60 != 0) {
  389. stockRestPojo.setDateSixty(stockRestPojo.getRealTime() + (60 * 60) - sec - (min % 60 * 60));
  390. } else {
  391. stockRestPojo.setDateSixty(stockRestPojo.getRealTime());
  392. }
  393. StringBuffer tableName = new StringBuffer("data_rt_");
  394. tableName.append(entry.getKey().replace(".", "_").toLowerCase());
  395. stockRestPojo.setCode(entry.getKey().toUpperCase());
  396. stockRestPojo.setTableName(tableName.toString());
  397. if(new BigDecimal(stockRestPojo.getNow()+"").compareTo(BigDecimal.ZERO)!=0) {
  398. stockRestPojo.setRealTime((System.currentTimeMillis() / 1000));
  399. stockRestPojoList.add(stockRestPojo);
  400. stockRestVO = new StockRestVO();
  401. BeanUtils.copyProperties(stockRestPojo, stockRestVO);
  402. stockRestVOList.add(stockRestVO);
  403. stockRestRedisVO = new StockRestRedisVO();
  404. stockRestRedisVO.setCode(entry.getKey().toUpperCase());
  405. stockRestRedisVO.setNowPrice(stockRestPojo.getNow());
  406. stockRestRedisVO.setChange(stockRestPojo.getChange());
  407. stockRestRedisVO.setHcrate(stockRestPojo.getPctchange());
  408. stockRestRedisVO.setHighlimit(stockRestPojo.getHighlimit());
  409. stockRestRedisVO.setLowlimit(stockRestPojo.getLowlimit());
  410. // stockRestRedisVOList.add(stockRestRedisVO);
  411. stockRestRedisVO.setBuy1(stockRestPojo.getBuyprice1());
  412. stockRestRedisVO.setBuy2(stockRestPojo.getBuyprice2());
  413. stockRestRedisVO.setBuy3(stockRestPojo.getBuyprice3());
  414. stockRestRedisVO.setBuy4(stockRestPojo.getBuyprice4());
  415. stockRestRedisVO.setBuy5(stockRestPojo.getBuyprice5());
  416. stockRestRedisVO.setSell1(stockRestPojo.getSellprice1());
  417. stockRestRedisVO.setSell2(stockRestPojo.getSellprice2());
  418. stockRestRedisVO.setSell3(stockRestPojo.getSellprice3());
  419. stockRestRedisVO.setSell4(stockRestPojo.getSellprice4());
  420. stockRestRedisVO.setSell5(stockRestPojo.getSellprice5());
  421. stockRestRedisVO.setBuy1_num(stockRestPojo.getBuyvolume1());
  422. stockRestRedisVO.setBuy2_num(stockRestPojo.getBuyvolume2());
  423. stockRestRedisVO.setBuy3_num(stockRestPojo.getBuyvolume3());
  424. stockRestRedisVO.setBuy4_num(stockRestPojo.getBuyvolume4());
  425. stockRestRedisVO.setBuy5_num(stockRestPojo.getBuyvolume5());
  426. stockRestRedisVO.setSell1_num(stockRestPojo.getSellvolume1());
  427. stockRestRedisVO.setSell2_num(stockRestPojo.getSellvolume2());
  428. stockRestRedisVO.setSell3_num(stockRestPojo.getSellvolume3());
  429. stockRestRedisVO.setSell4_num(stockRestPojo.getSellvolume4());
  430. stockRestRedisVO.setSell5_num(stockRestPojo.getSellvolume5());
  431. for (String key2 : WSOptions.keySet()){ // 所有的需要推送的客户
  432. // 如果该股票需要风控
  433. if(DataOptions.get("keyall").contains((WSOptions.get(key2).split("_")[0]+"_"+entry.getKey().toUpperCase()))){ // 存在该用户的该股票的风控 记录
  434. for (String key : DataOptions.keySet()) { // 所有的需要风控的 客户 BTC_BTC_hy LTC_hy 000001.SZ_hy LTC_test test_BTC_BTC
  435. // 风控 逻辑
  436. if("keyall".equals(key)){
  437. continue;
  438. }
  439. if (key.contains((WSOptions.get(key2).split("_")[0]+"_"+entry.getKey().toUpperCase()))) {// 存在该股票的风控 LTC_hy . contains (LTC)
  440. StockRestPojo stockRestPojoRisk = getRiskNow(stockRestPojo, key, DataOptions.get(key)); // 风控后的数据 //tocode & mini & risk & multiples & status
  441. stockRestPojoList.add(stockRestPojoRisk);
  442. StockRestRedisVO stockRestRedisVOf = new StockRestRedisVO();
  443. stockRestRedisVOf.setCode(stockRestPojoRisk.getCode().toUpperCase());
  444. stockRestRedisVOf.setNowPrice(stockRestPojoRisk.getNow());
  445. stockRestRedisVOf.setChange(stockRestPojoRisk.getChange());
  446. stockRestRedisVOf.setHcrate(stockRestPojoRisk.getPctchange());
  447. stockRestRedisVOf.setHighlimit(stockRestPojoRisk.getHighlimit());
  448. stockRestRedisVOf.setLowlimit(stockRestPojoRisk.getLowlimit());
  449. stockRestRedisVOf.setBuy1(stockRestPojo.getBuyprice1());
  450. stockRestRedisVOf.setBuy2(stockRestPojo.getBuyprice2());
  451. stockRestRedisVOf.setBuy3(stockRestPojo.getBuyprice3());
  452. stockRestRedisVOf.setBuy4(stockRestPojo.getBuyprice4());
  453. stockRestRedisVOf.setBuy5(stockRestPojo.getBuyprice5());
  454. stockRestRedisVOf.setSell1(stockRestPojo.getSellprice1());
  455. stockRestRedisVOf.setSell2(stockRestPojo.getSellprice2());
  456. stockRestRedisVOf.setSell3(stockRestPojo.getSellprice3());
  457. stockRestRedisVOf.setSell4(stockRestPojo.getSellprice4());
  458. stockRestRedisVOf.setSell5(stockRestPojo.getSellprice5());
  459. stockRestRedisVOf.setBuy1_num(stockRestPojo.getBuyvolume1());
  460. stockRestRedisVOf.setBuy2_num(stockRestPojo.getBuyvolume2());
  461. stockRestRedisVOf.setBuy3_num(stockRestPojo.getBuyvolume3());
  462. stockRestRedisVOf.setBuy4_num(stockRestPojo.getBuyvolume4());
  463. stockRestRedisVOf.setBuy5_num(stockRestPojo.getBuyvolume5());
  464. stockRestRedisVOf.setSell1_num(stockRestPojo.getSellvolume1());
  465. stockRestRedisVOf.setSell2_num(stockRestPojo.getSellvolume2());
  466. stockRestRedisVOf.setSell3_num(stockRestPojo.getSellvolume3());
  467. stockRestRedisVOf.setSell4_num(stockRestPojo.getSellvolume4());
  468. stockRestRedisVOf.setSell5_num(stockRestPojo.getSellvolume5());
  469. redisUtil.saveItem("index_"+WSOptions.get(key2).split("_")[0]+"_"+stockRestRedisVOf.getCode(),JSON.toJSONString(stockRestRedisVOf), 10, TimeUnit.DAYS);
  470. // mapws.get("stockRestRedisVO_" + WSOptions.get(key2).split("_")[0]).add(stockRestRedisVO); // stockRestRedisVO_hy
  471. }
  472. }
  473. }else {
  474. redisUtil.saveItem("index_"+WSOptions.get(key2).split("_")[0]+"_"+stockRestRedisVO.getCode(),JSON.toJSONString(stockRestRedisVO), 10, TimeUnit.DAYS);
  475. // mapws.get("stockRestRedisVO_"+ WSOptions.get(key2).split("_")[0]).add(stockRestRedisVO);
  476. }
  477. }
  478. }
  479. }catch (Exception e){
  480. log.error("错误数据导致失败" + e);
  481. }
  482. }
  483. map = null;
  484. }
  485. // 5.入库
  486. insertListAfter(stockRestPojoList, stockRestVOList, mapws,type);
  487. stockRestPojoList = null;
  488. }
  489. private void insertListAfter(List<StockRestPojo> list, List<StockRestVO> stockRestVOList, Map<String,List<StockRestRedisVO>>mapws ,String type) {
  490. int insertLength = list.size();
  491. int i = 0;
  492. int insertSize = 500;
  493. while (insertLength > insertSize) {
  494. choiceStockMapper.insertBatchByAfter(list.subList(i, i + insertSize));
  495. i = i + insertSize;
  496. insertLength = insertLength - insertSize;
  497. }
  498. if (insertLength > 0) {
  499. choiceStockMapper.insertBatchByAfter(list.subList(i, i + insertLength));
  500. }
  501. // Thread thread=new Thread(new Runnable() {
  502. // @Override
  503. // public void run() {
  504. //// HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
  505. //// if(BuyAndSellUtils.isTransState()){
  506. // try {
  507. // new WebSocketController().sendSysMsgType("ZS",mapws);
  508. //// HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
  509. // }catch (Exception e){
  510. // log.error("插入redis错误" + e);
  511. // }
  512. //// }
  513. // }
  514. // });
  515. // thread.start();
  516. }
  517. private void insertList(List<StockRestPojo> list,Map<String,List<StockRestRedisVO>>mapws ,String type) { // List<StockRestRedisVO> stockRestRedisVOList
  518. int insertLength = list.size();
  519. int i = 0;
  520. int insertSize = 500;
  521. while (insertLength > insertSize) {
  522. if("now".equals(type)){
  523. choiceStockMapper.insertBatch(list.subList(i, i + insertSize));
  524. }else {
  525. choiceStockMapper.insertBatchByAfter(list.subList(i, i + insertSize));
  526. }
  527. i = i + insertSize;
  528. insertLength = insertLength - insertSize;
  529. }
  530. if (insertLength > 0) {
  531. if("now".equals(type)){
  532. choiceStockMapper.insertBatch(list.subList(i, i + insertLength));
  533. }else {
  534. choiceStockMapper.insertBatchByAfter(list.subList(i, i + insertLength));
  535. }
  536. }
  537. // Thread thread=new Thread(new Runnable() {
  538. // @Override
  539. // public void run() {
  540. //// HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
  541. //// if(BuyAndSellUtils.isTransState()){
  542. // try {
  543. // new WebSocketController().sendSysMsgType("ZS",mapws);
  544. //// HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
  545. // }catch (Exception e){
  546. // log.error("插入redis错误" + e);
  547. // }
  548. //// }
  549. // }
  550. // });
  551. // thread.start();
  552. }
  553. public StockRestPojo getRiskNow (StockRestPojo stockRestPojo,String key,String value){
  554. // KEY = code _ hy // BTC_BTC_hy
  555. String [] valueArr = value.split("&"); //tocode & mini & risk & multiples & status
  556. String [] keyArr = key.split("_"); //code _ hy // BTC_BTC_hy CODE to tocode
  557. StringBuffer tableName = new StringBuffer("data_rt_");
  558. tableName.append(valueArr[0].replace(".", "_").toLowerCase());
  559. tableName.append("_"+keyArr[0]);
  560. if("1".equals(valueArr[4])){ // 如果此时没有开启风控 那么不变化
  561. StockRestPojo stockRestPojoNew = new StockRestPojo();
  562. BeanUtils.copyProperties(stockRestPojo,stockRestPojoNew,StockRestPojo.class);
  563. stockRestPojoNew.setCode(valueArr[0]);
  564. stockRestPojoNew.setTableName(tableName.toString());
  565. return stockRestPojoNew;
  566. }else {
  567. String tocode = valueArr[0]; // 新币
  568. String mini = valueArr[1];// 最小波动
  569. String risk1 = valueArr[2].substring(0, 1); // 风险 +-*/110
  570. String multiples = valueArr[3]; //倍数
  571. String risk2 = BigdecimalUtil.getNew(mini,valueArr[2].substring(1, valueArr[2].length()),"*",4,2) ; // 风险 +-*/110
  572. StockRestPojo stockRestPojoNew = new StockRestPojo();
  573. BeanUtils.copyProperties(stockRestPojo,stockRestPojoNew,StockRestPojo.class);
  574. String nnow = BigdecimalUtil.getNew(stockRestPojo.getNow() + "", risk2, risk1, 4, 2, multiples);
  575. stockRestPojoNew.setTableName(tableName.toString()); // 表
  576. stockRestPojoNew.setCode(tocode);
  577. stockRestPojoNew.setNow(nnow); // 现价
  578. // 高
  579. stockRestPojoNew.setHigh(BigdecimalUtil.getNew(stockRestPojo.getHigh() + "", risk2, risk1, 4, 2, multiples));
  580. // 低
  581. stockRestPojoNew.setLow(BigdecimalUtil.getNew(stockRestPojo.getLow() + "", risk2, risk1, 4, 2, multiples));
  582. //开 取当天第一条 存缓存
  583. if(StringUtils.isBlank(RiskOptions.get("open_"+key))){ // 为空就存 当前的now价为开盘价
  584. RiskOptions.set("open_"+key,stockRestPojoNew.getNow()+"");
  585. }
  586. stockRestPojoNew.setOpen(RiskOptions.get("open_"+key));
  587. //前收
  588. if(StringUtils.isBlank(RiskOptions.get("preclose_"+key))){ // 为空就存 当前的计算当前的 收盘价 为收盘价 因为 除非是系统上线 不然不会在这出现为空的情况
  589. String preclose = (BigdecimalUtil.getNew(stockRestPojo.getPreclose() + "", risk2, risk1, 4, 2, multiples)) ;
  590. RiskOptions.set("preclose_"+key,preclose);
  591. }
  592. stockRestPojoNew.setPreclose(RiskOptions.get("preclose_"+key));
  593. stockRestPojoNew.setChange((new BigDecimal(stockRestPojoNew.getNow() + "").
  594. subtract(new BigDecimal(stockRestPojoNew.getPreclose() + ""))).setScale(4, 2)+"");
  595. stockRestPojoNew.setPctchange(((!"0".equals(stockRestPojoNew.getPreclose())) ?
  596. ((new BigDecimal(stockRestPojoNew.getChange() + "").divide(new BigDecimal(stockRestPojoNew.getPreclose() + ""), 4, 2))) : 0)+"");
  597. stockRestPojoNew.setBuyprice1(randomBuySell(stockRestPojoNew.getNow(), 0));
  598. stockRestPojoNew.setBuyprice2(randomBuySell(stockRestPojoNew.getBuyprice1(), 0));
  599. stockRestPojoNew.setBuyprice3(randomBuySell(stockRestPojoNew.getBuyprice2(), 0));
  600. stockRestPojoNew.setBuyprice4(randomBuySell(stockRestPojoNew.getBuyprice3(), 0));
  601. stockRestPojoNew.setBuyprice5(randomBuySell(stockRestPojoNew.getBuyprice4(), 0));
  602. stockRestPojoNew.setBuyvolume1(String.valueOf(new BigDecimal(randomInt())));
  603. stockRestPojoNew.setBuyvolume2(String.valueOf(new BigDecimal(randomInt())));
  604. stockRestPojoNew.setBuyvolume3(String.valueOf(new BigDecimal(randomInt())));
  605. stockRestPojoNew.setBuyvolume4(String.valueOf(new BigDecimal(randomInt())));
  606. stockRestPojoNew.setBuyvolume5(String.valueOf(new BigDecimal(randomInt())));
  607. stockRestPojoNew.setSellprice1(randomBuySell(stockRestPojoNew.getNow(), 1));
  608. stockRestPojoNew.setSellprice2(randomBuySell(stockRestPojoNew.getSellprice1(), 1));
  609. stockRestPojoNew.setSellprice3(randomBuySell(stockRestPojoNew.getSellprice2(), 1));
  610. stockRestPojoNew.setSellprice4(randomBuySell(stockRestPojoNew.getSellprice3(), 1));
  611. stockRestPojoNew.setSellprice5(randomBuySell(stockRestPojoNew.getSellprice4(), 1));
  612. stockRestPojoNew.setSellvolume1(String.valueOf(new BigDecimal(randomInt())));
  613. stockRestPojoNew.setSellvolume2(String.valueOf(new BigDecimal(randomInt())));
  614. stockRestPojoNew.setSellvolume3(String.valueOf(new BigDecimal(randomInt())));
  615. stockRestPojoNew.setSellvolume4(String.valueOf(new BigDecimal(randomInt())));
  616. stockRestPojoNew.setSellvolume5(String.valueOf(new BigDecimal(randomInt())));
  617. return stockRestPojoNew;
  618. }
  619. }
  620. private String randomBuySell(String now, int type) {
  621. // 买
  622. if (0 == type) {
  623. return new BigDecimal(now).subtract(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
  624. } else {
  625. return new BigDecimal(now).add(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
  626. }
  627. }
  628. private int randomInt() {
  629. int max = 10000;
  630. int min = 5000;
  631. int ran = (int) (Math.random() * (max - min) + min);
  632. return ran;
  633. }
  634. }