251 lines
6.6 KiB
Go
251 lines
6.6 KiB
Go
package actions
|
|
|
|
import (
|
|
"math"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/iapologizewhenimwrong/Vestmore_GO/model"
|
|
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/basic"
|
|
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/marketdate"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// @Action securities/account
|
|
// SecuritiesAccount
|
|
// market: string;
|
|
// randstr: string;
|
|
// sign: string;
|
|
// action: string;
|
|
// app_market: int64;
|
|
// timestamp: int64;
|
|
// token: string;
|
|
func SecuritiesAccount(ctx *ActionContext[SecuritiesAccountParam]) (resp *basic.Response) {
|
|
|
|
market := ctx.Param.Market
|
|
if market == "" {
|
|
return resp.ErrorMsg(1, "market 参数缺失")
|
|
}
|
|
|
|
if ctx.Param.Token == "" {
|
|
return resp.ErrorMsg(3, "token 参数缺失")
|
|
}
|
|
|
|
var modelHold = model.Models.KillaraCustomerHoldModel
|
|
var modelTransaction = model.Models.KillaraCustomerTransactionModel
|
|
var modelBalance = model.Models.KillaraCustomerBalanceModel
|
|
|
|
market = strings.ToUpper(strings.TrimSpace(market))
|
|
|
|
var respData map[string]any
|
|
model.Models.KillaraCustomerModel.Transaction(func(tx *gorm.DB) error {
|
|
|
|
customerToken, err := model.Models.KillaraCustomerTokenModel.GetToken(tx, ctx.Param.Token)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
customer, err := model.Models.KillaraCustomerModel.GetCustomer(tx, *customerToken.CustomerId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var balance float64
|
|
if market == "HK" {
|
|
balance = *customer.HkBalance
|
|
} else if market == "US" {
|
|
balance = *customer.UsBalance
|
|
} else {
|
|
balance = 0
|
|
}
|
|
|
|
assetValue := balance
|
|
|
|
balanceFreeze, err := modelBalance.SumFreeze(tx, *customer.CustomerId, market, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
holdValue := 0.0
|
|
profitToday := 0.0
|
|
|
|
profitHistory, err := modelTransaction.GetHistoryProfit(map[string]interface{}{
|
|
"customer_id": *customer.CustomerId,
|
|
"market": market,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
profitHistory = math.Round(profitHistory*1000) / 1000
|
|
|
|
var holds []map[string]interface{}
|
|
var orders []map[string]interface{}
|
|
|
|
// 当前持仓
|
|
holdResults, err := modelHold.GetHoldsForFrontEnd(tx, map[string]interface{}{
|
|
"customer_id": *customer.CustomerId,
|
|
"market": market,
|
|
"status": 1,
|
|
"sort": "customer_hold_id",
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, result := range holdResults {
|
|
var stock map[string]interface{}
|
|
var nowPrice float64
|
|
|
|
if market == "HK" || market == "US" {
|
|
stock, err = model.Models.KillaraStockModel.StockDetail(nil,
|
|
*result.Market, *result.StockType, *result.StockSymbol,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if stock != nil {
|
|
nowPrice = stock["price"].(float64)
|
|
} else {
|
|
nowPrice = 0
|
|
}
|
|
} else {
|
|
// 调用 其一个方法查询股票详情 TODO:
|
|
// stock, err = (result.StockSymbol)
|
|
// if err != nil {
|
|
// return resp.ErrorMsg(1, err.Error())
|
|
// }
|
|
// if stock != nil {
|
|
// if result.Type == 0 || result.Type == 1 {
|
|
// nowPrice = stock["ask"].(float64)
|
|
// } else {
|
|
// nowPrice = stock["bid"].(float64)
|
|
// }
|
|
// } else {
|
|
// nowPrice = 0
|
|
// }
|
|
}
|
|
|
|
var value, originValue, profitLoss, profitLossRate float64
|
|
if nowPrice > 0 {
|
|
value = math.Round(nowPrice*float64(*result.Quantity)*1000) / 1000
|
|
originValue = math.Round(*result.Price*float64(*result.Quantity)*1000) / 1000
|
|
profitLoss = math.Round((value-originValue)*1000) / 1000
|
|
|
|
if profitLoss != 0 && originValue != 0 {
|
|
if *result.IsWithfunding == 1 {
|
|
profitLossRate = math.Round(profitLoss/originValue*100*float64(*result.WithfundingMagnification)*1000) / 1000
|
|
} else {
|
|
profitLossRate = math.Round(profitLoss/originValue*100*1000) / 1000
|
|
}
|
|
} else {
|
|
profitLossRate = 0
|
|
}
|
|
} else {
|
|
value = 0
|
|
originValue = 0
|
|
profitLoss = 0
|
|
profitLossRate = 0
|
|
}
|
|
|
|
isWithfunding := false
|
|
var withfundingData map[string]interface{}
|
|
if *result.IsWithfunding == 1 {
|
|
isWithfunding = true
|
|
withfundingData = map[string]interface{}{
|
|
"withfunding_total": result.WithfundingTotal,
|
|
"withfunding_magnification": result.WithfundingMagnification,
|
|
"withfunding_day": result.WithfundingDay,
|
|
"interest": result.Interest,
|
|
"warning_line_price": result.WarningLinePrice,
|
|
"clear_line_price": result.ClearLinePrice,
|
|
"withfunding_start_date": result.WithfundingStartDate,
|
|
"withfunding_end_date": result.WithfundingEndDate,
|
|
}
|
|
}
|
|
|
|
holds = append(holds, map[string]interface{}{
|
|
"id": result.CustomerHoldId,
|
|
"stock_name": result.StockName,
|
|
"stock_symbol": result.StockSymbol,
|
|
"quantity": result.Quantity,
|
|
"avg_price": result.Price,
|
|
"now_price": nowPrice,
|
|
"value": value,
|
|
"profit_loss": profitLoss,
|
|
"profit_loss_rate": profitLossRate,
|
|
"is_withfunding": isWithfunding,
|
|
"withfunding_data": withfundingData,
|
|
})
|
|
|
|
holdValue += value
|
|
profitToday += profitLoss
|
|
}
|
|
|
|
// // 当前委托
|
|
transactionResults, err := modelTransaction.GetTransactionsForFrontEnd(tx, map[string]interface{}{
|
|
"customer_id": *customer.CustomerId,
|
|
"market": market,
|
|
"status": 1,
|
|
"sort": "customer_transaction_id",
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, result := range transactionResults {
|
|
|
|
orders = append(orders, map[string]interface{}{
|
|
"id": result.CustomerTransactionId,
|
|
"stock_name": result.StockName,
|
|
"stock_symbol": result.StockSymbol,
|
|
"quantity": result.Quantity,
|
|
"price": result.Price,
|
|
"date": marketdate.HandleMarketDatetime(result),
|
|
"type": result.Type,
|
|
})
|
|
}
|
|
|
|
assetValue += holdValue + balanceFreeze
|
|
|
|
respData = map[string]interface{}{
|
|
"balance": balance,
|
|
"balance_freeze": balanceFreeze,
|
|
"hold_value": holdValue,
|
|
"asset_value": assetValue,
|
|
"profit_today": strconv.FormatFloat(profitToday, 'f', -1, 64),
|
|
"profit_history": profitHistory,
|
|
"orders": orders,
|
|
"holds": holds,
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return resp.Success(respData)
|
|
}
|
|
|
|
// @Action securities/currency
|
|
// SecuritiesCurrency
|
|
// randstr: string;
|
|
// sign: string;
|
|
// action: string;
|
|
// app_market: int64;
|
|
// timestamp: int64;
|
|
// token: string;
|
|
func SecuritiesCurrency(ctx *ActionContext[SecuritiesCurrencyParam]) (resp *basic.Response) {
|
|
|
|
return resp.Success()
|
|
}
|
|
|
|
// @Action securities/capital
|
|
// SecuritiesCapital
|
|
// randstr: string;
|
|
// sign: string;
|
|
// action: string;
|
|
// app_market: int64;
|
|
// timestamp: int64;
|
|
// token: string;
|
|
func SecuritiesCapital(ctx *ActionContext[SecuritiesCapitalParam]) (resp *basic.Response) {
|
|
|
|
return resp.Success()
|
|
}
|