feat 环境

This commit is contained in:
2026-01-24 11:15:41 +08:00
parent 4aa48a822e
commit 30ba2e8994
27 changed files with 3783 additions and 930 deletions

View File

@@ -0,0 +1,37 @@
// 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 TableNameCategory = "categories"
// Category mapped from table <categories>
type Category struct {
ID uint64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
Sn string `gorm:"column:sn;not null;comment:业务唯一编号" json:"sn"` // 业务唯一编号
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 Category's table name
func (*Category) TableName() string {
return TableNameCategory
}
// Auto sn
func (m *Category) BeforeCreate(_ *gorm.DB) error {
if m.Sn == "" {
m.Sn = utils.SnowflakeInstance().Generate().String()
}
return nil
}

View File

@@ -1,4 +1,4 @@
// 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.
// Code generated by gorm.io/gen. DO NOT EDIT.
@@ -7,8 +7,8 @@ package model
import (
"time"
"gorm.io/gorm"
"git.hlsq.asia/mmorpg/service-common/utils"
"gorm.io/gorm"
)
const TableNameQuestion = "questions"
@@ -22,7 +22,7 @@ type Question struct {
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"` // 分类
CategorySn string `gorm:"column:category_sn;not null;comment:分类-唯一编号" json:"category_sn"` // 分类-唯一编号
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"`

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

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

View File

@@ -0,0 +1,105 @@
// 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"
"database/sql"
"gorm.io/gorm"
"gorm.io/gen"
"gorm.io/plugin/dbresolver"
)
func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
return &Query{
db: db,
Category: newCategory(db, opts...),
Question: newQuestion(db, opts...),
Record: newRecord(db, opts...),
}
}
type Query struct {
db *gorm.DB
Category category
Question question
Record record
}
func (q *Query) Available() bool { return q.db != nil }
func (q *Query) clone(db *gorm.DB) *Query {
return &Query{
db: db,
Category: q.Category.clone(db),
Question: q.Question.clone(db),
Record: q.Record.clone(db),
}
}
func (q *Query) ReadDB() *Query {
return q.ReplaceDB(q.db.Clauses(dbresolver.Read))
}
func (q *Query) WriteDB() *Query {
return q.ReplaceDB(q.db.Clauses(dbresolver.Write))
}
func (q *Query) ReplaceDB(db *gorm.DB) *Query {
return &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),
}
}
func (q *Query) Transaction(fc func(tx *Query) error, opts ...*sql.TxOptions) error {
return q.db.Transaction(func(tx *gorm.DB) error { return fc(q.clone(tx)) }, opts...)
}
func (q *Query) Begin(opts ...*sql.TxOptions) *QueryTx {
tx := q.db.Begin(opts...)
return &QueryTx{Query: q.clone(tx), Error: tx.Error}
}
type QueryTx struct {
*Query
Error error
}
func (q *QueryTx) Commit() error {
return q.db.Commit().Error
}
func (q *QueryTx) Rollback() error {
return q.db.Rollback().Error
}
func (q *QueryTx) SavePoint(name string) error {
return q.db.SavePoint(name).Error
}
func (q *QueryTx) RollbackTo(name string) error {
return q.db.RollbackTo(name).Error
}

View File

