59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
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
|
|
})
|
|
}
|