diff --git a/dao/user.go b/dao/user.go index 7b23165..01f8cf4 100644 --- a/dao/user.go +++ b/dao/user.go @@ -173,3 +173,18 @@ func FindUsersDefault() []User { DB.Limit(20).Find(&users) return users } + +// 用户的信息统计数据 +type UserStatistics struct { + SessionCount int64 `json:"session_count" form:"session_count"` //会话数量 + FileCount int64 `json:"file_count" form:"file_count"` //文件数量 + MessageCount int64 `json:"message_count" form:"message_count"` //消息数量,提问数量 +} + +func UserStatisticsData(userID int) UserStatistics { + userStatistics := UserStatistics{} + DB.Model(&Session{}).Group("user_id").Count(&userStatistics.SessionCount) + DB.Model(&File{}).Group("user_id").Count(&userStatistics.FileCount) + DB.Model(&Message{}).Where("from_id = ?", userID).Group("from_id").Count(&userStatistics.MessageCount) + return userStatistics +} diff --git a/handler/user.go b/handler/user.go index 0ce1c40..5749f4e 100644 --- a/handler/user.go +++ b/handler/user.go @@ -29,6 +29,7 @@ func SetUpUserGroup(router *gin.Engine) { userGroup.POST("/sync", GetSyncUserInfo) userGroup.POST("/delete", DeleteUser) userGroup.POST("/reset", ResetPassword) + userGroup.POST("/statistic", GetUserStatistic) } type RLReq struct { @@ -61,6 +62,14 @@ type ResetPasswordReq struct { Code string `json:"code" form:"code"` //验证码 } +func GetUserStatistic(c *gin.Context) { + id, _ := c.Get("id") + userId := int(id.(float64)) + //获取数据 + us := service.GetUserStatistics(userId) + c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": us}) +} + func ResetPassword(c *gin.Context) { var req_data ResetPasswordReq if err := c.ShouldBind(&req_data); err == nil { diff --git a/service/userService.go b/service/userService.go index 63deb57..49a29de 100644 --- a/service/userService.go +++ b/service/userService.go @@ -417,3 +417,7 @@ func CreateTokenAndSave(user dao.User) (string, error) { // 返回令牌 return tokenString, err } + +func GetUserStatistics(userID int) dao.UserStatistics { + return dao.UserStatisticsData(userID) +}