ChoiceTempIndexServiceImpl.java 41 KB

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