feat kafka 改版 2
This commit is contained in:
38
internal/dao/model/point_card.gen.go
Normal file
38
internal/dao/model/point_card.gen.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// 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 TableNamePointCard = "point_card"
|
||||
|
||||
// PointCard mapped from table <point_card>
|
||||
type PointCard 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"` // 用户-唯一编号
|
||||
Point int64 `gorm:"column:point;not null;comment:积分" json:"point"` // 积分
|
||||
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 PointCard's table name
|
||||
func (*PointCard) TableName() string {
|
||||
return TableNamePointCard
|
||||
}
|
||||
|
||||
// Auto sn
|
||||
func (m *PointCard) BeforeCreate(_ *gorm.DB) error {
|
||||
if m.Sn == "" {
|
||||
m.Sn = utils.SnowflakeInstance().Generate().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
46
internal/dao/model/point_records.gen.go
Normal file
46
internal/dao/model/point_records.gen.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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 TableNamePointRecord = "point_records"
|
||||
|
||||
// PointRecord mapped from table <point_records>
|
||||
type PointRecord 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"` // 用户-唯一编号
|
||||
/*
|
||||
来源:
|
||||
0. 未知
|
||||
1. 随机答题
|
||||
2. 类目答题
|
||||
3. 限时答题
|
||||
*/
|
||||
Source int32 `gorm:"column:source;not null;comment:来源:\n0. 未知\n1. 随机答题\n2. 类目答题\n3. 限时答题" json:"source"`
|
||||
Point int64 `gorm:"column:point;not null;comment:积分" json:"point"` // 积分
|
||||
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 PointRecord's table name
|
||||
func (*PointRecord) TableName() string {
|
||||
return TableNamePointRecord
|
||||
}
|
||||
|
||||
// Auto sn
|
||||
func (m *PointRecord) BeforeCreate(_ *gorm.DB) error {
|
||||
if m.Sn == "" {
|
||||
m.Sn = utils.SnowflakeInstance().Generate().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -17,29 +17,35 @@ import (
|
||||
|
||||
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...),
|
||||
db: db,
|
||||
Category: newCategory(db, opts...),
|
||||
PointCard: newPointCard(db, opts...),
|
||||
PointRecord: newPointRecord(db, opts...),
|
||||
Question: newQuestion(db, opts...),
|
||||
Record: newRecord(db, opts...),
|
||||
}
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
db *gorm.DB
|
||||
|
||||
Category category
|
||||
Question question
|
||||
Record record
|
||||
Category category
|
||||
PointCard pointCard
|
||||
PointRecord pointRecord
|
||||
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),
|
||||
db: db,
|
||||
Category: q.Category.clone(db),
|
||||
PointCard: q.PointCard.clone(db),
|
||||
PointRecord: q.PointRecord.clone(db),
|
||||
Question: q.Question.clone(db),
|
||||
Record: q.Record.clone(db),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,24 +59,30 @@ func (q *Query) WriteDB() *Query {
|
||||
|
||||
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),
|
||||
db: db,
|
||||
Category: q.Category.replaceDB(db),
|
||||
PointCard: q.PointCard.replaceDB(db),
|
||||
PointRecord: q.PointRecord.replaceDB(db),
|
||||
Question: q.Question.replaceDB(db),
|
||||
Record: q.Record.replaceDB(db),
|
||||
}
|
||||
}
|
||||
|
||||
type queryCtx struct {
|
||||
Category *categoryDo
|
||||
Question *questionDo
|
||||
Record *recordDo
|
||||
Category *categoryDo
|
||||
PointCard *pointCardDo
|
||||
PointRecord *pointRecordDo
|
||||
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),
|
||||
Category: q.Category.WithContext(ctx),
|
||||
PointCard: q.PointCard.WithContext(ctx),
|
||||
PointRecord: q.PointRecord.WithContext(ctx),
|
||||
Question: q.Question.WithContext(ctx),
|
||||
Record: q.Record.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
353
internal/dao/query/point_card.gen.go
Normal file
353
internal/dao/query/point_card.gen.go
Normal file
@@ -0,0 +1,353 @@
|
||||
// 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 newPointCard(db *gorm.DB, opts ...gen.DOOption) pointCard {
|
||||
_pointCard := pointCard{}
|
||||
|
||||
_pointCard.pointCardDo.UseDB(db, opts...)
|
||||
_pointCard.pointCardDo.UseModel(&model.PointCard{})
|
||||
|
||||
tableName := _pointCard.pointCardDo.TableName()
|
||||
_pointCard.ALL = field.NewAsterisk(tableName)
|
||||
_pointCard.ID = field.NewUint64(tableName, "id")
|
||||
_pointCard.Sn = field.NewString(tableName, "sn")
|
||||
_pointCard.UserSn = field.NewString(tableName, "user_sn")
|
||||
_pointCard.Point = field.NewInt64(tableName, "point")
|
||||
_pointCard.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_pointCard.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||
_pointCard.DeletedAt = field.NewField(tableName, "deleted_at")
|
||||
|
||||
_pointCard.fillFieldMap()
|
||||
|
||||
return _pointCard
|
||||
}
|
||||
|
||||
type pointCard struct {
|
||||
pointCardDo pointCardDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Uint64
|
||||
Sn field.String // 业务唯一编号
|
||||
UserSn field.String // 用户-唯一编号
|
||||
Point field.Int64 // 积分
|
||||
CreatedAt field.Time
|
||||
UpdatedAt field.Time
|
||||
DeletedAt field.Field
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
|
||||
func (p pointCard) Table(newTableName string) *pointCard {
|
||||
p.pointCardDo.UseTable(newTableName)
|
||||
return p.updateTableName(newTableName)
|
||||
}
|
||||
|
||||
func (p pointCard) As(alias string) *pointCard {
|
||||
p.pointCardDo.DO = *(p.pointCardDo.As(alias).(*gen.DO))
|
||||
return p.updateTableName(alias)
|
||||
}
|
||||
|
||||
func (p *pointCard) updateTableName(table string) *pointCard {
|
||||
p.ALL = field.NewAsterisk(table)
|
||||
p.ID = field.NewUint64(table, "id")
|
||||
p.Sn = field.NewString(table, "sn")
|
||||
p.UserSn = field.NewString(table, "user_sn")
|
||||
p.Point = field.NewInt64(table, "point")
|
||||
p.CreatedAt = field.NewTime(table, "created_at")
|
||||
p.UpdatedAt = field.NewTime(table, "updated_at")
|
||||
p.DeletedAt = field.NewField(table, "deleted_at")
|
||||
|
||||
p.fillFieldMap()
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *pointCard) WithContext(ctx context.Context) *pointCardDo {
|
||||
return p.pointCardDo.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (p pointCard) TableName() string { return p.pointCardDo.TableName() }
|
||||
|
||||
func (p pointCard) Alias() string { return p.pointCardDo.Alias() }
|
||||
|
||||
func (p pointCard) Columns(cols ...field.Expr) gen.Columns { return p.pointCardDo.Columns(cols...) }
|
||||
|
||||
func (p *pointCard) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
_f, ok := p.fieldMap[fieldName]
|
||||
if !ok || _f == nil {
|
||||
return nil, false
|
||||
}
|
||||
_oe, ok := _f.(field.OrderExpr)
|
||||
return _oe, ok
|
||||
}
|
||||
|
||||
func (p *pointCard) fillFieldMap() {
|
||||
p.fieldMap = make(map[string]field.Expr, 7)
|
||||
p.fieldMap["id"] = p.ID
|
||||
p.fieldMap["sn"] = p.Sn
|
||||
p.fieldMap["user_sn"] = p.UserSn
|
||||
p.fieldMap["point"] = p.Point
|
||||
p.fieldMap["created_at"] = p.CreatedAt
|
||||
p.fieldMap["updated_at"] = p.UpdatedAt
|
||||
p.fieldMap["deleted_at"] = p.DeletedAt
|
||||
}
|
||||
|
||||
func (p pointCard) clone(db *gorm.DB) pointCard {
|
||||
p.pointCardDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
return p
|
||||
}
|
||||
|
||||
func (p pointCard) replaceDB(db *gorm.DB) pointCard {
|
||||
p.pointCardDo.ReplaceDB(db)
|
||||
return p
|
||||
}
|
||||
|
||||
type pointCardDo struct{ gen.DO }
|
||||
|
||||
func (p pointCardDo) Debug() *pointCardDo {
|
||||
return p.withDO(p.DO.Debug())
|
||||
}
|
||||
|
||||
func (p pointCardDo) WithContext(ctx context.Context) *pointCardDo {
|
||||
return p.withDO(p.DO.WithContext(ctx))
|
||||
}
|
||||
|
||||
func (p pointCardDo) ReadDB() *pointCardDo {
|
||||
return p.Clauses(dbresolver.Read)
|
||||
}
|
||||
|
||||
func (p pointCardDo) WriteDB() *pointCardDo {
|
||||
return p.Clauses(dbresolver.Write)
|
||||
}
|
||||
|
||||
func (p pointCardDo) Session(config *gorm.Session) *pointCardDo {
|
||||
return p.withDO(p.DO.Session(config))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Clauses(conds ...clause.Expression) *pointCardDo {
|
||||
return p.withDO(p.DO.Clauses(conds...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Returning(value interface{}, columns ...string) *pointCardDo {
|
||||
return p.withDO(p.DO.Returning(value, columns...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Not(conds ...gen.Condition) *pointCardDo {
|
||||
return p.withDO(p.DO.Not(conds...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Or(conds ...gen.Condition) *pointCardDo {
|
||||
return p.withDO(p.DO.Or(conds...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Select(conds ...field.Expr) *pointCardDo {
|
||||
return p.withDO(p.DO.Select(conds...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Where(conds ...gen.Condition) *pointCardDo {
|
||||
return p.withDO(p.DO.Where(conds...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Order(conds ...field.Expr) *pointCardDo {
|
||||
return p.withDO(p.DO.Order(conds...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Distinct(cols ...field.Expr) *pointCardDo {
|
||||
return p.withDO(p.DO.Distinct(cols...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Omit(cols ...field.Expr) *pointCardDo {
|
||||
return p.withDO(p.DO.Omit(cols...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Join(table schema.Tabler, on ...field.Expr) *pointCardDo {
|
||||
return p.withDO(p.DO.Join(table, on...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) LeftJoin(table schema.Tabler, on ...field.Expr) *pointCardDo {
|
||||
return p.withDO(p.DO.LeftJoin(table, on...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) RightJoin(table schema.Tabler, on ...field.Expr) *pointCardDo {
|
||||
return p.withDO(p.DO.RightJoin(table, on...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Group(cols ...field.Expr) *pointCardDo {
|
||||
return p.withDO(p.DO.Group(cols...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Having(conds ...gen.Condition) *pointCardDo {
|
||||
return p.withDO(p.DO.Having(conds...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Limit(limit int) *pointCardDo {
|
||||
return p.withDO(p.DO.Limit(limit))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Offset(offset int) *pointCardDo {
|
||||
return p.withDO(p.DO.Offset(offset))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *pointCardDo {
|
||||
return p.withDO(p.DO.Scopes(funcs...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Unscoped() *pointCardDo {
|
||||
return p.withDO(p.DO.Unscoped())
|
||||
}
|
||||
|
||||
func (p pointCardDo) Create(values ...*model.PointCard) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return p.DO.Create(values)
|
||||
}
|
||||
|
||||
func (p pointCardDo) CreateInBatches(values []*model.PointCard, batchSize int) error {
|
||||
return p.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 (p pointCardDo) Save(values ...*model.PointCard) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return p.DO.Save(values)
|
||||
}
|
||||
|
||||
func (p pointCardDo) First() (*model.PointCard, error) {
|
||||
if result, err := p.DO.First(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*model.PointCard), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p pointCardDo) Take() (*model.PointCard, error) {
|
||||
if result, err := p.DO.Take(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*model.PointCard), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p pointCardDo) Last() (*model.PointCard, error) {
|
||||
if result, err := p.DO.Last(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*model.PointCard), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p pointCardDo) Find() ([]*model.PointCard, error) {
|
||||
result, err := p.DO.Find()
|
||||
return result.([]*model.PointCard), err
|
||||
}
|
||||
|
||||
func (p pointCardDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.PointCard, err error) {
|
||||
buf := make([]*model.PointCard, 0, batchSize)
|
||||
err = p.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 (p pointCardDo) FindInBatches(result *[]*model.PointCard, batchSize int, fc func(tx gen.Dao, batch int) error) error {
|
||||
return p.DO.FindInBatches(result, batchSize, fc)
|
||||
}
|
||||
|
||||
func (p pointCardDo) Attrs(attrs ...field.AssignExpr) *pointCardDo {
|
||||
return p.withDO(p.DO.Attrs(attrs...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Assign(attrs ...field.AssignExpr) *pointCardDo {
|
||||
return p.withDO(p.DO.Assign(attrs...))
|
||||
}
|
||||
|
||||
func (p pointCardDo) Joins(fields ...field.RelationField) *pointCardDo {
|
||||
for _, _f := range fields {
|
||||
p = *p.withDO(p.DO.Joins(_f))
|
||||
}
|
||||
return &p
|
||||
}
|
||||
|
||||
func (p pointCardDo) Preload(fields ...field.RelationField) *pointCardDo {
|
||||
for _, _f := range fields {
|
||||
p = *p.withDO(p.DO.Preload(_f))
|
||||
}
|
||||
return &p
|
||||
}
|
||||
|
||||
func (p pointCardDo) FirstOrInit() (*model.PointCard, error) {
|
||||
if result, err := p.DO.FirstOrInit(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*model.PointCard), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p pointCardDo) FirstOrCreate() (*model.PointCard, error) {
|
||||
if result, err := p.DO.FirstOrCreate(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*model.PointCard), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p pointCardDo) FindByPage(offset int, limit int) (result []*model.PointCard, count int64, err error) {
|
||||
result, err = p.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 = p.Offset(-1).Limit(-1).Count()
|
||||
return
|
||||
}
|
||||
|
||||
func (p pointCardDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
|
||||
count, err = p.Count()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = p.Offset(offset).Limit(limit).Scan(result)
|
||||
return
|
||||
}
|
||||
|
||||
func (p pointCardDo) Scan(result interface{}) (err error) {
|
||||
return p.DO.Scan(result)
|
||||
}
|
||||
|
||||
func (p pointCardDo) Delete(models ...*model.PointCard) (result gen.ResultInfo, err error) {
|
||||
return p.DO.Delete(models)
|
||||
}
|
||||
|
||||
func (p *pointCardDo) withDO(do gen.Dao) *pointCardDo {
|
||||
p.DO = *do.(*gen.DO)
|
||||
return p
|
||||
}
|
||||
364
internal/dao/query/point_records.gen.go
Normal file
364
internal/dao/query/point_records.gen.go
Normal file
@@ -0,0 +1,364 @@
|
||||
// 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 newPointRecord(db *gorm.DB, opts ...gen.DOOption) pointRecord {
|
||||
_pointRecord := pointRecord{}
|
||||
|
||||
_pointRecord.pointRecordDo.UseDB(db, opts...)
|
||||
_pointRecord.pointRecordDo.UseModel(&model.PointRecord{})
|
||||
|
||||
tableName := _pointRecord.pointRecordDo.TableName()
|
||||
_pointRecord.ALL = field.NewAsterisk(tableName)
|
||||
_pointRecord.ID = field.NewUint64(tableName, "id")
|
||||
_pointRecord.Sn = field.NewString(tableName, "sn")
|
||||
_pointRecord.UserSn = field.NewString(tableName, "user_sn")
|
||||
_pointRecord.Source = field.NewInt32(tableName, "source")
|
||||
_pointRecord.Point = field.NewInt64(tableName, "point")
|
||||
_pointRecord.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_pointRecord.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||
_pointRecord.DeletedAt = field.NewField(tableName, "deleted_at")
|
||||
|
||||
_pointRecord.fillFieldMap()
|
||||
|
||||
return _pointRecord
|
||||
}
|
||||
|
||||
type pointRecord struct {
|
||||
pointRecordDo pointRecordDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Uint64
|
||||
Sn field.String // 业务唯一编号
|
||||
UserSn field.String // 用户-唯一编号
|
||||
/*
|
||||
来源:
|
||||
0. 未知
|
||||
1. 随机答题
|
||||
2. 类目答题
|
||||
3. 限时答题
|
||||
*/
|
||||
Source field.Int32
|
||||
Point field.Int64 // 积分
|
||||
CreatedAt field.Time
|
||||
UpdatedAt field.Time
|
||||
DeletedAt field.Field
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
|
||||
func (p pointRecord) Table(newTableName string) *pointRecord {
|
||||
p.pointRecordDo.UseTable(newTableName)
|
||||
return p.updateTableName(newTableName)
|
||||
}
|
||||
|
||||
func (p pointRecord) As(alias string) *pointRecord {
|
||||
p.pointRecordDo.DO = *(p.pointRecordDo.As(alias).(*gen.DO))
|
||||
return p.updateTableName(alias)
|
||||
}
|
||||
|
||||
func (p *pointRecord) updateTableName(table string) *pointRecord {
|
||||
p.ALL = field.NewAsterisk(table)
|
||||
p.ID = field.NewUint64(table, "id")
|
||||
p.Sn = field.NewString(table, "sn")
|
||||
p.UserSn = field.NewString(table, "user_sn")
|
||||
p.Source = field.NewInt32(table, "source")
|
||||
p.Point = field.NewInt64(table, "point")
|
||||
p.CreatedAt = field.NewTime(table, "created_at")
|
||||
p.UpdatedAt = field.NewTime(table, "updated_at")
|
||||
p.DeletedAt = field.NewField(table, "deleted_at")
|
||||
|
||||
p.fillFieldMap()
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *pointRecord) WithContext(ctx context.Context) *pointRecordDo {
|
||||
return p.pointRecordDo.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (p pointRecord) TableName() string { return p.pointRecordDo.TableName() }
|
||||
|
||||
func (p pointRecord) Alias() string { return p.pointRecordDo.Alias() }
|
||||
|
||||
func (p pointRecord) Columns(cols ...field.Expr) gen.Columns { return p.pointRecordDo.Columns(cols...) }
|
||||
|
||||
func (p *pointRecord) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
_f, ok := p.fieldMap[fieldName]
|
||||
if !ok || _f == nil {
|
||||
return nil, false
|
||||
}
|
||||
_oe, ok := _f.(field.OrderExpr)
|
||||
return _oe, ok
|
||||
}
|
||||
|
||||
func (p *pointRecord) fillFieldMap() {
|
||||
p.fieldMap = make(map[string]field.Expr, 8)
|
||||
p.fieldMap["id"] = p.ID
|
||||
p.fieldMap["sn"] = p.Sn
|
||||
p.fieldMap["user_sn"] = p.UserSn
|
||||
p.fieldMap["source"] = p.Source
|
||||
p.fieldMap["point"] = p.Point
|
||||
p.fieldMap["created_at"] = p.CreatedAt
|
||||
p.fieldMap["updated_at"] = p.UpdatedAt
|
||||
p.fieldMap["deleted_at"] = p.DeletedAt
|
||||
}
|
||||
|
||||
func (p pointRecord) clone(db *gorm.DB) pointRecord {
|
||||
p.pointRecordDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
return p
|
||||
}
|
||||
|
||||
func (p pointRecord) replaceDB(db *gorm.DB) pointRecord {
|
||||
p.pointRecordDo.ReplaceDB(db)
|
||||
return p
|
||||
}
|
||||
|
||||
type pointRecordDo struct{ gen.DO }
|
||||
|
||||
func (p pointRecordDo) Debug() *pointRecordDo {
|
||||
return p.withDO(p.DO.Debug())
|
||||
}
|
||||
|
||||
func (p pointRecordDo) WithContext(ctx context.Context) *pointRecordDo {
|
||||
return p.withDO(p.DO.WithContext(ctx))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) ReadDB() *pointRecordDo {
|
||||
return p.Clauses(dbresolver.Read)
|
||||
}
|
||||
|
||||
func (p pointRecordDo) WriteDB() *pointRecordDo {
|
||||
return p.Clauses(dbresolver.Write)
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Session(config *gorm.Session) *pointRecordDo {
|
||||
return p.withDO(p.DO.Session(config))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Clauses(conds ...clause.Expression) *pointRecordDo {
|
||||
return p.withDO(p.DO.Clauses(conds...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Returning(value interface{}, columns ...string) *pointRecordDo {
|
||||
return p.withDO(p.DO.Returning(value, columns...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Not(conds ...gen.Condition) *pointRecordDo {
|
||||
return p.withDO(p.DO.Not(conds...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Or(conds ...gen.Condition) *pointRecordDo {
|
||||
return p.withDO(p.DO.Or(conds...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Select(conds ...field.Expr) *pointRecordDo {
|
||||
return p.withDO(p.DO.Select(conds...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Where(conds ...gen.Condition) *pointRecordDo {
|
||||
return p.withDO(p.DO.Where(conds...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Order(conds ...field.Expr) *pointRecordDo {
|
||||
return p.withDO(p.DO.Order(conds...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Distinct(cols ...field.Expr) *pointRecordDo {
|
||||
return p.withDO(p.DO.Distinct(cols...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Omit(cols ...field.Expr) *pointRecordDo {
|
||||
return p.withDO(p.DO.Omit(cols...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Join(table schema.Tabler, on ...field.Expr) *pointRecordDo {
|
||||
return p.withDO(p.DO.Join(table, on...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) LeftJoin(table schema.Tabler, on ...field.Expr) *pointRecordDo {
|
||||
return p.withDO(p.DO.LeftJoin(table, on...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) RightJoin(table schema.Tabler, on ...field.Expr) *pointRecordDo {
|
||||
return p.withDO(p.DO.RightJoin(table, on...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Group(cols ...field.Expr) *pointRecordDo {
|
||||
return p.withDO(p.DO.Group(cols...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Having(conds ...gen.Condition) *pointRecordDo {
|
||||
return p.withDO(p.DO.Having(conds...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Limit(limit int) *pointRecordDo {
|
||||
return p.withDO(p.DO.Limit(limit))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Offset(offset int) *pointRecordDo {
|
||||
return p.withDO(p.DO.Offset(offset))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *pointRecordDo {
|
||||
return p.withDO(p.DO.Scopes(funcs...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Unscoped() *pointRecordDo {
|
||||
return p.withDO(p.DO.Unscoped())
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Create(values ...*model.PointRecord) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return p.DO.Create(values)
|
||||
}
|
||||
|
||||
func (p pointRecordDo) CreateInBatches(values []*model.PointRecord, batchSize int) error {
|
||||
return p.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 (p pointRecordDo) Save(values ...*model.PointRecord) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return p.DO.Save(values)
|
||||
}
|
||||
|
||||
func (p pointRecordDo) First() (*model.PointRecord, error) {
|
||||
if result, err := p.DO.First(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*model.PointRecord), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Take() (*model.PointRecord, error) {
|
||||
if result, err := p.DO.Take(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*model.PointRecord), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Last() (*model.PointRecord, error) {
|
||||
if result, err := p.DO.Last(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*model.PointRecord), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Find() ([]*model.PointRecord, error) {
|
||||
result, err := p.DO.Find()
|
||||
return result.([]*model.PointRecord), err
|
||||
}
|
||||
|
||||
func (p pointRecordDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.PointRecord, err error) {
|
||||
buf := make([]*model.PointRecord, 0, batchSize)
|
||||
err = p.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 (p pointRecordDo) FindInBatches(result *[]*model.PointRecord, batchSize int, fc func(tx gen.Dao, batch int) error) error {
|
||||
return p.DO.FindInBatches(result, batchSize, fc)
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Attrs(attrs ...field.AssignExpr) *pointRecordDo {
|
||||
return p.withDO(p.DO.Attrs(attrs...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Assign(attrs ...field.AssignExpr) *pointRecordDo {
|
||||
return p.withDO(p.DO.Assign(attrs...))
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Joins(fields ...field.RelationField) *pointRecordDo {
|
||||
for _, _f := range fields {
|
||||
p = *p.withDO(p.DO.Joins(_f))
|
||||
}
|
||||
return &p
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Preload(fields ...field.RelationField) *pointRecordDo {
|
||||
for _, _f := range fields {
|
||||
p = *p.withDO(p.DO.Preload(_f))
|
||||
}
|
||||
return &p
|
||||
}
|
||||
|
||||
func (p pointRecordDo) FirstOrInit() (*model.PointRecord, error) {
|
||||
if result, err := p.DO.FirstOrInit(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*model.PointRecord), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p pointRecordDo) FirstOrCreate() (*model.PointRecord, error) {
|
||||
if result, err := p.DO.FirstOrCreate(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*model.PointRecord), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p pointRecordDo) FindByPage(offset int, limit int) (result []*model.PointRecord, count int64, err error) {
|
||||
result, err = p.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 = p.Offset(-1).Limit(-1).Count()
|
||||
return
|
||||
}
|
||||
|
||||
func (p pointRecordDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
|
||||
count, err = p.Count()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = p.Offset(offset).Limit(limit).Scan(result)
|
||||
return
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Scan(result interface{}) (err error) {
|
||||
return p.DO.Scan(result)
|
||||
}
|
||||
|
||||
func (p pointRecordDo) Delete(models ...*model.PointRecord) (result gen.ResultInfo, err error) {
|
||||
return p.DO.Delete(models)
|
||||
}
|
||||
|
||||
func (p *pointRecordDo) withDO(do gen.Dao) *pointRecordDo {
|
||||
p.DO = *do.(*gen.DO)
|
||||
return p
|
||||
}
|
||||
@@ -2,7 +2,6 @@ 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"
|
||||
@@ -14,10 +13,10 @@ type CategoryDao struct {
|
||||
cache *redis.CacheClient
|
||||
}
|
||||
|
||||
func NewCategoryDao(ctx context.Context, cache ...*redis.CacheClient) *CategoryDao {
|
||||
func NewCategoryDao(ctx context.Context, query *query.Query, cache ...*redis.CacheClient) *CategoryDao {
|
||||
dao := &CategoryDao{
|
||||
ctx: ctx,
|
||||
query: query.Use(mysql.GetDB(dbName)),
|
||||
query: query,
|
||||
}
|
||||
if len(cache) > 0 {
|
||||
dao.cache = cache[0]
|
||||
|
||||
@@ -3,6 +3,7 @@ package repository
|
||||
import (
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/mysql"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/query"
|
||||
)
|
||||
|
||||
var dbName mysql.DBName = "qgdzs_db"
|
||||
@@ -11,6 +12,10 @@ var (
|
||||
cacheBySn = "c:%v:s:%v"
|
||||
)
|
||||
|
||||
func Query() *query.Query {
|
||||
return query.Use(mysql.GetDB(dbName))
|
||||
}
|
||||
|
||||
func keyCacheBySn(sn string, tableName string) string {
|
||||
return fmt.Sprintf(cacheBySn, tableName, sn)
|
||||
}
|
||||
|
||||
52
internal/dao/repository/point_card.go
Normal file
52
internal/dao/repository/point_card.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/model"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/query"
|
||||
)
|
||||
|
||||
type PointCardDao struct {
|
||||
ctx context.Context
|
||||
query *query.Query
|
||||
cache *redis.CacheClient
|
||||
}
|
||||
|
||||
func NewPointCardDao(ctx context.Context, query *query.Query, cache ...*redis.CacheClient) *PointCardDao {
|
||||
dao := &PointCardDao{
|
||||
ctx: ctx,
|
||||
query: query,
|
||||
}
|
||||
if len(cache) > 0 {
|
||||
dao.cache = cache[0]
|
||||
}
|
||||
return dao
|
||||
}
|
||||
|
||||
func (d *PointCardDao) Create(pointCard *model.PointCard) (*model.PointCard, error) {
|
||||
err := d.query.PointCard.WithContext(d.ctx).
|
||||
Create(pointCard)
|
||||
return pointCard, err
|
||||
}
|
||||
|
||||
func (d *PointCardDao) IncrPointCard(usn string, point int64) error {
|
||||
info, err := d.query.PointCard.WithContext(d.ctx).
|
||||
Where(d.query.PointCard.UserSn.Eq(usn)).
|
||||
UpdateSimple(d.query.PointCard.Point.Add(point))
|
||||
if err != nil {
|
||||
return utils.ErrorsWrap(err)
|
||||
}
|
||||
if info.RowsAffected == 0 {
|
||||
return utils.ErrorsWrap(errors.New("user not found"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *PointCardDao) FindByUserSn(usn string) (*model.PointCard, error) {
|
||||
return d.query.PointCard.WithContext(d.ctx).
|
||||
Where(d.query.PointCard.UserSn.Eq(usn)).
|
||||
First()
|
||||
}
|
||||
58
internal/dao/repository/point_records.go
Normal file
58
internal/dao/repository/point_records.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/model"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/query"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type PointRecordsDao struct {
|
||||
ctx context.Context
|
||||
query *query.Query
|
||||
cache *redis.CacheClient
|
||||
}
|
||||
|
||||
func NewPointRecordsDao(ctx context.Context, query *query.Query, cache ...*redis.CacheClient) *PointRecordsDao {
|
||||
dao := &PointRecordsDao{
|
||||
ctx: ctx,
|
||||
query: query,
|
||||
}
|
||||
if len(cache) > 0 {
|
||||
dao.cache = cache[0]
|
||||
}
|
||||
return dao
|
||||
}
|
||||
|
||||
func (d *PointRecordsDao) CreateAndIncrPointCard(pointRecord *model.PointRecord) error {
|
||||
return d.query.Transaction(func(tx *query.Query) error {
|
||||
if err := tx.PointRecord.WithContext(d.ctx).
|
||||
Create(pointRecord); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pcd := NewPointCardDao(d.ctx, tx)
|
||||
if _, err := pcd.FindByUserSn(pointRecord.UserSn); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
_, err = pcd.Create(&model.PointCard{
|
||||
UserSn: pointRecord.UserSn,
|
||||
Point: pointRecord.Point,
|
||||
})
|
||||
if err != nil {
|
||||
return utils.ErrorsWrap(err)
|
||||
}
|
||||
} else {
|
||||
return utils.ErrorsWrap(err)
|
||||
}
|
||||
} else {
|
||||
if err = pcd.IncrPointCard(pointRecord.UserSn, pointRecord.Point); err != nil {
|
||||
return utils.ErrorsWrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -2,7 +2,6 @@ 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-qgdzs/internal/dao/model"
|
||||
@@ -17,10 +16,10 @@ type QuestionDao struct {
|
||||
cache *redis.CacheClient
|
||||
}
|
||||
|
||||
func NewQuestionDao(ctx context.Context, cache ...*redis.CacheClient) *QuestionDao {
|
||||
func NewQuestionDao(ctx context.Context, query *query.Query, cache ...*redis.CacheClient) *QuestionDao {
|
||||
dao := &QuestionDao{
|
||||
ctx: ctx,
|
||||
query: query.Use(mysql.GetDB(dbName)),
|
||||
query: query,
|
||||
}
|
||||
if len(cache) > 0 {
|
||||
dao.cache = cache[0]
|
||||
@@ -73,23 +72,3 @@ func (d *QuestionDao) FindBySn(sn string) (*model.Question, error) {
|
||||
}
|
||||
return first, nil
|
||||
}
|
||||
|
||||
func (d *QuestionDao) FindByCategory(categorySn string) (*model.Question, error) {
|
||||
count, err := d.query.Question.WithContext(d.ctx).
|
||||
Where(d.query.Question.CategorySn.Eq(categorySn)).
|
||||
Count()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count == 0 {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
first, err := d.query.Question.WithContext(d.ctx).
|
||||
Where(d.query.Question.CategorySn.Eq(categorySn)).
|
||||
Offset(utils.RandInt(0, int(count-1))).
|
||||
First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return first, nil
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ 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"
|
||||
@@ -15,10 +14,10 @@ type RecordDao struct {
|
||||
cache *redis.CacheClient
|
||||
}
|
||||
|
||||
func NewRecordDao(ctx context.Context, cache ...*redis.CacheClient) *RecordDao {
|
||||
func NewRecordDao(ctx context.Context, query *query.Query, cache ...*redis.CacheClient) *RecordDao {
|
||||
dao := &RecordDao{
|
||||
ctx: ctx,
|
||||
query: query.Use(mysql.GetDB(dbName)),
|
||||
query: query,
|
||||
}
|
||||
if len(cache) > 0 {
|
||||
dao.cache = cache[0]
|
||||
|
||||
Reference in New Issue
Block a user