更新对应交易的代码实现

This commit is contained in:
2024-04-16 18:00:05 +08:00
parent b2185f7784
commit 798f566542
18 changed files with 858 additions and 43 deletions

View File

@@ -9,3 +9,24 @@ type KillaraCustomerBalanceModel struct {
db *gorm.DB
TableName string // 表名
}
func (m *KillaraCustomerBalanceModel) SumFreeze(tx *gorm.DB, customerID uint64, market string, currencyID uint64) (float64, error) {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
var total float64
err := db.Model(&KillaraCustomerBalance{}).
Select("IFNULL(SUM(total), 0.000) AS total").
Where("customer_id = ? AND market = ? AND freeze = 1 AND currency_id = ?", customerID, market, currencyID).
Scan(&total).Error
if err != nil {
return 0, err
}
return total, nil
}

View File

@@ -9,3 +9,49 @@ type KillaraCustomerHoldModel struct {
db *gorm.DB
TableName string // 表名
}
func (m *KillaraCustomerHoldModel) GetHoldsForFrontEnd(tx *gorm.DB, data map[string]interface{}) ([]*KillaraCustomerHold, error) {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
var holds []*KillaraCustomerHold
query := db.Model(&KillaraCustomerHold{})
if customerID, ok := data["customer_id"].(uint64); ok && customerID > 0 {
query = query.Where("customer_id = ?", customerID)
}
if market, ok := data["market"].(string); ok && market != "" {
query = query.Where("market = ?", market)
}
if holdType, ok := data["type"].(uint64); ok && holdType > 0 {
query = query.Where("type = ?", holdType)
}
if status, ok := data["status"].(uint64); ok && status > 0 {
query = query.Where("status = ?", status)
}
query = query.Order("customer_hold_id DESC")
if start, ok := data["start"].(int); ok && start > 0 {
query = query.Offset(start)
}
if limit, ok := data["limit"].(int); ok && limit > 0 {
query = query.Limit(limit)
} else {
query = query.Limit(10)
}
err := query.Find(&holds).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return holds, err
}

View File

