feat 历史记录

This commit is contained in:
2026-01-14 18:15:40 +08:00
parent 90dd60e6c6
commit d885998067
11 changed files with 519 additions and 185 deletions

View File

@@ -0,0 +1,40 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package model
import (
"time"
"git.hlsq.asia/mmorpg/service-common/utils"
"gorm.io/gorm"
)
const TableNameRecord = "records"
// Record mapped from table <records>
type Record struct {
ID uint64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
Sn string `gorm:"column:sn;not null;comment:业务唯一编号" json:"sn"` // 业务唯一编号
UserSn string `gorm:"column:user_sn;not null;comment:用户-唯一编号" json:"user_sn"` // 用户-唯一编号
QuestionSn string `gorm:"column:question_sn;not null;comment:题目-唯一编号" json:"question_sn"` // 题目-唯一编号
Answer string `gorm:"column:answer;not null;comment:答案" json:"answer"` // 答案
IsCorrect int32 `gorm:"column:is_correct;not null;comment:是否正确 0 否 1 是" json:"is_correct"` // 是否正确 0 否 1 是
CreatedAt time.Time `gorm:"column:created_at;not null" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;not null" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"`
}
// TableName Record's table name
func (*Record) TableName() string {
return TableNameRecord
}
// Auto sn
func (m *Record) BeforeCreate(_ *gorm.DB) error {
if m.Sn == "" {
m.Sn = utils.SnowflakeInstance().Generate().String()
}
return nil
}

View File

@@ -20,6 +20,7 @@ func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
db: db,
Category: newCategory(db, opts...),
Question: newQuestion(db, opts...),
Record: newRecord(db, opts...),
}
}
@@ -28,6 +29,7 @@ type Query struct {
Category category
Question question
Record record
}
func (q *Query) Available() bool { return q.db != nil }
@@ -37,6 +39,7 @@ func (q *Query) clone(db *gorm.DB) *Query {
db: db,
Category: q.Category.clone(db),
Question: q.Question.clone(db),
Record: q.Record.clone(db),
}
}
@@ -53,18 +56,21 @@ func (q *Query) ReplaceDB(db *gorm.DB) *Query {
db: db,
Category: q.Category.replaceDB(db),
Question: q.Question.replaceDB(db),
Record: q.Record.replaceDB(db),
}
}
type queryCtx struct {
Category *categoryDo
Question *questionDo
Record *recordDo
}
func (q *Query) WithContext(ctx context.Context) *queryCtx {
return &queryCtx{
Category: q.Category.WithContext(ctx),
Question: q.Question.WithContext(ctx),
Record: q.Record.WithContext(ctx),
}
}

View File

