软质押增加奖励发放记录

This commit is contained in:
lianghuanjie
2025-01-08 16:25:48 +08:00
parent 29abba438c
commit 5c4862fd70
8 changed files with 189 additions and 2 deletions

View File

@@ -107,6 +107,12 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/nft/stake",
Handler: task.StakeNftHandler(serverCtx),
},
{
// 质押奖励发放列表
Method: http.MethodGet,
Path: "/nft/stake_reward",
Handler: task.StakeRewardListHandler(serverCtx),
},
{
// 取消质押NFT
Method: http.MethodPost,

View File

@@ -0,0 +1,22 @@
package task
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"nova_task/internal/logic/task"
"nova_task/internal/svc"
)
// 质押奖励发放列表
func StakeRewardListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := task.NewStakeRewardListLogic(r.Context(), svcCtx)
resp, err := l.StakeRewardList()
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@@ -86,6 +86,11 @@ func (l *GetStakeTaskDetailLogic) GetStakeTaskDetail() (*types.StakeTaskDetail,
canReceiveTokens *= coefficient
totalIncomeReward, err := l.svcCtx.StakeRewardModel.CountReward(l.ctx, uid)
if err != nil {
l.Errorw("count reward failed", logx.Field("err", err), logx.Field("uid", uid))
}
return &types.StakeTaskDetail{
StartDate: start.Format(time.DateOnly),
EndDate: end.Format(time.DateOnly),
@@ -93,5 +98,6 @@ func (l *GetStakeTaskDetailLogic) GetStakeTaskDetail() (*types.StakeTaskDetail,
ProduceTokensToday: produceTokensToday,
GameBonus: gameBonus,
CanReceiveTokens: canReceiveTokens,
TotalIncomeTokens: totalIncomeReward,
}, nil
}

View File

@@ -0,0 +1,52 @@
package task
import (
"context"
"github.com/spf13/cast"
"nova_task/internal/pkg/errs"
"nova_task/internal/pkg/utils"
"time"
"nova_task/internal/svc"
"nova_task/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type StakeRewardListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 质押奖励发放列表
func NewStakeRewardListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StakeRewardListLogic {
return &StakeRewardListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *StakeRewardListLogic) StakeRewardList() (*types.StakeRewardList, error) {
uid := utils.GetUidUint(l.ctx)
rws, err := l.svcCtx.StakeRewardModel.FindRewardsByUid(l.ctx, uid)
if err != nil {
l.Errorw("find rewards by uid failed", logx.Field("err", err))
return nil, errs.New(errs.ErrDatabaseOperate, err)
}
var rs []types.StakeReward
for _, rw := range rws {
date, err := time.Parse("20060102", cast.ToString(rw.AwardSeq))
if err != nil {
l.Errorw("parse date failed", logx.Field("err", err), logx.Field("awardSeq", rw.AwardSeq))
continue
}
rs = append(rs, types.StakeReward{
Date: date.Format(time.DateOnly),
Reward: rw.Reward.InexactFloat64(),
})
}
return &types.StakeRewardList{RewardList: rs}, nil
}

View File

@@ -19,6 +19,8 @@ type (
withSession(session sqlx.Session) NhTaskNftStakeRewardModel
GetRandomCoefficientByUid(ctx context.Context, uid uint, awardSeq int, min, max float64) (float64, error)
SetReward(ctx context.Context, uid uint, awardSeq, occupyPercent int, pledgeOutput, reward decimal.Decimal) error
CountReward(ctx context.Context, uid uint) (float64, error)
FindRewardsByUid(ctx context.Context, uid uint) ([]*NhTaskNftStakeReward, error)
}
customNhTaskNftStakeRewardModel struct {
@@ -26,6 +28,23 @@ type (
}
)
func (m *customNhTaskNftStakeRewardModel) CountReward(ctx context.Context, uid uint) (float64, error) {
query := fmt.Sprintf("SELECT SUM(`reward`) FROM %s WHERE `uid` = ? AND `sent` = 1", m.table)
var reward float64
err := m.conn.QueryRowCtx(ctx, &reward, query, uid)
return reward, err
}
func (m *customNhTaskNftStakeRewardModel) FindRewardsByUid(ctx context.Context, uid uint) ([]*NhTaskNftStakeReward, error) {
query := fmt.Sprintf("SELECT * FROM %s WHERE `uid` = ? AND `sent` = 1", m.table)
var list []*NhTaskNftStakeReward
err := m.conn.QueryRowsCtx(ctx, &list, query, uid)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
return nil, err
}
return list, nil
}
// 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` = ?, `sent` = 1 WHERE `uid` = ? AND `award_seq` = ? AND `sent` = 0", m.table)

View File

@@ -63,6 +63,15 @@ type StakeNftList struct {
TokenIds []string `json:"token_ids"` // nft列表
}
type StakeReward struct {
Date string `json:"date"` // 日期
Reward float64 `json:"reward"` // 当日发放奖励
}
type StakeRewardList struct {
RewardList []StakeReward `json:"reward_list"`
}
type StakeTaskDetail struct {
StartDate string `json:"start_date"` // 开始日期
EndDate string `json:"end_date"` // 结束日期
@@ -70,6 +79,7 @@ type StakeTaskDetail struct {
ProduceTokensToday float64 `json:"produce_token_today"` // 今日产出代币
GameBonus int `json:"game_bonus"` // 游戏加成比率
CanReceiveTokens float64 `json:"can_receive_tokens"` // 可领取代币数量
TotalIncomeTokens float64 `json:"total_income_tokens"` // 累计收益
}
type Task struct {