50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-kratos/kratos/v3/errors"
|
|
"kra/internal/service/business"
|
|
"kra/internal/service/dto"
|
|
)
|
|
|
|
type Analytics struct{ service *business.AnalyticsService }
|
|
|
|
func NewAnalytics(service *business.AnalyticsService) *Analytics { return &Analytics{service: service} }
|
|
func (h *Analytics) Channels(c *gin.Context) {
|
|
var req dto.AnalyticsQuery
|
|
if c.ShouldBindQuery(&req) != nil {
|
|
Fail(c, "统计查询参数不正确")
|
|
return
|
|
}
|
|
if req.Export {
|
|
data, err := h.service.Export(c.Request.Context(), &req)
|
|
if err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
c.Header("Content-Disposition", "attachment; filename=channel-statistics.xlsx")
|
|
c.Header("success", "true")
|
|
c.Data(200, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", data)
|
|
return
|
|
}
|
|
data, err := h.service.Channels(c.Request.Context(), &req)
|
|
if err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OKWithData(c, data)
|
|
}
|
|
func (h *Analytics) Games(c *gin.Context) {
|
|
var req dto.AnalyticsQuery
|
|
if c.ShouldBindQuery(&req) != nil {
|
|
Fail(c, "统计查询参数不正确")
|
|
return
|
|
}
|
|
data, err := h.service.Games(c.Request.Context(), &req)
|
|
if err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OKWithData(c, data)
|
|
}
|