85 lines
2.4 KiB
Go
85 lines
2.4 KiB
Go
package earn
|
|
|
|
import (
|
|
"context"
|
|
ea "github.com/earn-alliance/earnalliance-go"
|
|
"github.com/spf13/cast"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"nova_task/internal/consts"
|
|
"nova_task/internal/svc"
|
|
)
|
|
|
|
// Earn 用户数据上报earn平台定时任务
|
|
type Earn struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewEarn(ctx context.Context, svcCtx *svc.ServiceContext) *Earn {
|
|
e := &Earn{ctx: ctx, svcCtx: svcCtx}
|
|
e.Run()
|
|
return e
|
|
}
|
|
|
|
func (e *Earn) Spec() string {
|
|
return "@every 5m"
|
|
}
|
|
|
|
func (e *Earn) Run() {
|
|
c, err := e.svcCtx.ConfigModel.FindOneByName(e.ctx, consts.EarnAllianceInviterId)
|
|
if err != nil {
|
|
logx.Errorw("find earn alliance inviter id failed", logx.Field("err", err))
|
|
return
|
|
}
|
|
e.pushUserInfo(cast.ToUint(c.Value))
|
|
e.pushUserBind(cast.ToUint(c.Value))
|
|
}
|
|
|
|
func (e *Earn) pushUserInfo(shareId uint) {
|
|
us, err := e.svcCtx.PromoteBindModel.FindRequirePushUser(e.ctx, shareId)
|
|
if err != nil {
|
|
logx.Errorw("find require push user failed", logx.Field("err", err), logx.Field("share_id", shareId))
|
|
return
|
|
}
|
|
for _, u := range us {
|
|
ui, err := e.svcCtx.UserModel.FindOne(e.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 := e.svcCtx.TwitterModel.FindOne(e.ctx, u.InvitedUid)
|
|
if err == nil {
|
|
twitterId = ut.TwitterId
|
|
}
|
|
|
|
e.svcCtx.Earn.SetIdentifiers(cast.ToString(ui.Id), &ea.Identifiers{
|
|
Email: ea.IdentifierFrom(ui.Email),
|
|
TwitterId: ea.IdentifierFrom(twitterId),
|
|
})
|
|
err = e.svcCtx.PromoteBindModel.UpdatePushUser(e.ctx, u.Id)
|
|
if err != nil {
|
|
logx.Errorw("update push user failed", logx.Field("err", err), logx.Field("uid", u.InvitedUid))
|
|
} else {
|
|
logx.Infow("push user info success", logx.Field("uid", u.InvitedUid))
|
|
}
|
|
}
|
|
}
|
|
|
|
func (e *Earn) pushUserBind(shareId uint) {
|
|
us, err := e.svcCtx.TouristBindModel.FindRequirePushUser(e.ctx, shareId)
|
|
if err != nil {
|
|
logx.Errorw("find require push bind role user failed", logx.Field("err", err), logx.Field("share_id", shareId))
|
|
return
|
|
}
|
|
for _, u := range us {
|
|
e.svcCtx.Earn.Track(cast.ToString(u.Uid), "BIND_ROLE", nil, nil)
|
|
err = e.svcCtx.PromoteBindModel.UpdatePushRole(e.ctx, u.Id)
|
|
if err != nil {
|
|
logx.Errorw("update push user failed", logx.Field("err", err), logx.Field("uid", u.Uid))
|
|
} else {
|
|
logx.Infow("push user info success", logx.Field("uid", u.Uid))
|
|
}
|
|
}
|
|
}
|