From 5ce072de78ae263b52dc0e2be5c7391b23c093cf Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Sun, 27 Apr 2025 18:29:22 +0800
Subject: [PATCH] 1

---
 src/main/java/com/nq/utils/task/stock/StockTask.java |  124 +++++++++++++++++++++++++++++++++++-----
 1 files changed, 107 insertions(+), 17 deletions(-)

diff --git a/src/main/java/com/nq/utils/task/stock/StockTask.java b/src/main/java/com/nq/utils/task/stock/StockTask.java
index d4a1b6b..08db5ed 100644
--- a/src/main/java/com/nq/utils/task/stock/StockTask.java
+++ b/src/main/java/com/nq/utils/task/stock/StockTask.java
@@ -1,26 +1,32 @@
 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;
@@ -32,6 +38,8 @@
 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;
@@ -55,11 +63,20 @@
     @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);
@@ -88,6 +105,78 @@
             }
         }
     }
+
+
+    /**
+     * 挂单
+     */
+    @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
@@ -140,7 +229,7 @@
         if (lock.tryLock()) { // 尝试获取锁
             try {
                 gold();
-                Thread.sleep(1000);
+                Thread.sleep(2000);
                 getKDate();
             } finally {
                 lock.unlock();  // 释放锁
@@ -224,7 +313,7 @@
                 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")) {
@@ -232,7 +321,7 @@
                 }
                 RedisShardedPoolUtils.set("k_crude_oil_"+entry.getKey(), c);
             }
-            Thread.sleep(1000);
+            Thread.sleep(2000);
         }
     }
 
@@ -264,12 +353,13 @@
             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){

--
Gitblit v1.9.3