166 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			166 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
|  | package routes | ||
|  | 
 | ||
|  | import ( | ||
|  | 	"tyapi-server/internal/infrastructure/http/handlers" | ||
|  | 	sharedhttp "tyapi-server/internal/shared/http" | ||
|  | 	"tyapi-server/internal/shared/middleware" | ||
|  | 
 | ||
|  | 	"go.uber.org/zap" | ||
|  | ) | ||
|  | 
 | ||
|  | // StatisticsRoutes 统计路由 | ||
|  | type StatisticsRoutes struct { | ||
|  | 	statisticsHandler *handlers.StatisticsHandler | ||
|  | 	auth              *middleware.JWTAuthMiddleware | ||
|  | 	optionalAuth      *middleware.OptionalAuthMiddleware | ||
|  | 	admin             *middleware.AdminAuthMiddleware | ||
|  | 	logger            *zap.Logger | ||
|  | } | ||
|  | 
 | ||
|  | // NewStatisticsRoutes 创建统计路由 | ||
|  | func NewStatisticsRoutes( | ||
|  | 	statisticsHandler *handlers.StatisticsHandler, | ||
|  | 	auth *middleware.JWTAuthMiddleware, | ||
|  | 	optionalAuth *middleware.OptionalAuthMiddleware, | ||
|  | 	admin *middleware.AdminAuthMiddleware, | ||
|  | 	logger *zap.Logger, | ||
|  | ) *StatisticsRoutes { | ||
|  | 	return &StatisticsRoutes{ | ||
|  | 		statisticsHandler: statisticsHandler, | ||
|  | 		auth:              auth, | ||
|  | 		optionalAuth:      optionalAuth, | ||
|  | 		admin:             admin, | ||
|  | 		logger:            logger, | ||
|  | 	} | ||
|  | } | ||
|  | 
 | ||
