Files
novatask/internal/logic/task/get_task_list_logic.go
2024-12-19 21:14:14 +08:00

97 lines
2.5 KiB
Go

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))
}
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 t.Type == model.TASKTYPE_INVITE_USER {
totalCount = cast.ToInt(t.Param)
hasFinishCount = cast.ToInt(count)
}
if finishState >= model.TASK_PROGRESS_WAIT_REWARD {
hasFinishCount = totalCount
}
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
}