69 lines
1.9 KiB
Go
Executable File
69 lines
1.9 KiB
Go
Executable File
package model
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
)
|
|
|
|
var _ NhStakePointsLogModel = (*customNhStakePointsLogModel)(nil)
|
|
|
|
type (
|
|
// NhStakePointsLogModel is an interface to be customized, add more methods here,
|
|
// and implement the added methods in customNhStakePointsLogModel.
|
|
NhStakePointsLogModel interface {
|
|
nhStakePointsLogModel
|
|
WithSession(session sqlx.Session) NhStakePointsLogModel
|
|
List(ctx context.Context, uid uint, roleId uint64, page, pageSize int) ([]*NhStakePointsLog, int64, error)
|
|
}
|
|
|
|
customNhStakePointsLogModel struct {
|
|
*defaultNhStakePointsLogModel
|
|
}
|
|
)
|
|
|
|
func (m *customNhStakePointsLogModel) List(ctx context.Context, uid uint, roleId uint64, page, pageSize int) ([]*NhStakePointsLog, int64, error) {
|
|
var query string
|
|
var args = []any{uid}
|
|
if roleId == 0 {
|
|
query = "select %s from %s where uid = ?"
|
|
} else {
|
|
query = "select %s from %s where uid = ? and role_id = ?"
|
|
args = append(args, roleId)
|
|
}
|
|
var err error
|
|
var count int64
|
|
var result []*NhStakePointsLog
|
|
err = m.conn.QueryRowCtx(ctx, &count, fmt.Sprintf(query, "count(*) as count", m.table), args...)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (page - 1) * pageSize
|
|
if offset >= int(count) {
|
|
return result, count, nil
|
|
}
|
|
|
|
query += " order by id desc limit ?, ?"
|
|
args = append(args, offset, pageSize)
|
|
|
|
err = m.conn.QueryRowsCtx(ctx, &result, query, args...)
|
|
|
|
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
|
|
return nil, count, err
|
|
}
|
|
return result, count, nil
|
|
}
|
|
|
|
// NewNhStakePointsLogModel returns a model for the database table.
|
|
func NewNhStakePointsLogModel(conn sqlx.SqlConn) NhStakePointsLogModel {
|
|
return &customNhStakePointsLogModel{
|
|
defaultNhStakePointsLogModel: newNhStakePointsLogModel(conn),
|
|
}
|
|
}
|
|
|
|
func (m *customNhStakePointsLogModel) WithSession(session sqlx.Session) NhStakePointsLogModel {
|
|
return NewNhStakePointsLogModel(sqlx.NewSqlConnFromSession(session))
|
|
}
|