package task import ( "context" "github.com/spf13/cast" "nova_task/internal/model" "nova_task/internal/pkg/errs" "time" "nova_task/internal/svc" "nova_task/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type GetTaskListLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } // NewGetTaskListLogic 获取任务列表 func NewGetTaskListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTaskListLogic { return &GetTaskListLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *GetTaskListLogic) GetTaskList(uid int, req *types.GetTaskListReq) (*types.GetTaskListResp, error) { //uid := cast.ToStringMapInt(l.ctx.Value("data"))["id"] tasks, err := l.svcCtx.TaskModel.FindTasksByCommunity(l.ctx, req.CommunityId) if err != nil { l.Errorw("get task list failed", logx.Field("err", err), logx.Field("communityId", req.CommunityId)) return nil, errs.New(errs.ErrDatabaseOperate, err) } count, err := l.svcCtx.PromoteBindModel.UserInviteCount(l.ctx, uint(uid)) if err != nil { l.Errorw("get user invite count failed", logx.Field("err", err), logx.Field("uid", uid)) } var isAmbassador, hasBindTwitter int resp := &types.GetTaskListResp{} for _, t := range tasks { // 任务序列号 taskSeq := 0 if t.Type == model.TASKTYPE_DAILY_PAY { taskSeq = cast.ToInt(time.Now().Format("20060102")) } // 读取任务完成状态 var finishState int8 if uid > 0 { tp, err := l.svcCtx.TaskProgressModel.FindOneByUidTaskIdTaskSeq(l.ctx, uid, t.Id, taskSeq) if err == nil { finishState = tp.Stage } } // 计算任务完成数量以及总数 hasFinishCount := 0 totalCount := 1 if finishState >= model.TASK_PROGRESS_WAIT_REWARD { // 如果任务状态为待领取奖励, 完成数量等于总数量 hasFinishCount = totalCount } else { // 分任务类型处理 switch t.Type { case model.TASKTYPE_INVITE_USER: totalCount = cast.ToInt(t.Param) hasFinishCount = cast.ToInt(count) case model.TASKTYPE_AMBASSADOR_TASK: if isAmbassador == 0 { if l.svcCtx.IsAmbassador(l.ctx, uint(uid), 0) { isAmbassador = 1 } else { isAmbassador = 2 } } if isAmbassador == 1 { hasFinishCount = 1 } else { hasFinishCount = 0 finishState = model.TASK_PROGRESS_NOT_ALLOWED } case model.TASKTYPE_BIND_TWITTER: if hasBindTwitter == 0 { if l.svcCtx.HasBindTwitter(l.ctx, uint(uid)) { hasBindTwitter = 1 } else { hasBindTwitter = 2 } } if hasBindTwitter == 1 { hasFinishCount = 1 } } } // 超过数量的显示任务要求的数量即可 if hasFinishCount > totalCount { hasFinishCount = totalCount } // 数量达标,且状态为未完成,则显示待校验状态 if hasFinishCount >= totalCount && finishState == model.TASK_PROGRESS_NOT_FINISHED { finishState = model.TASK_PROGRESS_WAIT_VERIFY } resp.Tasks = append(resp.Tasks, types.Task{ Id: t.Id, Title: t.Title, SubTitle: t.SubTitle, Description: t.Description, Points: t.Points, ButtonText: t.ButtonText, Params: t.Param, Type: t.Type, Url: t.Url, StartAt: t.StartAt.Time.Format(time.DateTime), EndAt: t.EndAt.Time.Format(time.DateTime), Sort: t.Sort, HasFinishCount: hasFinishCount, TotalCount: totalCount, FinishState: finishState, }) } return resp, nil }