billing_export_handler.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package admin
  2. import (
  3. "errors"
  4. "strconv"
  5. "time"
  6. "github.com/Wei-Shaw/sub2api/internal/pkg/response"
  7. middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
  8. "github.com/Wei-Shaw/sub2api/internal/service"
  9. "github.com/gin-gonic/gin"
  10. )
  11. // BillingExportHandler 账单:用户月账单与用量 CSV 导出,管理员可查任意用户。
  12. type BillingExportHandler struct {
  13. service *service.BillingExportService
  14. }
  15. func NewBillingExportHandler(svc *service.BillingExportService) *BillingExportHandler {
  16. return &BillingExportHandler{service: svc}
  17. }
  18. func (h *BillingExportHandler) requireService(c *gin.Context) bool {
  19. if h == nil || h.service == nil {
  20. response.ErrorFrom(c, errors.New("billing export service unavailable"))
  21. return false
  22. }
  23. return true
  24. }
  25. // Statement 用户月账单:?year=&month=。
  26. // GET /billing/statement
  27. func (h *BillingExportHandler) Statement(c *gin.Context) {
  28. if !h.requireService(c) {
  29. return
  30. }
  31. subject, ok := middleware2.GetAuthSubjectFromContext(c)
  32. if !ok {
  33. response.Unauthorized(c, "Unauthorized")
  34. return
  35. }
  36. now := time.Now().UTC()
  37. year, month := now.Year(), int(now.Month())
  38. if raw := c.Query("year"); raw != "" {
  39. if v, err := strconv.Atoi(raw); err == nil {
  40. year = v
  41. }
  42. }
  43. if raw := c.Query("month"); raw != "" {
  44. if v, err := strconv.Atoi(raw); err == nil {
  45. month = v
  46. }
  47. }
  48. stmt, err := h.service.Statement(c.Request.Context(), subject.UserID, year, month)
  49. if err != nil {
  50. response.ErrorFrom(c, err)
  51. return
  52. }
  53. response.Success(c, stmt)
  54. }
  55. // Export 用户用量 CSV:?start=RFC3339&end=RFC3339(≤31 天)。
  56. // GET /billing/export
  57. func (h *BillingExportHandler) Export(c *gin.Context) {
  58. if !h.requireService(c) {
  59. return
  60. }
  61. subject, ok := middleware2.GetAuthSubjectFromContext(c)
  62. if !ok {
  63. response.Unauthorized(c, "Unauthorized")
  64. return
  65. }
  66. start, end, ok := billingExportRange(c)
  67. if !ok {
  68. return
  69. }
  70. data, filename, err := h.service.ExportCSV(c.Request.Context(), subject.UserID, start, end)
  71. if err != nil {
  72. response.ErrorFrom(c, err)
  73. return
  74. }
  75. c.Header("Content-Disposition", "attachment; filename="+filename)
  76. c.Data(200, "text/csv; charset=utf-8", data)
  77. }
  78. // AdminStatement 管理员查任意用户月账单。
  79. // GET /api/v1/admin/billing/users/:userId/statement?year=&month=
  80. func (h *BillingExportHandler) AdminStatement(c *gin.Context) {
  81. if !h.requireService(c) {
  82. return
  83. }
  84. userID, err := strconv.ParseInt(c.Param("userId"), 10, 64)
  85. if err != nil || userID <= 0 {
  86. response.BadRequest(c, "Invalid user ID")
  87. return
  88. }
  89. now := time.Now().UTC()
  90. year, month := now.Year(), int(now.Month())
  91. if raw := c.Query("year"); raw != "" {
  92. if v, err := strconv.Atoi(raw); err == nil {
  93. year = v
  94. }
  95. }
  96. if raw := c.Query("month"); raw != "" {
  97. if v, err := strconv.Atoi(raw); err == nil {
  98. month = v
  99. }
  100. }
  101. stmt, err := h.service.Statement(c.Request.Context(), userID, year, month)
  102. if err != nil {
  103. response.ErrorFrom(c, err)
  104. return
  105. }
  106. response.Success(c, stmt)
  107. }
  108. // AdminExport 管理员导出任意用户用量 CSV。
  109. // GET /api/v1/admin/billing/users/:userId/export?start=&end=
  110. func (h *BillingExportHandler) AdminExport(c *gin.Context) {
  111. if !h.requireService(c) {
  112. return
  113. }
  114. userID, err := strconv.ParseInt(c.Param("userId"), 10, 64)
  115. if err != nil || userID <= 0 {
  116. response.BadRequest(c, "Invalid user ID")
  117. return
  118. }
  119. start, end, ok := billingExportRange(c)
  120. if !ok {
  121. return
  122. }
  123. data, filename, err := h.service.ExportCSV(c.Request.Context(), userID, start, end)
  124. if err != nil {
  125. response.ErrorFrom(c, err)
  126. return
  127. }
  128. c.Header("Content-Disposition", "attachment; filename="+filename)
  129. c.Data(200, "text/csv; charset=utf-8", data)
  130. }
  131. func billingExportRange(c *gin.Context) (time.Time, time.Time, bool) {
  132. end := time.Now().UTC()
  133. start := end.AddDate(0, 0, -7)
  134. if raw := c.Query("start"); raw != "" {
  135. v, err := time.Parse(time.RFC3339, raw)
  136. if err != nil {
  137. response.BadRequest(c, "Invalid start, use RFC3339")
  138. return time.Time{}, time.Time{}, false
  139. }
  140. start = v
  141. }
  142. if raw := c.Query("end"); raw != "" {
  143. v, err := time.Parse(time.RFC3339, raw)
  144. if err != nil {
  145. response.BadRequest(c, "Invalid end, use RFC3339")
  146. return time.Time{}, time.Time{}, false
  147. }
  148. end = v
  149. }
  150. return start, end, true
  151. }