Administrator
2025-12-09 249ddecd3afeb12537edfef4d7a127ffe32f7b9f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.xcong.excoin.modules.okxNewPrice.okxWs;
 
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.CoinEnums;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.OrderParamEnums;
import com.xcong.excoin.modules.okxNewPrice.okxpi.MallUtils;
import com.xcong.excoin.modules.okxNewPrice.utils.WsParamBuild;
import com.xcong.excoin.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
 
/**
 * @author Administrator
 */
@Slf4j
public class TradeOrderWs {
 
    public static final String ORDERWS_CHANNEL = "order";
 
    public static void orderEvent(WebSocketClient webSocketClient, RedisUtils redisUtils, String side) {
 
        String buyCnt = null;
 
        if (OrderParamEnums.ORDERING.getValue().equals(side)) {
            return;
        } else if (OrderParamEnums.HOLDING.getValue().equals(side)) {
            return;
        } else if (OrderParamEnums.INIT.getValue().equals(side)) {
            side = OrderParamEnums.BUY.getValue();
            buyCnt = getRedisValue(redisUtils, InstrumentsWs.INSTRUMENTSWS_CHANNEL, ":ctVal");
        } else if (OrderParamEnums.OUT.getValue().equals(side)) {
            side = OrderParamEnums.SELL.getValue();
            buyCnt = getRedisValue(redisUtils, PositionsWs.POSITIONSWS_CHANNEL, ":pos");
        } else {
            String buyCntNormal = getRedisValue(redisUtils, PositionsWs.POSITIONSWS_CHANNEL, ":buyCnt");
            if (StrUtil.isNotBlank(buyCntNormal)) {
                buyCnt = buyCntNormal;
            }
        }
 
        // 校验必要参数
        if (StrUtil.isBlank(side)) {
            log.warn("下单参数 side 为空,取消发送");
            return;
        }
 
        if (StrUtil.isBlank(buyCnt)) {
            log.warn("下单数量 buyCnt 为空,取消发送");
            return;
        }
 
        try {
            String clOrdId = MallUtils.getOrderNum(side);
            JSONArray argsArray = new JSONArray();
            JSONObject args = new JSONObject();
            args.put("instId", CoinEnums.HE_YUE.getCode());
            args.put("tdMode", CoinEnums.CROSS.getCode());
            args.put("clOrdId", clOrdId);
            args.put("side", side);
            args.put("posSide", CoinEnums.POSSIDE_LONG.getCode());
            args.put("ordType", CoinEnums.ORDTYPE_MARKET.getCode());
            args.put("sz", buyCnt);
            argsArray.add(args);
 
            String connId = MallUtils.getOrderNum(ORDERWS_CHANNEL);
            JSONObject jsonObject = WsParamBuild.buildJsonObject(connId, ORDERWS_CHANNEL, argsArray);
            webSocketClient.send(jsonObject.toJSONString());
            log.info("发送下单频道:{},数量:{}", side, buyCnt);
            boolean setResult =
                    redisUtils.set(ORDERWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":clOrdId", clOrdId, 0)
                    && redisUtils.set(ORDERWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":state", CoinEnums.ORDER_FILLED.getCode(), 0)
                    && redisUtils.set(InstrumentsWs.INSTRUMENTSWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":state", OrderParamEnums.STATE_4.getValue(), 0);
            if (!setResult) {
                log.warn("Redis set operation failed for key: order:{}", CoinEnums.HE_YUE.getCode());
            }
        } catch (Exception e) {
            log.error("下单构建失败", e);
        }
    }
 
    /**
     * 统一封装 Redis Key 构建逻辑
     *
     * @param redisUtils Redis 工具类实例
     * @param prefix     渠道前缀
     * @param suffix     字段后缀
     * @return Redis 中存储的值
     */
    private static String getRedisValue(RedisUtils redisUtils, String prefix, String suffix) {
        String key = prefix + ":" + CoinEnums.HE_YUE.getCode() + suffix;
        Object valueObj = redisUtils.get(key);
        return valueObj == null ? null : String.valueOf(valueObj);
    }
}