91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
package earn
|
|
|
|
import (
|
|
"context"
|
|
ea "github.com/earn-alliance/earnalliance-go"
|
|
"github.com/robfig/cron/v3"
|
|
"github.com/spf13/cast"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"nova_task/internal/consts"
|
|
"nova_task/internal/svc"
|
|
)
|
|
|
|
// Cron 用户数据上报earn平台定时任务
|
|
type Cron struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewCron(ctx context.Context, svcCtx *svc.ServiceContext) cron.Job {
|
|
e := &Cron{ctx: ctx, svcCtx: svcCtx}
|
|
if e.svcCtx.Config.EarnCorn.RunOnStart {
|
|
e.Run()
|
|
}
|
|
return e
|
|
}
|
|
|
|
func (c *Cron) Spec() string {
|
|
return c.svcCtx.Config.EarnCorn.Spec
|
|
}
|
|
|
|
func (c *Cron) Run() {
|
|
logx.Debugw("run earn cron task")
|
|
conf, err := c.svcCtx.ConfigModel.FindOneByName(c.ctx, consts.EarnAllianceInviterId)
|
|
if err != nil {
|
|
logx.Errorw("find earn alliance inviter id failed", logx.Field("err", err))
|
|
return
|
|
}
|
|
c.pushUserInfo(cast.ToUint(conf.Value))
|
|
c.pushUserBind(cast.ToUint(conf.Value))
|
|
}
|
|
|
|
func (c *Cron) pushUserInfo(shareId uint) {
|
|
us, err := c.svcCtx.PromoteBindModel.FindRequirePushUser(c.ctx, shareId)
|
|
if err != nil {
|
|
logx.Errorw("find require push user failed", logx.Field("err", err), logx.Field("share_id", shareId))
|
|
return
|
|
}
|
|
logx.Debugw("find require push user", logx.Field("count", len(us)))
|
|
for _, u := range us {
|
|
ui, err := c.svcCtx.UserModel.FindOne(c.ctx, u.InvitedUid)
|
|
if err != nil {
|
|
logx.Errorw("find user failed", logx.Field("err", err), logx.Field("uid", u.InvitedUid))
|
|
continue
|
|
}
|
|
var twitterId string
|
|
ut, err := c.svcCtx.TwitterModel.FindOne(c.ctx, u.InvitedUid)
|
|
if err == nil {
|
|
twitterId = ut.TwitterId
|
|
}
|
|
|
|
err = c.svcCtx.PromoteBindModel.UpdatePushUser(c.ctx, u.Id)
|
|
if err != nil {
|
|
logx.Errorw("update push user failed", logx.Field("err", err), logx.Field("uid", u.InvitedUid), logx.Field("twitter_id", twitterId), logx.Field("email", ui.Email))
|
|
} else {
|
|
c.svcCtx.Earn.SetIdentifiers(cast.ToString(ui.Id), &ea.Identifiers{
|
|
Email: ea.IdentifierFrom(ui.Email),
|
|
TwitterId: ea.IdentifierFrom(twitterId),
|
|
})
|
|
logx.Infow("push user info success", logx.Field("uid", u.InvitedUid), logx.Field("twitter_id", twitterId), logx.Field("email", ui.Email))
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Cron) pushUserBind(shareId uint) {
|
|
us, err := c.svcCtx.TouristBindModel.FindRequirePushUser(c.ctx, shareId)
|
|
if err != nil {
|
|
logx.Errorw("find require push bind role user failed", logx.Field("err", err), logx.Field("share_id", shareId))
|
|
return
|
|
}
|
|
logx.Debugw("find require push bind role user", logx.Field("count", len(us)))
|
|
for _, u := range us {
|
|
err = c.svcCtx.PromoteBindModel.UpdatePushRole(c.ctx, u.Id)
|
|
if err != nil {
|
|
logx.Errorw("update push user failed", logx.Field("err", err), logx.Field("uid", u.Uid))
|
|
} else {
|
|
c.svcCtx.Earn.Track(cast.ToString(u.Uid), "BIND_ROLE", nil, nil)
|
|
logx.Infow("push user info success", logx.Field("uid", u.Uid))
|
|
}
|
|
}
|
|
}
|