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.
105 lines
2.9 KiB
105 lines
2.9 KiB
package virtual
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"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-挂单(委托),2-已撤单,3-完成订单)
|
|
1>限价
|
|
1、写入挂单缓存列表
|
|
2、监控挂单缓存列表
|
|
3、处理完成订单
|
|
4、清理缓存列表
|
|
2>市价
|
|
1.平仓操作
|
|
*/
|
|
|
|
// SpotsTallyCache
|
|
// @Description:
|
|
type SpotsTallyCache struct {
|
|
UserId int64 // 用户ID
|
|
OrderId string // 订单ID
|
|
Symbol string // 交易对
|
|
Status string // 订单状态
|
|
OpenPrice string // 开仓价格
|
|
ClosingPrice string // 平仓价格
|
|
Order structure.SpotsOrder // 下单信息
|
|
}
|
|
|
|
// DealSpotsLimitedPrice
|
|
//
|
|
// @Description:
|
|
// @param red
|
|
// @param userId
|
|
// @param order
|
|
// @param Symbol
|
|
// @param orderId
|
|
// @return error
|
|
func DealSpotsLimitedPrice(red *redis.Client, userId int64, order structure.SpotsOrder, Symbol, orderId string) error {
|
|
data := &SpotsTallyCache{
|
|
OrderId: orderId, // 下单生成订单ID
|
|
Symbol: Symbol, // 下单用户交易对
|
|
Status: flags.Entrust, // 下单用户挂单状态
|
|
Order: order, // 下单信息
|
|
UserId: userId, // 下单用户ID
|
|
}
|
|
content, err := json.Marshal(data)
|
|
if err != nil {
|
|
applogger.Error("%v DealSpotsLimitedPrice.Marshal:%v", common.ErrSpots, err)
|
|
return flags.ErrCacheDB
|
|
}
|
|
if !flags.CheckSetting {
|
|
applogger.Debug("挂单信息:%v", string(content))
|
|
}
|
|
|
|
// 写入限价下单挂单缓存列表
|
|
if err = red.HSet(context.Background(), setting.MarketSpotsEntrust, orderId, string(content)).Err(); err != nil {
|
|
applogger.Error("%v DealSpotsLimitedPrice.MarketSpotsEntrust.HSet:%v", common.ErrSpots, err)
|
|
return flags.ErrCacheDB
|
|
}
|
|
|
|
// 写入用户订单订阅缓存hash列表
|
|
cacheKey := OrderIdListKey(setting.SpotsSubscribe, userId)
|
|
if err = red.HSet(context.Background(), cacheKey, orderId, string(content)).Err(); err != nil {
|
|
applogger.Error("%v DealSpotsLimitedPrice.SpotsSubscribe.HSet:%v", common.ErrSpots, err)
|
|
return flags.ErrCacheDB
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// DealSpotsMarketPrice
|
|
//
|
|
// @Description:
|
|
// @param ctx
|
|
// @param subKey
|
|
// @return string
|
|
// @return error
|
|
func DealSpotsMarketPrice(ctx context.Context, subKey string) (string, error) {
|
|
price, err := memory.SpotsCache.Get(subKey)
|
|
if err != nil {
|
|
return flags.SetNull, err
|
|
}
|
|
|
|
return string(price), err
|
|
}
|
|
|
|
// OrderIdListKey
|
|
//
|
|
// @Description:
|
|
// @param topIc
|
|
// @param userId
|
|
// @return string
|
|
func OrderIdListKey(topIc string, userId int64) string {
|
|
return fmt.Sprintf("%v-%v", topIc, userId)
|
|
}
|
|
|