package model import ( "errors" "fmt" "strconv" "gorm.io/gorm" ) type KillaraCustomerTransactionModel struct { // fields ... 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 }