You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.0 KiB
Java

package com.ibeetl.jlw.service;
import cn.hutool.core.util.IdUtil;
import com.ibeetl.jlw.dao.HsValuesDao;
import com.ibeetl.jlw.entity.HsValues;
import com.ibeetl.jlw.util.HttpJsonRequest;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
@Service
@Transactional
@Slf4j
public class HsValuesService {
@Autowired
private HsValuesDao hsValuesDao;
public HsValues findByCreateTime(String createTime) {
return hsValuesDao.findByCreateTime(createTime);
}
// 每天早上9点执行一次任务
@Scheduled(cron = "0 */2 * * * *")
public void getHsValues() throws JSONException, ParseException {
log.info("------------------------------------开始执行沪深300数据导入-----------------------------");
LocalDate startTime = LocalDate.now().minusDays(5);
LocalDate endTime = LocalDate.now().plusDays(1);
String urlTwo = "https://api.myquant.cn:9001/ds-history-rpcgw/v3/data-history/benchmark-return"
+ "?symbol=SHSE.000300&frequency=1d&startTime=" + startTime.toString()
+ "&endTime=" + endTime.toString() + "&adjust=0";
String authorizationHeader = "bearer ee2c685459aa218ded58d0d7ad2ef2faf6f0d0d4";
String jsonResponse = HttpJsonRequest.sendGetRequest(urlTwo, authorizationHeader);
JSONArray dataArray = HttpJsonRequest.extractDataArray(jsonResponse);
//循环dataArray处理数据
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SimpleDateFormat stringDateFormat = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < dataArray.length(); i++) {
JSONObject jsonObject = dataArray.getJSONObject(i);
String createdAt = jsonObject.getString("created_at");
Date createdDate = dateFormat.parse(createdAt);
String dateString = stringDateFormat.format(createdDate);
if (findByTimeStr(dateString) != null)
continue;
HsValues hsValues = new HsValues();
if (jsonObject.has("ratio")) {
hsValues.setHsValue(Double.valueOf(jsonObject.getString("ratio")));
} else {
hsValues.setHsValue(0.0);
}
hsValues.setCreateTime(createdDate);
hsValues.setCreateTimeStr(dateString);
hsValuesDao.insert(hsValues);
}
}
public HsValues findByTimeStr(String timeStr) {
return hsValuesDao.findByTimeStr(timeStr);
}
public List<HsValues> findAll() {
return hsValuesDao.all();
}
}