更新对应交易的代码实现
This commit is contained in:
@@ -69,7 +69,7 @@ func BaseGetToken(ctx *ActionContext[BaseGetTokenParam]) (resp *basic.Response)
|
||||
|
||||
if result.CustomerId != nil && *result.CustomerId != 0 {
|
||||
modelCustomer := model.Models.KillaraCustomerModel
|
||||
customer, err := modelCustomer.GetCustomer(*result.CustomerId)
|
||||
customer, err := modelCustomer.GetCustomer(nil, *result.CustomerId)
|
||||
if err == nil && customer != nil {
|
||||
isLogin = true
|
||||
} else {
|
||||
@@ -155,13 +155,13 @@ func AccountLoginWithTelephonePassword(ctx *ActionContext[AccountLoginWithTeleph
|
||||
var customer *model.KillaraCustomer
|
||||
var err error
|
||||
|
||||
customer, err = model.Models.KillaraCustomerModel.GetCustomerByTelephoneForBackEnd(telephone)
|
||||
customer, err = model.Models.KillaraCustomerModel.GetCustomerByTelephoneForBackEnd(nil, telephone)
|
||||
if err != nil {
|
||||
resp.ErrorMsg(1, err.Error())
|
||||
return
|
||||
}
|
||||
if customer == nil {
|
||||
customer, err = model.Models.KillaraCustomerModel.GetCustomerByCode(telephone)
|
||||
customer, err = model.Models.KillaraCustomerModel.GetCustomerByCode(nil, telephone)
|
||||
if err != nil {
|
||||
resp.ErrorMsg(1, err.Error())
|
||||
return
|
||||
@@ -335,7 +335,7 @@ func AccountLoginWithEmailPassword(ctx *ActionContext[AccountLoginWithEmailPassw
|
||||
var customer *model.KillaraCustomer
|
||||
var err error
|
||||
|
||||
customer, err = model.Models.KillaraCustomerModel.GetCustomerByEmailForBackEnd(email)
|
||||
customer, err = model.Models.KillaraCustomerModel.GetCustomerByEmailForBackEnd(nil, email)
|
||||
if err != nil {
|
||||
return resp.ErrorErr(1, err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
package actions
|
||||
|
||||
import "github.com/iapologizewhenimwrong/Vestmore_GO/utils/basic"
|
||||
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
|
||||
@@ -13,7 +22,205 @@ import "github.com/iapologizewhenimwrong/Vestmore_GO/utils/basic"
|
||||
// token: string;
|
||||
func SecuritiesAccount(ctx *ActionContext[SecuritiesAccountParam]) (resp *basic.Response) {
|
||||
|
||||
return resp.Success()
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user