109 lines
3.3 KiB
Go
109 lines
3.3 KiB
Go
package controllers
|
||
|
||
import (
|
||
"server/models"
|
||
|
||
"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()
|
||
}
|