96 lines
2.9 KiB
Go
96 lines
2.9 KiB
Go
package task
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/go-sql-driver/mysql"
|
|
"github.com/spf13/cast"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"nova_task/internal/model"
|
|
"nova_task/internal/pkg/errs"
|
|
"nova_task/internal/pkg/utils"
|
|
"nova_task/internal/svc"
|
|
"nova_task/internal/types"
|
|
)
|
|
|
|
type StakeNftLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewStakeNftLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StakeNftLogic {
|
|
return &StakeNftLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *StakeNftLogic) StakeNft(req *types.StakeNftList) error {
|
|
uid := utils.GetUidUint(l.ctx)
|
|
|
|
for _, tokenId := range req.TokenIds {
|
|
tkHolder, err := l.svcCtx.NftHolderModel.FindOneByTokenId(l.ctx, tokenId)
|
|
if err != nil {
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
l.Errorw("find nft holder not exist", logx.Field("err", err), logx.Field("uid", uid), logx.Field("tokenId", tokenId))
|
|
continue
|
|
}
|
|
l.Errorw("find nft holder failed", logx.Field("err", err), logx.Field("uid", uid), logx.Field("tokenId", tokenId))
|
|
return errs.New(errs.ErrDatabaseOperate, err)
|
|
}
|
|
wUid, err := l.svcCtx.WalletModel.FindUidByAddress(l.ctx, tkHolder.Address)
|
|
if err != nil {
|
|
if !errors.Is(err, model.ErrNotFound) {
|
|
l.Errorw("find wallet by address failed", logx.Field("err", err), logx.Field("uid", uid), logx.Field("address", tkHolder.Address))
|
|
return errs.New(errs.ErrDatabaseOperate, err)
|
|
}
|
|
return errs.New(errs.ErrNftNotBelongToUser, "nft not belong to user")
|
|
}
|
|
if wUid != uid {
|
|
l.Errorw("nft not belong to user", logx.Field("uid", uid), logx.Field("address", tkHolder.Address))
|
|
return errs.New(errs.ErrNftNotBelongToUser, "nft not belong to user")
|
|
}
|
|
var sns []int8
|
|
var propertyId string
|
|
var tarotType int8
|
|
if utils.IsBigTarot(tokenId) {
|
|
sns = []int8{1, 2}
|
|
propertyId = "405005"
|
|
tarotType = 1
|
|
} else {
|
|
sns = []int8{1}
|
|
propertyId = "405105"
|
|
}
|
|
err = l.svcCtx.StakeNftModel.StakeNft(l.ctx, uid, req.RoleId, tokenId, tarotType)
|
|
if err != nil {
|
|
l.Errorw("stake nft failed", logx.Field("err", err), logx.Field("uid", uid), logx.Field("tokenIds", req.TokenIds))
|
|
return errs.New(errs.ErrDatabaseOperate, err)
|
|
}
|
|
|
|
if req.RoleId > 0 {
|
|
for _, sn := range sns {
|
|
_, err = l.svcCtx.StakePropertyModel.Insert(l.ctx, &model.NhNftStakeProperty{
|
|
Uid: uid,
|
|
RoleId: int64(req.RoleId),
|
|
TokenId: cast.ToUint(tokenId),
|
|
PropertyId: propertyId,
|
|
Sn: sn,
|
|
})
|
|
if err != nil {
|
|
errMySQL := new(mysql.MySQLError)
|
|
if errors.As(err, &errMySQL) {
|
|
switch errMySQL.Number {
|
|
case 1062:
|
|
default:
|
|
l.Errorw("insert stake property failed", logx.Field("err", err), logx.Field("uid", uid), logx.Field("tokenId", tokenId), logx.Field("propertyId", propertyId))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return errs.Success()
|
|
}
|