From 640ccb9229224642515527daf87f308a7aa9bdf4 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Wed, 10 Jun 2026 11:47:26 +0800
Subject: [PATCH] 1

---
 trading-order-admin/src/main/java/com/yami/trading/admin/facade/MarketQuotationsFacade.java |   90 ++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 80 insertions(+), 10 deletions(-)

diff --git a/trading-order-admin/src/main/java/com/yami/trading/admin/facade/MarketQuotationsFacade.java b/trading-order-admin/src/main/java/com/yami/trading/admin/facade/MarketQuotationsFacade.java
index 5dc7e7c..9860d57 100644
--- a/trading-order-admin/src/main/java/com/yami/trading/admin/facade/MarketQuotationsFacade.java
+++ b/trading-order-admin/src/main/java/com/yami/trading/admin/facade/MarketQuotationsFacade.java
@@ -12,6 +12,7 @@
 import com.yami.trading.common.util.Arith;
 import com.yami.trading.common.util.IPHelper;
 import com.yami.trading.common.util.ThreadUtils;
+import com.yami.trading.huobi.data.AdjustmentValueCache;
 import com.yami.trading.huobi.data.DataCache;
 import com.yami.trading.huobi.data.internal.AdjustmentValueService;
 import com.yami.trading.huobi.data.model.AdjustmentValue;
@@ -183,19 +184,88 @@
 
     }
 
-    public void adjust(String symbol, Double second, BigDecimal value) {
-        BigDecimal currentValue = this.adjustmentValueService.getCurrentValue(symbol);
-        if (currentValue == null) {
-            Realtime realtime = this.dataService.realtime(symbol).get(0);
-            currentValue = realtime.getClose();
+    /**
+     * 提交调整(与 getValue.action 预计算使用同一套 type 规则)。
+     */
+    public void adjust(String symbol, Double second, BigDecimal value, String type) {
+        AdjustmentValueCache.getDelayValue().remove(symbol);
+        AdjustmentValueCache.getPreAllocatedAdjustments().remove(symbol);
+        AdjustmentValueCache.getCurrentAdjustmentIndex().remove(symbol);
+        AdjustmentValueCache.getFrequency().remove(symbol);
+
+        String adjustType = StringUtils.isBlank(type) ? "2" : type;
+        BigDecimal effectiveDelta = resolveEffectiveAdjustDelta(symbol, adjustType, value);
+        if (effectiveDelta == null || effectiveDelta.compareTo(BigDecimal.ZERO) == 0) {
+            log.warn("行情调整无效 symbol={} value={} type={}", symbol, value, adjustType);
+            return;
         }
-        String log = MessageFormat.format("ip:" + IPHelper.getIpAddr() + ",管理员调整行情,币种:{0},原值:{1},调整值:{2},调整时间:{3}",
-                symbol, currentValue.toPlainString(), value.toPlainString(), second);
 
-        this.adjustmentValueService.adjust(symbol, value, second);
-        saveLog(log);
-        ThreadUtils.sleep(1000);
+        BigDecimal beforeAdjust = adjustmentValueService.getCurrentValue(symbol);
+        double secondVal = second == null ? 0D : second;
 
+        String logContent = MessageFormat.format(
+                "ip:" + IPHelper.getIpAddr() + ",管理员调整行情,币种:{0},调整前累计:{1},本次增量:{2},type:{3},延迟秒:{4}",
+                symbol,
+                beforeAdjust == null ? "0" : beforeAdjust.toPlainString(),
+                effectiveDelta.toPlainString(),
+                adjustType,
+                secondVal);
+
+        adjustmentValueService.adjust(symbol, effectiveDelta, secondVal);
+
+        // 立即生效时同步内存行情,避免只改缓存累计值但页面仍显示旧价
+        if (secondVal <= 0) {
+            syncRealtimeCacheAfterAdjust(symbol, effectiveDelta);
+        } else {
+            log.info("延迟调整已提交 symbol={} 目标增量={} 时长约{}秒,价格将分步变化(非瞬间到位)",
+                    symbol, effectiveDelta.toPlainString(), secondVal);
+        }
+
+        saveLog(logContent);
+        ThreadUtils.sleep(500);
+    }
+
+    /**
+     * 与 calculateValue 一致:type 0/1 在输入值基础上加减 pips,type 2 直接使用输入值作为本次增量。
+     */
+    private BigDecimal resolveEffectiveAdjustDelta(String symbol, String type, BigDecimal inputValue) {
+        if (inputValue == null) {
+            return null;
+        }
+        Item item = itemService.findBySymbol(symbol);
+        if (item == null) {
+            return inputValue;
+        }
+        double pips = item.getPips() != null ? item.getPips().doubleValue() : 0D;
+        double v = inputValue.doubleValue();
+        if ("0".equalsIgnoreCase(type)) {
+            return BigDecimal.valueOf(Arith.add(v, pips));
+        }
+        if ("1".equalsIgnoreCase(type)) {
+            return BigDecimal.valueOf(Arith.sub(v, pips));
+        }
+        return inputValue;
+    }
+
+    /** 将本次增量反映到 DataCache,与 WebSocket/采集任务展示逻辑一致 */
+    private void syncRealtimeCacheAfterAdjust(String symbol, BigDecimal effectiveDelta) {
+        Realtime realtime = DataCache.getRealtime(symbol);
+        if (realtime == null) {
+            List<Realtime> list = dataService.realtime(symbol);
+            if (CollectionUtil.isEmpty(list)) {
+                return;
+            }
+            realtime = list.get(0);
+        }
+        Integer decimal = itemService.getDecimal(symbol);
+        realtime.setClose(realtime.getClose().add(effectiveDelta).setScale(decimal, RoundingMode.HALF_UP));
+        if (realtime.getAsk() != null) {
+            realtime.setAsk(realtime.getAsk().add(effectiveDelta).setScale(decimal, RoundingMode.HALF_UP));
+        }
+        if (realtime.getBid() != null) {
+            realtime.setBid(realtime.getBid().add(effectiveDelta).setScale(decimal, RoundingMode.HALF_UP));
+        }
+        DataCache.putRealtime(symbol, realtime);
     }
 
     public void saveLog(String content) {

--
Gitblit v1.9.3