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.
150 lines
4.2 KiB
150 lines
4.2 KiB
2 months ago
|
package money
|
||
|
|
||
|
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>清理缓存列表
|
||
|
*/
|
||
|
|
||
|
// MoneyTallyCache
|
||
|
// @Description:
|
||
|
type MoneyTallyCache struct {
|
||
|
UserId int64 // 用户ID
|
||
|
OrderId string // 订单ID
|
||
|
Symbol string // 交易对
|
||
|
Status string // 订单状态
|
||
|
OpenPrice string // 开仓价格
|
||
|
ClosingPrice string // 平仓价格
|
||
|
StrongParity string // 强平价格
|
||
|
Order structure.MoneyOrder // 下单信息
|
||
|
}
|
||
|
|
||
|
// MoneyCacheDeal
|
||
|
//
|
||
|
// @Description:
|
||
|
// @param ctx
|
||
|
// @param userId
|
||
|
// @param orderId
|
||
|
// @param priceNew
|
||
|
// @param order
|
||
|
// @return string
|
||
|
// @return *MoneyTallyCache
|
||
|
// @return error
|
||
|
func MoneyCacheDeal(ctx context.Context, userId int64, orderId, priceNew string, order structure.MoneyOrder) (string, *MoneyTallyCache, error) {
|
||
|
var marketStatus string
|
||
|
tallyCache := &MoneyTallyCache{
|
||
|
UserId: userId, // 下单用户ID
|
||
|
OrderId: orderId, // 下单订单ID
|
||
|
Symbol: order.StockId, // 下单交易对
|
||
|
Status: flags.Entrust, // 下单挂单状态
|
||
|
Order: order, // 完整下单信息
|
||
|
}
|
||
|
switch order.DealType {
|
||
|
case flags.DealTypeLimited: // 限价(买涨|买跌)
|
||
|
marketStatus = setting.MarketMoneyEntrust
|
||
|
case flags.DealTypeMarket: // 市价(买涨|买跌)
|
||
|
if order.Type == flags.SpotsMarketType {
|
||
|
tallyCache.OpenPrice = priceNew // 现货市价下单开仓价格(price)
|
||
|
tallyCache.Status = flags.Close // 现货市价下单持仓状态(status)
|
||
|
marketStatus = setting.MarketMoneyClose // 现货(平仓)标识
|
||
|
} else {
|
||
|
tallyCache.OpenPrice = priceNew // (合约|外汇)市价下单开仓价格(price)
|
||
|
tallyCache.Status = flags.Position // (合约|外汇)市价下单持仓状态(status)
|
||
|
marketStatus = setting.MarketMoneyPosition // (合约|外汇)(挂单|持仓)缓存列表
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return marketStatus, tallyCache, nil
|
||
|
}
|
||
|
|
||
|
// MoneySubMarketPrice
|
||
|
//
|
||
|
// @Description: 下单订阅行情数据
|
||
|
// @param ctx
|
||
|
// @param subKey
|
||
|
// @return string
|
||
|
// @return error
|
||
|
func MoneySubMarketPrice(ctx context.Context, subKey string, marketType int64) (string, error) {
|
||
|
var err error
|
||
|
var price []byte
|
||
|
|
||
|
switch marketType {
|
||
|
case flags.SpotsMarketType: // 现货即时价格
|
||
|
price, err = memory.GetMoneySpotsCache(subKey)
|
||
|
if err != nil {
|
||
|
return flags.SetNull, err
|
||
|
}
|
||
|
case flags.ContractMarketType: // 合约即时价格
|
||
|
price, err = memory.GetMoneyContractCache(subKey)
|
||
|
if err != nil {
|
||
|
return flags.SetNull, err
|
||
|
}
|
||
|
case flags.ForexMarketType: // 外汇即时价格
|
||
|
price, err = memory.GetMoneyForexCache(subKey)
|
||
|
if err != nil {
|
||
|
return flags.SetNull, err
|
||
|
}
|
||
|
default:
|
||
|
}
|
||
|
|
||
|
return string(price), nil
|
||
|
}
|
||
|
|
||
|
// MoneyPushAddCache
|
||
|
//
|
||
|
// @Description: 下单录入(挂单|持仓)缓存列表
|
||
|
// @param red
|
||
|
// @param cacheKey
|
||
|
// @param tallyCache
|
||
|
// @return error
|
||
|
func MoneyPushAddCache(red *redis.Client, cacheKey string, tallyCache *MoneyTallyCache) error {
|
||
|
content, err := json.Marshal(tallyCache)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if err = red.HSet(context.Background(), cacheKey, tallyCache.OrderId, string(content)).Err(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// MoneyHashSetOrderId
|
||
|
//
|
||
|
// @Description: 录入订单ID-Hash缓存列表
|
||
|
// @param red
|
||
|
// @param cacheKey
|
||
|
// @param order
|
||
|
// @return error
|
||
|
func MoneyHashSetOrderId(red *redis.Client, cacheKey string, order *MoneyTallyCache) error {
|
||
|
byteStr, err := json.Marshal(order)
|
||
|
if err != nil {
|
||
|
applogger.Error("%v MoneyHashSetOrderId.Marshal:%v", common.ErrMoney, err)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
err = red.HSet(context.Background(), cacheKey, order.OrderId, string(byteStr)).Err()
|
||
|
if err != nil {
|
||
|
applogger.Error("%v MoneyHashSetOrderId.HSet:%v", common.ErrMoney, err)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|