statistics.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package http
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/render"
  9. )
  10. var (
  11. _layout = "2006-01-02"
  12. )
  13. func statisGraph(c *bm.Context) {
  14. v := new(struct {
  15. Type int64 `form:"type"`
  16. TagID string `form:"tag_id" validate:"required"`
  17. Compare int `form:"compare"`
  18. })
  19. if err := c.Bind(v); err != nil {
  20. return
  21. }
  22. data, err := svr.StatisGraph(c, v.Type, v.TagID, v.Compare)
  23. if err != nil {
  24. log.Error("svr.StatisGraph error(%v)", err)
  25. c.JSON(nil, err)
  26. return
  27. }
  28. c.JSON(data, nil)
  29. }
  30. func statisList(c *bm.Context) {
  31. v := new(struct {
  32. Type int64 `form:"type"`
  33. TagID string `form:"tag_id" validate:"required"`
  34. Compare int `form:"compare"`
  35. })
  36. if err := c.Bind(v); err != nil {
  37. return
  38. }
  39. data, err := svr.StatisList(c, v.Type, v.TagID, v.Compare)
  40. if err != nil {
  41. log.Error("svr.StatisList error(%v)", err)
  42. c.JSON(nil, err)
  43. return
  44. }
  45. c.JSON(data, nil)
  46. }
  47. func statisExport(c *bm.Context) {
  48. v := new(struct {
  49. Type int64 `form:"type"`
  50. TagID string `form:"tag_id" validate:"required"`
  51. Compare int `form:"compare"`
  52. })
  53. if err := c.Bind(v); err != nil {
  54. return
  55. }
  56. content, err := svr.StatisExport(c, v.Type, v.TagID, v.Compare)
  57. if err != nil {
  58. log.Error("svr.StatisExport error(%v)", err)
  59. c.JSON(nil, err)
  60. return
  61. }
  62. c.Render(http.StatusOK, CSV{
  63. Content: content,
  64. Title: fmt.Sprintf("%s-%s", time.Now().Format("2006-01-02"), "statistics"),
  65. })
  66. }
  67. func ascList(c *bm.Context) {
  68. v := new(struct {
  69. Type string `form:"type" validate:"required"`
  70. Tags []int64 `form:"tag_ids,split" validate:"required"`
  71. Date string `form:"date" validate:"required"`
  72. ScoreMin int `form:"score_min"`
  73. ScoreMax int `form:"score_max"`
  74. MID int64 `form:"mid"`
  75. From int `form:"from" default:"0" validate:"min=0"`
  76. Limit int `form:"limit" default:"20" validate:"min=1"`
  77. })
  78. if err := c.Bind(v); err != nil {
  79. return
  80. }
  81. date, err := time.Parse(_layout, v.Date)
  82. if err != nil {
  83. return
  84. }
  85. total, data, err := svr.GetTrendAsc(c, v.Type, v.Tags, date, v.ScoreMin, v.ScoreMax, v.MID, v.From, v.Limit)
  86. if err != nil {
  87. log.Error("svr.GetTrendAsc error(%v)", err)
  88. c.JSON(nil, err)
  89. return
  90. }
  91. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  92. "code": 0,
  93. "message": "0",
  94. "data": data,
  95. "paging": map[string]int{
  96. "page_size": v.Limit,
  97. "total": total,
  98. },
  99. }))
  100. }
  101. func descList(c *bm.Context) {
  102. v := new(struct {
  103. Type string `form:"type" validate:"required"`
  104. Tags []int64 `form:"tag_ids,split" validate:"required"`
  105. Date string `form:"date" validate:"required"`
  106. ScoreMin int `form:"score_min"`
  107. ScoreMax int `form:"score_max"`
  108. MID int64 `form:"mid"`
  109. From int `form:"from" default:"0" validate:"min=0"`
  110. Limit int `form:"limit" default:"20" validate:"min=1"`
  111. })
  112. if err := c.Bind(v); err != nil {
  113. return
  114. }
  115. date, err := time.Parse(_layout, v.Date)
  116. if err != nil {
  117. return
  118. }
  119. total, data, err := svr.GetTrendDesc(c, v.Type, v.Tags, date, v.ScoreMin, v.ScoreMax, v.MID, v.From, v.Limit)
  120. if err != nil {
  121. log.Error("svr.GetTrendDesc error(%v)", err)
  122. c.JSON(nil, err)
  123. return
  124. }
  125. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  126. "code": 0,
  127. "message": "0",
  128. "data": data,
  129. "paging": map[string]int{
  130. "page_size": v.Limit,
  131. "total": total,
  132. },
  133. }))
  134. }