@@ -12,90 +12,163 @@ type KillaraCustomerModel struct {
TableName string // 表名
}
func (m *KillaraCustomerModel) InsertCustomer(customer *KillaraCustomer) (uint64, error) {
func (m *KillaraCustomerModel) InsertCustomer(tx *gorm.DB, customer *KillaraCustomer) (uint64, error) {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
if err := m.db.Model(&KillaraCustomer{}).Create(customer).Error; err != nil {
if err := db.Model(&KillaraCustomer{}).Create(customer).Error; err != nil {
return 0, err
}
return *customer.CustomerId, nil
}
func (m *KillaraCustomerModel) UpdateCustomer(customer *KillaraCustomer) error {
return m.db.Model(&KillaraCustomer{}).Where("customer_id = ?", customer.CustomerId).Updates(customer).Error
func (m *KillaraCustomerModel) UpdateCustomer(tx *gorm.DB, customer *KillaraCustomer) error {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
return db.Model(&KillaraCustomer{}).Where("customer_id = ?", customer.CustomerId).Updates(customer).Error
}
func (m *KillaraCustomerModel) DeleteCustomer(customer *KillaraCustomer) error {
return m.db.Where("customer_id = ?", customer.CustomerId).Delete(&KillaraCustomer{}).Error
func (m *KillaraCustomerModel) DeleteCustomer(tx *gorm.DB, customer *KillaraCustomer) error {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
return db.Where("customer_id = ?", customer.CustomerId).Delete(&KillaraCustomer{}).Error
}
func (m *KillaraCustomerModel) GetCustomer(customerID uint64) (*KillaraCustomer, error) {
func (m *KillaraCustomerModel) GetCustomer(tx *gorm.DB, customerID uint64) (*KillaraCustomer, error) {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
var customer KillaraCustomer
err := m.db.Where("customer_id = ? AND status = 1", customerID).First(&customer).Error
err := db.Where("customer_id = ? AND status = 1", customerID).First(&customer).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &customer, err
}
func (m *KillaraCustomerModel) CheckTelephoneExists(telephone string) (bool, error) {
// var count int64
// err := m.db.Model(&KillaraCustomer{}).Where("telephone = ?", telephone).Count(&count).Error
err := m.db.Model(&KillaraCustomer{}).Where("telephone = ?", telephone).First(nil).Error
func (m *KillaraCustomerModel) CheckTelephoneExists(tx *gorm.DB, telephone string) (bool, error) {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
err := db.Model(&KillaraCustomer{}).Where("telephone = ?", telephone).First(nil).Error
if err == gorm.ErrRecordNotFound {
return false, nil
}
return err != nil, err
}
func (m *KillaraCustomerModel) CheckEmailExists(email string) (bool, error) {
// var count int64
err := m.db.Model(&KillaraCustomer{}).Where("email = ?", email).First(nil).Error
func (m *KillaraCustomerModel) CheckEmailExists(tx *gorm.DB, email string) (bool, error) {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
err := db.Model(&KillaraCustomer{}).Where("email = ?", email).First(nil).Error
if err == gorm.ErrRecordNotFound {
return false, nil
}
return err != nil, err
}
func (m *KillaraCustomerModel) GetCustomerByCode(code string) (*KillaraCustomer, error) {
func (m *KillaraCustomerModel) GetCustomerByCode(tx *gorm.DB, code string) (*KillaraCustomer, error) {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
var customer KillaraCustomer
err := m.db.Where("code = ? AND status = 1", code).First(&customer).Error
err := db.Where("code = ? AND status = 1", code).First(&customer).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &customer, err
}
func (m *KillaraCustomerModel) GetCustomerByTelephone(telephone string) (*KillaraCustomer, error) {
func (m *KillaraCustomerModel) GetCustomerByTelephone(tx *gorm.DB, telephone string) (*KillaraCustomer, error) {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
var customer KillaraCustomer
err := m.db.Where("telephone = ? AND status = 1", telephone).Order("customer_id").First(&customer).Error
err := db.Where("telephone = ? AND status = 1", telephone).Order("customer_id").First(&customer).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &customer, err
}
func (m *KillaraCustomerModel) GetCustomerByTelephoneForBackEnd(telephone string) (*KillaraCustomer, error) {
func (m *KillaraCustomerModel) GetCustomerByTelephoneForBackEnd(tx *gorm.DB, telephone string) (*KillaraCustomer, error) {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
var customer KillaraCustomer
err := m.db.Where("telephone = ?", telephone).First(&customer).Error
err := db.Where("telephone = ?", telephone).First(&customer).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &customer, err
}
func (m *KillaraCustomerModel) GetCustomerByEmailForBackEnd(email string) (*KillaraCustomer, error) {
func (m *KillaraCustomerModel) GetCustomerByEmailForBackEnd(tx *gorm.DB, email string) (*KillaraCustomer, error) {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
var customer KillaraCustomer
err := m.db.Where("email = ?", email).First(&customer).Error
err := db.Where("email = ?", email).First(&customer).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &customer, err
}
func (m *KillaraCustomerModel) GetCustomerForBackEnd(customerID uint64) (*KillaraCustomer, error) {
func (m *KillaraCustomerModel) GetCustomerForBackEnd(tx *gorm.DB, customerID uint64) (*KillaraCustomer, error) {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
var customer KillaraCustomer
err := m.db.Where("customer_id = ?", customerID).First(&customer).Error
err := db.Where("customer_id = ?", customerID).First(&customer).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
@@ -327,7 +400,7 @@ func (m *KillaraCustomerModel) GetFullParents(parentID uint64) ([]*KillaraCustom
var parents []*KillaraCustomer
// 获取当前父级客户
parent, err := m.GetCustomer(parentID)
parent, err := m.GetCustomer(nil, parentID)
if err != nil {
return nil, err
}
@@ -349,7 +422,7 @@ func (m *KillaraCustomerModel) GetFullParents(parentID uint64) ([]*KillaraCustom
// 获取客户自身及所有上级
func (m *KillaraCustomerModel) GetFullParentsFromSelf(customerID uint64) ([]*KillaraCustomer, error) {
customer, err := m.GetCustomer(customerID)
customer, err := m.GetCustomer(nil, customerID)
if err != nil {
return nil, err
}
@@ -394,7 +467,7 @@ func (m *KillaraCustomerModel) GetFullChildren(parentID uint64) ([]*KillaraCusto
// 获取客户自身及所有下级
func (m *KillaraCustomerModel) GetFullChildrenWithSelf(customerID uint64) ([]*KillaraCustomer, error) {
customer, err := m.GetCustomer(customerID)
customer, err := m.GetCustomer(nil, customerID)
if err != nil {
return nil, err
}
@@ -411,3 +484,8 @@ func (m *KillaraCustomerModel) GetFullChildrenWithSelf(customerID uint64) ([]*Ki
return children, nil
}
// 用于客户的全局事物
func (m *KillaraCustomerModel) Transaction(do func(tx *gorm.DB) error) error {
return m.db.Transaction(do)
}

View File

@@ -86,9 +86,16 @@ func (m *KillaraCustomerTokenModel) DeleteToken(token string) error {
return err
}
func (m *KillaraCustomerTokenModel) GetToken(token string) (*KillaraCustomerToken, error) {
func (m *KillaraCustomerTokenModel) GetToken(tx *gorm.DB, token string) (*KillaraCustomerToken, error) {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
var tokenData KillaraCustomerToken
err := m.db.Where("token = ?", token).First(&tokenData).Error
err := db.Model(&tokenData).Where("token = ?", token).First(&tokenData).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}

View File

@@ -1,6 +1,10 @@
package model
import (
"errors"
"fmt"
"strconv"
"gorm.io/gorm"
)
@@ -9,3 +13,128 @@ type KillaraCustomerTransactionModel struct {
db *gorm.DB
TableName string // 表名
}
func (m *KillaraCustomerTransactionModel) GetHistoryProfit(data map[string]interface{}) (float64, error) {
var profitLoss float64
query := m.db.Model(&KillaraCustomerTransaction{}).
Select("SUM(profit_loss) AS profit_loss").
Where("type = 2 AND status = 3")
if customerID, ok := data["customer_id"].(uint64); ok && customerID > 0 {
query = query.Where("customer_id = ?", customerID)
}
if customerHoldID, ok := data["customer_hold_id"].(uint64); ok && customerHoldID > 0 {
query = query.Where("customer_hold_id = ?", customerHoldID)
}
if market, ok := data["market"].(string); ok && market != "" {
query = query.Where("market = ?", market)
}
if stockSymbol, ok := data["stock_symbol"].(string); ok && stockSymbol != "" {
query = query.Where("stock_symbol = ?", stockSymbol)
}
if orderType, ok := data["order_type"].(uint64); ok && orderType > 0 {
query = query.Where("order_type = ?", orderType)
}
if exchangeOrder, ok := data["exchange_order"].(uint64); ok {
query = query.Where("exchange_order = ?", exchangeOrder)
}
if exchangeStatus, ok := data["exchange_status"].(uint64); ok {
query = query.Where("exchange_status = ?", exchangeStatus)
}
err := query.Scan(&profitLoss).Error
if err != nil {
return 0, err
}
return profitLoss, nil
}
func (m *KillaraCustomerTransactionModel) GetTransactionsForFrontEnd(tx *gorm.DB, data map[string]interface{}) ([]*KillaraCustomerTransaction, error) {
var db *gorm.DB
if tx != nil {
db = tx
} else {
db = m.db
}
var transactions []*KillaraCustomerTransaction
query := db.Model(&KillaraCustomerTransaction{})
if customerID, ok := data["customer_id"]; ok && customerID != "" {
query = query.Where("customer_id = ?", customerID)
}
if customerHoldID, ok := data["customer_hold_id"]; ok && customerHoldID != "" {
query = query.Where("customer_hold_id = ?", customerHoldID)
}
if market, ok := data["market"]; ok && market != "" {
if markets, ok := market.([]string); ok {
query = query.Where("market IN (?)", markets)
} else {
query = query.Where("market = ?", market)
}
}
if stockSymbol, ok := data["stock_symbol"]; ok && stockSymbol != "" {
query = query.Where("stock_symbol = ?", stockSymbol)
}
if transType, ok := data["type"]; ok && transType != "" {
query = query.Where("type = ?", transType)
}
if orderType, ok := data["order_type"]; ok && orderType != "" {
query = query.Where("order_type = ?", orderType)
}
if status, ok := data["status"]; ok && status != "" {
query = query.Where("status = ?", status)
}
if exchangeOrder, ok := data["exchange_order"]; ok {
query = query.Where("exchange_order = ?", exchangeOrder)
}
if exchangeStatus, ok := data["exchange_status"]; ok {
query = query.Where("exchange_status = ?", exchangeStatus)
}
if sort, ok := data["sort"]; ok && sort != "" {
if order, ok := data["order"]; ok && order != "" {
query = query.Order(fmt.Sprintf("%s %s", sort, order))
} else {
query = query.Order(fmt.Sprintf("%s DESC", sort))
}
}
if start, ok := data["start"]; ok && start != "" {
startInt, _ := strconv.Atoi(start.(string))
if limit, ok := data["limit"]; ok && limit != "" {
limitInt, _ := strconv.Atoi(limit.(string))
query = query.Offset(startInt).Limit(limitInt)
} else {
query = query.Offset(startInt).Limit(10)
}
} else if limit, ok := data["limit"]; ok && limit != "" {
limitInt, _ := strconv.Atoi(limit.(string))
query = query.Limit(limitInt)
}
err := query.Find(&transactions).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
return transactions, nil
}

View File

@@ -1,6 +1,8 @@
package model
import (
"github.com/iapologizewhenimwrong/Vestmore_GO/proto"
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/constants"
"gorm.io/gorm"
)
@@ -9,3 +11,83 @@ type KillaraStockModel struct {
db *gorm.DB
TableName string // 表名
}
// , stockLocalHistoryID uint64, source string
func (m *KillaraStockModel) StockDetail(tx *gorm.DB, market string, stockType int, stockSymbol string) (map[string]interface{}, error) {
// var db *gorm.DB
// if tx != nil {
// db = tx
// } else {
// db = m.db
// }
trueStockSymbol := stockSymbol
// var localData *KillaraStockLocal
// if stockLocalHistoryID != 0 {
// localData = StockLocalHistoryMapLocalModel.Find(stockLocalHistoryID)
// } else {
// localData = StockLocalDao.FindSymbolWithTrashed(market, stockSymbol)
// }
// if localData != nil {
// trueStockSymbol = *localData.OriginStockSymbol
// }
// modelStockTool := &StockTool{}
marketForSearch := market
if market == "HK" {
marketForSearch = constants.HKEX
}
// data := modelStockTool.Info(fmt.Sprintf("%s|%d|%s", marketForSearch, stockType, trueStockSymbol))
data, err := proto.StockDetail(marketForSearch, stockType, trueStockSymbol)
if err != nil {
return nil, err
}
data["symbol_empty"] = data["symbol"] == nil // 空
data["symbol"] = stockSymbol // 覆盖
data["true_symbol"] = trueStockSymbol // 真实
// if localData != nil && !localData.DeleteDate.IsZero() {
// // 这个已经删除了
// data["local_delete_date"] = localData.DeleteDate
// }
// 空股票也会返回数据
if data["symbol_empty"].(bool) {
return data, nil
}
// if localData != nil {
// if source == "app" {
// data["name"] = localData.LanguageAppName
// } else {
// data["name"] = localData.LanguageAdminName
// }
// data["origin_price"] = data["price"]
// price, _ := decimal.NewFromString(fmt.Sprintf("%v", data["price"]))
// data["price"] = StockLocalLogic.CalPrice(price, localData.Diff, market).String()
// if localData.Stop == StockLocalModel.StopNo {
// data["securityStatus"] = 1
// } else {
// data["securityStatus"] = 3
// }
// data["msgs"] = localData.GroupNum
// data["local_stock_symbol"] = localData.StockSymbol
// data["local_origin_stock_symbol"] = localData.OriginStockSymbol
// data["local_stock_market"] = localData.Market
// data["stock_local_id"] = localData.ID
// data["stock_local_diff"] = localData.Diff
// data["stock_group_num"] = localData.GroupNum
// data["stock_name_json"] = localData.StockNameJSON
// }
return data, nil
}