feat 拆出qgdzs

This commit is contained in:
2026-01-12 16:53:48 +08:00
parent a0b434fe7c
commit b8e5aba27c
12 changed files with 13 additions and 1060 deletions

View File

@@ -1,42 +0,0 @@
// 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 TableNameQuestion = "questions"
// Question mapped from table <questions>
type Question struct {
ID uint64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
Sn string `gorm:"column:sn;not null;comment:业务唯一编号" json:"sn"` // 业务唯一编号
Question string `gorm:"column:question;not null;comment:题干" json:"question"` // 题干
Options string `gorm:"column:options;not null;comment:选项" json:"options"` // 选项
Answer string `gorm:"column:answer;not null;comment:答案" json:"answer"` // 答案
Explanation string `gorm:"column:explanation;not null;comment:解析" json:"explanation"` // 解析
Difficulty int32 `gorm:"column:difficulty;not null;comment:难度分 0 - 100" json:"difficulty"` // 难度分 0 - 100
Category string `gorm:"column:category;not null;comment:分类" json:"category"` // 分类
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 Question's table name
func (*Question) TableName() string {
return TableNameQuestion
}
// Auto sn
func (m *Question) BeforeCreate(_ *gorm.DB) error {
if m.Sn == "" {
m.Sn = utils.SnowflakeInstance().Generate().String()
}
return nil
}

View File

@@ -17,26 +17,23 @@ import (
func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
return &Query{
db: db,
Question: newQuestion(db, opts...),
User: newUser(db, opts...),
db: db,
User: newUser(db, opts...),
}
}
type Query struct {
db *gorm.DB
Question question
User user
User user
}
func (q *Query) Available() bool { return q.db != nil }
func (q *Query) clone(db *gorm.DB) *Query {
return &Query{
db: db,
Question: q.Question.clone(db),
User: q.User.clone(db),
db: db,
User: q.User.clone(db),
}
}
@@ -50,21 +47,18 @@ func (q *Query) WriteDB() *Query {
func (q *Query) ReplaceDB(db *gorm.DB) *Query {
return &Query{
db: db,
Question: q.Question.replaceDB(db),
User: q.User.replaceDB(db),
db: db,
User: q.User.replaceDB(db),
}
}
type queryCtx struct {
Question *questionDo
User *userDo
User *userDo
}
func (q *Query) WithContext(ctx context.Context) *queryCtx {
return &queryCtx{
Question: q.Question.WithContext(ctx),
User: q.User.WithContext(ctx),
User: q.User.WithContext(ctx),
}
}

View File

@@ -1,367 +0,0 @@
// 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-user/internal/dao/model"
)
func newQuestion(db *gorm.DB, opts ...gen.DOOption) question {
_question := question{}
_question.questionDo.UseDB(db, opts...)
_question.questionDo.UseModel(&model.Question{})
tableName := _question.questionDo.TableName()
_question.ALL = field.NewAsterisk(tableName)
_question.ID = field.NewUint64(tableName, "id")
_question.Sn = field.NewString(tableName, "sn")
_question.Question = field.NewString(tableName, "question")
_question.Options = field.NewString(tableName, "options")
_question.Answer = field.NewString(tableName, "answer")
_question.Explanation = field.NewString(tableName, "explanation")
_question.Difficulty = field.NewInt32(tableName, "difficulty")
_question.Category = field.NewString(tableName, "category")
_question.CreatedAt = field.NewTime(tableName, "created_at")
_question.UpdatedAt = field.NewTime(tableName, "updated_at")
_question.DeletedAt = field.NewField(tableName, "deleted_at")
_question.fillFieldMap()
return _question
}
type question struct {
questionDo questionDo
ALL field.Asterisk
ID field.Uint64
Sn field.String // 业务唯一编号
Question field.String // 题干
Options field.String // 选项
Answer field.String // 答案
Explanation field.String // 解析
Difficulty field.Int32 // 难度分 0 - 100
Category field.String // 分类
CreatedAt field.Time
UpdatedAt field.Time
DeletedAt field.Field
fieldMap map[string]field.Expr
}
func (q question) Table(newTableName string) *question {
q.questionDo.UseTable(newTableName)
return q.updateTableName(newTableName)
}
func (q question) As(alias string) *question {
q.questionDo.DO = *(q.questionDo.As(alias).(*gen.DO))
return q.updateTableName(alias)
}
func (q *question) updateTableName(table string) *question {
q.ALL = field.NewAsterisk(table)
q.ID = field.NewUint64(table, "id")
q.Sn = field.NewString(table, "sn")
q.Question = field.NewString(table, "question")
q.Options = field.NewString(table, "options")
q.Answer = field.NewString(table, "answer")
q.Explanation = field.NewString(table, "explanation")
q.Difficulty = field.NewInt32(table, "difficulty")
q.Category = field.NewString(table, "category")
q.CreatedAt = field.NewTime(table, "created_at")
q.UpdatedAt = field.NewTime(table, "updated_at")
q.DeletedAt = field.NewField(table, "deleted_at")
q.fillFieldMap()
return q
}
func (q *question) WithContext(ctx context.Context) *questionDo { return q.questionDo.WithContext(ctx) }
func (q question) TableName() string { return q.questionDo.TableName() }
func (q question) Alias() string { return q.questionDo.Alias() }
func (q question) Columns(cols ...field.Expr) gen.Columns { return q.questionDo.Columns(cols...) }
func (q *question) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
_f, ok := q.fieldMap[fieldName]
if !ok || _f == nil {
return nil, false
}
_oe, ok := _f.(field.OrderExpr)
return _oe, ok
}
func (q *question) fillFieldMap() {
q.fieldMap = make(map[string]field.Expr, 11)
q.fieldMap["id"] = q.ID
q.fieldMap["sn"] = q.Sn
q.fieldMap["question"] = q.Question
q.fieldMap["options"] = q.Options
q.fieldMap["answer"] = q.Answer
q.fieldMap["explanation"] = q.Explanation
q.fieldMap["difficulty"] = q.Difficulty
q.fieldMap["category"] = q.Category
q.fieldMap["created_at"] = q.CreatedAt
q.fieldMap["updated_at"] = q.UpdatedAt
q.fieldMap["deleted_at"] = q.DeletedAt
}
func (q question) clone(db *gorm.DB) question {
q.questionDo.ReplaceConnPool(db.Statement.ConnPool)
return q
}
func (q question) replaceDB(db *gorm.DB) question {
q.questionDo.ReplaceDB(db)
return q
}
type questionDo struct{ gen.DO }
func (q questionDo) Debug() *questionDo {
return q.withDO(q.DO.Debug())
}
func (q questionDo) WithContext(ctx context.Context) *questionDo {
return q.withDO(q.DO.WithContext(ctx))
}
func (q questionDo) ReadDB() *questionDo {
return q.Clauses(dbresolver.Read)
}
func (q questionDo) WriteDB() *questionDo {
return q.Clauses(dbresolver.Write)
}
func (q questionDo) Session(config *gorm.Session) *questionDo {
return q.withDO(q.DO.Session(config))
}
func (q questionDo) Clauses(conds ...clause.Expression) *questionDo {
return q.withDO(q.DO.Clauses(conds...))
}
func (q questionDo) Returning(value interface{}, columns ...string) *questionDo {
return q.withDO(q.DO.Returning(value, columns...))
}
func (q questionDo) Not(conds ...gen.Condition) *questionDo {
return q.withDO(q.DO.Not(conds...))
}
func (q questionDo) Or(conds ...gen.Condition) *questionDo {
return q.withDO(q.DO.Or(conds...))
}
func (q questionDo) Select(conds ...field.Expr) *questionDo {
return q.withDO(q.DO.Select(conds...))
}
func (q questionDo) Where(conds ...gen.Condition) *questionDo {
return q.withDO(q.DO.Where(conds...))
}
func (q questionDo) Order(conds ...field.Expr) *questionDo {
return q.withDO(q.DO.Order(conds...))
}
func (q questionDo) Distinct(cols ...field.Expr) *questionDo {
return q.withDO(q.DO.Distinct(cols...))
}
func (q questionDo) Omit(cols ...field.Expr) *questionDo {
return q.withDO(q.DO.Omit(cols...))
}
func (q questionDo) Join(table schema.Tabler, on ...field.Expr) *questionDo {
return q.withDO(q.DO.Join(table, on...))
}
func (q questionDo) LeftJoin(table schema.Tabler, on ...field.Expr) *questionDo {
return q.withDO(q.DO.LeftJoin(table, on...))
}
func (q questionDo) RightJoin(table schema.Tabler, on ...field.Expr) *questionDo {
return q.withDO(q.DO.RightJoin(table, on...))
}
func (q questionDo) Group(cols ...field.Expr) *questionDo {
return q.withDO(q.DO.Group(cols...))
}
func (q questionDo) Having(conds ...gen.Condition) *questionDo {
return q.withDO(q.DO.Having(conds...))
}
func (q questionDo) Limit(limit int) *questionDo {
return q.withDO(q.DO.Limit(limit))
}
func (q questionDo) Offset(offset int) *questionDo {
return q.withDO(q.DO.Offset(offset))
}
func (q questionDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *questionDo {
return q.withDO(q.DO.Scopes(funcs...))
}
func (q questionDo) Unscoped() *questionDo {
return q.withDO(q.DO.Unscoped())
}
func (q questionDo) Create(values ...*model.Question) error {
if len(values) == 0 {
return nil
}
return q.DO.Create(values)
}
func (q questionDo) CreateInBatches(values []*model.Question, batchSize int) error {
return q.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 (q questionDo) Save(values ...*model.Question) error {
if len(values) == 0 {
return nil
}
return q.DO.Save(values)
}
func (q questionDo) First() (*model.Question, error) {
if result, err := q.DO.First(); err != nil {
return nil, err
} else {
return result.(*model.Question), nil
}
}
func (q questionDo) Take() (*model.Question, error) {
if result, err := q.DO.Take(); err != nil {
return nil, err
} else {
return result.(*model.Question), nil
}
}
func (q questionDo) Last() (*model.Question, error) {
if result, err := q.DO.Last(); err != nil {
return nil, err
} else {
return result.(*model.Question), nil
}
}
func (q questionDo) Find() ([]*model.Question, error) {
result, err := q.DO.Find()
return result.([]*model.Question), err
}
func (q questionDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Question, err error) {
buf := make([]*model.Question, 0, batchSize)
err = q.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 (q questionDo) FindInBatches(result *[]*model.Question, batchSize int, fc func(tx gen.Dao, batch int) error) error {
return q.DO.FindInBatches(result, batchSize, fc)
}
func (q questionDo) Attrs(attrs ...field.AssignExpr) *questionDo {
return q.withDO(q.DO.Attrs(attrs...))
}
func (q questionDo) Assign(attrs ...field.AssignExpr) *questionDo {
return q.withDO(q.DO.Assign(attrs...))
}
func (q questionDo) Joins(fields ...field.RelationField) *questionDo {
for _, _f := range fields {
q = *q.withDO(q.DO.Joins(_f))
}
return &q
}
func (q questionDo) Preload(fields ...field.RelationField) *questionDo {
for _, _f := range fields {
q = *q.withDO(q.DO.Preload(_f))
}
return &q
}
func (q questionDo) FirstOrInit() (*model.Question, error) {
if result, err := q.DO.FirstOrInit(); err != nil {
return nil, err
} else {
return result.(*model.Question), nil
}
}
func (q questionDo) FirstOrCreate() (*model.Question, error) {
if result, err := q.DO.FirstOrCreate(); err != nil {
return nil, err
} else {
return result.(*model.Question), nil
}
}
func (q questionDo) FindByPage(offset int, limit int) (result []*model.Question, count int64, err error) {
result, err = q.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 = q.Offset(-1).Limit(-1).Count()
return
}
func (q questionDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
count, err = q.Count()
if err != nil {
return
}
err = q.Offset(offset).Limit(limit).Scan(result)
return
}
func (q questionDo) Scan(result interface{}) (err error) {
return q.DO.Scan(result)
}
func (q questionDo) Delete(models ...*model.Question) (result gen.ResultInfo, err error) {
return q.DO.Delete(models)
}
func (q *questionDo) withDO(do gen.Dao) *questionDo {
q.DO = *do.(*gen.DO)
return q
}

View File

@@ -1,83 +0,0 @@
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-common/utils"
"git.hlsq.asia/mmorpg/service-user/internal/dao/model"
"git.hlsq.asia/mmorpg/service-user/internal/dao/query"
"gorm.io/gorm"
"time"
)
type QuestionDao struct {
ctx context.Context
query *query.Query
cache *redis.CacheClient
}
func NewQuestionDao(ctx context.Context, cache ...*redis.CacheClient) *QuestionDao {
dao := &QuestionDao{
ctx: ctx,
query: query.Use(mysql.GetDB(dbName)),
}
if len(cache) > 0 {
dao.cache = cache[0]
}
return dao
}
func (d *QuestionDao) Create(question *model.Question) error {
err := d.query.Question.WithContext(d.ctx).
Create(question)
return err
}
func (d *QuestionDao) FindByRandom() (*model.Question, error) {
count, err := d.query.Question.WithContext(d.ctx).Count()
if err != nil {
return nil, err
}
if count == 0 {
return nil, gorm.ErrRecordNotFound
}
first, err := d.query.Question.WithContext(d.ctx).
Offset(utils.RandInt(0, int(count-1))).
First()
if err != nil {
return nil, err
}
return first, nil
}
func (d *QuestionDao) FindBySn(sn string) (*model.Question, error) {
if d.cache != nil {
var question model.Question
if ok := d.cache.Get(d.ctx, keyCacheBySn(sn, question.TableName()), &question); ok {
return &question, nil
}
}
first, err := d.query.Question.WithContext(d.ctx).
Where(d.query.Question.Sn.Eq(sn)).
First()
if err != nil {
return nil, err
}
if d.cache != nil {
d.cache.Set(d.ctx, keyCacheBySn(sn, first.TableName()), first, 5*time.Minute)
}
return first, nil
}
func (d *QuestionDao) FindCategory() ([]string, error) {
var categories []string
err := d.query.Question.WithContext(d.ctx).
Select(d.query.Question.Category).
Distinct().
Scan(&categories)
if err != nil {
return nil, err
}
return categories, nil
}