@@ -0,0 +1,359 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package query
import (
"context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"gorm.io/gen"
"gorm.io/gen/field"
"gorm.io/plugin/dbresolver"
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/model"
)
func newRecord(db *gorm.DB, opts ...gen.DOOption) record {
_record := record{}
_record.recordDo.UseDB(db, opts...)
_record.recordDo.UseModel(&model.Record{})
tableName := _record.recordDo.TableName()
_record.ALL = field.NewAsterisk(tableName)
_record.ID = field.NewUint64(tableName, "id")
_record.Sn = field.NewString(tableName, "sn")
_record.UserSn = field.NewString(tableName, "user_sn")
_record.QuestionSn = field.NewString(tableName, "question_sn")
_record.Answer = field.NewString(tableName, "answer")
_record.IsCorrect = field.NewInt32(tableName, "is_correct")
_record.CreatedAt = field.NewTime(tableName, "created_at")
_record.UpdatedAt = field.NewTime(tableName, "updated_at")
_record.DeletedAt = field.NewField(tableName, "deleted_at")
_record.fillFieldMap()
return _record
}
type record struct {
recordDo recordDo
ALL field.Asterisk
ID field.Uint64
Sn field.String // 业务唯一编号
UserSn field.String // 用户-唯一编号
QuestionSn field.String // 题目-唯一编号
Answer field.String // 答案
IsCorrect field.Int32 // 是否正确 0 否 1 是
CreatedAt field.Time
UpdatedAt field.Time
DeletedAt field.Field
fieldMap map[string]field.Expr
}
func (r record) Table(newTableName string) *record {
r.recordDo.UseTable(newTableName)
return r.updateTableName(newTableName)
}
func (r record) As(alias string) *record {
r.recordDo.DO = *(r.recordDo.As(alias).(*gen.DO))
return r.updateTableName(alias)
}
func (r *record) updateTableName(table string) *record {
r.ALL = field.NewAsterisk(table)
r.ID = field.NewUint64(table, "id")
r.Sn = field.NewString(table, "sn")
r.UserSn = field.NewString(table, "user_sn")
r.QuestionSn = field.NewString(table, "question_sn")
r.Answer = field.NewString(table, "answer")
r.IsCorrect = field.NewInt32(table, "is_correct")
r.CreatedAt = field.NewTime(table, "created_at")
r.UpdatedAt = field.NewTime(table, "updated_at")
r.DeletedAt = field.NewField(table, "deleted_at")
r.fillFieldMap()
return r
}
func (r *record) WithContext(ctx context.Context) *recordDo { return r.recordDo.WithContext(ctx) }
func (r record) TableName() string { return r.recordDo.TableName() }
func (r record) Alias() string { return r.recordDo.Alias() }
func (r record) Columns(cols ...field.Expr) gen.Columns { return r.recordDo.Columns(cols...) }
func (r *record) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
_f, ok := r.fieldMap[fieldName]
if !ok || _f == nil {
return nil, false
}
_oe, ok := _f.(field.OrderExpr)
return _oe, ok
}
func (r *record) fillFieldMap() {
r.fieldMap = make(map[string]field.Expr, 9)
r.fieldMap["id"] = r.ID
r.fieldMap["sn"] = r.Sn
r.fieldMap["user_sn"] = r.UserSn
r.fieldMap["question_sn"] = r.QuestionSn
r.fieldMap["answer"] = r.Answer
r.fieldMap["is_correct"] = r.IsCorrect
r.fieldMap["created_at"] = r.CreatedAt
r.fieldMap["updated_at"] = r.UpdatedAt
r.fieldMap["deleted_at"] = r.DeletedAt
}
func (r record) clone(db *gorm.DB) record {
r.recordDo.ReplaceConnPool(db.Statement.ConnPool)
return r
}
func (r record) replaceDB(db *gorm.DB) record {
r.recordDo.ReplaceDB(db)
return r
}
type recordDo struct{ gen.DO }
func (r recordDo) Debug() *recordDo {
return r.withDO(r.DO.Debug())
}
func (r recordDo) WithContext(ctx context.Context) *recordDo {
return r.withDO(r.DO.WithContext(ctx))
}
func (r recordDo) ReadDB() *recordDo {
return r.Clauses(dbresolver.Read)
}
func (r recordDo) WriteDB() *recordDo {
return r.Clauses(dbresolver.Write)
}
func (r recordDo) Session(config *gorm.Session) *recordDo {
return r.withDO(r.DO.Session(config))
}
func (r recordDo) Clauses(conds ...clause.Expression) *recordDo {
return r.withDO(r.DO.Clauses(conds...))
}
func (r recordDo) Returning(value interface{}, columns ...string) *recordDo {
return r.withDO(r.DO.Returning(value, columns...))
}
func (r recordDo) Not(conds ...gen.Condition) *recordDo {
return r.withDO(r.DO.Not(conds...))
}
func (r recordDo) Or(conds ...gen.Condition) *recordDo {
return r.withDO(r.DO.Or(conds...))
}
func (r recordDo) Select(conds ...field.Expr) *recordDo {
return r.withDO(r.DO.Select(conds...))
}
func (r recordDo) Where(conds ...gen.Condition) *recordDo {
return r.withDO(r.DO.Where(conds...))
}
func (r recordDo) Order(conds ...field.Expr) *recordDo {
return r.withDO(r.DO.Order(conds...))
}
func (r recordDo) Distinct(cols ...field.Expr) *recordDo {
return r.withDO(r.DO.Distinct(cols...))
}
func (r recordDo) Omit(cols ...field.Expr) *recordDo {
return r.withDO(r.DO.Omit(cols...))
}
func (r recordDo) Join(table schema.Tabler, on ...field.Expr) *recordDo {
return r.withDO(r.DO.Join(table, on...))
}
func (r recordDo) LeftJoin(table schema.Tabler, on ...field.Expr) *recordDo {
return r.withDO(r.DO.LeftJoin(table, on...))
}
func (r recordDo) RightJoin(table schema.Tabler, on ...field.Expr) *recordDo {
return r.withDO(r.DO.RightJoin(table, on...))
}
func (r recordDo) Group(cols ...field.Expr) *recordDo {
return r.withDO(r.DO.Group(cols...))
}
func (r recordDo) Having(conds ...gen.Condition) *recordDo {
return r.withDO(r.DO.Having(conds...))
}
func (r recordDo) Limit(limit int) *recordDo {
return r.withDO(r.DO.Limit(limit))
}
func (r recordDo) Offset(offset int) *recordDo {
return r.withDO(r.DO.Offset(offset))
}
func (r recordDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *recordDo {
return r.withDO(r.DO.Scopes(funcs...))
}
func (r recordDo) Unscoped() *recordDo {
return r.withDO(r.DO.Unscoped())
}
func (r recordDo) Create(values ...*model.Record) error {
if len(values) == 0 {
return nil
}
return r.DO.Create(values)
}
func (r recordDo) CreateInBatches(values []*model.Record, batchSize int) error {
return r.DO.CreateInBatches(values, batchSize)
}
// Save : !!! underlying implementation is different with GORM
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
func (r recordDo) Save(values ...*model.Record) error {
if len(values) == 0 {
return nil
}
return r.DO.Save(values)
}
func (r recordDo) First() (*model.Record, error) {
if result, err := r.DO.First(); err != nil {
return nil, err
} else {
return result.(*model.Record), nil
}
}
func (r recordDo) Take() (*model.Record, error) {
if result, err := r.DO.Take(); err != nil {
return nil, err
} else {
return result.(*model.Record), nil
}
}
func (r recordDo) Last() (*model.Record, error) {
if result, err := r.DO.Last(); err != nil {
return nil, err
} else {
return result.(*model.Record), nil
}
}
func (r recordDo) Find() ([]*model.Record, error) {
result, err := r.DO.Find()
return result.([]*model.Record), err
}
func (r recordDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Record, err error) {
buf := make([]*model.Record, 0, batchSize)
err = r.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
defer func() { results = append(results, buf...) }()
return fc(tx, batch)
})
return results, err
}
func (r recordDo) FindInBatches(result *[]*model.Record, batchSize int, fc func(tx gen.Dao, batch int) error) error {
return r.DO.FindInBatches(result, batchSize, fc)
}
func (r recordDo) Attrs(attrs ...field.AssignExpr) *recordDo {
return r.withDO(r.DO.Attrs(attrs...))
}
func (r recordDo) Assign(attrs ...field.AssignExpr) *recordDo {
return r.withDO(r.DO.Assign(attrs...))
}
func (r recordDo) Joins(fields ...field.RelationField) *recordDo {
for _, _f := range fields {
r = *r.withDO(r.DO.Joins(_f))
}
return &r
}
func (r recordDo) Preload(fields ...field.RelationField) *recordDo {
for _, _f := range fields {
r = *r.withDO(r.DO.Preload(_f))
}
return &r
}
func (r recordDo) FirstOrInit() (*model.Record, error) {
if result, err := r.DO.FirstOrInit(); err != nil {
return nil, err
} else {
return result.(*model.Record), nil
}
}
func (r recordDo) FirstOrCreate() (*model.Record, error) {
if result, err := r.DO.FirstOrCreate(); err != nil {
return nil, err
} else {
return result.(*model.Record), nil
}
}
func (r recordDo) FindByPage(offset int, limit int) (result []*model.Record, count int64, err error) {
result, err = r.Offset(offset).Limit(limit).Find()
if err != nil {
return
}
if size := len(result); 0 < limit && 0 < size && size < limit {
count = int64(size + offset)
return
}
count, err = r.Offset(-1).Limit(-1).Count()
return
}
func (r recordDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
count, err = r.Count()
if err != nil {
return
}
err = r.Offset(offset).Limit(limit).Scan(result)
return
}
func (r recordDo) Scan(result interface{}) (err error) {
return r.DO.Scan(result)
}
func (r recordDo) Delete(models ...*model.Record) (result gen.ResultInfo, err error) {
return r.DO.Delete(models)
}
func (r *recordDo) withDO(do gen.Dao) *recordDo {
r.DO = *do.(*gen.DO)
return r
}

