v1.1
This commit is contained in:
@@ -1,24 +1,26 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"ycc-server/common/globalkey"
|
||||
"fmt"
|
||||
"time"
|
||||
"context"
|
||||
"database/sql"
|
||||
"ycc-server/common/globalkey"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ QueryModel = (*customQueryModel)(nil)
|
||||
|
||||
type (
|
||||
// QueryModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customQueryModel.
|
||||
QueryModel interface {
|
||||
queryModel
|
||||
DeleteBefore(ctx context.Context, before time.Time) (int64, error)
|
||||
}
|
||||
// QueryModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customQueryModel.
|
||||
QueryModel interface {
|
||||
queryModel
|
||||
DeleteBefore(ctx context.Context, before time.Time) (int64, error)
|
||||
UpdateUserIDWithSession(ctx context.Context, session sqlx.Session, sourceUserID, targetUserID string) error
|
||||
}
|
||||
|
||||
customQueryModel struct {
|
||||
*defaultQueryModel
|
||||
@@ -27,9 +29,9 @@ type (
|
||||
|
||||
// NewQueryModel returns a model for the database table.
|
||||
func NewQueryModel(conn sqlx.SqlConn, c cache.CacheConf) QueryModel {
|
||||
return &customQueryModel{
|
||||
defaultQueryModel: newQueryModel(conn, c),
|
||||
}
|
||||
return &customQueryModel{
|
||||
defaultQueryModel: newQueryModel(conn, c),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customQueryModel) DeleteBefore(ctx context.Context, before time.Time) (int64, error) {
|
||||
@@ -58,3 +60,26 @@ func (m *customQueryModel) DeleteBefore(ctx context.Context, before time.Time) (
|
||||
|
||||
return affected, nil
|
||||
}
|
||||
|
||||
func (m *customQueryModel) UpdateUserIDWithSession(ctx context.Context, session sqlx.Session, sourceUserID, targetUserID string) error {
|
||||
builder := m.defaultQueryModel.SelectBuilder().Where("user_id = ?", sourceUserID)
|
||||
rows, err := m.defaultQueryModel.FindAll(ctx, builder, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
keys := make([]string, 0, len(rows)*2)
|
||||
for _, r := range rows {
|
||||
keys = append(keys, fmt.Sprintf("%s%v", cacheYccQueryIdPrefix, r.Id))
|
||||
keys = append(keys, fmt.Sprintf("%s%v", cacheYccQueryOrderIdPrefix, r.OrderId))
|
||||
}
|
||||
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("UPDATE %s SET user_id = ? WHERE user_id = ?", m.defaultQueryModel.tableName())
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, targetUserID, sourceUserID)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, targetUserID, sourceUserID)
|
||||
}, keys...)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user