41 lines
1.3 KiB
Go
41 lines
1.3 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/auth"
|
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/model"
|
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/response"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Handler struct{ db *gorm.DB }
|
|
|
|
func NewHandler(db *gorm.DB) *Handler { return &Handler{db: db} }
|
|
|
|
func (h *Handler) Summary(c *gin.Context) {
|
|
p, _ := auth.PrincipalFromContext(c)
|
|
counts := map[string]int64{}
|
|
queries := []struct {
|
|
key string
|
|
model interface{}
|
|
where string
|
|
args []interface{}
|
|
}{
|
|
{key: "scenarios", model: &model.Scenario{}, where: "tenant_id = ? AND status <> ?", args: []interface{}{p.TenantID, "archived"}},
|
|
{key: "published_sops", model: &model.SOP{}, where: "tenant_id = ? AND status = ?", args: []interface{}{p.TenantID, "published"}},
|
|
{key: "runs", model: &model.SOPRun{}, where: "tenant_id = ?", args: []interface{}{p.TenantID}},
|
|
{key: "completed_runs", model: &model.SOPRun{}, where: "tenant_id = ? AND status = ?", args: []interface{}{p.TenantID, "completed"}},
|
|
}
|
|
for _, query := range queries {
|
|
var count int64
|
|
if err := h.db.Model(query.model).Where(query.where, query.args...).Count(&count).Error; err != nil {
|
|
response.Error(c, http.StatusInternalServerError, "QUERY_FAILED", "查询统计数据失败")
|
|
return
|
|
}
|
|
counts[query.key] = count
|
|
}
|
|
response.OK(c, counts)
|
|
}
|