f
This commit is contained in:
85
app/main/model/queryModel.go
Normal file
85
app/main/model/queryModel.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"qnc-server/common/globalkey"
|
||||
"time"
|
||||
|
||||
"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)
|
||||
UpdateUserIDWithSession(ctx context.Context, session sqlx.Session, sourceUserID, targetUserID string) error
|
||||
}
|
||||
|
||||
customQueryModel struct {
|
||||
*defaultQueryModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewQueryModel returns a model for the database table.
|
||||
func NewQueryModel(conn sqlx.SqlConn, c cache.CacheConf) QueryModel {
|
||||
return &customQueryModel{
|
||||
defaultQueryModel: newQueryModel(conn, c),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customQueryModel) DeleteBefore(ctx context.Context, before time.Time) (int64, error) {
|
||||
var affected int64 = 0
|
||||
|
||||
// 使用事务处理批量删除
|
||||
err := m.defaultQueryModel.Trans(ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
query := fmt.Sprintf("DELETE FROM %s WHERE create_time < ? AND del_state = ?", m.defaultQueryModel.table)
|
||||
result, err := session.ExecCtx(ctx, query, before.Format("2006-01-02 15:04:05"), globalkey.DelStateNo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rows, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
affected = rows
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
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", cacheQncQueryIdPrefix, r.Id))
|
||||
keys = append(keys, fmt.Sprintf("%s%v", cacheQncQueryOrderIdPrefix, 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