150 lines
5.7 KiB
Go
150 lines
5.7 KiB
Go
// Code generated by goctl. DO NOT EDIT.
|
|
// versions:
|
|
// goctl version: 1.7.2
|
|
|
|
package model
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/builder"
|
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
|
)
|
|
|
|
var (
|
|
deductionsFieldNames = builder.RawFieldNames(&Deductions{})
|
|
deductionsRows = strings.Join(deductionsFieldNames, ",")
|
|
deductionsRowsExpectAutoSet = strings.Join(stringx.Remove(deductionsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
|
deductionsRowsWithPlaceHolder = strings.Join(stringx.Remove(deductionsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
|
|
|
cacheDeductionsIdPrefix = "cache:deductions:id:"
|
|
cacheDeductionsTransactionIdPrefix = "cache:deductions:transactionId:"
|
|
)
|
|
|
|
type (
|
|
deductionsModel interface {
|
|
Insert(ctx context.Context, data *Deductions) (sql.Result, error)
|
|
FindOne(ctx context.Context, id int64) (*Deductions, error)
|
|
FindOneByTransactionId(ctx context.Context, transactionId string) (*Deductions, error)
|
|
Update(ctx context.Context, data *Deductions) error
|
|
Delete(ctx context.Context, id int64) error
|
|
}
|
|
|
|
defaultDeductionsModel struct {
|
|
sqlc.CachedConn
|
|
table string
|
|
}
|
|
|
|
Deductions struct {
|
|
Id int64 `db:"id"` // 扣款记录ID
|
|
UserId int64 `db:"user_id"` // 用户ID
|
|
Amount float64 `db:"amount"` // 扣款金额
|
|
TransactionId string `db:"transaction_id"` // 交易流水号
|
|
CreatedAt time.Time `db:"created_at"` // 扣款时间
|
|
}
|
|
)
|
|
|
|
func newDeductionsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultDeductionsModel {
|
|
return &defaultDeductionsModel{
|
|
CachedConn: sqlc.NewConn(conn, c, opts...),
|
|
table: "`deductions`",
|
|
}
|
|
}
|
|
|
|
func (m *defaultDeductionsModel) Delete(ctx context.Context, id int64) error {
|
|
data, err := m.FindOne(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
deductionsIdKey := fmt.Sprintf("%s%v", cacheDeductionsIdPrefix, id)
|
|
deductionsTransactionIdKey := fmt.Sprintf("%s%v", cacheDeductionsTransactionIdPrefix, data.TransactionId)
|
|
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
|
return conn.ExecCtx(ctx, query, id)
|
|
}, deductionsIdKey, deductionsTransactionIdKey)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultDeductionsModel) FindOne(ctx context.Context, id int64) (*Deductions, error) {
|
|
deductionsIdKey := fmt.Sprintf("%s%v", cacheDeductionsIdPrefix, id)
|
|
var resp Deductions
|
|
err := m.QueryRowCtx(ctx, &resp, deductionsIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", deductionsRows, m.table)
|
|
return conn.QueryRowCtx(ctx, v, query, id)
|
|
})
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlc.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (m *defaultDeductionsModel) FindOneByTransactionId(ctx context.Context, transactionId string) (*Deductions, error) {
|
|
deductionsTransactionIdKey := fmt.Sprintf("%s%v", cacheDeductionsTransactionIdPrefix, transactionId)
|
|
var resp Deductions
|
|
err := m.QueryRowIndexCtx(ctx, &resp, deductionsTransactionIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
|
query := fmt.Sprintf("select %s from %s where `transaction_id` = ? limit 1", deductionsRows, m.table)
|
|
if err := conn.QueryRowCtx(ctx, &resp, query, transactionId); err != nil {
|
|
return nil, err
|
|
}
|
|
return resp.Id, nil
|
|
}, m.queryPrimary)
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlc.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (m *defaultDeductionsModel) Insert(ctx context.Context, data *Deductions) (sql.Result, error) {
|
|
deductionsIdKey := fmt.Sprintf("%s%v", cacheDeductionsIdPrefix, data.Id)
|
|
deductionsTransactionIdKey := fmt.Sprintf("%s%v", cacheDeductionsTransactionIdPrefix, data.TransactionId)
|
|
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?)", m.table, deductionsRowsExpectAutoSet)
|
|
return conn.ExecCtx(ctx, query, data.UserId, data.Amount, data.TransactionId)
|
|
}, deductionsIdKey, deductionsTransactionIdKey)
|
|
return ret, err
|
|
}
|
|
|
|
func (m *defaultDeductionsModel) Update(ctx context.Context, newData *Deductions) error {
|
|
data, err := m.FindOne(ctx, newData.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
deductionsIdKey := fmt.Sprintf("%s%v", cacheDeductionsIdPrefix, data.Id)
|
|
deductionsTransactionIdKey := fmt.Sprintf("%s%v", cacheDeductionsTransactionIdPrefix, data.TransactionId)
|
|
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, deductionsRowsWithPlaceHolder)
|
|
return conn.ExecCtx(ctx, query, newData.UserId, newData.Amount, newData.TransactionId, newData.Id)
|
|
}, deductionsIdKey, deductionsTransactionIdKey)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultDeductionsModel) formatPrimary(primary any) string {
|
|
return fmt.Sprintf("%s%v", cacheDeductionsIdPrefix, primary)
|
|
}
|
|
|
|
func (m *defaultDeductionsModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", deductionsRows, m.table)
|
|
return conn.QueryRowCtx(ctx, v, query, primary)
|
|
}
|
|
|
|
func (m *defaultDeductionsModel) tableName() string {
|
|
return m.table
|
|
}
|