Files
novatask/internal/model/nh_castile_token_log_model.go
2025-05-07 14:36:19 +08:00

76 lines
2.3 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, int64, 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, 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 []*NhCastileTokenLog
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, fmt.Sprintf(query, nhCastileTokenLogRows, m.table), args...)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
return nil, 0, err
}
return result, count, nil
}