| | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.fasterxml.jackson.databind.JsonNode; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.google.gson.Gson; |
| | | import com.nq.dao.StockMapper; |
| | | import com.nq.dao.UserPositionMapper; |
| | |
| | | import com.nq.service.IStockService; |
| | | import com.nq.service.IUserPositionService; |
| | | import com.nq.utils.http.HttpClientRequest; |
| | | import com.nq.utils.redis.RedisKeyConstant; |
| | | import com.nq.utils.redis.RedisKeyUtil; |
| | | import com.nq.utils.redis.RedisShardedPoolUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | |
| | | /** |
| | | * 同步系统所需要的股票 |
| | | */ |
| | | @Scheduled(cron = "0 0/10 * * * ?") |
| | | @Scheduled(cron = "0 0/1 * * * ?") |
| | | public void syncINStockData() { |
| | | if (syncINStockData.get()) { // 判断任务是否在处理中 |
| | | return; |
| | |
| | | if (syncINStockDataLock.tryLock()) { |
| | | try { |
| | | syncINStockData.set(true); // 设置处理中标识为true |
| | | loadAllStock(EStockType.IN); |
| | | loadAllStock(EStockType.ST); |
| | | } finally { |
| | | syncINStockDataLock.unlock(); |
| | | syncINStockData.set(false); // 设置处理中标识为false |
| | |
| | | } |
| | | } |
| | | |
| | | //黄金 |
| | | private static final String gold_API_URL = "http://139.196.211.109/exchange_curr.action?username=Qq112233&password=3ce25a66d5b3a8cd661024fea6c79388&id=AUUSDO"; |
| | | |
| | | //原油 |
| | | private static final String crude_oil_API_URL = "http://47.112.169.122/fOption_curr.action?username=Qq112233&password=3ce25a66d5b3a8cd661024fea6c79388&id=@CL0W"; |
| | | |
| | | @Scheduled(cron = "0/10 * * * * ?") // 每6秒执行一次 |
| | | public void gold() { |
| | | try { |
| | | // 使用RestTemplate发起HTTP请求 |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | String response = restTemplate.getForObject(gold_API_URL, String.class); |
| | | |
| | | // 解析返回的CSV格式数据,去除可能存在的换行符 |
| | | if (response != null) { |
| | | // 清除换行符并按逗号分割数据 |
| | | String[] parts = response.trim().split(","); |
| | | String price = parts[2].trim(); // "3348.4" |
| | | // 转换价格为Double类型 |
| | | // 保存价格到Redis |
| | | RedisShardedPoolUtils.set(RedisKeyConstant.gold, String.valueOf(price)); |
| | | log.info("黄金定时任务------成功"); |
| | | } else { |
| | | log.info("黄金定时任务------没有接收到数据"); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("黄金定时任务------请求或数据解析失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | @Scheduled(cron = "0/15 * * * * ?") // 每6秒执行一次 |
| | | public void crudeOil() { |
| | | try { |
| | | // 使用RestTemplate发起HTTP请求 |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | String response = restTemplate.getForObject(crude_oil_API_URL, String.class); |
| | | |
| | | // 解析返回的CSV格式数据,去除可能存在的换行符 |
| | | if (response != null) { |
| | | // 清除换行符并按逗号分割数据 |
| | | String[] parts = response.trim().split(","); |
| | | String price = parts[2].trim(); // "3348.4" |
| | | // 转换价格为Double类型 |
| | | // 保存价格到Redis |
| | | RedisShardedPoolUtils.set(RedisKeyConstant.crude_oil, String.valueOf(price)); |
| | | log.info("原油定时任务------没有接收到数据"); |
| | | } else { |
| | | log.info("原油定时任务------没有接收到数据"); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("原油定时任务------请求或数据解析失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | //黄金 |
| | | private static final String k_gold_API_URL = "http://139.196.211.109/ldMetal_k.action?username=Qq112233&password=3ce25a66d5b3a8cd661024fea6c79388&id=AUUSDO&num=-100&period="; |
| | | |
| | | //原油 |
| | | private static final String k_crude_oil_API_URL = "http://47.112.169.122/fOption_k.action?username=Qq112233&password=3ce25a66d5b3a8cd661024fea6c79388&id=@CL0W&num=-100&period="; |
| | | |
| | | @Scheduled(cron = "0/6 * * * * ?") // 每6秒执行一次 |
| | | public void getKDate() throws InterruptedException { |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | String[] arr = {"d", "w", "m", "1", "5", "30"}; |
| | | for (String str : arr) { |
| | | String g = restTemplate.getForObject(k_gold_API_URL+str, String.class); |
| | | RedisShardedPoolUtils.set("k_gold_"+str, g); |
| | | Thread.sleep(6000); |
| | | String c = restTemplate.getForObject(k_crude_oil_API_URL+str, String.class); |
| | | RedisShardedPoolUtils.set("k_crude_oil_"+str, c); |
| | | Thread.sleep(6000); |
| | | } |
| | | } |
| | | /** |
| | | * 同步美国股票 |
| | | */ |
| | | // @Scheduled(cron = "0 0/30 * * * ?") |
| | | public void loadStockCompanies() { |
| | | loadAllCompanies(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 加载公司信息 |
| | | */ |
| | | public void loadAllCompanies() { |
| | | List<Stock> list = stockMapper.findStockList(); |
| | | for (int i = 0; i < list.size(); i++) { |
| | | Stock stock = list.get(i); |
| | | EStockType eStockType = EStockType.getEStockTypeByCode(stock.getStockType()); |
| | | String result = HttpClientRequest.doGet(eStockType.stockUrl + "companies?pid=+" + stock.getStockCode() + "+country_id=" + eStockType.getContryId() + "&size=1&page=1&key=" + eStockType.stockKey); |
| | | try { |
| | | JSONObject jsonObject = JSONObject.parseObject(result); |
| | | JSONObject companiesInfo = jsonObject.getJSONArray("data").getJSONObject(0); |
| | | RedisKeyUtil.setCacheCompanies(stock, new Gson().toJson(companiesInfo)); |
| | | } catch (Exception e) { |
| | | log.info(""); |
| | | |
| | | } |
| | | } |
| | | |
| | | } |
| | | // public void loadStockCompanies() { |
| | | // loadAllCompanies(); |
| | | // } |
| | | // |
| | | // |
| | | // /** |
| | | // * 加载公司信息 |
| | | // */ |
| | | // public void loadAllCompanies() { |
| | | // List<Stock> list = stockMapper.findStockList(); |
| | | // for (int i = 0; i < list.size(); i++) { |
| | | // Stock stock = list.get(i); |
| | | // EStockType eStockType = EStockType.getEStockTypeByCode(stock.getStockType()); |
| | | // String result = HttpClientRequest.doGet(eStockType.stockUrl + "companies?pid=+" + stock.getStockCode() + "+country_id=" + eStockType.getContryId() + "&size=1&page=1&key=" + eStockType.stockKey); |
| | | // try { |
| | | // JSONObject jsonObject = JSONObject.parseObject(result); |
| | | // JSONObject companiesInfo = jsonObject.getJSONArray("data").getJSONObject(0); |
| | | // RedisKeyUtil.setCacheCompanies(stock, new Gson().toJson(companiesInfo)); |
| | | // } catch (Exception e) { |
| | | // log.info(""); |
| | | // |
| | | // } |
| | | // } |
| | | // |
| | | // } |
| | | |
| | | /** |
| | | * 加载所有股票数据 |
| | |
| | | * 强制平仓 |
| | | */ |
| | | // @Scheduled(cron = "0/1 * * * * ?") |
| | | public void stockConstraint() { |
| | | if (stockConstraint.get()) { // 判断任务是否在处理中 |
| | | return; |
| | | } |
| | | if (stockConstraintLock.tryLock()) { |
| | | try { |
| | | stockConstraint.set(true); // 设置处理中标识为true |
| | | List<UserPosition> userPositions = userPositionMapper.selectList(new LambdaQueryWrapper<UserPosition>().isNull(UserPosition::getSellOrderId)); |
| | | if (CollectionUtils.isNotEmpty(userPositions)) { |
| | | userPositionService.stockConstraint(userPositions); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("强制平仓任务错误:" + e.getMessage()); |
| | | } finally { |
| | | stockConstraintLock.unlock(); |
| | | stockConstraint.set(false); // 设置处理中标识为false |
| | | } |
| | | } else { |
| | | log.info("强制平仓任务--------->上次任务还未执行完成,本次任务忽略"); |
| | | } |
| | | } |
| | | // public void stockConstraint() { |
| | | // if (stockConstraint.get()) { // 判断任务是否在处理中 |
| | | // return; |
| | | // } |
| | | // if (stockConstraintLock.tryLock()) { |
| | | // try { |
| | | // stockConstraint.set(true); // 设置处理中标识为true |
| | | // List<UserPosition> userPositions = userPositionMapper.selectList(new LambdaQueryWrapper<UserPosition>().isNull(UserPosition::getSellOrderId)); |
| | | // if (CollectionUtils.isNotEmpty(userPositions)) { |
| | | // userPositionService.stockConstraint(userPositions); |
| | | // } |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // log.error("强制平仓任务错误:" + e.getMessage()); |
| | | // } finally { |
| | | // stockConstraintLock.unlock(); |
| | | // stockConstraint.set(false); // 设置处理中标识为false |
| | | // } |
| | | // } else { |
| | | // log.info("强制平仓任务--------->上次任务还未执行完成,本次任务忽略"); |
| | | // } |
| | | // } |
| | | } |