temp
This commit is contained in:
27
apps/user/internal/model/apirequestsmodel.go
Normal file
27
apps/user/internal/model/apirequestsmodel.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ ApiRequestsModel = (*customApiRequestsModel)(nil)
|
||||
|
||||
type (
|
||||
// ApiRequestsModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customApiRequestsModel.
|
||||
ApiRequestsModel interface {
|
||||
apiRequestsModel
|
||||
}
|
||||
|
||||
customApiRequestsModel struct {
|
||||
*defaultApiRequestsModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewApiRequestsModel returns a model for the database table.
|
||||
func NewApiRequestsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) ApiRequestsModel {
|
||||
return &customApiRequestsModel{
|
||||
defaultApiRequestsModel: newApiRequestsModel(conn, c, opts...),
|
||||
}
|
||||
}
|
||||
152
apps/user/internal/model/apirequestsmodel_gen.go
Normal file
152
apps/user/internal/model/apirequestsmodel_gen.go
Normal file
@@ -0,0 +1,152 @@
|
||||
// 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 (
|
||||
apiRequestsFieldNames = builder.RawFieldNames(&ApiRequests{})
|
||||
apiRequestsRows = strings.Join(apiRequestsFieldNames, ",")
|
||||
apiRequestsRowsExpectAutoSet = strings.Join(stringx.Remove(apiRequestsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
apiRequestsRowsWithPlaceHolder = strings.Join(stringx.Remove(apiRequestsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
|
||||
cacheApiRequestsIdPrefix = "cache:apiRequests:id:"
|
||||
cacheApiRequestsTransactionIdPrefix = "cache:apiRequests:transactionId:"
|
||||
)
|
||||
|
||||
type (
|
||||
apiRequestsModel interface {
|
||||
Insert(ctx context.Context, data *ApiRequests) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*ApiRequests, error)
|
||||
FindOneByTransactionId(ctx context.Context, transactionId string) (*ApiRequests, error)
|
||||
Update(ctx context.Context, data *ApiRequests) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultApiRequestsModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
ApiRequests struct {
|
||||
Id int64 `db:"id"` // 请求记录ID
|
||||
TransactionId string `db:"transaction_id"` // 交易ID
|
||||
UserId int64 `db:"user_id"` // 用户ID
|
||||
ProductCode string `db:"product_code"` // 产品编码
|
||||
Status string `db:"status"` // 请求状态:success=成功,failed=失败
|
||||
Charges int64 `db:"charges"` // 是否需要付费
|
||||
Remark sql.NullString `db:"remark"` // 备注
|
||||
Timestamp time.Time `db:"timestamp"` // 请求时间
|
||||
}
|
||||
)
|
||||
|
||||
func newApiRequestsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultApiRequestsModel {
|
||||
return &defaultApiRequestsModel{
|
||||
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||||
table: "`api_requests`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultApiRequestsModel) Delete(ctx context.Context, id int64) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
apiRequestsIdKey := fmt.Sprintf("%s%v", cacheApiRequestsIdPrefix, id)
|
||||
apiRequestsTransactionIdKey := fmt.Sprintf("%s%v", cacheApiRequestsTransactionIdPrefix, 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)
|
||||
}, apiRequestsIdKey, apiRequestsTransactionIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultApiRequestsModel) FindOne(ctx context.Context, id int64) (*ApiRequests, error) {
|
||||
apiRequestsIdKey := fmt.Sprintf("%s%v", cacheApiRequestsIdPrefix, id)
|
||||
var resp ApiRequests
|
||||
err := m.QueryRowCtx(ctx, &resp, apiRequestsIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", apiRequestsRows, 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 *defaultApiRequestsModel) FindOneByTransactionId(ctx context.Context, transactionId string) (*ApiRequests, error) {
|
||||
apiRequestsTransactionIdKey := fmt.Sprintf("%s%v", cacheApiRequestsTransactionIdPrefix, transactionId)
|
||||
var resp ApiRequests
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, apiRequestsTransactionIdKey, 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", apiRequestsRows, 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 *defaultApiRequestsModel) Insert(ctx context.Context, data *ApiRequests) (sql.Result, error) {
|
||||
apiRequestsIdKey := fmt.Sprintf("%s%v", cacheApiRequestsIdPrefix, data.Id)
|
||||
apiRequestsTransactionIdKey := fmt.Sprintf("%s%v", cacheApiRequestsTransactionIdPrefix, 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, apiRequestsRowsExpectAutoSet)
|
||||
return conn.ExecCtx(ctx, query, data.TransactionId, data.UserId, data.ProductCode, data.Status, data.Charges, data.Remark, data.Timestamp)
|
||||
}, apiRequestsIdKey, apiRequestsTransactionIdKey)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultApiRequestsModel) Update(ctx context.Context, newData *ApiRequests) error {
|
||||
data, err := m.FindOne(ctx, newData.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
apiRequestsIdKey := fmt.Sprintf("%s%v", cacheApiRequestsIdPrefix, data.Id)
|
||||
apiRequestsTransactionIdKey := fmt.Sprintf("%s%v", cacheApiRequestsTransactionIdPrefix, 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, apiRequestsRowsWithPlaceHolder)
|
||||
return conn.ExecCtx(ctx, query, newData.TransactionId, newData.UserId, newData.ProductCode, newData.Status, newData.Charges, newData.Remark, newData.Timestamp, newData.Id)
|
||||
}, apiRequestsIdKey, apiRequestsTransactionIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultApiRequestsModel) formatPrimary(primary any) string {
|
||||
return fmt.Sprintf("%s%v", cacheApiRequestsIdPrefix, primary)
|
||||
}
|
||||
|
||||
func (m *defaultApiRequestsModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", apiRequestsRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, primary)
|
||||
}
|
||||
|
||||
func (m *defaultApiRequestsModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
52
apps/user/internal/model/deductionsmodel.go
Normal file
52
apps/user/internal/model/deductionsmodel.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ DeductionsModel = (*customDeductionsModel)(nil)
|
||||
|
||||
type (
|
||||
// DeductionsModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customDeductionsModel.
|
||||
DeductionsModel interface {
|
||||
deductionsModel
|
||||
InsertDeductionsTrans(ctx context.Context, deductions *Deductions, session sqlx.Session) (sql.Result, error)
|
||||
}
|
||||
|
||||
customDeductionsModel struct {
|
||||
*defaultDeductionsModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewDeductionsModel returns a model for the database table.
|
||||
func NewDeductionsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) DeductionsModel {
|
||||
return &customDeductionsModel{
|
||||
defaultDeductionsModel: newDeductionsModel(conn, c, opts...),
|
||||
}
|
||||
}
|
||||
func (m *customDeductionsModel) InsertDeductionsTrans(ctx context.Context, deductions *Deductions, session sqlx.Session) (sql.Result, error) {
|
||||
|
||||
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?, ?, ?)", m.table, deductionsRowsExpectAutoSet)
|
||||
ret, err := session.ExecCtx(ctx, query, deductions.UserId, deductions.Amount, deductions.TransactionId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deductionsTransactionIdKey := fmt.Sprintf("%s%v", cacheDeductionsTransactionIdPrefix, deductions.TransactionId)
|
||||
// 2. 更新缓存,保证所有缓存操作成功
|
||||
cacheKeys := []string{deductionsTransactionIdKey}
|
||||
cacheErrors := make([]error, len(cacheKeys))
|
||||
|
||||
cacheErrors[0] = m.DelCacheCtx(ctx, deductionsTransactionIdKey)
|
||||
// 3. 检查缓存操作是否全部成功
|
||||
for _, cacheErr := range cacheErrors {
|
||||
if cacheErr != nil {
|
||||
return nil, cacheErr // 返回第一个缓存更新失败的错误
|
||||
}
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
149
apps/user/internal/model/deductionsmodel_gen.go
Normal file
149
apps/user/internal/model/deductionsmodel_gen.go
Normal file
@@ -0,0 +1,149 @@
|
||||
// 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
|
||||
}
|
||||
@@ -45,13 +45,16 @@ type (
|
||||
}
|
||||
|
||||
Users struct {
|
||||
Id int64 `db:"id"` // 用户ID
|
||||
Username string `db:"username"` // 用户名
|
||||
Password string `db:"password"` // 用户密码
|
||||
Phone string `db:"phone"` // 用户手机号
|
||||
AuthStatus string `db:"auth_status"` // 认证状态:unverified=未提交,pending=待审核,approved=审核通过,rejected=审核拒绝
|
||||
CreatedAt time.Time `db:"created_at"` // 用户创建时间
|
||||
UpdatedAt time.Time `db:"updated_at"` // 用户更新时间
|
||||
Id int64 `db:"id"` // 用户ID
|
||||
Username string `db:"username"` // 用户名
|
||||
Password string `db:"password"` // 用户密码
|
||||
Phone string `db:"phone"` // 用户手机号
|
||||
AuthStatus string `db:"auth_status"` // 认证状态:unverified=未提交,pending=待审核,approved=审核通过,rejected=审核拒绝
|
||||
Disable int64 `db:"disable"` // 是否禁用,0=未禁用,1=禁用
|
||||
Internal int64 `db:"internal"` // 是否内部人员,0=否,1=是
|
||||
QuotaExceeded int64 `db:"quota_exceeded"` // 是否额度用完,0=否,1=是
|
||||
CreatedAt time.Time `db:"created_at"` // 用户创建时间
|
||||
UpdatedAt time.Time `db:"updated_at"` // 用户更新时间
|
||||
}
|
||||
)
|
||||
|
||||
@@ -140,8 +143,8 @@ func (m *defaultUsersModel) Insert(ctx context.Context, data *Users) (sql.Result
|
||||
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
|
||||
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, data.Username)
|
||||
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, usersRowsExpectAutoSet)
|
||||
return conn.ExecCtx(ctx, query, data.Username, data.Password, data.Phone, data.AuthStatus)
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, usersRowsExpectAutoSet)
|
||||
return conn.ExecCtx(ctx, query, data.Username, data.Password, data.Phone, data.AuthStatus, data.Disable, data.Internal, data.QuotaExceeded)
|
||||
}, usersIdKey, usersPhoneKey, usersUsernameKey)
|
||||
return ret, err
|
||||
}
|
||||
@@ -157,7 +160,7 @@ func (m *defaultUsersModel) Update(ctx context.Context, newData *Users) error {
|
||||
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, data.Username)
|
||||
_, 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, usersRowsWithPlaceHolder)
|
||||
return conn.ExecCtx(ctx, query, newData.Username, newData.Password, newData.Phone, newData.AuthStatus, newData.Id)
|
||||
return conn.ExecCtx(ctx, query, newData.Username, newData.Password, newData.Phone, newData.AuthStatus, newData.Disable, newData.Internal, newData.QuotaExceeded, newData.Id)
|
||||
}, usersIdKey, usersPhoneKey, usersUsernameKey)
|
||||
return err
|
||||
}
|
||||
|
||||
89
apps/user/internal/model/walletsmodel.go
Normal file
89
apps/user/internal/model/walletsmodel.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ WalletsModel = (*customWalletsModel)(nil)
|
||||
|
||||
type (
|
||||
// WalletsModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customWalletsModel.
|
||||
WalletsModel interface {
|
||||
walletsModel
|
||||
InsertWalletsTrans(ctx context.Context, wallets *Wallets, session sqlx.Session) (sql.Result, error)
|
||||
UpdateBalance(session sqlx.Session, ctx context.Context, userId int64, amount float64) error
|
||||
TransCtx(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error
|
||||
}
|
||||
|
||||
customWalletsModel struct {
|
||||
*defaultWalletsModel
|
||||
}
|
||||
)
|
||||
|
||||
var ErrBalanceNotEnough = errors.New("余额不足")
|
||||
var ErrVersionMismatch = errors.New("版本号不匹配,请重试")
|
||||
|
||||
// NewWalletsModel returns a model for the database table.
|
||||
func NewWalletsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) WalletsModel {
|
||||
return &customWalletsModel{
|
||||
defaultWalletsModel: newWalletsModel(conn, c, opts...),
|
||||
}
|
||||
}
|
||||
func (m *customWalletsModel) TransCtx(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
|
||||
// 使用带 ctx 的事务处理
|
||||
err := m.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
return fn(ctx, session)
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新余额的方法
|
||||
func (m *customWalletsModel) UpdateBalance(session sqlx.Session, ctx context.Context, userId int64, amount float64) error {
|
||||
|
||||
wallet, err := m.FindOneByUserId(ctx, userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 检查余额是否足够
|
||||
if wallet.Balance+amount < 0 {
|
||||
return ErrBalanceNotEnough
|
||||
}
|
||||
|
||||
// 使用乐观锁更新余额
|
||||
result, err := session.Exec("UPDATE wallets SET balance = balance + ?, version = version + 1 WHERE user_id = ? AND version = ?", amount, userId, wallet.Version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 检查影响的行数,确保更新成功
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return ErrVersionMismatch
|
||||
}
|
||||
|
||||
walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, userId)
|
||||
cacheErrors := m.DelCacheCtx(ctx, walletsUserIdKey)
|
||||
if cacheErrors != nil {
|
||||
return cacheErrors
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (m *customWalletsModel) InsertWalletsTrans(ctx context.Context, wallets *Wallets, session sqlx.Session) (sql.Result, error) {
|
||||
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?, ?, ?)", m.table, walletsRowsExpectAutoSet)
|
||||
ret, err := session.ExecCtx(ctx, query, wallets.UserId, wallets.Balance, wallets.Version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ret, err
|
||||
}
|
||||
150
apps/user/internal/model/walletsmodel_gen.go
Normal file
150
apps/user/internal/model/walletsmodel_gen.go
Normal file
@@ -0,0 +1,150 @@
|
||||
// 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 (
|
||||
walletsFieldNames = builder.RawFieldNames(&Wallets{})
|
||||
walletsRows = strings.Join(walletsFieldNames, ",")
|
||||
walletsRowsExpectAutoSet = strings.Join(stringx.Remove(walletsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
walletsRowsWithPlaceHolder = strings.Join(stringx.Remove(walletsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
|
||||
cacheWalletsIdPrefix = "cache:wallets:id:"
|
||||
cacheWalletsUserIdPrefix = "cache:wallets:userId:"
|
||||
)
|
||||
|
||||
type (
|
||||
walletsModel interface {
|
||||
Insert(ctx context.Context, data *Wallets) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*Wallets, error)
|
||||
FindOneByUserId(ctx context.Context, userId int64) (*Wallets, error)
|
||||
Update(ctx context.Context, data *Wallets) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultWalletsModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
Wallets struct {
|
||||
Id int64 `db:"id"` // 钱包ID
|
||||
UserId int64 `db:"user_id"` // 用户ID
|
||||
Balance float64 `db:"balance"` // 钱包余额
|
||||
Version int64 `db:"version"` // 乐观锁版本号
|
||||
CreatedAt time.Time `db:"created_at"` // 创建时间
|
||||
UpdatedAt time.Time `db:"updated_at"` // 更新时间
|
||||
}
|
||||
)
|
||||
|
||||
func newWalletsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultWalletsModel {
|
||||
return &defaultWalletsModel{
|
||||
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||||
table: "`wallets`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultWalletsModel) Delete(ctx context.Context, id int64) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
walletsIdKey := fmt.Sprintf("%s%v", cacheWalletsIdPrefix, id)
|
||||
walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, data.UserId)
|
||||
_, 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)
|
||||
}, walletsIdKey, walletsUserIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultWalletsModel) FindOne(ctx context.Context, id int64) (*Wallets, error) {
|
||||
walletsIdKey := fmt.Sprintf("%s%v", cacheWalletsIdPrefix, id)
|
||||
var resp Wallets
|
||||
err := m.QueryRowCtx(ctx, &resp, walletsIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", walletsRows, 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 *defaultWalletsModel) FindOneByUserId(ctx context.Context, userId int64) (*Wallets, error) {
|
||||
walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, userId)
|
||||
var resp Wallets
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, walletsUserIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `user_id` = ? limit 1", walletsRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, userId); 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 *defaultWalletsModel) Insert(ctx context.Context, data *Wallets) (sql.Result, error) {
|
||||
walletsIdKey := fmt.Sprintf("%s%v", cacheWalletsIdPrefix, data.Id)
|
||||
walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, data.UserId)
|
||||
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, walletsRowsExpectAutoSet)
|
||||
return conn.ExecCtx(ctx, query, data.UserId, data.Balance, data.Version)
|
||||
}, walletsIdKey, walletsUserIdKey)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultWalletsModel) Update(ctx context.Context, newData *Wallets) error {
|
||||
data, err := m.FindOne(ctx, newData.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
walletsIdKey := fmt.Sprintf("%s%v", cacheWalletsIdPrefix, data.Id)
|
||||
walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, data.UserId)
|
||||
_, 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, walletsRowsWithPlaceHolder)
|
||||
return conn.ExecCtx(ctx, query, newData.UserId, newData.Balance, newData.Version, newData.Id)
|
||||
}, walletsIdKey, walletsUserIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultWalletsModel) formatPrimary(primary any) string {
|
||||
return fmt.Sprintf("%s%v", cacheWalletsIdPrefix, primary)
|
||||
}
|
||||
|
||||
func (m *defaultWalletsModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", walletsRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, primary)
|
||||
}
|
||||
|
||||
func (m *defaultWalletsModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
Reference in New Issue
Block a user