85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"mime"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-kratos/kratos/v3/errors"
|
|
"kra/internal/service/business"
|
|
"kra/internal/service/dto"
|
|
)
|
|
|
|
type Ledger struct{ service *business.LedgerService }
|
|
|
|
func NewLedger(service *business.LedgerService) *Ledger { return &Ledger{service: service} }
|
|
func (h *Ledger) Registrations(c *gin.Context) {
|
|
var req dto.LedgerQuery
|
|
if c.ShouldBindQuery(&req) != nil {
|
|
Fail(c, "明细查询参数不正确")
|
|
return
|
|
}
|
|
data, err := h.service.Registrations(c.Request.Context(), &req)
|
|
if err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OKWithData(c, data)
|
|
}
|
|
func (h *Ledger) Spends(c *gin.Context) {
|
|
var req dto.LedgerQuery
|
|
if c.ShouldBindQuery(&req) != nil {
|
|
Fail(c, "明细查询参数不正确")
|
|
return
|
|
}
|
|
data, err := h.service.Spends(c.Request.Context(), &req)
|
|
if err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OKWithData(c, data)
|
|
}
|
|
func (h *Ledger) Games(c *gin.Context) {
|
|
var req dto.GameOptionQuery
|
|
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)
|
|
}
|
|
func (h *Ledger) Export(spend bool) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 1<<20)
|
|
var req dto.LedgerQuery
|
|
if c.ShouldBind(&req) != nil {
|
|
c.Header("msg", url.PathEscape("明细查询参数不正确"))
|
|
Fail(c, "明细查询参数不正确")
|
|
return
|
|
}
|
|
var data []byte
|
|
var err error
|
|
name := "注册明细.xlsx"
|
|
if spend {
|
|
name = "充值明细.xlsx"
|
|
data, err = h.service.ExportSpends(c.Request.Context(), &req)
|
|
} else {
|
|
data, err = h.service.ExportRegistrations(c.Request.Context(), &req)
|
|
}
|
|
if err != nil {
|
|
message := errors.FromError(err).Message
|
|
c.Header("msg", url.PathEscape(message))
|
|
Fail(c, message)
|
|
return
|
|
}
|
|
c.Header("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": name}))
|
|
c.Header("success", "true")
|
|
c.Data(http.StatusOK, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", data)
|
|
}
|
|
}
|