增加统一加资产接口
This commit is contained in:
@@ -4,15 +4,18 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
ea "github.com/earn-alliance/earnalliance-go"
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
"nova_task/internal/config"
|
||||
"nova_task/internal/consts"
|
||||
"nova_task/internal/middleware"
|
||||
"nova_task/internal/model"
|
||||
"nova_task/internal/pkg/errs"
|
||||
"nova_task/internal/types"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
@@ -145,3 +148,56 @@ func (s *ServiceContext) GetUidByEmail(ctx context.Context, email string) (uint,
|
||||
}
|
||||
return u.Id, nil
|
||||
}
|
||||
|
||||
func (s *ServiceContext) AddUserAsset(ctx context.Context, uid uint, asset consts.AssetType, amount decimal.Decimal, remark string, eventId uint64, provideUid uint, referralReward bool) error {
|
||||
var err error
|
||||
switch asset {
|
||||
case consts.AssetType_Points:
|
||||
err = s.TaskAssetModel.AddPoint(ctx, uid, amount)
|
||||
case consts.AssetType_Castile:
|
||||
err = s.TaskAssetModel.AddCastile(ctx, uid, amount)
|
||||
case consts.AssetType_Elite_Points:
|
||||
err = s.TaskAssetModel.AddElitePoints(ctx, uid, amount)
|
||||
case consts.AssetType_Keys:
|
||||
err = s.TaskAssetModel.AddKeys(ctx, uid, int(amount.IntPart()))
|
||||
default:
|
||||
return errors.New("unknown asset type")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.TaskAssetRecordModel.Insert(ctx, &model.NhTaskAssetRecord{
|
||||
Uid: int(uid),
|
||||
EventId: eventId,
|
||||
AssetField: string(asset),
|
||||
Count: amount.InexactFloat64(),
|
||||
Remark: remark,
|
||||
ProvideUid: provideUid,
|
||||
CreateTime: int(time.Now().Unix()),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !referralReward {
|
||||
return nil
|
||||
}
|
||||
// 返利给上级
|
||||
pb, err := s.PromoteBindModel.FindOneByInvitedUid(ctx, uid)
|
||||
if err != nil {
|
||||
if !errors.Is(err, model.ErrNotFound) {
|
||||
logx.Errorw("find promote bind error", logx.Field("err", err), logx.Field("uid", uid))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if pb.ShareUid == 0 {
|
||||
return nil
|
||||
}
|
||||
var rate = 5
|
||||
if s.NftHolderModel.HoldNft(ctx, pb.ShareUid) {
|
||||
rate = 15
|
||||
} else if s.AmbassadorModel.IsAmbassador(ctx, pb.ShareUid) {
|
||||
rate = 10
|
||||
}
|
||||
rewardAmount := amount.Mul(decimal.NewFromFloat(float64(rate) / 100))
|
||||
return s.AddUserAsset(ctx, pb.ShareUid, asset, rewardAmount, "referral reward", 0, uid, false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user