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