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.
1172 lines
36 KiB
1172 lines
36 KiB
package socket
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/shopspring/decimal"
|
|
"matchmaking-system/internal/data"
|
|
"matchmaking-system/internal/data/memory"
|
|
"matchmaking-system/internal/data/socket/forexData"
|
|
"matchmaking-system/internal/data/socket/moneyData"
|
|
"matchmaking-system/internal/data/socket/optionData"
|
|
"matchmaking-system/internal/data/socket/shareData"
|
|
"matchmaking-system/internal/data/socket/virtualData"
|
|
forexd "matchmaking-system/internal/data/tradedeal/forex"
|
|
"matchmaking-system/internal/data/tradedeal/money"
|
|
"matchmaking-system/internal/data/tradedeal/option"
|
|
"matchmaking-system/internal/data/tradedeal/share"
|
|
"matchmaking-system/internal/data/tradedeal/virtual"
|
|
"matchmaking-system/internal/pkg/flags"
|
|
"matchmaking-system/internal/pkg/logging/applogger"
|
|
"matchmaking-system/internal/pkg/setting"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// orderSubAdminShareBlkSubscribeByOrder
|
|
//
|
|
// @Description: 管理员-大宗交易股|持仓订单订阅|持仓浮动盈亏订阅
|
|
// @receiver u
|
|
// @param psgMsg
|
|
func (u *Client) orderSubAdminShareBlkSubscribeByOrder(psgMsg *SymbolMessage) {
|
|
for {
|
|
if psgMsg.Symbol == setting.SubscribeAdminShareBlk {
|
|
var hashList []share.ShareBlkTallyCache
|
|
for _, field := range psgMsg.Order {
|
|
fieldStr, err := data.Reds.HGet(context.Background(), setting.AdminShareBlkSubscribe, field).Result()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
var msg share.ShareBlkTallyCache
|
|
if err = json.Unmarshal([]byte(fieldStr), &msg); err != nil {
|
|
time.Sleep(5 * time.Second)
|
|
continue
|
|
}
|
|
hashList = append(hashList, msg)
|
|
}
|
|
|
|
for _, value := range hashList {
|
|
orderModel := shareData.ShareBlkOrderProcessing(setting.AdminShareBlkSubscribe, 1, value)
|
|
if orderModel != nil {
|
|
_, ok := u.symbol.Load(psgMsg.Symbol)
|
|
if ok {
|
|
// 清理[撤单|平仓]缓存队列
|
|
if orderModel.Status == flags.Cancel || orderModel.Status == flags.Close {
|
|
if err := data.Reds.HDel(context.Background(), setting.AdminShareBlkSubscribe, orderModel.OrderId).Err(); err != nil {
|
|
applogger.Error("AdminShareBlkSubscribe.HDel:%v", err)
|
|
continue
|
|
}
|
|
}
|
|
orderStr, err := json.Marshal(orderModel)
|
|
if err != nil {
|
|
applogger.Error("Administrator block stock subscription cache order error:%v", err)
|
|
time.Sleep(5 * time.Second)
|
|
continue
|
|
}
|
|
|
|
u.msg <- orderStr // 用户(挂单|持仓)订阅
|
|
} else {
|
|
applogger.Info("Administrator cancels block order subscription.")
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
time.Sleep(600 * time.Millisecond)
|
|
}
|
|
}
|
|
func (u *Client) orderSubAdminShareBlkSubscribeBySum(psgMsg *SymbolMessage) {
|
|
for {
|
|
if psgMsg.Symbol == setting.AdminShareBlkSumSubscribe {
|
|
priceSum, err := memory.ShareBlkFloating.Get(flags.FloatingBlk)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
result := &SymbolSumResult{
|
|
Symbol: psgMsg.Symbol,
|
|
Price: string(priceSum),
|
|
}
|
|
|
|
priceByte, err := json.Marshal(result)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
_, ok := u.symbol.Load(psgMsg.Symbol)
|
|
if ok {
|
|
u.msg <- priceByte
|
|
} else {
|
|
applogger.Info("Administrator cancels block stock order subscription.")
|
|
return
|
|
}
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
}
|
|
|
|
// orderSubAdminUserShareSubscribeBySum
|
|
//
|
|
// @Description: 管理员用户[美股订单|马股订单|泰股订单|印尼股订单|印度股订单|新加坡股订单|港股订单|期权股订单|大宗股订单]持仓总浮动盈亏
|
|
// @receiver u
|
|
// @param psgMsg
|
|
func (u *Client) orderSubAdminUserShareSubscribeBySum(psgMsg *SymbolMessage) {
|
|
for {
|
|
if psgMsg.Symbol == setting.AdminShareUserSumSubscribe {
|
|
userSumPrice := make(map[int64]decimal.Decimal)
|
|
|
|
// 统计用户总浮动盈亏
|
|
for _, value := range psgMsg.Order {
|
|
uid, err := strconv.Atoi(value)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
userId := int64(uid)
|
|
// 用户美股订单持仓总浮动盈亏
|
|
usPriceSum := GetShareUsByPriceSum(userId, setting.AdminShareUsSubscribe)
|
|
// 用户马股订单持仓总浮动盈亏
|
|
mysPriceSum := GetShareMysByPriceSum(userId, setting.AdminShareMysSubscribe)
|
|
// 用户泰股订单持仓总浮动盈亏
|
|
thaPriceSum := GetShareThaByPriceSum(userId, setting.AdminShareThaSubscribe)
|
|
// 用户印尼股订单持仓总浮动盈亏
|
|
idnPriceSum := GetShareIdnByPriceSum(userId, setting.AdminShareIdnSubscribe)
|
|
// 用户印度股订单持仓总浮动盈亏
|
|
inrPriceSum := GetShareInrByPriceSum(userId, setting.AdminShareInrSubscribe)
|
|
// 用户新加坡股订单持仓总浮动盈亏
|
|
sgdPriceSum := GetShareSgdByPriceSum(userId, setting.AdminShareSgdSubscribe)
|
|
// 用户德股订单持仓总浮动盈亏
|
|
eurPriceSum := GetShareEurByPriceSum(userId, setting.AdminShareEurSubscribe)
|
|
// 用户巴西股订单持仓总浮动盈亏
|
|
brlPriceSum := GetShareBrlByPriceSum(userId, setting.AdminShareBrlSubscribe)
|
|
// 用户法股订单持仓总浮动盈亏
|
|
furPriceSum := GetShareFurByPriceSum(userId, setting.AdminShareFurSubscribe)
|
|
// 用户日股订单持仓总浮动盈亏
|
|
jpyPriceSum := GetShareJpyByPriceSum(userId, setting.AdminShareJpySubscribe)
|
|
// 用户英股订单持仓总浮动盈亏
|
|
gbxPriceSum := GetShareGbxByPriceSum(userId, setting.AdminShareGbxSubscribe)
|
|
// 用户港股订单持仓总浮动盈亏
|
|
hkdPriceSum := GetShareHkdByPriceSum(userId, setting.AdminShareHkdSubscribe)
|
|
// 用户大宗股订单持仓总浮动盈亏
|
|
blkPriceSum := GetShareBlkByPriceSum(userId, setting.AdminShareBlkSubscribe)
|
|
// 用户外汇股订单持仓总浮动盈亏
|
|
forexPriceSum := GetForexByPriceSum(userId, setting.AdminForexSubscribe)
|
|
// 用户印度期权订单持仓总浮动盈亏
|
|
optionInrPriceSum := GetShareOptionInrByPriceSum(userId)
|
|
|
|
userSumPrice[userId] = usPriceSum.Add(mysPriceSum).Add(thaPriceSum).
|
|
Add(idnPriceSum).Add(inrPriceSum).Add(sgdPriceSum).
|
|
Add(hkdPriceSum).Add(blkPriceSum).Add(optionInrPriceSum).
|
|
Add(gbxPriceSum).Add(eurPriceSum).Add(furPriceSum).
|
|
Add(brlPriceSum).Add(jpyPriceSum).Add(forexPriceSum)
|
|
}
|
|
|
|
result := &SymbolUserSumResult{
|
|
Symbol: psgMsg.Symbol,
|
|
UserSumPrice: userSumPrice,
|
|
}
|
|
|
|
priceByte, err := json.Marshal(result)
|
|
if err != nil {
|
|
applogger.Error("orderSubAdminUserShareSubscribeBySum json Marshal err:%v", err)
|
|
continue
|
|
}
|
|
|
|
_, ok := u.symbol.Load(psgMsg.Symbol)
|
|
if ok {
|
|
u.msg <- priceByte
|
|
} else {
|
|
applogger.Error("Administrator cancels block stock order subscription.")
|
|
return
|
|
}
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
}
|
|
|
|
// GetShareData
|
|
//
|
|
// @Description: 查询用户美股订单总浮动盈亏
|
|
// @param userid
|
|
// @param shareKey
|
|
// @return decimal.Decimal
|
|
func GetShareUsByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ShareUsSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareData.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg share.ShareUsTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := shareData.ShareUsOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetShareThaByPriceSum
|
|
//
|
|
// @Description: 查询用户泰股订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetShareThaByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ShareThaSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareData.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg share.ShareThaTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := shareData.ShareThaOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetShareMysByPriceSum
|
|
//
|
|
// @Description: 查询用户马股订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetShareMysByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ShareMysSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareData.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg share.ShareMysTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := shareData.ShareMysOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetShareIdnByPriceSum
|
|
//
|
|
// @Description: 查询用户印尼股订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetShareIdnByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ShareIdnSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareData.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg share.ShareIdnTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := shareData.ShareIdnOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetShareInrByPriceSum
|
|
//
|
|
// @Description: 查询用户印度股订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetShareInrByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ShareInrSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareData.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg share.ShareInrTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := shareData.ShareInrOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetShareSgdByPriceSum
|
|
//
|
|
// @Description: 查询用户新加坡股订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetShareSgdByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ShareSgdSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareData.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg share.ShareSgdTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := shareData.ShareSgdOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetShareFurByPriceSum
|
|
//
|
|
// @Description: 查询用户法股订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetShareFurByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ShareFurSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareFurByPriceSum.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg share.ShareFurTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := shareData.ShareFurOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetShareJpyByPriceSum
|
|
//
|
|
// @Description: 查询用户日股订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetShareJpyByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ShareJpySubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareJpyByPriceSum.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg share.ShareJpyTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := shareData.ShareJpyOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetShareEurByPriceSum
|
|
//
|
|
// @Description: 查询用户德股订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetShareEurByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ShareEurSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareEurByPriceSum.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg share.ShareEurTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := shareData.ShareEurOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetShareBrlByPriceSum
|
|
//
|
|
// @Description: 查询用户巴西股订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetShareBrlByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ShareBrlSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareBrlByPriceSum.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg share.ShareBrlTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := shareData.ShareBrlOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetShareGbxByPriceSum
|
|
//
|
|
// @Description: 查询用户英股订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetShareGbxByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ShareGbxSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareGbxByPriceSum.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg share.ShareGbxTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := shareData.ShareGbxOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetShareHkdByPriceSum
|
|
//
|
|
// @Description: 查询用户港股订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetShareHkdByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ShareHkdSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareData.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg share.ShareHkdTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := shareData.ShareHkdOrderProcessing(topIc, 0, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetShareBlkByPriceSum
|
|
//
|
|
// @Description: 查询用户大宗股订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetShareBlkByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ShareBlkSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareData.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg share.ShareBlkTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := shareData.ShareBlkOrderProcessing(topIc, 1, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetShareOptionInrByPriceSum
|
|
//
|
|
// @Description: 查询用户印度期权股订单总浮动盈亏
|
|
// @param userid
|
|
// @return decimal.Decimal
|
|
func GetShareOptionInrByPriceSum(userid int64) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.OptionInrSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareData.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg option.OptionInrTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := optionData.OptionInrOrderProcessing(1, msg)
|
|
if orderModel != nil {
|
|
var newPrice, openPrice, orderNumber, subPrice, resultPrice decimal.Decimal
|
|
newPrice, err = decimal.NewFromString(orderModel.Price) // 股票实时价格
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err = decimal.NewFromString(orderModel.OpenPrice) // 订单开仓价格
|
|
if err != nil {
|
|
continue
|
|
}
|
|
orderNumber = decimal.RequireFromString(orderModel.OrderNumber) // 订单量
|
|
subPrice = OptionResultPrice(msg.Order.TradeType, msg.Order.TradingType, openPrice, newPrice) // 计算浮动盈亏
|
|
resultPrice = subPrice.Mul(orderNumber) // 计算订单浮动总盈亏
|
|
priceSum = priceSum.Add(resultPrice) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// OptionResultPrice
|
|
//
|
|
// @Description: 查询用户印度期权股订单浮动盈亏
|
|
// @param tradeType
|
|
// @param tradingType
|
|
// @param openPrice
|
|
// @param newPrice
|
|
// @return decimal.Decimal
|
|
func OptionResultPrice(tradeType, tradingType int64, openPrice, newPrice decimal.Decimal) decimal.Decimal {
|
|
//浮动盈亏(P/L)
|
|
//1>buy call & buy put
|
|
// 1> bid买一价为0, P/L显示为 -
|
|
// 2> bid买一价大于0, P/L =(bid买一价 - Cost Price)*Contracts Quantity
|
|
//2>sell call & sell put
|
|
// 1> ask卖一价为0, P/L显示为 -
|
|
// 2> ask卖一价大于0, P/L =(Cost Price - ASK卖一价)*Contracts Quantity
|
|
var subPrice decimal.Decimal
|
|
switch tradeType {
|
|
case flags.OptionCalls: // Call
|
|
switch tradingType {
|
|
case flags.OptionBuy: // buy - call
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.OptionSell: // sell - call
|
|
subPrice = openPrice.Sub(newPrice)
|
|
}
|
|
case flags.OptionPuts: // PUT
|
|
switch tradingType {
|
|
case flags.OptionBuy: // buy - put
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.OptionSell: // sell - put
|
|
subPrice = openPrice.Sub(newPrice)
|
|
}
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
return subPrice
|
|
}
|
|
|
|
// GetShareBlkByPriceSum
|
|
//
|
|
// @Description: 查询用户外汇股订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetForexByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ForexSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetShareGbxByPriceSum.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg forexd.ForexTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := forexData.ForexOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(msg.Order.OrderNumber)
|
|
pryNum := decimal.RequireFromString(msg.Order.PryNum)
|
|
price := subPrice.Mul(orderNumber).Mul(pryNum)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetMoneyByPriceSum
|
|
//
|
|
// @Description: 查询用户综合(现货|合约|外汇)订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetMoneyByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.MoneySubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetMoneyByPriceSum.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg money.MoneyTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := moneyData.MoneyOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(msg.Order.OrderNumber)
|
|
pryNum := decimal.RequireFromString(msg.Order.PryNum)
|
|
price := subPrice.Mul(orderNumber).Mul(pryNum)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetSpotsByPriceSum
|
|
//
|
|
// @Description: 查询用户现货订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetSpotsByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.SpotsSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetSpotsByPriceSum.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg virtual.SpotsTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
_, orderModel := virtualData.SpotOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(msg.Order.OrderNumber)
|
|
price := subPrice.Mul(orderNumber)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetContractByPriceSum
|
|
//
|
|
// @Description: 查询用户合约订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetContractByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.ContractSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetContractByPriceSum.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg virtual.ContractTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := virtualData.ContractOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(msg.Order.OrderNumber)
|
|
pryNum := decimal.RequireFromString(msg.Order.PryNum)
|
|
price := subPrice.Mul(orderNumber).Mul(pryNum)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|
|
// GetSecondByPriceSum
|
|
//
|
|
// @Description: 查询用户秒合约订单总浮动盈亏
|
|
// @param userid
|
|
// @param topIc
|
|
// @return decimal.Decimal
|
|
func GetSecondByPriceSum(userid int64, topIc string) decimal.Decimal {
|
|
shareKey := fmt.Sprintf("%v-%v", setting.SecondSubscribe, userid)
|
|
hashMap, err := data.Reds.HGetAll(context.Background(), shareKey).Result()
|
|
if err != nil {
|
|
applogger.Error("GetSecondByPriceSum.HGetAll:%v", err)
|
|
return decimal.Zero
|
|
}
|
|
|
|
var priceSum decimal.Decimal
|
|
for _, value := range hashMap {
|
|
var msg virtual.ContractTallyCache
|
|
if err = json.Unmarshal([]byte(value), &msg); err != nil {
|
|
continue
|
|
}
|
|
if msg.Status == flags.Position {
|
|
orderModel := virtualData.SecondOrderProcessing(topIc, msg)
|
|
if orderModel != nil {
|
|
var subPrice decimal.Decimal
|
|
newPrice, err := decimal.NewFromString(orderModel.Price)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
openPrice, err := decimal.NewFromString(orderModel.OpenPrice)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
switch msg.Order.TradeType {
|
|
case flags.TradeTypeBuy: // 买涨 = 新价格 - 开仓价
|
|
subPrice = newPrice.Sub(openPrice)
|
|
case flags.TradeTypeSell: // 卖跌 = 开仓价 - 新价格
|
|
subPrice = openPrice.Sub(newPrice)
|
|
default:
|
|
subPrice = decimal.Zero
|
|
}
|
|
orderNumber := decimal.RequireFromString(msg.Order.OrderNumber)
|
|
pryNum := decimal.RequireFromString(msg.Order.PryNum)
|
|
price := subPrice.Mul(orderNumber).Mul(pryNum)
|
|
priceSum = priceSum.Add(price) // 累加盈亏统计
|
|
}
|
|
}
|
|
}
|
|
|
|
return priceSum
|
|
}
|
|
|