app-cloudep-trade-service/internal/model/transaction_model_gen.go

119 lines
4.4 KiB
Go
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Code generated by goctl. DO NOT EDIT.
package model
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
)
var (
transactionFieldNames = builder.RawFieldNames(&Transaction{})
transactionRows = strings.Join(transactionFieldNames, ",")
transactionRowsExpectAutoSet = strings.Join(stringx.Remove(transactionFieldNames, "`id`"), ",")
transactionRowsWithPlaceHolder = strings.Join(stringx.Remove(transactionFieldNames, "`id`"), "=?,") + "=?"
)
type (
transactionModel interface {
Insert(ctx context.Context, data *Transaction) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*Transaction, error)
FindOneByOrderIdType(ctx context.Context, orderId string, tp int64) (*Transaction, error)
Update(ctx context.Context, data *Transaction) error
Delete(ctx context.Context, id int64) error
}
defaultTransactionModel struct {
conn sqlx.SqlConn
table string
}
Transaction struct {
Id int64 `db:"id"` // 交易流水號
TxId string `db:"tx_id"` // 交易 ID
OrderId string `db:"order_id"` // 訂單 ID
Brand string `db:"brand"` // 品牌名稱
Uid string `db:"uid"` // 發起 UID
ToUid string `db:"to_uid"` // 接收方UID
Type int64 `db:"type"` // 交易類型: 1=提現, 2=充值
BusinessType int64 `db:"business_type"` // 業務類型
Currency string `db:"currency"` // 幣種
Amount float64 `db:"amount"` // 交易金額
Balance float64 `db:"balance"` // 交易後餘額
BeforeBalance float64 `db:"before_balance"` // 交易前餘額
Status int64 `db:"status"` // 結算狀態
CreateTime int64 `db:"create_time"` // 創建時間Unix 時間戳,毫秒)
}
)
func newTransactionModel(conn sqlx.SqlConn) *defaultTransactionModel {
return &defaultTransactionModel{
conn: conn,
table: "`transaction`",
}
}
func (m *defaultTransactionModel) withSession(session sqlx.Session) *defaultTransactionModel {
return &defaultTransactionModel{
conn: sqlx.NewSqlConnFromSession(session),
table: "`transaction`",
}
}
func (m *defaultTransactionModel) Delete(ctx context.Context, id int64) error {
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
_, err := m.conn.ExecCtx(ctx, query, id)
return err
}
func (m *defaultTransactionModel) FindOne(ctx context.Context, id int64) (*Transaction, error) {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", transactionRows, m.table)
var resp Transaction
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultTransactionModel) FindOneByOrderIdType(ctx context.Context, orderId string, tp int64) (*Transaction, error) {
var resp Transaction
query := fmt.Sprintf("select %s from %s where `order_id` = ? and `type` = ? limit 1", transactionRows, m.table)
err := m.conn.QueryRowCtx(ctx, &resp, query, orderId, tp)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultTransactionModel) Insert(ctx context.Context, data *Transaction) (sql.Result, error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, transactionRowsExpectAutoSet)
ret, err := m.conn.ExecCtx(ctx, query, data.TxId, data.OrderId, data.Brand, data.Uid, data.ToUid, data.Type, data.BusinessType, data.Currency, data.Amount, data.Balance, data.BeforeBalance, data.Status, data.CreateTime)
return ret, err
}
func (m *defaultTransactionModel) Update(ctx context.Context, newData *Transaction) error {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, transactionRowsWithPlaceHolder)
_, err := m.conn.ExecCtx(ctx, query, newData.TxId, newData.OrderId, newData.Brand, newData.Uid, newData.ToUid, newData.Type, newData.BusinessType, newData.Currency, newData.Amount, newData.Balance, newData.BeforeBalance, newData.Status, newData.CreateTime, newData.Id)
return err
}
func (m *defaultTransactionModel) tableName() string {
return m.table
}