|  | // Register 注册统计相关路由 | ||
|  | func (r *StatisticsRoutes) Register(router *sharedhttp.GinRouter) { | ||
|  | 	engine := router.GetEngine() | ||
|  | 
 | ||
|  | 	// ================ 用户端统计路由 ================ | ||
|  | 
 | ||
|  | 	// 统计公开接口 | ||
|  | 	statistics := engine.Group("/api/v1/statistics") | ||
|  | 	{ | ||
|  | 		// 获取公开统计信息 | ||
|  | 		statistics.GET("/public", r.statisticsHandler.GetPublicStatistics) | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// 用户统计接口 - 需要认证 | ||
|  | 	userStats := engine.Group("/api/v1/statistics", r.auth.Handle()) | ||
|  | 	{ | ||
|  | 		// 获取用户统计信息 | ||
|  | 		userStats.GET("/user", r.statisticsHandler.GetUserStatistics) | ||
|  | 
 | ||
|  | 		// 独立统计接口(用户只能查询自己的数据) | ||
|  | 		userStats.GET("/api-calls", r.statisticsHandler.GetApiCallsStatistics) | ||
|  | 		userStats.GET("/consumption", r.statisticsHandler.GetConsumptionStatistics) | ||
|  | 		userStats.GET("/recharge", r.statisticsHandler.GetRechargeStatistics) | ||
|  | 		 | ||
|  | 		// 获取最新产品推荐 | ||
|  | 		userStats.GET("/latest-products", r.statisticsHandler.GetLatestProducts) | ||
|  | 
 | ||
|  | 		// 获取指标列表 | ||
|  | 		userStats.GET("/metrics", r.statisticsHandler.GetMetrics) | ||
|  | 
 | ||
|  | 		// 获取指标详情 | ||
|  | 		userStats.GET("/metrics/:id", r.statisticsHandler.GetMetricDetail) | ||
|  | 
 | ||
|  | 		// 获取仪表板列表 | ||
|  | 		userStats.GET("/dashboards", r.statisticsHandler.GetDashboards) | ||
|  | 
 | ||
|  | 		// 获取仪表板详情 | ||
|  | 		userStats.GET("/dashboards/:id", r.statisticsHandler.GetDashboardDetail) | ||
|  | 
 | ||
|  | 		// 获取仪表板数据 | ||
|  | 		userStats.GET("/dashboards/:id/data", r.statisticsHandler.GetDashboardData) | ||
|  | 
 | ||
|  | 		// 获取报告列表 | ||
|  | 		userStats.GET("/reports", r.statisticsHandler.GetReports) | ||
|  | 
 | ||
|  | 		// 获取报告详情 | ||
|  | 		userStats.GET("/reports/:id", r.statisticsHandler.GetReportDetail) | ||
|  | 
 | ||
|  | 		// 创建报告 | ||
|  | 		userStats.POST("/reports", r.statisticsHandler.CreateReport) | ||
|  | 	} | ||
|  | 
 | ||
|  | 	// ================ 管理员统计路由 ================ | ||
|  | 
 | ||
|  | 	// 管理员路由组 | ||
|  | 	adminGroup := engine.Group("/api/v1/admin") | ||
|  | 	adminGroup.Use(r.admin.Handle()) // 管理员权限验证 | ||
|  | 	{ | ||
|  | 		// 统计指标管理 | ||
|  | 		metrics := adminGroup.Group("/statistics/metrics") | ||
|  | 		{ | ||
|  | 			metrics.GET("", r.statisticsHandler.AdminGetMetrics) | ||
|  | 			metrics.POST("", r.statisticsHandler.AdminCreateMetric) | ||
|  | 			metrics.PUT("/:id", r.statisticsHandler.AdminUpdateMetric) | ||
|  | 			metrics.DELETE("/:id", r.statisticsHandler.AdminDeleteMetric) | ||
|  | 		} | ||
|  | 
 | ||
|  | 		// 仪表板管理 | ||
|  | 		dashboards := adminGroup.Group("/statistics/dashboards") | ||
|  | 		{ | ||
|  | 			dashboards.GET("", r.statisticsHandler.AdminGetDashboards) | ||
|  | 			dashboards.POST("", r.statisticsHandler.AdminCreateDashboard) | ||
|  | 			dashboards.PUT("/:id", r.statisticsHandler.AdminUpdateDashboard) | ||
|  | 			dashboards.DELETE("/:id", r.statisticsHandler.AdminDeleteDashboard) | ||
|  | 		} | ||
|  | 
 | ||
|  | 		// 报告管理 | ||
|  | 		reports := adminGroup.Group("/statistics/reports") | ||
|  | 		{ | ||
|  | 			reports.GET("", r.statisticsHandler.AdminGetReports) | ||
|  | 		} | ||
|  | 
 | ||
|  | 		// 系统统计 | ||
|  | 		system := adminGroup.Group("/statistics/system") | ||
|  | 		{ | ||
|  | 			system.GET("", r.statisticsHandler.AdminGetSystemStatistics) | ||
|  | 		} | ||
|  | 
 | ||
|  | 		// 独立域统计接口 | ||
|  | 		domainStats := adminGroup.Group("/statistics") | ||
|  | 		{ | ||
|  | 			domainStats.GET("/user-domain", r.statisticsHandler.AdminGetUserDomainStatistics) | ||
|  | 			domainStats.GET("/api-domain", r.statisticsHandler.AdminGetApiDomainStatistics) | ||
|  | 			domainStats.GET("/consumption-domain", r.statisticsHandler.AdminGetConsumptionDomainStatistics) | ||
|  | 			domainStats.GET("/recharge-domain", r.statisticsHandler.AdminGetRechargeDomainStatistics) | ||
|  | 		} | ||
|  | 
 | ||
|  | 		// 排行榜接口 | ||
|  | 		rankings := adminGroup.Group("/statistics") | ||
|  | 		{ | ||
|  | 			rankings.GET("/user-call-ranking", r.statisticsHandler.AdminGetUserCallRanking) | ||
|  | 			rankings.GET("/recharge-ranking", r.statisticsHandler.AdminGetRechargeRanking) | ||
|  | 			rankings.GET("/api-popularity-ranking", r.statisticsHandler.AdminGetApiPopularityRanking) | ||
|  | 			rankings.GET("/today-certified-enterprises", r.statisticsHandler.AdminGetTodayCertifiedEnterprises) | ||
|  | 		} | ||
|  | 
 | ||
|  | 		// 用户统计 | ||
|  | 		userStats := adminGroup.Group("/statistics/users") | ||
|  | 		{ | ||
|  | 			userStats.GET("/:user_id", r.statisticsHandler.AdminGetUserStatistics) | ||
|  | 		} | ||
|  | 
 | ||
|  | 		// 独立统计接口(管理员可查询任意用户) | ||
|  | 		independentStats := adminGroup.Group("/statistics") | ||
|  | 		{ | ||
|  | 			independentStats.GET("/api-calls", r.statisticsHandler.GetApiCallsStatistics) | ||
|  | 			independentStats.GET("/consumption", r.statisticsHandler.GetConsumptionStatistics) | ||
|  | 			independentStats.GET("/recharge", r.statisticsHandler.GetRechargeStatistics) | ||
|  | 		} | ||
|  | 
 | ||
|  | 		// 数据聚合 | ||
|  | 		aggregation := adminGroup.Group("/statistics/aggregation") | ||
|  | 		{ | ||
|  | 			aggregation.POST("/trigger", r.statisticsHandler.AdminTriggerAggregation) | ||
|  | 		} | ||
|  | 	} | ||
|  | 
 | ||
|  | 	r.logger.Info("统计路由注册完成") | ||
|  | } |