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.okxWs.wanggeList.WangGeListEnum; import com.xcong.excoin.modules.okxNewPrice.okxpi.MallUtils; import com.xcong.excoin.modules.okxNewPrice.utils.WsMapBuild; 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; import java.math.BigDecimal; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * @author Administrator */ @Slf4j public class OrderInfoWs { // 使用双层Map,第一层key为账号名称,第二层key为数据key public static final Map> ORDERINFOWSMAP = new ConcurrentHashMap<>(); // 获取指定账号的Map,如果不存在则创建 public static Map getAccountMap(String accountName) { return ORDERINFOWSMAP.computeIfAbsent(accountName, k -> new ConcurrentHashMap<>()); } 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); } } public static void initEvent(JSONObject response, String accountName) { // log.info("订阅成功: {}", response.getJSONObject("arg")); } 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, String accountName) { // 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( "{}: 订单详情-币种: {}, 系统编号: {}, 自定义编号: {}, 订单方向: {}, 交易模式: {}," + " 累计成交数量: {}, 成交均价: {}, 订单状态: {}", accountName, instId, ordId, clOrdId, side, tdMode, accFillSz, avgPx,state ); String clOrdIdStr = TradeOrderWs.getAccountMap(accountName).get("clOrdId"); String stateStr = TradeOrderWs.getAccountMap(accountName).get("state"); if ( StrUtil.isNotBlank(clOrdIdStr) && clOrdId.equals(clOrdIdStr) && StrUtil.isNotBlank(stateStr) && state.equals(stateStr) ){ Map accountMap = getAccountMap(accountName); //记录成交均价 if (accountMap.get("orderPrice") == null){ WsMapBuild.saveStringToMap(accountMap, "orderPrice",avgPx); } WsMapBuild.saveStringToMap(TradeOrderWs.getAccountMap(accountName), "state", CoinEnums.ORDER_LIVE.getCode()); //保存上一个网格信息 WangGeListEnum gridByPrice = WangGeListEnum.getGridByPrice(new BigDecimal(avgPx)); if (gridByPrice != null){ Map instrumentsMap = InstrumentsWs.getAccountMap(accountName); WsMapBuild.saveStringToMap(instrumentsMap, CoinEnums.WANG_GE_OLD.name(), gridByPrice.name()); } // 使用账号特定的Map String positionAccountName = PositionsWs.initAccountName(accountName, side); Map positionsMap = PositionsWs.getAccountMap(positionAccountName); WsMapBuild.saveBigDecimalToMap(positionsMap, CoinEnums.READY_STATE.name(), WsMapBuild.parseBigDecimalSafe(CoinEnums.READY_STATE_NO.getCode())); Map accountWsMap = AccountWs.getAccountMap(accountName); WsMapBuild.saveStringToMap(accountWsMap, CoinEnums.READY_STATE.name(), CoinEnums.READY_STATE_NO.getCode()); log.info("{}: 订单详情已完成: {}, 自定义编号: {}", accountName, CoinEnums.HE_YUE.getCode(), clOrdId); } } } catch (Exception e) { log.error("处理订单频道推送数据失败", e); } } }