From 65afb3bb73d4eb3d02418a64dc35486490ca7b79 Mon Sep 17 00:00:00 2001 From: zouap Date: Wed, 14 Sep 2022 16:59:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BF=90=E8=90=A5=E5=88=86?= =?UTF-8?q?=E6=9E=90=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/user_invitation.go | 14 +++-- routers/api/v1/api.go | 10 ++++ routers/repo/user_invitation.go | 98 +++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 routers/repo/user_invitation.go diff --git a/models/user_invitation.go b/models/user_invitation.go index 4831e27da..5d9105fa7 100644 --- a/models/user_invitation.go +++ b/models/user_invitation.go @@ -32,18 +32,22 @@ func QueryInvitaionByPhone(phone string) []*Invitation { } } -func QueryInvitaion(start int64, end int64) ([]*Invitation, int) { +func QueryInvitaionPage(startTime int64, endTime int64, start int, pageSize int) ([]*Invitation, int64) { statictisSess := xStatistic.NewSession() defer statictisSess.Close() - cond := "created_unix >=" + fmt.Sprint(start) + " and created_unix <=" + fmt.Sprint(end) + cond := "created_unix >=" + fmt.Sprint(startTime) + " and created_unix <=" + fmt.Sprint(endTime) + allCount, err := statictisSess.Where(cond).Count(new(Invitation)) + if err != nil { + log.Info("query error." + err.Error()) + return nil, 0 + } invitationList := make([]*Invitation, 0) - - if err := statictisSess.Table(new(Invitation)).Where(cond).OrderBy("created_unix desc"). + if err := statictisSess.Table(new(Invitation)).Where(cond).OrderBy("created_unix desc").Limit(pageSize, start). Find(&invitationList); err != nil { return nil, 0 } - return invitationList, len(invitationList) + return invitationList, allCount } func InsertInvitaion(invitationUser *Invitation) error { diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 3e588d942..36ba44ce5 100755 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -572,6 +572,16 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/query_user_all", operationReq, repo_ext.QueryUserStaticAll) m.Get("/query_user_activity", operationReq, repo_ext.QueryUserActivity) m.Get("/query_user_login", operationReq, repo_ext.QueryUserLoginInfo) + + m.Get("/query_invitation_current_month", operationReq, repo_ext.QueryInvitationCurrentMonth) + m.Get("/query_invitation_current_week", operationReq, repo_ext.QueryInvitationCurrentWeek) + m.Get("/query_invitation_last_week", operationReq, repo_ext.QueryInvitationLastWeek) + m.Get("/query_invitation_current_year", operationReq, repo_ext.QueryInvitationCurrentYear) + m.Get("/query_invitation_last30_day", operationReq, repo_ext.QueryInvitationLast30Day) + m.Get("/query_invitation_last_month", operationReq, repo_ext.QueryInvitationLastMonth) + m.Get("/query_invitation_yesterday", operationReq, repo_ext.QueryInvitationYesterday) + m.Get("/query_invitation_all", operationReq, repo_ext.QueryInvitationAll) + //cloudbrain board m.Group("/cloudbrainboard", func() { m.Get("/downloadAll", repo.DownloadCloudBrainBoard) diff --git a/routers/repo/user_invitation.go b/routers/repo/user_invitation.go new file mode 100644 index 000000000..462fe6b4a --- /dev/null +++ b/routers/repo/user_invitation.go @@ -0,0 +1,98 @@ +package repo + +import ( + "net/http" + "time" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/setting" +) + +func QueryInvitationCurrentMonth(ctx *context.Context) { + + currentTimeNow := time.Now() + pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location()) + pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 0, 0, 0, 0, currentTimeNow.Location()) + + queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix()) +} + +func QueryInvitationCurrentWeek(ctx *context.Context) { + currentTimeNow := time.Now() + offset := int(time.Monday - currentTimeNow.Weekday()) + if offset > 0 { + offset = -6 + } + pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset) + pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location()) + queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix()) +} + +func QueryInvitationLastWeek(ctx *context.Context) { + currentTimeNow := time.Now() + offset := int(time.Monday - currentTimeNow.Weekday()) + if offset > 0 { + offset = -6 + } + pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset) + pageStartTime := pageEndTime.AddDate(0, 0, -7) + queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix()) +} + +func QueryInvitationCurrentYear(ctx *context.Context) { + currentTimeNow := time.Now() + pageStartTime := time.Date(currentTimeNow.Year(), 1, 1, 0, 0, 0, 0, currentTimeNow.Location()) + pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location()) + queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix()) +} + +func QueryInvitationLast30Day(ctx *context.Context) { + currentTimeNow := time.Now() + pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, -30) + pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location()) + queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix()) +} + +func QueryInvitationLastMonth(ctx *context.Context) { + currentTimeNow := time.Now() + thisMonth := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 0, 0, 0, 0, currentTimeNow.Location()) + pageStartTime := thisMonth.AddDate(0, -1, 0) + pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), 1, 23, 59, 59, 0, currentTimeNow.Location()).AddDate(0, 0, -1) + queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix()) +} + +func QueryInvitationYesterday(ctx *context.Context) { + currentTimeNow := time.Now().AddDate(0, 0, -1) + pageStartTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, time.Local) + pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 23, 59, 59, 0, currentTimeNow.Location()) + queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix()) +} + +func QueryInvitationAll(ctx *context.Context) { + currentTimeNow := time.Now() + pageStartTime := time.Date(2022, 8, 5, 0, 0, 0, 0, currentTimeNow.Location()) + pageEndTime := time.Date(currentTimeNow.Year(), currentTimeNow.Month(), currentTimeNow.Day(), 0, 0, 0, 0, currentTimeNow.Location()) + queryData(ctx, pageStartTime.Unix(), pageEndTime.Unix()) +} + +func queryData(ctx *context.Context, startTime int64, endTime int64) { + page, pageSize := getPageInfo(ctx) + result, count := models.QueryInvitaionPage(startTime, endTime, (page-1)*pageSize, pageSize) + mapInterface := make(map[string]interface{}) + mapInterface["data"] = result + mapInterface["count"] = count + ctx.JSON(http.StatusOK, mapInterface) +} + +func getPageInfo(ctx *context.Context) (int, int) { + page := ctx.QueryInt("page") + if page <= 0 { + page = 1 + } + pageSize := ctx.QueryInt("pageSize") + if pageSize <= 0 { + pageSize = setting.UI.IssuePagingNum + } + return page, pageSize +}