Files
novatask/internal/model/nh_tribally_user_model.go
2025-05-08 14:45:46 +08:00

63 lines
2.2 KiB
Go
Executable File

package model
import (
"context"
"errors"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"time"
)
var _ NhTriballyUserModel = (*customNhTriballyUserModel)(nil)
type (
// NhTriballyUserModel is an interface to be customized, add more methods here,
// and implement the added methods in customNhTriballyUserModel.
NhTriballyUserModel interface {
nhTriballyUserModel
withSession(session sqlx.Session) NhTriballyUserModel
FindUpdateTriballyUsers(ctx context.Context, start, end time.Time, count int) ([]*TriballyUserChapter, error)
UpdateUserChapter(ctx context.Context, uid uint, chapter int) error
}
customNhTriballyUserModel struct {
*defaultNhTriballyUserModel
}
TriballyUserChapter struct {
Id uint `db:"id"`
Uid uint `db:"uid"` // 用户id
Email string `db:"email"` // 邮箱
Chapter int `db:"chapter"` // 章节
MaxChapter int `db:"max_chapter"` // 最大章节
UpdatedAt time.Time `db:"updated_at"` // 修改时间
}
)
func (m *customNhTriballyUserModel) UpdateUserChapter(ctx context.Context, uid uint, chapter int) error {
update := fmt.Sprintf("UPDATE %s SET `chapter` = ?, WHERE `uid` = ?", m.table)
_, err := m.conn.ExecCtx(ctx, update, chapter, uid)
return err
}
func (m *customNhTriballyUserModel) FindUpdateTriballyUsers(ctx context.Context, start, end time.Time, count int) ([]*TriballyUserChapter, error) {
query := fmt.Sprintf("SELECT t.uid, t.email, t.chapter, MAX(g.chapter) as max_chapter, g.updated_at FROM nh_tribally_user t JOIN nh_game_report g ON t.uid = g.uid WHERE g.updated_at >= ? AND g.updated_at < ? AND g.chapter > t.chapter GROUP BY uid LIMIT ?")
var result []*TriballyUserChapter
err := m.conn.QueryRowsCtx(ctx, &result, query, start, end, count)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
return nil, err
}
return result, nil
}
// NewNhTriballyUserModel returns a model for the database table.
func NewNhTriballyUserModel(conn sqlx.SqlConn) NhTriballyUserModel {
return &customNhTriballyUserModel{
defaultNhTriballyUserModel: newNhTriballyUserModel(conn),
}
}
func (m *customNhTriballyUserModel) withSession(session sqlx.Session) NhTriballyUserModel {
return NewNhTriballyUserModel(sqlx.NewSqlConnFromSession(session))
}