You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.2 KiB

2 months ago
package virtual
import (
"context"
"encoding/json"
"github.com/redis/go-redis/v9"
"matchmaking-system/internal/biz/structure"
"matchmaking-system/internal/data/memory"
"matchmaking-system/internal/pkg/flags"
"matchmaking-system/internal/pkg/logging/applogger"
"matchmaking-system/internal/pkg/logging/common"
"matchmaking-system/internal/pkg/setting"
)
/*
处理秒合约下单交易(订单状态:0-挂单(委托),1-持仓订单,2-已撤单,3-完成订单)
1>写入挂单缓存列表
2>监控挂单缓存列表
3>监控持仓缓存列表
4>处理完成订单
5>清理缓存列表
*/
// SecondCacheDeal
//
// @Description:
// @param ctx
// @param userId
// @param orderId
// @param priceNew
// @param order
// @return string
// @return *ContractTallyCache
// @return error
func SecondCacheDeal(ctx context.Context, userId int64, orderId, priceNew string, order structure.ContractOrder) (string, *ContractTallyCache, error) {
var marketStatus string
tallyCache := &ContractTallyCache{
UserId: userId, // 下单用户ID
OrderId: orderId, // 下单订单ID
Symbol: order.ContractId, // 下单交易对
Status: flags.Entrust, // 下单挂单状态
Order: order, // 完整下单信息
}
switch order.DealType {
case flags.DealTypeLimited: // TODO: 暂无-限价(买涨|买跌)
marketStatus = setting.MarketSecondPosition
case flags.DealTypeMarket: // 市价(买涨|买跌)
tallyCache.OpenPrice = priceNew // 市价下单开仓价格(price)
tallyCache.Status = flags.Position // 市价下单持仓状态(status)
marketStatus = setting.MarketSecondPosition // (挂单|持仓)缓存列表
default:
}
return marketStatus, tallyCache, nil
}
// SecondSubMarketPrice
//
// @Description: 下单订阅行情数据
// @param ctx
// @param subKey
// @return string
// @return error
func SecondSubMarketPrice(ctx context.Context, subKey string) (string, error) {
price, err := memory.GetSecondCache(subKey)
if err != nil {
return flags.SetNull, err
}
return string(price), nil
}
// SecondPushAddCache
//
// @Description: 下单录入(挂单|持仓)缓存列表
// @param red
// @param cacheKey
// @param tallyCache
// @return error
func SecondPushAddCache(red *redis.Client, cacheKey string, tallyCache *ContractTallyCache) error {
content, err := json.Marshal(tallyCache)
if err != nil {
applogger.Error("%v ContractSecondPushAddCache.Marshal:%v", common.ErrSecond, err)
return err
}
if err = red.HSet(context.Background(), cacheKey, tallyCache.OrderId, string(content)).Err(); err != nil {
applogger.Error("%v ContractSecondPushAddCache.HSet:%v", common.ErrSecond, err)
return err
}
return nil
}
// SecondHashSetOrderId
//
// @Description: 录入订单ID-Hash缓存列表
// @param red
// @param cacheKey
// @param order
// @return error
func SecondHashSetOrderId(red *redis.Client, cacheKey string, order *ContractTallyCache) error {
byteStr, err := json.Marshal(order)
if err != nil {
applogger.Error("%v ContractSecondHashSetOrderId.Marshal:%v", common.ErrSecond, err)
return err
}
err = red.HSet(context.Background(), cacheKey, order.OrderId, string(byteStr)).Err()
if err != nil {
applogger.Error("%v ContractSecondHashSetOrderId.HSet:%v", common.ErrSecond, err)
return err
}
return nil
}