feat: complete pet SOP governance workflow
This commit is contained in:
@@ -3,6 +3,7 @@ package dashboard
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/access"
|
||||
"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"
|
||||
@@ -15,26 +16,46 @@ 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)
|
||||
principal, _ := 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"}},
|
||||
|
||||
scenarioQuery := access.ScopeScenarios(h.db.Model(&model.Scenario{}), principal, "scenarios").Where("scenarios.status <> ?", "archived")
|
||||
var scenarios int64
|
||||
if err := scenarioQuery.Count(&scenarios).Error; err != nil {
|
||||
queryFailed(c)
|
||||
return
|
||||
}
|
||||
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
|
||||
counts["scenarios"] = scenarios
|
||||
|
||||
sopQuery := h.db.Table("sops s").Joins("JOIN scenarios sc ON sc.id = s.scenario_id")
|
||||
sopQuery = access.ScopeScenarios(sopQuery, principal, "sc").Where("s.status = ?", "published")
|
||||
var publishedSOPs int64
|
||||
if err := sopQuery.Count(&publishedSOPs).Error; err != nil {
|
||||
queryFailed(c)
|
||||
return
|
||||
}
|
||||
counts["published_sops"] = publishedSOPs
|
||||
|
||||
runQuery := h.db.Model(&model.SOPRun{}).Where("tenant_id = ?", principal.TenantID)
|
||||
if !auth.HasPermission(principal, "runs.view_all") && !auth.HasPermission(principal, "*") {
|
||||
runQuery = runQuery.Where("operator_id = ?", principal.UserID)
|
||||
}
|
||||
var runs int64
|
||||
if err := runQuery.Count(&runs).Error; err != nil {
|
||||
queryFailed(c)
|
||||
return
|
||||
}
|
||||
counts["runs"] = runs
|
||||
var completedRuns int64
|
||||
if err := runQuery.Where("status = ?", "completed").Count(&completedRuns).Error; err != nil {
|
||||
queryFailed(c)
|
||||
return
|
||||
}
|
||||
counts["completed_runs"] = completedRuns
|
||||
|
||||
response.OK(c, counts)
|
||||
}
|
||||
|
||||
func queryFailed(c *gin.Context) {
|
||||
response.Error(c, http.StatusInternalServerError, "QUERY_FAILED", "查询统计数据失败")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user