查询当前登录用户排名,
This commit is contained in:
129
internal/logic/task/task.go
Normal file
129
internal/logic/task/task.go
Normal file
@ -0,0 +1,129 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
type sTask struct{}
|
||||
|
||||
func New() service.ITask {
|
||||
return &sTask{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterTask(New())
|
||||
}
|
||||
|
||||
func (s *sTask) UserTaskRankingList(ctx context.Context, in *model.UserTaskRankingIn) (out *model.UserTaskRankingOut, err error) {
|
||||
|
||||
// 判断排行类型
|
||||
var start, end time.Time
|
||||
if in.Type == 1 {
|
||||
// 日
|
||||
now := time.Now()
|
||||
// 当天开始时间
|
||||
start = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
// 当天结束时间
|
||||
end = time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, now.Location())
|
||||
|
||||
fmt.Println("开始时间:", start)
|
||||
fmt.Println("结束时间:", end)
|
||||
|
||||
} else if in.Type == 2 {
|
||||
// 周
|
||||
now := time.Now()
|
||||
loc := now.Location()
|
||||
weekday := int(now.Weekday())
|
||||
if weekday == 0 { // 周日是0
|
||||
weekday = 7
|
||||
}
|
||||
// 本周开始时间(周一 00:00:00)
|
||||
start = time.Date(now.Year(), now.Month(), now.Day()-weekday+1, 0, 0, 0, 0, loc)
|
||||
// 本周结束时间(周日 23:59:59)
|
||||
end = time.Date(now.Year(), now.Month(), now.Day()-weekday+7, 23, 59, 59, 0, loc)
|
||||
} else if in.Type == 3 {
|
||||
// 月
|
||||
now := time.Now()
|
||||
loc := now.Location()
|
||||
// 本月开始时间(1号 00:00:00)
|
||||
start = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, loc)
|
||||
// 下月1号
|
||||
nextMonth := start.AddDate(0, 1, 0)
|
||||
// 本月结束时间(本月最后一天 23:59:59)
|
||||
end = nextMonth.Add(-time.Second)
|
||||
} else {
|
||||
return nil, ecode.Params.Sub("排行方式错误")
|
||||
}
|
||||
m := dao.UserTasks.Ctx(ctx)
|
||||
|
||||
// 构建查询条件
|
||||
if in.Page == 0 {
|
||||
in.Page = 1
|
||||
}
|
||||
|
||||
if in.Size == 0 {
|
||||
in.Size = 10
|
||||
}
|
||||
|
||||
if in.StoreId > 0 {
|
||||
m = m.Where(dao.UserTasks.Columns().StoreId, in.StoreId)
|
||||
}
|
||||
|
||||
list := make([]model.UserTaskRankingArgs, 0)
|
||||
var total int
|
||||
// SELECT `uid`,`nickname` FROM `user` ORDER BY `uid` asc
|
||||
err = m.Page(in.Page, in.Size).LeftJoin(dao.Users.Table(), fmt.Sprintf("`%s`.`id` = `%s`.`user_id`", dao.Users.Table(), dao.UserTasks.Table())).
|
||||
Fields("username,avatar,count(*) num").Where(dao.UserTasks.Columns().Status, 3).
|
||||
WhereBetween(dao.UserTasks.Columns().CompletedAt, start, end).OrderDesc(dao.UserTasks.Columns().CompletedAt).Group("user_id").
|
||||
ScanAndCount(&list, &total, false)
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询排行榜失败")
|
||||
}
|
||||
|
||||
// 判断登录查询当前任务完成数量,排名
|
||||
var completedDay, rankingDay int
|
||||
var loginUserRanking []model.LoginUserRanking
|
||||
if in.OperatorId != 0 {
|
||||
err := dao.UserTasks.Ctx(ctx).Fields("count(*) num").Where(dao.UserTasks.Columns().UserId, in.OperatorId).Where(dao.UserTasks.Columns().Status, 3).
|
||||
WhereBetween(dao.UserTasks.Columns().CompletedAt, start, end).OrderDesc(dao.UserTasks.Columns().CompletedAt).Group("user_id").
|
||||
Scan(&completedDay)
|
||||
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询当前登录用户完成数失败")
|
||||
}
|
||||
|
||||
err = dao.UserTasks.Ctx(ctx).Fields("user_id,count(*) num").Where(dao.UserTasks.Columns().Status, 3).
|
||||
WhereBetween(dao.UserTasks.Columns().CompletedAt, start, end).OrderDesc(dao.UserTasks.Columns().CompletedAt).Group("user_id").
|
||||
Scan(&loginUserRanking)
|
||||
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询当前登录用户排名失败")
|
||||
}
|
||||
|
||||
// 查找当前登录用户排名
|
||||
index := sort.Search(len(loginUserRanking), func(i int) bool {
|
||||
return loginUserRanking[i].Num >= completedDay
|
||||
})
|
||||
//for index, ranking := range loginUserRanking {
|
||||
// if ranking.UserId == in.OperatorId {
|
||||
// rankingDay = index + 1
|
||||
// break
|
||||
// }
|
||||
//}
|
||||
rankingDay = index + 1
|
||||
}
|
||||
|
||||
return &model.UserTaskRankingOut{
|
||||
List: list,
|
||||
Total: total,
|
||||
CompletedDay: completedDay,
|
||||
RankingDay: rankingDay,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user