yunzer_go/server/controllers/dashboard.go

212 lines
6.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controllers
import (
"server/models"
"server/services"
"time"
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
)
// DashboardController 仪表盘控制器
type DashboardController struct {
beego.Controller
}
// GetPlatformStats 获取平台统计数据(平台用户使用)
// @router /api/dashboard/platform-stats [get]
func (c *DashboardController) GetPlatformStats() {
// 获取租户总数
tenants, err := models.GetTenantList()
tenantCount := int64(0)
if err == nil {
tenantCount = int64(len(tenants))
}
// 获取知识库总数及增长率(所有租户)
knowledgeCount, currentMonthKnowledge, lastMonthKnowledge, knowledgeGrowthRate, _ := models.GetKnowledgeCountWithGrowth(0)
// 获取用户总数(所有租户的用户,使用简单查询)
o := orm.NewOrm()
var userCount int64
o.Raw("SELECT COUNT(*) FROM yz_users WHERE delete_time IS NULL").QueryRow(&userCount)
// 获取员工总数(所有租户的员工)
var employeeCount int64
o.Raw("SELECT COUNT(*) FROM yz_tenant_employees WHERE delete_time IS NULL").QueryRow(&employeeCount)
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "success",
"data": map[string]interface{}{
"tenantCount": tenantCount,
"userCount": userCount,
"knowledgeCount": map[string]interface{}{
"total": knowledgeCount,
"currentMonth": currentMonthKnowledge,
"lastMonth": lastMonthKnowledge,
"growthRate": knowledgeGrowthRate,
},
"employeeCount": employeeCount,
},
}
c.ServeJSON()
}
// GetTenantStats 获取租户统计数据(租户员工使用)
// @router /api/dashboard/tenant-stats [get]
func (c *DashboardController) GetTenantStats() {
// 获取租户ID从JWT token中获取
tenantId := 0
if tenantIdVal, ok := c.Ctx.Input.GetData("tenantId").(int); ok && tenantIdVal > 0 {
if userType, ok := c.Ctx.Input.GetData("userType").(string); ok && userType == "employee" {
tenantId = tenantIdVal
}
}
if tenantId <= 0 {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "无法获取租户信息",
"data": nil,
}
c.ServeJSON()
return
}
// 获取知识库数量及增长率
knowledgeCount, currentMonthKnowledge, lastMonthKnowledge, knowledgeGrowthRate, _ := models.GetKnowledgeCountWithGrowth(tenantId)
// 获取员工数量(简单查询)
o := orm.NewOrm()
var employeeCount int64
o.Raw("SELECT COUNT(*) FROM yz_tenant_employees WHERE tenant_id = ? AND delete_time IS NULL", tenantId).QueryRow(&employeeCount)
// 获取部门数量
var departmentCount int64
o.Raw("SELECT COUNT(*) FROM yz_tenant_departments WHERE tenant_id = ? AND delete_time IS NULL", tenantId).QueryRow(&departmentCount)
// 获取职位数量
var positionCount int64
o.Raw("SELECT COUNT(*) FROM yz_tenant_positions WHERE tenant_id = ? AND delete_time IS NULL", tenantId).QueryRow(&positionCount)
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "success",
"data": map[string]interface{}{
"knowledgeCount": map[string]interface{}{
"total": knowledgeCount,
"currentMonth": currentMonthKnowledge,
"lastMonth": lastMonthKnowledge,
"growthRate": knowledgeGrowthRate,
},
"employeeCount": employeeCount,
"departmentCount": departmentCount,
"positionCount": positionCount,
},
}
c.ServeJSON()
}
// GetUserActivityLogs 获取当前用户的最新活动日志(包含操作日志和登录日志)
// @router /api/dashboard/user-activity-logs [get]
func (c *DashboardController) GetUserActivityLogs() {
// 获取当前用户ID和租户ID
userId := 0
if userIdVal, ok := c.Ctx.Input.GetData("userId").(int); ok {
userId = userIdVal
}
tenantId := 0
if tenantIdVal, ok := c.Ctx.Input.GetData("tenantId").(int); ok {
tenantId = tenantIdVal
}
if userId <= 0 {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "无法获取用户信息",
"data": nil,
}
c.ServeJSON()
return
}
// 获取查询参数
pageNum, _ := c.GetInt("page_num", 1)
pageSize, _ := c.GetInt("page_size", 10)
logType := c.GetString("type") // "operation" 或 "access" 或 "all"
operation := c.GetString("operation") // 操作类型过滤
if logType == "" {
logType = "all"
}
type ActivityLog struct {
Id int64 `json:"id"`
Type string `json:"type"`
Username string `json:"username"`
Module string `json:"module"`
Operation string `json:"operation"`
Action string `json:"action"`
Description string `json:"description"`
Timestamp time.Time `json:"timestamp"`
}
var logs []ActivityLog
// 获取操作日志
if logType == "operation" || logType == "all" {
operationLogs, _, err := services.GetOperationLogs(tenantId, userId, "", operation, nil, nil, pageNum, pageSize)
if err == nil && operationLogs != nil {
for _, log := range operationLogs {
logs = append(logs, ActivityLog{
Id: log.Id,
Type: "operation",
Username: log.Username,
Module: log.Module,
Operation: log.Operation,
Action: log.Operation,
Description: log.Description,
Timestamp: log.CreateTime,
})
}
}
}
// 获取访问/登录日志
if logType == "access" || logType == "all" {
accessLogs, _, err := services.GetAccessLogs(tenantId, userId, "", "", nil, nil, pageNum, pageSize)
if err == nil && accessLogs != nil {
for _, log := range accessLogs {
logs = append(logs, ActivityLog{
Id: log.Id,
Type: "access",
Username: log.Username,
Module: log.Module,
Operation: log.RequestMethod,
Action: "访问",
Description: "访问 " + log.Module,
Timestamp: log.CreateTime,
})
}
}
}
// 按时间戳排序(最新的在前)
if len(logs) > pageSize {
logs = logs[:pageSize]
}
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "success",
"data": map[string]interface{}{
"logs": logs,
"total": len(logs),
},
}
c.ServeJSON()
}