nft质押任务逻辑
This commit is contained in:
52
internal/model/nh_task_nft_stake_model.go
Executable file
52
internal/model/nh_task_nft_stake_model.go
Executable file
@@ -0,0 +1,52 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ NhTaskNftStakeModel = (*customNhTaskNftStakeModel)(nil)
|
||||
|
||||
type (
|
||||
// NhTaskNftStakeModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customNhTaskNftStakeModel.
|
||||
NhTaskNftStakeModel interface {
|
||||
nhTaskNftStakeModel
|
||||
withSession(session sqlx.Session) NhTaskNftStakeModel
|
||||
StakeNft(ctx context.Context, uid uint, tokens []string) error
|
||||
UnStakeNft(ctx context.Context, uid uint, token string) error
|
||||
}
|
||||
|
||||
customNhTaskNftStakeModel struct {
|
||||
*defaultNhTaskNftStakeModel
|
||||
}
|
||||
)
|
||||
|
||||
func (m *customNhTaskNftStakeModel) UnStakeNft(ctx context.Context, uid uint, token string) error {
|
||||
update := fmt.Sprintf("UPDATE %s SET `state` = 0 WHERE `uid` = ? AND `token_id` = ?", m.table)
|
||||
_, err := m.conn.ExecCtx(ctx, update, uid, token)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *customNhTaskNftStakeModel) StakeNft(ctx context.Context, uid uint, tokens []string) error {
|
||||
insertOrUpdate := fmt.Sprintf("INSERT INTO %s (`uid`, `token_id`, `state`) VALUES (?, ?, 1) ON DUPLICATE KEY UPDATE `state` = 1", m.table)
|
||||
for _, token := range tokens {
|
||||
_, err := m.conn.ExecCtx(ctx, insertOrUpdate, uid, token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewNhTaskNftStakeModel returns a model for the database table.
|
||||
func NewNhTaskNftStakeModel(conn sqlx.SqlConn) NhTaskNftStakeModel {
|
||||
return &customNhTaskNftStakeModel{
|
||||
defaultNhTaskNftStakeModel: newNhTaskNftStakeModel(conn),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customNhTaskNftStakeModel) withSession(session sqlx.Session) NhTaskNftStakeModel {
|
||||
return NewNhTaskNftStakeModel(sqlx.NewSqlConnFromSession(session))
|
||||
}
|
||||
Reference in New Issue
Block a user