nft质押道具发放
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/spf13/cast"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"nova_task/internal/model"
|
||||
"nova_task/internal/pkg/utils"
|
||||
"nova_task/internal/svc"
|
||||
"time"
|
||||
@@ -28,6 +29,7 @@ func (c *Cron) Spec() string {
|
||||
}
|
||||
|
||||
func (c *Cron) Run() {
|
||||
logx.Debugw("run settle cron task")
|
||||
start, end, err := c.svcCtx.ConfigModel.GetNftStakeTaskOpenDate(c.ctx)
|
||||
if err != nil {
|
||||
logx.Errorw("get nft stake task open date failed", logx.Field("err", err))
|
||||
@@ -95,9 +97,24 @@ func (c *Cron) Run() {
|
||||
continue
|
||||
}
|
||||
|
||||
// todo: 增加资产
|
||||
//c.svcCtx.TaskAssetModel.AddUserPoint()
|
||||
// 加资产
|
||||
err = c.svcCtx.TaskAssetModel.AddCastile(c.ctx, uid, reward)
|
||||
if err != nil {
|
||||
logx.Errorw("add castile failed", logx.Field("err", err), logx.Field("uid", uid), logx.Field("awardSeq", awardSeq), logx.Field("reward", reward))
|
||||
continue
|
||||
}
|
||||
// 加资产记录表
|
||||
_, err = c.svcCtx.TaskAssetRecordModel.Insert(c.ctx, &model.NhTaskAssetRecord{
|
||||
Uid: int(uid),
|
||||
EventId: 0,
|
||||
AssetField: "castile",
|
||||
Count: reward.InexactFloat64(),
|
||||
Remark: "nft软质押奖励",
|
||||
ProvideUid: 0,
|
||||
CreateTime: int(now.Unix()),
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorw("insert task asset record failed", logx.Field("err", err), logx.Field("uid", uid), logx.Field("awardSeq", awardSeq), logx.Field("reward", reward))
|
||||
}
|
||||
}
|
||||
|
||||
logx.Debugw("run settle cron task")
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ func (l *GetTaskRewardLogic) GetTaskReward(req *types.TaskIdPath) (*types.GetTas
|
||||
return err
|
||||
}
|
||||
// 给予用户奖励
|
||||
err = l.svcCtx.TaskAssetModel.WithSession(session).AddUserPoint(l.ctx, uid, task.Points)
|
||||
err = l.svcCtx.TaskAssetModel.WithSession(session).AddPoint(l.ctx, uid, task.Points)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2,12 +2,15 @@ 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"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type StakeNftLogic struct {
|
||||
@@ -32,5 +35,35 @@ func (l *StakeNftLogic) StakeNft(req *types.StakeNftList) error {
|
||||
return errs.New(errs.ErrDatabaseOperate, err)
|
||||
}
|
||||
|
||||
for _, tokenId := range req.TokenIds {
|
||||
var sns []int8
|
||||
var propertyId string
|
||||
if utils.IsBigTarot(tokenId) {
|
||||
sns = []int8{1, 2}
|
||||
propertyId = "402505"
|
||||
} else {
|
||||
sns = []int8{1}
|
||||
propertyId = "402605"
|
||||
}
|
||||
for _, sn := range sns {
|
||||
_, err = l.svcCtx.StakePropertyModel.Insert(l.ctx, &model.NhNftStakeProperty{
|
||||
Uid: uid,
|
||||
RoleId: int64(req.RoleId),
|
||||
TokenId: cast.ToUint(tokenId),
|
||||
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()
|
||||
}
|
||||
|
||||
29
internal/model/nh_nft_stake_property_model.go
Executable file
29
internal/model/nh_nft_stake_property_model.go
Executable file
@@ -0,0 +1,29 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var _ NhNftStakePropertyModel = (*customNhNftStakePropertyModel)(nil)
|
||||
|
||||
type (
|
||||
// NhNftStakePropertyModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customNhNftStakePropertyModel.
|
||||
NhNftStakePropertyModel interface {
|
||||
nhNftStakePropertyModel
|
||||
withSession(session sqlx.Session) NhNftStakePropertyModel
|
||||
}
|
||||
|
||||
customNhNftStakePropertyModel struct {
|
||||
*defaultNhNftStakePropertyModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewNhNftStakePropertyModel returns a model for the database table.
|
||||
func NewNhNftStakePropertyModel(conn sqlx.SqlConn) NhNftStakePropertyModel {
|
||||
return &customNhNftStakePropertyModel{
|
||||
defaultNhNftStakePropertyModel: newNhNftStakePropertyModel(conn),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customNhNftStakePropertyModel) withSession(session sqlx.Session) NhNftStakePropertyModel {
|
||||
return NewNhNftStakePropertyModel(sqlx.NewSqlConnFromSession(session))
|
||||
}
|
||||
111
internal/model/nh_nft_stake_property_model_gen.go
Executable file
111
internal/model/nh_nft_stake_property_model_gen.go
Executable file
@@ -0,0 +1,111 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.7.3
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
nhNftStakePropertyFieldNames = builder.RawFieldNames(&NhNftStakeProperty{})
|
||||
nhNftStakePropertyRows = strings.Join(nhNftStakePropertyFieldNames, ",")
|
||||
nhNftStakePropertyRowsExpectAutoSet = strings.Join(stringx.Remove(nhNftStakePropertyFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
nhNftStakePropertyRowsWithPlaceHolder = strings.Join(stringx.Remove(nhNftStakePropertyFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
)
|
||||
|
||||
type (
|
||||
nhNftStakePropertyModel interface {
|
||||
Insert(ctx context.Context, data *NhNftStakeProperty) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id uint) (*NhNftStakeProperty, error)
|
||||
FindOneByTokenIdSn(ctx context.Context, tokenId uint, sn int8) (*NhNftStakeProperty, error)
|
||||
Update(ctx context.Context, data *NhNftStakeProperty) error
|
||||
Delete(ctx context.Context, id uint) error
|
||||
}
|
||||
|
||||
defaultNhNftStakePropertyModel struct {
|
||||
conn sqlx.SqlConn
|
||||
table string
|
||||
}
|
||||
|
||||
NhNftStakeProperty struct {
|
||||
Id uint `db:"id"`
|
||||
Uid uint `db:"uid"` // 用户ID
|
||||
RoleId int64 `db:"role_id"` // 角色id
|
||||
TokenId uint `db:"token_id"` // NFT tokenId
|
||||
PropertyId string `db:"property_id"` // 道具ID
|
||||
Sn int8 `db:"sn"` // 序号
|
||||
CallbackStatus int8 `db:"callback_status"` // 下发通知状态:0未通知,1已通知,2通知异常
|
||||
CallbackNum int `db:"callback_num"` // 交易成功发送通知次数
|
||||
CallbackAt sql.NullTime `db:"callback_at"` // 发送通知最新时间
|
||||
CallbackRemark string `db:"callback_remark"` // 通知回调备注
|
||||
CreatedAt time.Time `db:"created_at"` // 创建时间
|
||||
UpdatedAt time.Time `db:"updated_at"` // 修改时间
|
||||
}
|
||||
)
|
||||
|
||||
func newNhNftStakePropertyModel(conn sqlx.SqlConn) *defaultNhNftStakePropertyModel {
|
||||
return &defaultNhNftStakePropertyModel{
|
||||
conn: conn,
|
||||
table: "`nh_nft_stake_property`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultNhNftStakePropertyModel) Delete(ctx context.Context, id uint) error {
|
||||
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||||
_, err := m.conn.ExecCtx(ctx, query, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultNhNftStakePropertyModel) FindOne(ctx context.Context, id uint) (*NhNftStakeProperty, error) {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", nhNftStakePropertyRows, m.table)
|
||||
var resp NhNftStakeProperty
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultNhNftStakePropertyModel) FindOneByTokenIdSn(ctx context.Context, tokenId uint, sn int8) (*NhNftStakeProperty, error) {
|
||||
var resp NhNftStakeProperty
|
||||
query := fmt.Sprintf("select %s from %s where `token_id` = ? and `sn` = ? limit 1", nhNftStakePropertyRows, m.table)
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, tokenId, sn)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultNhNftStakePropertyModel) Insert(ctx context.Context, data *NhNftStakeProperty) (sql.Result, error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, nhNftStakePropertyRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.Uid, data.RoleId, data.TokenId, data.PropertyId, data.Sn, data.CallbackStatus, data.CallbackNum, data.CallbackAt, data.CallbackRemark)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultNhNftStakePropertyModel) Update(ctx context.Context, newData *NhNftStakeProperty) error {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, nhNftStakePropertyRowsWithPlaceHolder)
|
||||
_, err := m.conn.ExecCtx(ctx, query, newData.Uid, newData.RoleId, newData.TokenId, newData.PropertyId, newData.Sn, newData.CallbackStatus, newData.CallbackNum, newData.CallbackAt, newData.CallbackRemark, newData.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultNhNftStakePropertyModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package model
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"time"
|
||||
)
|
||||
@@ -15,7 +16,8 @@ type (
|
||||
NhTaskAssetModel interface {
|
||||
nhTaskAssetModel
|
||||
WithSession(session sqlx.Session) NhTaskAssetModel
|
||||
AddUserPoint(ctx context.Context, uid int, points int) error
|
||||
AddPoint(ctx context.Context, uid int, points int) error
|
||||
AddCastile(ctx context.Context, uid uint, castile decimal.Decimal) error
|
||||
}
|
||||
|
||||
customNhTaskAssetModel struct {
|
||||
@@ -23,6 +25,12 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func (m *customNhTaskAssetModel) AddCastile(ctx context.Context, uid uint, castile decimal.Decimal) error {
|
||||
insertOrUpdate := fmt.Sprintf("INSERT INTO %s (`uid`, `castile`, `create_time`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `castile` = `castile` + ?", m.table)
|
||||
_, err := m.conn.ExecCtx(ctx, insertOrUpdate, uid, castile, time.Now().Unix(), castile)
|
||||
return err
|
||||
}
|
||||
|
||||
// NewNhTaskAssetModel returns a model for the database table.
|
||||
func NewNhTaskAssetModel(conn sqlx.SqlConn) NhTaskAssetModel {
|
||||
return &customNhTaskAssetModel{
|
||||
@@ -34,7 +42,7 @@ func (m *customNhTaskAssetModel) WithSession(session sqlx.Session) NhTaskAssetMo
|
||||
return NewNhTaskAssetModel(sqlx.NewSqlConnFromSession(session))
|
||||
}
|
||||
|
||||
func (m *customNhTaskAssetModel) AddUserPoint(ctx context.Context, uid int, points int) error {
|
||||
func (m *customNhTaskAssetModel) AddPoint(ctx context.Context, uid int, points int) error {
|
||||
insertOrUpdate := fmt.Sprintf("INSERT INTO %s (`uid`, `points`, `create_time`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `points` = `points` + ?", m.table)
|
||||
_, err := m.conn.ExecCtx(ctx, insertOrUpdate, uid, points, time.Now().Unix(), points)
|
||||
return err
|
||||
|
||||
@@ -45,6 +45,7 @@ type (
|
||||
ElitePoints decimal.Decimal `db:"elite_points"` // 高级积分
|
||||
Keys int `db:"keys"` // 宝箱钥匙
|
||||
CreateTime int `db:"create_time"` // 创建时间
|
||||
Castile decimal.Decimal `db:"castile"` // castile积分
|
||||
}
|
||||
)
|
||||
|
||||
@@ -90,14 +91,14 @@ func (m *defaultNhTaskAssetModel) FindOneByUid(ctx context.Context, uid int) (*N
|
||||
}
|
||||
|
||||
func (m *defaultNhTaskAssetModel) Insert(ctx context.Context, data *NhTaskAsset) (sql.Result, error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, nhTaskAssetRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.Uid, data.Points, data.ElitePoints, data.Keys)
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?)", m.table, nhTaskAssetRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.Uid, data.Points, data.ElitePoints, data.Keys, data.Castile)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultNhTaskAssetModel) Update(ctx context.Context, newData *NhTaskAsset) error {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, nhTaskAssetRowsWithPlaceHolder)
|
||||
_, err := m.conn.ExecCtx(ctx, query, newData.Uid, newData.Points, newData.ElitePoints, newData.Keys, newData.Id)
|
||||
_, err := m.conn.ExecCtx(ctx, query, newData.Uid, newData.Points, newData.ElitePoints, newData.Keys, newData.Castile, newData.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -26,8 +26,9 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
// SetReward 对未发送奖励的用户发送奖励,支持并发,且不会重发
|
||||
func (m *customNhTaskNftStakeRewardModel) SetReward(ctx context.Context, uid uint, awardSeq, occupyPercent int, pledgeOutput, reward decimal.Decimal) error {
|
||||
update := fmt.Sprintf("UPDATE %s SET `occupy_percent` = ?, `pledge_output` = ?, `reward` = ? WHERE `uid` = ? AND `award_seq` = ?", m.table)
|
||||
update := fmt.Sprintf("UPDATE %s SET `occupy_percent` = ?, `pledge_output` = ?, `reward` = ?, `sent` = 1 WHERE `uid` = ? AND `award_seq` = ?, `sent` = 0", m.table)
|
||||
result, err := m.conn.ExecCtx(ctx, update, occupyPercent, pledgeOutput, reward, uid, awardSeq)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -29,6 +29,7 @@ type ServiceContext struct {
|
||||
StakeNftLogModel model.NhTaskNftStakeLogModel
|
||||
StakeRewardModel model.NhTaskNftStakeRewardModel
|
||||
GamePitModel model.NhGamePitModel
|
||||
StakePropertyModel model.NhNftStakePropertyModel
|
||||
|
||||
Earn *ea.Client
|
||||
DBConn sqlx.SqlConn
|
||||
@@ -57,6 +58,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
StakeNftLogModel: model.NewNhTaskNftStakeLogModel(dbConn),
|
||||
StakeRewardModel: model.NewNhTaskNftStakeRewardModel(dbConn),
|
||||
GamePitModel: model.NewNhGamePitModel(dbConn),
|
||||
StakePropertyModel: model.NewNhNftStakePropertyModel(dbConn),
|
||||
|
||||
Earn: c.Earn.BuildEarnClient(),
|
||||
DBConn: dbConn,
|
||||
|
||||
Reference in New Issue
Block a user