Files
novatask/internal/model/nh_castile_token_log_model.go
2025-04-28 20:03:37 +08:00

61 lines
2.1 KiB
Go

package model
import (
"context"
"errors"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ NhCastileTokenLogModel = (*customNhCastileTokenLogModel)(nil)
type (
// NhCastileTokenLogModel is an interface to be customized, add more methods here,
// and implement the added methods in customNhCastileTokenLogModel.
NhCastileTokenLogModel interface {
nhCastileTokenLogModel
WithSession(session sqlx.Session) NhCastileTokenLogModel
UpdateStatus(ctx context.Context, status uint, id uint) error
List(ctx context.Context, uid uint, roleId uint64, page, pageSize int) ([]*NhCastileTokenLog, error)
}
customNhCastileTokenLogModel struct {
*defaultNhCastileTokenLogModel
}
)
// NewNhCastileTokenLogModel returns a model for the database table.
func NewNhCastileTokenLogModel(conn sqlx.SqlConn) NhCastileTokenLogModel {
return &customNhCastileTokenLogModel{
defaultNhCastileTokenLogModel: newNhCastileTokenLogModel(conn),
}
}
func (m *customNhCastileTokenLogModel) WithSession(session sqlx.Session) NhCastileTokenLogModel {
return NewNhCastileTokenLogModel(sqlx.NewSqlConnFromSession(session))
}
func (m *customNhCastileTokenLogModel) UpdateStatus(ctx context.Context, status uint, id uint) error {
query := fmt.Sprintf("update %s set `callback_status` = ? where `id` = ?", m.table)
_, err := m.conn.ExecCtx(ctx, query, status, id)
return err
}
func (m *customNhCastileTokenLogModel) List(ctx context.Context, uid uint, roleId uint64, page, pageSize int) ([]*NhCastileTokenLog, error) {
var query string
var result []*NhCastileTokenLog
var err error
if roleId == 0 {
query = fmt.Sprintf("select %s from %s where uid = ? order by id desc limit ?, ?", nhCastileTokenLogRows, m.table)
err = m.conn.QueryRowsCtx(ctx, &result, query, uid, (page-1)*pageSize, pageSize)
} else {
query = fmt.Sprintf("select %s from %s where uid = ? and role_id = ? order by id desc limit ?, ?", nhCastileTokenLogRows, m.table)
err = m.conn.QueryRowsCtx(ctx, &result, query, uid, roleId, (page-1)*pageSize, pageSize)
}
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
return nil, err
}
return result, nil
}