@@ -16,7 +16,7 @@ import (
"gorm.io/plugin/dbresolver"
"git.hlsq.asia/mmorpg/user_db/model"
"git.hlsq.asia/mmorpg/qgdzs_db/model"
)
func newQuestion(db *gorm.DB, opts ...gen.DOOption) question {
@@ -34,7 +34,7 @@ func newQuestion(db *gorm.DB, opts ...gen.DOOption) question {
_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.CategorySn = field.NewString(tableName, "category_sn")
_question.CreatedAt = field.NewTime(tableName, "created_at")
_question.UpdatedAt = field.NewTime(tableName, "updated_at")
_question.DeletedAt = field.NewField(tableName, "deleted_at")
@@ -55,7 +55,7 @@ type question struct {
Answer field.String // 答案
Explanation field.String // 解析
Difficulty field.Int32 // 难度分 0 - 100
Category field.String // 分类
CategorySn field.String // 分类-唯一编号
CreatedAt field.Time
UpdatedAt field.Time
DeletedAt field.Field
@@ -82,7 +82,7 @@ func (q *question) updateTableName(table string) *question {
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.CategorySn = field.NewString(table, "category_sn")
q.CreatedAt = field.NewTime(table, "created_at")
q.UpdatedAt = field.NewTime(table, "updated_at")
q.DeletedAt = field.NewField(table, "deleted_at")
@@ -118,7 +118,7 @@ func (q *question) fillFieldMap() {
q.fieldMap["answer"] = q.Answer
q.fieldMap["explanation"] = q.Explanation
q.fieldMap["difficulty"] = q.Difficulty
q.fieldMap["category"] = q.Category
q.fieldMap["category_sn"] = q.CategorySn
q.fieldMap["created_at"] = q.CreatedAt
q.fieldMap["updated_at"] = q.UpdatedAt
q.fieldMap["deleted_at"] = q.DeletedAt

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/qgdzs_db/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

@@ -1,4 +1,4 @@
// 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.
// Code generated by gorm.io/gen. DO NOT EDIT.
@@ -7,21 +7,23 @@ package model
import (
"time"
"gorm.io/gorm"
"git.hlsq.asia/mmorpg/service-common/utils"
"gorm.io/gorm"
)
const TableNameUser = "users"
// User mapped from table <users>
type User struct {
ID uint64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
Sn string `gorm:"column:sn;not null;comment:业务唯一编号" json:"sn"` // 业务唯一编号
Name string `gorm:"column:name;not null" json:"name"`
Phone string `gorm:"column:phone;not null" json:"phone"`
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"`
ID uint64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
Sn string `gorm:"column:sn;not null;comment:业务唯一编号" json:"sn"` // 业务唯一编号
Name string `gorm:"column:name;not null" json:"name"`
Phone string `gorm:"column:phone" json:"phone"`
WxUnionID string `gorm:"column:wx_union_id;comment:微信用户唯一标识" json:"wx_union_id"` // 微信用户唯一标识
WxMiniOpenID string `gorm:"column:wx_mini_open_id;comment:微信小程序的openID" json:"wx_mini_open_id"` // 微信小程序的openID
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 User's table name

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

@@ -31,6 +31,8 @@ func newUser(db *gorm.DB, opts ...gen.DOOption) user {
_user.Sn = field.NewString(tableName, "sn")
_user.Name = field.NewString(tableName, "name")
_user.Phone = field.NewString(tableName, "phone")
_user.WxUnionID = field.NewString(tableName, "wx_union_id")
_user.WxMiniOpenID = field.NewString(tableName, "wx_mini_open_id")
_user.CreatedAt = field.NewTime(tableName, "created_at")
_user.UpdatedAt = field.NewTime(tableName, "updated_at")
_user.DeletedAt = field.NewField(tableName, "deleted_at")
@@ -43,14 +45,16 @@ func newUser(db *gorm.DB, opts ...gen.DOOption) user {
type user struct {
userDo userDo
ALL field.Asterisk
ID field.Uint64
Sn field.String // 业务唯一编号
Name field.String
Phone field.String
CreatedAt field.Time
UpdatedAt field.Time
DeletedAt field.Field
ALL field.Asterisk
ID field.Uint64
Sn field.String // 业务唯一编号
Name field.String
Phone field.String
WxUnionID field.String // 微信用户唯一标识
WxMiniOpenID field.String // 微信小程序的openID
CreatedAt field.Time
UpdatedAt field.Time
DeletedAt field.Field
fieldMap map[string]field.Expr
}
@@ -71,6 +75,8 @@ func (u *user) updateTableName(table string) *user {
u.Sn = field.NewString(table, "sn")
u.Name = field.NewString(table, "name")
u.Phone = field.NewString(table, "phone")
u.WxUnionID = field.NewString(table, "wx_union_id")
u.WxMiniOpenID = field.NewString(table, "wx_mini_open_id")
u.CreatedAt = field.NewTime(table, "created_at")
u.UpdatedAt = field.NewTime(table, "updated_at")
u.DeletedAt = field.NewField(table, "deleted_at")
@@ -98,11 +104,13 @@ func (u *user) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (u *user) fillFieldMap() {
u.fieldMap = make(map[string]field.Expr, 7)
u.fieldMap = make(map[string]field.Expr, 9)
u.fieldMap["id"] = u.ID
u.fieldMap["sn"] = u.Sn
u.fieldMap["name"] = u.Name
u.fieldMap["phone"] = u.Phone
u.fieldMap["wx_union_id"] = u.WxUnionID
u.fieldMap["wx_mini_open_id"] = u.WxMiniOpenID
u.fieldMap["created_at"] = u.CreatedAt
u.fieldMap["updated_at"] = u.UpdatedAt
u.fieldMap["deleted_at"] = u.DeletedAt