View File

@@ -0,0 +1,64 @@
package repository
import (
"context"
"git.hlsq.asia/mmorpg/service-common/db/mysql"
"git.hlsq.asia/mmorpg/service-common/db/redis"
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/model"
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/query"
"time"
)
type RecordDao struct {
ctx context.Context
query *query.Query
cache *redis.CacheClient
}
func NewRecordDao(ctx context.Context, cache ...*redis.CacheClient) *RecordDao {
dao := &RecordDao{
ctx: ctx,
query: query.Use(mysql.GetDB(dbName)),
}
if len(cache) > 0 {
dao.cache = cache[0]
}
return dao
}
func (d *RecordDao) Create(record *model.Record) (*model.Record, error) {
err := d.query.Record.WithContext(d.ctx).
Create(record)
return record, err
}
type RecordItem struct {
QuestionSn string `gorm:"column:question_sn"`
Question string `gorm:"column:question"`
Difficulty int32 `gorm:"column:difficulty"`
Category string `gorm:"column:category"`
IsCorrect int32 `gorm:"column:is_correct"`
CreatedAt time.Time `gorm:"column:created_at"`
}
func (d *RecordDao) FindByUSN(usn string, page, pageSize int) ([]*RecordItem, int64, error) {
result := make([]*RecordItem, 0)
count, err := d.query.Record.WithContext(d.ctx).
Select(
d.query.Question.Sn.As("question_sn"),
d.query.Question.Question,
d.query.Question.Difficulty,
d.query.Category.Category,
d.query.Record.IsCorrect,
d.query.Record.CreatedAt,
).
Where(d.query.Record.UserSn.Eq(usn)).
LeftJoin(d.query.Question, d.query.Question.Sn.EqCol(d.query.Record.QuestionSn)).
LeftJoin(d.query.Category, d.query.Category.Sn.EqCol(d.query.Question.CategorySn)).
Order(d.query.Record.CreatedAt.Desc()).
ScanByPage(&result, (page-1)*pageSize, pageSize)
if err != nil {
return nil, 0, err
}
return result, count, nil
}