feat kafka 改版 2
This commit is contained in:
@@ -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