| | |
| | | package com.nq.utils.task.stock; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.json.JSONArray; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.fasterxml.jackson.databind.JsonNode; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.google.gson.Gson; |
| | | import com.nq.common.ServerResponse; |
| | | import com.nq.dao.StockMapper; |
| | | import com.nq.dao.UserMapper; |
| | | import com.nq.dao.UserPendingorderMapper; |
| | | import com.nq.dao.UserPositionMapper; |
| | | import com.nq.enums.EConfigKey; |
| | | import com.nq.enums.EStockType; |
| | | import com.nq.pojo.DataStockBean; |
| | | import com.nq.pojo.ReponseBase; |
| | | import com.nq.pojo.Stock; |
| | | import com.nq.pojo.UserPosition; |
| | | import com.nq.enums.EUserAssets; |
| | | import com.nq.pojo.*; |
| | | import com.nq.pojo.reponse.kResponse; |
| | | import com.nq.service.IMandatoryLiquidationService; |
| | | import com.nq.service.IStockService; |
| | | import com.nq.service.IUserPositionService; |
| | | import com.nq.service.*; |
| | | import com.nq.service.impl.StockServiceImpl; |
| | | import com.nq.service.impl.UserServiceImpl; |
| | | import com.nq.utils.ConverterUtil; |
| | | import com.nq.utils.KeyUtils; |
| | | 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 com.nq.utils.stock.GeneratePosition; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | |
| | | import java.io.BufferedReader; |
| | | import java.io.InputStreamReader; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.net.URLEncoder; |
| | |
| | | @Autowired |
| | | UserPositionMapper userPositionMapper; |
| | | |
| | | private final Lock stockConstraintLock = new ReentrantLock(); |
| | | |
| | | |
| | | @Autowired |
| | | private UserPendingorderService userPendingorderService; |
| | | @Autowired |
| | | IMandatoryLiquidationService mandatoryLiquidationService; |
| | | @Autowired |
| | | IUserAssetsServices iUserAssetsServices; |
| | | @Autowired |
| | | IStockConfigServices iStockConfigServices; |
| | | |
| | | @Autowired |
| | | IPriceServices priceServices; |
| | | @Autowired |
| | | UserMapper userMapper; |
| | | |
| | | |
| | | |
| | | private static final Logger log = LoggerFactory.getLogger(StockTask.class); |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 挂单 |
| | | */ |
| | | @Scheduled(cron = "0/10 * * * * ?") |
| | | public void pendingOrder() { |
| | | ReentrantLock lock = new ReentrantLock(); |
| | | if (lock.tryLock()) { // 尝试获取锁 |
| | | try { |
| | | pending(); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | log.error("挂单定时任务报错:",e.getMessage()); |
| | | } |
| | | finally { |
| | | lock.unlock(); // 释放锁 |
| | | } |
| | | } else { |
| | | // 如果锁不可用,可以选择不执行或打印日志等 |
| | | System.out.println("挂单定时任务 任务正在执行,跳过本次执行。"); |
| | | } |
| | | } |
| | | |
| | | public void pending(){ |
| | | List<UserPendingorder> list = userPendingorderService.list(new LambdaQueryWrapper<>(UserPendingorder.class).eq(UserPendingorder::getPositionType, 1)); |
| | | if(CollectionUtil.isNotEmpty(list)){ |
| | | list.forEach(f->{ |
| | | if(f.getStockGid().equals("ST")){ |
| | | //获取当前价格 |
| | | //股票类型 现价 数据源的处理 |
| | | BigDecimal nowPrice = priceServices.getNowPrice(f.getStockCode()); |
| | | if(f.getBuyOrderPrice().compareTo(nowPrice) <= 0){ |
| | | stockTransferPositions(f); |
| | | } |
| | | }else{ |
| | | String price = RedisShardedPoolUtils.get(RedisKeyConstant.getRedisKey(f.getStockName())); |
| | | if(f.getBuyOrderPrice().compareTo(new BigDecimal(price)) <= 0){ |
| | | hjTransferPositions(f); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | public void hjTransferPositions(UserPendingorder userPendingorder){ |
| | | User user = userMapper.selectById(userPendingorder.getUserId()); |
| | | BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ; |
| | | BigDecimal orderFree = siteSettingBuyFee.multiply(userPendingorder.getOrderTotalPrice()); |
| | | UserPosition userPosition = ConverterUtil.convert(userPendingorder, UserPosition.class); |
| | | userPosition.setPositionType(user.getAccountType()); |
| | | userPosition.setId(null); |
| | | userPendingorder.setPositionType(0); |
| | | userPendingorderService.updateById(userPendingorder); |
| | | userPositionMapper.insert(userPosition); |
| | | iUserAssetsServices.availablebalanceChange("USDT", user.getId(), EUserAssets.HANDLING_CHARGE, orderFree, "", ""); |
| | | } |
| | | |
| | | public void stockTransferPositions(UserPendingorder userPendingorder){ |
| | | // 手续费率 |
| | | BigDecimal siteSettingBuyFee = new BigDecimal(iStockConfigServices.queryByKey(EConfigKey.BUY_HANDLING_CHARGE.getCode()).getCValue()) ; |
| | | BigDecimal orderFree = siteSettingBuyFee.multiply(userPendingorder.getOrderTotalPrice()); |
| | | User user = userMapper.selectById(userPendingorder.getUserId()); |
| | | UserPosition userPosition = ConverterUtil.convert(userPendingorder, UserPosition.class); |
| | | userPosition.setPositionType(user.getAccountType()); |
| | | userPosition.setId(null); |
| | | userPendingorder.setPositionType(0); |
| | | userPendingorderService.updateById(userPendingorder); |
| | | userPositionMapper.insert(userPosition); |
| | | //挂单成功扣除手续费 |
| | | iUserAssetsServices.availablebalanceChange("ST", user.getId(), EUserAssets.HANDLING_CHARGE, orderFree, "", ""); |
| | | } |
| | | |
| | | //最新价格url |
| | | private static final String PRICE_URL = "https://quote.alltick.io/quote-b-api/trade-tick?token=794ea65dc03c5af582cddec476fbb0d7-c-app&query="; |
| | | //k线url |
| | |
| | | if (lock.tryLock()) { // 尝试获取锁 |
| | | try { |
| | | gold(); |
| | | Thread.sleep(1000); |
| | | Thread.sleep(2000); |
| | | getKDate(); |
| | | } finally { |
| | | lock.unlock(); // 释放锁 |
| | |
| | | RedisShardedPoolUtils.set("k_gold_"+entry.getKey(), gold); |
| | | } |
| | | |
| | | Thread.sleep(1000); |
| | | Thread.sleep(2000); |
| | | String c = getResp(K_URL+getKQueryData(entry.getValue(),"USOIL")); |
| | | if(StringUtils.isNotEmpty(c)){ |
| | | if(entry.getKey().equals("d")) { |
| | |
| | | } |
| | | RedisShardedPoolUtils.set("k_crude_oil_"+entry.getKey(), c); |
| | | } |
| | | Thread.sleep(1000); |
| | | Thread.sleep(2000); |
| | | } |
| | | } |
| | | |
| | |
| | | kData.setVo(item.getTurnover()); |
| | | kDataList.add(kData); |
| | | } |
| | | DecimalFormat decimalFormat = new DecimalFormat("#.00"); |
| | | double oneC = Double.valueOf(kDataList.get(kDataList.size() - 1).getC()); |
| | | double twoC = Double.valueOf(kDataList.get(kDataList.size() - 2).getC()); |
| | | String h = String.valueOf(decimalFormat.format(((oneC - twoC) / oneC * 100))); |
| | | RedisShardedPoolUtils.set(key+"_H",h); |
| | | RedisShardedPoolUtils.set(key+"_H",h); |
| | | String h = String.valueOf(((oneC - twoC) / oneC * 100)); |
| | | BigDecimal bd = new BigDecimal(h); |
| | | bd = bd.setScale(2, RoundingMode.DOWN); // RoundingMode.DOWN 表示截断而非四舍五入 |
| | | RedisShardedPoolUtils.set(key+"_H",bd.toString()); |
| | | RedisShardedPoolUtils.set(key+"_H",bd.toString()); |
| | | } |
| | | |
| | | public String getResp(String url){ |