From 821a302d9076abc96d8a44f4f3a6ef9724ad132a Mon Sep 17 00:00:00 2001 From: zouap Date: Wed, 28 Sep 2022 11:05:08 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/user_invitation.go | 12 +++ routers/api/v1/api.go | 2 + routers/repo/user_invitation.go | 152 ++++++++++++++++++++++++++++++-- 3 files changed, 158 insertions(+), 8 deletions(-) diff --git a/models/user_invitation.go b/models/user_invitation.go index 7b0ca1cf9..2d37bcb23 100644 --- a/models/user_invitation.go +++ b/models/user_invitation.go @@ -64,6 +64,18 @@ func QueryInvitaionPage(start int, pageSize int) ([]*Invitation, int64) { return invitationList, allCount } +func QueryInvitaionByTime(startTime int64, endTime int64) []*Invitation { + statictisSess := xStatistic.NewSession() + defer statictisSess.Close() + cond := "created_unix >=" + fmt.Sprint(startTime) + " and created_unix <=" + fmt.Sprint(endTime) + invitationList := make([]*Invitation, 0) + if err := statictisSess.Table(new(Invitation)).Where(cond).OrderBy("created_unix desc"). + Find(&invitationList); err != nil { + return nil + } + return invitationList +} + func InsertInvitaion(invitationUser *Invitation) error { statictisSess := xStatistic.NewSession() defer statictisSess.Close() diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index f430460f6..6dd1cb32f 100755 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -581,6 +581,8 @@ func RegisterRoutes(m *macaron.Macaron) { 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) + m.Get("/query_invitation_userdefine", operationReq, repo_ext.QueryUserDefineInvitationPage) + m.Get("/download_invitation_detail", operationReq, repo_ext.DownloadInvitationDetail) //cloudbrain board diff --git a/routers/repo/user_invitation.go b/routers/repo/user_invitation.go index b0aac4eed..e926f6936 100644 --- a/routers/repo/user_invitation.go +++ b/routers/repo/user_invitation.go @@ -4,6 +4,8 @@ import ( "fmt" "net/http" "net/url" + "sort" + "time" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" @@ -271,14 +273,148 @@ func QueryInvitationAll(ctx *context.Context) { queryDataFromStaticTable(ctx, "public.user_business_analysis_all", new(models.UserBusinessAnalysisAll)) } -// 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 QueryUserDefineInvitationPage(ctx *context.Context) { + startDate := ctx.Query("startDate") + endDate := ctx.Query("endDate") + startTime, _ := time.ParseInLocation("2006-01-02", startDate, time.Local) + //startTime = startTime.UTC() + endTime, _ := time.ParseInLocation("2006-01-02", endDate, time.Local) + + queryData(ctx, startTime, endTime) +} + +func queryData(ctx *context.Context, startTime time.Time, endTime time.Time) { + page, pageSize := getPageInfo(ctx) + IsReturnFile := ctx.QueryBool("IsReturnFile") + + dbResult := models.QueryInvitaionByTime(startTime.Unix(), endTime.Unix()) + + invitaionNumMap := make(map[int64]int, 0) + allUserIds := make([]int64, 0) + for _, record := range dbResult { + if _, ok := invitaionNumMap[record.SrcUserID]; !ok { + invitaionNumMap[record.SrcUserID] = 1 + } else { + invitaionNumMap[record.SrcUserID] = invitaionNumMap[record.SrcUserID] + 1 + } + } + invitaionNumList := make([]models.Invitation, 0) + for key, value := range invitaionNumMap { + invi := models.Invitation{ + SrcUserID: key, + InvitationUserNum: value, + } + invitaionNumList = append(invitaionNumList, invi) + allUserIds = append(allUserIds, key) + } + sort.Slice(invitaionNumList, func(i, j int) bool { + return invitaionNumList[i].InvitationUserNum > invitaionNumList[j].InvitationUserNum + }) + if IsReturnFile { + xlsx := excelize.NewFile() + sheetName := ctx.Tr("user.static.invitationsheetname") + index := xlsx.NewSheet(sheetName) + xlsx.DeleteSheet("Sheet1") + excelHeader := getInvitationExcelHeader(ctx) + for k, v := range excelHeader { + //设置单元格的值 + xlsx.SetCellValue(sheetName, k, v) + } + end := 100 + userMap := make(map[int64]*models.User, 0) + for i := 0; i < len(allUserIds); i += 100 { + if end >= len(allUserIds) { + end = len(allUserIds) - 1 + } + if i == end { + break + } + userList, err := models.GetUsersByIDs(allUserIds[i:end]) + if err == nil { + for _, tmp := range userList { + userMap[tmp.ID] = tmp + } + } + end = end + 100 + } + row := 1 + for _, userRecord := range invitaionNumList { + row++ + rows := fmt.Sprint(row) + var tmp byte + tmp = 0 + + xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.SrcUserID) + tmp = tmp + 1 + name := "" + if userMap[userRecord.SrcUserID] != nil { + name = userMap[userRecord.SrcUserID].Name + } + xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, name) + tmp = tmp + 1 + + xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.InvitationUserNum) + tmp = tmp + 1 + Phone := "" + if userMap[userRecord.SrcUserID] != nil { + Phone = userMap[userRecord.SrcUserID].PhoneNumber + } + xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, Phone) + tmp = tmp + 1 + + formatTime := "" + if userMap[userRecord.SrcUserID] != nil { + formatTime = userMap[userRecord.SrcUserID].CreatedUnix.Format("2006-01-02 15:04:05") + } + xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, formatTime[0:len(formatTime)-3]) + } + //设置默认打开的表单 + xlsx.SetActiveSheet(index) + filename := sheetName + "_" + getTimeFileName(startTime) + "_" + getTimeFileName(endTime) + ".xlsx" + //filename := sheetName + "_" + ctx.Tr("user.static."+tableName) + ".xlsx" + ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(filename)) + ctx.Resp.Header().Set("Content-Type", "application/octet-stream") + if _, err := xlsx.WriteTo(ctx.Resp); err != nil { + log.Info("writer exel error." + err.Error()) + } + } else { + result := make([]models.Invitation, 0) + userIds := make([]int64, 0) + end := len(invitaionNumList) - 1 + for start := (page - 1) * pageSize; start <= end; start++ { + invi := invitaionNumList[start] + //todo name phone,createunix + result = append(result, invi) + userIds = append(userIds, invi.SrcUserID) + if len(result) == pageSize { + break + } + } + userList, err := models.GetUsersByIDs(userIds) + if err == nil { + for _, invi := range result { + tmpUser := userList[0] + for _, tmp := range userList { + if tmp.ID == invi.SrcUserID { + tmpUser = tmp + break + } + } + if invi.SrcUserID == tmpUser.ID { + invi.Name = tmpUser.Name + invi.Phone = tmpUser.PhoneNumber + invi.CreatedUnix = tmpUser.CreatedUnix + } + } + } else { + log.Info("query user error." + err.Error()) + } + mapInterface := make(map[string]interface{}) + mapInterface["data"] = result + mapInterface["count"] = len(invitaionNumList) + ctx.JSON(http.StatusOK, mapInterface) + } +} func getPageInfo(ctx *context.Context) (int, int) { page := ctx.QueryInt("page") From fcf6a5e13137423715920b209a5d43d7ac196292 Mon Sep 17 00:00:00 2001 From: zouap Date: Wed, 28 Sep 2022 11:33:26 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/repo/user_invitation.go | 1 + 1 file changed, 1 insertion(+) diff --git a/routers/repo/user_invitation.go b/routers/repo/user_invitation.go index e926f6936..686d490d2 100644 --- a/routers/repo/user_invitation.go +++ b/routers/repo/user_invitation.go @@ -401,6 +401,7 @@ func queryData(ctx *context.Context, startTime time.Time, endTime time.Time) { } } if invi.SrcUserID == tmpUser.ID { + log.Info("find name........") invi.Name = tmpUser.Name invi.Phone = tmpUser.PhoneNumber invi.CreatedUnix = tmpUser.CreatedUnix From 11ddfe89b2090dec62ce1a0f033aea959110bca3 Mon Sep 17 00:00:00 2001 From: zouap Date: Wed, 28 Sep 2022 11:37:43 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/repo/user_invitation.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/routers/repo/user_invitation.go b/routers/repo/user_invitation.go index 686d490d2..75d416346 100644 --- a/routers/repo/user_invitation.go +++ b/routers/repo/user_invitation.go @@ -378,13 +378,13 @@ func queryData(ctx *context.Context, startTime time.Time, endTime time.Time) { log.Info("writer exel error." + err.Error()) } } else { - result := make([]models.Invitation, 0) + result := make([]*models.Invitation, 0) userIds := make([]int64, 0) end := len(invitaionNumList) - 1 for start := (page - 1) * pageSize; start <= end; start++ { invi := invitaionNumList[start] //todo name phone,createunix - result = append(result, invi) + result = append(result, &invi) userIds = append(userIds, invi.SrcUserID) if len(result) == pageSize { break @@ -401,7 +401,6 @@ func queryData(ctx *context.Context, startTime time.Time, endTime time.Time) { } } if invi.SrcUserID == tmpUser.ID { - log.Info("find name........") invi.Name = tmpUser.Name invi.Phone = tmpUser.PhoneNumber invi.CreatedUnix = tmpUser.CreatedUnix From 741456b0e814706b55c5ab550b9de6bf7db391d8 Mon Sep 17 00:00:00 2001 From: zouap Date: Wed, 28 Sep 2022 11:43:28 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/repo/user_invitation.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/routers/repo/user_invitation.go b/routers/repo/user_invitation.go index 75d416346..e63907afe 100644 --- a/routers/repo/user_invitation.go +++ b/routers/repo/user_invitation.go @@ -346,7 +346,7 @@ func queryData(ctx *context.Context, startTime time.Time, endTime time.Time) { xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, userRecord.SrcUserID) tmp = tmp + 1 - name := "" + name := "已注销" if userMap[userRecord.SrcUserID] != nil { name = userMap[userRecord.SrcUserID].Name } @@ -365,8 +365,9 @@ func queryData(ctx *context.Context, startTime time.Time, endTime time.Time) { formatTime := "" if userMap[userRecord.SrcUserID] != nil { formatTime = userMap[userRecord.SrcUserID].CreatedUnix.Format("2006-01-02 15:04:05") + formatTime = formatTime[0 : len(formatTime)-3] } - xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, formatTime[0:len(formatTime)-3]) + xlsx.SetCellValue(sheetName, getColumn(tmp)+rows, formatTime) } //设置默认打开的表单 xlsx.SetActiveSheet(index) @@ -404,6 +405,8 @@ func queryData(ctx *context.Context, startTime time.Time, endTime time.Time) { invi.Name = tmpUser.Name invi.Phone = tmpUser.PhoneNumber invi.CreatedUnix = tmpUser.CreatedUnix + } else { + invi.Name = "已注销" } } } else { From 3db04cdaa658fa3c5ad260db1ed266d4609ebd1b Mon Sep 17 00:00:00 2001 From: zouap Date: Wed, 28 Sep 2022 11:47:10 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/repo/user_invitation.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/routers/repo/user_invitation.go b/routers/repo/user_invitation.go index e63907afe..52d985235 100644 --- a/routers/repo/user_invitation.go +++ b/routers/repo/user_invitation.go @@ -333,11 +333,13 @@ func queryData(ctx *context.Context, startTime time.Time, endTime time.Time) { if err == nil { for _, tmp := range userList { userMap[tmp.ID] = tmp + log.Info("userMap key=" + fmt.Sprint(tmp.ID)) } } end = end + 100 } row := 1 + log.Info("len(userMap)=" + fmt.Sprint(len(userMap))) for _, userRecord := range invitaionNumList { row++ rows := fmt.Sprint(row) From 5dcef74c9ad92dbb4c7c1b6c6157448bd3d63d5d Mon Sep 17 00:00:00 2001 From: zouap Date: Wed, 28 Sep 2022 11:50:18 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/repo/user_invitation.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/routers/repo/user_invitation.go b/routers/repo/user_invitation.go index 52d985235..c5a5195a8 100644 --- a/routers/repo/user_invitation.go +++ b/routers/repo/user_invitation.go @@ -322,10 +322,12 @@ func queryData(ctx *context.Context, startTime time.Time, endTime time.Time) { } end := 100 userMap := make(map[int64]*models.User, 0) + log.Info("len(allUserIds)=" + fmt.Sprint(len(allUserIds))) for i := 0; i < len(allUserIds); i += 100 { if end >= len(allUserIds) { end = len(allUserIds) - 1 } + log.Info("i=" + fmt.Sprint(i) + " end=" + fmt.Sprint(end)) if i == end { break } From 49e24592f47898d8ee80486696b6097161a056ee Mon Sep 17 00:00:00 2001 From: zouap Date: Wed, 28 Sep 2022 11:54:02 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/repo/user_invitation.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/routers/repo/user_invitation.go b/routers/repo/user_invitation.go index c5a5195a8..79cb3b48d 100644 --- a/routers/repo/user_invitation.go +++ b/routers/repo/user_invitation.go @@ -325,7 +325,7 @@ func queryData(ctx *context.Context, startTime time.Time, endTime time.Time) { log.Info("len(allUserIds)=" + fmt.Sprint(len(allUserIds))) for i := 0; i < len(allUserIds); i += 100 { if end >= len(allUserIds) { - end = len(allUserIds) - 1 + end = len(allUserIds) } log.Info("i=" + fmt.Sprint(i) + " end=" + fmt.Sprint(end)) if i == end { @@ -337,6 +337,8 @@ func queryData(ctx *context.Context, startTime time.Time, endTime time.Time) { userMap[tmp.ID] = tmp log.Info("userMap key=" + fmt.Sprint(tmp.ID)) } + } else { + } end = end + 100 } From 1b37c8e08f918d961f3e16f25b4c7c2d779e6b7c Mon Sep 17 00:00:00 2001 From: zouap Date: Wed, 28 Sep 2022 14:08:13 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82=E8=A7=A3=E5=86=B3=E8=87=AA=E5=AE=9A=E4=B9=89=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=9F=A5=E8=AF=A2=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/repo/user_invitation.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/routers/repo/user_invitation.go b/routers/repo/user_invitation.go index 79cb3b48d..a2752a481 100644 --- a/routers/repo/user_invitation.go +++ b/routers/repo/user_invitation.go @@ -125,6 +125,9 @@ func DownloadInvitationDetail(ctx *context.Context) { for _, userRecord := range re { row++ userRecord.Name = userNameMap[userRecord.UserID] + if userRecord.Name == "" { + userRecord.Name = "已注销" + } writeInvitationDetailExcel(row, xlsx, sheetName, userRecord) } indexTotal += PAGE_SIZE @@ -335,7 +338,6 @@ func queryData(ctx *context.Context, startTime time.Time, endTime time.Time) { if err == nil { for _, tmp := range userList { userMap[tmp.ID] = tmp - log.Info("userMap key=" + fmt.Sprint(tmp.ID)) } } else {