53 lines
1.3 KiB
Go
53 lines
1.3 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"
|
|
)
|
|
|
|
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()
|
|
}
|