@Override public Stock getStockByCode(String code) { List<StockItem> items = stockItemRepository.getStocksByCode(code); if (items.isEmpty()) return null; Stock stock = new Stock(items); StockStatistics stat = stockStatisticsRepository.findByCode(code); if (stat != null) { stock.setIncreaseTotal(stat.getIncreaseTotal()); stock.setHighestPrice(stat.getHighestPrice()); stock.setAverageGoldDays(stat.getAverageGoldDays()); } return stock; }
@Override public StockStatics getLastYearStockStatics() { List<String> codes = stockItemRepository.getCodes(); List<Stock> stocks = new ArrayList<>(codes.size()); Timestamp from = TimeUtil.oneYearAgo(); Timestamp to = TimeUtil.now(); for (String code : codes) { Stock stock = getStockByCodeAndTime(code, from, to); if (stock != null && !stock.isStop()) { stocks.add(stock); } } StockStatics result = new StockStatics(stocks); return result; }
@Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { logger.info("Update Stock Items Task Begin"); try { init(context); List<String> codes = stockService.getAllCodes(); for (String code : codes) { String url = getUrl(code); ClientRequest request = new ClientRequest(url); ClientResponse<String> response; try { response = request.get(String.class); BufferedReader br = new BufferedReader( new InputStreamReader(new ByteArrayInputStream(response.getEntity().getBytes()))); String output = br.readLine(); StockItem item = toDomain(output); item.setCode(code); StockItem last = stockService.getLatestStockItemByCode(code); if (last.getLogDate().equals(item.getLogDate())) continue; if (last.isStop()) { last.updateItem(item, MACD_START, MACD_END); stockService.save(last); } else { last.setLast(false); List<StockItem> items = new ArrayList<>(); items.add(last); items.add(item); Stock stock = new Stock(items); stock.calculateMacd(MACD_START, MACD_END, false); if (item.isStop()) { item.setMacdDea(last.getMacdDea()); item.setMacdDiff(last.getMacdDiff()); item.setMacd(last.getMacd()); item.setEma12(last.getEma12()); item.setEma26(last.getEma26()); } stockService.saveAll(items); } } catch (Exception e) { System.out.printf("Code :\"%s\" Not Updated%n", code); e.printStackTrace(); continue; } } for (String code : codes) { List<StockItem> items = stockService.getLast30Stock(code); Stock stock = new Stock(items); stock.processAverage(); stockService.saveAll(items); } } catch (Exception ex) { logger.error("Init error", ex); } logger.info("Update Stock Item Task Finished!"); }