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 OrderInfoWs { public static final String ORDERINFOWS_CHANNEL = "orders"; public static void subscribeOrderInfoChannel(WebSocketClient webSocketClient, String option) { try { JSONArray argsArray = new JSONArray(); JSONObject args = new JSONObject(); args.put("channel", ORDERINFOWS_CHANNEL); args.put("instType", CoinEnums.INSTTYPE_SWAP.getCode()); args.put("instId", CoinEnums.HE_YUE.getCode()); argsArray.add(args); String connId = MallUtils.getOrderNum(ORDERINFOWS_CHANNEL); JSONObject jsonObject = WsParamBuild.buildJsonObject(connId, option, argsArray); webSocketClient.send(jsonObject.toJSONString()); log.info("发送订单频道频道:{}", option); } catch (Exception e) { log.error("订阅订单频道构建失败", e); } } private static final String DATA_KEY = "data"; private static final String INSTID_KEY = "instId"; private static final String ORDID_KEY = "ordId"; private static final String CLORDID_KEY = "clOrdId"; private static final String SIDE_KEY = "side"; private static final String TDMODE_KEY = "tdMode"; private static final String ACCFILLSZ_KEY = "accFillSz"; private static final String AVGPX_KEY = "avgPx"; private static final String STATE_KEY = "state"; public static void handleEvent(JSONObject response, RedisUtils redisUtils) { log.info("开始执行OrderInfoWs......"); try { JSONArray dataArray = response.getJSONArray(DATA_KEY); if (dataArray == null || dataArray.isEmpty()) { log.warn("订单频道数据为空"); return; } for (int i = 0; i < dataArray.size(); i++) { JSONObject detail = dataArray.getJSONObject(i); String instId = detail.getString(INSTID_KEY); if (!CoinEnums.HE_YUE.getCode().equals(instId)){ log.info( "订单详情-币种: {} 没有成交订单", CoinEnums.HE_YUE.getCode() ); continue; } String ordId = detail.getString(ORDID_KEY); String clOrdId = detail.getString(CLORDID_KEY); String side = detail.getString(SIDE_KEY); String tdMode = detail.getString(TDMODE_KEY); String accFillSz = detail.getString(ACCFILLSZ_KEY); String avgPx = detail.getString(AVGPX_KEY); String state = detail.getString(STATE_KEY); log.info( "订单详情-币种: {}, 系统编号: {}, 自定义编号: {}, 订单方向: {}, 交易模式: {}," + " 累计成交数量: {}, 成交均价: {}, 订单状态: {}", instId, ordId, clOrdId, side, tdMode, accFillSz, avgPx,state ); String clOrdIdStr = (String) redisUtils.get(TradeOrderWs.ORDERWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":clOrdId"); String stateStr = (String) redisUtils.get(TradeOrderWs.ORDERWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":state"); if ( StrUtil.isNotBlank(clOrdIdStr) && clOrdId.equals(clOrdIdStr) && StrUtil.isNotBlank(stateStr) && state.equals(stateStr) ){ redisUtils.set(InstrumentsWs.INSTRUMENTSWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":state", OrderParamEnums.STATE_0.getValue(), 0); } } } catch (Exception e) { log.error("处理订单频道推送数据失败", e); } } }