ChoiceBondServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package com.zx.dataservice.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.zx.dataservice.controller.WebSocketController;
  4. import com.zx.dataservice.mapper2.ChoiceBondMapper;
  5. import com.zx.dataservice.pojo.StockRestPojo;
  6. import com.zx.dataservice.service.ChoiceBondService;
  7. import com.zx.dataservice.utils.*;
  8. import com.zx.dataservice.vo.StockRestRedisVO;
  9. import com.zx.dataservice.vo.StockRestVO;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.BeanUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import java.io.File;
  17. import java.util.*;
  18. @Service
  19. public class ChoiceBondServiceImpl implements ChoiceBondService {
  20. private static final Logger log = LoggerFactory.getLogger(ChoiceBondServiceImpl.class);
  21. private static final String sourceFileUrl = "/www/wwwroot/csqdata/bond";// 盘中
  22. private static final String goalFileUrl = "/www/wwwroot/csqdata/bond/backup"; // 盘中 备份
  23. private static final String url = "http://qwer16888.vip/api/stock/updateBatchStockInfo.do"; //
  24. private static final String urlRedis = "http://qwer16888.vip/init/redis/updateInfo.do";
  25. private static final String sourceAfterFileUrl = "/www/wwwroot/csqdata/bond/after"; // 盘后
  26. private static final String goalAfterFileUrl = "/www/wwwroot/csqdata/bond/after/backup"; // 盘后备份
  27. @Autowired
  28. private ChoiceBondMapper choiceBondMapper;
  29. @Override
  30. public void analysisBondRestData(String type) {
  31. // 1.获取文件夹文件 按照时间顺序拿到一个
  32. List<File> files = FileBondUtil.getFile("now".equals(type)?sourceFileUrl:sourceAfterFileUrl);
  33. if (null == files || 0 == files.size()) {
  34. return;
  35. }
  36. List<StockRestPojo> stockRestPojoList = new ArrayList<>();
  37. List<StockRestVO> stockRestVOList = new ArrayList<>();
  38. List<StockRestRedisVO> stockRestRedisVOList = new ArrayList<>();
  39. StockRestPojo stockRestPojo;
  40. StockRestVO stockRestVO;
  41. StockRestRedisVO stockRestRedisVO;
  42. for (File file : files) {
  43. // 2.移动文件去另外一个文件夹
  44. String str = "";
  45. if("now".equals(type)){
  46. FileBondUtil.moveFile(sourceFileUrl + File.separator + file.getName(), goalFileUrl);
  47. // 3.读取文件内容
  48. str = FileBondUtil.readFileContent(goalFileUrl + File.separator + file.getName());
  49. }else if("after".equals(type)){
  50. FileBondUtil.moveFile(sourceAfterFileUrl + File.separator + file.getName(), goalAfterFileUrl);
  51. // 3.读取文件内容
  52. str = FileBondUtil.readFileContent(goalAfterFileUrl + File.separator + file.getName());
  53. }
  54. // 4.并解析
  55. if(StringUtils.isEmpty(str)){
  56. continue;
  57. }
  58. Map<String, List<String>> map = new HashMap<String, List<String>>();
  59. map = JSON.parseObject(str, HashMap.class);
  60. str = null;
  61. for (Map.Entry<String, List<String>> entry : map.entrySet()) {
  62. try {
  63. stockRestPojo = new StockRestPojo();
  64. stockRestPojo.setDate(String.valueOf(entry.getValue().get(0)));
  65. if(String.valueOf(entry.getValue().get(1)).equals("0")){
  66. continue;
  67. }
  68. stockRestPojo.setTime("now".equals(type)?String.valueOf(entry.getValue().get(1)):"150000");
  69. stockRestPojo.setNow(String.valueOf(entry.getValue().get(2)));
  70. stockRestPojo.setHigh(String.valueOf(entry.getValue().get(3)));
  71. stockRestPojo.setLow(String.valueOf(entry.getValue().get(4)));
  72. stockRestPojo.setOpen(String.valueOf(entry.getValue().get(5)));
  73. stockRestPojo.setPreclose(String.valueOf(entry.getValue().get(6)));
  74. stockRestPojo.setRoundlot(String.valueOf(entry.getValue().get(7)));
  75. stockRestPojo.setChange(String.valueOf(entry.getValue().get(8)));
  76. stockRestPojo.setPctchange(String.valueOf(entry.getValue().get(9)));
  77. stockRestPojo.setVolume(String.valueOf(entry.getValue().get(10)));
  78. stockRestPojo.setAmount(String.valueOf(entry.getValue().get(11)));
  79. stockRestPojo.setVolumeratio("");
  80. stockRestPojo.setCommissionratio("");
  81. stockRestPojo.setCommissiondiff("");
  82. stockRestPojo.setTradestatus("");
  83. stockRestPojo.setOutvolume("");
  84. stockRestPojo.setInvolume("");
  85. stockRestPojo.setHighlimit("");
  86. stockRestPojo.setLowlimit("");
  87. stockRestPojo.setSpeed("");
  88. stockRestPojo.setAverageprice("");
  89. stockRestPojo.setBuyprice1(String.valueOf(entry.getValue().get(12)));
  90. stockRestPojo.setBuyprice2(String.valueOf(entry.getValue().get(13)));
  91. stockRestPojo.setBuyprice3(String.valueOf(entry.getValue().get(14)));
  92. stockRestPojo.setBuyprice4(String.valueOf(entry.getValue().get(15)));
  93. stockRestPojo.setBuyprice5(String.valueOf(entry.getValue().get(16)));
  94. stockRestPojo.setBuyvolume1(String.valueOf(entry.getValue().get(17)));
  95. stockRestPojo.setBuyvolume2(String.valueOf(entry.getValue().get(18)));
  96. stockRestPojo.setBuyvolume3(String.valueOf(entry.getValue().get(19)));
  97. stockRestPojo.setBuyvolume4(String.valueOf(entry.getValue().get(20)));
  98. stockRestPojo.setBuyvolume5(String.valueOf(entry.getValue().get(21)));
  99. stockRestPojo.setSellprice1(String.valueOf(entry.getValue().get(22)));
  100. stockRestPojo.setSellprice2(String.valueOf(entry.getValue().get(23)));
  101. stockRestPojo.setSellprice3(String.valueOf(entry.getValue().get(24)));
  102. stockRestPojo.setSellprice4(String.valueOf(entry.getValue().get(25)));
  103. stockRestPojo.setSellprice5(String.valueOf(entry.getValue().get(26)));
  104. stockRestPojo.setSellvolume1(String.valueOf(entry.getValue().get(27)));
  105. stockRestPojo.setSellvolume2(String.valueOf(entry.getValue().get(28)));
  106. stockRestPojo.setSellvolume3(String.valueOf(entry.getValue().get(29)));
  107. stockRestPojo.setSellvolume4(String.valueOf(entry.getValue().get(30)));
  108. stockRestPojo.setSellvolume5(String.valueOf(entry.getValue().get(31)));
  109. stockRestPojo.setClosedtime("");
  110. stockRestPojo.setClosedvolume("");
  111. stockRestPojo.setClosedamount("");
  112. StringBuffer dateStr = new StringBuffer(stockRestPojo.getDate());
  113. if (StringUtils.isEmpty(stockRestPojo.getTime())) {
  114. dateStr = dateStr.append(000000);
  115. } else {
  116. if (5 == stockRestPojo.getTime().length()) {
  117. // 长度为5表示 小时前面需要补0
  118. dateStr = dateStr.append(0).append(stockRestPojo.getTime());
  119. } else {
  120. dateStr = dateStr.append(stockRestPojo.getTime());
  121. }
  122. }
  123. Date date = TimeUtil.strToDate(dateStr.toString());
  124. // 毫秒转成秒
  125. stockRestPojo.setRealTime(date.getTime() / 1000);
  126. Calendar calendar = Calendar.getInstance();
  127. calendar.setTime(date);
  128. int min = calendar.get(Calendar.MINUTE); // 获取分钟
  129. int sec = calendar.get(Calendar.SECOND); // 获取秒数
  130. // 1分钟数据
  131. if (sec != 0) {
  132. stockRestPojo.setDateOne(stockRestPojo.getRealTime() + 60 - sec);
  133. } else {
  134. stockRestPojo.setDateOne(stockRestPojo.getRealTime());
  135. }
  136. // 5分钟数据
  137. if (sec != 0 || min % 5 != 0) {
  138. stockRestPojo.setDateFive(stockRestPojo.getRealTime() + (5 * 60) - sec - (min % 5 * 60));
  139. } else {
  140. stockRestPojo.setDateFive(stockRestPojo.getRealTime());
  141. }
  142. // 15分钟数据
  143. if (sec != 0 || min % 15 != 0) {
  144. stockRestPojo.setDateFifteen(stockRestPojo.getRealTime() + (15 * 60) - sec - (min % 15 * 60));
  145. } else {
  146. stockRestPojo.setDateFifteen(stockRestPojo.getRealTime());
  147. }
  148. // 30分钟数据
  149. if (sec != 0 || min % 30 != 0) {
  150. stockRestPojo.setDateThirty(stockRestPojo.getRealTime() + (30 * 60) - sec - (min % 30 * 60));
  151. } else {
  152. stockRestPojo.setDateThirty(stockRestPojo.getRealTime());
  153. }
  154. // 60分钟数据
  155. if (sec != 0 || min % 60 != 0) {
  156. stockRestPojo.setDateSixty(stockRestPojo.getRealTime() + (60 * 60) - sec - (min % 60 * 60));
  157. } else {
  158. stockRestPojo.setDateSixty(stockRestPojo.getRealTime());
  159. }
  160. StringBuffer tableName = new StringBuffer("data_rt_");
  161. tableName.append(entry.getKey().replace(".", "_").toLowerCase());
  162. stockRestPojo.setCode(entry.getKey().toUpperCase());
  163. stockRestPojo.setTableName(tableName.toString());
  164. stockRestPojoList.add(stockRestPojo);
  165. stockRestVO = new StockRestVO();
  166. BeanUtils.copyProperties(stockRestPojo, stockRestVO);
  167. stockRestVOList.add(stockRestVO);
  168. stockRestRedisVO = new StockRestRedisVO();
  169. stockRestRedisVO.setCode(entry.getKey().toUpperCase());
  170. stockRestRedisVO.setNowPrice(stockRestPojo.getNow());
  171. stockRestRedisVO.setChange(stockRestPojo.getChange());
  172. stockRestRedisVO.setHcrate(stockRestPojo.getPctchange());
  173. stockRestRedisVO.setHighlimit(stockRestPojo.getHighlimit());
  174. stockRestRedisVO.setLowlimit(stockRestPojo.getLowlimit());
  175. stockRestRedisVOList.add(stockRestRedisVO);
  176. }catch (Exception e){
  177. log.error("错误数据导致失败" + e);
  178. }
  179. }
  180. map = null;
  181. }
  182. // 5.入库
  183. insertList(stockRestPojoList, stockRestVOList, stockRestRedisVOList ,type );
  184. stockRestPojoList = null;
  185. }
  186. private void insertList(List<StockRestPojo> list, List<StockRestVO> stockRestVOList, List<StockRestRedisVO> stockRestRedisVOList,String type) {
  187. int insertLength = list.size();
  188. int i = 0;
  189. int insertSize = 500;
  190. while (insertLength > insertSize) {
  191. if("now".equals(type)){
  192. choiceBondMapper.insertBatch(list.subList(i, i + insertSize));
  193. }else {
  194. choiceBondMapper.insertBatchByAfter(list.subList(i, i + insertSize));
  195. }
  196. List<StockRestVO> subList = stockRestVOList.subList(i, i + insertSize);
  197. List<StockRestRedisVO> subRedisList = stockRestRedisVOList.subList(i, i + insertSize);
  198. i = i + insertSize;
  199. insertLength = insertLength - insertSize;
  200. Thread thread=new Thread(new Runnable() {
  201. @Override
  202. public void run() {
  203. // HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
  204. // if(BuyAndSellUtils.isTransState()){
  205. try{
  206. final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
  207. for (int j = 0; j < webArr.length; j++) {
  208. new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
  209. }
  210. // HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
  211. }catch (Exception e){
  212. log.error("插入redis错误" + e);
  213. }
  214. // }
  215. }
  216. });
  217. thread.start();
  218. }
  219. if (insertLength > 0) {
  220. if("now".equals(type)){
  221. choiceBondMapper.insertBatch(list.subList(i, i + insertLength));
  222. }else {
  223. choiceBondMapper.insertBatchByAfter(list.subList(i, i + insertLength));
  224. }
  225. List<StockRestVO> subList = stockRestVOList.subList(i, i + insertLength);
  226. List<StockRestRedisVO> subRedisList = stockRestRedisVOList.subList(i, i + insertLength);
  227. Thread thread=new Thread(new Runnable() {
  228. @Override
  229. public void run() {
  230. // HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
  231. // if(BuyAndSellUtils.isTransState()){
  232. try {
  233. final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
  234. for (int j = 0; j < webArr.length; j++) {
  235. new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
  236. }
  237. // HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
  238. }catch (Exception e){
  239. log.error("插入redis错误" + e);
  240. }
  241. // }
  242. }
  243. });
  244. thread.start();
  245. }
  246. }
  247. }