Files
novatask/internal/model/nh_game_server_notify_model.go
2025-04-03 16:17:20 +08:00

81 lines
2.6 KiB
Go
Executable File

package model
import (
"context"
"errors"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ NhGameServerNotifyModel = (*customNhGameServerNotifyModel)(nil)
type (
// NhGameServerNotifyModel is an interface to be customized, add more methods here,
// and implement the added methods in customNhGameServerNotifyModel.
NhGameServerNotifyModel interface {
nhGameServerNotifyModel
withSession(session sqlx.Session) NhGameServerNotifyModel
FindNotifyList(ctx context.Context, status int8) ([]*NhGameServerNotify, error)
UpdateNotifyStatus(ctx context.Context, id uint, status int8) error
}
customNhGameServerNotifyModel struct {
*defaultNhGameServerNotifyModel
}
)
const (
NTF_STATUS_UNKNOWN = 0 // 未知
NTF_STATUS_WAITING_PROCESS = 1 // 待处理
NTF_STATUS_PROCESSING = 2 // 处理中
NTF_STATUS_FAILED = 3 // 处理失败
NTF_STATUS_SUCCESS = 4 // 处理成功
)
func (m *customNhGameServerNotifyModel) UpdateNotifyStatus(ctx context.Context, id uint, status int8) error {
var update string
switch status {
case NTF_STATUS_PROCESSING:
update = fmt.Sprintf("update %s set `status` = 2, `retry_times` = `retry_times` + 1 where `id` = ? and (`status` = 1 or `status` = 3)", m.table)
case NTF_STATUS_FAILED:
update = fmt.Sprintf("update %s set `status` = 3 where `id` = ? and `status` = 1", m.table)
case NTF_STATUS_SUCCESS:
update = fmt.Sprintf("update %s set `status` = 4 where `id` = ? and `status` = 1", m.table)
default:
return fmt.Errorf("unsupported status %d", status)
}
result, err := m.conn.ExecCtx(ctx, update, id)
if err != nil {
return err
}
rows, err := result.RowsAffected()
if err != nil {
return err
}
if rows == 0 {
return ErrNoRowUpdate
}
return nil
}
func (m *customNhGameServerNotifyModel) FindNotifyList(ctx context.Context, status int8) ([]*NhGameServerNotify, error) {
query := fmt.Sprintf("select %s from %s where `status` = ? and `retry_times` < 6 limit 10", nhGameServerNotifyRows, m.table)
var ntfs []*NhGameServerNotify
err := m.conn.QueryRowsCtx(ctx, &ntfs, query, status)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
return nil, err
}
return ntfs, nil
}
// NewNhGameServerNotifyModel returns a model for the database table.
func NewNhGameServerNotifyModel(conn sqlx.SqlConn) NhGameServerNotifyModel {
return &customNhGameServerNotifyModel{
defaultNhGameServerNotifyModel: newNhGameServerNotifyModel(conn),
}
}
func (m *customNhGameServerNotifyModel) withSession(session sqlx.Session) NhGameServerNotifyModel {
return NewNhGameServerNotifyModel(sqlx.NewSqlConnFromSession(session))
}